def configure_environment(self): """The provisioning agent configure its environment on start or change. The environment contains the configuration th agent needs to interact with its machine provider, in order to do its work. This configuration data is deployed lazily over an encrypted connection upon first usage. The agent waits for this data to exist before completing its startup. """ try: get_d, watch_d = self.client.get_and_watch("/environment") environment_data, stat = yield get_d watch_d.addCallback(self._on_environment_changed) except NoNodeException: # Wait till the environment node appears. play twisted gymnastics exists_d, watch_d = self.client.exists_and_watch("/environment") stat = yield exists_d if stat: environment = yield self.configure_environment() else: watch_d.addCallback( lambda result: self.configure_environment()) if not stat: environment = yield watch_d returnValue(environment) config = EnvironmentsConfig() config.parse(environment_data) returnValue(config.get_default())
def get_config(self): try: content, stat = yield self._client.get("/environment") except zookeeper.NoNodeException: raise EnvironmentStateNotFound() config = EnvironmentsConfig() config.parse(content) returnValue(config)
def test_serialize_custom_variables_outside_environment(self): """Serializing captures custom variables out of the environment.""" data = yaml.load(SAMPLE_ENV) data["default"] = "myfirstenv" self.write_config(yaml.dump(data)) self.config.load() serialized = self.config.serialize() config = EnvironmentsConfig() config.parse(serialized) environment = config.get_default() self.assertEqual(environment.name, "myfirstenv")
def test_get_environment(self): config = { "environments": {"firstenv": {"type": "dummy"}}} self.write_config(dump(config)) env_config = EnvironmentsConfig() env_config.load_or_write_sample() options = FakeOptions() options.environment = None options.environments = env_config environment = get_environment(options) self.assertEqual(environment.name, "firstenv")
class EnvironmentsConfigTestBase(TestCase): @inlineCallbacks def setUp(self): yield super(EnvironmentsConfigTestBase, self).setUp() release_path = os.path.join(DATA_DIR, "lsb-release") self.patch(environment, "LSB_RELEASE_PATH", release_path) self.old_home = os.environ.get("HOME") self.tmp_home = self.makeDir() self.change_environment(HOME=self.tmp_home, PATH=os.environ["PATH"]) self.default_path = os.path.join(self.tmp_home, ".juju/environments.yaml") self.other_path = os.path.join(self.tmp_home, ".juju/other-environments.yaml") self.config = EnvironmentsConfig() def write_config(self, config_text, other_path=False): if other_path: path = self.other_path else: path = self.default_path parent_name = os.path.dirname(path) if not os.path.exists(parent_name): os.makedirs(parent_name) with open(path, "w") as file: file.write(config_text) # The following methods expect to be called *after* a subclass has set # self.client. def push_config(self, name, config): self.write_config(yaml.dump(config)) self.config.load() esm = EnvironmentStateManager(self.client) return esm.set_config_state(self.config, name) @inlineCallbacks 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)) @inlineCallbacks def push_default_config(self, with_constraints=True): config = { "environments": {"firstenv": { "type": "dummy", "storage-directory": self.makeDir()}}} yield self.push_config("firstenv", config) if with_constraints: yield self.push_env_constraints()
def test_load_serialized_environment(self): """ Serialize an environment, and then load it again via an EnvironmentsConfig. """ self.write_config(SAMPLE_ENV) self.config.load() serialized = self.config.serialize("myfirstenv") config = EnvironmentsConfig() config.parse(serialized) self.assertTrue( isinstance(config.get("myfirstenv"), Environment)) self.assertFalse( isinstance(config.get("mysecondenv"), Environment))
def test_get_nonexistant_environment(self): config = { "environments": {"firstenv": {"type": "dummy"}, "secondenv": {"type": "dummy"}}} self.write_config(dump(config)) env_config = EnvironmentsConfig() env_config.load_or_write_sample() options = FakeOptions() options.environment = "volcano" options.environments = env_config error = self.assertRaises( EnvironmentsConfigError, get_environment, options) self.assertIn("Invalid environment 'volcano'", str(error))
def test_get_environment_default_with_multiple(self): config = { "environments": {"firstenv": {"type": "dummy"}, "secondenv": {"type": "dummy"}}} self.write_config(dump(config)) env_config = EnvironmentsConfig() env_config.load_or_write_sample() options = FakeOptions() options.environment = None options.environments = env_config error = self.assertRaises( EnvironmentsConfigError, get_environment, options) self.assertIn( "There are multiple environments and no explicit default", str(error))
def main(args): """The main end user cli command for juju users.""" env_config = EnvironmentsConfig() env_config.load_or_write_sample() parser = setup_parser( subcommands=SUBCOMMANDS, prog="juju", description="juju cloud orchestration admin") parser.set_defaults(environments=env_config, log=log) # Some commands, like juju ssh, do a further parse on options by # delegating to another command (such as the underlying ssh). But # first need to parse nonstrictly all args to even determine what # command is even being used. options, extra = parser.parse_known_args(args) if options.command.passthrough: try: # Augments options with subparser specific passthrough parsing options.command.passthrough(options, extra) except ParseError, e: options.parser.error(str(e))
def setUp(self): yield super(EnvironmentsConfigTestBase, self).setUp() release_path = os.path.join(DATA_DIR, "lsb-release") self.patch(environment, "LSB_RELEASE_PATH", release_path) self.old_home = os.environ.get("HOME") self.tmp_home = self.makeDir() self.change_environment(HOME=self.tmp_home, PATH=os.environ["PATH"]) self.default_path = os.path.join(self.tmp_home, ".juju/environments.yaml") self.other_path = os.path.join(self.tmp_home, ".juju/other-environments.yaml") self.config = EnvironmentsConfig()
def get_test_environment_config(self): sample_config = SAMPLE_ENV % self.makeDir() config = EnvironmentsConfig() config.parse(sample_config) return config
def get_serialized_environment(self): config = EnvironmentsConfig() config.parse(SAMPLE_ENV) return config.serialize("myfirstenv")
def main(args): """The main end user cli command for juju users.""" parser = setup_parser( subcommands=SUBCOMMANDS, prog="juju", description="juju cloud orchestration admin") # Some commands, like juju ssh, do a further parse on options by # delegating to another command (such as the underlying ssh). But # first need to parse nonstrictly all args to even determine what # command is even being used. options, extra = parser.parse_known_args(args) if options.command.passthrough: try: # Augments options with subparser specific passthrough parsing options.command.passthrough(options, extra) except ParseError, e: options.parser.error(str(e)) else: # Otherwise, do be strict options = parser.parse_args(args) env_config = EnvironmentsConfig() env_config.load_or_write_sample() options.environments = env_config options.log = log setup_logging(options) options.command(options)