Esempio n. 1
0
def build():
  path = os.path.dirname(os.path.abspath(__file__))
  build_path = path + os.sep + 'build'
  build_out_path = path + os.sep + 'build.out'
  build_out = open(build_out_path, 'wb')

  # Delete old build dir if exists
  if (os.path.exists(build_path)):
    shutil.rmtree(build_path)
  pass

  cwd = os.getcwd()
  os.chdir(path)

  print 'Executing make at location: %s ' % path

  if sys.platform.startswith("win"):
    # Windows
    returncode = call(['make.bat', 'build'], stdout=build_out, stderr=build_out)
  else:
    # Unix based
    returncode = call(['make', 'build'], stdout=build_out, stderr=build_out)
  pass

  os.chdir(cwd)

  if returncode != 0:
    print 'psutil build failed. Please find build output at: %s' % build_out_path
  pass
 def create_host_name_script(self, host_name, host_name_script):
   """
   Creates a shell script that will echo the given hostname.
   :param host_name: Host name to echo
   :param host_name_script: Location to save the scrip to
   """
   template = "#!/bin/sh\n" \
              "echo HOSTNAME"
   with open(str(host_name_script), "w+") as f:
     f.writelines(template.replace("HOSTNAME", host_name))
   subprocess32.call("chmod +x %s" % host_name_script, shell=True)
  def generateJceks(self, commandJson):
    """
    Generates the JCEKS file with passwords for the service specified in commandJson

    :param commandJson: command JSON
    :return: An exit value from the external process that generated the JCEKS file. None if
    there are no passwords in the JSON.
    """
    cmd_result = None
    roleCommand = None
    if 'roleCommand' in commandJson:
      roleCommand = commandJson['roleCommand']
    task_id = None
    if 'taskId' in commandJson:
      task_id = commandJson['taskId']

    logger.info('Generating the JCEKS file: roleCommand={0} and taskId = {1}'.format(roleCommand, task_id))

    # Set up the variables for the external command to generate a JCEKS file
    java_home = commandJson['ambariLevelParams']['java_home']
    java_bin = '{java_home}/bin/java'.format(java_home=java_home)

    cs_lib_path = self.credential_shell_lib_path
    serviceName = commandJson['serviceName']

    # Gather the password values and remove them from the configuration
    configtype_credentials = self.getConfigTypeCredentials(commandJson)

    # CS is enabled but no config property is available for this command
    if len(configtype_credentials) == 0:
      logger.info("Credential store is enabled but no property are found that can be encrypted.")
      commandJson['credentialStoreEnabled'] = "false"

    for config_type, credentials in configtype_credentials.items():
      config = commandJson['configurations'][config_type]
      if 'role' in commandJson and commandJson['role']:
        roleName = commandJson['role']
        file_path = os.path.join(self.getProviderDirectory(roleName), "{0}.jceks".format(config_type))
      else:
        file_path = os.path.join(self.getProviderDirectory(serviceName), "{0}.jceks".format(config_type))
      if os.path.exists(file_path):
        os.remove(file_path)
      provider_path = 'jceks://file{file_path}'.format(file_path=file_path)
      logger.info('provider_path={0}'.format(provider_path))
      for alias, pwd in credentials.items():
        logger.debug("config={0}".format(config))
        protected_pwd = PasswordString(pwd)
        # Generate the JCEKS file
        cmd = (java_bin, '-cp', cs_lib_path, self.credential_shell_cmd, 'create',
               alias, '-value', protected_pwd, '-provider', provider_path)
        logger.info(cmd)
        cmd_result = subprocess32.call(cmd)
        logger.info('cmd_result = {0}'.format(cmd_result))
        os.chmod(file_path, 0644) # group and others should have read access so that the service user can read
      # Add JCEKS provider path instead
      config[self.CREDENTIAL_PROVIDER_PROPERTY_NAME] = provider_path
      config[self.CREDENTIAL_STORE_CLASS_PATH_NAME] = cs_lib_path

    return cmd_result
Esempio n. 4
0
    def run_custom_hook(self, command):
        """
    Runs custom hook
    """
        args = sys.argv

        # Hook script to run
        args[0] = args[0].replace('before-' + args[1], command)
        args[0] = args[0].replace('after-' + args[1], command)

        # Hook script base directory
        args[3] = args[3].replace('before-' + args[1], command)
        args[3] = args[3].replace('after-' + args[1], command)

        args[1] = command.split("-")[1]

        cmd = [sys.executable]
        cmd.extend(args)

        if subprocess32.call(cmd) != 0:
            self.fail_with_error(
                "Error: Unable to run the custom hook script " + cmd.__str__())
 def cmd_restart(self):
   print "Restarting %d host(s)" % len(self.hosts)
   for host in self.hosts:
     cmd = "ambari-agent restart --home %s" % (host.home_dir)
     os.environ['AMBARI_AGENT_CONF_DIR'] = os.path.join(host.home_dir, "etc/ambari-agent/conf")
     subprocess32.call(cmd, shell=True, env=os.environ)
Esempio n. 6
0
def runLocalCmd(cmd):
    return subprocess32.call(cmd, shell=True)