Esempio n. 1
0
 def scp_remote_to_local(self, appliance_ip, ssh_username, ssh_password,
                         remote_path, local_path):
     s = remote_actions(host=appliance_ip,
                        username=ssh_username,
                        password=ssh_password)
     rc = s.download_file_to_local(remote_path, local_path)
     return rc
Esempio n. 2
0
    def _reset_blade(self, encl):
        timeout = 180  # timeout for each bay's RESET operation to complete
        cli = remote_actions(host=encl.oa1hostname,
                             username=encl.oa1username,
                             password=encl.oa1password)

        for bay in encl.bays.bay:
            out = cli.call_cmd("RESET SERVER %s" % bay.number, realout=True)
            output = out.stdout

            if out.code != 0:
                logger.warn(
                    "Failed to run 'RESET SERVER %s' on enclosure '%s' bay '%s'"
                    % (bay.number, encl.name, bay.number))
                return False
            else:
                if re.search(
                        r'Successfully reset the E-Fuse for device bay %s' %
                        bay.number, output):
                    logger.info(
                        "'RESET SERVER %s' successfully performed on enclosure '%s' bay '%s'"
                        % (bay.number, encl.name, bay.number),
                        also_console=True)
                    # wait 10 seconds for the response of SHOW SERVER INFO <bay number> to really show the blade is being reset, otherwise the response still the old
                    # this is kind of like the response has been delayed for some seconds, which is not acting as the same as SSH to the OA and run the command via PUTTY
                    BuiltIn().sleep(10)
                    start = datetime.now()
                    while (datetime.now() - start).total_seconds() < timeout:
                        out = cli.call_cmd("SHOW SERVER INFO %s" % bay.number,
                                           realout=True)
                        output = out.stdout
                        if out.code != 0:
                            logger.warn(
                                "Failed to run 'SHOW SERVER INFO %s' on enclosure '%s' bay '%s'"
                                % (bay.number, encl.name, bay.number))
                            return False
                        else:
                            if re.search(r'Product Name: %s' % bay.ProductName,
                                         output):
                                logger.info(
                                    "'SHOW SERVER INFO %s' successfully got 'Product Name: %s' on enclosure '%s' bay '%s'"
                                    % (bay.number, bay.ProductName, encl.name,
                                       bay.number),
                                    also_console=True)
                                break
                            else:
                                logger.info(
                                    "'Product Name: %s' not found from command 'SHOW SERVER INFO %s' on enclosure '%s' bay '%s', retrying ..."
                                    % (bay.ProductName, bay.number, encl.name,
                                       bay.number),
                                    also_console=True)
                                BuiltIn().sleep(0.5)
                                continue
                else:
                    logger.warn(
                        "Failed to perform 'RESET SERVER %s' on enclosure '%s' bay '%s'!"
                        % (bay.number, encl.name, bay.number))
                    return False

        return True
Esempio n. 3
0
 def remove_all_sso_certificates_blade(self, ilo):
     timeout = 20
     if isinstance(ilo, dict):
         ilo = self.obj_dic(ilo)
     cli = remote_actions(host=ilo.ilo,
                          username=ilo.username,
                          password=ilo.password)
     cli.call_cmd("cd /map1/oemhp_ssocfg1")
     out = cli.call_cmd("show")
     output = out.stdout
     index = re.findall("Record\[\d+\]", output)
     count = 0
     print len(index)
     for i in range(len(index)):
         ind = re.findall("\[\d+\]", index[i])
         ind1 = str(ind)
         ind2 = ind1.strip('[\'[]\']')
         out2 = cli.call_cmd("delete %s" % ind2)
         output2 = out2.stdout
         if re.findall("SSO Server record deleted", output2):
             logger.debug("Successfully deleted the certificate")
             BuiltIn().sleep(timeout)
             count += 1
     if count == len(index):
         return True
     else:
         return False
Esempio n. 4
0
 def scp_local_to_remote(self, appliance_ip, ssh_username, ssh_password,
                         local_path, remote_path):
     s = remote_actions(host=appliance_ip,
                        username=ssh_username,
                        password=ssh_password)
     rc = s.upload_file_from_local(local_path, remote_path)
     return rc
Esempio n. 5
0
 def run_ssh_cmd(self, appliance_ip, ssh_username, ssh_password, command):
     cli = remote_actions(host=appliance_ip,
                          username=ssh_username,
                          password=ssh_password)
     logger.debug("start running commands on SSH server")
     logger.debug(command)
     rc = cli.call_cmd(command, realout=False)
     cli.close_ssh()
     return rc.stdout.strip('\n')
Esempio n. 6
0
    def power_off_all_blades(self, encl, timeout=100):
        if isinstance(encl, dict):
            encl = self.obj_dic(encl)
        cli = remote_actions(host=encl.hostname,
                             username=encl.username,
                             password=encl.password)
        pow = cli.call_cmd("poweroff server all")
        BuiltIn().sleep(timeout)

        return pow
Esempio n. 7
0
    def clear_oa_vc_mode(self, encl, timeout=60):
        if isinstance(encl, dict):
            encl = self.obj_dic(encl)
        logger.debug("Clear OA [%s] VC Mode" % encl.hostname)

        cli = remote_actions(host=encl.hostname,
                             username=encl.username,
                             password=encl.password)
        vc = cli.call_cmd("CLEAR VCMODE")
        BuiltIn().sleep(timeout)

        return vc
Esempio n. 8
0
    def restart_oa(self, encl, timeout=180):
        if isinstance(encl, dict):
            encl = self.obj_dic(encl)
        logger.debug("Restart OA [%s] VC Mode" % encl.hostname)

        cli = remote_actions(host=encl.hostname,
                             username=encl.username,
                             password=encl.password)
        res = cli.call_cmd("restart oa")
        BuiltIn().sleep(timeout)

        return res
Esempio n. 9
0
    def update_oa_firmware(self, encl, timeout=300):
        if isinstance(encl, dict):
            encl = self.obj_dic(encl)
        logger.debug("Update OA [%s] firmware to [%s]" %
                     (encl.hostname, encl.firmware_url))

        cli = remote_actions(host=encl.hostname,
                             username=encl.username,
                             password=encl.password)
        is_force = getattr(encl, "force_update", "").upper()
        cli.call_cmd("UPDATE IMAGE " + is_force + " " + encl.firmware_url)
        BuiltIn().sleep(timeout)

        return True
Esempio n. 10
0
 def run_multi_cmd(self,
                   appliance_ip,
                   ssh_username,
                   ssh_password,
                   commands,
                   interval='2s'):
     cli = remote_actions(host=appliance_ip,
                          username=ssh_username,
                          password=ssh_password)
     logger.debug("start running commands on appliance")
     out = ""
     for command in commands:
         logger.debug(command)
         rc = cli.call_cmd(command, realout=False)
         logger.debug(rc.stdout)
         out = out + rc.stdout + "\n"
         BuiltIn().sleep(interval)
     cli.close_ssh()
     return out
Esempio n. 11
0
    def _add_firmware_bundle(self, appliance_ip, ssh_username, ssh_password,
                             spp_file_name, spp_file_path, spp_file_md5, auth,
                             firmware_bundle_uri):

        cli = remote_actions(host=appliance_ip,
                             username=ssh_username,
                             password=ssh_password)

        # check if Firmware Bundle already added into OneView
        cmd = 'curl -m 1200 -k -X GET -H "Accept: application/json" -H "auth:%s" -H "X-API-Version:300" https://%s%s' % (
            auth, appliance_ip, firmware_bundle_uri)
        logger.debug(
            "send API GET request to check if Firmware Bundle <%s - %s> already exists in OneView's Firmware Bundle repo"
            % (spp_file_name, firmware_bundle_uri))
        rc = cli.call_cmd(cmd, realout=False)
        output = rc.stdout

        if json.loads(output).get('uri') == firmware_bundle_uri:
            logger.warn(
                "Firmware Bundle <%s - %s> already exists in OneView's Firmware Bundle repo, "
                "skipped to post SPP iso file to OneView FB repo" %
                (spp_file_name, firmware_bundle_uri))
            cli.close_ssh()
            return True

        require_download = True
        # check if SPP iso file already exists in Appliance's local directory (not OneView FWB repo)
        cmd = 'ls | grep %s' % spp_file_name
        logger.debug(
            "check if SPP file <%s> already exists in appliance's local directory"
            % spp_file_name)
        rc = cli.call_cmd(cmd, realout=True)
        output = rc.stdout
        if rc.code != 0:
            logger.debug(
                "SPP file <%s> does not exist in appliance's local directory" %
                spp_file_name)
        else:
            if re.search(spp_file_name, output):
                logger.debug(
                    "SPP file <%s> already exists in appliance's local directory, will check md5 first"
                    % spp_file_name)
                cmd = 'md5sum %s' % spp_file_name
                rc = cli.call_cmd(cmd, realout=True)
                output = rc.stdout
                if rc.code != 0:
                    logger.warn("failed to run command <%s>" % cmd)
                    cli.close_ssh()
                    return False
                else:
                    if re.search(spp_file_md5, output):
                        logger.debug(
                            "existed SPP iso file <%s> validate successfully" %
                            spp_file_name)
                        require_download = False
                    else:
                        logger.debug(
                            "SPP iso file <%s> validate failed, need remove and download again"
                            % spp_file_name)
                        cmd = 'rm -rf %s' % spp_file_name
                        logger.debug("try to delete SPP file <%s>" %
                                     spp_file_name)
                        rc = cli.call_cmd(cmd, realout=True)
                        if rc.code != 0:
                            logger.warn(
                                "failed to delete SPP file <%s> via command <%s>, please check or delete it manually"
                                % (spp_file_name, cmd))
                            cli.close_ssh()
                            return False
            else:
                logger.warn(
                    "failed to search <{0}> from the output <{1}> of listing existing iso files via  <ls | grep {0}>"
                    .format(spp_file_name, output))
                cli.close_ssh()
                return False

        # download SPP iso file from shared location
        if require_download is True:
            cmd = 'wget  -q %s%s' % (spp_file_path, spp_file_name)
            logger.debug("start downloading SPP iso file <%s> ......" %
                         spp_file_name)
            rc = cli.call_cmd(cmd, realout=True)
            if rc.code != 0:
                logger.warn(
                    "failed to run download SPP iso file command <%s>" % cmd)
                cli.close_ssh()
                return False
            else:
                cmd = 'md5sum %s' % spp_file_name
                rc = cli.call_cmd(cmd, realout=True)
                output = rc.stdout
                if rc.code != 0:
                    logger.warn("failed to run command <%s>" % cmd)
                    cli.close_ssh()
                    return False
                else:
                    if re.search(spp_file_md5, output):
                        logger.debug(
                            "successfully downloaded SPP iso file <%s>" %
                            spp_file_name)
                    else:
                        logger.warn(
                            "failed to download SPP iso file <%s>, md5 should be <%s>, but <%s>"
                            % (spp_file_name, spp_file_md5, output))
                        return False

        # send API POST request to pose SPP iso file into OneView FWB repo
        cmd = 'curl -m 1200 -k -X POST -H "Accept: application/json" -H "auth:%s" -H "uploadfilename:%s" -H "X-API-Version:300" -F file="@/root/%s" https://%s/rest/firmware-bundles' % (
            auth, spp_file_name, spp_file_name, appliance_ip)
        logger.debug(
            "send API POST request to post SPP iso file <%s> to OneView's Firmware Bundle repo ......"
            % spp_file_name)
        rc = cli.call_cmd(cmd, realout=True)
        output = rc.stdout
        if rc.code != 0:
            logger.warn("failed to run cmd <%s>" % cmd)
            cli.close_ssh()
            return False
        else:
            task_uri = json.loads(output).get('uri')
            if task_uri is None:
                message = json.loads(output).get('message')
                logger.warn("an error occur when upload SPP: <%s>" % message)
                return False
            cmd = 'curl -m 1200 -k -X GET -H "Accept: application/json" -H "auth:%s" -H "X-API-Version:300" https://%s%s' % (
                auth, appliance_ip, task_uri)
            timeout = 300
            start = datetime.now()
            while (datetime.now() - start).total_seconds() < timeout:
                stdin, stdout, stderr = cli.ssh.exec_command(cmd)
                output = stdout.readlines()[0]
                if json.loads(output).get('errorCode') is not None:
                    msg = ' ' * 7 + '\n{}'.format(' ' * 8).join(
                        output.split(','))
                    logger.warn(
                        "failed to run cmd <%s>, error message is: \n%s" %
                        (cmd, msg))
                    cli.close_ssh()
                    return False
                else:
                    resp = json.loads(output)
                    if resp.get('taskState') == 'Completed' and resp.get(
                            'uri') == task_uri:
                        logger.debug(
                            "successfully posted Firmware Bundle <%s - %s> to OneView"
                            % (spp_file_name, firmware_bundle_uri))
                        cli.close_ssh()
                        return True
                    elif resp.get('taskState') == 'Error' and resp.get(
                            'uri') == task_uri:
                        task_state = resp.get('taskState')
                        logger.warn(
                            "task of add Firmware Bundle <%s - %s> failed with (taskState: <%s>), return false ... "
                            % (spp_file_name, firmware_bundle_uri, task_state))
                        cli.close_ssh()
                        return False
                    else:
                        task_state = resp.get('taskState')
                        logger.debug(
                            "task of add Firmware Bundle <%s - %s> still running (taskState: <%s>), keep waiting ... "
                            % (spp_file_name, firmware_bundle_uri, task_state))
                        BuiltIn().sleep(15)
                        continue
            else:
                logger.warn(
                    "failed to wait for the task of adding Firmware Bundle <%s - %s> "
                    "completed within <%s> seconds" %
                    (spp_file_name, firmware_bundle_uri, timeout))
                cli.close_ssh()
                return False
Esempio n. 12
0
 def check_ssh_to_console(self, ip, username, password):
     cli = remote_actions(host=ip, username=username, password=password)
     login = True if cli.connected else False
     cli.close_ssh()
     return login