Skip to content

Instantly share code, notes, and snippets.

@roktas
Created May 22, 2014 21:56
Show Gist options
  • Save roktas/c4f515c29ddecb354b7c to your computer and use it in GitHub Desktop.
Save roktas/c4f515c29ddecb354b7c to your computer and use it in GitHub Desktop.
#!/bin/bash
# Setup database
: ${database_urls=mysql://vagrant:vagrant@localhost/vagrant}
: ${database_password=vagrant}
parseurl() {
eval "unset $1; declare -Ag $1=($(
python <<-EOF
import urlparse
url = urlparse.urlparse(u'$2')
name = url.path[1:]
print """
[scheme]='{url.scheme}'
[user]='{url.username}'
[password]='{url.password}'
[host]='{url.hostname}'
[port]='{url.port}'
[name]='{name}'
""".format(url=url, name=name)
EOF
))"
}
for url in $database_urls; do
parseurl db "$url"
case ${db[scheme]} in
mysql)
if [[ -z $mysql_initialized ]]; then
mysqladmin -u root password "$database_password"
mysql_initialized=yes
fi
mysql -uroot -p"$database_password" <<-sql
create user '${db[user]}'@'${db[host]}' identified by '${db[password]}';
create database ${db[name]} default character set utf8 collate utf8_unicode_ci;
grant all privileges on ${db[name]}.* to '${db[user]}'@'${db[host]}';
flush privileges;
sql
;;
postgres)
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment