Skip to content

Instantly share code, notes, and snippets.

@ReallyLiri
ReallyLiri / injectable_resource.py
Created July 11, 2019 08:47
Flask resource with http method logic injection capability
from functools import wraps
from flask_restplus import Resource
HTTP_METHODS = ["get", "post", "delete", "put", "patch"]
class InjectableResource(Resource):
def hook_all_http_methods(self, pre_execution=None, post_execution=None, exception_handler=None):
for method in HTTP_METHODS:
@ReallyLiri
ReallyLiri / ng-routing.interceptor.ts
Created July 10, 2019 07:51
Routing Interceptor for Angular7
import { Router, NavigationStart } from '@angular/router';
import { filter, pairwise } from 'rxjs/operators';
export function parseQuery(queryString: string) {
const query = {};
const pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&');
for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i].split('=');
query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
@ReallyLiri
ReallyLiri / gke-svc-ext-ip.sh
Last active July 10, 2019 07:41
Get external IP of a service in GKE
# required vars: gcloud-api-key.json, $GCLOUD_PROJECT, $GCLOUD_ZONE, $CLUSTER_NAME, $SERVICE_NAME
gcloud auth activate-service-account --key-file gcloud-api-key.json
gcloud config set project $GCLOUD_PROJECT
gcloud config set compute/zone $GCLOUD_ZONE
lines=`gcloud container clusters list --filter NAME="$CLUSTER_NAME" | wc -l`
if [ $lines -eq 0 ] ; then echo "No deployed cluster for $CLUSTER_NAME" && exit 1 ; fi
gcloud container clusters get-credentials $CLUSTER_NAME
external_ip=$(kubectl get svc $SERVICE_NAME --template="{{range .status.loadBalancer.ingress}}{{.ip}}{{end}}")
echo "IP for cluster $CLUSTER_NAME is $external_ip"
@ReallyLiri
ReallyLiri / uwsgi-nginx: Dockerfile
Last active July 3, 2019 10:21
Make tiangolo/uwsgi-nginx crash on python app failure
FROM tiangolo/uwsgi-nginx:python3.7
# or FROM tiangolo/uwsgi-nginx-flask:python3.7
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY stop-supervisor.sh /etc/supervisor/stop-supervisor.sh
RUN chmod +x /etc/supervisor/stop-supervisor.sh
@ReallyLiri
ReallyLiri / process.py
Created June 30, 2019 12:10
Run external process from python
import gzip
import shutil
import logging
import os
from subprocess import Popen, PIPE, TimeoutExpired
SUCCESS_CODE = 0
TIMEOUT_SECONDS = 600
@ReallyLiri
ReallyLiri / safe_contextlib.py
Created June 30, 2019 12:07
safe_contextmanager
import sys
from functools import wraps
from contextlib import ContextDecorator, AbstractContextManager
# Copied and changed from python3.6/contextlib.py
def safe_contextmanager(func):
@wraps(func)
def helper(*args, **kwds):