def psql(command): """Issue SQL commands to Postgres""" if type(command) == list: command = '\n'.join(command) with hide('running','output', 'warnings'): put(StringIO(command),'/tmp/psql.sql', use_sudo = True) print "[%s] psql %s" % (env.host,command) with hide('running'): sudo("""bash -c \'su - postgres -s /usr/bin/psql postgres postgres < /tmp/psql.sql\'""") with hide('running','output', 'warnings'): sudo('rm -f /tmp/psql.sql')
def run(self, command='', warn_only=False, cd=None): env.host_string = '%s@%s:%s' % (self.user, self.host, self.port) if self.empty is None: with hide('output'): self.empty = str(run('echo')) if cd is None: cd = '/home/{}'.format(self.user) with (fab_cd(cd)): with fab_settings(warn_only=warn_only): with hide('output'): output = str(run(command)) text = output[len(self.empty)+1:].replace('\r', '').strip() return text
def run(self, command='', warn_only=False, cd=None): env.host_string = '%s@%s:%s' % (self.user, self.host, self.port) if self.empty is None: with hide('output'): self.empty = str(run('echo')) if cd is None: cd = '/home/{}'.format(self.user) with (fab_cd(cd)): with fab_settings(warn_only=warn_only): with hide('output'): output = str(run(command)) text = output[len(self.empty) + 1:].replace('\r', '').strip() return text
def run(self, command="", warn_only=False, cd=None): env.host_string = "%s@%s:%s" % (self.user, self.host, self.port) if self.empty is None: with hide("output"): self.empty = str(run("echo")) if cd is None: cd = "/home/{}".format(self.user) with (fab_cd(cd)): with fab_settings(warn_only=warn_only): with hide("output"): output = str(run(command)) text = output[len(self.empty) + 1:].replace("\r", "").strip() return text
def _copy_tool_to_ovs_node(self): ''' Copies the tool contrail-ovs-tool.sh to the openvswitch node Std ovs-vtep does not have a way to work with non-default db So copy the patched ovs-vtep to the node ''' pwd = os.getcwd() with settings(hide('everything'), host_string='%s@%s' % (self.ssh_username, self.mgmt_ip), password=self.ssh_password): put('%s/tools/tor/contrail-ovs-tool.sh' % (pwd)) put('%s/tools/tor/ovs-vtep' % (pwd), '/usr/share/openvswitch/scripts/ovs-vtep') self.logger.debug('Copied contrail-ovs-tool.sh and ovs-vtep ', ' to %s' % (self.mgmt_ip)) if self.tor_ovs_protocol == 'pssl': self.remote_home = run('pwd') put(self.priv_key_file) put(self.cert_privkey_file) run('rm -f %s' % (self.cacert_file)) self.ssl_args = ' -p %s/sc-privkey.pem -c %s/sc-cert.pem '\ '-b %s ' % (self.remote_home, self.remote_home, self.cacert_file) self.common_cmd_str += '%s' % (self.ssl_args)
def _copy_tool_to_ovs_node(self): ''' Copies the tool contrail-ovs-tool.sh to the openvswitch node Std ovs-vtep does not have a way to work with non-default db So copy the patched ovs-vtep to the node ''' pwd = os.getcwd() with settings(hide('everything'),host_string='%s@%s' % ( self.ssh_username, self.mgmt_ip), password=self.ssh_password): put('%s/tools/tor/contrail-ovs-tool.sh' % (pwd)) put('%s/tools/tor/ovs-vtep' % (pwd), '/usr/share/openvswitch/scripts/ovs-vtep') self.logger.debug('Copied contrail-ovs-tool.sh and ovs-vtep ', ' to %s' % (self.mgmt_ip)) if self.tor_ovs_protocol == 'pssl': self.remote_home = run('pwd') put(self.priv_key_file) put(self.cert_privkey_file) run('rm -f %s' % (self.cacert_file)) self.ssl_args = ' -p %s/sc-privkey.pem -c %s/sc-cert.pem '\ '-b %s ' % (self.remote_home, self.remote_home, self.cacert_file) self.common_cmd_str += '%s' % (self.ssl_args)
def pylint_file(filenames, **kwargs): """ apply pylint to the file specified, return the filename, score """ command = "pylint " if 'rcfile' in kwargs and kwargs['rcfile'] is not None: command += " --rcfile={0} ".format(kwargs['rcfile']) command = command + ' '.join(filenames) # we use fabric to run the pylint command, hiding the normal fab # output and warnings with hide('output', 'running', 'warnings'), settings(warn_only=True): result = local(command, capture=True) score = 0.0 # parse the output from pylint for the score for line in result.split('\n'): if re.match("E....:.", line): LOGGER.info(line) if "Your code has been rated at" in line: score = re.findall("\d+.\d\d", line)[0] score = float(score) return filenames, score
def _test(option, path, use_sudo): if isinstance(path, Path): path = str(path) with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True): return run_or_sudo(f'test {option} {quote(path)}', use_sudo).succeeded
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 run(command, show=True, *args, **kwargs): """ Runs a shell command on the remote server. """ if show: print_command(command) with hide("running"): return _run(command, *args, **kwargs)
def execute_shell_command(self, command, pty=None): with settings(hide('warnings', 'running', 'stdout', 'stderr'), host_string=self.host, user=self.username, password=self.password, warn_only=True): if not pty: result = run(command, shell=True) else: result = run(command, shell=True, pty=False) return result, result.return_code
def setup_database(): """Create the application database and associated user with a temporary password""" with hide('output', 'running'): with settings(warn_only=True): psql("CREATE USER project UNENCRYPTED PASSWORD 'project';") psql("ALTER ROLE project REPLICATION LOGIN;") with settings(warn_only=True): psql("CREATE DATABASE project ENCODING 'UTF-8';") psql("GRANT ALL ON DATABASE project TO project;")
def get_environment(self, environment_name): with settings(hide('warnings', 'running', 'stdout', 'stderr'), host_string=self.host, user=self.username, password=self.password, warn_only=True): command = "echo $%s" % environment_name result = run(command, shell=True) if result.return_code == 0: return result else: return None
def setup_environment(): with hide('running','output','warnings'): setup_repo(repos['pgdg']) setup_repo(repos['dotdeb']) apt_update() map(install,packages['postgres']) map(install,packages['redis']) map(install,packages['base']) map(install,packages['python']) pip_install(packages['pip']) inject_files(configuration_files) shared_redis() postgres_cluster()
def pyflakes_file(filenames, verbose=False): """ _pyflakes_file_ Appyly pyflakes to file specified, return (filenames, score) """ command = 'pyflakes ' + ' '.join(filenames) # we use fabric to run the pyflakes command, hiding the normal fab # output and warnings with hide('output', 'running', 'warnings'), settings(warn_only=True): result = local(command, capture=True) flakes = 0 data = [x for x in result.split('\n') if x.strip()] if len(data) != 0: #We have at least one flake, find the rest flakes = count_flakes(data, verbose) + 1 else: flakes = 0 return filenames, flakes
def get_interface_address(intf='eth0'): """Obtain the IP address of a given interface on a host""" with hide('running','output', 'warnings'): return run('/sbin/ifconfig %s | grep "inet addr"' % intf).strip().split(' ')[0].split(':')[1]
def sudo_execute_shell_command(self, command): with settings(hide('warnings', 'running', 'stdout', 'stderr'), host_string=self.host, user=self.username, password=self.password, warn_only=True): result = sudo(command, user=self.username) return result, result.return_code
def path_existed(self, path): with settings(hide('warnings', 'running', 'stdout', 'stderr'), host_string=self.host, user=self.username, password=self.password, warn_only=True): return files.exists(path)
def upload_directory(self, local_dir, remote_dir): with settings(hide('warnings', 'running', 'stdout', 'stderr'), host_string=self.host, user=self.username, password=self.password, warn_only=True): if not files.exists(remote_dir): self.execute_shell_command("mkdir %s" % remote_dir) # FIX ME ,just support the 1 layer dir put(local_dir, remote_dir)