tl;dr blog

OpenPGP issue while adding repository key

Problem:

1
2
3
4
5
6
7
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /tmp/tmp.RP2VCnbOKX --trustdb-name /etc/apt/trustdb.gpg --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver keyserver.ubuntu.com --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
gpg: requesting key A88D21E9 from hkp server keyserver.ubuntu.com
?: keyserver.ubuntu.com: Connection refused
gpgkeys: HTTP fetch error 7: couldn't connect: Connection refused
gpg: no valid OpenPGP data found.
gpg: Total number processed: 0

Solution:

1
2
3
4
5
6
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /tmp/tmp.wCvQD0DLqr --trustdb-name /etc/apt/trustdb.gpg --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
gpg: requesting key A88D21E9 from hkp server keyserver.ubuntu.com
gpg: key A88D21E9: public key "Docker Release Tool (releasedocker) <docker@dotcloud.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

via Patrick.

Removing files of a certain size

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ find -maxdepth 1 -type f -size 5762c -print0 | xargs -0 rm -f

-size n[cwbkMG]

    File uses n units of space. The following suffixes can be used:

    `b'    for 512-byte blocks (this is the default if no suffix  is
                                used)

    `c'    for bytes

    `w'    for two-byte words

    `k'    for Kilobytes       (units of 1024 bytes)

    `M'    for Megabytes    (units of 1048576 bytes)

    `G'    for Gigabytes (units of 1073741824 bytes)

    The size does not count indirect blocks, but it does count
    blocks in sparse files that are not actually allocated. Bear in
    mind that the `%k' and `%b' format specifiers of -printf handle
    sparse files differently. The `b' suffix always denotes
    512-byte blocks and never 1 Kilobyte blocks, which is different
    to the behaviour of -ls.

Disabling Nginx cache for a folder

Edit /etc/nginx/sites-enabled/site/your_website:

1
2
3
4
5
6
7
8
server {
  location /static/ {
    root /var/www/data/;
    expires -1;
    add_header Cache-Control private;
    add_header Max-Age 0;
  }
}

Details here.

Splitting PEM file in Python

1
2
3
4
5
6
7
8
9
import pem # https://github.com/hynek/pem
import sys

with open(sys.argv[1], 'rb') as f:
  certificates = pem.parse(f.read())
  for idx, val in enumerate(certificates):
      f = open('cert-{0}.crt'.format(idx), 'w')
      f.write(str(val))
      f.close()

Handy Django snippets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Debug with PDB
import ipdb; ipdb.set_trace()

# Debug with Pyringe
import pyringe; pyringe.interact()

# Print to error stream
import sys; print >>sys.stderr, 'message'

# Print call stack
import traceback; traceback.print_stack()

# Dump an object
from pprint import pprint; pprint (vars(your_object))

# Force rollback
from django.db import transaction, connection
transaction.rollback()
connection._rollback()

# Print SQL queries
import logging
l = logging.getLogger('django.db.backends')
l.setLevel(logging.DEBUG)
l.addHandler(logging.StreamHandler())

Useful Git log aliases

1
2
3
[alias]
  lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
  lg-json = log --pretty=format:'\"%h\": {%n \"commit\": \"%H\",%n \"author\": \"%an <%ae>\",%n \"date\": \"%ad\",%n \"message\": \"%s\"%n}'

Unlocking files while embedding Jetty

Open the jetty-webapp artifact from m2repo/org/eclipse/jetty, edit webdefault.xml found on org.eclipse.jetty.webapp package, then set useFileMappedBuffer to false:

1
2
3
4
<init-param>
  <param-name>useFileMappedBuffer</param-name>
  <param-value>false</param-value>
</init-param>

Generating an SSL keystore for a website

First download the website’s certificate from Firefox:

View Page Info > Security > View Certificate > Details > Export >
...\jre1.6.0_33\lib\security\website.crt

Then generate the keystore:

$ cd ...\jre1.6.0_33
$ bin\keytool.exe -noprompt -import -keystore website.keystore -alias website -file lib\security\website.crt
Enter keystore password:
Re-enter new password:
Certificate was added to keystore