Skip to content

Instantly share code, notes, and snippets.

@jpetazzo
jpetazzo / Sample result.txt
Created August 17, 2011 22:13
Compare the available nginx versions and their modules (according to debian packages descriptions)
nginx-full nginx-extras nginx-light
Access YES YES YES
Addition File AIO Mail Core - YES -
Addition Mail Core YES - -
Auth Basic YES YES YES
Auto Index YES YES YES
Browser YES YES -
Charset YES YES YES
Echo YES YES -
Embedded Lua - YES -
@jpetazzo
jpetazzo / spotify.sh
Created August 19, 2011 06:53
If Spotify crashes right after start, try this.
#!/bin/sh
# Hopefully fixes the infamous "Spotify crashes immediately on startup" bug.
# In fact, it does not crash immediately; it seems to be crashing while
# looking for local files. The first time you start Spotify after the boot
# of your machine, you will have maybe 10 seconds before it crashes. Then,
# after each start, it will crash almost instantly (less than one second on
# my box).
#
# The following workaround replaces one of Spotify's configuration files
# with a FIFO, effectively preventing reading from the file. Then, it writes
@jpetazzo
jpetazzo / authproxy.py
Created October 13, 2011 05:29
WSGI Proxy App to add HTTP Basic Auth to outbound requests
HTTPUSER = 'mylogin'
HTTPPASS = 'mypassword'
from wsgiproxy.app import WSGIProxyApp
from base64 import encodestring
proxyapp = WSGIProxyApp('http://remote.server.domain.com/')
# Craft Basic Auth header. Don't forget to strip the trailing \n.
HTTPAUTH = 'Basic ' + encodestring(HTTPUSER+':'+HTTPPASS).strip()
@jpetazzo
jpetazzo / README
Created December 7, 2011 10:13
Get an "admin" port suitable for long-running HTTP connections (for e.g. big imports that take more than 1 minute)
Edit your dotcloud.yml file to add the "ports" section as shown in the sample dotcloud.yml.
Add a supervisord.conf to start the socat program.
Push your code. Use "dotcloud info" to retrieve the host+port to be used for your admin connection (it's conveniently named "admin" since it's what we requested in dotcloud.yml). Enjoy!
Note: this won't work with the custom service, but can be easily adapted if needed.
# Find all minutes during which there was at least one "502" status code:
grep ' 502 ' http.log | awk '{print $4}' | cut -d: -f1-3 | uniq
# Find all minutes during which there were valid requests (excluding 502 and 400 status codes):
grep -v ' 502 ' http.log | grep -v ' 400 ' | awk '{print $4}' | cut -d: -f1-3 | uniq
# If you want hours instead of minutes, replace f1-3 by f1-2.
@jpetazzo
jpetazzo / builder.sh
Created January 5, 2012 09:10
dotCloud builder for Cromlech
#!/bin/bash
[ -d ~/bin ] || virtualenv ~
. ~/bin/activate
cp -a . ~
cd
python bootstrap.py
sed -i 's/port = 8080/port = 42800/g' base.cfg # ugly hack
buildout
@jpetazzo
jpetazzo / pulseaudio-volume-control.py
Created January 7, 2012 22:24
Little script that I use to change the Pulseaudio volume with my keyboard bindings
#!/usr/bin/env python
import commands, sys, os
padump = commands.getoutput('pacmd dump').split('\n')
if sys.argv[1]=='toggle':
for muteline in padump:
if 'set-sink-mute' in muteline:
cmd, src, val = muteline.split()
val = {'yes':'no', 'no':'yes'}[val]
os.system('pacmd {cmd} {src} {val} >/dev/null'.format(**locals()))
else:
@jpetazzo
jpetazzo / rrdgauge.sh
Created February 4, 2012 06:43
Example of rrdtool GAUGE to store a value between 0 and 100
#!/bin/sh
rrdtool create foo.rrd --start -2h --step 900 DS:foo:GAUGE:3600:0:100 RRA:AVERAGE:0.5:1:1000
for N in $(seq 20 -1 1)
do
rrdtool update foo.rrd -- -$(($N*600)):$N$N
rrdtool lastupdate foo.rrd
done
rrdtool graph --start -1h foo.png DEF:foo=foo.rrd:foo:AVERAGE LINE:foo#000000:thefoo
rrdtool fetch foo.rrd AVERAGE | tail
@jpetazzo
jpetazzo / README
Created February 10, 2012 11:00
all-in-one ruby + memcached + jetty
Use this like the "ruby" service: it will install dependencies from Gemfile, and look for a Rails/Rack environment to serve it with passenger.
However, it will also run:
- a local memcached, on port 11211
- a local jetty configured to deploy WAR files, with servlets & JSP support, on port 8080
Place WAR files in the webapps directory as in this example.
If you need to add worker processes, add them to dotcloud.yml, in the processes section.
#!/usr/bin/env python
A = [1, 8, -3, 0, 1, 3, -2, 4, 5]
def complementary_pairs(K, A):
buckets = {}
for x in A:
if x not in buckets:
buckets[x] = 0
buckets[x] += 1