def getOutputs(run): ''' Get outputs of a given run. @type run: dict @param run: Run returned by getRuns() @rtype: list @return: List of outputs ''' # API : /jobs/{jobid}/runoutput?runid=<valid runid> # Method : GET # URL structure: https://<servername>/v0/jobs/{jobid}/runoutput?runid=<validrunid>&access_token=<valid access token> # Input : runid(required) # ''' path = '/jobs/%s/runoutput' % (str(run['jobId'])) query = { 'runid':run['runId'] } url = userinfo.geturl(path, query) request = urllib2.Request(url) response = urllib2.urlopen(request) returnData = json.loads(response.read()) return returnData
def getContent(script, version=-1): """ Get script content @type script: dict @param script: Script returned by getScripts() @type version: int @param version: Optional @rtype: dict @return: Content of script @change: - Not changed """ # API : /scripts/{id} # Method : GET # URL structure : https://<servername>/v0/scripts?access_token=<valid access token> # Input params : version (optional) parameter # ''' scriptid = script["scriptId"] path = "/scripts/%s" % (str(scriptid)) query = {} if version != -1: query["version"] = version url = userinfo.geturl(path, query) request = urllib2.Request(url) response = urllib2.urlopen(request) returnData = json.loads(response.read()) return returnData
def getAudits(node, type = 'updates'): ''' Get a list of Audit for a single node @param node: A node @type type: string @param type: Optional, default value is B{updates}, valid values: - B{updates} @rtype: list @return: list of audits ''' agentid = node['agentId'] path = '/nodes/%s/%s' % (str(agentid), str(type)) query = {} # if type != '': ## FIXME, add assert ## assert type in ['script', 'template', 'patch', 'update'], 'type invalid' # query['type'] = type url = userinfo.geturl(path, query) response = urllib2.urlopen(url) returnData = json.loads(response.read()) return returnData
def createGroup(name, parentGroup): ''' FIXME, not finished ''' ''' {\"groupType\":0, \"groupId\":0, \"groupName\":\"PacaficServers\", \"parentGroupId\":1, \"groupParent\":\"All Servers\", \"companyId\":0, \"organizationId\":0, \"groupItemList\":[{\"groupId\":0, \"companyId\":0, \"groupItem\":\"124\", \"organizationId\":0, \"groupItemType\":0}] }" ''' path = '/groups' url = userinfo.geturl(path) payload = { "groupType": 0, "groupId": 0, "groupName": None, "PacaficServers": arguments, "parentGroupId": agents, "groupParent": None, "companyId": 12, "organizationId": startTime, "groupItemList": 0, } postData = json.dumps(payload) request = urllib2.Request(url, postData) request.add_header('Content-Type', 'application/json') response = urllib2.urlopen(request) returnData = json.loads(response.read()) return returnData
def getNodes(platform = '', status = ''): ''' Get nodes @type platform: string @param platform: optional, valid values are B{Windows} or B{Linux} @type status: string @param status: optional, valid values are B{online} or B{offline} @rtype: list @return: list of nodes @change: - Add parameter platform and status ''' # NOTE, not add single node id # API : /nodes or /nodes/{id} # Method : GET # URL Structure: https://<servername>/v0/nodes?access_token=<valid access token> # Input params: # platform (optional), valid values Windows or Linux # status (optional), valid values online or offline assert platform in ['', 'Windows', 'Linux'], 'wrong platform' assert status in ['', 'online', 'offline'], 'wrong status' path = '/nodes' query = {} if platform != '': query['platform'] = platform if status != '': query['status'] = status url = userinfo.geturl(path, query) response = urllib2.urlopen(url) returnData = json.loads(response.read()) return returnData
def delete(script="", type=""): """ Delete a script or a group of scripts @todo: Add return info about this API @type script: dict @param script: Script returned by getScripts() @type type: string @param type: Valid values are B{user}, B{org} or B{purchase} @change: - Add parameter B{type}, you can delete a group of scripts now. """ # API : /scripts/1234 # Method : DELETE # /scripts?type=user Delete user scripts # /scripts?type=org Delete org scripts # /scripts?type=purchase Delete purchase scripts path = "/scripts" query = {} if script != "": path = path + "/" + str(script["scriptId"]) else: assert type in ["user", "org", "purchase"], "wrong script type" query["type"] = type url = userinfo.geturl(path, query) request = urllib2.Request(url) request.get_method = lambda: "DELETE" response = urllib2.urlopen(request) returnData = json.loads(response.read()) return returnData
def getRuns(job): ''' Get runs of a given job. @type job: dict @param job: Job returned by getJobs() @rtype: list @return: List of runs @change: - Not changed ''' # NOTE, no runid # API: /jobs/{jobid}/runinfo # Method: GET # URL structure: https://<servername>/v0/jobs/{jobid}/runinfo?access_token=<valid access token> # Input: runid (optional), can specify runid # Output: # [{"jobId":1814,"taskPropertyBeans":[],"status":"complete","role":"Admin","companyId":40042,"projectId":69,"runId":65,"user":"******","runTimestamp":1339099219184}] # ''' path = '/jobs/%s/runinfo' % (job['jobId']) url = userinfo.geturl(path) request = urllib2.Request(url) response = urllib2.urlopen(request) returnData = json.loads(response.read()) return returnData
def create(name, type, content, description="", params=[], tagList=[]): """ Create a script @todo: params and tags not implement @type name: string @param name: Script Name @type type: string @param type: Script type(filename extension) @type content: string @param content: Script content @type description: string @param description: Script description @rtype: dict @return: script just created @change: - Not changed """ # FIXME, no script attachments, no tags, no params # ttps://manage.scalextreme.com/library?rid=411C2ECD-BDD0-4F61-9F37-E3718F02E084 # API : /scripts # Method : POST # URL Structure: https://<servername>/v0/scripts?access_token=<valid token generated by authentication> # Input : Json payload like # { # "scriptName":"Test script", # "scriptType":"bat", # "scriptDescription":"", # "scriptInputParams":[], # "tagList":[], # "scriptAttachments":[], # "scriptContent":"bGluZTEKbGluZTIKbGluZTMKbGluZTQKbGluZTUKZWNobyAnSGknCg==" # } # ''' path = "/scripts" url = userinfo.geturl(path) payload = { "scriptName": name, "scriptType": type, "scriptDescription": base64.b64encode(description), "scriptInputParams": params, "tagList": tagList, "scriptAttachments": [], "scriptContent": base64.b64encode(content), } postData = json.dumps(payload) request = urllib2.Request(url, postData) request.add_header("Content-Type", "application/json") response = urllib2.urlopen(request) returnData = json.loads(response.read()) return returnData
def getJobs(type = 'script', object = {}): ''' Get jobs of a given script Example: - getJobs(type='patch') # get applied patch jobs - getJobs(type='script', object=script) # get script jobs @note: Currently support script jobs only. @todo: Add support for update/patch job @type type: string @param type: Job type, default is B{script}. Valid values are: - B{script}, get script jobs - B{patch}, get applied patch jobs - B{update}, get applied update jobs @type object: dict @param object: If type is script, object is the script returned by scalex.script.getScripts() @rtype: list @return: List of jobs @change: - API usage changed. - Add parameter B{type} - Add parameter B{object} ''' # # API : /jobs?type=<script, template etc,>&id=<id of script, id of template etc.,> # Method : GET # URL Structure: https://<servername>/v0/jobs?type=script&id=<script id>& access_token=<valid access token> # Input : # type (required), valid values are script, template, patch, update etc., # id # ''' # id = str(object['scriptId']) assert type in ['script', 'update', 'patch'], 'wrong type' path = '/jobs' query = { 'type': type, } if type in ['script']: assert object != {}, 'no script object' query['id'] = object['scriptId'] else: query['id'] = 0 query['type'] = 'apply' + type url = userinfo.geturl(path, query) request = urllib2.Request(url) response = urllib2.urlopen(request) returnData = json.loads(response.read()) return returnData
def deleteGroup(group): ''' Delete a server group @param group: The group you want to delete ''' path = '/groups/' + str(group['groupId']) url = userinfo.geturl(path) request = urllib2.Request(url) request.get_method = lambda: 'DELETE' response = urllib2.urlopen(request) returnData = json.loads(response.read()) return returnData
def getGroups(): ''' Get server groups @rtype: list @return: list of groups ''' path = '/groups' query = {} url = userinfo.geturl(path, query) response = urllib2.urlopen(url) returnData = json.loads(response.read()) return returnData
def getUpdates(node): ''' Get updates of a given node @param node: A node @rtype: list @return: list of updates ''' agentid = node['agentId'] path = '/nodes/%s/updates' % (str(agentid)) query = {} url = userinfo.geturl(path, query) response = urllib2.urlopen(url) returnData = json.loads(response.read()) return returnData
def getNodesOfGroup(group): ''' Get nodes of a given group @param group: A group returned by getGroups() @rtype: list @return: list of nodes ''' groupid = group['groupId'] path = '/groups/%s/nodes' % (str(groupid)) query = {} url = userinfo.geturl(path, query) response = urllib2.urlopen(url) returnData = json.loads(response.read()) # return returnData
def _scheduleUpdateOrPatches(name, targets, arguments, scriptType, startTime): ''' ''' if not isinstance(targets, list): t = targets targets = [] targets.append(t) agents = [] for n in targets: agents.append(n['agentId']) if startTime != 0: d = datetime.datetime.strptime(startTime, "%Y-%m-%d-%H:%M") startTime = int(time.mktime(d.timetuple())*1000) payload = { # "companyId": userinfo.companyid, # "user": str(userinfo.userid), # "role": userinfo.rolename, "scriptId": 0, "version": None, "scriptArgs": arguments, "targets": agents, "destInstallDir": None, "scheduleType": 12, "startTime": startTime, "endTime": 0, "repeatCount": 0, "repeatInterval": 0, "cronExpr": None, "timeZone": None, "name": name, "description": name, "jobId": 0, "jobName": name, "scriptType": scriptType } path = '/jobs' url = userinfo.geturl(path) postData = json.dumps(payload) request = urllib2.Request(url, postData) request.add_header('Content-Type', 'application/json') response = urllib2.urlopen(request) returnData = json.loads(response.read()) return returnData
def getAllAgentsWithPatch(patch): ''' Get a list of machines which have the same patches/updates missing @param patch: Valid values: - patch - list of patches - update - list of updates @rtype: list @return: list of agentId @change: - Old API getOtherAgentsWithPatch(node,patch) ''' path = '/missingupdates' query = { 'type':'PATCH', } if 'updaterelease' in str(patch): query['type'] = 'UPDATE' url = userinfo.geturl(path, query) if not isinstance(patch, list): p = patch patch = [] patch.append(p) # patches = [] for i in patch: d = {} d['name'] = i['name'] if query['type'] == 'UPDATE': d['arch'] = i['arch'] d['updateVersion'] = i['updateversion'] d['updateRelease'] = i['updaterelease'] patches.append(d) postData = json.dumps(patches) request = urllib2.Request(url, postData) request.add_header('Content-Type', 'application/json') # print patch # print postData response = urllib2.urlopen(request) returnData = json.loads(response.read()) return returnData
def cancel(run): ''' Cancel future runs @param run: The run you want to cancel ''' # NOTE, why we need a runID???? # /jobs/1234/cancel/ jobid = run['jobId'] runid = run['runId'] path = '/jobs/%s/cancel/' % (str(jobid)) query = { 'runid':runid, } url = userinfo.geturl(path, query) request = urllib2.Request(url, '') response = urllib2.urlopen(request) returnData = json.loads(response.read()) return returnData
def delete(job): ''' Delete a job @param job: The job you want to delete ''' path = '/jobs' query = {} # if script != '': path = path + '/' + str(job['jobId']) # else: # assert type in ['user', 'org', 'purchase'], 'wrong script type' # query['type'] = type url = userinfo.geturl(path, query) request = urllib2.Request(url) request.get_method = lambda: 'DELETE' response = urllib2.urlopen(request) returnData = json.loads(response.read()) return returnData
def getVersions(script): """ Get script content @type script: dict @param script: Script returned by getScripts() @rtype: list @return: Versions of specific script @change: - Not changed """ # API : /scripts/versions?id=1234 # Method : GET # ''' path = "/scripts/%s/versions" % (str(script["scriptId"])) query = {} url = userinfo.geturl(path, query) request = urllib2.Request(url) response = urllib2.urlopen(request) returnData = json.loads(response.read()) return returnData
def getScripts(type=""): """ Get scripts @type type: string @param type: Optional, valid values are B{user}, B{org} or B{purchase} @rtype: list @return: List of scripts @change: - Parameter type now changed. Used to be 0 or 1, now it can be one of B{user}, B{org} and B{purchase} """ # API : /scripts # Method : GET # URL structure : https://<servername>/v0/scripts?access_token=<valid access token> # Input params : # version (optional) parameter # type (optional) # type=user Return my scripts # type=org Return org scripts # type=purchase Return purchase scripts assert type in ["", "user", "org", "purchase"], "script type must be one of ['', 'user', 'org', 'purchase']" path = "/scripts" query = {} if type != "": query["type"] = type # elif script != '': # path = '/scripts/' + str(script['scriptId']) # if version != 0: # query['version'] = version url = userinfo.geturl(path, query) request = urllib2.Request(url) response = urllib2.urlopen(request) returnData = json.loads(response.read()) return returnData
def _create_or_update_job(path, name, script = None, targets = None, arguments = [], type = 'script', version = -1, serverGroups = [], scheduleType = 0, startTime = 0, repeatInterval = 60, endTime = 0, repeatCount = 0): ''' For Internal Use ONLY ''' # For Internal Use ONLY # API : /jobs # Method : POST # URL structure: https://<servername>/v0/jobs # Input param: Following json payload # { # "name":"Sample Job", # "scriptId":2446, # "targets":[140], # "scriptArgs":["Test1","Test2"], # "type":"script", # # "repeatCount":0, # "serverGroups":[], # "endTime":0, # "startTime":1339353250011, # "scheduleType":12, # "taskParameters":[], # "repeatInterval":0 # } # scheduleType: 0, Run Once # 1, Recurring # ''' _schecule_type = [12, 14, 2] if scheduleType == 0: if startTime != 0: d = datetime.datetime.strptime(startTime, "%Y-%m-%d-%H:%M") startTime = int(time.mktime(d.timetuple())*1000) elif scheduleType == 1: if repeatCount == 0 and endTime == 0: assert False, 'wrong schedule' if endTime != 0: d = datetime.datetime.strptime(endTime, "%Y-%m-%d-%H:%M") endTime = int(time.mktime(d.timetuple())*1000) pass else: assert False, 'wrong schedule type' scheduleType = _schecule_type[scheduleType] if not isinstance(targets, list): t = targets targets = [] targets.append(t) agents = [] for n in targets: agents.append(n['nodeId']) scriptid = 0 if type == 'script' and version == -1: version = script['version'] scriptid = script['scriptId'] payload = { "name":name, "scriptId":scriptid, "targets":agents, "scriptArgs":arguments, "type":type, "version":version, "repeatCount":repeatCount, "serverGroups":serverGroups, "endTime":endTime, "startTime":startTime, "scheduleType":scheduleType, "taskParameters":[], "repeatInterval":repeatInterval, } postData = json.dumps(payload) url = userinfo.geturl(path) request = urllib2.Request(url, postData) request.add_header('Content-Type', 'application/json') response = urllib2.urlopen(request) returnData = json.loads(response.read()) return returnData
def update(script, name="", type="", content="", description="", params=[], tagList=[]): """ Update a script @todo: params and tags not implement @type script: dict @param script: Script will be updated @type name: string @param name: Script Name @type type: string @param type: Script type(filename extension) @type content: string @param content: Script content @type description: string @param description: Script description @rtype: dict @return: script just created @change: - Not changed """ # API : /scripts/1234 # Method : POST # URL Structure: https://<servername>/v0/scripts?access_token=<valid token generated by authentication> # Input : Json payload like # { # "scriptName":"Test script", # "scriptType":"bat", # "scriptDescription":"", # "scriptInputParams":[], # "tagList":[], # "scriptAttachments":[], # "scriptContent":"bGluZTEKbGluZTIKbGluZTMKbGluZTQKbGluZTUKZWNobyAnSGknCg==" # } # '''def update(script, name = '', type = '', content = '', description = '', params = [], tags = [] ): # FIXME, no script attachments, incomplete params/tags/attachments path = "/scripts/" + str(script["scriptId"]) parameters = [] url = userinfo.geturl(path) script = getContent(script) # script contents if name == "": name = script["scriptName"] if type == "": type = script["scriptType"] if content == "": content = script["scriptContent"] else: content = base64.b64encode(content) payload = { "scriptName": name, "version": script["version"], "scriptType": type, "scriptDescription": base64.b64encode(description), "scriptInputParams": params, "tagList": tagList, "scriptAttachments": [], "scriptContent": content, } postData = json.dumps(payload) request = urllib2.Request(url, postData) request.add_header("Content-Type", "application/json") response = urllib2.urlopen(request) returnData = json.loads(response.read()) return returnData