Flask-SQLAlchemy ssl-connection

It took some time before realising that client certs and keys can be specified on the connection uri while setting config values. Like this:

SQLALCHEMY_DATABASE_URI: mysql://user:password@hostname/db?ssl_key=ssl/client-key.pem&ssl_cert=ssl/client-cert.pem

Where ssl/ is a relative path from the calling app.

Later on you can confirm that the connection really was over ssl with something like:

res = db.session.execute("SHOW STATUS LIKE '%ssl%'")
for r in res:
print r

You’ll then see some info regarding your connection.

Initialize WordPress development

This is just something I put together to jumpstart WordPress development.

#!/bin/bash
echo "$(tput setaf 2)Creating directories.$(tput sgr0)"
NEWDIR=$1
if [ -d $NEWDIR ]; then
echo "$(tput setaf 1)FAIL! Directory exists.$(tput sgr0)"
exit 1
fi
mkdir -p $NEWDIR/{htdocs,log}
cd $NEWDIR
echo ""
echo "$(tput setaf 2)Initializing git repo and adds wp as submodule.$(tput sgr0)"
git init
git submodule add git://github.com/WordPress/WordPress.git htdocs/wp
git submodule init
git submodule update
echo ""
echo "$(tput setaf 2)Fetching some stuff from Mark Jaquiths Skeleton repo.$(tput sgr0)"
wget https://raw.github.com/markjaquith/WordPress-Skeleton/master/wp-config.php -O htdocs/wp-config.php
wget https://raw.github.com/markjaquith/WordPress-Skeleton/master/local-config-sample.php -O htdocs/local-config.php
echo ""
echo "$(tput setaf 2)Add some .gitignore stuff.$(tput sgr0)"
cat >htdocs/.gitignore <<EOL
*.swp
htdocs/local-config.php
htdocs/content/*
!htdocs/content/upload
EOL
echo ""
echo "$(tput setaf 2)First init.$(tput sgr0)"
git commit -am "Autoinit"
echo ""
echo "$(tput setaf 2)DONE!$(tput sgr0)"

Set mtime with part of filename

Needed to go through a directory of backuped files and set the mtime. The files had the format x-yymmdd.sql.gz A recent copying of the directory had removed the original mtime attributes (note to self: use rsync with -a flag next time).

This one liner did the trick of converting them back to what they had:

for f in *.sql.gz; do touch -d $(echo $f | \
sed "s/^\w\+-\([0-9]\{2\}\)\([0-9]\{2\}\)
\([0-9]\{2\}\).*/20\1-\2-\3/") $f; done

Could this be improved? Most certainly. I havent figured out how to do repetitions in a good manner in regex

tracd with upstart on Ubuntu

After spending too many hours trying to get tracd running as a daemon on Ubuntu 10.04 I might as well share my experiences. At first I begun with  some init.d scripts I found googling for tracd init.d. The problem with them was that they were made to run tracd as root which frankly didn’t feel that great. I tried to modify them to use the –user/–chuid options to no avail. In the end I understood that the problem was not those options but that the pid file I was trying to use with the tracd command for some reason couldn’t be written.

These mishaps wasn’t only bad as I therefor started fiddling with upstart. upstart is a much more pleasant experience than classical init scripts. All I had to do was to create the file

/etc/init/tracd.conf

and put

description "tracd server"
author "KO"

start on startup
stop on shutdown

expect daemon

script
        exec sudo -u www-data /usr/bin/tracd --daemonize --port=8000 \
	--hostname=127.0.0.1 -s /path/to/trac/project
end script

in it.

The key here was the expect daemon stanza which saved my day. After that you can use start|stop|status processname like:

sudo start tracd

Now you might wonder why I wanted to run tracd as a daemon. I was about to switch to nginx on the server running trac and the daemon way seemed the easiest.