def get_all_vms():
    vspheres = client.EcxAPI(session, 'vsphere').list()
    allvms = []
    for vsphere in vspheres:
        vspherevms = client.EcxAPI(session, 'vsphere').get(url=vsphere['links']['vms']['href'])['vms']
        allvms.extend(vspherevms)
    return allvms
def create_restore_job(resseedpol, restarget, busource):
    logger.info("Creating restore job")
    newpol = resseedpol
    newjob = {}
    jobname = "SQL On-Demand Restore " + str(int(time.time()))
    del newpol['id']
    del newpol['links']
    del newpol['lastUpdated']
    del newpol['creationTime']
    del newpol['logicalDelete']
    del newpol['rbacPath']
    del newpol['tenantId']
    newpol['name'] = jobname
    newpol['spec']['source'] = []
    newpol['spec']['source'].append({})
    newpol['spec']['source'][0][
        'href'] = busource['database']['links']['self']['href'] + "?time=0"
    newpol['spec']['source'][0]['id'] = busource['database']['id']
    newpol['spec']['source'][0]['include'] = True
    newpol['spec']['source'][0]['resourceType'] = "database"
    newpol['spec']['source'][0]['metadata'] = {}
    newpol['spec']['source'][0]['metadata']['id'] = busource['database']['id']
    newpol['spec']['source'][0]['metadata']['name'] = busource['database'][
        'name']
    newpol['spec']['source'][0]['metadata']['path'] = "PATH"
    newpol['spec']['source'][0]['version'] = {}
    newpol['spec']['source'][0]['version']['href'] = busource['database'][
        'links']['self']['href'] + "/version/latest"
    newpol['spec']['source'][0]['version']['metadata'] = {}
    newpol['spec']['source'][0]['version']['metadata']['id'] = "latest"
    newpol['spec']['source'][0]['version']['metadata']['name'] = "Use Latest"
    newpol['spec']['subpolicy'][0]['description'] = ""
    newpol['spec']['subpolicy'][0]['destination']['mapdatabase'] = {}
    mdkey = busource['database']['links']['self']['href'] + "?time=0"
    newpol['spec']['subpolicy'][0]['destination']['mapdatabase'][mdkey] = {
        "name": options.tdb
    }
    newpol['spec']['subpolicy'][0]['destination']['target'][
        'href'] = restarget['links']['self']['href']
    newpol['spec']['subpolicy'][0]['destination']['target']['metadata'][
        'id'] = restarget['id']
    newpol['spec']['subpolicy'][0]['destination']['target']['metadata'][
        'name'] = restarget['name']
    newpol['spec']['subpolicy'][0]['destination']['target']['metadata'][
        'path'] = "PATH"
    newpol['spec']['subpolicy'][0]['name'] = jobname
    newpol['spec']['subpolicy'][0]['option']['makepermanent'] = "enabled"
    newpol['spec']['subpolicy'][0]['source']['copy']['site'][
        'href'] = busource['database']['links']['site']['href']
    newpol['spec']['subpolicy'][0]['source']['copy']['site']['metadata'][
        'name'] = "TEMP"
    newpol['spec']['subpolicy'][0]['source']['copy']['site']['metadata'][
        'pointintime'] = False
    polresp = client.EcxAPI(session, 'policy').post(data=newpol)
    newjob['description'] = "Temporary job"
    newjob['name'] = jobname
    newjob['policyId'] = polresp['id']
    newjob['triggerIds'] = []
    jobresp = client.EcxAPI(session, 'job').post(data=newjob)
    return jobresp
def build_version_for_vm(vm, jobparams):
    vmurl = vm['links']['self']['href']+"?time=0"
    vmurlarray = [vmurl]
    assdata = {'associatedWith': vmurlarray,"resourceType": "site"}
    association = client.EcxAPI(session, 'vsphere').post(path="query", data=assdata)['sites'][0]
    versionurl = vm['links']['self']['href']+"/version"
    versionparams = {'time': 0, 'filter': '[{"property":"siteId","op":"=","value":"%s"}]'%association['id']}
    versions = client.EcxAPI(session, 'vsphere').get(url=versionurl, params=versionparams)['versions']
    version = {}
    metadata = {}
    # no copy filters supplied use latest
    if (jobparams['end'] is None or jobparams['start'] is None or jobparams['end'] == "" or jobparams['start'] == ""):
        version['href'] = vm['links']['self']['href']+"/version/latest?time=0"
        metadata['id'] = "latest"
        metadata['name'] = "Use Latest"
        version['metadata'] = metadata
        return version
    # match on dates
    else:
        start = int(datetime.datetime.strptime(jobparams['start'], '%m/%d/%Y %H:%M').strftime("%s"))*1000
        end = int(datetime.datetime.strptime(jobparams['end'], '%m/%d/%Y %H:%M').strftime("%s"))*1000
        for vers in versions:
            prottime = int(vers['protectionInfo']['protectionTime'])
            if (start < prottime and prottime < end):
                version['href'] = vers['links']['self']['href']
                metadata['id'] = vers['id']
                metadata['name'] = time.ctime(prottime/1000)[4:].replace("  "," ")
                version['metadata'] = metadata
                return version
    logger.info("No backup copy found with provided dates")
    session.delete('endeavour/session/')
    sys.exit(2)
def build_alt_dest_ds(destination, vmqlist, jobparams):
    mapRRPdatastore = {}
    mapRRPdatastoremd = {}
    targethost = client.EcxAPI(session, 'vsphere').get(url=destination['target']['href'])
    targetdatastores = client.EcxAPI(session, 'vsphere').get(url=targethost['links']['datastores']['href'])['datastores']
    for tds in targetdatastores:
        if(tds['name'] == jobparams['dsdest']):
            targetds = tds
    try:
        targetds
    except NameError:
        logger.info("No datastore found with provided name")
        session.delete('endeavour/session/')
        sys.exit(2)
    vmurls = []
    for vm in vmqlist:
        vmurls.append(copy.deepcopy(vm['links']['self']['href']))
    querydata = {'associatedWith': vmurls,"resourceType": "datastore"}
    sourcedatastores = client.EcxAPI(session, 'vsphere').post(path="query", data=querydata)['datastores']
    for sds in sourcedatastores:
        sdskey = sds['links']['self']['href']
        mapRRPdatastore[sdskey] = targetds['links']['self']['href']
        mapRRPdatastoremd[sdskey] = {}
        mapRRPdatastoremd[sdskey]['source'] = {}
        mapRRPdatastoremd[sdskey]['source']['name'] = sds['name']
        mapRRPdatastoremd[sdskey]['source']['href'] = sdskey
        mapRRPdatastoremd[sdskey]['destination'] = {}
        mapRRPdatastoremd[sdskey]['destination']['name'] = targetds['name']
        mapRRPdatastoremd[sdskey]['destination']['href'] = targetds['links']['self']['href']
    mapRRPdatastore['metadata'] = mapRRPdatastoremd
    return mapRRPdatastore
def create_backup_job(buseedpol, busource):
    logger.info("Creating backup job")
    newpol = buseedpol
    newjob = {}
    jobname = "SQL On-Demand Backup " + str(int(time.time()))
    del newpol['id']
    del newpol['links']
    del newpol['lastUpdated']
    del newpol['creationTime']
    del newpol['logicalDelete']
    del newpol['rbacPath']
    del newpol['tenantId']
    newpol['name'] = jobname
    newpol['spec']['source'] = []
    newpol['spec']['source'].append({})
    newpol['spec']['source'][0]['href'] = busource['database']['links'][
        'self']['href']
    newpol['spec']['source'][0]['id'] = busource['database']['id']
    newpol['spec']['source'][0]['include'] = True
    newpol['spec']['source'][0]['resourceType'] = "database"
    newpol['spec']['source'][0]['metadata'] = {}
    newpol['spec']['source'][0]['metadata']['id'] = busource['database']['id']
    newpol['spec']['source'][0]['metadata']['name'] = busource['database'][
        'name']
    #since this job is temporary we don't need to worry about getting path data for UI
    newpol['spec']['source'][0]['metadata']['path'] = "PATH"
    polresp = client.EcxAPI(session, 'policy').post(data=newpol)
    newjob['description'] = "Temporary job"
    newjob['name'] = jobname
    newjob['policyId'] = polresp['id']
    newjob['triggerIds'] = []
    jobresp = client.EcxAPI(session, 'job').post(data=newjob)
    return jobresp
Beispiel #6
0
def run_backup_job_and_wait_for_finish(backupjob):
    run = client.JobAPI(session).run(backupjob['id'])
    logger.info("Running backup job, please wait.")
    time.sleep(5)
    job = client.EcxAPI(session, 'job').get(resid=backupjob['id'])
    while (job['lastrun']['status'] == "RUNNING"):
        time.sleep(5)
        job = client.EcxAPI(session, 'job').get(resid=backupjob['id'])
    return job
def build_alt_dest_vlan(destination, vmqlist):
    logger.info("Building alternate VLAN")
    mapvirtualnetwork = {}
    mapvnmetadata = {}
    targethost = client.EcxAPI(
        session, 'vsphere').get(url=destination['target']['href'])
    targetnetworks = client.EcxAPI(
        session,
        'vsphere').get(url=targethost['links']['networks']['href'])['networks']
    sourcenetworks = []
    for tnw in targetnetworks:
        if (tnw['name'] == options.pvlan):
            recoverynetwork = tnw
    for tnw in targetnetworks:
        if (tnw['name'] == options.tvlan):
            testnetwork = tnw
    try:
        recoverynetwork
    except NameError:
        logger.info("No prod. network found with provided name")
        session.delete('endeavour/session/')
        sys.exit(2)
    try:
        testnetwork
    except NameError:
        logger.info("No test network found with provided name")
        session.delete('endeavour/session/')
        sys.exit(2)
    vmurls = []
    for vm in vmqlist:
        vmurls.append(copy.deepcopy(vm['links']['self']['href']))
    querydata = {'associatedWith': vmurls, "resourceType": "network"}
    sourcenetworks = client.EcxAPI(session,
                                   'vsphere').post(path="query",
                                                   data=querydata)['networks']
    for snw in sourcenetworks:
        snwkey = snw['links']['self']['href']
        mapvirtualnetwork[snwkey] = {}
        mapvirtualnetwork[snwkey]['recovery'] = recoverynetwork['links'][
            'self']['href']
        mapvirtualnetwork[snwkey]['test'] = testnetwork['links']['self'][
            'href']
        mapvnmetadata[snwkey] = {}
        mapvnmetadata[snwkey]['source'] = {}
        mapvnmetadata[snwkey]['recovery'] = {}
        mapvnmetadata[snwkey]['test'] = {}
        mapvnmetadata[snwkey]['source']['name'] = snw['name']
        mapvnmetadata[snwkey]['source']['href'] = snwkey
        mapvnmetadata[snwkey]['recovery']['name'] = recoverynetwork['name']
        mapvnmetadata[snwkey]['recovery']['href'] = recoverynetwork['links'][
            'self']['href']
        mapvnmetadata[snwkey]['test']['name'] = testnetwork['name']
        mapvnmetadata[snwkey]['test']['href'] = testnetwork['links']['self'][
            'href']
    mapvirtualnetwork['metadata'] = mapvnmetadata
    return mapvirtualnetwork
Beispiel #8
0
def create_policy_and_job(policy):
    polresp = client.EcxAPI(session, 'policy').post(data=policy)
    generatedjob = {
        "name": policy['name'],
        "description": "Auto-generated job for Policy " + policy['name'],
        "policyId": polresp['id'],
        "triggerIds": []
    }
    jobresp = client.EcxAPI(session, 'job').post(data=generatedjob)
    return jobresp
def get_restore_seed_pol():
    jobs = client.EcxAPI(session, 'job').list()
    for job in jobs:
        if (job['name'].upper() == options.seedres.upper()):
            pol = client.EcxAPI(session, 'policy').get(resid=job['policyId'])
            if (pol['type'] == "recovery" and pol['subType'] == "application"):
                if (pol['applicationType'] == "sql"):
                    return pol
    logger.info("No restore seed policy found, please check name exists")
    session.delete('endeavour/session/')
    sys.exit(2)
Beispiel #10
0
def get_info_for_vms():
    vspheres = client.EcxAPI(session, 'vsphere').list()
    allvms = []
    selectedvms = []
    for vsphere in vspheres:
        vspherevms = client.EcxAPI(
            session, 'vsphere').get(url=vsphere['links']['vms']['href'])['vms']
        allvms.extend(vspherevms)
    for vm in allvms:
        if (vm['name'] in options.vms):
            selectedvms.append(copy.deepcopy(vm))
    return selectedvms
def build_path_for_vm(vm):
    vsphere = client.EcxAPI(session, 'vsphere').get(url=vm['links']['vsphere']['href'])
    dc = client.EcxAPI(session, 'vsphere').get(url=vm['links']['datacenter']['href'])
    sitepath = vsphere['siteName'] + ":" + vsphere['siteId']
    vcpath = vsphere['name'] + ":" + vsphere['id']
    dcpath = dc['name'] + ":" + dc['id']
    # seems like we're unable to build folder path without iterating through all of them
    # this causes minor issue with autodirect to selected VMs in folders in the ECX UI
    # not needed for succesful updating of the policy or running it, leaving it out for performance
    folderpath = "folder"
    path = sitepath + "/" + vcpath + "/" + dcpath + "/" + folderpath
    return path
def run_backup_job_and_wait_for_completion(bujob):
    logger.info("Running backup job... please wait.")
    run = client.JobAPI(session).run(bujob['id'])
    time.sleep(10)
    job = client.EcxAPI(session, 'job').get(resid=bujob['id'])
    while (job['lastrun']['status'] == "RUNNING"):
        time.sleep(10)
        job = client.EcxAPI(session, 'job').get(resid=bujob['id'])
    if (job['lastrun']['status'] == "COMPLETED"):
        return job
    logger.error("Job did not complete, please log in to UI to view logs.")
    session.delete('endeavour/session/')
    sys.exit(2)
def get_source_info():
    sourceinsdbinfo = {}
    instances = client.EcxAPI(session, 'application').get(path="/sql/instance")['instances']
    for instance in instances:
        sourcedbs = client.EcxAPI(session, 'application').get(url=instance['links']['databases']['href'])['databases']
        for sourcedb in sourcedbs:
            if (sourcedb['name'].upper() == options.source.upper()):
                sourceinsdbinfo['instance'] = instance
                sourceinsdbinfo['database'] = sourcedb
                return sourceinsdbinfo
    logger.info("No source dbs found with name %s" % options.source)
    session.delete('endeavour/session/')
    sys.exit(2)
def find_vcenter():
    vcenterlist = client.EcxAPI(session, 'vsphere').list()
    for vcenter in vcenterlist:
        if (vcenter['name'].upper() == options.provvc.upper()):
            return vcenter['id']
    print "Provider vCenter not found"
    sys.exit(2)
def build_subpol_source_site(sourceinfo):
    siteinfo = {}
    siteid = sourceinfo[0]['metadata']['path'].split("/")[0].split(":")[1]
    site = client.EcxAPI(session, 'site').get(resid=siteid)
    siteinfo['href'] = site['links']['self']['href']
    siteinfo['metadata'] = {"name": site['name']}
    return siteinfo
def create_job(newpolicy, jobparams):
    newjob = {}
    newjob['name'] = jobparams['restore']
    newjob['policyId'] = newpolicy['id']
    newjob['description'] = "Auto-generated job for Policy " + jobparams['restore']
    newjob['triggerIds'] = []
    return client.EcxAPI(session, 'job').post(data=newjob)
def find_site_id_by_name():
    sitelist = client.EcxAPI(session, 'site').list()
    for site in sitelist:
        if (site['name'].upper() == options.provsite.upper()):
            return site['id']
    print "Provider site not found"
    sys.exit(2)
def find_credential():
    userlist = client.EcxAPI(session, 'identityuser').list()
    for user in userlist:
        if (user['name'].upper() == options.provcred.upper()):
            return {"href": user['links']['self']['href']}
    print "Provider credentials not found"
    sys.exit(2)
Beispiel #19
0
def build_path_for_vol(vol):
    sitepath = vol['siteName'] + ":" + vol['siteId']
    svc = client.EcxAPI(session,
                        'ibmsvc').get(url=vol['links']['ibmsvc']['href'])
    svcpath = svc['name'] + ":" + svc['id']
    path = sitepath + "/" + svcpath
    return path
def get_swf_for_job(job):
    swfs = client.EcxAPI(session, 'policy').get(resid=job['policyId'])
    for swf in swfs:
        if (swf['name'] == options.workflow):
            return swf
    logger.info("No workflow found with name %d" % options.workflow)
    sys.exit(2)
Beispiel #21
0
def get_info_for_vols():
    svcs = client.EcxAPI(session, 'ibmsvc').list()
    allvols = []
    selectedvols = []
    for svc in svcs:
        try:
            svcvols = client.EcxAPI(
                session,
                'ibmsvc').get(url=svc['links']['volumes']['href'])['volumes']
            allvols.extend(svcvols)
        except:
            print "Invalid provider " + svc['id']
    for vol in allvols:
        if (vol['name'] in options.vols):
            selectedvols.append(copy.deepcopy(vol))
    return selectedvols
def get_restore_job():
    jobs = client.EcxAPI(session, 'job').list()
    for job in jobs:
        if (job['name'].upper() == options.restore.upper()):
            return job
    logger.info("No job found with name %s" % options.restore)
    session.delete('endeavour/session/')
    sys.exit(2)
def get_pending_job_session(job):
    sessionurl = job['links']['pendingjobsessions']['href']
    jobsession = client.EcxAPI(session, 'jobsession').get(url=sessionurl)
    if (len(jobsession['sessions']) < 1):
        logger.info("No pending job sessions found.")
        session.delete('endeavour/session/')
        sys.exit(2)
    return jobsession['sessions'][0]
def register_provider():
    provider = build_provider()
    try:
        resp = client.EcxAPI(session, 'appserver').post(data=provider)
        print "Provider " + options.provname + " registerd."
    except client.requests.exceptions.HTTPError as e:
        error = json.loads(e.response.content)
        print "Error registering provider: " + error['id']
def get_restore_job(jobparams):
    jobs = client.EcxAPI(session, 'job').list()
    for job in jobs:
        if(job['name'].upper() == jobparams['template'].upper()):
            return job
    logger.info("No template job found with name %s" % jobparams['template'].upper())
    session.delete('endeavour/session/')
    sys.exit(2)
def get_target_info():
    instances = client.EcxAPI(session, 'application').get(path="/sql/instance")['instances']
    for instance in instances:
        if (instance['name'].upper() == options.tinst):
            return instance
    logger.info("No target instance found with name %s" % options.tinst)
    session.delete('endeavour/session/')
    sys.exit(2)
def build_alt_dest_target():
    logger.info("Building alternate host/cluster destination")
    target = {}
    targetmd = {}
    targethost = ""
    vspheres = client.EcxAPI(session, 'vsphere').list()
    for vsphere in vspheres:
        try:
            hosts = client.EcxAPI(
                session,
                'vsphere').get(url=vsphere['links']['hosts']['href'])['hosts']
        except client.requests.exceptions.HTTPError as err:
            continue
        for host in hosts:
            if (host['name'] == options.hostdest):
                targethost = host
                targetvsphere = vsphere
                targetdc = client.EcxAPI(
                    session,
                    'vsphere').get(url=host['links']['datacenter']['href'])
                break
        if (targethost == ""):
            clusters = client.EcxAPI(session, 'vsphere').get(
                url=vsphere['links']['clusters']['href'])['clusters']
            for cluster in clusters:
                if (cluster['name'] == options.hostdest):
                    targethost = cluster
                    targetvsphere = vsphere
                    targetdc = client.EcxAPI(
                        session,
                        'vsphere').get(url=host['links']['datacenter']['href'])
                    break
    if (targethost == ""):
        logger.info("No target host found with name provided")
        session.delete('endeavour/session/')
        sys.exit(2)
    target['href'] = targethost['links']['self']['href']
    target['resourceType'] = targethost['resourceType']
    targetmd['path'] = targetvsphere['siteName'] + ":" + targetvsphere[
        'siteId'] + "/" + targetvsphere['name'] + ":"
    targetmd['path'] += targetvsphere['id'] + "/" + targetdc[
        'name'] + ":" + targetdc['id']
    targetmd['name'] = targethost['name']
    target['metadata'] = targetmd
    return target
def find_site_id_by_name():
    global site_found
    site_found = False
    sitelist = client.EcxAPI(session, 'site').list()
    for site in sitelist:
        if (site['name'].upper() == options.provsite.upper()):
            site_found = True
            return site['id']
    print "Provider site not found"
def find_vcenter():
    global vcenter_found
    vcenter_found = False
    vcenterlist = client.EcxAPI(session, 'vsphere').list()
    for vcenter in vcenterlist:
        if (vcenter['name'].upper() == options.provvc.upper()):
            vcenter_found = True
            return vcenter['id']
    print "Provider vCenter not found"
def run_restore_job_and_wait_for_completion(resjob):
    logger.info("Running restore job... please wait.")
    run = client.JobAPI(session).run(resjob['id'])
    time.sleep(10)
    job = client.EcxAPI(session, 'job').get(resid=resjob['id'])
    # Note: We need to also wait here on RESOURCE ACTIVE state as job goes into that
    # status during the "Make Permanent" operation
    # However, if this step fails, we are then stuck in that state until restore is cancelled
    # may want to introduce some sort of timeout failure here
    while (job['lastrun']['status'] == "RUNNING"
           or job['lastrun']['status'] == "RESOURCE ACTIVE"):
        time.sleep(10)
        job = client.EcxAPI(session, 'job').get(resid=resjob['id'])
    if (job['lastrun']['status'] == "COMPLETED"):
        return job
    logger.error("Job did not complete, please log in to UI to view logs.")
    session.delete('endeavour/session/')
    sys.exit(2)