def install_opstools_repo(shell, config, info, messages): """Installs OpsTools release repo RPM on all deployment hosts.""" global _OPSTOOLS_REPO_VR if not config['repos/install_repo']: return # parsing installed OpsTools release on localhost: we want to proceed # with this only once basename = _OPSTOOLS_REPO_BASE if not _OPSTOOLS_REPO_VR: click.echo('Parsed OpsTools release version: ', nl=False) rc, out, err = execute( "rpm -q {basename} --qf='%{{version}}-%{{release}}\n'".format( **locals() ), use_shell=True, ) match = re.match( r'^(?P<version>\w+)\-(?P<release>\d+)\n', out ) version, release = match.group('version'), match.group('release') _OPSTOOLS_REPO_VR = (version, release) click.echo('{version}-{release}'.format(**locals())) else: version, release = _OPSTOOLS_REPO_VR click.echo('Installing OpsTools release on host {0}'.format(shell.host)) repo_url = _OPSTOOLS_REPO_URL.format(**locals()) rc, out, err = shell.execute( '(rpm -q "{basename}-{version}-{release}" || ' 'yum install -y --nogpg {repo_url}) || true'.format(**locals()) )
def test_shell(self): """[Utils] Test shell""" # We need to override _register method RemoteShell._connections['127.0.0.1'] = FakeSSHClient() shell = RemoteShell('127.0.0.1') # Test passing cmd execution rc, out, err = shell.execute('pass') self.assertEqual(rc, 0) self.assertEqual(out, 'passed') self.assertEqual(err, 'passed') # Test failing cmd execution rc, out, err = shell.execute('fail', can_fail=False) self.assertEqual(rc, 1) self.assertEqual(out, 'failed') self.assertEqual(err, 'failed') self.assertRaises(RuntimeError, shell.execute, 'fail') # Test passing script execution rc, out, err = shell.run_script(['pass']) self.assertEqual(rc, 0) self.assertEqual(out, 'passed') self.assertEqual(err, 'passed') # Test failing script execution rc, out, err = shell.run_script(['fail'], can_fail=False) self.assertEqual(rc, 1) self.assertEqual(out, 'failed') self.assertEqual(err, 'failed') self.assertRaises(RuntimeError, shell.run_script, ['fail']) # Test masking mask_list = ['masked'] try: shell.execute('fail masked string', mask_list=mask_list) except RuntimeError as ex: self.assertEqual(str(ex), 'Failed to run following command ' 'on host 127.0.0.1: fail %s string' % STR_MASK) chout = FakeChannelFile() chout.output = ['This is masked', 'This should be also masked'] stdout, solog = shell._process_output('stdout', chout, mask_list, []) self.assertEqual(solog, '%s\nThis is %s\nThis should be also %s' % (OUTFMT % 'stdout', STR_MASK, STR_MASK)) # Test execute rc, out, err = execute('foo bar') self.assertEqual(out, 'passed') rc, out, err = execute(['ssh', 'bash -x']) self.assertEqual(out, 'passed')
def install_rdo_repo(shell, config, info, messages): """Installs RDO release repo RPM on all deployment hosts and enables testing repo in case it is required. """ global _RDO_REPO_VR if not config['repos/install_rdo']: return # parsing installed RDO release on localhost: we want to proceed with this # only once if not _RDO_REPO_VR: click.echo('Parsed RDO release version: ', nl=False) rc, out, err = execute( "rpm -q rdo-release --qf='%{version}-%{release}.%{arch}\n'", use_shell=True, ) match = re.match( r'^(?P<version>\w+)\-(?P<release>\d+\.[\d\w]+)\n', out ) version, release = match.group('version'), match.group('release') _RDO_REPO_VR = (version, release) click.echo('{version}-{release}'.format(**locals())) else: version, release = _RDO_REPO_VR click.echo('Installing RDO release on host {0}'.format(shell.host)) rdo_url = _RDO_REPO_URL.format(**locals()) rc, out, err = shell.execute( '(rpm -q "rdo-release-{version}" || yum install -y --nogpg {rdo_url})' ' || true'.format(**locals()) ) # install RDO repo on all hosts first and then proceed with enabling # proper repo greenlet.getcurrent().parent.switch() click.echo('Enabling proper repo on host {0}'.format(shell.host)) shell.execute('(rpm -q "yum-utils" || yum install -y yum-utils) || true') reponame = 'openstack-{}'.format(version) if config['repos/enable_rdo_testing']: cmd = 'yum-config-manager --enable {reponame}' else: cmd = ( 'yum-config-manager --disable {reponame}; ' 'yum-config-manager --enable {reponame}-testing' ) rc, out, err = shell.execute(cmd.format(**locals()), can_fail=False) match = re.search('enabled\s*=\s*(1|True)', out) if not match: msg = ( 'Failed to enable proper RDO repo on host {shell.host}:\n' 'RPM file seems to be installed, but appropriate repo file ' 'is probably missing in /etc/yum.repos.d/'.format(**locals()) ) raise tht_exceptions.PluginShellRuntimeError( msg, cmd=cmd, rc=rc, stdout=out, stderr=err )
def test_shell(self): """[Utils] Test shell""" # We need to override _register method RemoteShell._connections['127.0.0.1'] = FakeSSHClient() shell = RemoteShell('127.0.0.1') # Test passing cmd execution rc, out, err = shell.execute('pass') self.assertEqual(rc, 0) self.assertEqual(out, 'passed') self.assertEqual(err, 'passed') # Test failing cmd execution rc, out, err = shell.execute('fail', can_fail=False) self.assertEqual(rc, 1) self.assertEqual(out, 'failed') self.assertEqual(err, 'failed') self.assertRaises(RuntimeError, shell.execute, 'fail') # Test passing script execution rc, out, err = shell.run_script(['pass']) self.assertEqual(rc, 0) self.assertEqual(out, 'passed') self.assertEqual(err, 'passed') # Test failing script execution rc, out, err = shell.run_script(['fail'], can_fail=False) self.assertEqual(rc, 1) self.assertEqual(out, 'failed') self.assertEqual(err, 'failed') self.assertRaises(RuntimeError, shell.run_script, ['fail']) # Test masking mask_list = ['masked'] try: shell.execute('fail masked string', mask_list=mask_list) except RuntimeError as ex: self.assertEqual( str(ex)[:55], '[127.0.0.1] Failed to run command:\nfail %s string' % STR_MASK) # Test execute rc, out, err = execute('foo bar') self.assertEqual(out, 'passed') rc, out, err = execute(['ssh', 'bash -x']) self.assertEqual(out, 'passed')
def test_shell(self): """[Utils] Test shell""" # We need to override _register method RemoteShell._connections['127.0.0.1'] = FakeSSHClient() shell = RemoteShell('127.0.0.1') # Test passing cmd execution rc, out, err = shell.execute('pass') self.assertEqual(rc, 0) self.assertEqual(out, 'passed') self.assertEqual(err, 'passed') # Test failing cmd execution rc, out, err = shell.execute('fail', can_fail=False) self.assertEqual(rc, 1) self.assertEqual(out, 'failed') self.assertEqual(err, 'failed') self.assertRaises(RuntimeError, shell.execute, 'fail') # Test passing script execution rc, out, err = shell.run_script(['pass']) self.assertEqual(rc, 0) self.assertEqual(out, 'passed') self.assertEqual(err, 'passed') # Test failing script execution rc, out, err = shell.run_script(['fail'], can_fail=False) self.assertEqual(rc, 1) self.assertEqual(out, 'failed') self.assertEqual(err, 'failed') self.assertRaises(RuntimeError, shell.run_script, ['fail']) # Test masking mask_list = ['masked'] try: shell.execute('fail masked string', mask_list=mask_list) except RuntimeError as ex: self.assertEqual( str(ex)[:55], '[127.0.0.1] Failed to run command:\nfail %s string' % STR_MASK ) # Test execute rc, out, err = execute('foo bar') self.assertEqual(out, 'passed') rc, out, err = execute(['ssh', 'bash -x']) self.assertEqual(out, 'passed')
def install_rdo_repo(shell, config, info, messages): """Installs RDO release repo RPM on all deployment hosts and enables testing repo in case it is required. """ global _RDO_REPO_VR if not config['repos/install_rdo']: return # parsing installed RDO release on localhost: we want to proceed with this # only once if not _RDO_REPO_VR: click.echo('Parsed RDO release version: ', nl=False) rc, out, err = execute( "rpm -q rdo-release --qf='%{version}-%{release}.%{arch}\n'", use_shell=True, ) match = re.match(r'^(?P<version>\w+)\-(?P<release>\d+\.[\d\w]+)\n', out) version, release = match.group('version'), match.group('release') _RDO_REPO_VR = (version, release) click.echo('{version}-{release}'.format(**locals())) else: version, release = _RDO_REPO_VR click.echo('Installing RDO release on host {0}'.format(shell.host)) rdo_url = _RDO_REPO_URL.format(**locals()) rc, out, err = shell.execute( '(rpm -q "rdo-release-{version}" || yum install -y --nogpg {rdo_url})' ' || true'.format(**locals())) # install RDO repo on all hosts first and then proceed with enabling # proper repo greenlet.getcurrent().parent.switch() click.echo('Enabling proper repo on host {0}'.format(shell.host)) shell.execute('(rpm -q "yum-utils" || yum install -y yum-utils) || true') reponame = 'openstack-{}'.format(version) if config['repos/enable_rdo_testing']: cmd = 'yum-config-manager --enable {reponame}' else: cmd = ('yum-config-manager --disable {reponame}; ' 'yum-config-manager --enable {reponame}-testing') rc, out, err = shell.execute(cmd.format(**locals()), can_fail=False) match = re.search('enabled\s*=\s*(1|True)', out) if not match: msg = ('Failed to enable proper RDO repo on host {shell.host}:\n' 'RPM file seems to be installed, but appropriate repo file ' 'is probably missing in /etc/yum.repos.d/'.format(**locals())) raise tht_exceptions.PluginShellRuntimeError(msg, cmd=cmd, rc=rc, stdout=out, stderr=err)