def _lcmStateToString(self): lcm = self._lcmStateAsInt() if (lcm is not None) and (lcm >= 0) and (lcm < len(self.lcmStateDefintion)): return self.lcmStateDefintion[lcm] else: Util.printError('Invalid state: %s' % lcm, exit=False) return self.invalidState
def _remoteFileAppendContents(self, filename, data): data = Util.escapeDoubleQuotes(data, times=4) rc, output = self._nodeShell('"echo \\"%s\\" >> %s"' % (data, filename), withOutput=True, shell=True) if rc != 0: Util.printError("Failed to append to %s\n%s" % (filename, output))
def getUidGidFromNode(user): rc, output = self._nodeShell(getUidGidCmd % user, withOutput=True) if rc != 0: Util.printError("Error getting '%s' user UID/GID from Node.\n%s" % (user,output)) return _extractUidGidFromGetentPasswdOutput(output)
def sign(self): res, output = self._sign() if res: Util.printError(output, exit=False) self._cleanupTempFile() else: self._renameFiles() return res
def _startDhcp(self): Util.printDetail('(Re)Starting DHCP server.') serviceName = self.packages['dhcp'].initdScriptName rc = self.restartService(serviceName) if rc != 0: Util.printError('Failed to (re)start DHCP service.')
def _installDhcp(self): Util.printDetail("Installing DHCP server.") dhcpPackage = self.getPackageName("dhcp") self.installPackages([dhcpPackage]) if not self.isPackageInstalled(dhcpPackage): Util.printError("Failed to install %s." % dhcpPackage)
def doWork(self): configHolder = ConfigHolder(self.options.__dict__) signator = Signator(self.manifestFile, configHolder) isError = signator.sign() if isError: Util.printError('Error signing metadata file') else: Util.printDetail('Metadata file successfully signed: %s' % signator.outputManifestFile)
def _installDhcp(self): Util.printDetail('Installing DHCP server.') dhcpPackage = self.getPackageName('dhcp') self.installPackages([dhcpPackage]) if not self.isPackageInstalled(dhcpPackage): Util.printError('Failed to install %s.' % dhcpPackage)
def _getManifest(self, resourceUri): url = MarketplaceUtil.metadataUrl(self.marketplaceEndpoint, resourceUri) self._printDetail('Downloading from: %s' % url) try: return self.__getManifest(url) except: reason = ''.join(traceback.format_exception_only(*(sys.exc_info()[:2]))) Util.printError('Failed to get manifest for resource uri: %s. %s' % (url, reason))
def __getIpMacTuplesForNetworkType(self, _type): if _type not in self.NET_TYPES_DHCP: Util.printError('Expected one of: %s. Got %s'%(','.join(self.NET_TYPES_DHCP),_type)) _type = _type.replace(_type[0], _type[0].lower(), 1) ips = [x for x in getattr(self, '%sAddr'%_type).split()] macs = [x for x in getattr(self, '%sMac'%_type).split()] if len(ips) != len(macs): Util.printError('%s network: number of IPs should match number of MACs.'%_type) return zip(ips, macs)
def _remoteAppendOrReplaceInFile(self, filename, search, replace): res = self._nodeShell(['grep', '"%s"'%search, filename]) replace = Util.escapeDoubleQuotes(replace) if self._patternExists(res): rc, output = self._nodeShell('"sed -i \'s|%s|%s|\' %s"' % (search, replace, filename), withOutput=True, shell=True) if rc != 0: Util.printError("Failed to modify %s.\n%s" % (filename, output)) else: self._remoteFileAppendContents(filename, replace)
def _configureDbUser(self, username, password): mysqlCommand = "/usr/bin/mysql -uroot -p%s" % self.oneDbRootPassword userCreate = "CREATE USER '%s'@'localhost' IDENTIFIED BY '%s'" % (username, password) userGrant = "GRANT CREATE, DROP, SELECT, INSERT, DELETE, UPDATE, INDEX ON opennebula.* TO '%s'@'localhost'" % username rc, output = self._execute("%s -e \"%s\"" % (mysqlCommand, userCreate), withOutput=True, shell=True) if rc != 0: Util.printWarning("Couldn't create user '%s'. Already exists?\n%s" % (username, output)) rc, output = self._execute("%s -e \"%s\"" % (mysqlCommand, userGrant), withOutput=True, shell=True) if rc != 0: Util.printError("Error granting permission for user '%s'.\n%s" % (username, output))
def _configureQemuUserOnFrontend(self): """Add qemu user on Fronted with the same UID and GID as on the node being configured. Add qemu user to 'cloud' group both on Frontend and the node. """ if self.shareType != 'nfs': return user = group = 'qemu' getUidGidCmd = "getent passwd %s" Util.printDetail("Configuring '%s' user on Frontend as shared filesystem setup requested." % user) def getUidGidFromNode(user): rc, output = self._nodeShell(getUidGidCmd % user, withOutput=True) if rc != 0: Util.printError("Error getting '%s' user UID/GID from Node.\n%s" % (user,output)) return _extractUidGidFromGetentPasswdOutput(output) def _extractUidGidFromGetentPasswdOutput(output): uid, gid = output.split(':')[2:4] # uid, gid if not all([uid, gid]): Util.printError("Error extracting '%s' user UID/GID from output.\n%s" % (user,output)) return uid, gid uidNode, gidNode = getUidGidFromNode(user) rc, output = self._executeWithOutput((getUidGidCmd % uidNode).split()) if rc == 0: uidLocal, gidLocal = _extractUidGidFromGetentPasswdOutput(output) Util.printDetail("User with UID:%s/GID:%s already configured on Frontend." % (uidLocal, gidLocal), verboseLevel=self.verboseLevel) if gidNode != gidLocal: Util.printError("Frontend user '%s' GID:%s doesn't match GID:%s on Node %s." % (gidLocal, user, gidNode, self.nodeAddr)) else: self._execute(['groupadd', '-g', gidNode, '-r', group]) self._execute(['useradd', '-r', '-u', uidNode, '-g', group, '-d', '/', '-s', '/sbin/nologin', '-c', '"%s user"'%user, user]) # Instruct libvirt to run VMs with GID of ONE group. self.appendOrReplaceInFileCmd(self.qemuConf, '^group.*$', 'group = "%s"' % self.oneGroup)
def doWork(self): configHolder = ConfigHolder(self.options.__dict__) src = self._get_config_path() dst = configHolder.configFile if os.path.exists(dst) and not configHolder.force: Util.printError(self.existingFileMsg % dst) else: try: self._create_parent_dir(dst) self._copy_config(src, dst) Util.printDetail('wrote configuration file: %s' % dst) except Exception as e: Util.printError(e)
def _installCAs(self): packages = [] if self.certificateAuthorityPackages and self.certificateAuthorityRepo: caPackages = map(lambda x: x.strip(), self.certificateAuthorityPackages.split(",")) packages.extend(caPackages) repoConf = "\n".join([line.strip() for line in self.certificateAuthorityRepo.strip().split("\n")]) repoName = self.caRepoName for package in packages: self._updatePackageAndRepoInfo(package, repoName, repoConf) else: packages.append(self.getPackageName("CA")) self.installPackages(packages) for package in packages: if not self.isPackageInstalled(package): Util.printError("Failed to install %s." % package)
def cleanQuarantine(self): self._setPDiskUserCredentials() disks = self.describeVolumes({'quarantine': ['.*']}) threshold = self._getQuarantineThresholdDate() disksToCleanUp = [] for disk in disks: quarantineDate = self._parseQuarantineDate(disk['quarantine']) if quarantineDate < threshold: disksToCleanUp.append(disk) for disk in disksToCleanUp: self._printDetail('Removing disk: %s' % disk['uuid']) try: self.deleteVolume(disk['uuid']) except (ClientException, ServerException), ex: Util.printError(str(datetime.now()) + ' ' + str(ex))
def configureBridgeRemotely(self): def doNotConfigureBridge(): return Util.isFalseConfVal(getattr(self, "nodeBridgeConfigure", True)) if doNotConfigureBridge(): Util.printDetail("Asked not to configure bridge") return checkBridgeCmd = '"brctl show | grep ^%s.*%s$"' % (self.nodeBridgeName, self.nodeNetworkInterface) rc, output = self._nodeShell(checkBridgeCmd, withOutput=True, shell=True) if rc == 0: Util.printDetail("Bridge already configured") return else: Util.printDetail("Bridge is NOT configured. %s" % output) configureBridgeCmd = ( 'nohup "brctl addbr %(bridge)s; sleep 10; ifconfig %(interf)s 0.0.0.0; sleep 10; brctl addif %(bridge)s %(interf)s; sleep 10; dhclient %(bridge)s"' % {"bridge": self.nodeBridgeName, "interf": self.nodeNetworkInterface} ) rc, output = self._nodeShell(configureBridgeCmd, withOutput=True, shell=True) if rc != 0: Util.printDetail("Failed to configure bridge.\n%s" % output) else: sleepTime = 5 Util.printDetail("Sleeping %i sec for the bridge one the node to come up." % sleepTime) time.sleep(sleepTime) Util.printDetail("Testing connection to the node.") rc, output = self._nodeShell("true", withOutput=True) if rc == 0: Util.printDetail("OK.") else: Util.printError("Could not connect to the node after attempt to configre bridge.\n%s" % output) Util.printDetail("Testing if bridge was configured.") rc, output = self._nodeShell(checkBridgeCmd, withOutput=True, shell=True) if rc == 0: Util.printDetail("OK.") self._persistRemoteBridgeConfig(self.nodeNetworkInterface, self.nodeBridgeName) return else: Util.printError("Bridge was not configured.\n%s" % output)
def configureBridgeRemotely(self): def doNotConfigureBridge(): return Util.isFalseConfVal(getattr(self, 'nodeBridgeConfigure', True)) if doNotConfigureBridge(): Util.printDetail('Asked not to configure bridge') return checkBridgeCmd = '"brctl show | grep ^%s.*%s$"' % \ (self.nodeBridgeName, self.nodeNetworkInterface) rc, output = self._nodeShell(checkBridgeCmd, withOutput=True, shell=True) if rc == 0: Util.printDetail('Bridge already configured') return else: Util.printDetail('Bridge is NOT configured. %s' % output) configureBridgeCmd = 'nohup "brctl addbr %(bridge)s; sleep 10; ifconfig %(interf)s 0.0.0.0; sleep 10; brctl addif %(bridge)s %(interf)s; sleep 10; dhclient %(bridge)s"' % \ {'bridge' : self.nodeBridgeName, 'interf' : self.nodeNetworkInterface} rc, output = self._nodeShell(configureBridgeCmd, withOutput=True, shell=True) if rc != 0: Util.printDetail('Failed to configure bridge.\n%s' % output) else: sleepTime = 5 Util.printDetail('Sleeping %i sec for the bridge one the node to come up.' % sleepTime) time.sleep(sleepTime) Util.printDetail('Testing connection to the node.') rc, output = self._nodeShell('true', withOutput=True) if rc == 0: Util.printDetail('OK.') else: Util.printError('Could not connect to the node after attempt to configre bridge.\n%s' % output) Util.printDetail('Testing if bridge was configured.') rc, output = self._nodeShell(checkBridgeCmd, withOutput=True, shell=True) if rc == 0: Util.printDetail('OK.') self._persistRemoteBridgeConfig(self.nodeNetworkInterface, self.nodeBridgeName) return else: Util.printError('Bridge was not configured.\n%s' % output)
def formatVmList(self): configHolder = self.configHolder.copy() result = '' for endpoint, username, password in self.endpoints: result += '::: %s : %s :::' % (endpoint, username) configHolder.set('endpoint', endpoint) configHolder.set('username', username) configHolder.set('password', password) monitor = Monitor(configHolder) self._set_alarm() try: vmList = monitor.listVms() result += monitor.formatVmList(vmList) except Exception, ex: Util.printError(str(ex)) except KeyboardInterrupt: pass
def addRepositories(self, packages): """Accepts package names and aliases as defined in self.packages. """ repos = [] for pkgName in packages: repo = '' if pkgName in self.packages: repo = self.packages[pkgName].repository else: for pkgInfo in self.packages.values(): if pkgInfo.packageName == pkgName: repo = pkgInfo.repository if repo and repo not in repos: repos.append(repo) for repo in repos: if repo in self.extraRepos: filename = self.extraRepos[repo]['filename'] content = self.extraRepos[repo]['content'] Util.filePutContent(filename, content) else: Util.printError("Repository '%s' is not defined in the extra list of repositories (%s)." % \ (repo, ', '.join(self.extraRepos)))
def _compressFile(self, filename, fmt): if fmt.lower() == 'none': return filename if Compressor.getCompressionFormat(filename) != '': Util.printWarning('skipping compression; file appears to already be compressed') return filename compressionCmd = Compressor._getCompressionCommandByFormat(fmt) compressedFilename = '%s.%s' % (filename, fmt) if os.path.isfile(compressedFilename): Util.printWarning('Compressed file %s already exists, skipping' % compressedFilename) return compressedFilename if not os.path.exists(filename): Util.printError('Missing file: ' + filename, exit=True) ret = self._execute([compressionCmd, filename]) if ret != 0: Util.printError('Error compressing file: %s' % compressedFilename, exit=True) return compressedFilename
def _installVomsFiles(self): r = requests.get(self.voIdCardUrl) if r.status_code == requests.codes.ok: if not os.path.exists(self.vomsesDir): try: os.mkdir(self.vomsesDir) except Exception as e: Util.printError("could not create " + vomsesDir) vo_data = ET.fromstring(r.text) for idcard in vo_data: voname = idcard.attrib["Name"] vopath = os.path.join(self.vomsesDir, voname) if not os.path.exists(vopath): try: os.mkdir(vopath) except Exception as e: Util.printError("could not create " + vopath) for server in idcard.findall("./gLiteConf/VOMSServers/VOMS_Server"): hostname = server.find("hostname") dn = server.find("X509Cert/DN") ca_dn = server.find("X509Cert/CA_DN") if hostname is not None and dn is not None and ca_dn is not None: contents = "%s\n%s\n" % (dn.text, ca_dn.text) path = os.path.join(vopath, hostname.text + ".lsc") try: with open(path, "w") as f: f.write(contents) except Exception as e: Util.printError("could not create file " + path) else: Util.printError("error retrieving VO ID card data from " + self.voIdCardUrl)
def _installVomsFiles(self): r = requests.get(self.voIdCardUrl) if r.status_code == requests.codes.ok: if not os.path.exists(self.vomsesDir): try: os.mkdir(self.vomsesDir) except Exception as e: Util.printError('could not create ' + vomsesDir) vo_data = ET.fromstring(r.text) for idcard in vo_data: voname = idcard.attrib['Name'] vopath = os.path.join(self.vomsesDir, voname) if not os.path.exists(vopath): try: os.mkdir(vopath) except Exception as e: Util.printError('could not create ' + vopath) for server in idcard.findall('./gLiteConf/VOMSServers/VOMS_Server'): hostname = server.find('hostname') dn = server.find('X509Cert/DN') ca_dn = server.find('X509Cert/CA_DN') if hostname is not None and dn is not None and ca_dn is not None: contents = '%s\n%s\n' % (dn.text, ca_dn.text) path = os.path.join(vopath, hostname.text + '.lsc') try: with open(path, 'w') as f: f.write(contents) except Exception as e: Util.printError('could not create file ' + path) else: Util.printError('error retrieving VO ID card data from ' + self.voIdCardUrl)
def _printError(self, msg): self._notifyOnError(msg) Util.printError(msg)
def _confgureDhcp(self): def _isAllDhcpGroupsDefined(_groups): return all(_groups.values()) def _getConfGlobals(): _globals = """ ddns-update-style none; ignore unknown-clients; ignore bootp; """ if hasattr(self, 'dhcpNtpServers') and self.dhcpNtpServers: _globals += 'option ntp-servers %s;\n' % self.dhcpNtpServers return _globals def _getConfSubnets(): subnetTemplate = """ subnet %(subnet)s netmask %(netmask)s { option routers %(routers)s; } """ subnet = '' # All net types are defined together with NATing. Assuming NATing for # Local net type. Need to create a shared network. if Util.isTrueConfVal(self.nat) and _isAllDhcpGroupsDefined(dhcpGroups): subnet = """ shared-network StratusLab-LAN { """ for _type in self.NET_TYPES_DHCP: subnet += subnetTemplate % { 'subnet' : getattr(self, self._assembleDhcpAttributeName('%sSubnet' % _type)), 'netmask' : getattr(self, self._assembleDhcpAttributeName('%sNetmask' % _type)), 'routers' : getattr(self, self._assembleDhcpAttributeName('%sRouters' % _type))} subnet += "}\n" elif Util.isTrueConfVal(self.nat) and dhcpGroups['OneLocalNetwork']: subnet = """ shared-network StratusLab-LAN { """ # main interface subnet += """ subnet %(subnet)s netmask %(netmask)s { } """ % {'subnet' : self.dhcpSubnet, 'netmask' : self.dhcpNetmask} # virtual interface natGateway = getattr(self, 'natGateway', '') if not natGateway: natGateway = Util.gatewayIpFromNetAddress(self.natNetwork) subnet += subnetTemplate % {'subnet' : self.natNetwork, 'netmask' : self.natNetmask, 'routers' : natGateway} subnet += "}\n" elif dhcpGroups['OnePublicNetwork']: # main interface subnet += """ subnet %(subnet)s netmask %(netmask)s { } """ % {'subnet' : self.dhcpSubnet, 'netmask' : self.dhcpNetmask} elif dhcpGroups['OneLocalNetwork']: # virtual interface subnet = subnetTemplate % { 'subnet' : self.dhcpOneLocalNetworkSubnet, 'netmask' : self.dhcpOneLocalNetworkNetmask, 'routers' : self.dhcpOneLocalNetworkRouters} else: Util.printWarning('Invalid parameters combination to configure DHCP.') return subnet def _getConfGroups(): groupHeadTemplate = """ group { option broadcast-address %(broadcast)s; option subnet-mask %(netmask)s; option routers %(routers)s; option domain-name "%(domainName)s"; option domain-name-servers %(nameservers)s; """ hostTemplate = """ host %(type)s-vm%(id)s { hardware ethernet %(mac)s; fixed-address %(ip)s; max-lease-time %(leaseTime)s; } """ groups = '' for _type,ipsMacs in dhcpGroups.items(): if not ipsMacs: continue groups += groupHeadTemplate % \ {'broadcast' : getattr(self, self._assembleDhcpAttributeName('%sBroadcast' % _type)), 'netmask' : getattr(self, self._assembleDhcpAttributeName('%sNetmask' % _type)), 'routers' : getattr(self, self._assembleDhcpAttributeName('%sRouters' % _type)), 'domainName' : getattr(self, self._assembleDhcpAttributeName('%sDomainName' % _type)), 'nameservers' : getattr(self, self._assembleDhcpAttributeName('%sDomainNameServers' % _type))} hosts = '' for i,ipMac in enumerate(ipsMacs): hosts += hostTemplate % {'type' : _type.lower(), 'id' : str(i), 'mac' : ipMac[1], 'ip' : ipMac[0], 'leaseTime' : self.dhcpLeaseTime} groups += hosts groups += '}\n' return groups Util.printDetail('Configuring DHCP server.') _NOTHING = [] dhcpGroups = dict.fromkeys(self.NET_TYPES_DHCP, _NOTHING) for netType in self.NET_TYPES_DHCP: if Util.isTrueConfVal(getattr(self, self._assembleDhcpAttributeName(netType), False)): dhcpGroups[netType] = self.__getIpMacTuplesForNetworkType(netType) if not any(dhcpGroups.values()): Util.printError('When configuring DHCP %s networks IP/MAC pairs should be given.' % ','.join(self.NET_TYPES_DHCP)) content = _getConfGlobals() + \ _getConfSubnets() + \ _getConfGroups() confFile = self.getPackageConfigFileName('dhcp') Util.filePutContent(confFile, content)
def startCloudSystem(self): self.stopService('oned') if self.startService('oned'): Util.printError("ONE failed to start") Util.printDetail('Waiting for ONE to finish starting') time.sleep(10)
def _extractUidGidFromGetentPasswdOutput(output): uid, gid = output.split(':')[2:4] # uid, gid if not all([uid, gid]): Util.printError("Error extracting '%s' user UID/GID from output.\n%s" % (user,output)) return uid, gid
def startLibvirt(self): rc, output = self.executeCmd('service libvirtd restart'.split(), withOutput=True) if rc != 0: Util.printError('Could not start libvirt.\n%s' % output)
def printError(self, msg): Util.printError(msg, exit=False)
def _installFetchCrl(self): package = self.getPackageName('fetch-crl') self.installPackages([package]) if not self.isPackageInstalled(package): Util.printError('Failed to install %s.' % package)