def stage(): """Update the local staged buildout """ # Pull pull() # Update update() # Bootstrap bootstrap() # Buildout buildout() # Restart if _env.hostout.options.get('local-restart') == "true": local_sudo = _env.hostout.options.get('local-sudo') == "true" 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 local_sudo: cmd = 'sudo {0:s}'.format(cmd) if _output.running: print('[localhost] restart: {0:s}'.format(cmd)) _local(cmd)
def stage_supervisor(): """Update the local 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' local_sudo = _env.hostout.options.get('local-sudo') == "true" # Configure annotations = annotate() parts_directory = annotations['parts-directory'] cmd = 'cp {0:s} {1:s}'.format( os.path.join(parts_directory, os.path.basename(supervisor_conf)), supervisor_conf ) if local_sudo: cmd = 'sudo {0:s}'.format(cmd) if _output.running: print('[localhost] stage_supervisor: {0:s}'.format(cmd)) _local(cmd) # Update cmd = 'supervisorctl update' if local_sudo: cmd = 'sudo {0:s}'.format(cmd) if _output.running: print('[localhost] stage_supervisor: {0:s}'.format(cmd)) _local(cmd)
def update(branch=None): """Update the local buildout from its mercurial repository. """ buildout_directory = _env.hostout.options.get('path') fallback_user = _env.user or 'root' buildout_user = _env.hostout.options.get('buildout-user', fallback_user) local_sudo = _env.hostout.options.get('local-sudo') == "true" assert buildout_directory, u'No path found for the selected hostout' # Pull with _lcd(buildout_directory): cmd = 'hg pull' cmd = 'su {0:s} -c "{1:s}"'.format(buildout_user, cmd) if local_sudo: cmd = 'sudo {0:s}'.format(cmd) if _output.running: print('[localhost] update: {0:s}'.format(cmd)) _local(cmd) # Update branch = bool(branch) and ' {0:s}'.format(branch) or '' with _lcd(buildout_directory): cmd = 'hg update -C{0:s}'.format(branch) cmd = 'su {0:s} -c "{1:s}"'.format(buildout_user, cmd) if _env.hostout.options.get('local-sudo') == 'true': cmd = 'sudo {0:s}'.format(cmd) if _output.running: print('[localhost] update: {0:s}'.format(cmd)) _local(cmd)
def local(cmd): """ Executes a local command without capturing and returning the output. Thin wraper around the fabric.operations.local function """ return _local(cmd, capture=False)
def bootstrap(): """Execute bootstrap for the local buildout. The default effective python could be overridden by setting ``bootstrap-python`` -hostout-option with a path to an another python executable. """ buildout_directory = _env.hostout.options.get('path') fallback_user = _env.user or 'root' buildout_user = _env.hostout.options.get('buildout-user', fallback_user) local_sudo = _env.hostout.options.get('local-sudo') == "true" assert buildout_directory, u'No path found for the selected hostout' buildout_python = _env.hostout.options.get('executable') bootstrap_python = ( _env.hostout.options.get('bootstrap-python') or buildout_python ) # Bootstrap with _lcd(buildout_directory): cmd = '{0:s} bootstrap.py --distribute'.format(bootstrap_python) cmd = 'su {0:s} -c "{1:s}"'.format(buildout_user, cmd) if local_sudo: cmd = 'sudo {0:s}'.format(cmd) if _output.running: print('[localhost] bootstrap: %s' % cmd) with _settings(warn_only=True): res = _local(cmd) if res.failed: print('First bootstrap failed: we have a new bootstrap which ' 'has --distribute option now default. Trying again...') cmd = '{0:s} bootstrap.py'.format(bootstrap_python) cmd = 'su {0:s} -c "{1:s}"'.format(buildout_user, cmd) if local_sudo: cmd = 'sudo {0:s}'.format(cmd) if _output.running: print('[localhost] bootstrap: %s' % cmd) _local(cmd)
def clone(repository, branch=None): """Clone a new local buildout from a mercurial repository. """ buildout_directory = _env.hostout.options.get('path') fallback_user = _env.user or 'root' buildout_user = _env.hostout.options.get('buildout-user', fallback_user) local_sudo = _env.hostout.options.get('local-sudo') == "true" assert buildout_directory, u'No path found for the selected hostout' # Clone branch = branch and ' -r {0:s}'.format(branch) or '' cmd = 'hg clone {0:s}{1:s} {2:s}'.format(repository, branch, buildout_directory) cmd = 'su {0:s} -c "{1:s}"'.format(buildout_user, cmd) if local_sudo: cmd = 'sudo {0:s}'.format(cmd) if _output.running: print('[localhost] clone: {0:s}'.format(cmd)) _local(cmd)
def pull(): """Pull the data from the remote site into the local buildout. """ buildout_directory = _env.hostout.options.get('path') fallback_user = _env.user or 'root' effective_user = _env.hostout.options.get('effective-user', fallback_user) local_sudo = _env.hostout.options.get('local-sudo') == "true" assert buildout_directory, u'No path found for the selected hostout' var_directory = os.path.join(buildout_directory, 'var') filestorage_directory = os.path.join(var_directory, 'filestorage') # Ensure filestorage if not os.path.exists(var_directory): cmd = 'mkdir -p {0:s}'.format(filestorage_directory) if local_sudo: cmd = 'sudo {0:s}'.format(cmd) if _output.running: print('[localhost] pull: {0:s}'.format(cmd)) _local(cmd) # Pull filestorage _rsync(os.path.join(filestorage_directory, 'Data.fs'), os.path.join(filestorage_directory, 'Data.fs'), delete=True) # Pull blobstorage _rsync(os.path.join(var_directory, 'blobstorage'), var_directory, delete=True) # Chown var-directory var_directory = os.path.join(buildout_directory, 'var') cmd = 'chown -R {0:s} {1:s}'.format(effective_user, var_directory) if local_sudo: cmd = 'sudo {0:s}'.format(cmd) if _output.running: print('[localhost] pull: {0:s}'.format(cmd)) _local(cmd)
def buildout(*args): """Execute the local buildout """ buildout_directory = _env.hostout.options.get('path') fallback_user = _env.user or 'root' buildout_user = _env.hostout.options.get('buildout-user', fallback_user) effective_user = _env.hostout.options.get('effective-user', fallback_user) local_sudo = _env.hostout.options.get('local-sudo') == "true" assert buildout_directory, u'No path found for the selected hostout' # Configure offline = '-o' in args and ' -o' or '' parts = [arg for arg in args if not arg.startswith('-')] parts = parts and ' install {0:s}'.format(' '.join(parts)) or '' # Chown var-directory var_directory = os.path.join(buildout_directory, 'var') cmd = 'chown -R {0:s} {1:s}'.format(buildout_user, var_directory) if local_sudo: cmd = 'sudo {0:s}'.format(cmd) if _output.running: print('[localhost] pull: {0:s}'.format(cmd)) _local(cmd) # Buildout with _lcd(buildout_directory): cmd = 'bin/buildout{0:s}{1:s}'.format(parts, offline) cmd = 'su {0:s} -c "{1:s}"'.format(buildout_user, cmd) if local_sudo: cmd = 'sudo {0:s}'.format(cmd) if _output.running: print('[localhost] buildout: {0:s}'.format(cmd)) _local(cmd) # Chown var-directory var_directory = os.path.join(buildout_directory, 'var') cmd = 'chown -R {0:s} {1:s}'.format(effective_user, var_directory) if local_sudo: cmd = 'sudo {0:s}'.format(cmd) if _output.running: print('[localhost] pull: {0:s}'.format(cmd)) _local(cmd)
def local(cmd): if hasattr(env, 'capture_default'): _local(cmd, env.capture_default) else: _local(cmd)
def local(cmd): if hasattr(env,'capture_default'): _local(cmd, env.capture_default) else: _local(cmd)
def _rsync(from_path, to_path, reverse=False, exclude=(), delete=False, extra_opts="", ssh_opts="", capture=False): """Perform rsync from some remote location to some local location. Optionally does exactly the reverse (syncs from some local location to some remote location) """ # Adapted from: # https://github.com/fabric/fabric/blob/master/fabric/contrib/project.py # Create --exclude options from exclude list exclude_opts = ' --exclude "%s"' * len(exclude) # Double-backslash-escape exclusions = tuple([str(s).replace('"', '\\\\"') for s in exclude]) # Honor SSH key(s) key_string = '' keys = _key_filenames() if keys: key_string = '-i ' + ' -i '.join(keys) # Port user, host, port = _normalize(_env.host_string) if host.startswith('@'): host = host[1:] port_string = '-p {0:s}'.format(port) # RSH rsh_string = '' rsh_parts = [key_string, port_string, ssh_opts] if any(rsh_parts): rsh_string = '--rsh="ssh {0:s}"'.format(' '.join(rsh_parts)) # Set remote sudo if _env.hostout.options.get('remote-sudo') == 'true': remote_sudo = ' --rsync-path="sudo rsync"' extra_opts = (extra_opts + remote_sudo).strip() # Set up options part of string options_map = { 'delete': '--delete' if delete else '', 'exclude': exclude_opts % exclusions, 'rsh': rsh_string, 'extra': extra_opts } options = ('%(delete)s%(exclude)s -pthlrz ' '%(extra)s %(rsh)s') % options_map # Interpret direction and define command if not reverse: cmd = 'rsync {0:s} {1:s}@{2:s}:{3:s} {4:s}'.format( options, user, host, from_path, to_path) else: cmd = 'rsync {0:s} {1:s} {2:s}@{3:s}:{4:s}'.format( options, to_path, user, host, from_path) if _env.hostout.options.get('local-sudo') == 'true': cmd = 'sudo {0:s}'.format(cmd) if _output.running: print('[localhost] rsync: {0:s}'.format(cmd)) return _local(cmd, capture=capture)