Skip to content

Instantly share code, notes, and snippets.

@krusynth
Created October 13, 2012 22:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krusynth/3886332 to your computer and use it in GitHub Desktop.
Save krusynth/3886332 to your computer and use it in GitHub Desktop.
Builds site directory, database, db user, virtualhost file, and /etc/hosts entry
#!/usr/bin/python
# Base config
basedir = '/Users/YOURUSERNAME/'
user = 'YOURUSERNAME'
# Setup User
import pwd
pw = pwd.getpwnam(user)
uid = pw.pw_uid
gid = pw.pw_gid
# Get name from command line
import argparse
parser = argparse.ArgumentParser(description='Build site architecture.')
parser.add_argument('sitename', metavar='N', type=str,
help='Name of the site')
# parser.add_argument('--sum', dest='accumulate', action='store_const',
# const=sum, default=max,
# help='sum the integers (default: find the max)')
args = parser.parse_args()
sitename = args.sitename+'.localhost';
# Build directory
def builddir(dirpath) :
if not os.path.exists(dirpath) :
# Create local Directory
print 'Making "'+dirpath+'"'
os.mkdir(dirpath, 0775)
os.chown(dirpath, uid, gid)
else :
print 'Exists "'+dirpath+'"'
# make sure directory doesn't already exist
import os
dirpath = basedir+'Sites/'+sitename+'/'
httpdir = dirpath + 'http/'
docsdir = dirpath + 'docs/'
builddir(dirpath)
builddir(docsdir)
builddir(httpdir)
# Create VirtualHost File
virtualhostpath = '/etc/apache2/virtualhosts/'+sitename+'.conf'
if not os.path.exists(virtualhostpath) :
# Create File
print 'Making "'+virtualhostpath+'"'
contents = "<VirtualHost *:80>\n\
ServerName "+sitename+"\n\
DocumentRoot "+httpdir+"\n\
<Directory "+httpdir+">\n\
Options All\n\
AllowOverride All\n\
</Directory>\n\
</VirtualHost>"
fd = os.open(virtualhostpath, os.O_WRONLY|os.O_CREAT, 0644)
os.write(fd, contents)
os.close(fd)
else :
print 'Exists "'+virtualhostpath+'"'
# Add to /etc/hosts
hostspath = '/etc/hosts'
if os.path.exists(hostspath) :
fd = os.open(hostspath, os.O_WRONLY|os.O_APPEND)
contents = "\n127.0.0.1 "+sitename
os.write(fd, contents)
os.close(fd)
# Restart Apache
import subprocess
cmd = ["apachectl", "restart"]
subprocess.check_call(cmd);
# Password generator
import random
def randstr(strlength) :
randstring = ""
chars = range(ord('0'),ord('9')) + range(ord('A'),ord('Z')) + range(ord('a'),ord('z'))
end_range = len(chars) - 1
for i in range(strlength) :
letter = random.randrange(0, end_range)
randstring += chr(chars[letter])
return randstring
# Create DB Info File
mysqlfile = httpdir + 'mysql.conf';
if not os.path.exists(mysqlfile) :
username = args.sitename
password = randstr(8)
dbname = args.sitename + "db"
contents = "username: " + username + "\n\
password: " + password + "\n\
database: " + dbname + "\n\
hostname: localhost\n"
print 'Making "'+mysqlfile+'"'
fd = os.open(mysqlfile, os.O_WRONLY|os.O_CREAT, 0644)
os.write(fd, contents)
os.close(fd)
os.chown(mysqlfile, uid, gid)
# Create Database
# Create DB User
import MySQLdb
db = MySQLdb.connect(host="localhost", user="root", passwd="YOURPASSWORD",db="mysql")
c=db.cursor()
c.execute('SHOW DATABASES')
dbs = [db[0] for db in c.fetchall()]
if dbname in dbs :
print 'Database Exists "' + dbname + '"'
else :
print 'Creating database "' + dbname + '"'
c.execute('CREATE DATABASE ' + dbname)
print 'Creating user "' + username + '"'
c.execute('grant all on ' + dbname + '.* to ' + username + '@localhost identified by "'+password+'"')
c.execute('flush privileges')
else:
print 'Exists "'+mysqlfile+'"'
print '\nDone.\n';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment