コード例 #1
0
def dedicate(rh):
    """
    Dedicate device.

    Input:
       Request Handle with the following properties:
          function    - 'CHANGEVM'
          subfunction - 'DEDICATEDM'
          userid      - userid of the virtual machine
          parms['vaddr']      - Virtual address
          parms['raddr']      - Real address
          parms['mode']       - Read only mode or not.

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """
    rh.printSysLog("Enter changeVM.dedicate")

    parms = [
        "-T", rh.userid, "-v", rh.parms['vaddr'], "-r", rh.parms['raddr'],
        "-R", rh.parms['mode']
    ]

    hideList = []
    results = invokeSMCLI(rh,
                          "Image_Device_Dedicate_DM",
                          parms,
                          hideInLog=hideList)

    if results['overallRC'] != 0:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI

    if results['overallRC'] == 0:
        results = isLoggedOn(rh, rh.userid)
        if (results['overallRC'] == 0 and results['rs'] == 0):
            # Dedicate device to active configuration.
            parms = [
                "-T", rh.userid, "-v", rh.parms['vaddr'], "-r",
                rh.parms['raddr'], "-R", rh.parms['mode']
            ]

            results = invokeSMCLI(rh, "Image_Device_Dedicate", parms)
            if results['overallRC'] == 0:
                rh.printLn(
                    "N", "Dedicated device " + rh.parms['vaddr'] +
                    " to the active configuration.")
            else:
                # SMAPI API failed.
                rh.printLn("ES", results['response'])
                rh.updateResults(results)  # Use results from invokeSMCLI

    rh.printSysLog("Exit changeVM.dedicate, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #2
0
def unpause(rh):
    """
    Unpause a virtual machine.

    Input:
       Request Handle with the following properties:
          function    - 'POWERVM'
          subfunction - 'UNPAUSE'
          userid      - userid of the virtual machine

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter powerVM.unpause, userid: " + rh.userid)

    parms = ["-T", rh.userid, "-k", "PAUSE=NO"]

    results = invokeSMCLI(rh, "Image_Pause", parms)
    if results['overallRC'] != 0:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI

    rh.printSysLog("Exit powerVM.unpause, rc: " + str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #3
0
def removeIPL(rh):
    """
    Sets the IPL statement in the virtual machine's directory entry.

    Input:
       Request Handle with the following properties:
          function    - 'CHANGEVM'
          subfunction - 'REMOVEIPL'
          userid      - userid of the virtual machine

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter changeVM.removeIPL")

    parms = ["-T", rh.userid]
    results = invokeSMCLI(rh, "Image_IPL_Delete_DM", parms)

    if results['overallRC'] != 0:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI

    rh.printSysLog("Exit changeVM.removeIPL, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #4
0
def getAllDirectory(rh):
    """
    Get a list of defined virtual images.
    Input:
       Request Handle with the following properties:
          function    - 'CMDVM'
          subfunction - 'CMD'
    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """
    rh.printSysLog("Enter getVM.getAllDirectory")

    parms = []
    results = invokeSMCLI(rh, "Image_Name_Query_DM", parms)
    if results['overallRC'] == 0:
        results['response'] = re.sub('\*DVHOPT.*', '', results['response'])
        rh.printLn("N", results['response'])
    else:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI

    rh.printSysLog("Exit getVM.getAllDirectory, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #5
0
def getDiskPoolNames(rh):
    """
    Obtain the list of disk pools known to the directory manager.

    Input:
       Request Handle with the following properties:
          function    - 'GETHOST'
          subfunction - 'DISKPOOLNAMES'

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter getHost.getDiskPoolNames")

    parms = ["-q", "1", "-e", "3", "-T", "dummy"]
    results = invokeSMCLI(rh, "Image_Volume_Space_Query_DM", parms)
    if results['overallRC'] == 0:
        for line in results['response'].splitlines():
            poolName = line.partition(' ')[0]
            rh.printLn("N", poolName)
    else:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI

    rh.printSysLog("Exit getHost.getDiskPoolNames, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #6
0
def getFcpDevices(rh):
    """
    Lists the FCP device channels that are active, free, or offline.

    Input:
       Request Handle with the following properties:
          function    - 'GETHOST'
          subfunction - 'FCPDEVICES'

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter getHost.getFcpDevices")

    parms = ["-T", "dummy"]
    results = invokeSMCLI(rh, "System_WWPN_Query", parms)
    if results['overallRC'] == 0:
        rh.printLn("N", results['response'])
    else:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI

    rh.printSysLog("Exit getHost.getFcpDevices, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #7
0
ファイル: test_vmUtils.py プロジェクト: wngzhe/feilong
 def test_getVM_directory_py3(self):
     rh = ReqHandle.ReqHandle(captureLogs=False)
     with mock.patch('subprocess.check_output') as exec_cmd:
         # subprocess.check_output returns bytes in py3
         exec_cmd.return_value = (
             b"0 0 0 (details) None\n"
             b"USER T9572493 LBYONLY 2048m 64G G\nINCLUDE ZCCDFLT\n"
             b"COMMAND DEF STOR RESERVED 63488M\n"
             b"CPU 00 BASE\nIPL 0100\nLOGONBY MAINT\nMACHINE ESA 32\n"
             b"MDISK 0100 3390 48697 5500 OMB1B6 MR\n"
             b"*DVHOPT LNK0 LOG1 RCM1 SMS0 NPW1 LNGAMENG PWC20180808 "
             b"CRC\xf3:\n")
         expected_resp = (
             u"USER T9572493 LBYONLY 2048m 64G G\nINCLUDE ZCCDFLT\n"
             u"COMMAND DEF STOR RESERVED 63488M\nCPU 00 BASE\nIPL 0100\n"
             u"LOGONBY MAINT\nMACHINE ESA 32\n"
             u"MDISK 0100 3390 48697 5500 OMB1B6 MR\n"
             u"*DVHOPT LNK0 LOG1 RCM1 SMS0 NPW1 LNGAMENG PWC20180808 "
             u"CRC\ufffd:\n")
         res = vmUtils.invokeSMCLI(rh, "Image_Query_DM", ['-T', 'fakeuid'])
         self.assertEqual(res['response'], expected_resp)
         exec_cmd.assert_called_once_with([
             'sudo', '/opt/zthin/bin/smcli', 'Image_Query_DM',
             '--addRCheader', '-T', 'fakeuid'
         ],
                                          close_fds=True)
コード例 #8
0
def activate(rh):
    """
    Activate a virtual machine.

    Input:
       Request Handle with the following properties:
          function    - 'POWERVM'
          subfunction - 'ON'
          userid      - userid of the virtual machine
          parms['desiredState']   - Desired state. Optional,
                                    unless 'maxQueries' is specified.
          parms['maxQueries']     - Maximum number of queries to issue.
                                    Optional.
          parms['maxWait']        - Maximum time to wait in seconds. Optional,
                                    unless 'maxQueries' is specified.
          parms['poll']           - Polling interval in seconds. Optional,
                                    unless 'maxQueries' is specified.

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """
    rh.printSysLog("Enter powerVM.activate, userid: " + rh.userid)

    parms = ["-T", rh.userid]
    smcliResults = invokeSMCLI(rh, "Image_Activate", parms)
    if smcliResults['overallRC'] == 0:
        pass
    elif (smcliResults['overallRC'] == 8 and smcliResults['rc'] == 200
          and smcliResults['rs'] == 8):
        pass  # All good.  No need to change the ReqHandle results.
    else:
        # SMAPI API failed.
        rh.printLn("ES", smcliResults['response'])
        rh.updateResults(smcliResults)  # Use results from invokeSMCLI

    if rh.results['overallRC'] == 0 and 'maxQueries' in rh.parms:
        # Wait for the system to be in the desired state of:
        # OS is 'up' and reachable or VM is 'on'.
        if rh.parms['desiredState'] == 'up':
            results = waitForOSState(rh,
                                     rh.userid,
                                     rh.parms['desiredState'],
                                     maxQueries=rh.parms['maxQueries'],
                                     sleepSecs=rh.parms['poll'])
        else:
            results = waitForVMState(rh,
                                     rh.userid,
                                     rh.parms['desiredState'],
                                     maxQueries=rh.parms['maxQueries'],
                                     sleepSecs=rh.parms['poll'])

        if results['overallRC'] == 0:
            rh.printLn("N", "%s: %s" % (rh.userid, rh.parms['desiredState']))
        else:
            rh.updateResults(results)

    rh.printSysLog("Exit powerVM.activate, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #9
0
ファイル: migrateVM.py プロジェクト: wngzhe/feilong
def modifyMigrate(rh):
    """
    Modify an existing VMRelocate request.

    Input:
       Request Handle with the following properties:
          function    - 'MIGRATEVM'
          subfunction - 'MODIFY'
          userid      - userid of the virtual machine
          parms['maxQuiesce'] - maximum quiesce time in seconds,
                                or -1 to indicate no limit.
          parms['maxTotal']   - maximum total time in seconds,
                                or -1 to indicate no limit.

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter migrateVM.modifyMigrate")

    parms = ["-T", rh.userid]

    if 'maxQuiesce' in rh.parms:
        if rh.parms['maxQuiesce'] == -1:
            parms.extend(["-k", "max_quiesce=NOLIMIT"])
        else:
            parms.extend(["-k", "max_quiesce=" + str(rh.parms['maxQuiesce'])])
    if 'maxTotal' in rh.parms:
        if rh.parms['maxTotal'] == -1:
            parms.extend(["-k", "max_total=NOLIMIT"])
        else:
            parms.extend(["-k", "max_total=" + str(rh.parms['maxTotal'])])

    results = invokeSMCLI(rh, "VMRELOCATE_Modify", parms)
    if results['overallRC'] != 0:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI
        if results['rc'] == 8 and results['rs'] == 3010:
            if "1926" in results['response']:
                # No relocations in progress
                msg = msgs.msg['0419'][1] % (modId, rh.userid)
                rh.printLn("ES", msg)
                rh.updateResults(msgs.msg['0419'][0])
            else:
                # More details in message codes
                lines = results['response'].split("\n")
                for line in lines:
                    if "Details:" in line:
                        codes = line.split(' ', 1)[1]
                msg = msgs.msg['0420'][1] % (modId, "VMRELOCATE Modify",
                                             rh.userid, codes)
                rh.printLn("ES", msg)

    rh.printSysLog("Exit migrateVM.modifyMigrate, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #10
0
ファイル: powerVM.py プロジェクト: zhshuj/python-zvm-sdk
def deactivate(rh):
    """
    Deactivate a virtual machine.

    Input:
       Request Handle with the following properties:
          function    - 'POWERVM'
          subfunction - 'OFF'
          userid      - userid of the virtual machine
          parms['maxQueries']     - Maximum number of queries to issue.
                                    Optional.
          parms['maxWait']        - Maximum time to wait in seconds. Optional,
                                    unless 'maxQueries' is specified.
          parms['poll']           - Polling interval in seconds. Optional,
                                    unless 'maxQueries' is specified.

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter powerVM.deactivate, userid: " +
        rh.userid)

    parms = ["-T", rh.userid, "-f", "IMMED"]
    results = invokeSMCLI(rh, "Image_Deactivate", parms)
    if results['overallRC'] == 0:
        pass
    elif (results['overallRC'] == 8 and results['rc'] == 200 and
        (results['rs'] == 12 or results['rs'] == 16)):
        # Tolerable error.  Machine is already in or going into the state
        # we want it to enter.
        rh.printLn("N", rh.userid + ": off")
        rh.updateResults({}, reset=1)
    else:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)    # Use results from invokeSMCLI

    if results['overallRC'] == 0 and 'maxQueries' in rh.parms:
        results = waitForVMState(
            rh,
            rh.userid,
            'off',
            maxQueries=rh.parms['maxQueries'],
            sleepSecs=rh.parms['poll'])
        if results['overallRC'] == 0:
            rh.printLn("N", rh.userid + ": off")
        else:
            rh.updateResults(results)

    rh.printSysLog("Exit powerVM.deactivate, rc: " +
        str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #11
0
ファイル: migrateVM.py プロジェクト: wngzhe/feilong
def getStatus(rh):
    """
    Get status of a VMRelocate request.

    Input:
       Request Handle with the following properties:
          function    - 'MIGRATEVM'
          subfunction - 'STATUS'
          userid      - userid of the virtual machine
          parms['all']       - If present, set status_target to ALL.
          parms['incoming']  - If present, set status_target to INCOMING.
          parms['outgoing']  - If present, set status_target to OUTGOING.
          if parms does not contain 'all', 'incoming' or 'outgoing', the
            status_target is set to 'USER <userid>'.

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter migrateVM.getStatus")

    parms = ["-T", rh.userid]

    if 'all' in rh.parms:
        parms.extend(["-k", "status_target=ALL"])
    elif 'incoming' in rh.parms:
        parms.extend(["-k", "status_target=INCOMING"])
    elif 'outgoing' in rh.parms:
        parms.extend(["-k", "status_target=OUTGOING"])
    else:
        parms.extend(["-k", "status_target=USER " + rh.userid + ""])

    results = invokeSMCLI(rh, "VMRELOCATE_Status", parms)
    if results['overallRC'] != 0:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI
        if results['rc'] == 4 and results['rs'] == 3001:
            # No relocation in progress
            msg = msgs.msg['0419'][1] % (modId, rh.userid)
            rh.printLn("ES", msg)
            rh.updateResults(msgs.msg['0419'][0])
    else:
        rh.printLn("N", results['response'])

    rh.printSysLog("Exit migrateVM.getStatus, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #12
0
def getVolumeInfo(rh):
    """
    Obtain the description info of the volume on the hypervisor.

    Input:
       Request Handle with the following properties:
          function    - 'GETHOST'
          subfunction - 'VOLUMEINFO'

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """
    rh.printSysLog("Enter getHost.getVolumeInfo")

    if 'volumeName' not in rh.parms:
        volumeName = ["*"]
    else:
        if isinstance(rh.parms['volumeName'], list):
            volumeName = rh.parms['volumeName']
        else:
            volumeName = [rh.parms['volumeName']]

    parms = ["-q", "1", "-e", "1", "-T", "dummy", "-n", " ".join(volumeName)]
    results = invokeSMCLI(rh, "Image_Volume_Space_Query_DM", parms)
    if results['overallRC'] == 0:
        for line in results['response'].splitlines():
            volumeInfo = line.strip().split()
            volumeName = volumeInfo[0]
            volumeType = volumeInfo[1]
            volumeSize = volumeInfo[2]
            # Create output string
            outstr1 = 'volume_name:' + volumeName
            outstr2 = 'volume_type:' + volumeType
            outstr3 = 'volume_size:' + volumeSize
            rh.printLn("N", outstr1)
            rh.printLn("N", outstr2)
            rh.printLn("N", outstr3)
    else:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI

    rh.printSysLog("Exit getHost.getVolumeInfo, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #13
0
ファイル: migrateVM.py プロジェクト: wngzhe/feilong
def testMigrate(rh):
    """
    Test the ability to use VMRelocate on the target userid.

    Input:
       Request Handle with the following properties:
          function       - 'MIGRATEVM'
          subfunction    - 'TEST'
          userid         - userid of the virtual machine
          parms['dest']  - Target SSI system.

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter migrateVM.testMigrate")

    parms = ["-T", rh.userid, "-k", "action=TEST"]

    if 'dest' in rh.parms:
        parms.extend(["-k", "destination=" + rh.parms['dest']])

    results = invokeSMCLI(rh, "VMRELOCATE", parms)
    if results['overallRC'] != 0:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI
        if results['rc'] == 4 and results['rs'] == 3000:
            if "0045" in results['response']:
                # User not logged on
                msg = msgs.msg['0418'][1] % (modId, rh.userid)
                rh.printLn("ES", msg)
                rh.updateResults(msgs.msg['0418'][0])
            else:
                # More details in message codes
                lines = results['response'].split("\n")
                for line in lines:
                    if "Details:" in line:
                        codes = line.split(' ', 1)[1]
                msg = msgs.msg['0420'][1] % (modId, "VMRELOCATE Move",
                                             rh.userid, codes)

    rh.printSysLog("Exit migrateVM.testMigrate, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #14
0
def getCPUCount(rh):
    """
    Obtain general information about the host.

    Input:
       Request Handle

    Output:
       Request Handle updated with the results.
       Return code - not 0: ok
       Return code - 0,0: problem getting some info by System_Processor_Query
    """

    rh.printSysLog("Enter getHost.lparCPUCount")
    rh.results['overallRC'] = 0

    # LPAR CPUs total and used is not support mixed CP + IFL
    # So get cpu num from System_Processor_Query
    # to override LPAR CPUs total and used
    parms = []
    results = invokeSMCLI(rh, "System_Processor_Query", parms)
    cpu_total = 0
    cpu_use = 0
    if results['overallRC'] == 0:
        flag = 0
        for line in results['response'].splitlines():
            line_value = line.partition(' ')[2]
            if not line_value.strip():
                continue
            else:
                type_row = line_value.split(' ')
                if len(type_row) > 1:
                    type_row = line_value.split(' ')[1]
                    if type_row == 'TYPE':
                        flag = 1
                    if flag == 1:
                        status_row = line_value.split(' ')[0]
                        if (status_row.find('MASTER') != -1
                                or status_row == 'ALTERNATE'
                                or status_row == 'PARKED'):
                            cpu_use = cpu_use + 1
                        if (type_row == 'CP' or type_row == 'IFL'):
                            cpu_total = cpu_total + 1
    return cpu_total, cpu_use
コード例 #15
0
ファイル: migrateVM.py プロジェクト: wngzhe/feilong
def cancelMigrate(rh):
    """
    Cancel an existing VMRelocate request.

    Input:
       Request Handle with the following properties:
          function    - 'MIGRATEVM'
          subfunction - 'CANCEL'
          userid      - userid of the virtual machine

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter migrateVM.cancelMigrate")

    parms = ["-T", rh.userid, "-k", "action=CANCEL"]
    results = invokeSMCLI(rh, "VMRELOCATE", parms)
    if results['overallRC'] != 0:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI
        if results['rc'] == 8 and results['rs'] == 3000:
            if "1926" in results['response']:
                # No relocation in progress
                msg = msgs.msg['0419'][1] % (modId, rh.userid)
                rh.printLn("ES", msg)
                rh.updateResults(msgs.msg['0419'][0])
            else:
                # More details in message codes
                lines = results['response'].split("\n")
                for line in lines:
                    if "Details:" in line:
                        codes = line.split(' ', 1)[1]
                msg = msgs.msg['420'][1] % (modId, "VMRELOCATE Cancel",
                                            rh.userid, codes)
                rh.printLn("ES", msg)

    rh.printSysLog("Exit migrateVM.cancelMigrate, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #16
0
def invokeSmapiApi(rh):
    """
    Invoke a SMAPI API.

    Input:
       Request Handle with the following properties:
          function    - 'SMAPI'
          subfunction - 'API'
          userid      - 'HYPERVISOR'
          parms['apiName']   - Name of API as defined by SMCLI
          parms['operands']  - List (array) of operands to send or
                               an empty list.

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter smapi.invokeSmapiApi")
    if rh.userid != 'HYPERVISOR':
        userid = rh.userid
    else:
        userid = 'dummy'

    parms = ["-T", userid]
    if 'operands' in rh.parms:
        parms.extend(rh.parms['operands'])

    # SSI_Query does not need any param
    if rh.parms['apiName'] == 'SSI_Query':
        parms = []

    results = invokeSMCLI(rh, rh.parms['apiName'], parms)
    if results['overallRC'] == 0:
        rh.printLn("N", results['response'])
    else:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI

    rh.printSysLog("Exit smapi.invokeCmd, rc: " + str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #17
0
def getDiskPoolVolumes(rh):
    """
    Obtain the list of volumes for the disk_pools on the hypervisor.

    Input:
       Request Handle with the following properties:
          function    - 'GETHOST'
          subfunction - 'DISKPOOLVOLUMES'

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """
    rh.printSysLog("Enter getHost.getDiskPoolVolumes")

    if 'poolName' not in rh.parms:
        poolNames = ["*"]
    else:
        if isinstance(rh.parms['poolName'], list):
            poolNames = rh.parms['poolName']
        else:
            poolNames = [rh.parms['poolName']]

    parms = ["-q", "1", "-e", "3", "-T", "dummy", "-n", " ".join(poolNames)]
    results = invokeSMCLI(rh, "Image_Volume_Space_Query_DM", parms)
    if results['overallRC'] == 0:
        for line in results['response'].splitlines():
            poolVolumes = line.strip().split()
            poolVolumes.pop(0)
            poolVolumes = ' '.join(poolVolumes)
            # Create output string
            outstr = 'Diskpool Volumes:' + poolVolumes
            rh.printLn("N", outstr)
    else:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI

    rh.printSysLog("Exit getHost.getDiskPoolVolumes, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #18
0
ファイル: getVM.py プロジェクト: wgxoyun/python-zvm-sdk
def fcpinfo(rh):
    """
    Get fcp info and filter by the status.

    Input:
       Request Handle with the following properties:
          function    - 'GETVM'
          subfunction - 'FCPINFO'
          userid      - userid of the virtual machine
          parms['status']       - The status for filter results.

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """
    rh.printSysLog("Enter changeVM.dedicate")

    parms = ["-T", rh.userid]

    hideList = []
    results = invokeSMCLI(rh,
                          "System_WWPN_Query",
                          parms,
                          hideInLog=hideList)

    if results['overallRC'] != 0:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)    # Use results from invokeSMCLI

    if results['overallRC'] == 0:
        # extract data from smcli return
        ret = extract_fcp_data(results['response'], rh.parms['status'])
        # write the ret into results['response']
        rh.printLn("N", ret)
    else:
        rh.printLn("ES", results['response'])
        rh.updateResults(results)    # Use results from invokeSMCLI

    return rh.results['overallRC']
コード例 #19
0
def addIPL(rh):
    """
    Sets the IPL statement in the virtual machine's directory entry.

    Input:
       Request Handle with the following properties:
          function    - 'CHANGEVM'
          subfunction - 'IPL'
          userid      - userid of the virtual machine
          parms['addrOrNSS']  - Address or NSS name
          parms['loadparms']  - Loadparms operand (optional)
          parms['parms']      - Parms operand (optional)

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter changeVM.addIPL")

    parms = ["-T", rh.userid, "-s", rh.parms['addrOrNSS']]

    if 'loadparms' in rh.parms:
        parms.extend(["-l", rh.parms['loadparms']])
    if 'parms' in rh.parms:
        parms.extend(["-p", rh.parms['parms']])

    results = invokeSMCLI(rh, "Image_IPL_Set_DM", parms)

    if results['overallRC'] != 0:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI

    rh.printSysLog("Exit changeVM.addIPL, rc: " + str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #20
0
def getGeneralInfo(rh):
    """
    Obtain general information about the host.

    Input:
       Request Handle with the following properties:
          function    - 'GETHOST'
          subfunction - 'GENERAL'

    Output:
       Request Handle updated with the results.
       Return code - 0: ok
       Return code - 4: problem getting some info
    """

    rh.printSysLog("Enter getHost.getGeneralInfo")

    # Get host using VMCP
    rh.results['overallRC'] = 0
    cmd = ["sudo", "/sbin/vmcp", "query userid"]
    strCmd = ' '.join(cmd)
    rh.printSysLog("Invoking: " + strCmd)
    try:
        host = subprocess.check_output(cmd,
                                       close_fds=True,
                                       stderr=subprocess.STDOUT)
        host = bytes.decode(host)
        userid = host.split()[0]
        host = host.split()[2]
    except subprocess.CalledProcessError as e:
        msg = msgs.msg['0405'][1] % (modId, "Hypervisor Name", strCmd,
                                     e.output)
        rh.printLn("ES", msg)
        rh.updateResults(msgs.msg['0405'][0])
        host = "no info"
    except Exception as e:
        # All other exceptions.
        rh.printLn(
            "ES",
            msgs.msg['0421'][1] % (modId, strCmd, type(e).__name__, str(e)))
        rh.updateResults(msgs.msg['0421'][0])
        host = "no info"

    # Get a bunch of info from /proc/sysinfo
    lparCpuTotal = "no info"
    lparCpuUsed = "no info"
    cecModel = "no info"
    cecVendor = "no info"
    hvInfo = "no info"
    with open('/proc/sysinfo', 'r') as myFile:
        for num, line in enumerate(myFile, 1):
            # Get total physical CPU in this LPAR
            if "LPAR CPUs Total" in line:
                lparCpuTotal = line.split()[3]
            # Get used physical CPU in this LPAR
            if "LPAR CPUs Configured" in line:
                lparCpuUsed = line.split()[3]
            # Get CEC model
            if "Type:" in line:
                cecModel = line.split()[1]
            # Get vendor of CEC
            if "Manufacturer:" in line:
                cecVendor = line.split()[1]
            # Get hypervisor type and version
            if "VM00 Control Program" in line:
                hvInfo = line.split()[3] + " " + line.split()[4]
    if lparCpuTotal == "no info":
        msg = msgs.msg['0405'][1] % (modId, "LPAR CPUs Total",
                                     "cat /proc/sysinfo", "not found")
        rh.printLn("ES", msg)
        rh.updateResults(msgs.msg['0405'][0])
    if lparCpuUsed == "no info":
        msg = msgs.msg['0405'][1] % (modId, "LPAR CPUs Configured",
                                     "cat /proc/sysinfo", "not found")
        rh.printLn("ES", msg)
        rh.updateResults(msgs.msg['0405'][0])
    if cecModel == "no info":
        msg = msgs.msg['0405'][1] % (modId, "Type:", "cat /proc/sysinfo",
                                     "not found")
        rh.printLn("ES", msg)
        rh.updateResults(msgs.msg['0405'][0])
    if cecVendor == "no info":
        msg = msgs.msg['0405'][1] % (modId, "Manufacturer:",
                                     "cat /proc/sysinfo", "not found")
        rh.printLn("ES", msg)
        rh.updateResults(msgs.msg['0405'][0])
    if hvInfo == "no info":
        msg = msgs.msg['0405'][1] % (modId, "VM00 Control Program",
                                     "cat /proc/sysinfo", "not found")
        rh.printLn("ES", msg)
        rh.updateResults(msgs.msg['0405'][0])

    # Get processor architecture
    arch = str(os.uname()[4])

    # Get LPAR memory total & offline
    parm = ["-T", "dummy", "-k", "STORAGE="]

    lparMemTotal = "no info"
    lparMemStandby = "no info"
    results = invokeSMCLI(rh, "System_Information_Query", parm)
    if results['overallRC'] == 0:
        for line in results['response'].splitlines():
            if "STORAGE=" in line:
                lparMemOnline = line.split()[0]
                lparMemStandby = line.split()[4]
                lparMemTotal = lparMemOnline.split("=")[2]
                lparMemStandby = lparMemStandby.split("=")[1]
    else:
        # SMAPI API failed, so we put out messages
        # 300 and 405 for consistency
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI
        msg = msgs.msg['0405'][1] % (modId, "LPAR memory", "(see message 300)",
                                     results['response'])
        rh.printLn("ES", msg)

    # Get LPAR memory in use
    parm = ["-T", "dummy", "-k", "detailed_cpu=show=no"]

    lparMemUsed = "no info"
    results = invokeSMCLI(rh, "System_Performance_Information_Query", parm)
    if results['overallRC'] == 0:
        for line in results['response'].splitlines():
            if "MEMORY_IN_USE=" in line:
                lparMemUsed = line.split("=")[1]
                lparMemUsed = generalUtils.getSizeFromPage(rh, lparMemUsed)
    else:
        # SMAPI API failed, so we put out messages
        # 300 and 405 for consistency
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI
        msg = msgs.msg['0405'][1] % (modId, "LPAR memory in use",
                                     "(see message 300)", results['response'])
        rh.printLn("ES", msg)

    # Get IPL Time
    ipl = ""
    cmd = ["sudo", "/sbin/vmcp", "query cplevel"]
    strCmd = ' '.join(cmd)
    rh.printSysLog("Invoking: " + strCmd)
    try:
        ipl = subprocess.check_output(cmd,
                                      close_fds=True,
                                      stderr=subprocess.STDOUT)
        ipl = bytes.decode(ipl).split("\n")[2]
    except subprocess.CalledProcessError as e:
        msg = msgs.msg['0405'][1] % (modId, "IPL Time", strCmd, e.output)
        rh.printLn("ES", msg)
        rh.updateResults(msgs.msg['0405'][0])
    except Exception as e:
        # All other exceptions.
        rh.printLn(
            "ES",
            msgs.msg['0421'][1] % (modId, strCmd, type(e).__name__, str(e)))
        rh.updateResults(msgs.msg['0421'][0])

    # Create output string
    outstr = "ZCC USERID: " + userid
    outstr += "\nz/VM Host: " + host
    outstr += "\nArchitecture: " + arch
    outstr += "\nCEC Vendor: " + cecVendor
    outstr += "\nCEC Model: " + cecModel
    outstr += "\nHypervisor OS: " + hvInfo
    outstr += "\nHypervisor Name: " + host
    outstr += "\nLPAR CPU Total: " + lparCpuTotal
    outstr += "\nLPAR CPU Used: " + lparCpuUsed
    outstr += "\nLPAR Memory Total: " + lparMemTotal
    outstr += "\nLPAR Memory Offline: " + lparMemStandby
    outstr += "\nLPAR Memory Used: " + lparMemUsed
    outstr += "\nIPL Time: " + ipl

    rh.printLn("N", outstr)
    rh.printSysLog("Exit getHost.getGeneralInfo, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #21
0
def getDiskPoolSpace(rh):
    """
    Obtain disk pool space information for all or a specific disk pool.

    Input:
       Request Handle with the following properties:
          function            - 'GETHOST'
          subfunction         - 'DISKPOOLSPACE'
          parms['poolName']   - Name of the disk pool. Optional,
                                if not present then information for all
                                disk pools is obtained.

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter getHost.getDiskPoolSpace")

    results = {'overallRC': 0}

    if 'poolName' not in rh.parms:
        poolNames = ["*"]
    else:
        if isinstance(rh.parms['poolName'], list):
            poolNames = rh.parms['poolName']
        else:
            poolNames = [rh.parms['poolName']]

    if results['overallRC'] == 0:
        # Loop thru each pool getting total.  Do it for query 2 & 3
        totals = {}
        for qType in ["2", "3"]:
            parms = [
                "-q", qType, "-e", "3", "-T", "DUMMY", "-n",
                " ".join(poolNames)
            ]

            results = invokeSMCLI(rh, "Image_Volume_Space_Query_DM", parms)
            if results['overallRC'] == 0:
                for line in results['response'].splitlines():
                    parts = line.split()
                    if len(parts) == 9:
                        poolName = parts[7]
                    else:
                        poolName = parts[4]
                    if poolName not in totals:
                        totals[poolName] = {"2": 0., "3": 0.}

                    if parts[1][:4] == "3390":
                        totals[poolName][qType] += int(parts[3]) * 737280
                    elif parts[1][:4] == "9336":
                        totals[poolName][qType] += int(parts[3]) * 512
            else:
                # SMAPI API failed.
                rh.printLn("ES", results['response'])
                rh.updateResults(results)  # Use results from invokeSMCLI
                break

        if results['overallRC'] == 0:
            if len(totals) == 0:
                # No pool information found.
                msg = msgs.msg['0402'][1] % (modId, " ".join(poolNames))
                rh.printLn("ES", msg)
                rh.updateResults(msgs.msg['0402'][0])
            else:
                # Produce a summary for each pool
                for poolName in sorted(totals):
                    total = totals[poolName]["2"] + totals[poolName]["3"]
                    rh.printLn(
                        "N", poolName + " Total: " +
                        generalUtils.cvtToMag(rh, total))
                    rh.printLn(
                        "N", poolName + " Used: " +
                        generalUtils.cvtToMag(rh, totals[poolName]["3"]))
                    rh.printLn(
                        "N", poolName + " Free: " +
                        generalUtils.cvtToMag(rh, totals[poolName]["2"]))

    rh.printSysLog("Exit getHost.getDiskPoolSpace, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #22
0
ファイル: makeVM.py プロジェクト: chris6154/feilong
def createVM(rh):
    """
    Create a virtual machine in z/VM.

    Input:
       Request Handle with the following properties:
          function    - 'CMDVM'
          subfunction - 'CMD'
          userid      - userid of the virtual machine

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter makeVM.createVM")

    dirLines = []
    dirLines.append("USER " + rh.userid + " " + rh.parms['pw'] + " " +
                    rh.parms['priMemSize'] + " " + rh.parms['maxMemSize'] +
                    " " + rh.parms['privClasses'])

    if 'profName' in rh.parms:
        dirLines.append("INCLUDE " + rh.parms['profName'])

    if 'maxCPU' in rh.parms:
        dirLines.append("MACHINE ESA %i" % rh.parms['maxCPU'])

    dirLines.append("CPU 00 BASE")
    if 'cpuCnt' in rh.parms:
        for i in range(1, rh.parms['cpuCnt']):
            dirLines.append("CPU %0.2X" % i)

    if 'ipl' in rh.parms:
        ipl_string = "IPL %s " % rh.parms['ipl']

        if 'iplParam' in rh.parms:
            ipl_string += ("PARM %s " % rh.parms['iplParam'])

        if 'iplLoadparam' in rh.parms:
            ipl_string += ("LOADPARM %s " % rh.parms['iplLoadparam'])

        dirLines.append(ipl_string)

    if 'byUsers' in rh.parms:
        for user in rh.parms['byUsers']:
            dirLines.append("LOGONBY " + user)

    priMem = rh.parms['priMemSize'].upper()
    maxMem = rh.parms['maxMemSize'].upper()
    if 'setReservedMem' in rh.parms:
        reservedSize = getReservedMemSize(rh, priMem, maxMem)
        if rh.results['overallRC'] != 0:
            rh.printSysLog("Exit makeVM.createVM, rc: " +
                           str(rh.results['overallRC']))
            return rh.results['overallRC']
        # Even reservedSize is 0M, still write the line "COMMAND DEF
        # STOR RESERVED 0M" in direct entry, in case cold resize of
        # memory decreases the defined memory, then reserved memory
        # size would be > 0, this line in direct entry need be updated.
        # If no such line defined in user direct, resizing would report
        # error due to it can't get the original reserved memory value.
        dirLines.append("COMMAND DEF STOR RESERVED %s" % reservedSize)

    if 'loadportname' in rh.parms:
        wwpn = rh.parms['loadportname'].replace("0x", "")
        dirLines.append("LOADDEV PORTname %s" % wwpn)

    if 'loadlun' in rh.parms:
        lun = rh.parms['loadlun'].replace("0x", "")
        dirLines.append("LOADDEV LUN %s" % lun)

    if 'dedicate' in rh.parms:
        vdevs = rh.parms['dedicate'].split()
        # add a DEDICATE statement for each vdev
        for vdev in vdevs:
            dirLines.append("DEDICATE %s %s" % (vdev, vdev))

    if 'vdisk' in rh.parms:
        v = rh.parms['vdisk'].split(':')
        sizeUpper = v[1].strip().upper()
        sizeUnit = sizeUpper[-1]
        # blocks = size / 512, as we are using M,
        # it means 1024*1024 / 512 = 2048
        if sizeUnit == 'M':
            blocks = int(sizeUpper[0:len(sizeUpper) - 1]) * 2048
        else:
            blocks = int(sizeUpper[0:len(sizeUpper) - 1]) * 2097152

        if blocks > 4194304:
            # not support exceed 2G disk size
            msg = msgs.msg['0207'][1] % (modId)
            rh.printLn("ES", msg)
            rh.updateResults(msgs.msg['0207'][0])
            rh.printSysLog("Exit makeVM.createVM, rc: " +
                           str(rh.results['overallRC']))
            return rh.results['overallRC']

        # https://www.ibm.com/support/knowledgecenter/SSB27U_6.4.0/
        # com.ibm.zvm.v640.hcpb7/defvdsk.htm#defvdsk
        # the maximum number of VDISK blocks is 4194296
        if blocks > MAX_VDISK_BLOCKS:
            blocks = MAX_VDISK_BLOCKS

        dirLines.append("MDISK %s FB-512 V-DISK %s MWV" % (v[0], blocks))

    # Construct the temporary file for the USER entry.
    fd, tempFile = mkstemp()
    to_write = '\n'.join(dirLines) + '\n'
    os.write(fd, to_write.encode())
    os.close(fd)

    parms = ["-T", rh.userid, "-f", tempFile]
    results = invokeSMCLI(rh, "Image_Create_DM", parms)
    if results['overallRC'] != 0:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI

    os.remove(tempFile)

    rh.printSysLog("Exit makeVM.createVM, rc: " + str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #23
0
def addLOADDEV(rh):
    """
    Sets the LOADDEV statement in the virtual machine's directory entry.

    Input:
       Request Handle with the following properties:
          function    - 'CHANGEVM'
          subfunction - 'ADDLOADDEV'
          userid      - userid of the virtual machine
          parms['boot']       - Boot program number
          parms['addr']       - Logical block address of the boot record
          parms['lun']        - One to eight-byte logical unit number
                                of the FCP-I/O device.
          parms['wwpn']       - World-Wide Port Number
          parms['scpDataType'] - SCP data type
          parms['scpData']    - Designates information to be passed to the
                                program is loaded during guest IPL.

          Note that any of the parms may be left blank, in which case
          we will not update them.


    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """
    rh.printSysLog("Enter changeVM.addLOADDEV")

    # scpDataType and scpData must appear or disappear concurrently
    if ('scpData' in rh.parms and 'scpDataType' not in rh.parms):
        msg = msgs.msg['0014'][1] % (modId, "scpData", "scpDataType")
        rh.printLn("ES", msg)
        rh.updateResults(msgs.msg['0014'][0])
        return
    if ('scpDataType' in rh.parms and 'scpData' not in rh.parms):
        if rh.parms['scpDataType'].lower() == "delete":
            scpDataType = 1
        else:
            # scpDataType and scpData must appear or disappear
            # concurrently unless we're deleting data
            msg = msgs.msg['0014'][1] % (modId, "scpDataType", "scpData")
            rh.printLn("ES", msg)
            rh.updateResults(msgs.msg['0014'][0])
            return

    scpData = ""
    if 'scpDataType' in rh.parms:
        if rh.parms['scpDataType'].lower() == "hex":
            scpData = rh.parms['scpData']
            scpDataType = 3
        elif rh.parms['scpDataType'].lower() == "ebcdic":
            scpData = rh.parms['scpData']
            scpDataType = 2
        # scpDataType not hex, ebcdic or delete
        elif rh.parms['scpDataType'].lower() != "delete":
            msg = msgs.msg['0016'][1] % (modId, rh.parms['scpDataType'])
            rh.printLn("ES", msg)
            rh.updateResults(msgs.msg['0016'][0])
            return
    else:
        # Not specified, 0 for do nothing
        scpDataType = 0
        scpData = ""

    if 'boot' not in rh.parms:
        boot = ""
    else:
        boot = rh.parms['boot']
    if 'addr' not in rh.parms:
        block = ""
    else:
        block = rh.parms['addr']
    if 'lun' not in rh.parms:
        lun = ""
    else:
        lun = rh.parms['lun']
        # Make sure it doesn't have the 0x prefix
        lun.replace("0x", "")
    if 'wwpn' not in rh.parms:
        wwpn = ""
    else:
        wwpn = rh.parms['wwpn']
        # Make sure it doesn't have the 0x prefix
        wwpn.replace("0x", "")

    parms = [
        "-T", rh.userid, "-b", boot, "-k", block, "-l", lun, "-p", wwpn, "-s",
        str(scpDataType)
    ]

    if scpData != "":
        parms.extend(["-d", scpData])

    results = invokeSMCLI(rh, "Image_SCSI_Characteristics_Define_DM", parms)

    # SMAPI API failed.
    if results['overallRC'] != 0:
        rh.printLn("ES", results['response'])
        rh.updateResults(results)

    rh.printSysLog("Exit changeVM.addLOADDEV, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #24
0
def removeDisk(rh):
    """
    Remove a disk from a virtual machine.

    Input:
       Request Handle with the following properties:
          function         - 'CHANGEVM'
          subfunction      - 'REMOVEDISK'
          userid           - userid of the virtual machine
          parms['vaddr']   - Virtual address

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter changeVM.removeDisk")

    results = {'overallRC': 0, 'rc': 0, 'rs': 0}

    # Is image logged on
    loggedOn = False
    results = isLoggedOn(rh, rh.userid)
    if results['overallRC'] == 0:
        if results['rs'] == 0:
            loggedOn = True

            results = disableEnableDisk(rh, rh.userid, rh.parms['vaddr'], '-d')
            if results['overallRC'] != 0:
                rh.printLn("ES", results['response'])
                rh.updateResults(results)

    if results['overallRC'] == 0 and loggedOn:
        strCmd = "/sbin/vmcp detach " + rh.parms['vaddr']
        results = execCmdThruIUCV(rh, rh.userid, strCmd)
        if results['overallRC'] != 0:
            if re.search('(^HCP\w\w\w040E)', results['response']):
                # Device does not exist, ignore the error
                results = {'overallRC': 0, 'rc': 0, 'rs': 0, 'response': ''}
            else:
                rh.printLn("ES", results['response'])
                rh.updateResults(results)

    if results['overallRC'] == 0:
        # Remove the disk from the user entry.
        parms = ["-T", rh.userid, "-v", rh.parms['vaddr'], "-e", "0"]

        results = invokeSMCLI(rh, "Image_Disk_Delete_DM", parms)
        if results['overallRC'] != 0:
            if (results['overallRC'] == 8 and results['rc'] == 208
                    and results['rs'] == 36):
                # Disk does not exist, ignore the error
                results = {'overallRC': 0, 'rc': 0, 'rs': 0, 'response': ''}
            else:
                # SMAPI API failed.
                rh.printLn("ES", results['response'])
                rh.updateResults(results)  # Use results from invokeSMCLI

    else:
        # Unexpected error.  Message already sent.
        rh.updateResults(results)

    rh.printSysLog("Exit changeVM.removeDisk, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #25
0
def getConsole(rh):
    """
    Get the virtual machine's console output.

    Input:
       Request Handle with the following properties:
          function    - 'CMDVM'
          subfunction - 'CMD'
          userid      - userid of the virtual machine

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter getVM.getConsole")

    # Transfer the console to this virtual machine.
    parms = ["-T", rh.userid]
    results = invokeSMCLI(rh, "Image_Console_Get", parms)

    if results['overallRC'] != 0:
        if (results['overallRC'] == 8 and results['rc'] == 8
                and results['rs'] == 8):
            # Give a more specific message.  Userid is either
            # not logged on or not spooling their console.
            msg = msgs.msg['0409'][1] % (modId, rh.userid)
        else:
            msg = results['response']
        rh.updateResults(results)  # Use results from invokeSMCLI
        rh.printLn("ES", msg)
        rh.printSysLog("Exit getVM.parseCmdLine, rc: " +
                       str(rh.results['overallRC']))
        return rh.results['overallRC']

    # Check whether the reader is online
    with open('/sys/bus/ccw/drivers/vmur/0.0.000c/online', 'r') as myfile:
        out = myfile.read().replace('\n', '')
        myfile.close()

    # Nope, offline, error out and exit
    if int(out) != 1:
        msg = msgs.msg['0411'][1]
        rh.printLn("ES", msg)
        rh.updateResults(msgs.msg['0411'][0])
        rh.printSysLog("Exit getVM.parseCmdLine, rc: " +
                       str(rh.results['overallRC']))
        return rh.results['overallRC']

    # We should set class to *, otherwise we will get errors like:
    # vmur: Reader device class does not match spool file class
    cmd = ["sudo", "/sbin/vmcp", "spool reader class *"]
    strCmd = ' '.join(cmd)
    rh.printSysLog("Invoking: " + strCmd)
    try:
        subprocess.check_output(cmd, close_fds=True, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        # If we couldn't change the class, that's not fatal
        # But we want to warn about possibly incomplete
        # results
        msg = msgs.msg['0407'][1] % (modId, strCmd, e.output)
        rh.printLn("WS", msg)
    except Exception as e:
        # All other exceptions.
        # If we couldn't change the class, that's not fatal
        # But we want to warn about possibly incomplete
        # results
        rh.printLn(
            "ES",
            msgs.msg['0422'][1] % (modId, strCmd, type(e).__name__, str(e)))
        rh.printLn("ES", msgs.msg['0423'][1] % modId, strCmd,
                   type(e).__name__, str(e))

    # List the spool files in the reader
    cmd = ["sudo", "/usr/sbin/vmur", "list"]
    strCmd = ' '.join(cmd)
    rh.printSysLog("Invoking: " + strCmd)
    try:
        files = subprocess.check_output(cmd,
                                        close_fds=True,
                                        stderr=subprocess.STDOUT)
        files = bytes.decode(files)
    except subprocess.CalledProcessError as e:
        # Uh oh, vmur list command failed for some reason
        msg = msgs.msg['0408'][1] % (modId, rh.userid, strCmd, e.output)
        rh.printLn("ES", msg)
        rh.updateResults(msgs.msg['0408'][0])
        rh.printSysLog("Exit getVM.parseCmdLine, rc: " +
                       str(rh.results['overallRC']))
        return rh.results['overallRC']
    except Exception as e:
        # All other exceptions.
        rh.printLn(
            "ES",
            msgs.msg['0421'][1] % (modId, strCmd, type(e).__name__, str(e)))
        rh.updateResults(msgs.msg['0421'][0])
        rh.printSysLog("Exit getVM.parseCmdLine, rc: " +
                       str(rh.results['overallRC']))
        return rh.results['overallRC']

    # Now for each line that contains our user and is a
    # class T console file, add the spool id to our list
    spoolFiles = files.split('\n')
    outstr = ""
    for myfile in spoolFiles:
        if (myfile != "" and myfile.split()[0] == rh.userid
                and myfile.split()[2] == "T" and myfile.split()[3] == "CON"):

            fileId = myfile.split()[1]
            outstr += fileId + " "

    # No files in our list
    if outstr == "":
        msg = msgs.msg['0410'][1] % (modId, rh.userid)
        rh.printLn("ES", msg)
        rh.updateResults(msgs.msg['0410'][0])
        rh.printSysLog("Exit getVM.parseCmdLine, rc: " +
                       str(rh.results['overallRC']))
        return rh.results['overallRC']

    # Output the list
    rh.printLn(
        "N", "List of spool files containing "
        "console logs from %s: %s" % (rh.userid, outstr))

    rh.results['overallRC'] = 0
    rh.printSysLog("Exit getVM.getConsole, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #26
0
ファイル: migrateVM.py プロジェクト: wngzhe/feilong
def moveVM(rh):
    """
    Initiate a VMRelocate request to move a userid.

    Input:
       Request Handle with the following properties:
          function    - 'MIGRATEVM'
          subfunction - 'MOVE'
          userid      - userid of the virtual machine
          parms['destination']  - target SSI member
          parms['forcearch']    - if present, force=architecture is set.
          parms['forcedomain']  - if present, force=domain is set.
          parms['forcestorage'] - if present, force=storage is set.
          parms['immediate']    - if present, immediate=YES is set.
          parms['maxquiesce']   - maximum quiesce time in seconds,
                                  or -1 to indicate no limit.
          parms['maxTotal']     - maximum total time in seconds,
                                  or -1 to indicate no limit.

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter migrateVM.moveVM")

    parms = ["-T", rh.userid, "-k", "action=MOVE"]

    if 'dest' in rh.parms:
        parms.extend(["-k", "destination=" + rh.parms['dest']])

    forceOption = ''
    if 'forcearch' in rh.parms:
        forceOption = "ARCHITECTURE "
    if 'forcedomain' in rh.parms:
        forceOption = forceOption + "DOMAIN "
    if 'forcestorage' in rh.parms:
        forceOption = forceOption + "STORAGE "
    if forceOption != '':
        parms.extend(["-k", "\'force=" + forceOption + "\'"])

    if 'immediate' in rh.parms:
        parms.extend(["-k", "\'immediate=YES"])

    if 'maxQuiesce' in rh.parms:
        if rh.parms['maxQuiesce'] == -1:
            parms.extend(["-k", "max_quiesce=NOLIMIT"])
        else:
            parms.extend(["-k", "max_quiesce=" + str(rh.parms['maxQuiesce'])])
    if 'maxTotal' in rh.parms:
        if rh.parms['maxTotal'] == -1:
            parms.extend(["-k", "max_total=NOLIMIT"])
        else:
            parms.extend(["-k", "max_total=" + str(rh.parms['maxTotal'])])

    results = invokeSMCLI(rh, "VMRELOCATE", parms)
    if results['overallRC'] != 0:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI
        if results['rc'] == 8 and results['rs'] == 3000:
            if "0045" in results['response']:
                # User not logged on
                msg = msgs.msg['0418'][1] % (modId, rh.userid)
                rh.printLn("ES", msg)
                rh.updateResults(msgs.msg['0418'][0])
            else:
                # More details in message codes
                lines = results['response'].split("\n")
                for line in lines:
                    if "Details:" in line:
                        codes = line.split(' ', 1)[1]
                msg = msgs.msg['0420'][1] % (modId, "VMRELOCATE Move",
                                             rh.userid, codes)
                rh.printLn("ES", msg)

    rh.printSysLog("Exit migrateVM.moveVM, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #27
0
ファイル: makeVM.py プロジェクト: wuhaijie-ibm/python-zvm-sdk
def createVM(rh):
    """
    Create a virtual machine in z/VM.

    Input:
       Request Handle with the following properties:
          function    - 'CMDVM'
          subfunction - 'CMD'
          userid      - userid of the virtual machine

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter makeVM.createVM")

    dirLines = []
    dirLines.append("USER " + rh.userid + " " + rh.parms['pw'] + " " +
                    rh.parms['priMemSize'] + " " + rh.parms['maxMemSize'] +
                    " " + rh.parms['privClasses'])
    if 'profName' in rh.parms:
        dirLines.append("INCLUDE " + rh.parms['profName'])

    if 'maxCPU' in rh.parms:
        dirLines.append("MACHINE ESA %i" % rh.parms['maxCPU'])

    dirLines.append("CPU 00 BASE")
    if 'cpuCnt' in rh.parms:
        for i in range(1, rh.parms['cpuCnt']):
            dirLines.append("CPU %0.2X" % i)

    if 'ipl' in rh.parms:
        ipl_string = "IPL %s " % rh.parms['ipl']

        if 'iplParam' in rh.parms:
            ipl_string += ("PARM %s " % rh.parms['iplParam'])

        if 'iplLoadparam' in rh.parms:
            ipl_string += ("LOADPARM %s " % rh.parms['iplLoadparam'])

        dirLines.append(ipl_string)

    if 'byUsers' in rh.parms:
        for user in rh.parms['byUsers']:
            dirLines.append("LOGONBY " + user)

    priMem = rh.parms['priMemSize'].upper()
    maxMem = rh.parms['maxMemSize'].upper()
    if 'setReservedMem' in rh.parms and (priMem != maxMem):
        reservedSize = getReservedMemSize(rh, priMem, maxMem)
        if rh.results['overallRC'] != 0:
            rh.printSysLog("Exit makeVM.createVM, rc: " +
                           str(rh.results['overallRC']))
            return rh.results['overallRC']
        if reservedSize != '0M':
            dirLines.append("COMMAND DEF STOR RESERVED %s" % reservedSize)

    # Construct the temporary file for the USER entry.
    fd, tempFile = mkstemp()
    to_write = '\n'.join(dirLines) + '\n'
    os.write(fd, to_write.encode())
    os.close(fd)

    parms = ["-T", rh.userid, "-f", tempFile]
    results = invokeSMCLI(rh, "Image_Create_DM", parms)
    if results['overallRC'] != 0:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)  # Use results from invokeSMCLI

    os.remove(tempFile)

    rh.printSysLog("Exit makeVM.createVM, rc: " + str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #28
0
def softDeactivate(rh):
    """
    Deactivate a virtual machine by first shutting down Linux and
    then log it off.

    Input:
       Request Handle with the following properties:
          function    - 'POWERVM'
          subfunction - 'SOFTOFF'
          userid      - userid of the virtual machine
          parms['maxQueries']     - Maximum number of queries to issue.
                                    Optional.
          parms['maxWait']        - Maximum time to wait in seconds.
                                    Optional,
                                    unless 'maxQueries' is specified.
          parms['poll']           - Polling interval in seconds. Optional,
                                    unless 'maxQueries' is specified.

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter powerVM.softDeactivate, userid: " + rh.userid)

    strCmd = "echo 'ping'"
    iucvResults = execCmdThruIUCV(rh, rh.userid, strCmd)

    if iucvResults['overallRC'] == 0:
        # We could talk to the machine, tell it to shutdown nicely.
        strCmd = "shutdown -h now"
        iucvResults = execCmdThruIUCV(rh, rh.userid, strCmd)
        if iucvResults['overallRC'] == 0:
            time.sleep(15)
        else:
            # Shutdown failed.  Let CP take down the system
            # after we log the results.
            rh.printSysLog("powerVM.softDeactivate " + rh.userid +
                           " is unreachable. Treating it as already shutdown.")
    else:
        # Could not ping the machine.  Treat it as a success
        # after we log the results.
        rh.printSysLog("powerVM.softDeactivate " + rh.userid +
                       " is unreachable. Treating it as already shutdown.")

    # Tell z/VM to log off the system.
    parms = ["-T", rh.userid]
    smcliResults = invokeSMCLI(rh, "Image_Deactivate", parms)
    if smcliResults['overallRC'] == 0:
        pass
    elif (smcliResults['overallRC'] == 8 and smcliResults['rc'] == 200
          and (smcliResults['rs'] == 12 or +smcliResults['rs'] == 16)):
        # Tolerable error.
        # Machine is already logged off or is logging off.
        rh.printLn("N", rh.userid + " is already logged off.")
    else:
        # SMAPI API failed.
        rh.printLn("ES", smcliResults['response'])
        rh.updateResults(smcliResults)  # Use results from invokeSMCLI

    if rh.results['overallRC'] == 0 and 'maxQueries' in rh.parms:
        # Wait for the system to log off.
        waitResults = waitForVMState(rh,
                                     rh.userid,
                                     'off',
                                     maxQueries=rh.parms['maxQueries'],
                                     sleepSecs=rh.parms['poll'])
        if waitResults['overallRC'] == 0:
            rh.printLn(
                "N", "Userid '" + rh.userid + " is in the desired state: off")
        else:
            rh.updateResults(waitResults)

    rh.printSysLog("Exit powerVM.softDeactivate, rc: " +
                   str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #29
0
ファイル: makeVM.py プロジェクト: 56KbModem/python-zvm-sdk
def createVM(rh):
    """
    Create a virtual machine in z/VM.

    Input:
       Request Handle with the following properties:
          function    - 'CMDVM'
          subfunction - 'CMD'
          userid      - userid of the virtual machine

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter makeVM.createVM")
    dirLines = []
    dirLines.append("USER " + rh.userid + " " + rh.parms['pw'] +
         " " + rh.parms['priMemSize'] + " " +
         rh.parms['maxMemSize'] + " " + rh.parms['privClasses'])

    if 'profName' in rh.parms:
        dirLines.append("INCLUDE " + rh.parms['profName'])

    # if it is direct-SCSI the order of
    # DirMaint statements is very important.
    # because of this an extra control flow tree is added.
    if 'isSCSI' in rh.parms:
        dirLines.append("LOADDEV PORT %s LUN %s BOOTPROG 0" % (rh.parms['loadportname'],
                                                                rh.parms['loadlun']))
        priMem = rh.parms['priMemSize'].upper()
        maxMem = rh.parms['maxMemSize'].upper()
        if 'setReservedMem' in rh.parms and (priMem != maxMem):
            reservedSize = getReservedMemSize(rh, priMem, maxMem)
            if rh.results['overallRC'] != 0:
                rh.printSysLog("Exit makeVM.createVM, rc: " +
                       str(rh.results['overallRC']))
                return rh.results['overallRC']
            if reservedSize != '0M':
                 dirLines.append("COMMAND DEF STOR RESERVED %s" % reservedSize)

        # IPL for DSCSI will be the FCP device.
        # This has been handled beforehand and given
        # with the IPL variable to makeVM.py
        dirLines.append("IPL %s" % rh.parms['ipl'])

        if 'maxCPU' in rh.parms:
            dirLines.append("MACHINE ESA %i" % rh.parms['maxCPU'])
        dirLines.append("CPU 00 BASE")

        if 'cpuCnt' in rh.parms:
            for i in range(1, rh.parms['cpuCnt']):
                dirLines.append("CPU %0.2X" % i)

        dirLines.append("DEDICATE 5C51 %s" % rh.parms['dedicate'])

    else:
        if 'maxCPU' in rh.parms:
            dirLines.append("MACHINE ESA %i" % rh.parms['maxCPU'])
        
        dirLines.append("CPU 00 BASE")
        if 'cpuCnt' in rh.parms:
            for i in range(1, rh.parms['cpuCnt']):
                dirLines.append("CPU %0.2X" % i)

        if 'ipl' in rh.parms:
            ipl_string = "IPL %s " % rh.parms['ipl']

            if 'iplParam' in rh.parms:
                ipl_string += ("PARM %s " % rh.parms['iplParam'])

            if 'iplLoadparam' in rh.parms:
                ipl_string += ("LOADPARM %s " % rh.parms['iplLoadparam'])

            dirLines.append(ipl_string)

        if 'byUsers' in rh.parms:
            for user in rh.parms['byUsers']:
                dirLines.append("LOGONBY " + user)

        priMem = rh.parms['priMemSize'].upper()
        maxMem = rh.parms['maxMemSize'].upper()
        if 'setReservedMem' in rh.parms and (priMem != maxMem):
            reservedSize = getReservedMemSize(rh, priMem, maxMem)
            if rh.results['overallRC'] != 0:
                rh.printSysLog("Exit makeVM.createVM, rc: " +
                       str(rh.results['overallRC']))
                return rh.results['overallRC']
            if reservedSize != '0M':
                dirLines.append("COMMAND DEF STOR RESERVED %s" % reservedSize)

        if 'loadportname' in rh.parms:
            wwpn = rh.parms['loadportname'].replace("0x", "")
            dirLines.append("LOADDEV PORTname %s" % wwpn)

        if 'loadlun' in rh.parms:
            lun = rh.parms['loadlun'].replace("0x", "")
            dirLines.append("LOADDEV LUN %s" % lun)

        if 'dedicate' in rh.parms and not 'isSCSI' in rh.parms:
            vdevs = rh.parms['dedicate'].split()
            # add a DEDICATE statement for each vdev
            for vdev in vdevs:
                dirLines.append("DEDICATE %s %s" % (vdev, vdev))

    # Construct the temporary file for the USER entry.
    fd, tempFile = mkstemp()
    to_write = '\n'.join(dirLines) + '\n'
    os.write(fd, to_write.encode())
    os.close(fd)
    # --DUCK--
#    print("[DEBUG]: dedicate: {}".format(rh.parms['dedicate']))
#    print("[DEBUG]: typeof dedicate: {}".format(type(rh.parms['dedicate'])))
#    print("[DEBUG]: dirLines: ")
    for line in dirLines:
        print(line)

    parms = ["-T", rh.userid, "-f", tempFile]
    results = invokeSMCLI(rh, "Image_Create_DM", parms)
    if results['overallRC'] != 0:
        # SMAPI API failed.
        rh.printLn("ES", results['response'])
        rh.updateResults(results)    # Use results from invokeSMCLI

    os.remove(tempFile)

    rh.printSysLog("Exit makeVM.createVM, rc: " +
        str(rh.results['overallRC']))
    return rh.results['overallRC']
コード例 #30
0
def reset(rh):
    """
    Reset a virtual machine.

    Input:
       Request Handle with the following properties:
          function    - 'POWERVM'
          subfunction - 'RESET'
          userid      - userid of the virtual machine
          parms['maxQueries']     - Maximum number of queries to issue.
                                    Optional.
          parms['maxWait']        - Maximum time to wait in seconds. Optional,
                                    unless 'maxQueries' is specified.
          parms['poll']           - Polling interval in seconds. Optional,
                                    unless 'maxQueries' is specified.

    Output:
       Request Handle updated with the results.
       Return code - 0: ok, non-zero: error
    """

    rh.printSysLog("Enter powerVM.reset, userid: " + rh.userid)

    # Log off the user
    parms = ["-T", rh.userid]
    results = invokeSMCLI(rh, "Image_Deactivate", parms)
    if results['overallRC'] != 0:
        if results['rc'] == 200 and results['rs'] == 12:
            # Tolerated error.  Machine is already in the desired state.
            results['overallRC'] = 0
            results['rc'] = 0
            results['rs'] = 0
        else:
            # SMAPI API failed.
            rh.printLn("ES", results['response'])
            rh.updateResults(results)  # Use results from invokeSMCLI

    # Wait for the logoff to complete
    if results['overallRC'] == 0:
        results = waitForVMState(rh,
                                 rh.userid,
                                 "off",
                                 maxQueries=30,
                                 sleepSecs=10)

    # Log the user back on
    if results['overallRC'] == 0:
        parms = ["-T", rh.userid]
        results = invokeSMCLI(rh, "Image_Activate", parms)
        if results['overallRC'] != 0:
            # SMAPI API failed.
            rh.printLn("ES", results['response'])
            rh.updateResults(results)  # Use results from invokeSMCLI

    if results['overallRC'] == 0 and 'maxQueries' in rh.parms:
        if rh.parms['desiredState'] == 'up':
            results = waitForOSState(rh,
                                     rh.userid,
                                     rh.parms['desiredState'],
                                     maxQueries=rh.parms['maxQueries'],
                                     sleepSecs=rh.parms['poll'])
        else:
            results = waitForVMState(rh,
                                     rh.userid,
                                     rh.parms['desiredState'],
                                     maxQueries=rh.parms['maxQueries'],
                                     sleepSecs=rh.parms['poll'])
        if results['overallRC'] == 0:
            rh.printLn("N", rh.userid + ": " + rh.parms['desiredState'])
        else:
            rh.updateResults(results)

    rh.printSysLog("Exit powerVM.reset, rc: " + str(rh.results['overallRC']))
    return rh.results['overallRC']