Ejemplo n.º 1
0
def perform_prestart_checks(expected_hostname):
  # Check if current hostname is equal to expected one (got from the server
  # during bootstrap.
  global config

  if expected_hostname is not None:
    current_hostname = hostname.hostname(config)
    if current_hostname != expected_hostname:
      print("Determined hostname does not match expected. Please check agent "
            "log for details")
      msg = "Ambari agent machine hostname ({0}) does not match expected ambari " \
            "server hostname ({1}). Aborting registration. Please check hostname, " \
            "hostname -f and /etc/hosts file to confirm your " \
            "hostname is setup correctly".format(current_hostname, expected_hostname)
      logger.error(msg)
      sys.exit(1)
  # Check if there is another instance running
  if os.path.isfile(ProcessHelper.pidfile) and not OSCheck.get_os_family() == OSConst.WINSRV_FAMILY:
    print("%s already exists, exiting" % ProcessHelper.pidfile)
    sys.exit(1)
  # check if ambari prefix exists
  elif config.has_option('agent', 'prefix') and not os.path.isdir(os.path.abspath(config.get('agent', 'prefix'))):
    msg = "Ambari prefix dir %s does not exists, can't continue" \
          % config.get("agent", "prefix")
    logger.error(msg)
    print(msg)
    sys.exit(1)
  elif not config.has_option('agent', 'prefix'):
    msg = "Ambari prefix dir %s not configured, can't continue"
    logger.error(msg)
    print(msg)
    sys.exit(1)
Ejemplo n.º 2
0
    def checkLiveServices(self, services, result):
        osType = OSCheck.get_os_family()
        for service in services:
            svcCheckResult = {}
            if isinstance(service, dict):
                serviceName = service[osType]
            else:
                serviceName = service

            service_check_live = shlex.split(self.SERVICE_STATUS_CMD)
            service_check_live[1] = serviceName

            svcCheckResult['name'] = serviceName
            svcCheckResult['status'] = "UNKNOWN"
            svcCheckResult['desc'] = ""
            try:
                osStat = subprocess.Popen(service_check_live,
                                          stdout=subprocess.PIPE,
                                          stderr=subprocess.PIPE)
                out, err = osStat.communicate()
                if 0 != osStat.returncode:
                    svcCheckResult['status'] = "Unhealthy"
                    svcCheckResult['desc'] = out
                    if len(out) == 0:
                        svcCheckResult['desc'] = err
                else:
                    svcCheckResult['status'] = "Healthy"
            except Exception, e:
                svcCheckResult['status'] = "Unhealthy"
                svcCheckResult['desc'] = repr(e)
            result.append(svcCheckResult)
Ejemplo n.º 3
0
  def checkLiveServices(self, services, result):
    osType = OSCheck.get_os_family()
    for service in services:
      svcCheckResult = {}
      if isinstance(service, dict):
        serviceName = service[osType]
      else:
        serviceName = service

      service_check_live = shlex.split(self.SERVICE_STATUS_CMD)
      service_check_live[1] = serviceName

      svcCheckResult['name'] = serviceName
      svcCheckResult['status'] = "UNKNOWN"
      svcCheckResult['desc'] = ""
      try:
        osStat = subprocess.Popen(service_check_live, stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE)
        out, err = osStat.communicate()
        if 0 != osStat.returncode:
          svcCheckResult['status'] = "Unhealthy"
          svcCheckResult['desc'] = out
          if len(out) == 0:
            svcCheckResult['desc'] = err
        else:
          svcCheckResult['status'] = "Healthy"
      except Exception, e:
        svcCheckResult['status'] = "Unhealthy"
        svcCheckResult['desc'] = repr(e)
      result.append(svcCheckResult)
Ejemplo n.º 4
0
def perform_prestart_checks(expected_hostname):
  # Check if current hostname is equal to expected one (got from the server
  # during bootstrap.
  global config

  if expected_hostname is not None:
    current_hostname = hostname.hostname(config)
    if current_hostname != expected_hostname:
      print("Determined hostname does not match expected. Please check agent "
            "log for details")
      msg = "Ambari agent machine hostname ({0}) does not match expected ambari " \
            "server hostname ({1}). Aborting registration. Please check hostname, " \
            "hostname -f and /etc/hosts file to confirm your " \
            "hostname is setup correctly".format(current_hostname, expected_hostname)
      logger.error(msg)
      sys.exit(1)
  # Check if there is another instance running
  if os.path.isfile(ProcessHelper.pidfile) and not OSCheck.get_os_family() == OSConst.WINSRV_FAMILY:
    print("%s already exists, exiting" % ProcessHelper.pidfile)
    sys.exit(1)
  # check if ambari prefix exists
  elif config.has_option('agent', 'prefix') and not os.path.isdir(os.path.abspath(config.get('agent', 'prefix'))):
    msg = "Ambari prefix dir %s does not exists, can't continue" \
          % config.get("agent", "prefix")
    logger.error(msg)
    print(msg)
    sys.exit(1)
  elif not config.has_option('agent', 'prefix'):
    msg = "Ambari prefix dir %s not configured, can't continue"
    logger.error(msg)
    print(msg)
    sys.exit(1)
Ejemplo n.º 5
0
    def find_repo_files_for_repos(self, repoNames):
        repoFiles = []
        osType = OSCheck.get_os_family()
        repoNameList = []
        for repoName in repoNames:
            if len(repoName.strip()) > 0:
                repoNameList.append("[" + repoName + "]")
                repoNameList.append("name=" + repoName)
        if repoNameList:
            # get list of files
            if OSCheck.is_suse_family():
                fileList = self.get_files_in_dir(REPO_PATH_SUSE)
            elif OSCheck.is_redhat_family():
                fileList = self.get_files_in_dir(REPO_PATH_RHEL)
            elif OSCheck.is_ubuntu_family():
                fileList = self.get_files_in_dir(REPO_PATH_UBUNTU)
            else:
                logger.warn(
                    "Unsupported OS type, cannot get repository location.")
                return []

            if fileList:
                for filePath in fileList:
                    with open(filePath, 'r') as file:
                        content = file.readline()
                        while (content != ""):
                            for repoName in repoNameList:
                                if content.find(
                                        repoName
                                ) == 0 and filePath not in repoFiles:
                                    repoFiles.append(filePath)
                                    break
                            content = file.readline()

        return repoFiles
Ejemplo n.º 6
0
  def find_repo_files_for_repos(self, repoNames):
    repoFiles = []
    osType = OSCheck.get_os_family()
    repoNameList = []
    for repoName in repoNames:
      if len(repoName.strip()) > 0:
        repoNameList.append("[" + repoName + "]")
        repoNameList.append("name=" + repoName)
    if repoNameList:
      # get list of files
      if OSCheck.is_suse_family():
        fileList = self.get_files_in_dir(REPO_PATH_SUSE)
      elif OSCheck.is_redhat_family():
        fileList = self.get_files_in_dir(REPO_PATH_RHEL)
      elif OSCheck.is_ubuntu_family():
        fileList = self.get_files_in_dir(REPO_PATH_UBUNTU)
      else:
        logger.warn("Unsupported OS type, cannot get repository location.")
        return []

      if fileList:
        for filePath in fileList:
          with open(filePath, 'r') as file:
            content = file.readline()
            while (content != "" ):
              for repoName in repoNameList:
                if content.find(repoName) == 0 and filePath not in repoFiles:
                  repoFiles.append(filePath)
                  break;
              content = file.readline()

    return repoFiles
Ejemplo n.º 7
0
 def os_family(self):
   """
   Return values:
   redhat, ubuntu, suse
   
   In case cannot detect raises exception
   """
   return OSCheck.get_os_family()
Ejemplo n.º 8
0
 def os_family(self):
     """
 Return values:
 redhat, ubuntu, suse
 
 In case cannot detect raises exception
 """
     return OSCheck.get_os_family()
Ejemplo n.º 9
0
        def thunk(*args, **kwargs):
            fn_id_base = func.__module__ + "." + func.__name__
            fn_id = fn_id_base + "." + OSCheck.get_os_family()
            if fn_id not in self._func_impls:
                fn_id = fn_id_base + "." + OsFamilyImpl.DEFAULT

            fn = self._func_impls[fn_id]
            return fn(*args, **kwargs)
Ejemplo n.º 10
0
    def thunk(*args, **kwargs):
      fn_id_base = func.__module__ + "." + func.__name__
      fn_id = fn_id_base + "." + OSCheck.get_os_family()
      if fn_id not in self._func_impls:
        fn_id = fn_id_base + "." + OsFamilyImpl.DEFAULT

      fn = self._func_impls[fn_id]
      return fn(*args, **kwargs)
Ejemplo n.º 11
0
def get_erase_cmd():
  if OSCheck.is_redhat_family():
    return "yum erase -y {0}"
  elif OSCheck.is_suse_family():
    return "zypper -n -q remove {0}"
  elif OSCheck.is_ubuntu_family():
    return "/usr/bin/apt-get -y -q remove {0}"
  else:
    raise Exception("Unsupported OS family '{0}', cannot remove package. ".format(OSCheck.get_os_family()))
Ejemplo n.º 12
0
def get_pg_hba_init_files():
  if OSCheck.is_ubuntu_family():
    return '/etc/postgresql'
  elif OSCheck.is_redhat_family():
    return '/etc/rc.d/init.d/postgresql'
  elif OSCheck.is_suse_family():
    return '/etc/init.d/postgresql'
  else:
    raise Exception("Unsupported OS family '{0}'".format(OSCheck.get_os_family()))
Ejemplo n.º 13
0
 def getRepoDir(self):
   if OSCheck.is_redhat_family():
     return "/etc/yum.repos.d"
   elif OSCheck.is_suse_family():
     return "/etc/zypp/repos.d"
   elif OSCheck.is_ubuntu_family():
     return "/etc/apt/sources.list.d"
   else:
     raise Exception("Unsupported OS family '{0}'".format(OSCheck.get_os_family()))
Ejemplo n.º 14
0
def get_erase_cmd():
  if OSCheck.is_redhat_family():
    return "yum erase -y {0}"
  elif OSCheck.is_suse_family():
    return "zypper -n -q remove {0}"
  elif OSCheck.is_ubuntu_family():
    return "/usr/bin/apt-get -y -q remove {0}"
  else:
    raise Exception("Unsupported OS family '{0}', cannot remove package. ".format(OSCheck.get_os_family()))
Ejemplo n.º 15
0
def get_pg_hba_init_files():
    if OSCheck.is_ubuntu_family():
        return '/etc/postgresql'
    elif OSCheck.is_redhat_family():
        return '/etc/rc.d/init.d/postgresql'
    elif OSCheck.is_suse_family():
        return '/etc/init.d/postgresql'
    else:
        raise Exception("Unsupported OS family '{0}'".format(
            OSCheck.get_os_family()))
Ejemplo n.º 16
0
    def test_main(self, cleanup_mock, ping_port_init_mock,
                  ping_port_start_mock, data_clean_init_mock,
                  data_clean_start_mock, parse_args_mock, join_mock,
                  start_mock, Controller_init_mock, try_to_connect_mock,
                  update_log_level_mock, daemonize_mock,
                  perform_prestart_checks_mock, ambari_config_mock, stop_mock,
                  bind_signal_handlers_mock, setup_logging_mock, socket_mock):
        data_clean_init_mock.return_value = None
        Controller_init_mock.return_value = None
        ping_port_init_mock.return_value = None
        options = MagicMock()
        parse_args_mock.return_value = (options, MagicMock)
        try_to_connect_mock.return_value = (0, True)
        # use default unix config
        ambari_config_mock.return_value = os.path.abspath(
            "../../../conf/unix/ambari-agent.ini")
        #testing call without command-line arguments

        main.main()

        self.assertTrue(setup_logging_mock.called)
        self.assertTrue(bind_signal_handlers_mock.called)
        if OSCheck.get_os_family() != OSConst.WINSRV_FAMILY:
            self.assertTrue(stop_mock.called)
        #self.assertTrue(resolve_ambari_config_mock.called)
        self.assertTrue(perform_prestart_checks_mock.called)
        if OSCheck.get_os_family() != OSConst.WINSRV_FAMILY:
            self.assertTrue(daemonize_mock.called)
        self.assertTrue(update_log_level_mock.called)
        try_to_connect_mock.assert_called_once_with(ANY, -1, ANY)
        self.assertTrue(start_mock.called)
        self.assertTrue(data_clean_init_mock.called)
        self.assertTrue(data_clean_start_mock.called)
        self.assertTrue(ping_port_init_mock.called)
        self.assertTrue(ping_port_start_mock.called)
        self.assertTrue(cleanup_mock.called)
        perform_prestart_checks_mock.reset_mock()

        # Testing call with --expected-hostname parameter
        options.expected_hostname = "test.hst"
        main.main()
        perform_prestart_checks_mock.assert_called_once_with(
            options.expected_hostname)
Ejemplo n.º 17
0
    def get(cls):
        """
    Return Repository Manager object for current OS in safe manner.

    :rtype GenericManager
    """
        if not cls.__repo_manager:
            with cls.__lock:
                cls.__repo_manager = cls.get_new_instance(
                    OSCheck.get_os_family())

        return cls.__repo_manager
Ejemplo n.º 18
0
def bind_signal_handlers(new_handler=None):
  if OSCheck.get_os_family() != OSConst.WINSRV_FAMILY:
      signal.signal(signal.SIGINT, signal_handler)
      signal.signal(signal.SIGTERM, signal_handler)
      signal.signal(signal.SIGUSR1, debug)

  if new_handler is None:
    global _handler
    _handler = StopHandler()
  else:
    _handler = new_handler
  return _handler
Ejemplo n.º 19
0
  def test_main(self, cleanup_mock, ping_port_init_mock, ping_port_start_mock, data_clean_init_mock,data_clean_start_mock,
                parse_args_mock, join_mock, start_mock, Controller_init_mock, try_to_connect_mock,
                update_log_level_mock, daemonize_mock, perform_prestart_checks_mock,
                ambari_config_mock,
                stop_mock, bind_signal_handlers_mock,
                setup_logging_mock, socket_mock):
    data_clean_init_mock.return_value = None
    Controller_init_mock.return_value = None
    ping_port_init_mock.return_value = None
    options = MagicMock()
    parse_args_mock.return_value = (options, MagicMock)
    try_to_connect_mock.return_value = (0, True)
    # use default unix config
    ambari_config_mock.return_value = os.path.abspath("../../../conf/unix/ambari-agent.ini")
    #testing call without command-line arguments

    main.main()

    self.assertTrue(setup_logging_mock.called)
    self.assertTrue(bind_signal_handlers_mock.called)
    if OSCheck.get_os_family() != OSConst.WINSRV_FAMILY:
      self.assertTrue(stop_mock.called)
    #self.assertTrue(resolve_ambari_config_mock.called)
    self.assertTrue(perform_prestart_checks_mock.called)
    if OSCheck.get_os_family() != OSConst.WINSRV_FAMILY:
      self.assertTrue(daemonize_mock.called)
    self.assertTrue(update_log_level_mock.called)
    try_to_connect_mock.assert_called_once_with(ANY, -1, ANY)
    self.assertTrue(start_mock.called)
    self.assertTrue(data_clean_init_mock.called)
    self.assertTrue(data_clean_start_mock.called)
    self.assertTrue(ping_port_init_mock.called)
    self.assertTrue(ping_port_start_mock.called)
    self.assertTrue(cleanup_mock.called)
    perform_prestart_checks_mock.reset_mock()

    # Testing call with --expected-hostname parameter
    options.expected_hostname = "test.hst"
    main.main()
    perform_prestart_checks_mock.assert_called_once_with(options.expected_hostname)
Ejemplo n.º 20
0
 def test_aliases(self, mock_linux_distribution):
   OSConst.OS_TYPE_ALIASES['qwerty_os123'] = 'aliased_os5'
   OSConst.OS_FAMILY_COLLECTION.append({          
         'name': 'aliased_os_family',
         'os_list': ["aliased_os"]
   })
   
   mock_linux_distribution.return_value = ('qwerty_os', '123.45.67', '')
   
   self.assertEquals(OSCheck.get_os_type(), 'aliased_os')
   self.assertEquals(OSCheck.get_os_major_version(), '5')
   self.assertEquals(OSCheck.get_os_version(), '5.45.67')
   self.assertEquals(OSCheck.get_os_family(), 'aliased_os_family')
Ejemplo n.º 21
0
def allAvailablePackages(allAvailablePackages):
    osType = OSCheck.get_os_family()

    if OSCheck.is_suse_family():
        return _lookUpZypperPackages(
            ["sudo", "zypper", "search", "--uninstalled-only", "--details"],
            allAvailablePackages)
    elif OSCheck.is_redhat_family():
        return _lookUpYumPackages(["sudo", "yum", "list", "available"],
                                  'Available Packages', allAvailablePackages)
    elif OSCheck.is_ubuntu_family():
        return _lookUpAptPackages(LIST_AVAILABLE_PACKAGES_UBUNTU,
                                  allAvailablePackages)
Ejemplo n.º 22
0
    def test_aliases(self, mock_linux_distribution):
        OSConst.OS_TYPE_ALIASES['qwerty_os123'] = 'aliased_os5'
        OSConst.OS_FAMILY_COLLECTION.append({
            'name': 'aliased_os_family',
            'os_list': ["aliased_os"]
        })

        mock_linux_distribution.return_value = ('qwerty_os', '123.45.67', '')

        self.assertEquals(OSCheck.get_os_type(), 'aliased_os')
        self.assertEquals(OSCheck.get_os_major_version(), '5')
        self.assertEquals(OSCheck.get_os_version(), '5.45.67')
        self.assertEquals(OSCheck.get_os_family(), 'aliased_os_family')
Ejemplo n.º 23
0
def allAvailablePackages(allAvailablePackages):
    osType = OSCheck.get_os_family()

    if osType == OSConst.SUSE_FAMILY:
        return _lookUpZypperPackages(
            ["zypper", "search", "--uninstalled-only", "--details"],
            allAvailablePackages)
    elif osType == OSConst.REDHAT_FAMILY:
        return _lookUpYumPackages(["yum", "list", "available"],
                                  'Available Packages', allAvailablePackages)
    elif osType == OSConst.UBUNTU_FAMILY:
        return _lookUpAptPackages(LIST_AVAILABLE_PACKAGES_UBUNTU,
                                  allAvailablePackages)
Ejemplo n.º 24
0
def allInstalledPackages(allInstalledPackages):
    """
  All installed packages in system
  """
    osType = OSCheck.get_os_family()

    if OSCheck.is_suse_family():
        return _lookUpZypperPackages(
            ["sudo", "zypper", "search", "--installed-only", "--details"],
            allInstalledPackages)
    elif OSCheck.is_redhat_family():
        return _lookUpYumPackages(["sudo", "yum", "list", "installed"],
                                  'Installed Packages', allInstalledPackages)
    elif OSCheck.is_ubuntu_family():
        return _lookUpAptPackages(LIST_INSTALLED_PACKAGES_UBUNTU,
                                  allInstalledPackages)
def allAvailablePackages(allAvailablePackages):
  osType = OSCheck.get_os_family()

  if OSCheck.is_suse_family():
    return _lookUpZypperPackages(
      ["sudo", "zypper", "search", "--uninstalled-only", "--details"],
      allAvailablePackages)
  elif OSCheck.is_redhat_family():
    return _lookUpYumPackages(
      ["sudo", "yum", "list", "available"],
      'Available Packages',
      allAvailablePackages)
  elif OSCheck.is_ubuntu_family():
     return _lookUpAptPackages(
      LIST_AVAILABLE_PACKAGES_UBUNTU,
      allAvailablePackages)
Ejemplo n.º 26
0
def allInstalledPackages(allInstalledPackages):
    """
  All installed packages in system
  """
    osType = OSCheck.get_os_family()

    if osType == OSConst.SUSE_FAMILY:
        return _lookUpZypperPackages(
            ["zypper", "search", "--installed-only", "--details"],
            allInstalledPackages)
    elif osType == OSConst.REDHAT_FAMILY:
        return _lookUpYumPackages(["yum", "list", "installed"],
                                  'Installed Packages', allInstalledPackages)
    elif osType == OSConst.UBUNTU_FAMILY:
        return _lookUpAptPackages(LIST_INSTALLED_PACKAGES_UBUNTU,
                                  allInstalledPackages)
Ejemplo n.º 27
0
 def allInstalledPackages(self, allInstalledPackages):
   osType = OSCheck.get_os_family()
   
   if osType == OSConst.SUSE_FAMILY:
     return self.lookUpZypperPackages(
       ["zypper", "search", "--installed-only", "--details"],
       allInstalledPackages)
   elif osType == OSConst.REDHAT_FAMILY:
     return self.lookUpYumPackages(
       ["yum", "list", "installed"],
       'Installed Packages',
       allInstalledPackages)
   elif osType == OSConst.UBUNTU_FAMILY:
      return self.lookUpAptPackages(
       LIST_INSTALLED_PACKAGES_UBUNTU,
       allInstalledPackages)   
Ejemplo n.º 28
0
 def allAvailablePackages(self, allAvailablePackages):
   osType = OSCheck.get_os_family()
   
   if osType == OSConst.SUSE_FAMILY:
     return self.lookUpZypperPackages(
       ["zypper", "search", "--uninstalled-only", "--details"],
       allAvailablePackages)
   elif osType == OSConst.REDHAT_FAMILY:
     return self.lookUpYumPackages(
       ["yum", "list", "available"],
       'Available Packages',
       allAvailablePackages)
   elif osType == OSConst.UBUNTU_FAMILY:
      return self.lookUpAptPackages(
       LIST_AVAILABLE_PACKAGES_UBUNTU,
       allAvailablePackages)   
Ejemplo n.º 29
0
def main(argv=None):
  # Same logic that was in "os_type_check.sh"
  if len(sys.argv) != 2:
    print "Usage: <cluster_os>"
    raise Exception("Error in number of arguments. Usage: <cluster_os>")
    pass

  cluster_os = sys.argv[1]
  current_os = OSCheck.get_os_family() + OSCheck.get_os_major_version()

  # If agent/server have the same {"family","main_version"} - then ok.
  print "Cluster primary/cluster OS type is %s and local/current OS type is %s" % (
    cluster_os, current_os)
  if current_os == cluster_os:
    sys.exit(0)
  else:
    raise Exception("Local OS is not compatible with cluster primary OS. Please perform manual bootstrap on this host.")
def allInstalledPackages(allInstalledPackages):
  """
  All installed packages in system
  """
  osType = OSCheck.get_os_family()

  if OSCheck.is_suse_family():
    return _lookUpZypperPackages(
      ["sudo", "zypper", "search", "--installed-only", "--details"],
      allInstalledPackages)
  elif OSCheck.is_redhat_family():
    return _lookUpYumPackages(
      ["sudo", "yum", "list", "installed"],
      'Installed Packages',
      allInstalledPackages)
  elif OSCheck.is_ubuntu_family():
     return _lookUpAptPackages(
      LIST_INSTALLED_PACKAGES_UBUNTU,
      allInstalledPackages)
Ejemplo n.º 31
0
def main(argv=None):
    # Same logic that was in "os_type_check.sh"
    if len(sys.argv) != 2:
        print "Usage: <cluster_os>"
        raise Exception("Error in number of arguments. Usage: <cluster_os>")
        pass

    cluster_os = sys.argv[1]
    current_os = OSCheck.get_os_family() + OSCheck.get_os_major_version()

    # If agent/server have the same {"family","main_version"} - then ok.
    print "Cluster primary/cluster OS family is %s and local/current OS family is %s" % (
        cluster_os, current_os)
    if current_os == cluster_os:
        sys.exit(0)
    else:
        raise Exception(
            "Local OS is not compatible with cluster primary OS family. Please perform manual bootstrap on this host."
        )
Ejemplo n.º 32
0
 def checkLiveServices(self, services, result):
   osType = OSCheck.get_os_family()
   for service in services:
     svcCheckResult = {}
     serviceName = service
     svcCheckResult['name'] = serviceName
     svcCheckResult['status'] = "UNKNOWN"
     svcCheckResult['desc'] = ""
     try:
       out, err, code = self.getServiceStatus(serviceName)
       if 0 != code:
         svcCheckResult['status'] = "Unhealthy"
         svcCheckResult['desc'] = out
         if len(out) == 0:
           svcCheckResult['desc'] = err
       else:
         svcCheckResult['status'] = "Healthy"
     except Exception, e:
       svcCheckResult['status'] = "Unhealthy"
       svcCheckResult['desc'] = repr(e)
     result.append(svcCheckResult)
Ejemplo n.º 33
0
  def do_erase_packages(self, packageList):
    packageStr = None
    if packageList:
      packageStr = ' '.join(packageList)
      logger.debug("Erasing packages: " + packageStr)
    if packageStr is not None and packageStr:
      os_name = OSCheck.get_os_family()
      command = ''
      if os_name in PACKAGE_ERASE_CMD:
        command = PACKAGE_ERASE_CMD[os_name].format(packageStr)
      else:
        logger.warn("Unsupported OS type, cannot remove package.")

      if command != '':
        logger.debug('Executing: ' + str(command))
        (returncode, stdoutdata, stderrdata) = self.run_os_command(command)
        if returncode != 0:
          logger.warn("Erasing packages failed: " + stderrdata)
        else:
          logger.info("Erased packages successfully.\n" + stdoutdata)
    return 0
Ejemplo n.º 34
0
 def checkLiveServices(self, services, result):
     osType = OSCheck.get_os_family()
     for service in services:
         svcCheckResult = {}
         serviceName = service
         svcCheckResult['name'] = serviceName
         svcCheckResult['status'] = "UNKNOWN"
         svcCheckResult['desc'] = ""
         try:
             out, err, code = self.getServiceStatus(serviceName)
             if 0 != code:
                 svcCheckResult['status'] = "Unhealthy"
                 svcCheckResult['desc'] = out
                 if len(out) == 0:
                     svcCheckResult['desc'] = err
             else:
                 svcCheckResult['status'] = "Healthy"
         except Exception, e:
             svcCheckResult['status'] = "Unhealthy"
             svcCheckResult['desc'] = repr(e)
         result.append(svcCheckResult)
Ejemplo n.º 35
0
    def do_erase_packages(self, packageList):
        packageStr = None
        if packageList:
            packageStr = ' '.join(packageList)
            logger.debug("Erasing packages: " + packageStr)
        if packageStr is not None and packageStr:
            os_name = OSCheck.get_os_family()
            command = ''
            if os_name in PACKAGE_ERASE_CMD:
                command = PACKAGE_ERASE_CMD[os_name].format(packageStr)
            else:
                logger.warn("Unsupported OS type, cannot remove package.")

            if command != '':
                logger.debug('Executing: ' + str(command))
                (returncode, stdoutdata,
                 stderrdata) = self.run_os_command(command)
                if returncode != 0:
                    logger.warn("Erasing packages failed: " + stderrdata)
                else:
                    logger.info("Erased packages successfully.\n" + stdoutdata)
        return 0
Ejemplo n.º 36
0
    def test_perform_prestart_checks(self, hostname_mock, isdir_mock,
                                     isfile_mock, exit_mock, shell_mock):
        main.config = AmbariConfig().getConfig()
        shell_mock.return_value = {"exitCode": 0}

        # Check expected hostname test
        hostname_mock.return_value = "test.hst"

        main.perform_prestart_checks("another.hst")
        self.assertTrue(exit_mock.called)

        exit_mock.reset_mock()

        if OSCheck.get_os_family() != OSConst.WINSRV_FAMILY:
            # Trying case if there is another instance running, only valid for linux
            isfile_mock.return_value = True
            isdir_mock.return_value = True
            main.perform_prestart_checks(None)
            self.assertTrue(exit_mock.called)

        isfile_mock.reset_mock()
        isdir_mock.reset_mock()
        exit_mock.reset_mock()

        # Trying case if agent prefix dir does not exist
        isfile_mock.return_value = False
        isdir_mock.return_value = False
        main.perform_prestart_checks(None)
        self.assertTrue(exit_mock.called)

        isfile_mock.reset_mock()
        isdir_mock.reset_mock()
        exit_mock.reset_mock()

        # Trying normal case
        isfile_mock.return_value = False
        isdir_mock.return_value = True
        main.perform_prestart_checks(None)
        self.assertFalse(exit_mock.called)
Ejemplo n.º 37
0
    def get_new_instance(cls, os_family=None):
        """
    Construct new instance of Repository Manager object. Call is not thread-safe

    :param os_family:  os family string; best used in combination with `OSCheck.get_os_family()`
    :type os_family str
    :rtype GenericManager
    """
        if not os_family:
            os_family = OSCheck.get_os_family()

        construct_rules = {
            OSConst.UBUNTU_FAMILY: AptManager,
            OSConst.SUSE_FAMILY: ZypperManager,
            OSConst.REDHAT_FAMILY: YumManager,
            OSConst.WINSRV_FAMILY: ChocoManager
        }
        if os_family in construct_rules:
            return construct_rules[os_family]()

        raise RuntimeError(
            "Not able to create Repository Manager object for unsupported OS family {0}"
            .format(os_family))
Ejemplo n.º 38
0
  def test_perform_prestart_checks(self, hostname_mock, isdir_mock, isfile_mock, exit_mock):
    main.config = AmbariConfig().getConfig()

    # Check expected hostname test
    hostname_mock.return_value = "test.hst"

    main.perform_prestart_checks("another.hst")
    self.assertTrue(exit_mock.called)

    exit_mock.reset_mock()

    if OSCheck.get_os_family() != OSConst.WINSRV_FAMILY:
      # Trying case if there is another instance running, only valid for linux
      isfile_mock.return_value = True
      isdir_mock.return_value = True
      main.perform_prestart_checks(None)
      self.assertTrue(exit_mock.called)

    isfile_mock.reset_mock()
    isdir_mock.reset_mock()
    exit_mock.reset_mock()

    # Trying case if agent prefix dir does not exist
    isfile_mock.return_value = False
    isdir_mock.return_value = False
    main.perform_prestart_checks(None)
    self.assertTrue(exit_mock.called)

    isfile_mock.reset_mock()
    isdir_mock.reset_mock()
    exit_mock.reset_mock()

    # Trying normal case
    isfile_mock.return_value = False
    isdir_mock.return_value = True
    main.perform_prestart_checks(None)
    self.assertFalse(exit_mock.called)
Ejemplo n.º 39
0
  def test_get_os_family(self, mock_exists, mock_linux_distribution):

    # 1 - Any system
    mock_exists.return_value = False
    mock_linux_distribution.return_value = ('MY_os', '', '')
    result = OSCheck.get_os_family()
    self.assertEquals(result, 'my_os')

    # 2 - Redhat
    mock_exists.return_value = False
    mock_linux_distribution.return_value = ('Centos Linux', '', '')
    result = OSCheck.get_os_family()
    self.assertEquals(result, 'redhat')

    # 3 - Debian
    mock_exists.return_value = False
    mock_linux_distribution.return_value = ('Ubuntu', '', '')
    result = OSCheck.get_os_family()
    self.assertEquals(result, 'debian')

    # 4 - Suse
    mock_exists.return_value = False
    mock_linux_distribution.return_value = (
    'suse linux enterprise server', '', '')
    result = OSCheck.get_os_family()
    self.assertEquals(result, 'suse')

    mock_exists.return_value = False
    mock_linux_distribution.return_value = ('SLED', '', '')
    result = OSCheck.get_os_family()
    self.assertEquals(result, 'suse')

    # 5 - Negative case
    mock_linux_distribution.return_value = ('', '111', '2222')
    try:
      result = OSCheck.get_os_family()
      self.fail("Should throw exception in OSCheck.get_os_family()")
    except Exception as e:
      # Expected
      self.assertEquals("Cannot detect os type. Exiting...", str(e))
      pass
Ejemplo n.º 40
0
  def test_get_os_family(self, mock_exists, mock_linux_distribution):

    # 1 - Any system
    mock_exists.return_value = False
    mock_linux_distribution.return_value = ('MY_os', '', '')
    result = OSCheck.get_os_family()
    self.assertEquals(result, 'my_os')

    # 2 - Redhat
    mock_exists.return_value = False
    mock_linux_distribution.return_value = ('Centos Linux', '', '')
    result = OSCheck.get_os_family()
    self.assertEquals(result, 'redhat')

    # 3 - Ubuntu
    mock_exists.return_value = False
    mock_linux_distribution.return_value = ('Ubuntu', '', '')
    result = OSCheck.get_os_family()
    self.assertEquals(result, 'ubuntu')

    # 4 - Suse
    mock_exists.return_value = False
    mock_linux_distribution.return_value = (
    'suse linux enterprise server', '', '')
    result = OSCheck.get_os_family()
    self.assertEquals(result, 'suse')

    mock_exists.return_value = False
    mock_linux_distribution.return_value = ('SLED', '', '')
    result = OSCheck.get_os_family()
    self.assertEquals(result, 'suse')

    # 5 - Negative case
    mock_linux_distribution.return_value = ('', '111', '2222')
    try:
      result = OSCheck.get_os_family()
      self.fail("Should throw exception in OSCheck.get_os_family()")
    except Exception as e:
      # Expected
      self.assertEquals("Cannot detect os type. Exiting...", str(e))
      pass
Ejemplo n.º 41
0
    def test_get_os_family(self, mock_exists, mock_linux_distribution):

        # 1 - Any system
        mock_exists.return_value = False
        mock_linux_distribution.return_value = ("MY_os", "", "")
        result = OSCheck.get_os_family()
        self.assertEquals(result, "my_os")

        # 2 - Redhat
        mock_exists.return_value = False
        mock_linux_distribution.return_value = ("Centos Linux", "", "")
        result = OSCheck.get_os_family()
        self.assertEquals(result, "redhat")

        # 3 - Ubuntu
        mock_exists.return_value = False
        mock_linux_distribution.return_value = ("Ubuntu", "", "")
        result = OSCheck.get_os_family()
        self.assertEquals(result, "ubuntu")

        # 4 - Suse
        mock_exists.return_value = False
        mock_linux_distribution.return_value = ("suse linux enterprise server", "", "")
        result = OSCheck.get_os_family()
        self.assertEquals(result, "suse")

        mock_exists.return_value = False
        mock_linux_distribution.return_value = ("SLED", "", "")
        result = OSCheck.get_os_family()
        self.assertEquals(result, "suse")

        # 5 - Negative case
        mock_linux_distribution.return_value = ("", "111", "2222")
        try:
            result = OSCheck.get_os_family()
            self.fail("Should throw exception in OSCheck.get_os_family()")
        except Exception as e:
            # Expected
            self.assertEquals("Cannot detect os type. Exiting...", str(e))
            pass
Ejemplo n.º 42
0
    def test_main(self, get_status_commands_executor_mock,
                  exithelper_exit_mock, cleanup_mock, ping_port_init_mock,
                  ping_port_start_mock, data_clean_init_mock,
                  data_clean_start_mock, parse_args_mock, start_mock,
                  Controller_is_alive_mock, Controller_init_mock,
                  try_to_connect_mock, update_log_level_mock, daemonize_mock,
                  perform_prestart_checks_mock, ambari_config_mock, stop_mock,
                  bind_signal_handlers_mock, setup_logging_mock, socket_mock):
        data_clean_init_mock.return_value = None
        Controller_init_mock.return_value = None
        Controller_is_alive_mock.return_value = False
        ping_port_init_mock.return_value = None
        options = MagicMock()
        parse_args_mock.return_value = (options, MagicMock)
        try_to_connect_mock.return_value = (0, True, False
                                            )  # (retries, connected, stopped)
        # use default unix config
        ambari_config_mock.return_value = self.init_ambari_config_mock()
        #testing call without command-line arguments

        main.main()

        self.assertTrue(setup_logging_mock.called)
        self.assertTrue(perform_prestart_checks_mock.called)
        if OSCheck.get_os_family() != OSConst.WINSRV_FAMILY:
            self.assertTrue(daemonize_mock.called)
        self.assertTrue(update_log_level_mock.called)
        try_to_connect_mock.assert_called_once_with(ANY, main.MAX_RETRIES, ANY)
        self.assertTrue(start_mock.called)
        self.assertTrue(data_clean_init_mock.called)
        self.assertTrue(data_clean_start_mock.called)
        self.assertTrue(ping_port_init_mock.called)
        self.assertTrue(ping_port_start_mock.called)
        self.assertTrue(exithelper_exit_mock.called)
        perform_prestart_checks_mock.reset_mock()

        # Testing call with --expected-hostname parameter
        options.expected_hostname = "test.hst"
        main.main()
        perform_prestart_checks_mock.assert_called_once_with(
            options.expected_hostname)

        # Test with multiple server hostnames
        default_server_hostnames = hostname.cached_server_hostnames
        hostname.cached_server_hostnames = ['host1', 'host2', 'host3']

        def try_to_connect_impl(*args, **kwargs):
            for server_hostname in hostname.cached_server_hostnames:
                if (args[0].find(server_hostname) != -1):
                    if server_hostname == 'host1':
                        return 0, False, False
                    elif server_hostname == 'host2':
                        return 0, False, False
                    elif server_hostname == 'host3':
                        return 0, True, False
                    else:
                        return 0, True, False
            pass

        try_to_connect_mock.reset_mock()
        try_to_connect_mock.side_effect = try_to_connect_impl
        active_server = main.main()
        self.assertEquals(active_server, 'host3')
        hostname.cached_server_hostnames = default_server_hostnames
        pass
Ejemplo n.º 43
0
import subprocess
import threading
import shlex
import platform
from PackagesAnalyzer import PackagesAnalyzer
from HostCheckReportFileHandler import HostCheckReportFileHandler
from Hardware import Hardware
from ambari_commons import OSCheck, OSConst
import socket

logger = logging.getLogger()

# OS info
OS_VERSION = OSCheck().get_os_major_version()
OS_TYPE = OSCheck.get_os_type()
OS_FAMILY = OSCheck.get_os_family()

# service cmd
SERVICE_CMD = "/sbin/service"

# on ubuntu iptables service is called ufw
if OS_FAMILY == OSConst.DEBIAN_FAMILY:
    SERVICE_CMD = "/usr/sbin/service"


class HostInfo:
    # List of project names to be used to find alternatives folders etc.
    DEFAULT_PROJECT_NAMES = [
        "hadoop*", "hadoop", "hbase", "hcatalog", "hive", "ganglia", "nagios",
        "oozie", "sqoop", "hue", "zookeeper", "mapred", "hdfs", "flume",
        "storm", "hive-hcatalog", "tez", "falcon", "ambari_qa",
Ejemplo n.º 44
0
def main(heartbeat_stop_callback=None):
  global config
  parser = OptionParser()
  parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="verbose log output", default=False)
  parser.add_option("-e", "--expected-hostname", dest="expected_hostname", action="store",
                    help="expected hostname of current host. If hostname differs, agent will fail", default=None)
  (options, args) = parser.parse_args()

  expected_hostname = options.expected_hostname

  logging_level = logging.DEBUG if options.verbose else logging.INFO

  setup_logging(logger, AmbariConfig.AmbariConfig.getLogFile(), logging_level)
  global is_logger_setup
  is_logger_setup = True
  setup_logging(alerts_logger, AmbariConfig.AmbariConfig.getAlertsLogFile(), logging_level)
  Logger.initialize_logger('resource_management', logging_level=logging_level)

  # use the host's locale for numeric formatting
  try:
    locale.setlocale(locale.LC_ALL, '')
  except locale.Error as ex:
    logger.warning("Cannot set locale for ambari-agent. Please check your systemwide locale settings. Failed due to: {0}.".format(str(ex)))

  default_cfg = {'agent': {'prefix': '/home/ambari'}}
  config.load(default_cfg)

  if (len(sys.argv) > 1) and sys.argv[1] == 'stop':
    stop_agent()

  if (len(sys.argv) > 2) and sys.argv[1] == 'reset':
    reset_agent(sys.argv)

  # Check for ambari configuration file.
  resolve_ambari_config()
  
  # Add syslog hanlder based on ambari config file
  add_syslog_handler(logger)

  # Starting data cleanup daemon
  data_cleaner = None
  if config.has_option('agent', 'data_cleanup_interval') and int(config.get('agent','data_cleanup_interval')) > 0:
    data_cleaner = DataCleaner(config)
    data_cleaner.start()

  perform_prestart_checks(expected_hostname)

  # Starting ping port listener
  try:
    #This acts as a single process machine-wide lock (albeit incomplete, since
    # we still need an extra file to track the Agent PID)
    ping_port_listener = PingPortListener(config)
  except Exception as ex:
    err_message = "Failed to start ping port listener of: " + str(ex)
    logger.error(err_message)
    sys.stderr.write(err_message)
    sys.exit(1)
  ping_port_listener.start()

  update_log_level(config)

  if not OSCheck.get_os_family() == OSConst.WINSRV_FAMILY:
    daemonize()

  #
  # Iterate through the list of server hostnames and connect to the first active server
  #

  active_server = None
  server_hostnames = hostname.server_hostnames(config)

  connected = False
  stopped = False

  # Keep trying to connect to a server or bail out if ambari-agent was stopped
  while not connected and not stopped:
    for server_hostname in server_hostnames:
      try:
        server_ip = socket.gethostbyname(server_hostname)
        server_url = config.get_api_url(server_hostname)
        logger.info('Connecting to Ambari server at %s (%s)', server_url, server_ip)
      except socket.error:
        logger.warn("Unable to determine the IP address of the Ambari server '%s'", server_hostname)

      # Wait until MAX_RETRIES to see if server is reachable
      netutil = NetUtil(config, heartbeat_stop_callback)
      (retries, connected, stopped) = netutil.try_to_connect(server_url, MAX_RETRIES, logger)

      # if connected, launch controller
      if connected:
        logger.info('Connected to Ambari server %s', server_hostname)
        # Set the active server
        active_server = server_hostname
        # Launch Controller communication
        controller = Controller(config, server_hostname, heartbeat_stop_callback)
        controller.start()
        while controller.is_alive():
          time.sleep(0.1)

      #
      # If Ambari Agent connected to the server or
      # Ambari Agent was stopped using stop event
      # Clean up if not Windows OS
      #
      if connected or stopped:
        ExitHelper().exit(0)
        logger.info("finished")
        break
    pass # for server_hostname in server_hostnames
  pass # while not (connected or stopped)

  return active_server
Ejemplo n.º 45
0
import subprocess
import threading
import shlex
import platform
from PackagesAnalyzer import PackagesAnalyzer
from HostCheckReportFileHandler import HostCheckReportFileHandler
from Hardware import Hardware
from ambari_commons import OSCheck, OSConst
import socket

logger = logging.getLogger()

# OS info
OS_VERSION = OSCheck().get_os_major_version()
OS_TYPE = OSCheck.get_os_type()
OS_FAMILY = OSCheck.get_os_family()

# service cmd
SERVICE_CMD = "/sbin/service"

# on ubuntu iptables service is called ufw
if OS_FAMILY == OSConst.DEBIAN_FAMILY:
  SERVICE_CMD = "/usr/sbin/service"


class HostInfo:
  # List of project names to be used to find alternatives folders etc.
  DEFAULT_PROJECT_NAMES = [
    "hadoop*", "hadoop", "hbase", "hcatalog", "hive", "ganglia", "nagios",
    "oozie", "sqoop", "hue", "zookeeper", "mapred", "hdfs", "flume",
    "storm", "hive-hcatalog", "tez", "falcon", "ambari_qa", "hadoop_deploy",
Ejemplo n.º 46
0
def main(heartbeat_stop_callback=None):
  global config
  parser = OptionParser()
  parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="verbose log output", default=False)
  parser.add_option("-e", "--expected-hostname", dest="expected_hostname", action="store",
                    help="expected hostname of current host. If hostname differs, agent will fail", default=None)
  (options, args) = parser.parse_args()

  expected_hostname = options.expected_hostname

  current_user = getpass.getuser()

  setup_logging(options.verbose)
  
  default_cfg = {'agent': {'prefix': '/home/ambari'}}
  config.load(default_cfg)

  bind_signal_handlers(agentPid)

  if (len(sys.argv) > 1) and sys.argv[1] == 'stop':
    stop_agent()

  if (len(sys.argv) > 2) and sys.argv[1] == 'reset':
    reset_agent(sys.argv)

  # Check for ambari configuration file.
  resolve_ambari_config()

  # Starting data cleanup daemon
  data_cleaner = None
  if config.has_option('agent', 'data_cleanup_interval') and int(config.get('agent','data_cleanup_interval')) > 0:
    data_cleaner = DataCleaner(config)
    data_cleaner.start()

  perform_prestart_checks(expected_hostname)

  # Starting ping port listener
  try:
    #This acts as a single process machine-wide lock (albeit incomplete, since
    # we still need an extra file to track the Agent PID)
    ping_port_listener = PingPortListener(config)
  except Exception as ex:
    err_message = "Failed to start ping port listener of: " + str(ex)
    logger.error(err_message)
    sys.stderr.write(err_message)
    sys.exit(1)
  ping_port_listener.start()

  update_log_level(config)

  server_hostname = config.get('server', 'hostname')
  server_url = config.get_api_url()

  if not OSCheck.get_os_family() == OSConst.WINSRV_FAMILY:
    daemonize()

  try:
    server_ip = socket.gethostbyname(server_hostname)
    logger.info('Connecting to Ambari server at %s (%s)', server_url, server_ip)
  except socket.error:
    logger.warn("Unable to determine the IP address of the Ambari server '%s'", server_hostname)

  # Wait until server is reachable
  netutil = NetUtil(heartbeat_stop_callback)
  retries, connected = netutil.try_to_connect(server_url, -1, logger)
  # Ambari Agent was stopped using stop event
  if connected:
    # Launch Controller communication
    controller = Controller(config, heartbeat_stop_callback)
    controller.start()
    controller.join()
  if not OSCheck.get_os_family() == OSConst.WINSRV_FAMILY:
    stop_agent()
  logger.info("finished")
Ejemplo n.º 47
0
 def getOsFamily(self):
   return OSCheck.get_os_family()
Ejemplo n.º 48
0
 def getOsFamily(self):
   return OSCheck.get_os_family()
Ejemplo n.º 49
0
 def __init__(self):
     # OS info
     self.OS_VERSION = OSCheck().get_os_major_version()
     self.OS_TYPE = OSCheck.get_os_type()
     self.OS_FAMILY = OSCheck.get_os_family()
Ejemplo n.º 50
0
 def __init__(self):
   # OS info
   self.OS_VERSION = OSCheck().get_os_major_version()
   self.OS_TYPE = OSCheck.get_os_type()
   self.OS_FAMILY = OSCheck.get_os_family()
Ejemplo n.º 51
0
def main(heartbeat_stop_callback=None):
  global config
  parser = OptionParser()
  parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="verbose log output", default=False)
  parser.add_option("-e", "--expected-hostname", dest="expected_hostname", action="store",
                    help="expected hostname of current host. If hostname differs, agent will fail", default=None)
  (options, args) = parser.parse_args()

  expected_hostname = options.expected_hostname

  setup_logging(logger, AmbariConfig.AmbariConfig.getLogFile(), options.verbose)
  global is_logger_setup
  is_logger_setup = True
  setup_logging(alerts_logger, AmbariConfig.AmbariConfig.getAlertsLogFile(), options.verbose)

  default_cfg = {'agent': {'prefix': '/home/ambari'}}
  config.load(default_cfg)

  if (len(sys.argv) > 1) and sys.argv[1] == 'stop':
    stop_agent()

  if (len(sys.argv) > 2) and sys.argv[1] == 'reset':
    reset_agent(sys.argv)

  # Check for ambari configuration file.
  resolve_ambari_config()
  
  # Add syslog hanlder based on ambari config file
  add_syslog_handler(logger)

  # Starting data cleanup daemon
  data_cleaner = None
  if config.has_option('agent', 'data_cleanup_interval') and int(config.get('agent','data_cleanup_interval')) > 0:
    data_cleaner = DataCleaner(config)
    data_cleaner.start()

  perform_prestart_checks(expected_hostname)

  # Starting ping port listener
  try:
    #This acts as a single process machine-wide lock (albeit incomplete, since
    # we still need an extra file to track the Agent PID)
    ping_port_listener = PingPortListener(config)
  except Exception as ex:
    err_message = "Failed to start ping port listener of: " + str(ex)
    logger.error(err_message)
    sys.stderr.write(err_message)
    sys.exit(1)
  ping_port_listener.start()

  update_log_level(config)

  server_hostname = hostname.server_hostname(config)
  server_url = config.get_api_url()

  if not OSCheck.get_os_family() == OSConst.WINSRV_FAMILY:
    daemonize()

  try:
    server_ip = socket.gethostbyname(server_hostname)
    logger.info('Connecting to Ambari server at %s (%s)', server_url, server_ip)
  except socket.error:
    logger.warn("Unable to determine the IP address of the Ambari server '%s'", server_hostname)

  # Wait until server is reachable
  netutil = NetUtil(heartbeat_stop_callback)
  retries, connected = netutil.try_to_connect(server_url, -1, logger)
  # Ambari Agent was stopped using stop event
  if connected:
    # Launch Controller communication
    controller = Controller(config, heartbeat_stop_callback)
    controller.start()
    controller.join()
  if not OSCheck.get_os_family() == OSConst.WINSRV_FAMILY:
    ExitHelper.execute_cleanup()
    stop_agent()
  logger.info("finished")
Ejemplo n.º 52
0
 def new(cls, *args, **kwargs):
     if OSCheck.get_os_family() in cls._impls:
         os_impl_cls = cls._impls[OSCheck.get_os_family()]
     else:
         os_impl_cls = cls._impls[OsFamilyImpl.DEFAULT]
     return object.__new__(os_impl_cls)
Ejemplo n.º 53
0
def main(heartbeat_stop_callback=None):
    global config
    global home_dir

    parser = OptionParser()
    parser.add_option("-v",
                      "--verbose",
                      dest="verbose",
                      action="store_true",
                      help="verbose log output",
                      default=False)
    parser.add_option(
        "-e",
        "--expected-hostname",
        dest="expected_hostname",
        action="store",
        help=
        "expected hostname of current host. If hostname differs, agent will fail",
        default=None)
    parser.add_option("--home",
                      dest="home_dir",
                      action="store",
                      help="Home directory",
                      default="")
    (options, args) = parser.parse_args()

    expected_hostname = options.expected_hostname
    home_dir = options.home_dir

    logging_level = logging.DEBUG if options.verbose else logging.INFO

    setup_logging(logger, AmbariConfig.AmbariConfig.getLogFile(),
                  logging_level)
    global is_logger_setup
    is_logger_setup = True
    setup_logging(alerts_logger, AmbariConfig.AmbariConfig.getAlertsLogFile(),
                  logging_level)
    Logger.initialize_logger('resource_management',
                             logging_level=logging_level)

    if home_dir != "":
        # When running multiple Ambari Agents on this host for simulation, each one will use a unique home directory.
        Logger.info("Agent is using Home Dir: %s" % str(home_dir))

    # use the host's locale for numeric formatting
    try:
        locale.setlocale(locale.LC_ALL, '')
    except locale.Error as ex:
        logger.warning(
            "Cannot set locale for ambari-agent. Please check your systemwide locale settings. Failed due to: {0}."
            .format(str(ex)))

    default_cfg = {'agent': {'prefix': '/home/ambari'}}
    config.load(default_cfg)

    if (len(sys.argv) > 1) and sys.argv[1] == 'stop':
        stop_agent()

    if (len(sys.argv) > 2) and sys.argv[1] == 'reset':
        reset_agent(sys.argv)

    # Check for ambari configuration file.
    resolve_ambari_config()

    # Add syslog hanlder based on ambari config file
    add_syslog_handler(logger)

    # Starting data cleanup daemon
    data_cleaner = None
    if config.has_option('agent', 'data_cleanup_interval') and int(
            config.get('agent', 'data_cleanup_interval')) > 0:
        data_cleaner = DataCleaner(config)
        data_cleaner.start()

    perform_prestart_checks(expected_hostname)

    # Starting ping port listener
    try:
        #This acts as a single process machine-wide lock (albeit incomplete, since
        # we still need an extra file to track the Agent PID)
        ping_port_listener = PingPortListener(config)
    except Exception as ex:
        err_message = "Failed to start ping port listener of: " + str(ex)
        logger.error(err_message)
        sys.stderr.write(err_message)
        sys.exit(1)
    ping_port_listener.start()

    update_log_level(config)

    update_open_files_ulimit(config)

    if not config.use_system_proxy_setting():
        logger.info('Agent is configured to ignore system proxy settings')
        reconfigure_urllib2_opener(ignore_system_proxy=True)

    if not OSCheck.get_os_family() == OSConst.WINSRV_FAMILY:
        daemonize()

    #
    # Iterate through the list of server hostnames and connect to the first active server
    #

    active_server = None
    server_hostnames = hostname.server_hostnames(config)

    connected = False
    stopped = False

    # Keep trying to connect to a server or bail out if ambari-agent was stopped
    while not connected and not stopped:
        for server_hostname in server_hostnames:
            server_url = config.get_api_url(server_hostname)
            try:
                server_ip = socket.gethostbyname(server_hostname)
                logger.info('Connecting to Ambari server at %s (%s)',
                            server_url, server_ip)
            except socket.error:
                logger.warn(
                    "Unable to determine the IP address of the Ambari server '%s'",
                    server_hostname)

            # Wait until MAX_RETRIES to see if server is reachable
            netutil = NetUtil(config, heartbeat_stop_callback)
            (retries, connected,
             stopped) = netutil.try_to_connect(server_url, MAX_RETRIES, logger)

            # if connected, launch controller
            if connected:
                logger.info('Connected to Ambari server %s', server_hostname)
                # Set the active server
                active_server = server_hostname
                # Launch Controller communication
                run_threads(server_hostname, heartbeat_stop_callback)

            #
            # If Ambari Agent connected to the server or
            # Ambari Agent was stopped using stop event
            # Clean up if not Windows OS
            #
            if connected or stopped:
                ExitHelper().exit(0)
                logger.info("finished")
                break
        pass  # for server_hostname in server_hostnames
    pass  # while not (connected or stopped)

    return active_server
Ejemplo n.º 54
0
 def new(cls, *args, **kwargs):
   if OSCheck.get_os_family() in cls._impls:
     os_impl_cls = cls._impls[OSCheck.get_os_family()]
   else:
     os_impl_cls = cls._impls[OsFamilyImpl.DEFAULT]
   return object.__new__(os_impl_cls)