def _get_defaults(manager_version): broker_config = BootstrapContext(ctx.bootstrap_context).broker_config() if not broker_config.get('broker_ssl_cert'): with open(get_broker_ssl_cert_path(), 'r') as f: broker_config['broker_ssl_cert'] = f.read() return { 'version': str(manager_version), 'broker_config': broker_config }
def test_autoscale_from_bootstrap_context(self): node_id = 'node_id' ctx = MockCloudifyContext( deployment_id='test', node_id=node_id, runtime_properties={ 'ip': '192.168.0.1' }, properties={ 'cloudify_agent': { 'user': getpass.getuser(), 'home_dir': self._get_home_dir(), 'key': KEY_FILE_PATH, 'distro': 'Ubuntu', 'distro_codename': 'trusty' } }, bootstrap_context=BootstrapContext({ 'cloudify_agent': { 'min_workers': 2, 'max_workers': 5, } }) ) conf = m(ctx) self.assertEqual(conf['min_workers'], 2) self.assertEqual(conf['max_workers'], 5) ctx = MockCloudifyContext( deployment_id='test', node_id=node_id, runtime_properties={ 'ip': '192.168.0.1' }, properties={ 'cloudify_agent': { 'user': getpass.getuser(), 'home_dir': self._get_home_dir(), 'key': KEY_FILE_PATH, 'distro': 'Ubuntu', 'distro_codename': 'trusty' } }, bootstrap_context=BootstrapContext({ 'cloudify_agent': { 'min_workers': 0, 'max_workers': 5, } }) ) conf = m(ctx) self.assertEqual(conf['min_workers'], 0) self.assertEqual(conf['max_workers'], 5)
def get_broker_configuration(agent): client = CloudifyClient( agent['manager_ip'], agent['manager_port'], ) bootstrap_context_dict = client.manager.get_context() bootstrap_context_dict = bootstrap_context_dict['context']['cloudify'] bootstrap_context = BootstrapContext(bootstrap_context_dict) attributes = bootstrap_context.broker_config( fallback_to_manager_ip=False) if not attributes.get('broker_ip'): attributes['broker_ip'] = agent['manager_ip'] return attributes
def get_broker_configuration(agent): client = get_rest_client( rest_host=agent['rest_host'], rest_port=agent['rest_port'], rest_token=agent['rest_token'], rest_tenant=agent['rest_tenant'], ssl_cert_path=agent['local_rest_cert_file'], bypass_maintenance_mode=agent['bypass_maintenance_mode']) bootstrap_context_dict = client.manager.get_context() bootstrap_context_dict = bootstrap_context_dict['context']['cloudify'] bootstrap_context = BootstrapContext(bootstrap_context_dict) attributes = bootstrap_context.broker_config() return attributes
def test_bootstrap_context_key_pair(self, *_): """tests finding a key file via bootstrap context """ name = 'test_bootstrap_context_key_pair' private_key_dir = tempfile.mkdtemp() private_key_path = '{0}/{1}.pem' \ .format(private_key_dir, name) open(private_key_path, 'w').close() cloudify_agent = MockContext() cloudify_agent['agent_key_path'] = private_key_path bootstrap_ctx = BootstrapContext({'cloudify_agent': cloudify_agent}) ctx = MockCloudifyContext(bootstrap_context=bootstrap_ctx, node_name=name) current_ctx.set(ctx=ctx) with mock.patch( 'cloudify_aws.utils.get_single_connected_node_by_type') \ as mock_get_connected: with mock.patch('cloudify_aws.ec2.instance.Instance.__init__') \ as mock_instance_init: mock_instance_init.return_value = None mock_get_connected.return_value = None output = instance.Instance()._get_private_key('') self.assertEqual(private_key_path, output)
def get_remote_context(overriding_properties=None): blueprint_id = 'mock_blueprint' node_id = 'node-{0}'.format(str(uuid.uuid4())[:5]) properties = { 'cloudify_agent': { 'user': '******', 'host': VAGRANT_MACHINE_IP, 'key': '~/.vagrant.d/insecure_private_key', 'port': 2222 } } if overriding_properties: properties.update(overriding_properties) return MockCloudifyContext(blueprint_id=blueprint_id, node_id=node_id, properties=properties, runtime_properties={'ip': '127.0.0.1'}, bootstrap_context=BootstrapContext({ 'cloudify_agent': { 'min_workers': 2, 'max_workers': 5, 'user': '******', 'remote_execution_port': 2222 } }))
def _dump_agents(tempdir): ctx.send_event('Preparing agents data') client = get_rest_client() broker_config = BootstrapContext(ctx.bootstrap_context).broker_config() defaults = { 'version': str(_get_manager_version(client)), 'broker_config': broker_config } result = {} for deployment in client.deployments.list(): deployment_result = {} for node in client.nodes.list(deployment_id=deployment.id): if _is_compute(node): node_result = {} for node_instance in client.node_instances.list( deployment_id=deployment.id, node_name=node.id): overrides = {} current = node_instance.runtime_properties.get( 'cloudify_agent', {}) for k, v in defaults.iteritems(): overrides[k] = current.get(k, v) node_result[node_instance.id] = overrides deployment_result[node.id] = node_result result[deployment.id] = deployment_result with open(os.path.join(tempdir, _AGENTS_FILE), 'w') as out: out.write(json.dumps(result))
def test_ssh_port_from_config_override_bootstrap(self): node_id = 'node_id' ctx = MockCloudifyContext( deployment_id='test', node_id=node_id, runtime_properties={ 'ip': '192.168.0.1' }, properties={ 'cloudify_agent': { 'home_dir': self._get_home_dir(), 'distro': 'Ubuntu', 'distro_codename': 'trusty', 'port': 3333 }, }, bootstrap_context=BootstrapContext({ 'cloudify_agent': { 'agent_key_path': KEY_FILE_PATH, 'user': getpass.getuser(), 'remote_execution_port': 2222 } }) ) conf = m(ctx) self.assertEqual(conf['port'], 3333)
def __init__(self, node_id=None, node_name=None, blueprint_id=None, deployment_id=None, execution_id=None, properties=None, runtime_properties=None, relationships=None, capabilities=None, related=None, source=None, target=None, operation=None, resources=None, tenant=None, rest_token=None, provider_context=None, bootstrap_context=None): tenant = tenant or {} super(MockCloudifyContext, self).__init__({ 'blueprint_id': blueprint_id, 'deployment_id': deployment_id, 'node_id': node_id, 'node_name': node_name, 'node_properties': properties, 'operation': operation, 'tenant': tenant, 'rest_token': rest_token }) self._node_id = node_id self._node_name = node_name self._deployment_id = deployment_id self._execution_id = execution_id self._properties = properties or {} self._runtime_properties = \ runtime_properties if runtime_properties is not None else {} self._resources = resources or {} self._source = source self._target = target if capabilities and not isinstance(capabilities, ContextCapabilities): raise ValueError("MockCloudifyContext(capabilities=?) must be " "instance of ContextCapabilities, not {0}".format( capabilities)) self._related = related self._provider_context = provider_context or {} self._bootstrap_context = bootstrap_context or BootstrapContext({}) self._mock_context_logger = setup_logger('mock-context-logger') if node_id: self._instance = MockNodeInstanceContext( id=node_id, runtime_properties=self._runtime_properties, relationships=relationships) self._capabilities = capabilities or ContextCapabilities( self._endpoint, self._instance) self._node = MockNodeContext(node_name, properties) if self._source is None and self._target: self._source = MockContext({'instance': None, 'node': None})
def mock_context(agent_ssl_cert, agent_properties=None, agent_runtime_properties=None, agent_context=None, **kwargs): agent_context = agent_context or {} agent_properties = agent_properties or {} agent_runtime_properties = agent_runtime_properties or {} context = { 'node_id': 'test_node', 'node_name': 'test_node', 'blueprint_id': 'test_blueprint', 'deployment_id': 'test_deployment', 'execution_id': 'test_execution', 'rest_token': 'test_token', 'properties': { 'cloudify_agent': agent_properties }, 'runtime_properties': { 'cloudify_agent': agent_runtime_properties }, 'managers': [{ 'networks': { 'default': '127.0.0.1' }, 'ca_cert_content': agent_ssl_cert.DUMMY_CERT, 'hostname': 'cloudify' }], 'brokers': [{ 'networks': { 'default': '127.0.0.1' }, 'ca_cert_content': agent_ssl_cert.DUMMY_CERT }], 'bootstrap_context': BootstrapContext(bootstrap_context={'cloudify_agent': agent_context}), 'tenant': { 'name': 'default_tenant', 'rabbitmq_username': '******', 'rabbitmq_password': '******', 'rabbitmq_vhost': '/' } } context.update(kwargs) context = MockCloudifyContext(**context) context.installer = MagicMock() context._get_current_object = lambda: context return context
def get_broker_configuration(agent): client = get_rest_client( security_enabled=agent['security_enabled'], rest_host=agent['rest_host'], rest_protocol=agent['rest_protocol'], rest_port=agent['rest_port'], rest_username=agent['rest_username'], rest_password=agent['rest_password'], rest_token=agent['rest_token'], verify_rest_certificate=agent['verify_rest_certificate'], ssl_cert_path=agent['local_rest_cert_file'], bypass_maintenance_mode=agent['bypass_maintenance_mode']) bootstrap_context_dict = client.manager.get_context() bootstrap_context_dict = bootstrap_context_dict['context']['cloudify'] bootstrap_context = BootstrapContext(bootstrap_context_dict) attributes = bootstrap_context.broker_config() return attributes
def mock_ctx(self, test_vars, test_id, test_deployment_id, runtime_properties=None): ctx = MockContext() ctx.node = MockNodeContext(properties=test_vars) ctx.bootstrap_context = BootstrapContext( common_test.BOOTSTRAP_CONTEXTS_WITHOUT_PREFIX[0]) ctx.instance = MockNodeInstanceContext( id=test_id, runtime_properties=runtime_properties or {}) ctx.deployment = mock.Mock() ctx.deployment.id = test_deployment_id ctx.type = NODE_INSTANCE ctx.logger = mock.Mock() return ctx
def test_network_no_prefix(self): ctx = self._setup_ctx('network') for pctx in common_test.BOOTSTRAP_CONTEXTS_WITHOUT_PREFIX: ctx._bootstrap_context = BootstrapContext(pctx) self.neutron_mock.create_network.reset_mock() self.neutron_mock.create_network.return_value = { 'network': { 'id': 'network_id', } } neutron_plugin.network.create(ctx) calls = self.neutron_mock.create_network.mock_calls self.assertEquals(len(calls), 1) # Exactly one network created # Indexes into call[]: # 0 - the only call # 1 - regular arguments # 0 - first argument arg = calls[0][1][0] self.assertEquals(arg['network']['name'], 'network_name', "Failed with context: " + str(pctx))
def get_remote_context(): node_id = 'node-{0}'.format(str(uuid.uuid4())[:5]) return MockCloudifyContext(node_id=node_id, properties={ 'cloudify_agent': { 'user': '******', 'host': VAGRANT_MACHINE_IP, 'key': '~/.vagrant.d/insecure_private_key', 'port': 2222 }, }, runtime_properties={'ip': '127.0.0.1'}, bootstrap_context=BootstrapContext({ 'cloudify_agent': { 'min_workers': 2, 'max_workers': 5, 'user': '******', 'remote_execution_port': 2222 } }))
def mock_context(agent_properties=None, agent_runtime_properties=None, agent_context=None): if not agent_properties: agent_properties = {} if not agent_runtime_properties: agent_runtime_properties = {} context = MockCloudifyContext( node_id='test_node', node_name='test_node', blueprint_id='test_blueprint', deployment_id='test_deployment', execution_id='test_execution', properties={'cloudify_agent': agent_properties}, runtime_properties={'cloudify_agent': agent_runtime_properties}, bootstrap_context=BootstrapContext( bootstrap_context={'cloudify_agent': agent_context})) setattr(context, 'installer', MagicMock()) return context
def test_bad_key_path(self): node_id = 'node_id' ctx = MockCloudifyContext( deployment_id='test', node_id=node_id, runtime_properties={ 'ip': '192.168.0.1' }, properties={ 'cloudify_agent': { 'distro': 'Ubuntu', }, }, bootstrap_context=BootstrapContext({ 'cloudify_agent': { 'agent_key_path': 'bad_key_path', 'user': getpass.getuser(), } }) ) self.assertRaises(NonRecoverableError, m, ctx)
def mock_context(agent_properties=None, agent_runtime_properties=None, agent_context=None): agent_context = agent_context or {} agent_properties = agent_properties or {} agent_runtime_properties = agent_runtime_properties or {} context = MockCloudifyContext( node_id='test_node', node_name='test_node', blueprint_id='test_blueprint', deployment_id='test_deployment', execution_id='test_execution', rest_token='test_token', properties={'cloudify_agent': agent_properties}, runtime_properties={'cloudify_agent': agent_runtime_properties}, bootstrap_context=BootstrapContext( bootstrap_context={'cloudify_agent': agent_context})) context.installer = MagicMock() context._get_current_object = lambda: context return context
def test_ssh_port_default(self): node_id = 'node_id' ctx = MockCloudifyContext( deployment_id='test', node_id=node_id, runtime_properties={ 'ip': '192.168.0.1' }, properties={ 'cloudify_agent': { 'distro': 'Ubuntu', }, }, bootstrap_context=BootstrapContext({ 'cloudify_agent': { 'agent_key_path': KEY_FILE_PATH, 'user': getpass.getuser(), } }) ) conf = m(ctx) self.assertEqual(conf['port'], 22)
def test_network_no_prefix(self, neutron_mock): ctx = self._setup_ctx('network') for pctx in common_test.BOOTSTRAP_CONTEXTS_WITHOUT_PREFIX: ctx._bootstrap_context = BootstrapContext(pctx) neutron_mock.create_network.reset_mock() neutron_mock.create_network.return_value = { 'network': { 'id': 'network_id', 'name': 'network_name', } } with mock.patch('openstack_plugin_common._find_context_in_kw', return_value=self.ctx): neutron_plugin.network.create(neutron_client=neutron_mock, ctx=self.ctx, args={}) neutron_mock.create_network.assert_called_once_with( {'network': { 'name': 'network_name', 'admin_state_up': True }})
def test_user_from_bootstrap_context(self): node_id = 'node_id' ctx = MockCloudifyContext( deployment_id='test', node_id=node_id, runtime_properties={ 'ip': '192.168.0.1' }, properties={ 'cloudify_agent': { 'home_dir': self._get_home_dir(), 'distro': 'Ubuntu', 'distro_codename': 'trusty' }, }, bootstrap_context=BootstrapContext({ 'cloudify_agent': { 'agent_key_path': KEY_FILE_PATH, 'user': getpass.getuser() } }) ) conf = m(ctx) self.assertEqual(conf['user'], getpass.getuser())