def manage_rdo(host, config): """ Installs and enables RDO repo on host in case it is installed locally. """ try: cmd = "rpm -q rdo-release --qf='%{version}-%{release}.%{arch}\n'" rc, out = utils.execute(cmd, use_shell=True) except exceptions.ExecuteRuntimeError: # RDO repo is not installed, so we don't need to continue return match = re.match(r'^(?P<version>\w+)\-(?P<release>\d+\.[\d\w]+)\n', out) version, release = match.group('version'), match.group('release') rdo_url = ("http://rdo.fedorapeople.org/openstack/openstack-%(version)s/" "rdo-release-%(version)s-%(release)s.rpm" % locals()) server = utils.ScriptRunner(host) server.append("(rpm -q 'rdo-release-%(version)s' ||" " yum install -y --nogpg %(rdo_url)s) || true" % locals()) try: server.execute() except exceptions.ScriptRuntimeError as ex: msg = 'Failed to set RDO repo on host %s:\n%s' % (host, ex) raise exceptions.ScriptRuntimeError(msg) reponame = 'openstack-%s' % version server.clear() server.append('yum-config-manager --enable %(reponame)s' % locals()) # yum-config-manager returns 0 always, but returns current setup if succeeds rc, out = server.execute() match = re.search('enabled\s*=\s*(1|True)', out) if not match: msg = ('Failed to set RDO repo on host %s:\nRPM file seems to be ' 'installed, but appropriate repo file is probably missing ' 'in /etc/yum.repos.d/' % host) raise exceptions.ScriptRuntimeError(msg)
def manage_epel(host, config): """ Installs and/or enables EPEL repo if it is required or disables it if it is not required. """ if config['HOST_DETAILS'][host]['os'] in ('Fedora', 'Unknown'): return # yum's $releasever can be non numeric on RHEL, so interpolate here releasever = config['HOST_DETAILS'][host]['release'].split('.')[0] mirrors = ('https://mirrors.fedoraproject.org/metalink?repo=epel-%s&' 'arch=$basearch' % releasever) server = utils.ScriptRunner(host) if config['CONFIG_USE_EPEL'] == 'y': server.append('REPOFILE=$(mktemp)') server.append('cat /etc/yum.conf > $REPOFILE') server.append("echo -e '[packstack-epel]\nname=packstack-epel\n" "enabled=1\nmirrorlist=%(mirrors)s' >> $REPOFILE" % locals()) server.append('( rpm -q --whatprovides epel-release ||' ' yum install -y --nogpg -c $REPOFILE epel-release ) ' '|| true') server.append('rm -rf $REPOFILE') try: server.execute() except exceptions.ScriptRuntimeError as ex: msg = 'Failed to set EPEL repo on host %s:\n%s' % (host, ex) raise exceptions.ScriptRuntimeError(msg) # if there's an epel repo explicitly enables or disables it # according to: CONFIG_USE_EPEL if config['CONFIG_USE_EPEL'] == 'y': cmd = 'enable' enabled = '(1|True)' else: cmd = 'disable' enabled = '(0|False)' server.clear() server.append('yum-config-manager --%(cmd)s epel' % locals()) rc, out = server.execute() # yum-config-manager returns 0 always, but returns current setup # if succeeds match = re.search('enabled\s*\=\s*%(enabled)s' % locals(), out) if match: return msg = 'Failed to set EPEL repo on host %s:\n' if cmd == 'enable': # fail in case user wants to have EPEL enabled msg += ('RPM file seems to be installed, but appropriate repo file is ' 'probably missing in /etc/yum.repos.d/') raise exceptions.ScriptRuntimeError(msg % host) else: # just warn in case disabling failed which might happen when EPEL repo # is not installed at all msg += 'This is OK in case you don\'t want EPEL installed and enabled.' # TO-DO: fill logger name when logging will be refactored. logger = logging.getLogger() logger.warn(msg % host)
def discover(config, messages): """ Discovers details about hosts. """ # TODO: Once Controller is refactored, move this function to it (facter can # be used for that too). details = {} release_regexp = re.compile(r'^(?P<OS>.*) release (?P<release>[\d\.]*)') config['HOST_LIST'] = list(filtered_hosts(config)) for host in config['HOST_LIST']: details.setdefault(host, {}) server = utils.ScriptRunner(host) # discover OS and release server.append('cat /etc/redhat-release') try: rc, out = server.execute() match = release_regexp.search(out) if not match: raise exceptions.ScriptRuntimeError() except exceptions.ScriptRuntimeError: details[host]['os'] = 'Unknown' details[host]['release'] = 'Unknown' else: opsys = match.group('OS') for pattern, surr in [('^Red Hat Enterprise Linux.*', 'RHEL'), ('^Fedora.*', 'Fedora'), ('^CentOS.*', 'CentOS'), ('^Scientific Linux.*', 'SL')]: opsys = re.sub(pattern, surr, opsys) details[host]['os'] = opsys details[host]['release'] = match.group('release') # Create the packstack tmp directory server.clear() server.append("mkdir -p %s" % basedefs.PACKSTACK_VAR_DIR) # Separately create the tmp directory for this packstack run, this will # fail if the directory already exists host_dir = os.path.join(basedefs.PACKSTACK_VAR_DIR, uuid.uuid4().hex) server.append("mkdir --mode 0700 %s" % host_dir) for i in ('modules', 'resources'): server.append("mkdir --mode 0700 %s" % os.path.join(host_dir, i)) server.execute() details[host]['tmpdir'] = host_dir config['HOST_DETAILS'] = details
def bring_up_ifcfg(host, device): """ Brings given device up if it's down. Raises ScriptRuntimeError in case of failure. """ server = utils.ScriptRunner(host) server.append('ip link show up | grep "%s"' % device) try: server.execute() except exceptions.ScriptRuntimeError: server.clear() cmd = 'ip link set dev %s up' server.append(cmd % device) try: server.execute() except exceptions.ScriptRuntimeError: msg = ('Failed to bring up network interface %s on host %s.' ' Interface should be up so OpenStack can work' ' properly.' % (device, host)) raise exceptions.ScriptRuntimeError(msg)