def stop(): """Stop the remote site using supervisor """ site = _env.hostout.options.get('hostname') if _env.hostout.options.get('remote-sudo') == 'true': _sudo('supervisorctl stop {0:s}:*'.format(site)) else: _run('supervisorctl stop {0:s}:*'.format(site))
def restart(): """Restart the remote site using supervisor """ site = _env.hostout.options.get('hostname') if _env.hostout.options.get('remote-sudo') == 'true': _sudo('supervisorctl restart %s:*' % site) else: _run('supervisorctl restart %s:*' % site)
def sudo(command, show=True, *args, **kwargs): """ Runs a command as sudo on the remote server. """ if show: print_command(command) with hide("running"): return _sudo(command, *args, **kwargs)
def remote(cmd, pty=False, user=None, sudo=False): """ Executes the given remote command on the hosts list set in the current context. If user is given or sudo evaluates to True, then a fabric.operations.sudo call is made, otherwise a simple fabric.operations.run. """ if state.env.cwd: cmd = 'cd {0} && {1}'.format(state.env.cwd, cmd) with _context(user=settings.VM_USER, password=settings.VM_PASSWORD): for host in state.env.hosts or all_hosts(): with _context(host_string=host): if user or sudo: _sudo(cmd, user=user, pty=pty) else: _run(cmd, pty=pty)
def cook_resources(): """Cook plone resources on remote """ buildout_directory = _env.hostout.options.get('path') assert buildout_directory, u'No path found for the selected hostout' annotations = annotate() buildout_name = annotations['buildoutname'] cmd = '{0:s}/bin/instance -O {1:s} run `which resourcecooker.py`'.format( buildout_directory, buildout_name ) res = _sudo(cmd, warn_only=True) if res.failed: cmd = cmd.replace('/bin/instance -O', '/bin/instance1 -O') _sudo(cmd, warn_only=True)
def deploy(): """Deploys the local buildout to the remote site """ # Push the code push() deploy_etc() # Restart cmds = filter(bool, _env.hostout.options.get('restart').split('\n')) assert cmds, u'No restart commands found for the selected hostout' for cmd in cmds: if _env.hostout.options.get('remote-sudo') == 'true': _sudo(cmd) else: _run(cmd)
def deploy_etc(): """Copy system config from parts/system/etc to /etc """ buildout_directory = _env.hostout.options['path'] annotations = annotate() buildout_sub_directory = lambda x: os.path.join(buildout_directory, x) parts_directory = buildout_sub_directory(annotations['parts-directory']) if os.path.isdir('%s/system/etc' % parts_directory): cmd = 'cp -R %s /etc;supervisorctl reread;supervisorctl update' % \ (parts_directory + '/system/etc/*') if _env.hostout.options.get('remote-sudo') == 'true': _sudo(cmd) else: _run(cmd)
def deploy_supervisor(): """Update the remote supervisor configuration. Supervisord configuration path must be defined by setting a hostout-option ``supervisor-conf``. """ supervisor_conf = _env.hostout.options.get('supervisor-conf') assert supervisor_conf, \ u'No supervisor_conf found for the selected hostout' # Sync annotations = annotate() parts_directory = annotations.get('parts-directory') _rsync(supervisor_conf, os.path.join(parts_directory, os.path.basename(supervisor_conf)), reverse=True, delete=False) # Update if _env.hostout.options.get('remote-sudo') == 'true': _sudo('supervisorctl update') else: _run('supervisorctl update')
def restart(): if _is_local(env.host): _local("sudo apachectl graceful") else: _sudo("apache2ctl graceful")
def push(): """Push the local buildout results (without data) to the remote site. """ # TODO: Currently all remote directories are chown for effective-user. # We should remote this for everything else except var-directory buildout_directory = _env.hostout.options.get('path') fallback_user = _env.user or 'root' effective_user = _env.hostout.options.get('effective-user', fallback_user) remote_sudo = _env.hostout.options.get('remote-sudo') == 'true' assert buildout_directory, u'No path found for the selected hostout' buildout_sub_directory = lambda x: os.path.join(buildout_directory, x) var_directory = buildout_sub_directory('var') # Make sure that the buildout directory exists on the remote if remote_sudo: _sudo('mkdir -p {0:s}'.format(var_directory)) _sudo('chown {0:s} {1:s}'.format(effective_user, buildout_directory)) _sudo('chown {0:s} {1:s}'.format(effective_user, var_directory)) else: _run('mkdir -p {0:s}'.format(var_directory)) _run('chown {0:s} {1:s}'.format(effective_user, buildout_directory)) _run('chown {0:s} {1:s}'.format(effective_user, var_directory)) # Push annotations = annotate() bin_directory = buildout_sub_directory(annotations['bin-directory']) eggs_directory = buildout_sub_directory(annotations['eggs-directory']) parts_directory = buildout_sub_directory(annotations['parts-directory']) products_directory = buildout_sub_directory('products') for directory in [bin_directory, eggs_directory, parts_directory]: _rsync(directory, os.path.join(directory, '*'), reverse=True, delete=False) # Chown cmd = 'chown -R {0:s} {1:s}'.format(effective_user, directory) if remote_sudo: _sudo(cmd) else: _run(cmd) if os.path.isdir(products_directory): _rsync(products_directory, os.path.join(products_directory, '*'), reverse=True, delete=False) # Chown cmd = 'chown -R {0:s} {1:s}'.format(effective_user, products_directory) if remote_sudo: _sudo(cmd) else: _run(cmd) _rsync(var_directory, os.path.join(var_directory, '*'), reverse=True, delete=False, exclude=('blobstorage*', '*.fs', '*.old', '*.zip', '*.log', '*.backup'), extra_opts='--ignore-existing') # Push 'etc' (created by some buildout scripts) etc_directory = os.path.join(buildout_directory, 'etc') if os.path.exists(etc_directory): _rsync(etc_directory, os.path.join(etc_directory, '*'), reverse=True, delete=False) # Chown if os.path.exists(etc_directory): cmd = 'chown -R {0:s} {1:s}'.format(effective_user, etc_directory) if remote_sudo: _sudo(cmd) else: _run(cmd)