def _run_or_abort(self, cmd, host, error_message, tear_down=True, conn_params=None): """Attempt to run a command on the given host. If the command fails, error_message and the process error output will be printed. In addition, if tear_down is True, the tear_down() method will be called and the process will exit with return code 1""" if conn_params: p = EX.SshProcess(cmd, host, conn_params) else: p = EX.SshProcess(cmd, host) p.run() if p.exit_code != 0: logger.warn(error_message) if p.stderr is not None: logger.warn(p.stderr) logger.info(' '.join(p.cmd)) if tear_down: self.tear_down() exit(1)
def patch_sps(): git_download=EX.SshProcess('cd ~/;git clone https://github.com/openstack/nova; cd ~/;git clone https://github.com/openstack/python-novaclient;', hosts[0],connexion_params = {'user': '******'},pty = True).run() #copy_patch_file = EX.SshProcess('scp devstack/patch/sps-nova-patch.diff stack@'+hosts[0]+':nova/;scp devstack/patch/sps-novaclient-patch.diff stack@'+hosts[0]+':python-novaclient/;', # jobs[0][1],connexion_params = EX5.config.default_frontend_connexion_params, pty = True).run() EX.Put([hosts[0]],'~/workspacenova/devstack/patch/sps-nova-patch.diff', '~/nova', connexion_params = {'user': '******'}).run() EX.Put([hosts[0]],'~/workspacenova/devstack/patch/sps-novaclient-patch.diff', '~/python-novaclient', connexion_params = {'user': '******'}).run() patch_git = EX.SshProcess('git config --global user.email "*****@*****.**";git config --global user.name "Yulin Zhang"; cd ~/nova;git branch sps; git checkout sps; git am < sps-nova-patch.diff;cd ~/python-novaclient;git branch sps; git checkout sps; git am < sps-novaclient-patch.diff;', hosts[0],connexion_params = {'user': '******'}).run() print patch_git._process
def backup_patch(): create_patch_file = EX.SshProcess('cd ~/nova;git format-patch master --stdout > sps-nova-patch.diff; cd ~/python-novaclient;git format-patch master --stdout > sps-novaclient-patch.diff; ', hosts[0],connexion_params = {'user': '******'},pty = True).run() #copy_patch_file = EX.SshProcess('scp stack@'+hosts[0]+':nova/sps-nova-patch.diff devstack/patch/;scp stack@'+hosts[0]+':python-novaclient/sps-novaclient-patch.diff devstack/patch/;', # jobs[0][1],connexion_params = EX5.config.default_frontend_connexion_params, pty = True).run() EX.Get([hosts[0]],'~/nova/sps-nova-patch.diff', '~/workspacenova/devstack/patch', connexion_params = {'user': '******'}).run() EX.Get([hosts[0]],'~/python-novaclient/sps-novaclient-patch.diff', '~/workspacenova/devstack/patch', connexion_params = {'user': '******'}).run()
def modify_novaconf(): config_controller=EX.SshProcess('echo -e "\n[default]\nscheduler_driver = nova.scheduler.security_scheduler.FilterScheduler\n"'+ '"scheduler_available_filters=nova.scheduler.filters.all_filters\n"'+ '"scheduler_available_filters=nova.scheduler.filters.isolation_hosts_filter.IsolationHostsFilter\n"'+ '"scheduler_default_filters=IsolationHostsFilter" >> /etc/nova/nova.conf', hosts[0],connexion_params = {'user': '******'}).run()
def ask_ibrate(host, debug=False): """ same as ask_node(), but filter for IB '40' or '56' only builtin cmd: 'ibstat | grep Rate' return 'FDR10', 'FDR56' or 'No connection' """ cmd = 'ibstat | grep Rate' process = execo.SshProcess(cmd, host, {'user': '******'}).run() if process.stdout == '': return 'No connection' else: result = False attendu = ['40', 'FDR10'] retour = process.stdout.split() # parcours de liste (for i in ... if i in) en intension # for elt in retour: # if elt in attendu: # result = True result = [True for x in retour if str(x) in attendu] if result: return '<= FDR10 !!' else: return 'FDR56'
def get_data(host, debug=False): """ use execo as a better subprocess substitute """ cmd = "cat /sys/class/dmi/id/product_serial" process = execo.SshProcess(cmd, host, {'user': '******'}, shell=False).run() if debug: print(process.stderr) return process.stdout
def install_devstack(): install_stack_controller=EX.SshProcess('cd ~/devstack;./stack.sh', hosts[0],connexion_params = {'user': '******'},pty = True).run() install_stack_compute=EX.Remote('cd ~/devstack;./stack.sh', hosts[1:],connexion_params = {'user': '******'},pty = True).run() for p in install_stack_compute.processes(): print p.stdout() print hosts
def ask_node(cmd, host, debug=False): """ using execo, connect to host and execute cmd cmd be like: ibv_devinfo | grep fw_ver => '\tfw_ver:\t\t\t\t2.11.310\r\n' ibv_devinfo | grep board_id => '\tboard_id:\t\t\tHP_0280210019\r\n' return the last element of stdout, as a str """ process = execo.SshProcess(cmd, host, {'user': '******'}).run() if process.stdout == '': return 'No connection' else: return str(process.stdout.split()[-1]) # last of list
def setup_host(self): """Deploy a node, install dependencies and Rally""" logger.info('Deploying environment %s on %s' % (style.emph(self.config['env-name']), self.host) + (' (forced)' if self.options.force_deploy else '')) deployment = None if 'env-user' not in self.config or self.config['env-user'] == '': deployment = EX5.Deployment(hosts=[self.host], env_name=self.config['env-name']) else: deployment = EX5.Deployment(hosts=[self.host], env_name=self.config['env-name'], user=self.config['env-user']) deployed_hosts, _ = EX5.deploy( deployment, check_deployed_command=not self.options.force_deploy) # Test if rally is installed test_p = EX.SshProcess('rally version', self.host, {'user': '******'}) test_p.ignore_exit_code = True test_p.nolog_exit_code = True test_p.run() if test_p.exit_code != 0: # Install rally self._run_or_abort( "curl -sO %s" % RALLY_INSTALL_URL, self.host, "Could not download Rally install script from %s" % RALLY_INSTALL_URL, conn_params={'user': '******'}) logger.info("Installing dependencies on deployed host") self._run_or_abort('apt-get update && apt-get -y update', self.host, 'Could not update packages on host', conn_params={'user': '******'}) self._run_or_abort('apt-get -y install python-pip', self.host, 'Could not install pip on host', conn_params={'user': '******'}) self._run_or_abort('pip install --upgrade setuptools', self.host, 'Could not upgrade setuptools', conn_params={'user': '******'}) logger.info("Installing rally from %s" % style.emph(self.config['rally-git'])) self._run_or_abort("bash install_rally.sh -y --url %s" % self.config['rally-git'], self.host, 'Could not install Rally on host', conn_params={'user': '******'}) else: logger.info("Rally %s is already installed" % test_p.stdout.rstrip()) # Setup the deployment file vars = { "controller": self.config['os-services']['controller'], "os_region": self.config['authentication']['os-region'], "os_username": self.config['authentication']['os-username'], "os_password": self.config['authentication']['os-password'], "os_tenant": self.config['authentication']['os-tenant'], "os_user_domain": self.config['authentication']['os-user-domain'], "os_project_domain": self.config['authentication']['os-project-domain'] } rally_deployment = self._render_template( 'templates/deployment_existing.json', vars) EX.Put([self.host], [rally_deployment], remote_location='deployment_existing.json', connection_params={ 'user': '******' }).run() # Create a Rally deployment self._run_or_abort( "rally deployment create --filename deployment_existing.json " "--name %s" % self.config['deployment-name'], self.host, 'Could not create the Rally deployment', conn_params={'user': '******'}) self.rally_deployed = True logger.info("Rally has been deployed correctly")
def configure_devstack(): update = EX.Remote('apt-get update;apt-get install python-software-properties -y;add-apt-repository cloud-archive:havana -y; apt-get update;', hosts, connexion_params = {'user': '******'}).run() add_stack_user = EX.Remote('apt-get install -y git sudo;groupadd stack;useradd -g stack -s /bin/bash -d /opt/stack -m stack;'+ 'echo "stack ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers;'+ ' mkdir /opt/stack/.ssh/; cp /root/.ssh/authorized_keys /opt/stack/.ssh/;'+ ' chmod 700 ~/.ssh;'+ 'echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyYjfgyPazTvGpd8OaAvtU2utL8W6gWC4JdRS1J95GhNNfQd657yO6s1AH5KYQWktcE6FO/xNUC2reEXSGC7ezy+sGO1kj9Limv5vrvNHvF1+wts0Cmyx61D2nQw35/Qz8BvpdJANL7VwP/cFI/p3yhvx2lsnjFE3hN8xRB2LtLUopUSVdBwACOVUmH2G+2BWMJDjVINd2DPqRIA4Zhy09KJ3O1Joabr0XpQL0yt/I9x8BVHdAx6l9U0tMg9dj5+tAjZvMAFfye3PJcYwwsfJoFxC8w/SLtqlFX7Ehw++8RtvomvuipLdmWCy+T9hIkl+gHYE4cS3OIqXH7f49jdJf [email protected]" >> ~/.ssh/authorized_keys', hosts, connexion_params = {'user': '******'}).run() for host in hosts: proxy_config = EX.SshProcess("export ip=`/sbin/ifconfig br100 | sed '/inet\ /!d;s/.*r://g;s/\ .*//g'`;"+'echo -e "http_proxy=http://proxy.reims.grid5000.fr:3128/\nhttps_proxy=http://proxy.reims.grid5000.fr:3128/\nip=$ip">> /etc/environment;' ,host, connexion_params = {'user': '******'},pty = True).run() #reset = EX.SshProcess('echo -e "PATH=\\\"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games\\\"" > /etc/environment;' #,host, connexion_params = {'user': '******'},pty = True).run() ip_process = EX.Remote('host {{{host}}}', hosts).run() i=0 hs=[] for p in ip_process.processes(): ip = p.stdout().split(' ')[3] ip=str(ip) ip=ip.replace('\r\n', '') hs=hs+[{'host':hosts[i],'ip':ip}] i=i+1 ip_split=hs[0]['ip'].split('.') for host in hs: no_proxy_config= EX.SshProcess('echo -e "PATH=\\\"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games\\\"\nno_proxy=\\\"localhost,127.0.0.0,"'+host['ip']+'"\\\"" >> /etc/environment',host['host'], connexion_params = {'user': '******'},pty = True).run() print no_proxy_config.stderr() download_devstack = EX.Remote('cd ~;git clone https://github.com/openstack-dev/devstack;', hosts, connexion_params = {'user': '******'}).run() config_controller=EX.SshProcess('echo -e "HOST_IP=$ip\n"'+ '"FLAT_INTERFACE=eth0\n"'+ '"FIXED_RANGE=10.4.128.0/20\n"'+ '"FIXED_NETWORK_SIZE=4096\n"'+ #'"FLOATING_RANGE="'+ip_split[0]+'"."'+ip_split[1]+'"."'+ip_split[2]+'".128/25\n"'+ '"FLOATING_RANGE=10.36.66.0/26\n"'+ '"MULTI_HOST=1\n"'+ '"LOGFILE=/opt/stack/logs/stack.sh.log\n"'+ '"GIT_BASE=https://github.com\n"'+ '"ADMIN_PASSWORD=pass\n"'+ '"MYSQL_PASSWORD=pass\n"'+ '"RABBIT_PASSWORD=pass\n"'+ '"SERVICE_PASSWORD=pass\n"'+ '"SERVICE_TOKEN=s4c\n"'+ '"DISABLE_SERVICE=n-cpu" > /opt/stack/devstack/localrc', hosts[0],connexion_params = {'user': '******'}).run() config_compute=EX.Remote('echo -e "HOST_IP=$ip\n"'+ '"FLAT_INTERFACE=eth0\n"'+ '"FIXED_RANGE=10.4.128.0/20\n"'+ '"FIXED_NETWORK_SIZE=4096\n"'+ #'"FLOATING_RANGE="'+ip_split[0]+'"."'+ip_split[1]+'"."'+ip_split[2]+'".128/25\n"'+ '"FLOATING_RANGE=10.36.66.0/26\n"'+ '"MULTI_HOST=1\n"'+ '"LOGFILE=/opt/stack/logs/stack.sh.log\n"'+ '"GIT_BASE=https://github.com\n"'+ '"ADMIN_PASSWORD=pass\n"'+ '"MYSQL_PASSWORD=pass\n"'+ '"RABBIT_PASSWORD=pass\n"'+ '"SERVICE_PASSWORD=pass\n"'+ '"SERVICE_TOKEN=s4c\n"'+ '"DATABASE_TYPE=mysql\n"'+ '"SERVICE_HOST="'+hs[0]['ip']+'"\n"'+ '"MYSQL_HOST="'+hs[0]['ip']+'"\n"'+ '"RABBIT_HOST="'+hs[0]['ip']+'"\n"'+ '"GLANCE_HOSTPORT="'+hs[0]['ip']+'":9292\n"'+ '"ENABLED_SERVICES=n-cpu,n-net,n-api,rabbit,c-sch,c-api,c-vol\n" > /opt/stack/devstack/localrc', hosts[1:],connexion_params = {'user': '******'}).run()
def get_args(): """ get arguments from CLI """ parser = argparse.ArgumentParser(description='Create IB Map >> file') parser.add_argument('-d', '--debug', action='store_true', help='toggle debug on') parser.add_argument('host', type=str, help='host(s), nodeset syntax') return parser.parse_args() if __name__ == '__main__': args = get_args() if args.debug: execo.log.logger.setLevel('DEBUG') else: execo.log.logger.setLevel('ERROR') nodes = NodeSet(args.host) for node in nodes: try: process = execo.SshProcess(CMD, node, {'user': '******', 'nolog_error': True, 'ignore_error': True}, shell=True).run() except execo.exception.ProcessesFailed: execo.log.logger.exception('Process error') sys.exit(1) if process.stdout == '': print('No connection {}'.format(node)) else: # only need short GUID, might redond with awk print('H-{} {}'.format(process.stdout.split('0x')[-1].strip(), node))