Beispiel #1
0
    def __call__(self, behavior=None):

        # if behavior not in behavior_apis:
        #     raise CommandError('Unknown behavior.')

        bus.queryenv_service = new_queryenv(autoretry=False)
        api = ServiceAPI()
        # api.init_service()
        if behavior and behavior not in behavior_apis:
            raise CommandError('Unknown behavior.')
        if behavior:
            print "Reconfiguring behavior %s..." % behavior
        else:
            print "Reconfiguring..."

        behavior_params = {behavior: None} if behavior else None

        try:
            api.reconfigure(behavior_params=behavior_params, async=False)
        except (BaseException, Exception), e:
            raise CommandError('Reconfigure failed.\n%s' % e)
Beispiel #2
0
    def _init_services(self):
        logger = logging.getLogger(__name__)
        cnf = bus.cnf
        ini = cnf.rawini
        messaging_adp = ini.get('messaging', 'adapter')

        # Set base URL
        pr = urlparse(__node__['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._ensure_resolver(bus.scalr_url)

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

        logger.debug("Initialize QueryEnv client")
        queryenv = new_queryenv()

        if tuple(map(int,
                     STATE['queryenv.api_version']['version'].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('-')))

        self._init_bollard()

        if __node__['state'] != 'importing':
            lfrp = bus.queryenv_service.list_farm_role_params(
                __node__['farm_role_id'])['params']
            __node__['base'].update(lfrp.get('base'))
            if __node__['base'][
                    'union_script_executor'] and not linux.os.windows:
                # Replace script execution with a new system
                bus.cnf.rawini.remove_option('handlers', 'script_executor')
                bus.cnf.rawini.remove_option('handlers', 'chef')
                bus.cnf.rawini.set('handlers', 'union_base',
                                   'scalarizr.handlers.union.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['consumer_url']))
                consumer_url[1] = ':'.join(
                    (consumer_url[1].split(':')[0],
                     str(__node__['base']['messaging_port'])))
                params['consumer_url'] = urlunparse(consumer_url)

            params['server_id'] = __node__['server_id']
            params['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

        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)
Beispiel #3
0
 def _init_queryenv(self):
     LOG.debug('Initializing QueryEnv')
     queryenv_creds = (self.queryenv_url, self.server_id, self.crypto_file)
     self.queryenv = queryenv.new_queryenv(*queryenv_creds)
     self.queryenv.get_latest_version()  # check crypto key
     bus.queryenv_service = self.queryenv
Beispiel #4
0
 def queryenv(cls):
     if not hasattr(cls, '_queryenv'):
         cls._queryenv = new_queryenv(autoretry=False)
     return cls._queryenv
Beispiel #5
0
    def _init_services(self):
        logger = logging.getLogger(__name__)
        cnf = bus.cnf
        ini = cnf.rawini
        messaging_adp = ini.get('messaging', 'adapter')

        # Set base URL
        pr = urlparse(__node__['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 = new_queryenv()

        if tuple(map(int,
                     STATE['queryenv.api_version']['version'].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] = __node__['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)
Beispiel #6
0
 def queryenv(cls):
     if not hasattr(cls, '_queryenv'):
         cls._queryenv = new_queryenv()
     return cls._queryenv