Esempio n. 1
0
def main():
    '''
    Description:
        This script will be used on EC2 for configuring the running
        instance based on Cloud Engine configuration supplied at
        launch time in the user data.

        Config Server Status:
        200 HTTP OK - Success and no more data of this type
        202 HTTP Accepted - Success and more data of this type
        404 HTTP Not Found - This may be temporary so try again
    '''
    # parse the args and setup logging
    conf = parse_args()
    log_file = {}
    if 'pwd' in conf and conf.pwd:
        log_file = {'logfile_name': 'audrey.log'}

    logger = setup_logging(level=conf.log_level, **log_file)

    if not conf.endpoint:
        # discover the cloud I'm on
        # update the conf with the user data
        #conf = dict(vars(conf).items() + user_data.discover().read().items())
        vars(conf).update(user_data.discover().read().items())

    # ensure the conf is a dictionary, not a namespace
    if hasattr(conf, '__dict__'):
        conf = vars(conf)

    logger.info('Invoked audrey main')

    # Create the Client Object and test connectivity
    # to CS by negotiating the api version
    client = CSClient(**conf)
    client.test_connection()

    # Get the agent object
    agent = AudreyFactory(client.api_version).agent
    # run the agent
    agent(conf).run()
Esempio n. 2
0
class AgentV1(object):
    '''
    contains the main logic for processing
    This object is compatible with API Version 1
    '''
    def __init__(self, conf):
        '''
        conf: argparse dict
        '''
        tool_dir = {}
        if 'pwd' in conf and conf['pwd']:
            tool_dir = {'tool_dir': PWD_TOOLING}

        # Create the Client Object
        self.client = CSClient(**conf)
        self.client.test_connection()

        # Get any optional tooling from the Config Server
        tooling_status, tarball = self.client.get_tooling()
        if tooling_status != 200:
            LOGGER.error('Get Tooling returned: %s' % tooling_status)
            raise AAErrorGetTooling('Get Tooling returned: %s' % tooling_status)
        
        self.tooling = Tooling(tarball, **tool_dir)

    def run(self):
        '''
        Main agent loop, called by main() in /usr/bin/audrey
        '''
        # 0 means don't run again
        # -1 is non zero so initial runs will happen
        config_status = -1
        provides_status = -1

        max_retry = MAX_RETRY
        loop_count = 60
        services = []

        # Process the Requires and Provides parameters until the HTTP status
        # from the get_configs and the get_params both return 200
        while config_status or provides_status:

            LOGGER.debug('Config Parameter status: ' + str(config_status))
            LOGGER.debug('Return Parameter status: ' + str(provides_status))

            # Get the Required Configs from the Config Server
            if config_status:
                config_status, configs = self.client.get_configs()

                # Configure the system with the provided Required Configs
                if config_status == 200:
                    services = Service.parse_require_config(configs, self.tooling)
                    self.tooling.invoke_tooling(services)
                    # don't do any more config status work
                    # now that the tooling has run
                    config_status = 0
                else:
                    LOGGER.info(
                        'No configuration parameters provided. status: ' + \
                        str(config_status))

            # Get the requested provides from the Config Server
            if provides_status:
                get_status = self.client.get_provides()[0]

                # Gather the values from the system for the requested provides
                if get_status == 200:
                    params_values = Provides().generate_cs_str()
                else:
                    params_values = '|&|'

                # Put the requested provides with values to the Config Server
                provides_status = self.client.put_provides(params_values)[0]
                if provides_status == 200:
                    # don't operate on params anymore, all have been provided.
                    provides_status = 0

            # Retry a number of times if 404 HTTP Not Found is returned.
            if config_status == 404 or provides_status == 404:
                LOGGER.error('404 from Config Server.')
                LOGGER.error('Required Config status: %s' % config_status)
                LOGGER.info('Return Parameter status: %s' % provides_status)

                max_retry -= 1
                if max_retry < 0:
                    raise AAError('Too many 404 Config Server responses.')

            if loop_count:
                loop_count-=1
                sleep(SLEEP_SECS)
            else:
                break
Esempio n. 3
0
class AgentV1(object):
    '''
    contains the main logic for processing
    This object is compatible with API Version 1
    '''
    def __init__(self, conf):
        '''
        conf: argparse dict
        '''
        tool_dir = {}
        if 'pwd' in conf and conf['pwd']:
            tool_dir = {'tool_dir': PWD_TOOLING}

        # Create the Client Object
        self.client = CSClient(**conf)
        self.client.test_connection()

        # Get any optional tooling from the Config Server
        tooling_status, tarball = self.client.get_tooling()
        if tooling_status != 200:
            LOGGER.error('Get Tooling returned: %s' % tooling_status)
            raise AAErrorGetTooling('Get Tooling returned: %s' %
                                    tooling_status)

        self.tooling = Tooling(tarball, **tool_dir)

    def run(self):
        '''
        Main agent loop, called by main() in /usr/bin/audrey
        '''
        # 0 means don't run again
        # -1 is non zero so initial runs will happen
        config_status = -1
        provides_status = -1

        max_retry = MAX_RETRY
        loop_count = 60
        services = []

        # Process the Requires and Provides parameters until the HTTP status
        # from the get_configs and the get_params both return 200
        while config_status or provides_status:

            LOGGER.debug('Config Parameter status: ' + str(config_status))
            LOGGER.debug('Return Parameter status: ' + str(provides_status))

            # Get the Required Configs from the Config Server
            if config_status:
                config_status, configs = self.client.get_configs()

                # Configure the system with the provided Required Configs
                if config_status == 200:
                    services = Service.parse_require_config(
                        configs, self.tooling)
                    self.tooling.invoke_tooling(services)
                    # don't do any more config status work
                    # now that the tooling has run
                    config_status = 0
                else:
                    LOGGER.info(
                        'No configuration parameters provided. status: ' + \
                        str(config_status))

            # Get the requested provides from the Config Server
            if provides_status:
                get_status = self.client.get_provides()[0]

                # Gather the values from the system for the requested provides
                if get_status == 200:
                    params_values = Provides().generate_cs_str()
                else:
                    params_values = '|&|'

                # Put the requested provides with values to the Config Server
                provides_status = self.client.put_provides(params_values)[0]
                if provides_status == 200:
                    # don't operate on params anymore, all have been provided.
                    provides_status = 0

            # Retry a number of times if 404 HTTP Not Found is returned.
            if config_status == 404 or provides_status == 404:
                LOGGER.error('404 from Config Server.')
                LOGGER.error('Required Config status: %s' % config_status)
                LOGGER.info('Return Parameter status: %s' % provides_status)

                max_retry -= 1
                if max_retry < 0:
                    raise AAError('Too many 404 Config Server responses.')

            if loop_count:
                loop_count -= 1
                sleep(SLEEP_SECS)
            else:
                break