Skip to content

Instantly share code, notes, and snippets.

@jacob414
Forked from shazow/create_project.sh
Created May 16, 2012 20:18

Revisions

  1. @shazow shazow revised this gist Dec 7, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions create_project.sh
    Original file line number Diff line number Diff line change
    @@ -214,6 +214,7 @@ server {
    server_name $DOMAIN;
    access_log /home/$USERTARGET/logs/$PROJECTNAME/access.log;
    charset utf-8;
    client_max_body_size 32M;
    gzip on;
    gzip_http_version 1.1;
  2. @shazow shazow revised this gist Nov 14, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion create_project.sh
    Original file line number Diff line number Diff line change
    @@ -262,5 +262,5 @@ fi

    # Print instructions
    if [ "$TWOSTEP" == "y" ]; then
    deploy_instructions
    deploy_instructions $USERTARGET $PROJECTNAME
    fi
  3. @shazow shazow revised this gist Nov 13, 2011. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions create_project.sh
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ function deploy_instructions() {
    Setup your development clone as follows:
    git branch live
    git remote add -t live live ssh://$USERNAME@$IP_ADDRESS/home/$USERTARGET/repo/$PROJECTNAME
    git remote add -t live live ssh://$1@$IP_ADDRESS/home/$1/repo/$2
    Now you can deploy:
    @@ -59,8 +59,8 @@ while [ "$1" ]; do
    exit
    ;;
    "-p")
    deploy_instructions
    exit
    ONLY_INSTRUCTIONS="y"
    shift
    ;;
    "-f")
    FORCE="y"
    @@ -101,6 +101,11 @@ if [ ! "$DOMAIN" ]; then
    DOMAIN="$PROJECTNAME.com"
    fi

    if [ "$ONLY_INSTRUCTIONS" == "y" ]; then
    deploy_instructions $USERTARGET $PROJECTNAME
    exit
    fi


    if [ "$NOPROMPT" != "y" ]; then
    echo "This script is intended to be run on a remote server, not on a local development environment. It will create a bunch of directories and change a bunch of configurations."
  4. @shazow shazow revised this gist Nov 13, 2011. 1 changed file with 14 additions and 10 deletions.
    24 changes: 14 additions & 10 deletions create_project.sh
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ function deploy_instructions() {
    Setup your development clone as follows:
    git branch live
    git remote add -t live live ssh://$USERNAME@$IP_ADDRESS/home/$USERNAME/repo/$PROJECTNAME
    git remote add -t live live ssh://$USERNAME@$IP_ADDRESS/home/$USERTARGET/repo/$PROJECTNAME
    Now you can deploy:
    @@ -47,6 +47,9 @@ EOF
    }


    USERNAME="$(whoami)"
    USERTARGET="$USERNAME"

    positional=( );

    while [ "$1" ]; do
    @@ -74,6 +77,8 @@ while [ "$1" ]; do
    "-2")
    TWOSTEP="y"
    shift
    USERTARGET="$1"
    shift
    ;;
    *)
    positional[${#positional[*]}]="$1"
    @@ -83,7 +88,6 @@ while [ "$1" ]; do
    done


    USERNAME="$(whoami)"
    PROJECTNAME="${positional[0]}"
    DOMAIN="${positional[1]}"

    @@ -159,7 +163,7 @@ EOF

    if [ "$ONESTEP" != "y" ]; then
    echo "The rest of this script requires root permission, please give sudo permission or run it again as root manually."
    command="$0 $PROJECTNAME -2 -y"
    command="$0 $PROJECTNAME -2 $USERNAME -y"
    if [ "$FORCE" == "y" ]; then
    command="$command -f"
    fi
    @@ -182,11 +186,11 @@ else ################################################# Rootland configuration ##
    else
    tee $supervisor_conf_path > /dev/null << EOF
    [program:$PROJECTNAME-uwsgi]
    directory=/home/$USERNAME/deploy/$PROJECTNAME
    user=$USERNAME
    directory=/home/$USERTARGET/deploy/$PROJECTNAME
    user=$USERTARGET
    command=/usr/bin/uwsgi-python -C -H /home/$USERNAME/env/$PROJECTNAME -L -w wsgi --socket /tmp/$PROJECTNAME-uwsgi.sock --master --processes 1 --pidfile /tmp/$PROJECTNAME-uwsgi.pid
    stdout_logfile=/home/$USERNAME/logs/$PROJECTNAME/uwsgi.log
    command=/usr/bin/uwsgi-python -C -H /home/$USERTARGET/env/$PROJECTNAME -L -w wsgi --socket /tmp/$PROJECTNAME-uwsgi.sock --master --processes 1 --pidfile /tmp/$PROJECTNAME-uwsgi.pid
    stdout_logfile=/home/$USERTARGET/logs/$PROJECTNAME/uwsgi.log
    redirect_stderr=true
    stopsignal=INT
    autorestart=true
    @@ -203,7 +207,7 @@ EOF
    server {
    listen 80;
    server_name $DOMAIN;
    access_log /home/$USERNAME/logs/$PROJECTNAME/access.log;
    access_log /home/$USERTARGET/logs/$PROJECTNAME/access.log;
    charset utf-8;
    gzip on;
    @@ -216,11 +220,11 @@ server {
    gzip_disable "MSIE [1-6].(?!.*SV1)";
    location ~ ^/(robots.txt|favicon.ico)\$ {
    root /home/$USERNAME/public_html/$PROJECTNAME/static;
    root /home/$USERTARGET/public_html/$PROJECTNAME/static;
    }
    location /static {
    root /home/$USERNAME/public_html/$PROJECTNAME/;
    root /home/$USERTARGET/public_html/$PROJECTNAME/;
    expires max;
    add_header Cache-Control "public";
    }
  5. @shazow shazow revised this gist Nov 13, 2011. 1 changed file with 29 additions and 22 deletions.
    51 changes: 29 additions & 22 deletions create_project.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    function usage() {
    cat << EOF
    Usage: $0 PROJECT_NAME [OPTIONS]
    Usage: $0 PROJECT_NAME [DOMAIN] [OPTIONS]
    Options:
    -y No prompts, assume yes to all.
    @@ -18,16 +18,6 @@ EOF
    }


    PROJECTNAME="$1"
    DOMAIN="$PROJECTNAME.com"
    USERNAME="$(whoami)"

    if [ ! "$PROJECTNAME" ]; then
    echo "Error: Must specify a project name."
    exit 3;
    fi


    function deploy_instructions() {
    IP_ADDRESS="$(ifconfig eth0 | awk -F: '/inet addr:/ {print $2}' | awk '{ print $1 }')"

    @@ -57,40 +47,57 @@ EOF
    }


    while getopts "h:p:f:y:1:2" OPTION; do
    case $OPTION in
    h)
    positional=( );

    while [ "$1" ]; do
    case "$1" in
    "-h")
    usage
    exit
    ;;
    p)
    "-p")
    deploy_instructions
    exit
    ;;
    f)
    "-f")
    FORCE="y"
    shift
    ;;
    y)
    "-y")
    NOPROMPT="y"
    shift
    ;;
    1)
    "-1")
    ONESTEP="y"
    shift
    ;;
    2)
    "-2")
    TWOSTEP="y"
    shift
    ;;
    ?)
    usage
    exit
    *)
    positional[${#positional[*]}]="$1"
    shift
    ;;
    esac
    done


    USERNAME="$(whoami)"
    PROJECTNAME="${positional[0]}"
    DOMAIN="${positional[1]}"

    if [ ! "$PROJECTNAME" ]; then
    echo "Error: Must specify a project name."
    usage
    exit 3;
    fi

    if [ ! "$DOMAIN" ]; then
    DOMAIN="$PROJECTNAME.com"
    fi


    if [ "$NOPROMPT" != "y" ]; then
    echo "This script is intended to be run on a remote server, not on a local development environment. It will create a bunch of directories and change a bunch of configurations."
    read -n1 -p "Are you sure you want to continue? [y/N] " answer
  6. @shazow shazow revised this gist Nov 13, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion create_project.sh
    Original file line number Diff line number Diff line change
    @@ -57,7 +57,7 @@ EOF
    }


    while getopts "h:y:p:f:1:2" OPTION; do
    while getopts "h:p:f:y:1:2" OPTION; do
    case $OPTION in
    h)
    usage
  7. @shazow shazow revised this gist Nov 13, 2011. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion create_project.sh
    Original file line number Diff line number Diff line change
    @@ -152,7 +152,11 @@ EOF

    if [ "$ONESTEP" != "y" ]; then
    echo "The rest of this script requires root permission, please give sudo permission or run it again as root manually."
    sudo $0 $PROJECTNAME -2 -y
    command="$0 $PROJECTNAME -2 -y"
    if [ "$FORCE" == "y" ]; then
    command="$command -f"
    fi
    sudo $command
    fi

    else ################################################# Rootland configuration ##
  8. @shazow shazow revised this gist Nov 13, 2011. 1 changed file with 3 additions and 8 deletions.
    11 changes: 3 additions & 8 deletions create_project.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    function usage() {
    cat << EOF
    Usage: $0 [-y] PROJECT_NAME [DOMAIN]
    Usage: $0 PROJECT_NAME [OPTIONS]
    Options:
    -y No prompts, assume yes to all.
    @@ -19,7 +19,7 @@ EOF


    PROJECTNAME="$1"
    DOMAIN="$2"
    DOMAIN="$PROJECTNAME.com"
    USERNAME="$(whoami)"

    if [ ! "$PROJECTNAME" ]; then
    @@ -28,11 +28,6 @@ if [ ! "$PROJECTNAME" ]; then
    fi


    if [ ! "$DOMAIN" ]; then
    DOMAIN="$PROJECTNAME.com"
    fi


    function deploy_instructions() {
    IP_ADDRESS="$(ifconfig eth0 | awk -F: '/inet addr:/ {print $2}' | awk '{ print $1 }')"

    @@ -157,7 +152,7 @@ EOF

    if [ "$ONESTEP" != "y" ]; then
    echo "The rest of this script requires root permission, please give sudo permission or run it again as root manually."
    sudo $0 $PROJECTNAME $DOMAIN -2 -y
    sudo $0 $PROJECTNAME -2 -y
    fi

    else ################################################# Rootland configuration ##
  9. @shazow shazow revised this gist Nov 13, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion create_project.sh
    Original file line number Diff line number Diff line change
    @@ -157,7 +157,7 @@ EOF

    if [ "$ONESTEP" != "y" ]; then
    echo "The rest of this script requires root permission, please give sudo permission or run it again as root manually."
    sudo $0 $* -2 -y
    sudo $0 $PROJECTNAME $DOMAIN -2 -y
    fi

    else ################################################# Rootland configuration ##
  10. @shazow shazow revised this gist Nov 13, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion create_project.sh
    Original file line number Diff line number Diff line change
    @@ -157,7 +157,7 @@ EOF

    if [ "$ONESTEP" != "y" ]; then
    echo "The rest of this script requires root permission, please give sudo permission or run it again as root manually."
    sudo $0 $* -2
    sudo $0 $* -2 -y
    fi

    else ################################################# Rootland configuration ##
  11. @shazow shazow revised this gist Nov 13, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion create_project.sh
    Original file line number Diff line number Diff line change
    @@ -157,7 +157,7 @@ EOF

    if [ "$ONESTEP" != "y" ]; then
    echo "The rest of this script requires root permission, please give sudo permission or run it again as root manually."
    sudo $0 -2 $*
    sudo $0 $* -2
    fi

    else ################################################# Rootland configuration ##
  12. @shazow shazow revised this gist Nov 13, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion create_project.sh
    Original file line number Diff line number Diff line change
    @@ -62,7 +62,7 @@ EOF
    }


    while getopts "h:y" OPTION; do
    while getopts "h:y:p:f:1:2" OPTION; do
    case $OPTION in
    h)
    usage
  13. @shazow shazow revised this gist Nov 13, 2011. 1 changed file with 14 additions and 12 deletions.
    26 changes: 14 additions & 12 deletions create_project.sh
    Original file line number Diff line number Diff line change
    @@ -113,19 +113,19 @@ if [ "$USERNAME" != "root" ]; then ################### Userland configuration ##
    mkdir ~/{repo,deploy,logs,public_html,uploads}/$PROJECTNAME


    # Create a detached tree repository
    cd ~/repo/$PROJECTNAME
    if [ -d .git ]; then
    echo "Git repository already exists, aborting."
    exit 5
    fi
    git init --bare
    git config core.worktree ~/deploy/$PROJECTNAME
    git config receive.denycurrentbranch ignore
    git_repo=~/repo/$PROJECTNAME
    if [ -d $git_repo/config ] && [ "$FORCE" != "y" ]; then
    echo "Git repository already exists, skipping: $git_repo"
    else
    # Create a detached tree repository
    cd $git_repo
    git init --bare
    git config core.worktree ~/deploy/$PROJECTNAME
    git config receive.denycurrentbranch ignore


    # Setup post-receive hook to update detached tree on receive
    cat > hooks/post-receive << EOF
    # Setup post-receive hook to update detached tree on receive
    cat > hooks/post-receive << EOF
    #!/bin/sh
    git checkout -f
    @@ -136,7 +136,9 @@ if [ -f "\$UWSGI_PID" ]; then
    fi
    EOF

    chmod +x hooks/post-receive
    chmod +x hooks/post-receive
    cd -
    fi


    # Setup static assets to be served by nginx directly
  14. @shazow shazow revised this gist Nov 13, 2011. 1 changed file with 114 additions and 54 deletions.
    168 changes: 114 additions & 54 deletions create_project.sh
    Original file line number Diff line number Diff line change
    @@ -8,41 +8,93 @@ Usage: $0 [-y] PROJECT_NAME [DOMAIN]
    Options:
    -y No prompts, assume yes to all.
    -1 One-step, don't try to do the root parts automagically.
    -p Only print deploy instructions, don't do anything else.
    -f Overwrite any existing configurations.
    Example:
    $0 -y foo fooapp.com
    EOF
    }


    PROJECTNAME="$1"
    DOMAIN="$2"
    USERNAME="$(whoami)"

    if [ ! "$PROJECTNAME" ]; then
    echo "Error: Must specify a project name."
    exit 3;
    fi


    if [ ! "$DOMAIN" ]; then
    DOMAIN="$PROJECTNAME.com"
    fi


    function deploy_instructions() {
    IP_ADDRESS="$(ifconfig eth0 | awk -F: '/inet addr:/ {print $2}' | awk '{ print $1 }')"

    cat << EOF
    Setup your development clone as follows:
    git branch live
    git remote add -t live live ssh://$USERNAME@$IP_ADDRESS/home/$USERNAME/repo/$PROJECTNAME
    Now you can deploy:
    git checkout live
    git merge master
    git checkout master
    git push live
    Or use this handy 'deploy' alias in your ~/.gitconfig file:
    deploy = "!merge(){ git checkout \$2 && git merge \$1 && git push \$2 && git checkout \${1#refs/heads/}; }; merge \$(git symbolic-ref HEAD) \$1"
    So you can do (from 'master'):
    git deploy live
    Happy pushing!
    EOF
    }


    while getopts "h:y" OPTION; do
    case $OPTION in
    h)
    usage
    exit
    ;;
    p)
    deploy_instructions
    exit
    ;;
    f)
    FORCE="y"
    shift
    ;;
    y)
    NOPROMPT="y"
    shift
    ;;
    1)
    ONESTEP="y"
    shift
    ;;
    2)
    TWOSTEP="y"
    shift
    ;;
    ?)
    usage
    exit
    ;;
    esac
    done

    PROJECTNAME="$1"
    DOMAIN="$2"
    USERNAME="$(whoami)"

    if [ ! "$PROJECTNAME" ]; then
    echo "Error: Must specify a project name."
    exit 3;
    fi

    if [ ! "$DOMAIN" ]; then
    DOMAIN="$PROJECTNAME.com"
    fi

    if [ "$NOPROMPT" != "y" ]; then
    echo "This script is intended to be run on a remote server, not on a local development environment. It will create a bunch of directories and change a bunch of configurations."
    @@ -53,16 +105,20 @@ if [ "$NOPROMPT" != "y" ]; then
    fi
    fi

    if [ "$USERNAME" == "root" ]; then
    echo "Warning: Running as root, only doing root parts."
    else

    if [ "$USERNAME" != "root" ]; then ################### Userland configuration ##

    # Setup our directory structure
    mkdir ~/{deploy,env,logs,public_html,repo,uploads}
    mkdir ~/{repo,deploy,logs,public_html,uploads}/$PROJECTNAME


    # Create a detached tree repository
    cd ~/repo/$PROJECTNAME
    if [ -d .git ]; then
    echo "Git repository already exists, aborting."
    exit 5
    fi
    git init --bare
    git config core.worktree ~/deploy/$PROJECTNAME
    git config receive.denycurrentbranch ignore
    @@ -88,16 +144,35 @@ EOF


    # Virtualenv
    virtualenv ~/env/$PROJECTNAME
    source ~/env/$PROJECTNAME/bin/activate
    virtualenv_path=~/env/$PROJECTNAME
    if [ -d $virtualenv_path ] && [ "$FORCE" != "y" ]; then
    echo "Virtualenv already exists, skipping: $virtualenv_path"
    else
    rm -rf $virtualenv_path
    virtualenv $virtualenv_path
    fi
    source $virtualenv_path/bin/activate

    fi
    if [ "$ONESTEP" != "y" ]; then
    echo "The rest of this script requires root permission, please give sudo permission or run it again as root manually."
    sudo $0 -2 $*
    fi

    else ################################################# Rootland configuration ##

    if [ ! "$TWOSTEP" ]; then
    echo "Warning: Running as root, only doing root parts."
    fi

    # Install root config files

    # Install root config files
    ## Supervisor
    supervisor_conf_path=/etc/supervisor/conf.d/$PROJECTNAME.conf

    ## Supervisor
    sudo tee /etc/supervisor/conf.d/$PROJECTNAME.conf > /dev/null << EOF
    if [ -f $supervisor_conf_path ] && [ "$FORCE" != "y" ]; then
    echo "Supervisor configuration already exists, skipping: $supervisor_conf_path"
    else
    tee $supervisor_conf_path > /dev/null << EOF
    [program:$PROJECTNAME-uwsgi]
    directory=/home/$USERNAME/deploy/$PROJECTNAME
    user=$USERNAME
    @@ -108,9 +183,15 @@ redirect_stderr=true
    stopsignal=INT
    autorestart=true
    EOF
    fi

    ## Nginx
    sudo tee /etc/nginx/sites-available/$PROJECTNAME > /dev/null << EOF
    ## Nginx
    nginx_conf_path=/etc/nginx/sites-available/$PROJECTNAME

    if [ -f $nginx_conf_path ] && [ "$FORCE" != "y" ]; then
    echo "Nginx site configuration already exists, skipping: $nginx_conf_path"
    else
    tee $nginx_conf_path > /dev/null << EOF
    server {
    listen 80;
    server_name $DOMAIN;
    @@ -151,39 +232,18 @@ server {
    }
    EOF

    ## Enable new Nginx config
    sudo ln -sft /etc/nginx/sites-enabled/ "../sites-available/$PROJECTNAME"
    ## Enable new Nginx config
    ln -sft /etc/nginx/sites-enabled/ "../sites-available/$PROJECTNAME"

    # Restart things
    fi

    sudo /etc/init.d/nginx reload
    sudo supervisorctl reload
    # Restart things
    /etc/init.d/nginx reload
    supervisorctl reload

    fi

    # Print instructions

    IP_ADDRESS="$(ifconfig eth0 | awk -F: '/inet addr:/ {print $2}' | awk '{ print $1 }')"

    cat << EOF
    Setup your development clone as follows:
    git branch live
    git remote add -t live live ssh://$USERNAME@$IP_ADDRESS/home/$USERNAME/repo/$PROJECTNAME
    Now you can deploy:
    git checkout live
    git merge master
    git checkout master
    git push live
    Or use this handy 'deploy' alias in your ~/.gitconfig file:
    deploy = "!merge(){ git checkout \$2 && git merge \$1 && git push \$2 && git checkout \${1#refs/heads/}; }; merge \$(git symbolic-ref HEAD) \$1"
    So you can do (from 'master'):
    git deploy live
    Happy pushing!
    EOF
    if [ "$TWOSTEP" == "y" ]; then
    deploy_instructions
    fi
  15. @shazow shazow revised this gist Nov 13, 2011. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions create_project.sh
    Original file line number Diff line number Diff line change
    @@ -64,7 +64,6 @@ else
    # Create a detached tree repository
    cd ~/repo/$PROJECTNAME
    git init --bare
    git config core.bare false
    git config core.worktree ~/deploy/$PROJECTNAME
    git config receive.denycurrentbranch ignore

    @@ -128,11 +127,11 @@ server {
    gzip_disable "MSIE [1-6].(?!.*SV1)";
    location ~ ^/(robots.txt|favicon.ico)\$ {
    root /home/$USERNAME/public_html/$PROJECTNAME.com/static;
    root /home/$USERNAME/public_html/$PROJECTNAME/static;
    }
    location /static {
    root /home/$USERNAME/public_html/$PROJECTNAME.com/;
    root /home/$USERNAME/public_html/$PROJECTNAME/;
    expires max;
    add_header Cache-Control "public";
    }
  16. @shazow shazow revised this gist Nov 13, 2011. 1 changed file with 18 additions and 18 deletions.
    36 changes: 18 additions & 18 deletions create_project.sh
    Original file line number Diff line number Diff line change
    @@ -56,21 +56,21 @@ fi
    if [ "$USERNAME" == "root" ]; then
    echo "Warning: Running as root, only doing root parts."
    else
    # Setup our directory structure
    mkdir ~/{deploy,env,logs,public_html,repo,uploads}
    mkdir ~/{repo,deploy,logs,public_html,uploads}/$PROJECTNAME
    # Setup our directory structure
    mkdir ~/{deploy,env,logs,public_html,repo,uploads}
    mkdir ~/{repo,deploy,logs,public_html,uploads}/$PROJECTNAME


    # Create a detached tree repository
    cd ~/repo/$PROJECTNAME
    git init --bare
    git config core.bare false
    git config core.worktree ~/deploy/$PROJECTNAME
    git config receive.denycurrentbranch ignore
    # Create a detached tree repository
    cd ~/repo/$PROJECTNAME
    git init --bare
    git config core.bare false
    git config core.worktree ~/deploy/$PROJECTNAME
    git config receive.denycurrentbranch ignore


    # Setup post-receive hook to update detached tree on receive
    cat > hooks/post-receive << EOF
    # Setup post-receive hook to update detached tree on receive
    cat > hooks/post-receive << EOF
    #!/bin/sh
    git checkout -f
    @@ -81,16 +81,16 @@ if [ -f "\$UWSGI_PID" ]; then
    fi
    EOF

    chmod +x hooks/post-receive
    chmod +x hooks/post-receive


    # Setup static assets to be served by nginx directly
    ln -s ~/deploy/$PROJECTNAME/static ~/public_html/$PROJECTNAME/static
    # Setup static assets to be served by nginx directly
    ln -s ~/deploy/$PROJECTNAME/static ~/public_html/$PROJECTNAME/static


    # Virtualenv
    virtualenv ~/env/$PROJECTNAME
    source ~/env/$PROJECTNAME/bin/activate
    # Virtualenv
    virtualenv ~/env/$PROJECTNAME
    source ~/env/$PROJECTNAME/bin/activate

    fi

    @@ -187,4 +187,4 @@ So you can do (from 'master'):
    git deploy live
    Happy pushing!
    EOF
    EOF
  17. @shazow shazow revised this gist Nov 13, 2011. 1 changed file with 23 additions and 23 deletions.
    46 changes: 23 additions & 23 deletions create_project.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash
    # Setup deployment target for Nginx + Python/uWSGI + Supervisor + Git

    # From: https://gist.github.com/1210041

    function usage() {
    cat << EOF
    @@ -35,11 +35,6 @@ PROJECTNAME="$1"
    DOMAIN="$2"
    USERNAME="$(whoami)"

    if [ "$USERNAME" == "root" ]; then
    echo "Error: Must be run as non-root."
    exit 2
    fi

    if [ ! "$PROJECTNAME" ]; then
    echo "Error: Must specify a project name."
    exit 3;
    @@ -58,21 +53,24 @@ if [ "$NOPROMPT" != "y" ]; then
    fi
    fi

    # Setup our directory structure
    mkdir ~/{deploy,env,logs,public_html,repo,uploads}
    mkdir ~/{repo,deploy,logs,public_html,uploads}/$PROJECTNAME
    if [ "$USERNAME" == "root" ]; then
    echo "Warning: Running as root, only doing root parts."
    else
    # Setup our directory structure
    mkdir ~/{deploy,env,logs,public_html,repo,uploads}
    mkdir ~/{repo,deploy,logs,public_html,uploads}/$PROJECTNAME


    # Create a detached tree repository
    cd ~/repo/$PROJECTNAME
    git init --bare
    git config core.bare false
    git config core.worktree ~/deploy/$PROJECTNAME
    git config receive.denycurrentbranch ignore
    # Create a detached tree repository
    cd ~/repo/$PROJECTNAME
    git init --bare
    git config core.bare false
    git config core.worktree ~/deploy/$PROJECTNAME
    git config receive.denycurrentbranch ignore


    # Setup post-receive hook to update detached tree on receive
    cat > hooks/post-receive << EOF
    # Setup post-receive hook to update detached tree on receive
    cat > hooks/post-receive << EOF
    #!/bin/sh
    git checkout -f
    @@ -83,16 +81,18 @@ if [ -f "\$UWSGI_PID" ]; then
    fi
    EOF

    chmod +x hooks/post-receive
    chmod +x hooks/post-receive


    # Setup static assets to be served by nginx directly
    ln -s ~/deploy/$PROJECTNAME/static ~/public_html/$PROJECTNAME/static

    # Setup static assets to be served by nginx directly
    ln -s ~/deploy/$PROJECTNAME/static ~/public_html/$PROJECTNAME/static

    # Virtualenv
    virtualenv ~/env/$PROJECTNAME
    source ~/env/$PROJECTNAME/bin/activate

    # Virtualenv
    virtualenv ~/env/$PROJECTNAME
    source ~/env/$PROJECTNAME/bin/activate
    fi


    # Install root config files
  18. @shazow shazow revised this gist Sep 11, 2011. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions create_project.sh
    Original file line number Diff line number Diff line change
    @@ -50,10 +50,10 @@ if [ ! "$DOMAIN" ]; then
    fi

    if [ "$NOPROMPT" != "y" ]; then
    echo "This script is intended to be run on a remote server, not on a local development environment. It will create a bunch of directories and change a bunch of configurations, are you sure you want to continue? [y/N] "
    read -n1 answer
    echo "This script is intended to be run on a remote server, not on a local development environment. It will create a bunch of directories and change a bunch of configurations."
    read -n1 -p "Are you sure you want to continue? [y/N] " answer
    if [ "$answer" != "y" ]; then
    echo "\nAborting."
    echo "Aborting."
    exit 4
    fi
    fi
    @@ -79,7 +79,7 @@ git checkout -f
    UWSGI_PID=/tmp/$PROJECTNAME-uwsgi.pid
    if [ -f "\$UWSGI_PID" ]; then
    echo "Restarting uwsgi.";
    kill -HUP $(cat \$UWSGI_PID);
    kill -HUP \$(cat \$UWSGI_PID);
    fi
    EOF

  19. @shazow shazow revised this gist Sep 11, 2011. 1 changed file with 45 additions and 2 deletions.
    47 changes: 45 additions & 2 deletions create_project.sh
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,62 @@
    #!/bin/bash
    # Setup deployment target for Nginx + Python/uWSGI + Supervisor + Git


    function usage() {
    cat << EOF
    Usage: $0 [-y] PROJECT_NAME [DOMAIN]
    Options:
    -y No prompts, assume yes to all.
    Example:
    $0 -y foo fooapp.com
    EOF
    }

    while getopts "h:y" OPTION; do
    case $OPTION in
    h)
    usage
    exit
    ;;
    y)
    NOPROMPT="y"
    shift
    ;;
    ?)
    usage
    exit
    ;;
    esac
    done

    PROJECTNAME="$1"
    DOMAIN="$2"
    USERNAME="$(whoami)"

    if [ "$USERNAME" == "root" ]; then
    echo "Error: Must be run as non-root."
    exit 1
    exit 2
    fi

    if [ ! "$PROJECTNAME" ]; then
    echo "Error: Must specify a project name."
    exit 3;
    fi

    if [ ! "$DOMAIN" ]; then
    DOMAIN="$PROJECTNAME.com"
    fi

    if [ "$NOPROMPT" != "y" ]; then
    echo "This script is intended to be run on a remote server, not on a local development environment. It will create a bunch of directories and change a bunch of configurations, are you sure you want to continue? [y/N] "
    read -n1 answer
    if [ "$answer" != "y" ]; then
    echo "\nAborting."
    exit 4
    fi
    fi

    # Setup our directory structure
    mkdir ~/{deploy,env,logs,public_html,repo,uploads}
    @@ -144,4 +187,4 @@ So you can do (from 'master'):
    git deploy live
    Happy pushing!
    EOF
    EOF
  20. @shazow shazow created this gist Sep 11, 2011.
    147 changes: 147 additions & 0 deletions create_project.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,147 @@
    #!/bin/bash
    # Setup deployment target for Nginx + Python/uWSGI + Supervisor + Git

    PROJECTNAME="$1"
    DOMAIN="$2"
    USERNAME="$(whoami)"

    if [ "$USERNAME" == "root" ]; then
    echo "Error: Must be run as non-root."
    exit 1
    fi

    if [ ! "$DOMAIN" ]; then
    DOMAIN="$PROJECTNAME.com"
    fi


    # Setup our directory structure
    mkdir ~/{deploy,env,logs,public_html,repo,uploads}
    mkdir ~/{repo,deploy,logs,public_html,uploads}/$PROJECTNAME


    # Create a detached tree repository
    cd ~/repo/$PROJECTNAME
    git init --bare
    git config core.bare false
    git config core.worktree ~/deploy/$PROJECTNAME
    git config receive.denycurrentbranch ignore


    # Setup post-receive hook to update detached tree on receive
    cat > hooks/post-receive << EOF
    #!/bin/sh
    git checkout -f
    UWSGI_PID=/tmp/$PROJECTNAME-uwsgi.pid
    if [ -f "\$UWSGI_PID" ]; then
    echo "Restarting uwsgi.";
    kill -HUP $(cat \$UWSGI_PID);
    fi
    EOF

    chmod +x hooks/post-receive


    # Setup static assets to be served by nginx directly
    ln -s ~/deploy/$PROJECTNAME/static ~/public_html/$PROJECTNAME/static


    # Virtualenv
    virtualenv ~/env/$PROJECTNAME
    source ~/env/$PROJECTNAME/bin/activate


    # Install root config files

    ## Supervisor
    sudo tee /etc/supervisor/conf.d/$PROJECTNAME.conf > /dev/null << EOF
    [program:$PROJECTNAME-uwsgi]
    directory=/home/$USERNAME/deploy/$PROJECTNAME
    user=$USERNAME
    command=/usr/bin/uwsgi-python -C -H /home/$USERNAME/env/$PROJECTNAME -L -w wsgi --socket /tmp/$PROJECTNAME-uwsgi.sock --master --processes 1 --pidfile /tmp/$PROJECTNAME-uwsgi.pid
    stdout_logfile=/home/$USERNAME/logs/$PROJECTNAME/uwsgi.log
    redirect_stderr=true
    stopsignal=INT
    autorestart=true
    EOF

    ## Nginx
    sudo tee /etc/nginx/sites-available/$PROJECTNAME > /dev/null << EOF
    server {
    listen 80;
    server_name $DOMAIN;
    access_log /home/$USERNAME/logs/$PROJECTNAME/access.log;
    charset utf-8;
    gzip on;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_proxied any;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_buffers 16 8k;
    gzip_disable "MSIE [1-6].(?!.*SV1)";
    location ~ ^/(robots.txt|favicon.ico)\$ {
    root /home/$USERNAME/public_html/$PROJECTNAME.com/static;
    }
    location /static {
    root /home/$USERNAME/public_html/$PROJECTNAME.com/;
    expires max;
    add_header Cache-Control "public";
    }
    location / {
    include uwsgi_params;
    uwsgi_pass unix:///tmp/$PROJECTNAME-uwsgi.sock;
    uwsgi_param SCRIPT_NAME "";
    }
    }
    server {
    listen 80;
    server_name www.$DOMAIN;
    rewrite ^/(.*) http://$DOMAIN/\$1 permanent;
    }
    EOF

    ## Enable new Nginx config
    sudo ln -sft /etc/nginx/sites-enabled/ "../sites-available/$PROJECTNAME"

    # Restart things

    sudo /etc/init.d/nginx reload
    sudo supervisorctl reload


    # Print instructions

    IP_ADDRESS="$(ifconfig eth0 | awk -F: '/inet addr:/ {print $2}' | awk '{ print $1 }')"

    cat << EOF
    Setup your development clone as follows:
    git branch live
    git remote add -t live live ssh://$USERNAME@$IP_ADDRESS/home/$USERNAME/repo/$PROJECTNAME
    Now you can deploy:
    git checkout live
    git merge master
    git checkout master
    git push live
    Or use this handy 'deploy' alias in your ~/.gitconfig file:
    deploy = "!merge(){ git checkout \$2 && git merge \$1 && git push \$2 && git checkout \${1#refs/heads/}; }; merge \$(git symbolic-ref HEAD) \$1"
    So you can do (from 'master'):
    git deploy live
    Happy pushing!
    EOF