def constraints_set(env_config, environment, service_name, constraint_strs): """ Machine constraints allow you to pick the hardware to which your services will be deployed. Examples: $ juju set-constraints --service-name mysql mem=8G cpu=4 $ juju set-constraints instance-type=t1.micro Available constraints vary by provider type, and will be ignored if not understood by the current environment's provider. The current set of available constraints across all providers is: On Amazon EC2: * arch (CPU architecture: i386/amd64/arm; amd64 by default) * cpu (processing power in Amazon ECU; 1 by default) * mem (memory in [MGT]iB; 512M by default) * instance-type (unset by default) * ec2-zone (unset by default) On Orchestra: * orchestra-classes (unset by default) On MAAS: * maas-name (unset by default) Service settings, if specified, will override environment settings, which will in turn override the juju defaults of mem=512M, cpu=1, arch=amd64. New constraints set on an entity will completely replace that entity's pre-existing constraints. To override an environment constraint with the juju default when setting service constraints, just specify "name=" (rather than just not specifying the constraint at all, which will cause it to inherit the environment's value). To entirely unset a constraint, specify "name=any". """ provider = environment.get_machine_provider() constraint_set = yield provider.get_constraint_set() constraints = constraint_set.parse(constraint_strs) client = yield provider.connect() try: yield legacy.check_constraints(client, constraint_strs) yield sync_environment_state(client, env_config, environment.name) if service_name is None: esm = EnvironmentStateManager(client) yield esm.set_constraints(constraints) else: ssm = ServiceStateManager(client) service = yield ssm.get_service_state(service_name) yield service.set_constraints(constraints) finally: yield client.close()
def setUp(self): yield super(ConstraintsGetTest, self).setUp() env_constraints = dummy_cs.parse(["mem=1024"]) esm = EnvironmentStateManager(self.client) yield esm.set_constraints(env_constraints) self.expect_env = { "arch": "amd64", "cpu": 1.0, "mem": 1024.0, "provider-type": "dummy", "ubuntu-series": None} service_constraints = dummy_cs.parse(["cpu=10"]) service = yield self.add_service_from_charm( "mysql", constraints=service_constraints) # unit will snapshot the state of service when added unit = yield service.add_unit_state() self.expect_unit = { "arch": "amd64", "cpu": 10.0, "mem": 1024.0, "provider-type": "dummy", "ubuntu-series": "series"} # machine gets its own constraints machine_constraints = dummy_cs.parse(["cpu=15", "mem=8G"]) machine = yield self.add_machine_state( constraints=machine_constraints.with_series("series")) self.expect_machine = { "arch": "amd64", "cpu": 15.0, "mem": 8192.0, "provider-type": "dummy", "ubuntu-series": "series"} yield unit.assign_to_machine(machine) # service gets new constraints, leaves unit untouched yield service.set_constraints(dummy_cs.parse(["mem=16G"])) self.expect_service = { "arch": "amd64", "cpu": 1.0, "mem": 16384.0, "provider-type": "dummy", "ubuntu-series": "series"} self.log = self.capture_logging() self.stdout = self.capture_stream("stdout") self.finished = self.setup_cli_reactor() self.setup_exit(0) self.mocker.replay()
class EnvironmentStateManagerTest(StateTestBase, EnvironmentsConfigTestBase): @inlineCallbacks def setUp(self): yield super(EnvironmentStateManagerTest, self).setUp() self.environment_state_manager = EnvironmentStateManager(self.client) self.write_config(SAMPLE_ENV) self.config.load() @inlineCallbacks def tearDown(self): yield super(EnvironmentStateManagerTest, self).tearDown() @inlineCallbacks def test_set_config_state(self): """ The simplest thing the manager can do is serialize a given environment and save it in zookeeper. """ manager = self.environment_state_manager yield manager.set_config_state(self.config, "myfirstenv") serialized = self.config.serialize("myfirstenv") content, stat = yield self.client.get("/environment") self.assertEquals(yaml.load(content), yaml.load(serialized)) @inlineCallbacks def test_set_config_state_replaces_environment(self): """ Setting the environment should also work with an existing environment. """ yield self.client.create("/environment", "Replace me!") manager = self.environment_state_manager yield manager.set_config_state(self.config, "myfirstenv") serialized = self.config.serialize("myfirstenv") content, stat = yield self.client.get("/environment") self.assertEquals(yaml.load(content), yaml.load(serialized)) @inlineCallbacks def test_get_config(self): """ We can also retrieve a loaded config from the environment. """ manager = self.environment_state_manager yield manager.set_config_state(self.config, "myfirstenv") config = yield manager.get_config() serialized1 = self.config.serialize("myfirstenv") serialized2 = config.serialize("myfirstenv") self.assertEquals(yaml.load(serialized1), yaml.load(serialized2)) def test_get_config_when_missing(self): """ get_config should blow up politely if the environment config is missing. """ d = self.environment_state_manager.get_config() return self.assertFailure(d, EnvironmentStateNotFound) @inlineCallbacks def test_get_in_legacy_environment_no(self): yield self.push_default_config() esm = self.environment_state_manager legacy = yield esm.get_in_legacy_environment() self.assertEquals(legacy, False) @inlineCallbacks def test_get_in_legacy_environment_yes(self): yield self.push_default_config() self.client.delete("/constraints") esm = self.environment_state_manager legacy = yield esm.get_in_legacy_environment() self.assertEquals(legacy, True) def test_get_constraint_set_no_env(self): d = self.environment_state_manager.get_constraint_set() return self.assertFailure(d, EnvironmentStateNotFound) @inlineCallbacks def test_get_constraint_set(self): yield self.push_default_config() cs = yield self.environment_state_manager.get_constraint_set() constraints = cs.parse(["arch=any", "cpu=10"]) self.assertEquals(constraints, { "ubuntu-series": None, "provider-type": "dummy", "arch": None, "cpu": 10.0, "mem": 512.0}) def test_get_constraints_no_env(self): d = self.environment_state_manager.get_constraints() return self.assertFailure(d, EnvironmentStateNotFound) @inlineCallbacks def test_get_constraints_env_with_no_node(self): yield self.push_default_config() self.client.delete("/constraints") constraints = yield self.environment_state_manager.get_constraints() self.assertEquals(constraints.data, {}) @inlineCallbacks def test_set_constraints(self): yield self.push_default_config() constraints = dummy_cs.parse(["cpu=any", "mem=32T"]) yield self.environment_state_manager.set_constraints(constraints) roundtrip = yield self.environment_state_manager.get_constraints() self.assertEquals(roundtrip, constraints)
def push_env_constraints(self, *constraint_strs): esm = EnvironmentStateManager(self.client) constraint_set = yield esm.get_constraint_set() yield esm.set_constraints(constraint_set.parse(constraint_strs))