#!/usr/bin/env python ############################################################################## ### Copyright Sun Microsystems, Inc. ALL RIGHTS RESERVED ### Use of this software is authorized pursuant to the ### terms of the license found at ### http://www.sun.com/bigadmin/common/berkeley_license.html ############################################################################## # # create_profile.py # # Script to generate a UCE profile out of a list of # patches using the Python API. # # Author: Juergen.Fleischer@Sun.COM # # v1.0: 12-Nov-2006 # v1.1: 23-May-2007 # Changes: Updated script for SC 1.1 and included Stored Passwd stuff # See the 'store_api_login.py' script for more info # v1.2: 11-Jun-2007 # Changes: Check for conflicts, before adding a patch to the profile # v1.3: 23-Jun-2007 # Changes: Ask first for parameter, then inititialize API # ################################################################## # # common initialization # ################################################################## import sys,os,base64,socket,getopt,getpass,re,string,copy,time home = os.environ['HOME'] rc_path = home + '/.uce_python' netrc = rc_path + '/.netrc' sys.path.append('/opt/SUNWuce/api/python/lib') sys.path.append('/opt/local/uce/api/python/lib') from PyOsApi import OsApi from PyOsApi import OsApiExceptions from PyOsApi import OsApiConstants from PyOsApi.DataStructures.OsApiDataStructures import * def init_api(): try: logindata = open(netrc) except: # ask for user & password print "Username: ", user=sys.stdin.readline().rstrip() passwd=getpass.getpass("Password: ") else: (user, crypted_pw) = logindata.readline().split() passwd = base64.decodestring(crypted_pw) logindata.close() print "Creating an API instance ..." api = OsApi.OsApi(rc_path,check_updates=1,check_new_channels=1) api.Login(user,passwd,force = True) print "Successfully logged in ..." return api ################################################################## # # end common initialization # ################################################################## # # Main # print " Please enter the file with the patch list: ", filename=sys.stdin.readline().rstrip() print "Name of the channel (e.g. SOLARIS8_SPARC, SOLARIS10_X86): ", channel=sys.stdin.readline().rstrip() print "Name of the profile to be created: ", profile_name=sys.stdin.readline().rstrip() # contact the Satellite server and create an API instance api = init_api() ## read in the contents of a file f = open(filename) data = f.readlines() f.close() profile_actions = [] # create empty Profile try: api.AddProfile(profile_name, profile_actions) except OsApiExceptions.OsApiProfileExistsException: print "Info: Overwriting existing profile %s !" %profile_name except OsApiExceptions.OsApiUnknownException: print "unknown general exception while creating profile" sys.exit(1) # now, we loop over all patches for patch in data: patch = patch.strip() try: patch_node_id,channel_id = api.GetIdNode(patch, channel) except OsApiExceptions.OsApiNodeNotFoundException: print "%s not found in components tree for channel %s" %(patch,channel) continue # create profile action try: profile_action = TaskActionData(action_name = OsApiConstants.Action.ACTION_INSTALL , node_id = long(patch_node_id)) except OsApiExceptions.OsApiNodeNotFoundException: print "Exception in TaskActionData ! PatchID: %s" %patch continue # we have to create a copy of the current action list new_profile_actions = copy.copy(profile_actions) # add the new action to the just created copy new_profile_actions.append(profile_action) # test if the new action will be accepted try: api.EditProfile(profile_name, new_profile_actions) except OsApiExceptions.OsApiProfileConflictException: print"conflict found in actions list: %s" %patch except OsApiExceptions.OsApiNonInstallableException: print "component not installable: upload of patch %s required !" %patch except OsApiExceptions.OsApiNameNotFoundException: print "component name not found: %s" %patch except OsApiExceptions.OsApiUnknownException: print "item already in profile or unknown general exception: %s" %patch else: # OK, new action has been accepted. Rename new action list # to our good list of accepted actions profile_actions = new_profile_actions print "Adding patch %s to profile" %patch print "New profile %s successfully created" %profile_name api.Logout()