def npm(command): with fab.prefix("umask 0002"): if env.npm_local: with fab.lcd(env.npm_root): fab.local("sudo -u %s npm %s" % (env.user, command)) else: with fab.cd(env.npm_root): helpers.remote("npm %s" % command)
def update_conf(): '''Updates the uwsgi conf file on the server, using the template. Leaves a backup file in the uwsgi conf directory with a .bak extension. Use this to roll back if necessary''' with fab.prefix("umask 0002"): helpers.remote("mkdir -p %s" % env.uwsgi_conf_path) upload_template(env.uwsgi_conf_template, os.path.join(env.uwsgi_conf_path, "uwsgi.yml"), context=env, mode=0664)
def setup_virtualenv(virtualenv_path): virtualenv_cmd = ["virtualenv", "--distribute", "--no-site-packages", "-p %s" % os.path.join(env.virtualenv_python_dir, 'python'), "%s" % virtualenv_path] with fab.path(env.virtualenv_python_dir, behavior='prepend'): with fab.prefix("umask 0002"): helpers.remote(" ".join(virtualenv_cmd))
def make_workspace_file(): """Create a tag file that announces that a particular release is was made from a working copy, rather than from version control.""" ws_file = os.path.join(env.current_path, "WORKSPACE_RELEASE") ws_host = fab.local("hostname", capture=True) ws_string = "Installed from %s@%s:%s at %s" % (os.environ['USER'], ws_host, os.environ['PWD'], env.release_name) helpers.remote("echo \"%s\" > %s" % (ws_string, ws_file))
def setup_virtualenv(virtualenv_path): virtualenv_cmd = ["virtualenv", "--distribute", "--no-site-packages", "-p python2.7", "%s" % virtualenv_path] # TODO: extract to variable with fab.path("/seq/annotation/development/tools/python/2.7.1/bin", behavior='prepend'): with fab.prefix("umask 0002"): helpers.remote(" ".join(virtualenv_cmd))
def start(): '''Start the nginx instance.''' if running(): fab.abort("nginx pidfile already exists: %(host)s:%(nginx_pidfile)s" % env) command = [ os.path.join(env.servers_path, "bin", "nginx"), "-c %(nginx_conf)s" % env, ] with fab.cd(env.current_path): helpers.remote(" ".join(command))
def make_shared_children_symlinks(release_path): ''' Create symlinks the release we just checked out pointing to the shared children''' for child in env.shared_children: child_path = os.path.join(release_path, child) with fab.settings(fab.hide('warnings'), warn_only=True): helpers.remote("test -L %s && rm %s" % (child_path, child_path)) helpers.remote("ln -s %s %s" % ( os.path.join(env.shared_path, child), child_path ))
def update_conf(): with fab.prefix("umask 0002"): helpers.makedirs(os.path.dirname(env.monit_conf)) helpers.remote("mv %(monit_conf)s{,.bak}" % env) upload_template(env.monit_conf_template, env.monit_conf, context=env, backup=False, mode=0664) helpers.remote("mv %(monit_conf)s %(monit_conf)s.tmp" % env) helpers.remote("cp %(monit_conf)s.tmp %(monit_conf)s" % env) helpers.remote("rm %(monit_conf)s.tmp" % env) helpers.remote("chmod 700 %(monit_conf)s" % env)
def run(task, sudo=None, shell=True, pty=True, combine_stderr=True): ''' Runs an invoke task in a remote environment. Pass the task as a string WITH argument parameters. If sudo is desired, pass either a string username, or True to respect the sudo conventions outlined in `cotton.helpers.remote()`. ''' command = ' '.join([ 'invoke', task, ]) with fab.prefix("umask 0002"): with fab.prefix(env.activate_virtualenv): with fab.cd(env.current_path): if isinstance(sudo, basestring): return fab.sudo( command, shell, pty, combine_stderr, user=sudo ) elif sudo: return helpers.remote( command, shell=shell, pty=pty, combine_stderr=combine_stderr ) else: return fab.run(command, shell, pty, combine_stderr)
def run(task, sudo=None, shell=True, pty=True, combine_stderr=True): ''' Runs an invoke task in a remote environment. Pass the task as a string WITH argument parameters. If sudo is desired, pass either a string username, or True to respect the sudo conventions outlined in `cotton.helpers.remote()`. ''' command = ' '.join([ 'invoke', task, ]) with fab.prefix("umask 0002"): with fab.prefix(env.activate_virtualenv): with fab.cd(env.current_path): if isinstance(sudo, basestring): return fab.sudo(command, shell, pty, combine_stderr, user=sudo) elif sudo: return helpers.remote(command, shell=shell, pty=pty, combine_stderr=combine_stderr) else: return fab.run(command, shell, pty, combine_stderr)
def start(command_prefix=None, linked_libraries=None): '''Start the uwsgi instance.''' # command_prefix : environmental commands like dotkit (local) # linked_libraries : library prepending due to sudo-shell (remote) if running(): fab.abort("uwsgi pidfile already exists: %(uwsgi_pidfile)s" % env) command = [] if linked_libraries is not None: command = [ "LD_LIBRARY_PATH=%s" % ":".join(linked_libraries) ] command.extend([ "uwsgi", "--yaml %s" % os.path.join(env.uwsgi_conf_path, "uwsgi.yml") ]) if command_prefix is not None: command.insert(0, command_prefix + " && ") with fab.cd(env.current_path): with fab.prefix("umask 0002"): with fab.prefix(env.activate_virtualenv): helpers.remote(" ".join(command))
def make_directories(): with fab.prefix("umask 0002"): helpers.remote("mkdir -p %(deploy_to)s" % env) with fab.cd(env.deploy_to): helpers.remote("mkdir -p %(releases_dir)s" % env) helpers.remote("mkdir -p %(shared_dir)s" % env) make_shared_children_dirs()
def start(command_prefix=None, linked_libraries=[]): '''Start the uwsgi instance.''' # command_prefix : environmental commands like dotkit (local) # linked_libraries : library prepending due to sudo-shell (remote) if running(): fab.abort("uwsgi pidfile already exists: %(uwsgi_pidfile)s" % env) # TODO: ARRRRRG you can't source files in sudo # As long as we're using (0.9,1.1,1.2) built with our custom-build pcre # library, we need to tell uwsgi where to find this linked_libraries.append(PCRE) command = [ "LD_LIBRARY_PATH=%s" % ":".join(linked_libraries), os.path.join(env.servers_path, "bin", "uwsgi"), "--yaml %s" % os.path.join(env.uwsgi_conf_path, "uwsgi.yml") ] if command_prefix: command.insert(0, command_prefix + " && ") with fab.cd(env.current_path): with fab.prefix("umask 0002"): helpers.remote(" ".join(command))
def install_config(release_path, config_dir='config', config_extension='py', local_basename='local', command='ln -s'): config_dir = os.path.join(release_path, config_dir) if config_extension.startswith('.'): config_extension = config_extension[1:] paths = { "cmd": command, "deploy": os.path.join( release_path, env.config_dir, env.environments_dir, "%s.%s" % (env.configuration_name, config_extension) ), "local": os.path.join( config_dir, "%s.%s" % (local_basename, config_extension) ) } with fab.settings(fab.hide('warnings'), warn_only=True): helpers.remote("test -L %(local)s && rm %(local)s" % paths) helpers.remote("test -e %(deploy)s && %(cmd)s %(deploy)s %(local)s" % paths)
def install_config(release_path, config_dir='config', config_extension='py', local_basename='local', command='ln -s'): config_dir = os.path.join(release_path, config_dir) if config_extension.startswith('.'): config_extension = config_extension[1:] paths = { "cmd": command, "deploy": os.path.join( env.config_environments_path, "%s.%s" % (env.configuration_name, config_extension) ), "local": os.path.join( config_dir, "%s.%s" % (local_basename, config_extension) ) } with fab.settings(fab.hide('warnings'), warn_only=True): helpers.remote("test -L %(local)s && rm %(local)s" % paths) helpers.remote("test -e %(deploy)s && %(cmd)s %(deploy)s %(local)s" % paths)
def make_symlinks(release_path): '''Create a 'current' symlink pointing to a release we just checked out, and symlinks within pointing to the shared children''' with fab.settings(fab.hide('warnings'), warn_only=True): helpers.remote("test -L %(current_path)s && rm %(current_path)s" % env) helpers.remote("ln -s %s %s" % (release_path, env.current_path)) for child in env.shared_children: child_path = os.path.join(release_path, child) with fab.settings(fab.hide('warnings'), warn_only=True): helpers.remote("test -L %s && rm %s" % (child_path, child_path)) helpers.remote("ln -s %s %s" % ( os.path.join(env.shared_path, child), child_path ))
def install_workspace(): fab.local(choose_local_tar() + (" -czf %(release_name)s.tar *" % env)) with fab.prefix("umask 0002"): fab.run("mkdir -p %(release_path)s" % env) fab.put("%(release_name)s.tar" % env, env.release_path) with fab.cd(env.release_path): helpers.remote("tar -zxf %(release_name)s.tar" % env) helpers.remote("rm %(release_name)s.tar" % env) fab.local("rm %(release_name)s.tar" % env) helpers.remote("rm -f %(release_path)s/config/local.py" % env)
def pip(command): with fab.prefix("umask 0002"): with fab.prefix(env.activate_virtualenv): helpers.remote("pip %s" % command)
def httpd_cmd(cmd, sudo): c = [env.httpd_init_script, cmd, env.httpd_service_name] if sudo: c.insert(0, 'sudo') helpers.remote(' '.join(c))
def nginx_cmd(cmd, sudo): c = [env.nginx_init_script.format(cmd)] if sudo: c.insert(0, 'sudo') helpers.remote(' '.join(c))
def monit(command=None): with fab.prefix("umask 0002"): c = [os.path.join(env.monit_root, 'bin', 'monit')] if command: c.append(command) helpers.remote(' '.join(c))
def make_shared_children_dirs(): for child in env.shared_children: with fab.cd(env.shared_path): helpers.remote("test -d %s || mkdir %s" % (child, child))
def make_current_symlink(release_path): '''Create a 'current' symlink pointing to a release we just checked out. ''' with fab.settings(fab.hide('warnings'), warn_only=True): helpers.remote("test -L %(current_path)s && rm %(current_path)s" % env) helpers.remote("ln -s %s %s" % (release_path, env.current_path))
def httpd_cmd(cmd, sudo): c = [ env.httpd_init_script, cmd, env.httpd_service_name ] if sudo: c.insert(0, 'sudo') helpers.remote(' '.join(c))
def nginx_cmd(cmd, sudo): c = [ env.nginx_init_script.format(cmd) ] if sudo: c.insert(0, 'sudo') helpers.remote(' '.join(c))
def authenticate(): with fab.settings(fab.hide('running')): fab.run('echo "Authenticating..."') with fab.settings(fab.hide('stdout')): helpers.remote('echo -n')