def get_local_services(linst, sname=None): """ Iterates over the local services on a host creating a dictionary with the service name as the key and status, path, architecture and port as the value. If name is not None then it ensures that only the named services is retrieved. Args linst = smf.AISCF() name = service name Returns a service dictionary made up of a list of dictionary of services. { service1: [ { 'status':on1, 'path':path1, 'arch':arch1, 'port':port1 }, ... ], ... } the width of the widest service name Raises None """ width = 0 sdict = {} for akey in linst.services.keys(): serv = smf.AIservice(linst, akey) # ensure that the current service has the keys we need. # if not then report the error and exit. if not (has_key(serv, 'service_name') and has_key(serv, 'status') and has_key(serv, 'image_path') and has_key(serv, 'txt_record')): sys.stderr.write(_('%s: error: SMF service key ' 'property does not exist\n') % \ os.path.basename(sys.argv[0])) sys.exit(1) servicename = serv['service_name'] info = {'status': '', 'arch': '', 'port': '', 'path': ''} # if a service name is passed in then # ensure it matches the current name if not sname or sname == servicename: width = max(len(servicename), width) info['status'] = serv['status'] info['path'] = serv['image_path'] info['port'] = serv['txt_record'].split(':')[-1] info['arch'] = which_arch(info['path']) if sdict.has_key(servicename): slist = sdict[servicename] slist.extend([info]) sdict[servicename] = slist else: sdict[servicename] = [info] return sdict, width
def tearDown(self): '''Take down test environment''' if (self.service_name is not None and self.smf_instance is not None and aismf.is_pg(self.service_name)): service = smf.AIservice(self.smf_instance, self.service_name) # pylint: disable-msg=E1101 service.instance.del_service(service.serviceName) shutil.rmtree(self.dummy_service_path)
def parse_options(): ''' Parse and validate options when called as delete-service Args: None Returns: A tuple of an AIservice object representing service to delete and an options object ''' parser = OptionParser(usage=_("usage: %prog [options] install_service")) parser.add_option("-x", "--delete-image", dest="deleteImage", action="store_true", default=False, help=_("remove service image, if " + "not otherwise in use")) (options, args) = parser.parse_args() # check that we got the install service's name passed in if len(args) != 1: parser.print_help() sys.exit(1) serviceName = args[0] # check the system has the AI install SMF service available try: smf_instance = smf.AISCF(FMRI="system/install/server") except KeyError: raise SystemExit( _("Error:\tThe system does not have the " + "system/install/server SMF service")) # check the service exists if not serviceName in smf_instance.services.keys(): raise SystemExit( _("Error:\tThe specified service does not exist: %s") % serviceName) # return the AIservice object return ((smf.AIservice(smf_instance, serviceName)), options)
# check the system has the AI install SMF service available try: smf_instance = smf.AISCF(FMRI="system/install/server") except KeyError: parser.error( _("The system does not have the " "system/install/server SMF service.\n")) # check the service exists if options.service_name not in smf_instance.services.keys(): parser.error( _("The specified service does not exist: %s\n") % options.service_name) # store AIservice object in options.service options.service = smf.AIservice(smf_instance, options.service_name) # if image_path not specified, set it to the SMF service's image_path if not options.image_path: try: options.image_path = options.service['image_path'] except KeyError: parser.error( _("The specified service does not have an image_path " "property; please provide one.\n")) # set options.image to be an AIImage object image = com.AIImage(dir_path=options.image_path) # else, an image path was passed in, ensure it is the same architecture # as the service else:
def get_service_manifests(sname, linst): """ Iterate through all the manifests for the named service (sname) pointed to by the SCF service. Args sname = service name inst = smf.AISCF() Returns a dictionary of the criteria for the named service within a list: { servicename1:[ { 'arch':arch1, 'mem':memory1, 'ipv4':ipaddress1, 'mac':macaddr1, 'platform':platform1, 'network':network1 'cpu':cpu1 }, ... ] } * Note1: platform, network and cpu are currently not-implemented upstream. * Note2: could simply use a list of dictionaries but implemented as a dictionary of a list of dictionary which will allow for multiple services to be listed at the same time. width of longest manifest name width of longest criteria Raises None """ sdict = {} width = 0 cwidth = 0 # ensure the named service is in our service dictionary. lservices = linst.services.keys() if sname in lservices: serv = smf.AIservice(linst, sname) if not has_key(serv, 'txt_record'): sys.stderr.write(_('%s: error: SMF service key ' 'property does not exist\n') % \ os.path.basename(sys.argv[0])) sys.exit(1) port = serv['txt_record'].split(':')[-1] path = os.path.join('/var/ai', str(port), 'AI.db') if os.path.exists(path): try: maisql = AIdb.DB(path) maisql.verifyDBStructure() aiqueue = maisql.getQueue() for name in AIdb.getManNames(aiqueue): sdict[name] = [] instances = AIdb.numInstances(name, aiqueue) for instance in range(0, instances): criteria = AIdb.getManifestCriteria(name, instance, aiqueue, humanOutput = True, onlyUsed = True) width = max(len(name), width) tdict, twidth = get_criteria_info(criteria) cwidth = max(twidth, cwidth) sdict[name].extend([tdict]) except Exception, err: sys.stderr.write(_('%s: error: AI database access ' 'error\n%s\n') % \ (os.path.basename(sys.argv[0]), err)) sys.exit(1) else: sys.stderr.write(_('%s: error: unable to locate ' 'AI database on server for %s\n') % \ (os.path.basename(sys.argv[0]), sname)) sys.exit(1)
def get_manifest_names(linst): """ Iterate through the services from smf.AISCF() retrieving all the stored manifest names. Args inst = smf.AISCF() Returns a dictionary of service manifests within a list: { servicename1:[ manifest1, manifest2, ...], ... } the width of the longest service name Raises None """ width = 0 sdict = {} lservices = linst.services.keys() lservices.sort() for akey in lservices: serv = smf.AIservice(linst, akey) # ensure that the current service has the keys we need. # if not then continue with the next service. if not (has_key(serv, 'service_name') and has_key(serv, 'txt_record')): sys.stderr.write(_('%s: error: SMF service key ' 'property does not exist\n') % os.path.basename(sys.argv[0])) sys.exit(1) sname = serv['service_name'] port = serv['txt_record'].split(':')[-1] path = os.path.join('/var/ai', str(port), 'AI.db') if os.path.exists(path): try: maisql = AIdb.DB(path) maisql.verifyDBStructure() for name in AIdb.getManNames(maisql.getQueue()): width = max(len(sname), width) if sdict.has_key(sname): slist = sdict[sname] slist.extend([name]) sdict[sname] = slist else: sdict[sname] = [name] except Exception, err: sys.stderr.write(_('%s: error: AI database ' 'access error\n%s\n') % \ (os.path.basename(sys.argv[0]), err)) sys.exit(1) else: sys.stderr.write(_('%s: error: unable to locate ' 'AI database on server\n') % \ os.path.basename(sys.argv[0])) sys.exit(1)
query = AIdb.DBrequest(query, commit=True) db.getQueue().put(query) query.waitAns() # in case there's an error call the response function (which # will print the error) query.getResponse() if __name__ == '__main__': gettext.install("ai", "/usr/lib/locale") options = parse_options() # Get the SMF service object for the install service specified. try: svc = smf.AIservice(smf.AISCF(FMRI="system/install/server"), options.service_name) except KeyError: raise SystemExit( _("Error: Failed to find service %s") % options.service_name) # Get the install service's data directory and database path try: port = svc['txt_record'].rsplit(':')[-1] except KeyError: raise SystemExit( _("SMF data for service %s is corrupt.\n") % options.service_name) service_dir = os.path.abspath("/var/ai/" + port) database = os.path.join(service_dir, "AI.db") # Check that the service directory and database exist if not (os.path.isdir(service_dir) and os.path.exists(database)):