def _destroy_containers(self): container_map = yield get_containers(self._qualified_name) for container_name in container_map: container = LXCContainer(container_name, None, None, None) if container_map[container.container_name]: yield container.stop() yield container.destroy()
def __init__(self, unit_name, juju_home): super(UnitContainerDeployment, self).__init__(unit_name, juju_home) self._unit_namespace = os.environ.get("JUJU_UNIT_NS") self._juju_origin = os.environ.get("JUJU_ORIGIN") assert self._unit_namespace is not None, "Required unit ns not found" self.pid_file = None self.container = LXCContainer(self.container_name, None, None, None)
def test_lxc_container(self): self.addCleanup(self.clean_container, DEFAULT_CONTAINER) customize_log = self.makeFile() c = LXCContainer( DEFAULT_CONTAINER, "dsa...", "ppa", customize_log=customize_log) running = yield c.is_running() self.assertFalse(running) self.assertFalse(c.is_constructed()) # verify we can't run a non-constructed container failure = c.run() yield self.assertFailure(failure, LXCError) yield c.create() self.assertFalse(running) self.assertTrue(c.is_constructed()) yield c.run() running = yield c.is_running() self.assertTrue(running) self.assertTrue(c.is_constructed()) output = _lxc_ls() self.assertIn(DEFAULT_CONTAINER, output) # verify we have a path into the container self.assertTrue(os.path.exists(c.rootfs)) self.assertTrue(c.is_constructed()) self.verify_container(c, "dsa...", "ppa") # verify that we are in containers containers = yield get_containers(None) self.assertEqual(containers[DEFAULT_CONTAINER], True) # tear it down yield c.destroy() running = yield c.is_running() self.assertFalse(running) containers = yield get_containers(None) self.assertNotIn(DEFAULT_CONTAINER, containers) # Verify the customize log file. self.assertTrue(os.path.exists(customize_log)) # and its gone output = _lxc_ls() self.assertNotIn(DEFAULT_CONTAINER, output)
def test_container_clone(self): self.addCleanup(self.clean_container, DEFAULT_CONTAINER) self.addCleanup(self.clean_container, DEFAULT_CONTAINER + "_child") master_container = LXCContainer(DEFAULT_CONTAINER, origin="ppa", public_key="dsa...") # verify that we cannot clone an unconstructed container failure = master_container.clone("test_lxc_fail") yield self.assertFailure(failure, LXCError) yield master_container.create() # Clone a child container from the template child_name = DEFAULT_CONTAINER + "_child" c = yield master_container.clone(child_name) self.assertEqual(c.container_name, child_name) running = yield c.is_running() self.assertFalse(running) yield c.run() running = yield c.is_running() self.assertTrue(running) output = _lxc_ls() self.assertIn(DEFAULT_CONTAINER, output) self.verify_container(c, "dsa...", "ppa") # verify that we are in containers containers = yield get_containers(None) self.assertEqual(containers[child_name], True) # tear it down yield c.destroy() running = yield c.is_running() self.assertFalse(running) containers = yield get_containers(None) self.assertNotIn(child_name, containers) # and its gone output = _lxc_ls() self.assertNotIn(child_name, output) yield master_container.destroy()
def test_get_container(self): rootfs = self.makeDir() container = LXCContainer(self.unit_name, None, None, None) mock_deploy = self.mocker.patch(self.unit_deploy) mock_deploy._get_master_template(ANY, ANY, ANY) self.mocker.result(container) mock_container = self.mocker.patch(container) mock_container.clone(ANY) self.mocker.result(container) self.mocker.replay() container, rootfs = yield self.unit_deploy._get_container( "0", "127.0.0.1:2181", None, "dsa...") output = self.output.getvalue() self.assertIn("Container created for %s" % self.unit_deploy.unit_name, output)
def _get_master_template(self, machine_id, zookeeper_hosts, public_key): container_template_name = "%s-%s-template" % (self._unit_namespace, machine_id) master_template = LXCContainer(container_template_name, origin=self._juju_origin, public_key=public_key) # Debug log for the customize script, customize is only run on master. customize_log_path = os.path.join(self.juju_home, "units", "master-customize.log") master_template.customize_log = customize_log_path if not master_template.is_constructed(): log.debug("Creating master container...") yield master_template.create() log.debug("Created master container %s" % container_template_name) # it wasn't constructed and we couldn't construct it if not master_template.is_constructed(): raise LXCError("Unable to create master container") returnValue(master_template)
def test_start(self): container = LXCContainer(self.unit_name, None, None, None) rootfs = self.makeDir() env = dict(os.environ) env["JUJU_PUBLIC_KEY"] = "dsa ..." self.change_environment(**env) mock_deploy = self.mocker.patch(self.unit_deploy) # this minimally validates that we are also called with the # expect public key mock_deploy._get_container(ANY, ANY, ANY, env["JUJU_PUBLIC_KEY"]) self.mocker.result((container, rootfs)) mock_container = self.mocker.patch(container) mock_container.run() self.mocker.replay() self.unit_deploy.directory = rootfs os.makedirs(os.path.join(rootfs, "etc", "init")) yield self.unit_deploy.start("0", "127.0.1.1:2181", self.bundle) # Verify the upstart job upstart_agent_name = "%s-unit-agent.conf" % (self.unit_name.replace( "/", "-")) content = open(os.path.join(rootfs, "etc", "init", upstart_agent_name)).read() job = self.get_normalized(content) self.assertIn('JUJU_ZOOKEEPER="127.0.1.1:2181"', job) self.assertIn('JUJU_MACHINE_ID="0"', job) self.assertIn('JUJU_UNIT_NAME="riak/0"', job) # Verify the symlink exists self.assertTrue( os.path.lexists( os.path.join(self.unit_deploy.juju_home, "units", self.unit_deploy.unit_path_name, "unit.log"))) # Verify the charm is on disk. self.assertTrue( os.path.exists( os.path.join(self.unit_deploy.directory, "var", "lib", "juju", "units", self.unit_deploy.unit_path_name, "charm", "metadata.yaml"))) # Verify the directory structure in the unit. self.assertTrue( os.path.exists( os.path.join(self.unit_deploy.directory, "var", "lib", "juju", "state"))) self.assertTrue( os.path.exists( os.path.join(self.unit_deploy.directory, "var", "log", "juju"))) # Verify log output output = self.output.getvalue() self.assertIn("Charm extracted into container", output) self.assertIn("Started container for %s" % self.unit_deploy.unit_name, output)