Created
September 16, 2013 19:03
-
-
Save btompkins/6585020 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fabric.api import * | |
from fabric.contrib.files import * | |
env.host_string = 'your.host.ip.here' | |
env.user = 'root' | |
def change_my_password(): | |
prompt('Specify new password: ', 'new_password') | |
runcmd('echo {un}:{pw} | chpasswd'.format(un=env.user, pw=env.new_password)) | |
def deploy_db_server(): | |
""" | |
Setup apache, mysql, and phpmyadmin, git | |
""" | |
prompt('Specify db password: ', 'new_password') | |
install_apache() | |
install_mysql(env.new_password) | |
install_phpmyadmin() | |
setup_mysql_remote_access('192.168.0.0/16', env.host_string) | |
create_database('teamcity', 'root', '[pass]', 'myuser', '[pass]' ) | |
def install_apache(): | |
runcmd('apt-get -y install apache2 php5 libapache2-mod-php5 mysql-client php5-mysql') | |
runcmd('a2enmod rewrite') | |
runcmd('a2enmod deflate') | |
runcmd('a2enmod expires') | |
runcmd('a2enmod headers') | |
runcmd('sh -c "echo \'<?php phpinfo( ); ?>\' > /var/www/info.php"') | |
runcmd('/etc/init.d/apache2 restart') | |
def install_mysql(mysql_root_password): | |
runcmd('echo "mysql-server mysql-server/root_password select {password}" |' | |
'debconf-set-selections'.format( | |
password=mysql_root_password)) | |
runcmd('echo "mysql-server mysql-server/root_password_again select ' | |
'{password}" | debconf-set-selections'.format( | |
password=mysql_root_password)) | |
runcmd('apt-get -y install mysql-server') | |
def install_phpmyadmin(): | |
runcmd('DEBIAN_FRONTEND=noninteractive apt-get install -y phpmyadmin') | |
runcmd('ln -sv /etc/phpmyadmin/apache.conf ' | |
'/etc/apache2/conf.d/phpmyadmin.conf') | |
runcmd('/etc/init.d/apache2 restart') | |
# Now you can point your browser to: http://domain/phpmyadmin | |
def create_database(database_name, root_user, root_password, new_user, | |
new_user_password): | |
runcmd('mysql --user={root} --password={password} --execute="create ' | |
'database {database}"'.format(root=root_user, | |
password=root_password, | |
database=database_name)) | |
runcmd('mysql --user={root} --password={password} --execute="CREATE USER ' | |
'\'{user}\' IDENTIFIED BY \'{userpass}\'"' | |
.format(root=root_user, | |
password=root_password, | |
user=new_user, | |
userpass=new_user_password)) | |
runcmd('mysql --user={root} --password={password} ' | |
'--execute="GRANT ALL ON {database}.* TO ' | |
'\'{user}\'@\'%\' IDENTIFIED BY \'{userpass}\'"' | |
.format(root=root_user, | |
password=root_password, | |
database=database_name, | |
user=new_user, | |
userpass=new_user_password)) | |
runcmd('mysql --user={root} --password={password} ' | |
'--execute="FLUSH PRIVILEGES"' | |
.format(root=root_user, | |
password=root_password)) | |
def setup_mysql_remote_access(remote_range, bind_address): | |
comment('/etc/mysql/my.cnf', 'skip-networking', use_sudo=True) | |
sed('/etc/mysql/my.cnf', 'bind-address = 127.0.0.1', | |
'bind-address = {address}'.format(address=bind_address), use_sudo=True) | |
runcmd('restart mysql') | |
runcmd('iptables -A INPUT -i eth0 -s {remote} -p tcp --destination-port 3306 -j ACCEPT' | |
.format(remote=remote_range)) | |
runcmd('iptables-save') | |
# Helpers | |
def runcmd(arg): | |
if env.user != "root": | |
sudo("%s" % arg, pty=True) | |
else: | |
run("%s" % arg, pty=True) | |
# Run entire setup | |
deploy_db_server() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment