Skip to content

Instantly share code, notes, and snippets.

View fermayo's full-sized avatar

Fernando Mayo fermayo

View GitHub Profile
@fermayo
fermayo / cleanup-completed-jobs.sh
Created April 23, 2019 14:54
Deletes all completed jobs from all namespaces in a Kubernetes cluster
for NAMESPACE in $(kubectl get namespaces -o jsonpath='{.items[*].metadata.name}'); do
kubectl -n $NAMESPACE delete job $(kubectl -n $NAMESPACE get jobs -o jsonpath='{.items[?(@.status.completionTime)].metadata.name}')
done
@fermayo
fermayo / depaginate.py
Created February 23, 2018 02:54
Little function to repaginate APIs that use 'Link' headers (i.e. GitHub API v3)
import requests
def depaginate(initial_url, **kwargs):
r = requests.get(initial_url, **kwargs)
r.raise_for_status()
yield r.json()
while r.links.get('next'):
r = requests.get(r.links.get('next'), **kwargs)
r.raise_for_status()
yield r.json()
@fermayo
fermayo / Dockerfile
Created January 24, 2018 03:11
Automatically configure nginx to trust AWS Cloudfront IPs present in X-Fowarded-For
FROM alpine:latest AS cloudfront
RUN apk --no-cache add curl jq
RUN curl https://ip-ranges.amazonaws.com/ip-ranges.json | \
jq -r '.prefixes[] | select(.service=="CLOUDFRONT") | .ip_prefix' | \
xargs -I '{}' echo 'set_real_ip_from {};' > /cloudfront.conf && \
echo 'real_ip_header X-Forwarded-For;' >> /cloudfront.conf && \
echo 'real_ip_recursive on;' >> /cloudfront.conf
FROM nginx:latest
COPY --from=cloudfront /cloudfront.conf /etc/nginx/conf.d/cloudfront.conf
@fermayo
fermayo / tasks.py
Last active July 4, 2019 15:31
Generic Celery task that calls Django model methods asynchronously
import importlib
from functools import wraps
from myproject.celery import app
@app.task
def call_async_task(obj_module_name, obj_class_name, obj_pk, obj_method, obj_args=None, obj_kwargs=None):
model_class = getattr(importlib.import_module(obj_module_name), obj_class_name)
obj = model_class.objects.get(pk=obj_pk)
method = getattr(obj, obj_method)
@fermayo
fermayo / lambda_function.py
Last active April 1, 2020 10:37
Lambda function to be called from CodePipeline to start and wait for a one-off ECS task
# Required env vars:
# $CLUSTER: name of the ECS cluster (i.e. `myapp-prod`)
# $TASK_DEFINITION: name and revision of the task definition (i.e. `mytask:1`)
# $SUBNETS: comma-separated list of subnets to place the new task (i.e. `sg-12345678,sg-abcdef12`)
# $SECURITY_GROUPS: comma-separated list of security groups to be used for the new task (i.e. `subnet-12345678`)
import boto3
import os
import traceback
@fermayo
fermayo / lambda_function.py
Last active February 9, 2023 09:23
Lambda function to trigger a one-off ECS Fargate task
# Adapted from https://lobster1234.github.io/2017/12/03/run-tasks-with-aws-fargate-and-lambda/
import boto3
import os
def lambda_handler(event,context):
client = boto3.client('ecs')
response = client.run_task(
cluster=os.getenv('CLUSTER'),
launchType=os.getenv('LAUNCH_TYPE', 'FARGATE'),
@fermayo
fermayo / middleware.py
Created January 18, 2018 18:50
nginx-like access logging middleware for Django 2.0
import logging
import datetime
def access_logging(get_response):
logger = logging.getLogger("access_logging")
def middleware(request):
response = get_response(request)
log_format = '{remote_addr} - {remote_user} [{time_local}] "{request}" {status} {body_bytes_sent} ' \
REPO="dockercloud/client-proxy"
alias notary='notary -s https://notary.docker.io -d ~/.docker/trust'
# First you need to import the root key for Docker Cloud
notary key import 70340602d65cb8b39db81ca680269d93272b0925e279ec171c0f33d165977405.key
# Create target key for $REPO
notary init docker.io/$REPO
notary key rotate docker.io/$REPO snapshot -r
@fermayo
fermayo / hello-world.yml
Last active November 18, 2015 14:23
training
lb:
image: tutum/haproxy
ports:
- "80:80"
links:
- helloworld
roles:
- global
helloworld:
image: tutum/hello-world
@fermayo
fermayo / patch-ubuntu_14.04.1-ixgbevf-2.16.1-kcompat.h.patch
Last active December 3, 2015 18:46 — forked from defila-aws/patch-ubuntu_14.04.1-ixgbevf-2.16.1-kcompat.h.patch
patch-ubuntu_14.04.1-ixgbevf-2.16.1-kcompat.h.patch
--- /usr/src/ixgbevf-2.16.1/src/kcompat.h.orig 2015-02-03 18:34:22.901474028 +0000
+++ /usr/src/ixgbevf-2.16.1/src/kcompat.h 2015-02-03 18:35:32.577440102 +0000
@@ -3219,8 +3219,6 @@
#define u64_stats_update_begin(a) do { } while(0)
#define u64_stats_update_end(a) do { } while(0)
#define u64_stats_fetch_begin(a) do { } while(0)
-#define u64_stats_fetch_retry_bh(a) (0)
-#define u64_stats_fetch_begin_bh(a) (0)
#if (RHEL_RELEASE_CODE && RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(6,1))