Skip to content

Instantly share code, notes, and snippets.

@mikekunze
Created October 24, 2013 13:36
Show Gist options
  • Save mikekunze/7137464 to your computer and use it in GitHub Desktop.
Save mikekunze/7137464 to your computer and use it in GitHub Desktop.
This coffee script is meant to be used with a docker container running drupal instances. It starts mysql, httpd, postfix, and waits for mysql to boot up before creating the drupal database.
spawn = require('child_process').spawn
dohttpd = ()->
httpd = spawn '/etc/init.d/apache2', ['start']
httpd.stdout.setEncoding 'utf8'
httpd.stderr.setEncoding 'utf8'
httpd.stdout.on 'data', (data)->
console.log data
httpd.stderr.on 'data', (data)->
console.log data
domysql = ()->
mysql = spawn '/usr/sbin/mysqld'
mysql.stdout.setEncoding 'utf8'
mysql.stderr.setEncoding 'utf8'
mysql.stdout.on 'data', (data)->
console.log data
mysql.stderr.on 'data', (data)->
console.log data
dopostfix = ()->
postfix = spawn '/etc/init.d/postfix', ['start']
postfix.stdout.setEncoding 'utf8'
postfix.stderr.setEncoding 'utf8'
postfix.stdout.on 'data', (data)->
console.log data
postfix.stderr.on 'data', (data)->
console.log data
# Here we are waiting on mysql
net = require 'net'
tryCount = 0
checkMysqlPortAndDoCreateDb = ()->
sock = new net.Socket()
sock.connect(3306, '127.0.0.1')
sock.on 'connect', ()->
console.log 'connection to mysql succeeded, creating database'
docreatedb()
sock.destroy()
sock.on 'error', (e)->
if tryCount > 10
console.log 'giving up trying to create drupal table'
else
tryCount++
setTimeout checkMysqlPortAndDoCreateDb, 350
console.log e
sock.on 'timeout', (e)->
console.log e
docreatedb = ()->
createdb = spawn 'mysql', ['-uroot', '-e create database drupal']
createdb.stdout.setEncoding 'utf8'
createdb.stderr.setEncoding 'utf8'
createdb.stdout.on 'data', (data)->
console.log data
createdb.stderr.on 'data', (data)->
console.log data
dohttpd()
domysql()
dopostfix()
checkMysqlPortAndDoCreateDb()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment