Skip to content

Instantly share code, notes, and snippets.

View hvelarde's full-sized avatar

Héctor Velarde hvelarde

View GitHub Profile
@hvelarde
hvelarde / api_hacks.py
Created April 6, 2016 16:22
Hacks to work around API inconsistencies between Archetypes and Dexterity
# -*- coding: utf-8 -*-
"""Hacks to work around API inconsistencies between Archetypes and Dexterity."""
def set_image_field(obj, image, content_type):
"""Set image field in object on both, Archetypes and Dexterity."""
from plone.namedfile.file import NamedBlobImage
try:
obj.setImage(image) # Archetypes
except AttributeError:
@hvelarde
hvelarde / spoof.py
Last active February 28, 2018 13:19
Spoofing request when running Zope from the command line
from plone import api
from zope.component.hooks import setSite
import transaction
def spoof_request(app):
from AccessControl.SecurityManagement import newSecurityManager
from AccessControl.SecurityManager import setSecurityPolicy
from Testing.makerequest import makerequest
@hvelarde
hvelarde / varnish
Last active November 23, 2016 17:14
Varnish 4 configuration for Plone
# Configuration file for varnish
#
# /etc/init.d/varnish expects the variables $DAEMON_OPTS, $NFILES and $MEMLOCK
# to be set from this shell script fragment.
#
# This file should be symlinked to /etc/default/varnish
# Should we start varnishd at boot? Set to "no" to disable.
START=yes
@hvelarde
hvelarde / default.vcl
Last active November 23, 2016 18:36
Varnish 4 configuration for Plone
# VCL configuration file for Varnish. It defines 2 backends, one for
# each Plone instance, and uses hash load balancing.
#
# This file must me symlinked to /etc/varnish/default.vcl
vcl 4.0;
import directors;
import std;
@hvelarde
hvelarde / nginx.conf
Created November 23, 2016 17:13
nginx configuration for Plone
# Configuration file for nginx
#
# This file must me symlinked to /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 4096;
@hvelarde
hvelarde / default
Created November 23, 2016 17:17
nginx configuration for Plone
# Configuration file for nginx
#
# This file must me symlinked to /etc/nginx/sites-enabled/default
upstream plone {
server localhost:8081;
server localhost:8082;
}
server {
@hvelarde
hvelarde / restart
Created November 24, 2016 15:04
Part to restart instances with warm up
# need to change permission access to /etc/varnish/secret
[restart]
recipe = collective.recipe.template
input = inline:
#!/bin/bash
for i in {1..4}
do
varnishadm backend.set_health instance$i sick
${buildout:bin-directory}/supervisorctl restart app:instance$i
@hvelarde
hvelarde / supervisor.cfg
Created December 9, 2016 22:53
Typical buildout configuration to add support for Supervisor process control system
[buildout]
parts +=
supervisor
supervisor-cron
[supervisor]
recipe = collective.recipe.supervisor
plugins = superlance
user = admin
password = admin
# get total requests by status code
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -rn
# get top requesters by IP
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head | awk -v OFS='\t' '{"host " $2 | getline ip; print $0, ip}'
# get top requesters by user agent
awk -F'"' '{print $6}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head
# get top requests by URL
@hvelarde
hvelarde / capture.py
Created July 5, 2017 12:31
A context manager to capture stdout and stderr
import contextlib
@contextlib.contextmanager
def capture():
"""A context manager to capture stdout and stderr.
http://stackoverflow.com/a/10743550/644075
"""
import sys
from cStringIO import StringIO