def main(argv=None): ############################################################################## # Get a parser from omni that understands omni options ############################################################################## parser = omni.getParser() parser.set_usage("%s [options] username" % sys.argv[0]) options, args = omni.parse_args(sys.argv[1:], parser=parser) # pull username from the command line if len(args) > 0: username = args[0].strip() else: username = None # if not username: # sys.exit( "Must provide a username as the first argument of script" ) ############################################################################## # And now call omni, and omni sees your parsed options and arguments # (1) Run equivalent of 'omni.py listmyslices username' # (2) For each returned slicename run equivalent of: # 'omni.py print_slice_expiration slicename' ############################################################################## # (1) Run equivalent of 'omni.py listmyslices username' if username: text, sliceList = omni.call(['listmyslices', username], options) else: text, sliceList = omni.call(['listmyslices'], options) username = "******" # print some summary info printStr = "=" * 80 + "\n" if len(sliceList) > 0: printStr += "User %s has %d slice(s):\n" % (username, len(sliceList)) else: printStr += "User %s has NO slices\n" % (username) # (2) For each returned slicename run equivalent of: # 'omni.py print_slice_expiration slicename' for slicename in sliceList: omniargs = [] omniargs.append('print_slice_expiration') omniargs.append(slicename) text, expiration = omni.call(omniargs, options) printStr += "%s\n" % (str(expiration)) printStr += "=" * 80 return printStr
def main(argv=None): ############################################################################## # Get a parser from omni that understands omni options ############################################################################## parser = omni.getParser() parser.set_usage("%s [options] username"%sys.argv[0]) options, args = omni.parse_args(sys.argv[1:], parser=parser) # pull username from the command line if len(args) > 0: username = args[0].strip() else: username = None # if not username: # sys.exit( "Must provide a username as the first argument of script" ) ############################################################################## # And now call omni, and omni sees your parsed options and arguments # (1) Run equivalent of 'omni.py listmyslices username' # (2) For each returned slicename run equivalent of: # 'omni.py print_slice_expiration slicename' ############################################################################## # (1) Run equivalent of 'omni.py listmyslices username' if username: text, sliceList = omni.call( ['listmyslices', username], options ) else: text, sliceList = omni.call( ['listmyslices'], options ) username = "******" # print some summary info printStr = "="*80+"\n" if len(sliceList)>0: printStr += "User %s has %d slice(s):\n"%(username, len(sliceList)) else: printStr += "User %s has NO slices\n"%(username) # (2) For each returned slicename run equivalent of: # 'omni.py print_slice_expiration slicename' for slicename in sliceList: omniargs = [] omniargs.append('print_slice_expiration') omniargs.append(slicename) text, expiration = omni.call( omniargs, options ) printStr += "%s\n"%(str(expiration)) printStr += "="*80 return printStr
def main(argv=None): ############################################################################## # Get a parser from omni that understands omni options ############################################################################## parser = omni.getParser() # update usage for help message omni_usage = parser.get_usage() parser.set_usage(omni_usage+"\nrenewSliceAndSlivers.py renews the given slice and all slivers at aggregates known to the GENI CH for 60 days.\n " + "Takes slice name as argument") # options is an optparse.Values object, and args is a list options, args = omni.parse_args(sys.argv[1:], parser=parser) sliceName = args[0] newDate = datetime.datetime.utcnow() + datetime.timedelta(days=60) # Strip fractional seconds from times to avoid errors at PG AMs newDate = newDate.replace(microsecond=0) retcode = 0 for command in ['renewslice', 'renewsliver']: # Here we use --raise-error-on-v2-amapi-error. Note though that if 1 AM has a problem, the script stops. Is that what we want? # IE will all AMs return code 0 if they renew the slice alap? # Could supply arg '--warn' to turn down logging. But then we'd want this script to have Omni write to a log file. omniargs = ['--alap', '--useSliceAggregates', '--raise-error-on-v2-amapi-error', command, sliceName, "'%s'" % newDate.isoformat()] print "Calling Omni to renew slice %s%s until %sZ\n" % (sliceName, (" slivers" if command=="renewsliver" else ""), newDate.isoformat()) try: text, retItem = omni.call(omniargs, options) except OmniError, oe: print "\n ***** Omni call failed: %s\n" % oe retcode = str(oe) continue print text print "\n"
def main(argv=None): ############################################################################## # Get a parser from omni that understands omni options ############################################################################## parser = omni.getParser() # update usage for help message omni_usage = parser.get_usage() parser.set_usage( omni_usage + "\naddMemberToSliceAndSlivers.py adds " + "the given member to the given slice and installs " + "their SSH keys on all slivers known to the CH plus at all specified aggregates.\n " + "Takes slice name and username as arguments") # options is an optparse.Values object, and args is a list options, args = omni.parse_args(sys.argv[1:], parser=parser) if len(args) < 2: print "Usage: addMemberToSliceAndSlivers.py <slicename> <username>" sys.exit(-1) sliceName = args[0] userName = args[1] omniargs = ['addslicemember', sliceName, userName] print "Calling Omni to add %s to slice %s\n" % (userName, sliceName) try: text, retItem = omni.call(omniargs, options) if not retItem: print "\nFailed to add member to slice: %s" % text sys.exit(-1) except OmniError, oe: print "\nOmni call failed: %s\n" % oe sys.exit(-1)
def main(argv=None): ############################################################################## # Get a parser from omni that understands omni options ############################################################################## parser = omni.getParser() # update usage for help message omni_usage = parser.get_usage() parser.set_usage(omni_usage+"\naddMemberToSliceAndSlivers.py adds " + "the given member to the given slice and installs " + "their SSH keys on all slivers known to the CH plus at all specified aggregates.\n " + "Takes slice name and username as arguments") # options is an optparse.Values object, and args is a list options, args = omni.parse_args(sys.argv[1:], parser=parser) if len(args) < 2: print "Usage: addMemberToSliceAndSlivers.py <slicename> <username>" sys.exit(-1) sliceName = args[0] userName = args[1] omniargs = ['addslicemember', sliceName, userName] print "Calling Omni to add %s to slice %s\n" % (userName, sliceName) try: text, retItem = omni.call(omniargs, options) if not retItem: print "\nFailed to add member to slice: %s" % text sys.exit(-1) except OmniError, oe: print "\nOmni call failed: %s\n" % oe sys.exit(-1)
def getSliverStatus( amUrl, amType ) : tmpoptions = copy.deepcopy(options) tmpoptions.aggregate = [amUrl] # Run equivalent of 'omni.py sliverstatus username' if tmpoptions.api_version >=3: # For AM API v3 and later: # Run equivalent of 'omni.py status slicename' argv = ['status', slicename] else: # For AM API v1 or v2: # Run equivalent of 'omni.py sliverstatus slicename' argv = ['sliverstatus', slicename] try: text, sliverStatus = omni.call( argv, tmpoptions ) except (oe.AMAPIError, oe.OmniError) : print "ERROR: There was an error executing sliverstatus, review the logs." sys.exit(-1) if not sliverStatus: print "ERROR: Got no SliverStatus for AM %s; check the logs. Message: %s" % (amUrl, text) sys.exit(-1) if not sliverStatus.has_key(amUrl): if len(sliverStatus.keys()) == 1 : newAmUrl = sliverStatus.keys()[0] print "WARN: Got result for AM URL %s instead of %s - did Omni redirect you?" % (newAmUrl, amUrl) amUrl = newAmUrl else: print "ERROR: Got no SliverStatus for AM %s; check the logs." % (amUrl) sys.exit(-1) return sliverStatus[amUrl]
def main(argv=None): ############################################################################## # Get a parser from omni that understands omni options ############################################################################## parser = omni.getParser() # set usage for help message parser.set_usage( "renewSliceAndSlivers.py [-r projectname] <slicename>\n" + "renewSliceAndSlivers.py renews the given slice and all slivers at aggregates known to the GENI CH for 60 days.\n" + "Uses standard Omni config file and standard omni commandline options. Run 'omni -h' for details." ) if len(sys.argv[1:]) == 0 or (len(sys.argv[1:]) == 1 and str(sys.argv[1]).lower() in ("-h", "-?", "--help")): parser.print_usage() return 0 # options is an optparse.Values object, and args is a list options, args = omni.parse_args(sys.argv[1:], parser=parser) if not args or len(args) < 1: parser.print_usage() return 1 sliceName = args[0] newDate = datetime.datetime.utcnow() + datetime.timedelta(days=60) # Strip fractional seconds from times to avoid errors at PG AMs newDate = newDate.replace(microsecond=0) retcode = 0 for command in ["renewslice", "renewsliver"]: # Here we use --raise-error-on-v2-amapi-error. Note though that if 1 AM has a problem, the script stops. Is that what we want? # IE will all AMs return code 0 if they renew the slice alap? # Could supply arg '--warn' to turn down logging. But then we'd want this script to have Omni write to a log file. omniargs = [ "--alap", "--useSliceAggregates", "--raise-error-on-v2-amapi-error", command, sliceName, "'%s'" % newDate.isoformat(), ] print "Calling Omni to renew slice %s%s until %sZ\n" % ( sliceName, (" slivers" if command == "renewsliver" else ""), newDate.isoformat(), ) try: text, retItem = omni.call(omniargs, options) except OmniError, oe: print "\n ***** Omni call failed: %s\n" % oe retcode = str(oe) continue print text print "\n"
def main(argv=None): parser = omni.getParser() # Parse Options (options, args) = parser.parse_args() text, obj = omni.call(args, options) if type(obj) == type({}): obj2 = {} for key, value in obj.items(): obj2[str(key)] = value else: obj2 = obj # serialize using json jsonObj = json.dumps((text, obj2), indent=4) print jsonObj
def main(argv=None): parser = omni.getParser() # Parse Options (options, args) = parser.parse_args() text, obj = omni.call( args, options ) if type(obj) == type({}): obj2 = {} for key, value in obj.items(): obj2[str(key)]=value else: obj2 = obj # serialize using json jsonObj = json.dumps( (text, obj2), indent=4 ) print jsonObj
def main(argv=None): ############################################################################## # Get a parser from omni that understands omni options ############################################################################## parser = omni.getParser() # update usage for help message omni_usage = parser.get_usage() parser.set_usage( omni_usage + "\nrenewSliceAndSlivers.py renews the given slice and all slivers at aggregates known to the GENI CH for 60 days.\n " + "Takes slice name as argument") # options is an optparse.Values object, and args is a list options, args = omni.parse_args(sys.argv[1:], parser=parser) sliceName = args[0] newDate = datetime.datetime.utcnow() + datetime.timedelta(days=60) # Strip fractional seconds from times to avoid errors at PG AMs newDate = newDate.replace(microsecond=0) retcode = 0 for command in ['renewslice', 'renewsliver']: # Here we use --raise-error-on-v2-amapi-error. Note though that if 1 AM has a problem, the script stops. Is that what we want? # IE will all AMs return code 0 if they renew the slice alap? # Could supply arg '--warn' to turn down logging. But then we'd want this script to have Omni write to a log file. omniargs = [ '--alap', '--useSliceAggregates', '--raise-error-on-v2-amapi-error', command, sliceName, "'%s'" % newDate.isoformat() ] print "Calling Omni to renew slice %s%s until %sZ\n" % ( sliceName, (" slivers" if command == "renewsliver" else ""), newDate.isoformat()) try: text, retItem = omni.call(omniargs, options) except OmniError, oe: print "\n ***** Omni call failed: %s\n" % oe retcode = str(oe) continue print text print "\n"
def main(argv=None): ############################################################################## # Get a parser from omni that understands omni options ############################################################################## parser = omni.getParser() # update usage for help message omni_usage = parser.get_usage() parser.set_usage(omni_usage+"\nmyscript.py supports additional commands.\n\n\tCommands and their arguments are:\n\t\t\t[add stuff here]") ############################################################################## # Add additional optparse.OptionParser style options for your # script as needed. # Be sure not to re-use options already in use by omni for # different meanings, otherwise you'll raise an OptionConflictError ############################################################################## parser.add_option("--myScriptPrivateOption", help="A non-omni option added by %s"%sys.argv[0], action="store_true", default=False) # options is an optparse.Values object, and args is a list options, args = omni.parse_args(sys.argv[1:], parser=parser) if options.myScriptPrivateOption: # do something special for your private script's options print "Got myScriptOption" ############################################################################## # Try to read 2nd argument as an RSpec filename. Pull the AM URL and # and maybe slice name from that file. # Then construct omni args appropriately: command, slicename, action or rspecfile or datetime ############################################################################## omniargs = [] if args and len(args)>1: sliceurn = None # Try to read args[1] as an RSpec filename to read rspecfile = args[1] rspec = None if rspecfile: print "Looking for slice name and AM URL in RSpec file %s" % rspecfile try: rspec = readFile(rspecfile) except: print "Failed to read rspec from '%s'. Not an RSpec? Will try to get AM/slice from args." % rspecfile if rspec: # Now parse the comments, whch look like this: #<!-- Resources at AM: # URN: unspecified_AM_URN # URL: https://localhost:8001 # --> # Reserved resources for:\n\tSlice: %s # at AM:\n\tURN: %s\n\tURL: %s if not ("Resources at AM" in rspec or "Reserved resources for" in rspec): sys.exit("Could not find slice name or AM URL in RSpec '%s'" % rspec) amurn = None amurl = None # Pull out the AM URN and URL match = re.search(r"at AM:\n\tURN: (\S+)\n\tURL: (\S+)\n", rspec) if match: amurn = match.group(1) amurl = match.group(2) print " Found AM %s (%s)" % (amurn, amurl) omniargs.append("-a") omniargs.append(amurl) # Pull out the slice name or URN if any if "Reserved resources for" in rspec: match = re.search(r"Reserved resources for:\n\tSlice: (\S+)\n\t", rspec) if match: sliceurn = match.group(1) print " Found slice %s" % sliceurn command = args[0] rest = [] if len(args) > 2: rest = args[2:] # If the command requires a slice and we didn't get a readable rspec from the rspecfile, # Then treat that as the slice if not sliceurn and rspecfile and not rspec: sliceurn = rspecfile rspecfile = None # construct the args in order omniargs.append(command) if sliceurn: omniargs.append(sliceurn) if rspecfile and command.lower() in ('createsliver', 'allocate'): omniargs.append(rspecfile) for arg in rest: omniargs.append(arg) elif len(args) == 1: omniargs = args else: print "Got no command or rspecfile. Run '%s -h' for more information."%sys.argv[0] return ############################################################################## # And now call omni, and omni sees your parsed options and arguments ############################################################################## print "Call Omni with args %s:\n" % omniargs try: text, retItem = omni.call(omniargs, options) except OmniError, oe: sys.exit("\nOmni call failed: %s" % oe)
sliceName = args[0] userName = args[1] omniargs = ['addslicemember', sliceName, userName] print "Calling Omni to add %s to slice %s\n" % (userName, sliceName) try: text, retItem = omni.call(omniargs, options) if not retItem: print "\nFailed to add member to slice: %s" % text sys.exit(-1) except OmniError, oe: print "\nOmni call failed: %s\n" % oe sys.exit(-1) print text print "\n" omniargs = ['--useSliceMembers', '--ignoreConfigUsers', '--useSliceAggregates', '-V3', 'poa', sliceName, 'geni_update_users'] print "Calling Omni to add %s SSH keys to slice %s slivers\n" % (userName, sliceName) try: text, retItem = omni.call(omniargs, options) except OmniError, oe: print "\nOmni call failed: %s\n" % oe sys.exit(-1) print text print "\n" if __name__ == "__main__": sys.exit(main())
def main_no_print(argv=None, opts=None, slicen=None): global slicename, options, config, geni_username slicename = slicen parseArguments(argv=argv, opts=opts) # Call omni.initialize so that we get the config structure that # contains the configuration parameters from the omni_config file # We need them to get the ssh keys per user # Set loglevel to WARN to supress any normal printout options.warn = True framework, config, args, opts = omni.initialize( [], options ) handler = CallHandler(framework,config,options) # If creating an ansible inventory don't check the keys if options.ansible_inventory: options.include_keys = False geni_username = get_geni_username( handler, framework ) keyList = findUsersAndKeys( ) if options.include_keys and sum(len(val) for val in keyList.itervalues())== 0: print "ERROR: There are no keys. You can not login to your nodes." sys.exit(-1) aggregateURNs = [] if options.useSliceAggregates: # Find aggregates which have resources in this slice # Run equivalent of 'omni.py listslivers' argv = ['listslivers', slicename] try: text, slivers = omni.call( argv, options ) except (oe.AMAPIError, oe.OmniError) : print "ERROR: There was an error executing listslivers, review the logs." sys.exit(-1) aggregateURNs = slivers.keys() if len(aggregateURNs) == 0 and (not options.aggregate or (len(options.aggregate) == 0)): if not options.useSliceAggregates: print "ERROR: You must specify which aggregates to check. Try using '--useSliceAggregates' to check all aggregates known to have resources for this slice, or '-a' to specify one or more specific aggregates." else: print "ERROR: There are no known resources at any aggregates. Try using '-a' to specify an aggregate." sys.exit(-1) # construct a list of aggregates to newAggURLs = [ lookupURL( handler.logger, config, urn )[1] for urn in aggregateURNs ] if options.aggregate: options.aggregate = options.aggregate + newAggURLs else: options.aggregate = newAggURLs # Disable useSliceAggregates at this point, because we already have the list - don't fetch it again options.useSliceAggregates = False # Run equivalent of 'omni.py getversion' argv = ['--ForceUseGetVersionCache', 'getversion'] try: text, getVersion = omni.call( argv, options ) except (oe.AMAPIError, oe.OmniError) : print "ERROR: There was an error executing getVersion, review the logs." sys.exit(-1) if not getVersion: print "ERROR: Got no GetVersion output; review the logs." sys.exit(-1) loginInfoDict = {} for amUrl, amOutput in getVersion.items() : if not amOutput : print "%s returned an error on getVersion, skip!" % amUrl continue amType = getAMTypeFromGetVersionOut(amUrl, amOutput) if amType == "foam" : print "No login information for FOAM! Skip %s" %amUrl continue # XXX Although ProtoGENI returns the service tag in the manifest # it does not contain information for all the users, so we will # stick with the sliverstatus until this is fixed if amType == "sfa": amLoginInfo = getInfoFromSliverStatus(amUrl, amType) if len(amLoginInfo) > 0 : loginInfoDict[amUrl] = {'amType' : amType, 'info' : amLoginInfo } continue else: # Getting login info from manifest" amLoginInfo = getInfoFromSliceManifest(amUrl) # Get the status only if we care if len(amLoginInfo) > 0 : if options.readyonly or (amType == "protogeni") or (amType == "GRAM"): amLoginInfo = addNodeStatus(amUrl, amType, amLoginInfo) loginInfoDict[amUrl] = {'amType':amType, 'info':amLoginInfo } #else: # print "Not getting node status for %s" % amUrl return loginInfoDict, keyList
def getInfoFromSliceManifest( amUrl ) : tmpoptions = copy.deepcopy(options) tmpoptions.aggregate = [amUrl] # Run the equivalent of 'omni.py listresources <slicename>' if tmpoptions.api_version >= 3: apicall = 'describe' else : apicall = 'listresources' argv = [apicall, slicename] try: text, apicallout = omni.call( argv, tmpoptions ) except (oe.AMAPIError, oe.OmniError) as err: print "ERROR: There was an error executing %s, review the logs." % apicall #print "error was: %s" % err return [] key = amUrl if tmpoptions.api_version == 1: # Key is (urn,url) key = ("unspecified_AM_URN", amUrl) if not apicallout.has_key(key): if len(apicallout.keys()) == 1 : newkey = apicallout.keys()[0] print "WARN: Got result for AM URL %s instead of %s - did Omni redirect you?" % (newkey, key) key = newkey else: print "ERROR: No manifest found from %s at %s; review the logs." % \ sys.exit(-1) if tmpoptions.api_version == 1: manifest = apicallout[key] else: if not apicallout[key].has_key("value"): print "ERROR: No value slot in return from %s from %s; review the logs."\ % (apicall, amUrl) return [] if not apicallout[key].has_key('code') or not isinstance(apicallout[key]['code'], dict) or not apicallout[key]['code'].has_key('geni_code') or apicallout[key]['code']['geni_code'] != 0: msg = "ERROR: Failed to get manifest from %s call at %s; " % (apicall, amUrl) if apicallout[key].has_key('output') and str(apicallout[key]['output']).strip() != "": msg += apicallout[key]['output'] else: msg += "review the logs" print msg return [] value = apicallout[key]["value"] if tmpoptions.api_version == 2: manifest = value else: if tmpoptions.api_version == 3: if not (isinstance(value, dict) and value.has_key('geni_rspec')): print "ERROR: Malformed return from %s at %s - no rspec found" % (apicall, amUrl) return [] manifest = value['geni_rspec'] else: print "ERROR: API v%s not yet supported" %tmpoptions.api_version return [] maniInfo = getInfoFromManifest(manifest) return maniInfo
print "Calling Omni to add %s to slice %s\n" % (userName, sliceName) try: text, retItem = omni.call(omniargs, options) if not retItem: print "\nFailed to add member to slice: %s" % text sys.exit(-1) except OmniError, oe: print "\nOmni call failed: %s\n" % oe sys.exit(-1) print text print "\n" omniargs = [ '--useSliceMembers', '--ignoreConfigUsers', '--useSliceAggregates', '-V3', 'poa', sliceName, 'geni_update_users' ] print "Calling Omni to add %s SSH keys to slice %s slivers\n" % (userName, sliceName) try: text, retItem = omni.call(omniargs, options) except OmniError, oe: print "\nOmni call failed: %s\n" % oe sys.exit(-1) print text print "\n" if __name__ == "__main__": sys.exit(main())