Exemplo n.º 1
0
def generate_state_file():
    state_file_name = "state.conf"
    if configuration.get_state_directory_path() == configuration.DEFAULT_STATE_DIRECTORY_PATH:
        state_file_path = os.path.join(configuration.get_working_directory_path(), state_file_name)
    else:
        state_file_path = os.path.join(configuration.get_state_directory_path(), state_file_name)

    tracer.log_debug_trace("State file path : " + str(state_file_path))

    if os.path.isfile(state_file_path):
        os.remove(state_file_path)

    section = "state"
    conf_file = open(state_file_path, 'wb')
    config = ConfigParser.ConfigParser()
    config.add_section(section)
    config.set(section, configuration.STATE_PID, str(os.getpid()))
    config.set(section, configuration.WORKER_VERSION, str(configuration.get_worker_version()))

    # for OMS scenarios, optional for DIY
    if len(sys.argv) >= 3:
        config.set(section, configuration.STATE_WORKSPACE_ID, str(sys.argv[2]))

    if len(sys.argv) >= 4:
        config.set(section, configuration.STATE_RESOURCE_VERSION, str(sys.argv[3]))

    config.write(conf_file)
    conf_file.close()
Exemplo n.º 2
0
    def __init__(self,
                 cert_path,
                 key_path,
                 insecure=False,
                 proxy_configuration=None):
        self.cert_path = cert_path
        self.key_path = key_path
        self.insecure = insecure
        self.proxy_configuration = proxy_configuration

        # validate presence of cert/key in case they were removed after process creation
        if (cert_path is not None and not os.path.isfile(self.cert_path)) or \
                (key_path is not None and not os.path.isfile(self.key_path)):
            print cert_path
            raise Exception("Invalid certificate or key file path.")

        self.default_headers = {
            self.ACCEPT_HEADER_KEY:
            self.APP_JSON_HEADER_VALUE,
            self.CONNECTION_HEADER_KEY:
            self.KEEP_ALIVE_HEADER_VALUE,
            self.USER_AGENT_HEADER_KEY:
            "AzureAutomationHybridWorker/" +
            configuration.get_worker_version()
        }
        self.json = serializerfactory.get_serializer(sys.version_info)
Exemplo n.º 3
0
def init():
    """Initializes all required variable for the tracer."""
    global jrds_client, jrds_cert_path, jrds_key_path, jrds_base_uri, subscription_id, \
        account_id, machine_id, hybrid_worker_group_name, worker_version, activity_id, sandbox_id

    # Create the http client
    http_client_factory = HttpClientFactory(
        configuration.get_jrds_cert_path(), configuration.get_jrds_key_path(),
        configuration.get_verify_certificates())
    http_client = http_client_factory.create_http_client(sys.version_info)
    jrds_client = JRDSClient(http_client)

    # Populate global configuration values
    jrds_cert_path = configuration.get_jrds_cert_path()
    jrds_key_path = configuration.get_jrds_key_path()
    jrds_base_uri = configuration.get_jrds_base_uri()
    subscription_id = "00000000-0000-0000-0000-000000000000"  # temporary place holder
    account_id = configuration.get_account_id()
    machine_id = configuration.get_machine_id()
    hybrid_worker_group_name = configuration.get_hybrid_worker_name()
    worker_version = configuration.get_worker_version()

    sandbox_id = None
    try:
        sandbox_id = os.environ["sandbox_id"]
    except KeyError:
        pass

    # initialize the loggers for for all components except runbook
    if configuration.get_component() != "runbook":
        init_logger()
Exemplo n.º 4
0
def generate_state_file():
    state_file_name = "state.conf"
    if configuration.get_state_directory_path(
    ) == configuration.DEFAULT_STATE_DIRECTORY_PATH:
        state_file_path = os.path.join(
            configuration.get_working_directory_path(), state_file_name)
    else:
        state_file_path = os.path.join(
            configuration.get_state_directory_path(), state_file_name)

    tracer.log_debug_trace("State file path : " + str(state_file_path))

    if os.path.isfile(state_file_path):
        os.remove(state_file_path)

    section = "state"
    conf_file = open(state_file_path, 'wb')
    config = ConfigParser.ConfigParser()
    config.add_section(section)
    config.set(section, configuration.STATE_PID, str(os.getpid()))
    config.set(section, configuration.WORKER_VERSION,
               str(configuration.get_worker_version()))

    # for OMS scenarios, optional for DIY
    if len(sys.argv) >= 3:
        config.set(section, configuration.STATE_WORKSPACE_ID, str(sys.argv[2]))

    if len(sys.argv) >= 4:
        config.set(section, configuration.STATE_RESOURCE_VERSION,
                   str(sys.argv[3]))

    config.write(conf_file)
    conf_file.close()
Exemplo n.º 5
0
def init():
    """Initializes all required variable for the tracer."""
    global jrds_client, jrds_cert_path, jrds_key_path, jrds_base_uri, subscription_id, \
        account_id, machine_id, hybrid_worker_group_name, worker_version, sandbox_id

    # Create the http client
    http_client_factory = HttpClientFactory(configuration.get_jrds_cert_path(), configuration.get_jrds_key_path(),
                                            configuration.get_verify_certificates())
    http_client = http_client_factory.create_http_client(sys.version_info)
    jrds_client = JRDSClient(http_client)

    # Populate global configuration values
    jrds_cert_path = configuration.get_jrds_cert_path()
    jrds_key_path = configuration.get_jrds_key_path()
    jrds_base_uri = configuration.get_jrds_base_uri()
    subscription_id = "00000000-0000-0000-0000-000000000000"  # temporary place holder
    account_id = configuration.get_account_id()
    machine_id = configuration.get_machine_id()
    hybrid_worker_group_name = configuration.get_hybrid_worker_name()
    worker_version = configuration.get_worker_version()

    sandbox_id = None
    try:
        sandbox_id = os.environ["sandbox_id"]
    except KeyError:
        pass

    # initialize the loggers for for all components except runbook
    if configuration.get_component() != "runbook":
        locallogger.init_logger()
Exemplo n.º 6
0
 def __init__(self):
     tracer.log_worker_starting(configuration.get_worker_version())
     http_client_factory = HttpClientFactory(
         configuration.get_jrds_cert_path(),
         configuration.get_jrds_key_path())
     http_client = http_client_factory.create_http_client(
         sys.version_info, configuration.get_verify_certificates())
     self.jrds_client = JRDSClient(http_client)
     self.running_sandboxes = {}
Exemplo n.º 7
0
 def telemetry_routine(self):
     while True:
         tracer.log_worker_general_telemetry(
             configuration.get_worker_version())
         tracer.log_worker_python_telemetry(platform.python_version(),
                                            platform.python_build(),
                                            platform.python_compiler())
         tracer.log_worker_system_telemetry(platform.system(),
                                            platform.node(),
                                            platform.version(),
                                            platform.machine(),
                                            platform.processor())
         # sleep for 6 hours, this allows us to gather daily telemetry
         time.sleep(60 * 60 * 6)
    def __init__(self, cert_path, key_path, insecure=False):
        self.cert_path = cert_path
        self.key_path = key_path
        self.insecure = insecure

        if (cert_path is not None and not os.path.isfile(self.cert_path)) or \
                (key_path is not None and not os.path.isfile(self.key_path)):
            print cert_path
            raise Exception("Invalid certificate or key file path.")

        self.default_headers = {self.ACCEPT_HEADER_KEY: self.APP_JSON_HEADER_VALUE,
                                self.CONNECTION_HEADER_KEY: self.KEEP_ALIVE_HEADER_VALUE,
                                self.USER_AGENT_HEADER_KEY: "AzureAutomationHybridWorker/" +
                                                            configuration.get_worker_version()}
        self.json = serializerfactory.get_serializer(sys.version_info)
Exemplo n.º 9
0
def main():
    parser = OptionParser(usage="usage: %prog --initialize",
                          version="%prog " +
                          str(configuration.get_worker_version()))
    parser.add_option("--initialize",
                      action="store_true",
                      dest="initialize",
                      default=False)
    (options, args) = parser.parse_args()

    nxautomation_uid = int(pwd.getpwnam("nxautomation").pw_uid)
    if os.getuid() != nxautomation_uid:
        raise Exception("OMSUtil can only be ran as nxautomation user.")

    if options.initialize is True:
        initialize()
    else:
        raise Exception("No option specified.")
Exemplo n.º 10
0
def init():
    """Initializes all required variable for the tracer."""
    global default_logger, sandbox_stdout, jrds_client, jrds_cert_path, jrds_key_path, jrds_base_uri, subscription_id, \
        account_id, machine_id, hybrid_worker_group_name, worker_version, activity_id, sandbox_id

    # Create the http client
    http_client_factory = HttpClientFactory(
        configuration.get_jrds_cert_path(), configuration.get_jrds_key_path(),
        configuration.get_verify_certificates())
    http_client = http_client_factory.create_http_client(sys.version_info)
    jrds_client = JRDSClient(http_client)

    # Populate global configuration values
    jrds_cert_path = configuration.get_jrds_cert_path()
    jrds_key_path = configuration.get_jrds_key_path()
    jrds_base_uri = configuration.get_jrds_base_uri()
    subscription_id = "00000000-0000-0000-0000-000000000000"  # temporary place holder
    account_id = configuration.get_account_id()
    machine_id = configuration.get_machine_id()
    hybrid_worker_group_name = configuration.get_hybrid_worker_name()
    worker_version = configuration.get_worker_version()

    sandbox_id = None
    try:
        sandbox_id = os.environ["sandbox_id"]
    except KeyError:
        pass

    if sandbox_id is not None:
        log_file_name = configuration.get_component() + sandbox_id
    else:
        log_file_name = configuration.get_component()

    file_name = os.path.join(configuration.get_working_directory_path(),
                             log_file_name + '.log')
    logging.Formatter.converter = time.gmtime

    # Default logger
    default_logger = logging.getLogger("default_logger")
    default_logger.setLevel(logging.INFO)

    # Logger for the sandbox traces coming back to worker
    sandbox_stdout = logging.getLogger("sandbox_stdout_logger")
    sandbox_stdout.setLevel(logging.INFO)

    # Default rotating file handler write traces with the specified format to disk.
    default_rf_handler = logging.handlers.RotatingFileHandler(
        file_name, maxBytes=10485760, backupCount=5)
    formatter = logging.Formatter('%(asctime)s (' + str(os.getpid()) + ')' +
                                  configuration.get_component() +
                                  ' : %(message)s',
                                  datefmt="%Y-%m-%d %H:%M:%S")
    default_rf_handler.setFormatter(formatter)
    default_logger.addHandler(default_rf_handler)

    # Traces coming from sandbox child process and collected by the worker are already formatted, hence no formatter
    # needed.
    worker_sandbox_rf_handler = logging.handlers.RotatingFileHandler(
        file_name, maxBytes=10485760, backupCount=5)
    sandbox_stdout.addHandler(worker_sandbox_rf_handler)

    # Stdout handler (Worker traces have to be formatted).
    log_stream = logging.StreamHandler(sys.stdout)
    log_stream.setFormatter(formatter)
    default_logger.addHandler(log_stream)

    # Stdout handler (traces coming from child process are already formatted).
    sandbox_log_stream = logging.StreamHandler(sys.stdout)
    sandbox_stdout.addHandler(sandbox_log_stream)
Exemplo n.º 11
0
 def __init__(self):
     tracer.log_worker_starting(configuration.get_worker_version())
     http_client_factory = HttpClientFactory(configuration.get_jrds_cert_path(), configuration.get_jrds_key_path())
     http_client = http_client_factory.create_http_client(sys.version_info, configuration.get_verify_certificates())
     self.jrds_client = JRDSClient(http_client)
     self.running_sandboxes = {}
Exemplo n.º 12
0
def generate_state_file():
    state_file_name = "state.conf"
    if configuration.get_state_directory_path(
    ) == configuration.DEFAULT_STATE_DIRECTORY_PATH:
        state_file_path = os.path.join(
            configuration.get_working_directory_path(), state_file_name)
    else:
        state_file_path = os.path.join(
            configuration.get_state_directory_path(), state_file_name)

    tracer.log_debug_trace("State file path : " + str(state_file_path))

    if os.path.isfile(state_file_path):
        os.remove(state_file_path)

    section = "state"
    conf_file = open(state_file_path, 'wb')
    config = ConfigParser.ConfigParser()
    config.add_section(section)
    config.set(section, configuration.STATE_PID, str(os.getpid()))
    config.set(section, configuration.WORKER_VERSION,
               str(configuration.get_worker_version()))

    # for OMS scenarios, optional for DIY
    if len(sys.argv) >= 3:
        config.set(section, configuration.STATE_WORKSPACE_ID, str(sys.argv[2]))

    if len(sys.argv) >= 4:
        config.set(section, configuration.STATE_RESOURCE_VERSION,
                   str(sys.argv[3]))

    config.write(conf_file)
    conf_file.close()

    # OMS integration
    # set the ownership of the state file to nxautomation:omiusers
    # set the permission of the state file to 660
    if os.name.lower() != "nt":
        import pwd
        try:
            nxautomation_uid = int(pwd.getpwnam('nxautomation').pw_uid)
            if os.getuid() == nxautomation_uid:
                retval = subprocess.call([
                    "sudo", "chown", "nxautomation:omiusers", state_file_path
                ])
                if retval != 0:
                    exit_on_error(
                        "Could not change owner of state file %s to nxautomation:omiusers"
                        % (state_file_path))

                retval = subprocess.call(
                    ["sudo", "chmod", "660", state_file_path])
                if retval != 0:
                    exit_on_error(
                        "Could not change permission of state file %s " %
                        (state_file_path))
        except KeyError:
            # nxautomation user was not found on the system, skip this step
            tracer.log_debug_trace(
                "State file permission change skipped. nxautomation user not found."
            )
            pass
Exemplo n.º 13
0
def init():
    """Initializes all required variable for the tracer."""
    global default_logger, sandbox_stdout, jrds_client, jrds_cert_path, jrds_key_path, jrds_base_uri, subscription_id,\
        account_id, machine_id, hybrid_worker_group_name, worker_version, activity_id

    # Create the http client
    http_client_factory = HttpClientFactory(configuration.get_jrds_cert_path(), configuration.get_jrds_key_path())
    http_client = http_client_factory.create_http_client(sys.version_info, configuration.get_verify_certificates())
    jrds_client = JRDSClient(http_client)

    # Populate global configuration values
    jrds_cert_path = configuration.get_jrds_cert_path()
    jrds_key_path = configuration.get_jrds_key_path()
    jrds_base_uri = configuration.get_jrds_base_uri()
    subscription_id = "00000000-0000-0000-0000-000000000000" # temporary place holder
    account_id = configuration.get_account_id()
    machine_id = configuration.get_machine_id()
    hybrid_worker_group_name = configuration.get_hybrid_worker_name()
    worker_version = configuration.get_worker_version()
    activity_id = generate_activity_id()

    sandbox_id = None
    try:
        sandbox_id = os.environ["sandbox_id"]
    except KeyError:
        pass

    if sandbox_id is not None:
        log_file_name = configuration.get_component() + sandbox_id
    else:
        log_file_name = configuration.get_component()

    file_name = os.path.join(configuration.get_working_directory_path(), log_file_name + '.log')
    logging.Formatter.converter = time.gmtime

    # Default logger
    default_logger = logging.getLogger("default_logger")
    default_logger.setLevel(logging.INFO)

    # Logger for the sandbox traces coming back to worker
    sandbox_stdout = logging.getLogger("sandbox_stdout_logger")
    sandbox_stdout.setLevel(logging.INFO)

    # Default rotating file handler write traces with the specified format to disk.
    default_rf_handler = logging.handlers.RotatingFileHandler(file_name, maxBytes=10485760, backupCount=5)
    formatter = logging.Formatter('%(asctime)s (' + str(os.getpid()) + ')' + configuration.get_component() +
                                  ' : %(message)s', datefmt="%Y-%m-%d %H:%M:%S")
    default_rf_handler.setFormatter(formatter)
    default_logger.addHandler(default_rf_handler)

    # Traces coming from sandbox child process and collected by the worker are already formatted, hence no formatter
    # needed.
    worker_sandbox_rf_handler = logging.handlers.RotatingFileHandler(file_name, maxBytes=10485760, backupCount=5)
    sandbox_stdout.addHandler(worker_sandbox_rf_handler)

    # Stdout handler (Worker traces have to be formatted).
    log_stream = logging.StreamHandler(sys.stdout)
    log_stream.setFormatter(formatter)
    default_logger.addHandler(log_stream)

    # Stdout handler (traces coming from child process are already formatted).
    sandbox_log_stream = logging.StreamHandler(sys.stdout)
    sandbox_stdout.addHandler(sandbox_log_stream)