Exemple #1
0
 def wait_for_ssh_host_key(self, max_retry=1800, sleep_time=1):
     """
     Wait for cloud-init to generate ssh host key
     """
     keypair_type = conf.get_ssh_host_keypair_type()  # pylint: disable=W0612
     path = conf.get_ssh_key_public_path()
     logging_interval = 10
     max_logging_interval = 320
     for retry in range(0, max_retry):
         if os.path.isfile(path):
             logger.info("ssh host key found at: {0}".format(path))
             try:
                 thumbprint = self.get_ssh_host_key_thumbprint(
                     chk_err=False)
                 logger.info("Thumbprint obtained from : {0}".format(path))
                 return thumbprint
             except ProvisionError:
                 logger.warn(
                     "Could not get thumbprint from {0}".format(path))
         if retry < max_retry - 1:
             if retry % logging_interval == 0:
                 logger.info("Waiting for ssh host key be generated at {0} "
                             "[{1} attempts remaining, "
                             "sleeping {2}s between retries]".format(
                                 path, max_retry - retry, sleep_time))
                 if not cloud_init_is_enabled():
                     logger.warn("cloud-init does not appear to be running")
                 logging_interval = min(logging_interval * 2,
                                        max_logging_interval)
             time.sleep(sleep_time)
     raise ProvisionError("Giving up, ssh host key was not found at {0} "
                          "after {1}s".format(path, max_retry * sleep_time))
Exemple #2
0
 def wait_for_ovfenv(self, max_retry=1800, sleep_time=1):
     """
     Wait for cloud-init to copy ovf-env.xml file from provision ISO
     """
     ovf_file_path = os.path.join(conf.get_lib_dir(), OVF_FILE_NAME)
     logging_interval = 10
     max_logging_interval = 320
     for retry in range(0, max_retry):
         if os.path.isfile(ovf_file_path):
             try:
                 ovf_env = OvfEnv(fileutil.read_file(ovf_file_path))
                 self.handle_provision_guest_agent(
                     ovf_env.provision_guest_agent)
                 return
             except ProtocolError as pe:
                 raise ProvisionError("OVF xml could not be parsed "
                                      "[{0}]: {1}".format(
                                          ovf_file_path, ustr(pe)))
         else:
             if retry < max_retry - 1:
                 if retry % logging_interval == 0:
                     logger.info(
                         "Waiting for cloud-init to copy ovf-env.xml to {0} "
                         "[{1} retries remaining, "
                         "sleeping {2}s between retries]".format(
                             ovf_file_path, max_retry - retry, sleep_time))
                     if not cloud_init_is_enabled():
                         logger.warn(
                             "cloud-init does not appear to be enabled")
                     logging_interval = min(logging_interval * 2,
                                            max_logging_interval)
                 time.sleep(sleep_time)
     raise ProvisionError("Giving up, ovf-env.xml was not copied to {0} "
                          "after {1}s".format(ovf_file_path,
                                              max_retry * sleep_time))
Exemple #3
0
    def run(self):
        if not conf.get_provision_enabled():
            logger.info("Provisioning is disabled, skipping.")
            self.write_provisioned()
            self.report_ready()
            return

        try:
            utc_start = datetime.utcnow()
            thumbprint = None  # pylint: disable=W0612

            if self.check_provisioned_file():
                logger.info("Provisioning already completed, skipping.")
                return

            logger.info("Running default provisioning handler")

            if cloud_init_is_enabled():
                raise ProvisionError(
                    "cloud-init appears to be installed and enabled, "
                    "this is not expected, cannot continue")

            logger.info("Copying ovf-env.xml")
            ovf_env = self.protocol_util.copy_ovf_env()

            self.protocol_util.get_protocol()  # Trigger protocol detection
            self.report_not_ready("Provisioning", "Starting")
            logger.info("Starting provisioning")

            self.provision(ovf_env)

            thumbprint = self.reg_ssh_host_key()
            self.osutil.restart_ssh_service()

            self.write_provisioned()

            self.report_event("Provisioning succeeded ({0}s)".format(
                self._get_uptime_seconds()),
                              is_success=True,
                              duration=elapsed_milliseconds(utc_start))

            self.handle_provision_guest_agent(ovf_env.provision_guest_agent)

            self.report_ready()
            logger.info("Provisioning complete")

        except (ProtocolError, ProvisionError) as e:
            msg = "Provisioning failed: {0} ({1}s)".format(
                ustr(e), self._get_uptime_seconds())
            logger.error(msg)
            self.report_not_ready("ProvisioningFailed", ustr(e))
            self.report_event(msg, is_success=False)
            return