Skip to content

Instantly share code, notes, and snippets.

Tarek Ziade tarekziade

View GitHub Profile
@tarekziade
tarekziade / mutation.js
Created Mar 21, 2018
Demonstrates how to detect when a button gets enabled
View mutation.js
/* Like for the IntersectionObserver, we're using here the
* Mutation observer to detect when an element attribute is changed
* by some Javascript on the page.
*
* use case: a button is enabled
*/
function watchMutation(selector, condition) {
var target = document.querySelector(selector);
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
View send_code_metrics.py
import functools
import logging
import graypy
import json
import time
import random
from collections import defaultdict, deque
from flask import Flask, jsonify, g
app = Flask(__name__)
@tarekziade
tarekziade / marionette.py
Last active Apr 8, 2017
async marionette calls
View marionette.py
# see https://dxr.mozilla.org/mozilla-central/source/testing/marionette/driver.js?from=testing%2Fmarionette%2Fdriver.js#2957
import functools
import json
from asyncio import open_connection
import asyncio
from molotov import *
class Marionette(object):
def __init__(self, host='localhost', port=2828, loop=None):
@tarekziade
tarekziade / grum.py
Last active Apr 5, 2017
poutine test
View grum.py
import asyncio
import configparser
import pytest
from smwogger import API
@pytest.fixture
def conf():
config = configparser.ConfigParser()
config.read('manifest.ini')
View call_hb.py
import asyncio
from smwogger import API
async def run_heartbeat():
async with API('__api__.json') as api:
res = await api.__heartbeat__()
body = await res.json()
print(body)
@tarekziade
tarekziade / check_redirect.py
Created Mar 18, 2017
White list any 302 locations
View check_redirect.py
from flask import make_response
from urllib.parse import urlparse
# domain:port
SAFE_DOMAINS = ['ziade.org:443']
@app.after_request
def check_redirect(response):
if response.status_code != 302:
@tarekziade
tarekziade / api.py
Last active Aug 1, 2018
connexion as an extension
View api.py
import pathlib
from connexion.api import Api, Resolver
import werkzeug.exceptions
def swagger_blueprint(specification, specification_dir=None, validate_responses=False,
strict_validation=False, resolver_error=None,
base_path='', debug=False):
resolver = Resolver()
swagger_json = False
@tarekziade
tarekziade / async_workers.py
Created Jan 9, 2017
Async Workers using Threads
View async_workers.py
import requests
import asyncio
import sys
from functools import partial
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
loop = asyncio.get_event_loop()
@tarekziade
tarekziade / detect.rst
Created Oct 14, 2016
Detect and kill an idling loads ec2 instance
View detect.rst

On the EC2 instance A few variable to set:

Run a docker with the docker sock linked and a few variables set:

docker run -t -v /var/run/docker.sock:/run/docker.sock -v $(which docker):/bin/docker  -i ubuntu /bin/bash
apt-get update && apt-get install -y python-pip && pip install docker-py==1.6.0

export CID=`cat /proc/self/cgroup | grep -o  -e "docker-.*.scope" | head -n 1 | sed "s/docker-\(.*\).scope/\\1/"`
@tarekziade
tarekziade / distribution.py
Last active Jun 4, 2016
Consistent Distribution of users across servers
View distribution.py
""" Consistent load-balancing.
We have a few servers and we want a load-balancer to
distribute incoming requests across them in a deterministic
and consistent way - without keeping any counter to make the
decision.
Removing a backend server should not impact users on other
servers.
You can’t perform that action at this time.