Ejemplo n.º 1
0
    def _checkPromises(self, computer_partition):
        self.logger.info("Checking promises...")
        instance_path = os.path.join(self.instance_root, computer_partition.getId())

        uid, gid = None, None
        stat_info = os.stat(instance_path)

        # stat sys call to get statistics informations
        uid = stat_info.st_uid
        gid = stat_info.st_gid

        promise_present = False
        # Get the list of promises
        promise_dir = os.path.join(instance_path, "etc", "promise")
        if os.path.exists(promise_dir) and os.path.isdir(promise_dir):
            cwd = instance_path
            promises_list = os.listdir(promise_dir)

            # Check whether every promise is kept
            for promise in promises_list:
                promise_present = True

                command = [os.path.join(promise_dir, promise)]

                promise = os.path.basename(command[0])
                self.logger.info("Checking promise %r.", promise)

                kw = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE)

                process_handler = SlapPopen(command, preexec_fn=lambda: dropPrivileges(uid, gid), cwd=cwd, env={}, **kw)

                time.sleep(self.promise_timeout)

                if process_handler.poll() is None:
                    process_handler.terminate()
                    raise Slapgrid.PromiseError("The promise %r timed out" % promise)
                elif process_handler.poll() != 0:
                    stderr = process_handler.communicate()[1]
                    if stderr is None:
                        stderr = "No error output from %r." % promise
                    else:
                        stderr = "Promise %r:" % promise + stderr
                    raise Slapgrid.PromiseError(stderr)

        if not promise_present:
            self.logger.info("No promise.")
Ejemplo n.º 2
0
  def processComputerPartitionList(self):
    """Will start supervisord and process each Computer Partition.
    """
    logger = logging.getLogger('ComputerPartitionProcessing')
    logger.info("Processing computer partitions...")
    # Prepares environment
    self.checkEnvironmentAndCreateStructure()
    self._launchSupervisord()
    # Process Computer Partitions
    clean_run = True
    for computer_partition in self.getComputerPartitionList():
      computer_partition_id = computer_partition.getId()
      try:
        software_url = computer_partition.getSoftwareRelease().getURI()
      except NotFoundError:
        software_url = None
      software_path = os.path.join(self.software_root,
            getSoftwareUrlHash(software_url))
      local_partition = Partition(
        software_path=software_path,
        instance_path=os.path.join(self.instance_root,
            computer_partition.getId()),
        supervisord_partition_configuration_path=os.path.join(
          self.supervisord_configuration_directory, '%s.conf' %
          computer_partition_id),
        supervisord_socket=self.supervisord_socket,
        computer_partition=computer_partition,
        computer_id=self.computer_id,
        partition_id=computer_partition_id,
        server_url=self.master_url,
        software_release_url=software_url,
        certificate_repository_path=self.certificate_repository_path,
        console=self.console, buildout=self.buildout)
      # There are no conditions to try to instanciate partition
      try:
        computer_partition_state = computer_partition.getState()
        if computer_partition_state == "started":
          local_partition.install()
          computer_partition.available()
          local_partition.start()
          computer_partition.started()
        elif computer_partition_state == "stopped":
          local_partition.install()
          computer_partition.available()
          local_partition.stop()
          computer_partition.stopped()
        elif computer_partition_state == "destroyed":
          # Stop, but safely
          try:
            local_partition.stop()
            try:
              computer_partition.stopped()
            except (SystemExit, KeyboardInterrupt):
              exception = traceback.format_exc()
              computer_partition.error(exception)
              raise
            except Exception:
              pass
          except (SystemExit, KeyboardInterrupt):
            exception = traceback.format_exc()
            computer_partition.error(exception)
            raise
          except Exception:
            clean_run = False
            exception = traceback.format_exc()
            logger.error(exception)
            computer_partition.error(exception)
        else:
          error_string = "Computer Partition %r has unsupported state: %s" % \
            (computer_partition_id, computer_partition_state)
          computer_partition.error(error_string)
          raise NotImplementedError(error_string)
      except (SystemExit, KeyboardInterrupt):
        exception = traceback.format_exc()
        computer_partition.error(exception)
        raise
      except Exception:
        clean_run = False
        exception = traceback.format_exc()
        logger.error(exception)
        computer_partition.error(exception)

      # Promises

      instance_path = os.path.join(self.instance_root,
          computer_partition.getId())

      uid, gid = None, None
      stat_info = os.stat(instance_path)

      #stat sys call to get statistics informations
      uid = stat_info.st_uid
      gid = stat_info.st_gid

      # Get the list of promises
      promise_dir = os.path.join(instance_path, 'etc', 'promise')
      if os.path.exists(promise_dir) and os.path.isdir(promise_dir):
        cwd = instance_path
        promises_list = os.listdir(promise_dir)

        # Check whether every promise is kept
        for promise in promises_list:

          command = os.path.join(promise_dir, promise)

          kw = dict()
          if not self.console:
            kw.update(stdout=subprocess.PIPE, stderr=subprocess.PIPE)

          process_handler = SlapPopen(command,
            preexec_fn=lambda: dropPrivileges(uid, gid),
            cwd=cwd,
            env=None, **kw)

          time.sleep(self.promise_timeout)

          promise = os.path.basename(command)

          if process_handler.poll() is None:
            process_handler.kill()
            computer_partition.error("The promise %r timed out" % promise)
            clean_run = False
          elif process_handler.poll() != 0:
            stderr = process_handler.communicate()[1]
            if stderr is None:
              stderr = 'No error output from %r.' % promise
            computer_partition.error(stderr)
            clean_run = False


    logger.info("Finished computer partitions...")
    return clean_run