Ejemplo n.º 1
0
  def execute(self, argv):
    """
    This executes the service given the command line arguments.
    The first two argument are 'ent_home' and 'task' than is it's the
    children job to parse the extra args if it wants by overriding parse_args
    """

    # Args parsing
    if len(argv) < 3:
      sys.exit(self.usage())

    # Get the first two arguments and intitialize
    self.init_service(argv[1])
    self.task = string.strip(argv[2])

    # Get the other arguments and call the parsing function
    flags_argv = [argv[0]]
    flags_argv.extend(argv[3:])
    self.parse_args(flags_argv)

    # Extra checks
    if not self.service_to_be_up():
      sys.exit('%s not active' % self.service_name)

    # check if the node is enabled
    testver = install_utilities.is_test(self.version)
    if core_utils.AmIDisabled(self.version, testver):
      logging.error('I am disabled.')
      sys.exit(-1)

    if (self.performs_only_on_master and
        self.task not in ("activate", "deactivate")):
      if not self.local_machine_is_master:
        logging.error('I am not the master')
        self.nop()
        sys.exit(0)


    if not self.task:
      sys.exit(self.usage())

    # Execute the operations behind a lock
    lockfile = "%s/%s_service_lock_%s" % (self.tmpdir,
                                          self.service_name,
                                          self.version)
    pidfile = "%s/%s_service_pid_%s" % (self.tmpdir,
                                          self.service_name,
                                          self.version)

    # Execute the task: unlocked on activate/deactivate /
    # locked else
    if self.task in ["activate", "deactivate"]:
      do_task(self, self.task)
    else:
      if self.check_previous_cron_job:
        # kill previous cron job if lockfile timestamp is too old
        valid_lock_duration = self.secs_to_kill_previous_job
        # for "stop" and "restart" task, do it immediately
        if self.task in ["stop", "restart"]:
          valid_lock_duration = 0
        E.exec_locked(lockfile, 1, do_task, (self, self.task,), {},
                      valid_lock_duration, pidfile)
      else:
        # Lock will time out after 60 rounds of 10 seconds
        E.exec_locked(lockfile, 60, do_task, (self, self.task,))