Created
April 23, 2015 16:07
-
-
Save victoraguilarc/dfc83a5f71d642bf00e7 to your computer and use it in GitHub Desktop.
Ejemplo para desplegar una pagina HTML5 con GIT
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 exists, upload_template | |
| from fabric.operations import sudo | |
| APP = { | |
| "url": "owl.jvacx.com", | |
| "name": "owl", | |
| "user": "vagrant", | |
| "path": "/var/www/owl", | |
| "deps": [ # System's Packages dependencies | |
| "php5-fpm", | |
| "php5-cli", | |
| "nginx", | |
| ], | |
| } | |
| # Path configurations. | |
| CONFIG_DIR = './config' # Locally | |
| NGINX_DIR = '/etc/nginx' # Remote | |
| PROJECT_GIT_PATH = "%s/%s.git"%(APP["path"], APP["name"]) | |
| SERVERS = { | |
| "production": { | |
| "domain": "192.168.33.3", | |
| "ssh_port": "22", | |
| "branch": "master", | |
| }, | |
| "development": { | |
| "domain": "192.168.33.4", | |
| "ssh_port": "22", | |
| "branch": "master", | |
| }, | |
| "test": { | |
| "domain": "192.168.33.5", | |
| "ssh_port": "22", | |
| "branch": "master", | |
| }, | |
| } | |
| class Server(object): | |
| @staticmethod | |
| def upgrade(): | |
| sudo('apt-get update -y') | |
| sudo('apt-get upgrade -y') | |
| @staticmethod | |
| def deps(): | |
| sudo('apt-get install -y %s' % ' '.join(APP["deps"])) | |
| @staticmethod | |
| def restart_services(): | |
| sudo('service nginx restart') | |
| @staticmethod | |
| def clean(): | |
| sudo('apt-get remove %s' % ' '.join(APP["deps"])) | |
| class Conf(object): | |
| @staticmethod | |
| def nginx(): | |
| """ | |
| 1. Configura el servidor web con la plantilla proporcionada. | |
| 2. Crea los enlaces del servidor web. | |
| """ | |
| if exists('%s/sites-enabled/default'%NGINX_DIR): | |
| sudo('rm %s/sites-enabled/default'%NGINX_DIR) | |
| if exists('%s/sites-available/default'%NGINX_DIR): | |
| sudo('rm %s/sites-available/default'%NGINX_DIR) | |
| if exists('%s/sites-enabled/%s'%(NGINX_DIR,APP["url"])): | |
| sudo('rm %s/sites-enabled/%s'%(NGINX_DIR,APP["url"])) | |
| if exists('%s/sites-available/%s'%(NGINX_DIR,APP["url"])): | |
| sudo('rm %s/sites-available/%s'%(NGINX_DIR,APP["url"])) | |
| with lcd(CONFIG_DIR): | |
| with cd('%s/sites-available/'%NGINX_DIR): | |
| upload_template( | |
| filename="./nginx.conf", | |
| destination='%s/sites-available/%s'%(NGINX_DIR,APP["url"]), | |
| template_dir="./", | |
| context={ | |
| "project_url": APP["url"], | |
| "project_path": APP["path"], | |
| }, | |
| use_sudo=True, | |
| ) | |
| sudo('ln -s %s/sites-available/%s \ | |
| %s/sites-enabled/'%(NGINX_DIR,APP["url"],NGINX_DIR)) | |
| @staticmethod | |
| def git(): | |
| """ | |
| 1. Configura el repositorio GIT Base | |
| 2. Crea el hook post-receive para el repositorio. | |
| """ | |
| if exists("/var/www") is False: | |
| sudo("mkdir -p /var/www") | |
| if exists(APP["path"]) is False: | |
| sudo('mkdir %s'%APP["path"]) | |
| with cd(APP["path"]): | |
| sudo('mkdir -p %s.git'%APP["name"]) | |
| with cd('%s.git'%APP["name"]): | |
| sudo('git init --bare') | |
| with lcd(CONFIG_DIR): | |
| with cd('hooks'): | |
| upload_template( | |
| filename="post-receive", | |
| destination=PROJECT_GIT_PATH+"/hooks", | |
| template_dir="./", | |
| context={ | |
| "project_path": APP["path"], | |
| }, | |
| use_sudo=True, | |
| ) | |
| sudo('chmod +x post-receive') | |
| local('git remote remove %s'%env.remote) | |
| local('git remote add %s %s@%s:%s/%s.git'%( | |
| env.remote, APP["user"], env.hosts[0], APP["path"], APP["name"], | |
| )) | |
| @staticmethod | |
| def fix_permissions(): | |
| """ | |
| 1. Repara los permisos de propiedad de la carpeta del proyecto | |
| """ | |
| sudo('chown -R %s %s'%(APP["user"], APP["path"])) | |
| sudo('chown -R %s:%s %s'%( | |
| APP["user"], "www-data", APP["path"], | |
| )) | |
| sudo('chmod -R g+w %s'%APP["path"]) | |
| class Project(object): | |
| @staticmethod | |
| def push(): | |
| """Push cambios del repositorio al servidor principal""" | |
| local("git push %s %s" %(env.remote, env.branch) ) | |
| @staticmethod | |
| def clean(): | |
| """ Desinstala el Proyecto """ | |
| sudo("rm -R %s" % APP["path"] ) | |
| if exists('%s/sites-enabled/%s'%(NGINX_DIR,APP["url"])): | |
| sudo('rm %s/sites-enabled/%s'%(NGINX_DIR,APP["url"])) | |
| if exists('%s/sites-available/%s'%(NGINX_DIR,APP["url"])): | |
| sudo('rm %s/sites-available/%s'%(NGINX_DIR,APP["url"])) | |
| def deploy(*args): | |
| """Deploy APPlication on a server""" | |
| if len(args) <= 0: | |
| servers() | |
| for server in args: | |
| if server in SERVERS.keys(): | |
| env.user = APP["user"] | |
| env.hosts = [SERVERS[server]["domain"], ] | |
| env.path = APP["path"] | |
| env.remote = server | |
| env.branch = SERVERS[server]["branch"] | |
| with settings(warn_only=True): | |
| execute(Project.push, hosts=[SERVERS[server]["domain"], ]) | |
| else: | |
| servers() | |
| def install(*args): | |
| """Init base of APPlication on a server""" | |
| if len(args) <= 0: | |
| servers() | |
| for server in args: | |
| if server in SERVERS.keys(): | |
| env.user = APP["user"] | |
| env.hosts = [SERVERS[server]["domain"], ] | |
| env.path = APP["path"] | |
| env.remote = server | |
| with settings(warn_only=True): | |
| HOST = "%s:%s" % ( | |
| SERVERS[server]["domain"], | |
| SERVERS[server]["ssh_port"], | |
| ) | |
| execute(Project.clean, hosts=[HOST,]) | |
| execute(Server.deps, hosts=[HOST,]) | |
| execute(Conf.git, hosts=[HOST,]) | |
| execute(Conf.nginx, hosts=[HOST,]) | |
| execute(Conf.fix_permissions, hosts=[HOST,]) | |
| execute(Server.restart_services, hosts=[HOST,]) | |
| # execute(Project.push, hosts=[HOST,]) | |
| else: | |
| servers() | |
| def uninstall(*args): | |
| """Clean app and related files od server""" | |
| if len(args) <= 0: | |
| servers() | |
| for server in args: | |
| if server in SERVERS.keys(): | |
| env.user = APP["user"] | |
| env.hosts = [SERVERS[server]["domain"], ] | |
| env.path = APP["path"] | |
| with settings(warn_only=True): | |
| HOST = "%s:%s" % ( | |
| SERVERS[server]["domain"], | |
| SERVERS[server]["ssh_port"], | |
| ) | |
| execute(Project.clean, hosts=[HOST,]) | |
| else: | |
| servers() | |
| def servers(): | |
| print "" | |
| print "---------------------------------------------------------------------" | |
| print "---------------------------- Available SERVERS ----------------------" | |
| print "---------------------------------------------------------------------" | |
| print "" | |
| for server in SERVERS.keys(): | |
| print " :"+server | |
| print "" | |
| print "---------------------------------------------------------------------" | |
| print "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment