def _exec_remote_with_pty(self, task_name, *args): stream_proto = AttachStdinProtocol() stdio.StandardIO(stream_proto) from mcloud.remote import Client, Task client = Client(host=self.host, port=self.port, settings=self.settings) try: yield txtimeout(client.connect(), 20, 'Can\'t connect to the server on host %s' % self.host) task = Task(task_name) task.on_progress = self.print_progress task.on_stdout = stream_proto.write stream_proto.listener = task.on_stdin try: yield client.call(task, *args, size=stream_proto.term.get_size()) res = yield task.wait_result() yield client.shutdown() yield sleep(0.1) defer.returnValue(res) except Exception as e: print repr(e) print('Failed to execute the task: %s' % e.message) except ConnectionRefusedError: print 'Can\'t connect to mcloud server' finally: stream_proto.stop()
def test_app_load(): def timeout(): print('Can not connect to redis!') reactor.stop() redis = yield txtimeout(txredisapi.Connection(dbid=2), 2, timeout) yield redis.flushdb() def configure(binder): binder.bind(txredisapi.Connection, redis) binder.bind(IDockerClient, DockerTwistedClient()) with inject_services(configure): app = Application({'path': os.path.realpath(os.path.dirname(__file__) + '/_files/')}, name='myapp') config = yield app.load() assert isinstance(config, YamlConfig) assert len(config.get_services()) == 1 assert config.app_name == 'myapp' service = config.get_services()['controller.myapp'] assert isinstance(service, Service) assert isinstance(service.image_builder, DockerfileImageBuilder) assert service.is_inspected()
def test_set_var_task(): def timeout(): print('Can not connect to redis!') reactor.stop() redis = yield txtimeout(txredisapi.Connection(dbid=2), 2, timeout) yield redis.flushdb() def configure(binder): binder.bind(txredisapi.Connection, redis) with inject_services(configure): ts = TaskService() r = yield ts.task_list_vars(123123) assert r == {} r = yield ts.task_set_var(123123, 'boo', '123') assert r == {'boo': 123} r = yield ts.task_list_vars(123123) assert r == {'boo': 123} r = yield ts.task_set_var(123123, 'boo2', '1234') assert r == {'boo': 123, 'boo2': 1234} r = yield ts.task_rm_var(123123, 'boo') assert r == {'boo2': 1234}
def test_set_var_task(): def timeout(): print('Can not connect to redis!') reactor.stop() redis = yield txtimeout(txredisapi.Connection(dbid=2), 2, timeout) yield redis.flushdb() def configure(binder): binder.bind(txredisapi.Connection, redis) with inject_services(configure): ts = TaskService() r = yield ts.task_list_vars(123123) assert r == {} r = yield ts.task_set_var(123123, 'boo', '123') assert r == {'boo': 123} r = yield ts.task_list_vars(123123) assert r == {'boo': 123} r = yield ts.task_set_var(123123, 'boo2', '1234') assert r == {'boo': 123, 'boo2': 1234} r = yield ts.task_rm_var(123123, 'boo') assert r == {'boo2': 1234}
def wait_for_event(self, pattern, timeout=False): d = defer.Deferred() def _on_message(channel, message): if not d.called: d.callback(message) self.on(pattern, _on_message) if not timeout == 0: return txtimeout(d, timeout, lambda: d.callback(None)) else: return d
def wait_for_event(self, pattern, timeout=False): d = defer.Deferred() def _on_message(channel, message): if not d.called: d.callback(message) self.on(pattern, _on_message) if not timeout == 0: return txtimeout(d, timeout, lambda: d.callback(None)) else: return d
def test_app_controller(): def timeout(): print('Can not connect to redis!') reactor.stop() redis = yield txtimeout(txredisapi.Connection(dbid=2), 2, timeout) yield redis.flushdb() def configure(binder): binder.bind(txredisapi.Connection, redis) with inject_services(configure): controller = ApplicationController() with pytest.raises(AppDoesNotExist): yield controller.get('foo') r = yield controller.create('foo', {'path': 'some/path'}, skip_validation=True) assert isinstance(r, Application) assert r.name == 'foo' assert r.config['path'] == 'some/path' r = yield controller.get('foo') assert isinstance(r, Application) assert r.name == 'foo' assert r.config['path'] == 'some/path' r = yield controller.create('boo', {'path': 'other/path'}, skip_validation=True) assert isinstance(r, Application) assert r.name == 'boo' assert r.config['path'] == 'other/path' mockapp = flexmock() flexmock(Application).new_instances(mockapp) mockapp.should_receive('load').with_args(need_details=True).and_return(defer.succeed({'foo': 'bar'})) r = yield controller.list() assert isinstance(r, list) assert len(r) == 2 for app in r: assert app == {'foo': 'bar'} yield controller.remove('foo') with pytest.raises(AppDoesNotExist): yield controller.get('foo')
def _remote_exec(self, task_name, *args, **kwargs): from mcloud.remote import Client, Task client = Client(host=self.host, port=self.port, settings=self.settings) self.current_client = client yield txtimeout(client.connect(), 20, 'Can\'t connect to the server on host %s' % self.host) task = Task(task_name) task.on_progress = self.print_progress self.current_task = task yield client.call(task, *args, **kwargs) res = yield task.wait_result() yield client.shutdown() yield sleep(0.1) defer.returnValue(res)
def _remote_exec(self, task_name, *args, **kwargs): from mcloud.remote import Client, Task client = Client(host=self.host, port=self.port, settings=self.settings) self.current_client = client yield txtimeout(client.connect(), 20, 'Can\'t connect to the server on host %s' % self.host) task = Task(task_name) task.on_progress = self.print_progress self.current_task = task yield client.call(task, *args, **kwargs) res = yield task.wait_result() yield client.shutdown() yield sleep(0.1) defer.returnValue(res)
def _exec_remote_with_pty(self, task_name, *args): stream_proto = AttachStdinProtocol() stdio.StandardIO(stream_proto) from mcloud.remote import Client, Task client = Client(host=self.host, port=self.port, settings=self.settings) try: yield txtimeout( client.connect(), 20, 'Can\'t connect to the server on host %s' % self.host) task = Task(task_name) task.on_progress = self.print_progress task.on_stdout = stream_proto.write stream_proto.listener = task.on_stdin try: yield client.call(task, *args, size=stream_proto.term.get_size()) res = yield task.wait_result() yield client.shutdown() yield sleep(0.1) defer.returnValue(res) except Exception as e: print repr(e) print('Failed to execute the task: %s' % e.message) except ConnectionRefusedError: print 'Can\'t connect to mcloud server' finally: stream_proto.stop()
def entry_point(): console_handler = logging.StreamHandler(stream=sys.stderr) console_handler.setFormatter( logging.Formatter( fmt='[%(asctime)s][%(levelname)s][%(name)s] %(message)s')) console_handler.setLevel(logging.ERROR) root_logger = logging.getLogger() root_logger.addHandler(console_handler) root_logger.setLevel(logging.ERROR) log.msg('Logger initialized') parser = get_argparser() args = parser.parse_args() class _McloudConfiguration(McloudConfiguration): CONF_PATHS = [args.config] settings = _McloudConfiguration.load() if args.no_ssl: settings.ssl.enabled = False def resolve_host_ip(): if 'docker0' in netifaces.interfaces(): return netifaces.ifaddresses('docker0')[ netifaces.AF_INET][0]['addr'] else: import netinfo host_ip = None for route in netinfo.get_routes(): if route['dest'] == '0.0.0.0': # default route host_ip = route['gateway'] if not host_ip: reactor.stop() print( 'ERROR: Can not get default route - can not connect to Docker' ) return host_ip @inlineCallbacks def run_server(redis): from mcloud.events import EventBus from mcloud.remote import ApiRpcServer, Server from mcloud.tasks import TaskService log.msg('Running server') eb = EventBus(redis) log.msg('Connecting event bus') yield eb.connect() log.msg('Configuring injector.') plugins_loaded = [] def my_config(binder): binder.bind(txredisapi.Connection, redis) binder.bind(EventBus, eb) binder.bind('settings', settings) binder.bind('host-ip', resolve_host_ip()) binder.bind('dns-search-suffix', settings.dns_search_suffix) binder.bind('plugins', plugins_loaded) # Configure a shared injector. inject.configure(my_config) api = inject.instance(ApiRpcServer) tasks = inject.instance(TaskService) api.tasks = tasks.collect_tasks() log.msg('Starting rpc listener on port %d' % settings.websocket_port) server = Server(port=settings.websocket_port) server.bind() # load plugins for ep in pkg_resources.iter_entry_points(group='mcloud_plugins'): try: plugin_class = ep.load() log.msg('=' * 80) log.msg('Loading plugin %s' % plugin_class) log.msg('-' * 80) yield verifyClass(IMcloudPlugin, plugin_class) plugin = plugin_class() yield plugin.setup() plugins_loaded.append(plugin) print "Loaded %s - OK" % plugin_class except Exception as e: print '!-' * 40 print e.__class__.__name__ print e print(traceback.format_exc()) print '!-' * 40 # reactor.stop() log.msg('=' * 80) log.msg('-' * 80) log.msg('All plugins loaded.') log.msg('=' * 80) deployment_controller = inject.instance(DeploymentController) yield deployment_controller.configure_docker_machine() log.msg('Started.') def timeout(): print('Can not connect to redis!') reactor.stop() print '*******' print 'Connecting redis:' print settings.redis print '*******' txtimeout( txredisapi.Connection(dbid=settings.redis.dbid, host=settings.redis.host, port=settings.redis.port, password=settings.redis.password), settings.redis.timeout, timeout).addCallback(run_server) reactor.run()
def entry_point(): console_handler = logging.StreamHandler(stream=sys.stderr) console_handler.setFormatter(logging.Formatter(fmt='[%(asctime)s][%(levelname)s][%(name)s] %(message)s')) console_handler.setLevel(logging.ERROR) root_logger = logging.getLogger() root_logger.addHandler(console_handler) root_logger.setLevel(logging.ERROR) log.msg('Logger initialized') parser = get_argparser() args = parser.parse_args() class _McloudConfiguration(McloudConfiguration): CONF_PATHS = [args.config] settings = _McloudConfiguration.load() if args.no_ssl: settings.ssl.enabled = False def resolve_host_ip(): if 'docker0' in netifaces.interfaces(): return netifaces.ifaddresses('docker0')[netifaces.AF_INET][0]['addr'] else: import netinfo host_ip = None for route in netinfo.get_routes(): if route['dest'] == '0.0.0.0': # default route host_ip = route['gateway'] if not host_ip: reactor.stop() print('ERROR: Can not get default route - can not connect to Docker') return host_ip @inlineCallbacks def run_server(redis): from mcloud.events import EventBus from mcloud.remote import ApiRpcServer, Server from mcloud.tasks import TaskService log.msg('Running server') eb = EventBus(redis) log.msg('Connecting event bus') yield eb.connect() log.msg('Configuring injector.') plugins_loaded = [] def my_config(binder): binder.bind(txredisapi.Connection, redis) binder.bind(EventBus, eb) binder.bind('settings', settings) binder.bind('host-ip', resolve_host_ip()) binder.bind('dns-search-suffix', settings.dns_search_suffix) binder.bind('plugins', plugins_loaded) # Configure a shared injector. inject.configure(my_config) api = inject.instance(ApiRpcServer) tasks = inject.instance(TaskService) api.tasks = tasks.collect_tasks() log.msg('Starting rpc listener on port %d' % settings.websocket_port) server = Server(port=settings.websocket_port) server.bind() # load plugins for ep in pkg_resources.iter_entry_points(group='mcloud_plugins'): try: plugin_class = ep.load() log.msg('=' * 80) log.msg('Loading plugin %s' % plugin_class) log.msg('-' * 80) yield verifyClass(IMcloudPlugin, plugin_class) plugin = plugin_class() yield plugin.setup() plugins_loaded.append(plugin) print "Loaded %s - OK" % plugin_class except Exception as e: print '!-' * 40 print e.__class__.__name__ print e print(traceback.format_exc()) print '!-' * 40 # reactor.stop() log.msg('=' * 80) log.msg('-' * 80) log.msg('All plugins loaded.') log.msg('=' * 80) deployment_controller = inject.instance(DeploymentController) yield deployment_controller.configure_docker_machine() log.msg('Started.') def timeout(): print('Can not connect to redis!') reactor.stop() print '*******' print 'Connecting redis:' print settings.redis print '*******' txtimeout(txredisapi.Connection( dbid=settings.redis.dbid, host=settings.redis.host, port=settings.redis.port, password=settings.redis.password ), settings.redis.timeout, timeout).addCallback(run_server) reactor.run()