Esempio n. 1
0
    def get_ready_behaviours(self):
        LOG.info('Detecting supported behaviors...')
        if linux.os['family'] != 'Windows':
            system_packages = pkgmgr.package_mgr().list()
            possible_behaviors = config.BuiltinBehaviours.values()
        else:
            system_packages = []
            possible_behaviors = ('base', 'chef')

        msg = (
            "Scalr built-in automation: checking for supported software.\n"
            "If installed software isn't detected, "
            "review the Scalr Wiki: https://scalr-wiki.atlassian.net/wiki/x/IoB1"
        )
        LOG.info(msg)

        ready_behaviors = list()
        for behavior in possible_behaviors:
            if behavior in ['base', 'mongodb'
                            ] or behavior not in api.api_routes.keys():
                continue
            try:
                api_cls = util.import_class(api.api_routes[behavior])
                installed = api_cls.check_software(system_packages)
                ready_behaviors.append(behavior)
                LOG.info('%s: Available. Installed version: %s', behavior,
                         installed[1])
            except (exceptions.NotFound, exceptions.UnsupportedBehavior,
                    ImportError), e:
                if isinstance(e, exceptions.UnsupportedBehavior):
                    LOG.info('%s: %s', behavior, e.args[1])
                else:
                    LOG.info('%s: %s', behavior, e)
                continue
Esempio n. 2
0
def check_supported_behaviors(*args):
    # *args to adaptee as a handler function
    if linux.os.windows:
        return
    system_packages = pkgmgr.package_mgr().list()
    for behavior in __node__['behavior']:
        if behavior in ['base', 'mongodb'
                        ] or behavior not in api.api_routes.keys():
            continue
        try:
            api_cls = util.import_class(api.api_routes[behavior])
            api_cls.check_software(system_packages)
        except exceptions.NotFound as e:
            LOG.error(e)
        except exceptions.UnsupportedBehavior as e:
            if e.args[0] == 'chef':
                # We pass it, cause a lot of roles has chef behavior without chef installed on them
                continue
            __node__['messaging'].send('RuntimeError',
                                       body={
                                           'code': 'UnsupportedBehavior',
                                           'message': str(e)
                                       })
            raise sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]
Esempio n. 3
0
    def get(self, timeout=None):
        self._task.load()
        remaining_timeout = timeout

        # lool until timeout will occurred or task state will be changed to completed or failed
        while self._task['state'] in ['pending', 'running']:
            if timeout and not remaining_timeout:
                # Timeout has occurred
                break
            if self._ready_event:
                if timeout:
                    curr_timeout = min(self.ready_event_timeout, remaining_timeout)
                else:
                    curr_timeout = self.ready_event_timeout
                self._ready_event.wait(timeout=curr_timeout)
            else:
                if timeout:
                    curr_timeout = min(self.result_poll_timeout, remaining_timeout)
                else:
                    curr_timeout = self.result_poll_timeout
                time.sleep(curr_timeout)
            if timeout:
                remaining_timeout -= curr_timeout
            self._task.load()

        if self._task['state'] not in ['completed', 'failed']:
            raise TimeoutError("Task '%s' get timeout" % self._task['task_id'])

        result = json.loads(str(self._task['result']))

        if self._task['state'] == 'failed':
            cls = util.import_class(result['exc_type'])
            exc = cls(result['exc_message'], **result['exc_data'])
            raise exc

        return result
Esempio n. 4
0
    def _init_services(self):
        logger = logging.getLogger(__name__)
        cnf = bus.cnf
        ini = cnf.rawini
        server_id = ini.get('general', 'server_id')
        queryenv_url = ini.get('general', 'queryenv_url')
        messaging_adp = ini.get('messaging', 'adapter')

        # Set base URL
        pr = urlparse(queryenv_url)
        bus.scalr_url = urlunparse((pr.scheme, pr.netloc, '', '', '', ''))
        logger.debug("Got scalr url: '%s'" % bus.scalr_url)

        if not linux.os.windows and __node__['platform'].name == 'openstack':
            self._try_resolver(bus.scalr_url)

        # Create periodical executor for background tasks (cleanup, rotate, gc, etc...)
        bus.periodical_executor = PeriodicalExecutor()

        logger.debug("Initialize QueryEnv client")
        queryenv = QueryEnvService(queryenv_url, server_id,
                                   cnf.key_path(cnf.DEFAULT_KEY), '2008-12-16')
        queryenv_latest = queryenv.get_latest_version()
        queryenv = QueryEnvService(queryenv_url, server_id,
                                   cnf.key_path(cnf.DEFAULT_KEY),
                                   queryenv_latest)

        if tuple(map(int, queryenv_latest.split('-'))) >= (2012, 7, 1):
            scalr_version = queryenv.get_global_config()['params'].get(
                'scalr.version')
            if scalr_version:
                bus.scalr_version = tuple(map(int, scalr_version.split('.')))
                version_file = cnf.private_path('.scalr-version')
                with open(version_file, 'w') as fp:
                    fp.write(scalr_version)

        bus.queryenv_service = queryenv
        bus.queryenv_version = tuple(map(int, queryenv.api_version.split('-')))

        if __node__['state'] != 'importing':
            lfrp = bus.queryenv_service.list_farm_role_params(
                __node__['farm_role_id'])['params']
            __node__['base'].update(lfrp.get('base', {}))
        ports_non_default = self._select_control_ports()

        logger.debug("Initialize messaging")
        factory = MessageServiceFactory()
        try:
            params = dict(ini.items("messaging_" + messaging_adp))
            if ports_non_default:
                consumer_url = list(
                    urlparse(params[P2pConfigOptions.CONSUMER_URL]))
                consumer_url[1] = ':'.join(
                    (consumer_url[1].split(':')[0],
                     str(__node__['base']['messaging_port'])))
                params[P2pConfigOptions.CONSUMER_URL] = urlunparse(
                    consumer_url)

            params[P2pConfigOptions.SERVER_ID] = server_id
            params[P2pConfigOptions.CRYPTO_KEY_PATH] = cnf.key_path(
                cnf.DEFAULT_KEY)

            msg_service = factory.new_service(messaging_adp, **params)
            bus.messaging_service = msg_service
        except (BaseException, Exception):
            raise ScalarizrError(
                "Cannot create messaging service adapter '%s'" %
                (messaging_adp))

        optparser = bus.optparser
        if optparser and not optparser.values.import_server and linux.os[
                'family'] != 'Windows':
            system_packages = pkgmgr.package_mgr().list()
            for behavior in __node__['behavior']:
                if behavior in ['base', 'mongodb'
                                ] or behavior not in api.api_routes.keys():
                    continue
                try:
                    api_cls = util.import_class(api.api_routes[behavior])
                    api_cls.check_software(system_packages)
                except exceptions.NotFound as e:
                    logger.error(e)
                except exceptions.UnsupportedBehavior as e:
                    if e.args[0] == 'chef':
                        # We pass it, cause a lot of roles has chef behavior without chef installed on them
                        continue
                    __node__['messaging'].send('RuntimeError',
                                               body={
                                                   'code':
                                                   'UnsupportedBehavior',
                                                   'message': str(e)
                                               })
                    raise sys.exc_info()[0], sys.exc_info()[1], sys.exc_info(
                    )[2]

        logger.debug('Initialize message handlers')
        consumer = msg_service.get_consumer()
        consumer.listeners.append(MessageListener())

        producer = msg_service.get_producer()

        def msg_meta(queue, message):
            """
            Add scalarizr version to meta
            """
            message.meta.update({
                'szr_version':
                __version__,
                'timestamp':
                os_time.utcnow().strftime("%a %d %b %Y %H:%M:%S %z")
            })

        producer.on('before_send', msg_meta)

        Storage.maintain_volume_table = True

        if not bus.api_server:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            api_port = 8010
            try:
                sock.connect(('0.0.0.0', api_port))
                api_port = 8009
                sock.close()
            except socket.error:
                pass
            STATE['global.api_port'] = api_port
            api_app = jsonrpc_http.WsgiApplication(
                rpc.RequestHandler(api.api_routes),
                cnf.key_path(cnf.DEFAULT_KEY))

            class ThreadingWSGIServer(SocketServer.ThreadingMixIn,
                                      wsgiref.simple_server.WSGIServer):
                pass

            bus.api_server = wsgiref.simple_server.make_server(
                '0.0.0.0',
                __node__['base']['api_port'],
                api_app,
                server_class=ThreadingWSGIServer)

        if ports_non_default:
            msg = msg_service.new_message(
                'HostUpdate', None, {
                    'base': {
                        'api_port': __node__['base']['api_port'],
                        'messaging_port': __node__['base']['messaging_port']
                    }
                })
            msg_service.get_producer().send(Queues.CONTROL, msg)
Esempio n. 5
0
    def _init_services(self):
        logger = logging.getLogger(__name__)
        cnf = bus.cnf; ini = cnf.rawini
        server_id = ini.get('general', 'server_id')
        queryenv_url = ini.get('general', 'queryenv_url')
        messaging_adp = ini.get('messaging', 'adapter')

        # Set base URL
        pr = urlparse(queryenv_url)
        bus.scalr_url = urlunparse((pr.scheme, pr.netloc, '', '', '', ''))
        logger.debug("Got scalr url: '%s'" % bus.scalr_url)

        if not linux.os.windows and __node__['platform'].name == 'openstack':
            self._try_resolver(bus.scalr_url)

        # Create periodical executor for background tasks (cleanup, rotate, gc, etc...)
        bus.periodical_executor = PeriodicalExecutor()

        logger.debug("Initialize QueryEnv client")
        queryenv = QueryEnvService(queryenv_url, server_id, cnf.key_path(cnf.DEFAULT_KEY), '2008-12-16')
        queryenv_latest = queryenv.get_latest_version()
        queryenv = QueryEnvService(queryenv_url, server_id, cnf.key_path(cnf.DEFAULT_KEY), queryenv_latest)

        if tuple(map(int, queryenv_latest.split('-'))) >= (2012, 7, 1):
            scalr_version = queryenv.get_global_config()['params'].get('scalr.version')
            if scalr_version:
                bus.scalr_version = tuple(map(int, scalr_version.split('.')))
                version_file = cnf.private_path('.scalr-version')
                with open(version_file, 'w') as fp:
                    fp.write(scalr_version)

        bus.queryenv_service = queryenv
        bus.queryenv_version = tuple(map(int, queryenv.api_version.split('-')))

        if __node__['state'] != 'importing':
            lfrp = bus.queryenv_service.list_farm_role_params(__node__['farm_role_id'])['params']
            __node__['base'].update(lfrp.get('base', {}))
        ports_non_default = self._select_control_ports()

        logger.debug("Initialize messaging")
        factory = MessageServiceFactory()
        try:
            params = dict(ini.items("messaging_" + messaging_adp))
            if ports_non_default:
                consumer_url = list(urlparse(params[P2pConfigOptions.CONSUMER_URL]))
                consumer_url[1] = ':'.join((consumer_url[1].split(':')[0], str(__node__['base']['messaging_port'])))
                params[P2pConfigOptions.CONSUMER_URL] = urlunparse(consumer_url)

            params[P2pConfigOptions.SERVER_ID] = server_id
            params[P2pConfigOptions.CRYPTO_KEY_PATH] = cnf.key_path(cnf.DEFAULT_KEY)

            msg_service = factory.new_service(messaging_adp, **params)
            bus.messaging_service = msg_service
        except (BaseException, Exception):
            raise ScalarizrError("Cannot create messaging service adapter '%s'" % (messaging_adp))

        optparser = bus.optparser
        if optparser and not optparser.values.import_server and linux.os['family'] != 'Windows':
            system_packages = pkgmgr.package_mgr().list()
            for behavior in __node__['behavior']:
                if behavior in ['base', 'mongodb'] or behavior not in api.api_routes.keys():
                    continue
                try:
                    api_cls = util.import_class(api.api_routes[behavior])
                    api_cls.check_software(system_packages)
                except exceptions.NotFound as e:
                    logger.error(e)
                except exceptions.UnsupportedBehavior as e:
                    if e.args[0] == 'chef':
                        # We pass it, cause a lot of roles has chef behavior without chef installed on them
                        continue
                    __node__['messaging'].send(
                        'RuntimeError',
                        body={
                            'code': 'UnsupportedBehavior',
                            'message': str(e)
                        }
                    )
                    raise sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]

        logger.debug('Initialize message handlers')
        consumer = msg_service.get_consumer()
        consumer.listeners.append(MessageListener())

        producer = msg_service.get_producer()
        def msg_meta(queue, message):
            """
            Add scalarizr version to meta
            """
            message.meta.update({
                'szr_version': __version__,
                'timestamp': os_time.utcnow().strftime("%a %d %b %Y %H:%M:%S %z")
            })
        producer.on('before_send', msg_meta)

        Storage.maintain_volume_table = True

        if not bus.api_server:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            api_port = 8010
            try:
                sock.connect(('0.0.0.0', api_port))
                api_port = 8009
                sock.close()
            except socket.error:
                pass
            STATE['global.api_port'] = api_port
            api_app = jsonrpc_http.WsgiApplication(rpc.RequestHandler(api.api_routes),
                                                cnf.key_path(cnf.DEFAULT_KEY))
            class ThreadingWSGIServer(SocketServer.ThreadingMixIn, wsgiref.simple_server.WSGIServer):
                pass
            bus.api_server = wsgiref.simple_server.make_server('0.0.0.0',
                                __node__['base']['api_port'], 
                                api_app, 
                                server_class=ThreadingWSGIServer)

        if ports_non_default:
            msg = msg_service.new_message('HostUpdate', None, {
                'base': {
                    'api_port': __node__['base']['api_port'],
                    'messaging_port': __node__['base']['messaging_port']
                }
            })
            msg_service.get_producer().send(Queues.CONTROL, msg)