def setup_method(self, method): imp.reload(configreader) imp.reload(test.unit.agent.common.config.app) context.setup( app='test', app_config=test.unit.agent.common.config.app.TestingConfig() ) context.setup_thread_id() context.default_log.info( '%s %s::%s %s' % ('=' * 20, self.__class__.__name__, self._testMethodName, '=' * 20) ) # modify http client to store http requests from amplify.agent.common.util.http import HTTPClient self.http_requests = [] original_get = HTTPClient.get original_post = HTTPClient.post def fake_get(obj, url, *args, **kwargs): self.http_requests.append(url) return original_get(obj, url, *args, **kwargs) def fake_post(obj, url, *args, **kwargs): self.http_requests.append(url) return original_post(obj, url, *args, **kwargs) HTTPClient.get = fake_get HTTPClient.post = fake_post import amplify.agent.pipelines.file amplify.agent.pipelines.file.OFFSET_CACHE = {}
def teardown_method(self, method): context.app_config['credentials']['imagename'] = None context.app_config['credentials']['uuid'] = DEFAULT_UUID context.setup(app='test', app_config=context.app_config) context.objects = None context._setup_object_tank() super(ContainerSystemManagerTestCase, self).teardown_method(method)
def setup_method(self, method): super(ContainerSystemManagerTestCase, self).setup_method(method) context.objects = None context._setup_object_tank() context.app_config['credentials']['imagename'] = 'DockerTest' context.app_config['credentials']['uuid'] = None context.setup(app='test', app_config=context.app_config)
def test_freeze_api_url(self): # check that if api_url is not set it will not prevent agent from setting api_url from cloud context.app_config['cloud']['api_url'] = '' context.setup(app='test', app_config=context.app_config) assert_that(context.freeze_api_url, equal_to(False)) # check that an api_url from our receiver's domain will not prevent agent from setting api_url from cloud context.app_config['cloud']['api_url'] = 'https://receiver.amplify.nginx.com:443/1.1' context.setup(app='test', app_config=context.app_config) assert_that(context.freeze_api_url, equal_to(False)) # check that a custom api_url will prevent agent from setting api_url from cloud context.app_config['cloud']['api_url'] = 'http://some.other.domain/endpoint/' context.setup(app='test', app_config=context.app_config) assert_that(context.freeze_api_url, equal_to(True))
def run(agent_name=None): """ Agent startup procedure Reads options, sets the environment, does other good things :param agent_name: str agent name """ try: from setproctitle import setproctitle proctitle = '%s-agent' % agent_name setproctitle(proctitle) except ImportError: pass try: action = sys.argv[1] if action not in ('start', 'stop', 'configtest', 'debug'): raise IndexError except IndexError: print("Invalid action or no action supplied\n") parser.print_help() sys.exit(1) # check config before start if action in ('configtest', 'debug', 'start'): wait_for_cloud = True if action == 'start' else False rc = test_configuration_and_enviroment(options.config, options.pid, wait_for_cloud, agent_name) print("") if action == 'configtest' or rc: sys.exit(rc) # setup the context debug_mode = action == 'debug' try: from amplify.agent.common.context import context context.setup(app='agent', config_file=options.config, pid_file=options.pid, log_file=options.log, debug=debug_mode, agent_name=agent_name) except: print(traceback.format_exc(sys.exc_traceback)) # run the agent try: from amplify.agent.supervisor import Supervisor supervisor = Supervisor(foreground=options.foreground, debug=debug_mode) if options.foreground or (debug_mode and options.log): supervisor.run() else: from amplify.agent.common.runner import Runner daemon_runner = Runner(supervisor) daemon_runner.do_action() except: context.default_log.error('uncaught exception during run time', exc_info=True) print(traceback.format_exc(sys.exc_traceback))
wait_for_cloud = True if action == 'start' else False rc = test_configuration_and_enviroment(options.config, options.pid, wait_for_cloud) print("") if action == 'configtest' or rc: sys.exit(rc) # setup the context debug_mode = action == 'debug' try: from amplify.agent.common.context import context context.setup(app='agent', config_file=options.config, pid_file=options.pid, log_file=options.log, debug=debug_mode) except: import traceback print(traceback.format_exc(sys.exc_traceback)) # run the agent try: from amplify.agent.supervisor import Supervisor supervisor = Supervisor(foreground=options.foreground, debug=debug_mode) if options.foreground or (debug_mode and options.log): supervisor.run() else:
def test(config_filename, pid_file, wait_for_cloud, agent_name): """ Checks important parameters and checks connection to the cloud :param config_filename: str config file :param pid_file: str pid file :param wait_for_cloud: bool - if True the agent will try to connect to the Cloud once again :param agent_name :return: int: 0 if everything is ok, 1 if something is wrong """ print('') try: # check that config file exists if not os.path.isfile(config_filename) or not os.access( config_filename, os.R_OK): print( "\033[31mConfig file %s could not be found or opened.\033[0m\n" % config_filename) print( "If you installed the agent from the package you should do the following actions:" ) print(" 1. sudo cp %s.default %s" % (config_filename, config_filename)) print(" 2. sudo chown nginx %s" % config_filename) print(" 3. write your API key in [credentials][api_key]") return 1 # check it can be loaded try: from amplify.agent.common.context import context context.setup(app='agent', agent_name=agent_name, config_file=config_filename, pid_file=pid_file, skip_uuid=True) except IOError, e: if hasattr(e, 'filename'): # log error pass else: raise e # check that it has API url if not context.app_config['cloud']['api_url']: if agent_name == 'amplify': api_url = 'https://receiver.amplify.nginx.com:443/1.4' else: api_url = 'https://FQDN-OF-YOUR-INSTALLATION:8443/1.4' print("\033[31mAPI url is not specified in %s\033[0m\n" % config_filename) print("Write API url %s in [cloud][api_url]" % api_url) return 1 # check that is has API key if not context.app_config['credentials']['api_key']: print("\033[31mAPI key is not specified in %s\033[0m\n" % config_filename) print("Write your API key in [credentials][api_key]") return 1 # test logger: get log filename first try: log_filename = context.default_log.handlers[0].baseFilename log_folder = '/'.join(log_filename.split('/')[:-1]) except: print( "\033[31mCould not setup log file based on config in %s\033[0m\n" % config_filename) print( "Please check the file name in [handler_agent-default][args]") return 1 # test logger: the ability to write logs try: context.log.info('performing configtest check...') except: current_user = pwd.getpwuid(os.getuid())[0] print("\033[31mCould not write to %s\033[0m\n" % log_filename) print( "Either wrong permissions, or the log directory doesn't exist\n" ) print("The following may help:") print(" 1. sudo mkdir %s" % log_folder) print(" 2. sudo touch %s" % log_filename) print(" 3. sudo chown %s %s" % (current_user, log_filename)) return 1 # try to connect to the cloud tries = 0 while tries <= 3: tries += 1 try: context.http_client.post('agent/', {}) except (requests.HTTPError, requests.ConnectionError) as e: api_url = context.app_config['cloud']['api_url'] print("\033[31mCould not connect to API via url %s\033[0m\n" % api_url) if e.response and e.response.status_code == 404: api_key = context.app_config['credentials']['api_key'] print( "\033[31mIt seems like your API key '%s' is wrong. \033[0m\n" % api_key) return 1 else: if (wait_for_cloud and tries == 1) or wait_for_cloud is False: print( "\033[31mIt seems like we have little problems connecting to API.\033[0m" ) print("\033[31mApologies and bear with us. \033[0m\n") if wait_for_cloud and tries < 3: print( "\033[31mWe will try to establish a connection once again in a minute.\033[0m\n" ) if wait_for_cloud and tries == 3: print("\033[31mGiving up after three attempts...\033[0m\n") return 1 elif wait_for_cloud is False: return 1 else: time.sleep(60) else: break
def test(config_file, pid_file): print('') try: # check that config file exists if not os.path.isfile(config_file) or not os.access( config_file, os.R_OK): print( "\033[31mConfig file %s could not be found or opened.\033[0m\n" % config_file) print( "If you installed the agent from the package you should do the following actions:" ) print( " 1. sudo cp /etc/amplify-agent/agent.conf.default /etc/amplify-agent/agent.conf" ) print(" 2. sudo chown nginx /etc/amplify-agent/agent.conf") print(" 3. write your API key in [credentials][api_key]") return 1 # check it can be loaded from amplify.agent.common.context import context context.setup(app='agent', config_file=config_file, pid_file=pid_file) # check that it contain needed stuff if not context.app_config['cloud']['api_url']: print("\033[31mAPI url is not specified in %s\033[0m\n" % config_file) print( "Write API url https://receiver.amplify.nginx.com:443/1.3 in [cloud][api_url]" ) return 1 if not context.app_config['credentials']['api_key']: print("\033[31mAPI key is not specified in %s\033[0m\n" % config_file) print("Write your API key in [credentials][api_key]") return 1 # test logger try: context.log.info('configtest check') except: print("\033[31mCould not write to log\033[0m\n") print("Maybe the log folder doesn't exist or rights are broken") print("You should do the following actions:") print(" 1. sudo mkdir /var/log/amplify-agent") print(" 2. sudo touch /var/log/amplify-agent/agent.log") print(" 3. sudo chown nginx /var/log/amplify-agent/agent.log") return 1 # try to connect to the cloud try: context.http_client.post('agent/', {}) except requests.HTTPError as e: api_url = context.app_config['cloud']['api_url'] print("\033[31mCould not connect to cloud via url %s\033[0m" % api_url) if e.response.status_code == 404: api_key = context.app_config['credentials']['api_key'] print( "\033[31mIt seems like your API key '%s' is wrong. \033[0m\n" % api_key) else: print( "\033[31mIt seems like we have little problems at our side. \nApologies and bear with us\033[0m\n" ) return 1 except: print("\033[31mSomething failed:\033[0m\n") print(traceback.format_exc()) return 1 print("\033[32mConfig file %s is OK\033[0m" % config_file) return 0
def setup_method(self, method): super(ContextContainerTestCase, self).setup_method(method) context.app_config['credentials']['imagename'] = 'DockerTest' context.setup(app='test', app_config=context.app_config)
def teardown_method(self, method): context.app_config['credentials']['imagename'] = None context.app_config['credentials']['uuid'] = DEFAULT_UUID context.setup(app='test', app_config=context.app_config)
#!/usr/bin/python # -*- coding: utf-8 -*- import os import sys import asyncore import socket from optparse import OptionParser, Option from collections import deque sys.path.append(os.getcwd()) # to make amplify libs available from amplify.agent.common.context import context context.setup(app="agent", config_file="etc/agent.conf.development") from amplify.agent.pipelines.syslog import SyslogServer __author__ = "Grant Hulegaard" __copyright__ = "Copyright (C) Nginx, Inc. All rights reserved." __credits__ = ["Mike Belov", "Andrei Belov", "Ivan Poluyanov", "Oleg Mamontov", "Andrew Alexeev", "Grant Hulegaard"] __license__ = "" __maintainer__ = "Mike Belov" __email__ = "*****@*****.**" # HELPERS cache = deque(maxlen=10000)
print("Invalid action or no action supplied\n") parser.print_help() sys.exit(1) # check config before start if action in ('configtest', 'start'): rc = test_config(options.config, options.pid) print("") if action == 'configtest' or rc: sys.exit(rc) try: from amplify.agent.common.context import context context.setup(app='agent', config_file=options.config, pid_file=options.pid) except: import traceback print(traceback.format_exc(sys.exc_traceback)) try: from amplify.agent.supervisor import Supervisor supervisor = Supervisor(foreground=options.foreground) if not options.foreground: from amplify.agent.common.runner import Runner daemon_runner = Runner(supervisor) daemon_runner.do_action() else: supervisor.run()
parser.print_help() sys.exit(1) # check config before start if action in ('configtest', 'start'): rc = test_config(options.config, options.pid) print("") if action == 'configtest' or rc: sys.exit(rc) try: from amplify.agent.common.context import context context.setup( app='agent', config_file=options.config, pid_file=options.pid ) except: import traceback print(traceback.format_exc(sys.exc_traceback)) try: from amplify.agent.supervisor import Supervisor supervisor = Supervisor(foreground=options.foreground) if not options.foreground: from amplify.agent.common.runner import Runner daemon_runner = Runner(supervisor) daemon_runner.do_action() else:
#!/usr/bin/python # -*- coding: utf-8 -*- import json import os import sys import time from optparse import OptionParser, Option sys.path.append(os.getcwd()) # to make amplify libs available from amplify.agent.common.context import context context.setup( app='agent', config_file='etc/agent.conf.development', ) from amplify.agent.objects.nginx.config import NginxConfig __author__ = "Mike Belov" __copyright__ = "Copyright (C) Nginx, Inc. All rights reserved." __credits__ = ["Mike Belov", "Andrei Belov", "Ivan Poluyanov", "Oleg Mamontov", "Andrew Alexeev", "Grant Hulegaard"] __license__ = "" __maintainer__ = "Mike Belov" __email__ = "*****@*****.**" usage = "usage: sudo -u nginx %prog -h" option_list = ( Option(
#!/usr/bin/python # -*- coding: utf-8 -*- import os import sys import asyncore import socket from optparse import OptionParser, Option from collections import deque sys.path.append(os.getcwd()) # to make amplify libs available from amplify.agent.common.context import context context.setup( app='agent', config_file='etc/agent.conf.development', ) from amplify.agent.pipelines.syslog import SyslogServer __author__ = "Grant Hulegaard" __copyright__ = "Copyright (C) Nginx, Inc. All rights reserved." __credits__ = [ "Mike Belov", "Andrei Belov", "Ivan Poluyanov", "Oleg Mamontov", "Andrew Alexeev", "Grant Hulegaard" ] __license__ = "" __maintainer__ = "Mike Belov" __email__ = "*****@*****.**" # HELPERS
def test(config_file, pid_file, wait_for_cloud=False): """ Checks important parameters and checks connection to the cloud :param config_file: str config file :param pid_file: str pid file :param wait_for_cloud: bool - if True the agent will try to connect to the Cloud once again :return: """ print('') try: # check that config file exists if not os.path.isfile(config_file) or not os.access( config_file, os.R_OK): print( "\033[31mConfig file %s could not be found or opened.\033[0m\n" % config_file) print( "If you installed the agent from the package you should do the following actions:" ) print( " 1. sudo cp /etc/amplify-agent/agent.conf.default /etc/amplify-agent/agent.conf" ) print(" 2. sudo chown nginx /etc/amplify-agent/agent.conf") print(" 3. write your API key in [credentials][api_key]") return 1 # check it can be loaded try: from amplify.agent.common.context import context context.setup(app='agent', config_file=config_file, pid_file=pid_file, skip_uuid=True) except IOError, e: if hasattr(e, 'filename'): # log error pass else: raise e # check that it contain needed stuff if not context.app_config['cloud']['api_url']: print("\033[31mAPI url is not specified in %s\033[0m\n" % config_file) print( "Write API url https://receiver.amplify.nginx.com:443/1.3 in [cloud][api_url]" ) return 1 if not context.app_config['credentials']['api_key']: print("\033[31mAPI key is not specified in %s\033[0m\n" % config_file) print("Write your API key in [credentials][api_key]") return 1 # test logger try: context.log.info('configtest check') except: current_user = pwd.getpwuid(os.getuid())[0] print( "\033[31mCould not write to /var/log/amplify-agent/agent.log\033[0m\n" ) print( "Either wrong permissions, or the log directory doesn't exist\n" ) print("The following may help:") print(" 1. sudo mkdir /var/log/amplify-agent") print(" 2. sudo touch /var/log/amplify-agent/agent.log") print(" 3. sudo chown %s /var/log/amplify-agent/agent.log" % current_user) return 1 # try to connect to the cloud tries = 0 while tries <= 3: tries += 1 try: context.http_client.post('agent/', {}) except (requests.HTTPError, requests.ConnectionError) as e: api_url = context.app_config['cloud']['api_url'] print( "\033[31mCould not connect to cloud via url %s\033[0m\n" % api_url) if e.response and e.response.status_code == 404: api_key = context.app_config['credentials']['api_key'] print( "\033[31mIt seems like your API key '%s' is wrong. \033[0m\n" % api_key) return 1 else: if (wait_for_cloud and tries == 1) or wait_for_cloud is False: print( "\033[31mIt seems like we have little problems connecting to cloud.\033[0m" ) print("\033[31mApologies and bear with us. \033[0m\n") if wait_for_cloud and tries < 3: print( "\033[31mWe will try to establish a connection once again in a minute.\033[0m\n" ) if wait_for_cloud and tries == 3: print("\033[31mGiving up after three attempts...\033[0m\n") return 1 elif wait_for_cloud is False: return 1 else: time.sleep(60)
def test(config_file, pid_file): print('') try: # check that config file exists if not os.path.isfile(config_file) or not os.access(config_file, os.R_OK): print("\033[31mConfig file %s could not be found or opened.\033[0m\n" % config_file) print("If you installed the agent from the package you should do the following actions:") print(" 1. sudo cp /etc/amplify-agent/agent.conf.default /etc/amplify-agent/agent.conf") print(" 2. sudo chown nginx /etc/amplify-agent/agent.conf") print(" 3. write your API key in [credentials][api_key]") return 1 # check it can be loaded from amplify.agent.common.context import context context.setup( app='agent', config_file=config_file, pid_file=pid_file ) # check that it contain needed stuff if not context.app_config['cloud']['api_url']: print("\033[31mAPI url is not specified in %s\033[0m\n" % config_file) print("Write API url https://receiver.amplify.nginx.com:443/1.0 in [cloud][api_url]") return 1 if not context.app_config['credentials']['api_key']: print("\033[31mAPI key is not specified in %s\033[0m\n" % config_file) print("Write your API key in [credentials][api_key]") return 1 # test logger try: context.log.info('configtest check') except: print("\033[31mCould not write to log\033[0m\n") print("Maybe the log folder doesn't exist or rights are broken") print("You should do the following actions:") print(" 1. sudo mkdir /var/log/amplify-agent") print(" 2. sudo touch /var/log/amplify-agent/agent.log") print(" 3. sudo chown nginx /var/log/amplify-agent/agent.log") return 1 # try to connect to the cloud try: context.http_client.post('agent/', {}) except requests.HTTPError as e: api_url = context.app_config['cloud']['api_url'] print("\033[31mCould not connect to cloud via url %s\033[0m" % api_url) if e.response.status_code == 404: api_key = context.app_config['credentials']['api_key'] print("\033[31mIt seems like your API key '%s' is wrong. \033[0m\n" % api_key) else: print("\033[31mIt seems like we have little problems at our side. \nApologies and bear with us\033[0m\n") return 1 except: print("\033[31mSomething failed:\033[0m\n") print(traceback.format_exc()) return 1 print("\033[32mConfig file %s is OK\033[0m" % config_file) return 0
# -*- coding: utf-8 -*- import json import os import sys import time # make amplify libs available script_location = os.path.abspath(os.path.expanduser(__file__)) agent_repo_path = os.path.dirname(os.path.dirname(script_location)) agent_config_file = os.path.join(agent_repo_path, 'etc', 'agent.conf.development') sys.path.append(agent_repo_path) # setup agent config from amplify.agent.common.context import context context.setup(app='agent', config_file=agent_config_file) context.app_config['daemon']['cpu_sleep'] = 0.0 from amplify.agent.objects.nginx.config.config import NginxConfig __author__ = "Mike Belov" __copyright__ = "Copyright (C) Nginx, Inc. All rights reserved." __license__ = "" __maintainer__ = "Mike Belov" __email__ = "*****@*****.**" def parse_args(): from argparse import ArgumentParser parser = ArgumentParser( description='A tool for using the NGINX Amplify config parser')