Created
October 31, 2012 17:57
-
-
Save fform/3988705 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/php | |
<?php | |
define('NL',"\n"); | |
$arg = (empty($argv[1]) ? "" : $argv[1]); | |
function capture_cmd($cmd){ | |
$output = array(); | |
exec($cmd, $output); | |
return $output; | |
} | |
function read_line(){ | |
return strtolower(trim(fgets(STDIN))); | |
} | |
function read_yn(){ | |
$val = read_line(); | |
return $val == 'y'; | |
} | |
function logbool($val){ | |
return $val ? "Yes" : "No"; | |
} | |
function ask_question($question){ | |
echo $question . ': '; | |
return read_line(); | |
} | |
function ask_yn($question){ | |
echo $question . ' [Y/n]: '; | |
return read_yn(); | |
} | |
if($arg == "taredown"){ | |
$site = ask_question("Site name"); | |
if(is_dir("/var/www/$site")) | |
exec("rm -rf /var/www/$site"); | |
if(is_dir("/home/git/$site.git")) | |
exec("rm -rf /home/git/$site.git"); | |
if(is_file("/etc/httpd/conf/vhosts/$site.conf")){ | |
exec("rm -f /etc/httpd/conf/vhosts/$site.conf"); | |
exec("/etc/init.d/httpd reload"); | |
} | |
echo "Tore $site down".NL; | |
exit; | |
} | |
if(exec("whoami") != "root"){ | |
echo "Need to be root".NL; | |
exit; | |
} | |
$apacheUser = ask_question("Apache user"); | |
if(strpos(exec("id git"), "no such user") ){ | |
echo "Need to make git"; | |
exec("useradd -d /home/git -m git"); | |
echo "Give user 'git' a password:"; | |
exec("passwd git"); | |
exec('mkdir /home/git/.ssh'); | |
exec('cp ~/.ssh/authorized_keys /home/git/.ssh/authorized_keys'); | |
exec('chmod 755 /home/git/.ssh'); | |
exec('chmod 600 /home/git/.ssh/authorized_keys'); | |
exec("chown -R git:git /home/git"); | |
} | |
define('FILE_PRSCRIPT_PATH', '/usr/local/scripts/'); | |
define('FILE_PRSCRIPT', '/usr/local/scripts/pullrepo'); | |
$gitRepoName = ask_question("Git repo name (no path, no .git)"); | |
echo "Checking for Script folder... "; | |
if(!is_dir(FILE_PRSCRIPT_PATH)){ | |
echo "creating "; | |
mkdir(FILE_PRSCRIPT_PATH); | |
} | |
echo "done".NL; | |
echo "Checking for script file... "; | |
if(!is_file(FILE_PRSCRIPT)){ | |
echo "making "; | |
file_put_contents(FILE_PRSCRIPT, ' | |
#!/bin/sh | |
cd "$1" | |
git reset --hard HEAD | |
git pull | |
'); | |
exec("chmod +x " . FILE_PRSCRIPT); | |
} | |
echo "done".NL; | |
echo "Checking if repo exists... "; | |
if(!is_dir("/home/git/$gitRepoName.git")){ | |
echo "making repo "; | |
exec("git init /home/git/$gitRepoName.git --bare"); | |
mkdir("/var/www/$gitRepoName"); | |
exec("git clone /home/git/$gitRepoName.git /var/www/$gitRepoName"); | |
file_put_contents("/home/git/$gitRepoName.git/hooks/post-receive", " | |
#!/bin/sh | |
sudo -u$apacheUser -g$apacheUser /usr/local/scripts/pullrepo /var/www/$gitRepoName | |
"); | |
exec("chmod +x /home/git/$gitRepoName.git/hooks/post-receive"); | |
exec("chown -R git:git /home/git/$gitRepoName.git"); | |
exec("chown -R $apacheUser:$apacheUser /var/www/$gitRepoName"); | |
//exec("git submodule update --init"); | |
} | |
$vhost = " | |
<VirtualHost *:80> | |
SetEnv APP_ENVIRONMENT production | |
DocumentRoot /var/www/$gitRepoName/public | |
ServerName $gitRepoName.proto.idealab.com | |
ErrorLog /etc/httpd/logs/$gitRepoName.error.log | |
CustomLog /etc/httpd/logs/$gitRepoName.access.log common | |
<Directory /var/www/$gitRepoName/public> | |
Options -Indexes FollowSymLinks MultiViews | |
AllowOverride All | |
Order allow,deny | |
allow from all | |
</Directory> | |
ServerAdmin apache@idealab.com | |
</VirtualHost>"; | |
echo "Example vhost config:".NL; | |
echo "---------------------".NL; | |
echo $vhost . NL; | |
echo "---------------------".NL; | |
if(ask_yn("Want me to make that for you")){ | |
file_put_contents("/etc/httpd/conf/vhosts/$gitRepoName.conf", $vhost); | |
exec("/etc/init.d/httpd reload"); | |
} | |
echo NL."Done!".NL; | |
echo "If you haven't already, add:".NL; | |
echo "git ALL=($apacheUser:$apacheUser) NOPASSWD: /usr/local/scripts/pullrepo".NL; | |
echo "to your sudoers file using `visudo`".NL.NL; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment