Skip to content

Instantly share code, notes, and snippets.

@okovalov
Created September 8, 2016 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okovalov/54e718ecc4f748709d7941f177493ca8 to your computer and use it in GitHub Desktop.
Save okovalov/54e718ecc4f748709d7941f177493ca8 to your computer and use it in GitHub Desktop.
from __future__ import with_statement
from fabric.api import local, env, settings, abort, run, cd
from fabric.contrib.console import confirm
import time
# Please create this directory on your production server
code_dir='/var/www/deploy-stage'
# Please make sure this directory exists and contains uploaded files
# most of the cases it is a good idea to keep your uploads apart from your source code
uploads_dir='/var/www/deploy-stage/shared/uploads'
# That is where Github repo is located
repo='git@github.com:your-organization/your-project.git'
timestamp="release_%s" % int(time.time() * 1000)
# Please note - /var/www/public has to be a symbolic link on the server where you will be deploing to
# so if such folder already exists - please either rename it or remove it
env.app_dir='/var/www/public'
# Git branch which is used by git to clone the repo - in case you need smth else than master which is used by default
env.branchname='develop'
env.update_permissions_commands = []
env.update_symlinks_commands = []
env.copy_config_commands = []
# Default environment name
env.environmentName = "dev"
def print_configuration():
print("")
print("---------------------")
print("Global variables are")
print("---------------------")
print("Deploy stage directory on a remote server: %s" % code_dir)
print("Uploads directory on a remote server: %s" % uploads_dir)
print("Git repository to fetch from: %s" % repo)
print("Release name: %s" % timestamp)
print("")
print("---------------------")
print("Environment variables are:")
print("---------------------")
print("use_ssh_config=%s" % env.use_ssh_config)
print("hosts=%s" % env.hosts)
print("key_filename=%s" % env.key_filename)
print("Application directory on a remote server: %s" % env.app_dir)
print("Git branch to be used for deployment: %s" % env.branchname)
print("")
def prod_env_init():
print("Setting up configuration for deploying to PRODUCTION environment")
env.app_dir='/var/www/public'
env.branchname='master'
env.use_ssh_config = True
env.hosts = ['your-project-prod.com']
environmentName = "prod"
env.update_permissions_commands = [
"chgrp -R vagrant .",
"chmod -R ug+rwx ."
]
env.update_symlinks_commands = [
"ln -nfs %s %s" % (code_dir+'/releases/'+timestamp, env.app_dir),
"chgrp -h vagrant %s" % env.app_dir,
"ln -nfs %s %s/content/" % (uploads_dir, env.app_dir),
"chgrp -h vagrant %s/content/uploads" % env.app_dir
]
env.copy_config_commands = [
"cp %s/shared/wp-config-%s.php %s/wp-config.php" % (code_dir, environmentName, env.app_dir)
]
def stage_env_init():
print("Setting up configuration for deploying to STAGING environment")
env.app_dir='/var/www/html'
env.branchname='develop'
env.key_filename='~/.ssh/your-pem-file.pem'
env.hosts = ['user@XX.XXX.XXX.XX']
environmentName = "stage"
env.update_permissions_commands = [
"chgrp -R www .",
"chmod -R ug+rwx ."]
env.update_symlinks_commands = [
"sudo ln -nfs %s %s" % (code_dir+'/releases/'+timestamp, env.app_dir),
"sudo chgrp -h www %s" % env.app_dir,
"sudo ln -nfs %s %s/content/" % (uploads_dir, env.app_dir),
"sudo chgrp -h www %s/content/uploads" % env.app_dir
]
env.copy_config_commands = [
"cp %s/shared/wp-config-%s.php %s/wp-config.php" % (code_dir, environmentName, env.app_dir)
]
def dev_env_init():
print("Setting up configuration for deploying to DEVELOPMENT environment")
print("Feel free to update this configuration..")
def set_env(environment):
print("")
print("Choosing environment to set")
print("")
if environment == "prod":
prod_env_init()
elif environment == "stage":
stage_env_init()
elif environment == "dev":
dev_env_init()
else:
print("Environment %s is not known. Exiting..." % environment)
print("")
def deploy():
print_configuration()
fetch_repo()
run_composer()
update_permissions()
update_symlinks()
copy_config()
def fetch_repo():
with cd(code_dir):
with settings(warn_only=True):
run("mkdir releases")
with cd("%s/releases" % code_dir):
run("git clone %s %s --branch %s" % (repo, timestamp, env.branchname))
def run_composer():
with cd("%s/releases/%s" % (code_dir, timestamp)):
run("composer install")
def update_permissions():
with cd("%s/releases/%s" % (code_dir, timestamp)):
for command in env.update_permissions_commands:
run(command)
def update_symlinks():
for command in env.update_symlinks_commands:
run(command)
def copy_config():
for command in env.copy_config_commands:
run(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment