def destroy_environment(self): """Shutdown the machine environment. """ # Stop all the containers log.info("Destroying unit containers...") yield self._destroy_containers() # Stop the machine agent log.debug("Stopping machine agent...") agent = ManagedMachineAgent(self._qualified_name) yield agent.stop() # Stop the storage server log.debug("Stopping storage server...") storage_server = StorageServer(self._qualified_name) yield storage_server.stop() # Stop zookeeper log.debug("Stopping zookeeper...") zookeeper_dir = os.path.join(self._directory, "zookeeper") zookeeper = Zookeeper(zookeeper_dir, None) yield zookeeper.stop() # Clean out local state yield self.save_state(False) # Don't stop the network since we're using the default libvirt log.debug("Environment destroyed.")
def destroy_environment(self): """Shutdown the machine environment. """ # Stop all the containers log.info("Destroying unit containers...") yield self._destroy_containers() # Stop the machine agent log.debug("Stopping machine agent...") pid_file = os.path.join(self._directory, "machine-agent.pid") agent = ManagedMachineAgent(pid_file) yield agent.stop() # Stop the storage server log.debug("Stopping storage server...") pid_file = os.path.join(self._directory, "storage-server.pid") storage_server = StorageServer(pid_file) yield storage_server.stop() # Stop zookeeper log.debug("Stopping zookeeper...") zookeeper_dir = os.path.join(self._directory, "zookeeper") zookeeper = Zookeeper(zookeeper_dir, None) yield zookeeper.stop() # Clean out local state yield self.save_state(False) # Don't stop the network since we're using the default libvirt log.debug("Environment destroyed.")
def setUp(self): self._storage_path = self.makeDir() self._storage = LocalStorage(self._storage_path) self._log_path = self.makeFile() self._pid_path = self.makeFile() self._port = get_open_port() self._server = StorageServer(self._pid_path, self._storage_path, "localhost", get_open_port(), self._log_path)
def setUp(self): yield super(WebFileStorageTest, self).setUp() self._storage_path = self.makeDir() self._logfile = self.makeFile() self._storage = LocalStorage(self._storage_path) self._port = get_open_port() self._server = StorageServer( "ns1", self._storage_path, "localhost", self._port, self._logfile)
def setUp(self): self._storage_path = self.makeDir() self._storage = LocalStorage(self._storage_path) self._log_path = self.makeFile() self._pid_path = self.makeFile() self._port = get_open_port() self._server = StorageServer( self._pid_path, self._storage_path, "localhost", get_open_port(), self._log_path)
def test_capture_errors(self): self._port = get_open_port() self._server = StorageServer( "borken", self._storage_path, "lol borken", self._port, self._logfile) d = self._server.start() e = yield self.assertFailure(d, ServiceError) self.assertTrue(str(e).startswith( "Failed to start job juju-borken-file-storage; got output:\n")) self.assertIn("Wrong number of arguments", str(e)) yield self._server.stop()
class WebFileStorageTest(TestCase): def setUp(self): self._storage_path = self.makeDir() self._storage = LocalStorage(self._storage_path) self._log_path = self.makeFile() self._pid_path = self.makeFile() self._port = get_open_port() self._server = StorageServer( self._pid_path, self._storage_path, "localhost", get_open_port(), self._log_path) @inlineCallbacks def test_start_stop(self): yield self._storage.put("abc", StringIO("hello world")) yield self._server.start() storage_url = yield self._storage.get_url("abc") contents = yield getPage(storage_url) self.assertEqual("hello world", contents) self._server.stop() # Stopping multiple times is fine. self._server.stop() def test_start_missing_args(self): server = StorageServer(self._pid_path) return self.assertFailure(server.start(), AssertionError) def test_start_invalid_directory(self): os.rmdir(self._storage_path) return self.assertFailure(self._server.start(), AssertionError) def test_stop_missing_pid(self): server = StorageServer(self._pid_path) server.stop()
class WebFileStorageTest(TestCase): def setUp(self): self._storage_path = self.makeDir() self._storage = LocalStorage(self._storage_path) self._log_path = self.makeFile() self._pid_path = self.makeFile() self._port = get_open_port() self._server = StorageServer(self._pid_path, self._storage_path, "localhost", get_open_port(), self._log_path) @inlineCallbacks def test_start_stop(self): yield self._storage.put("abc", StringIO("hello world")) yield self._server.start() storage_url = yield self._storage.get_url("abc") contents = yield getPage(storage_url) self.assertEqual("hello world", contents) self._server.stop() # Stopping multiple times is fine. self._server.stop() def test_start_missing_args(self): server = StorageServer(self._pid_path) return self.assertFailure(server.start(), AssertionError) def test_start_invalid_directory(self): os.rmdir(self._storage_path) return self.assertFailure(self._server.start(), AssertionError) def test_stop_missing_pid(self): server = StorageServer(self._pid_path) server.stop()
def test_namespacing(self): alt_storage_path = self.makeDir() alt_storage = LocalStorage(alt_storage_path) yield alt_storage.put("some-path", StringIO("alternative")) yield self._storage.put("some-path", StringIO("original")) alt_server = StorageServer( "ns2", alt_storage_path, "localhost", get_open_port(), self.makeFile()) yield alt_server.start() yield self._server.start() yield self.wait_for_server(alt_server) yield self.wait_for_server(self._server) alt_contents = yield getPage( (yield alt_storage.get_url("some-path"))) self.assertEquals(alt_contents, "alternative") orig_contents = yield getPage( (yield self._storage.get_url("some-path"))) self.assertEquals(orig_contents, "original") yield alt_server.stop() yield self._server.stop()
def test_stop_missing_pid(self): server = StorageServer(self._pid_path) server.stop()
def test_start_missing_args(self): server = StorageServer(self._pid_path) return self.assertFailure(server.start(), AssertionError)
class WebFileStorageTest(TestCase): @inlineCallbacks def setUp(self): yield super(WebFileStorageTest, self).setUp() self._storage_path = self.makeDir() self._logfile = self.makeFile() self._storage = LocalStorage(self._storage_path) self._port = get_open_port() self._server = StorageServer( "ns1", self._storage_path, "localhost", self._port, self._logfile) @inlineCallbacks def wait_for_server(self, server): while not (yield server.is_serving()): yield self.sleep(0.1) def test_start_missing_args(self): server = StorageServer("ns1", self._storage_path) return self.assertFailure(server.start(), AssertionError) def test_start_invalid_directory(self): os.rmdir(self._storage_path) return self.assertFailure(self._server.start(), AssertionError) @inlineCallbacks def test_upstart(self): subprocess_calls = [] def intercept_args(args, **kwargs): subprocess_calls.append(args) self.assertEquals(args[0], "sudo") if args[1] == "cp": return real_check_call(args[1:], **kwargs) return 0 real_check_call = self.patch(subprocess, "check_call", intercept_args) init_dir = self.makeDir() self.patch(UpstartService, "init_dir", init_dir) # Mock out the repeated checking for unstable pid, after an initial # stop/waiting to induce the actual start getProcessOutput = self.mocker.replace( "twisted.internet.utils.getProcessOutput") getProcessOutput("/sbin/status", ["juju-ns1-file-storage"]) self.mocker.result(succeed("stop/waiting")) for _ in range(5): getProcessOutput("/sbin/status", ["juju-ns1-file-storage"]) self.mocker.result(succeed("start/running 123")) self.mocker.replay() try: os.remove("/tmp/juju-ns1-file-storage.output") except OSError: pass # just make sure it's not there, so the .start() # doesn't insert a spurious rm yield self._server.start() chmod = subprocess_calls[1] conf_dest = os.path.join(init_dir, "juju-ns1-file-storage.conf") self.assertEquals(chmod, ("sudo", "chmod", "644", conf_dest)) start = subprocess_calls[-1] self.assertEquals( start, ("sudo", "/sbin/start", "juju-ns1-file-storage")) with open(conf_dest) as f: for line in f: if line.startswith("env"): self.fail("didn't expect any special environment") if line.startswith("exec"): exec_ = line[5:].strip() expect_exec = ( "twistd --nodaemon --uid %s --gid %s --logfile %s --pidfile= -d " "%s web --port tcp:%s:interface=localhost --path %s >> " "/tmp/juju-ns1-file-storage.output 2>&1" % (os.getuid(), os.getgid(), self._logfile, self._storage_path, self._port, self._storage_path)) self.assertEquals(exec_, expect_exec) @uses_sudo @inlineCallbacks def test_start_stop(self): yield self._storage.put("abc", StringIO("hello world")) yield self._server.start() # Starting multiple times is fine. yield self._server.start() storage_url = yield self._storage.get_url("abc") # It might not have started actually accepting connections yet... yield self.wait_for_server(self._server) self.assertEqual((yield getPage(storage_url)), "hello world") # Check that it can be killed by the current user (ie, is not running # as root) and still comes back up old_pid = yield self._server.get_pid() os.kill(old_pid, signal.SIGKILL) new_pid = yield self._server.get_pid() self.assertNotEquals(old_pid, new_pid) # Give it a moment to actually start serving again yield self.wait_for_server(self._server) self.assertEqual((yield getPage(storage_url)), "hello world") yield self._server.stop() # Stopping multiple times is fine too. yield self._server.stop() @uses_sudo @inlineCallbacks def test_namespacing(self): alt_storage_path = self.makeDir() alt_storage = LocalStorage(alt_storage_path) yield alt_storage.put("some-path", StringIO("alternative")) yield self._storage.put("some-path", StringIO("original")) alt_server = StorageServer( "ns2", alt_storage_path, "localhost", get_open_port(), self.makeFile()) yield alt_server.start() yield self._server.start() yield self.wait_for_server(alt_server) yield self.wait_for_server(self._server) alt_contents = yield getPage( (yield alt_storage.get_url("some-path"))) self.assertEquals(alt_contents, "alternative") orig_contents = yield getPage( (yield self._storage.get_url("some-path"))) self.assertEquals(orig_contents, "original") yield alt_server.stop() yield self._server.stop() @uses_sudo @inlineCallbacks def test_capture_errors(self): self._port = get_open_port() self._server = StorageServer( "borken", self._storage_path, "lol borken", self._port, self._logfile) d = self._server.start() e = yield self.assertFailure(d, ServiceError) self.assertTrue(str(e).startswith( "Failed to start job juju-borken-file-storage; got output:\n")) self.assertIn("Wrong number of arguments", str(e)) yield self._server.stop()
def bootstrap(self): """Bootstrap a local development environment. """ # Check for existing environment state = yield self.load_state() if state is not False: raise ProviderError("Environment already bootstrapped") # Check for required packages log.info("Checking for required packages...") missing = check_packages(*REQUIRED_PACKAGES) if missing: raise ProviderError("Missing packages %s" % ( ", ".join(sorted(list(missing))))) # Get/create directory for zookeeper and files zookeeper_dir = os.path.join(self._directory, "zookeeper") if not os.path.exists(zookeeper_dir): os.makedirs(zookeeper_dir) # Start networking, and get an open port. log.info("Starting networking...") net = Network("default", subnet=122) # Start is a noop if its already started, which it is by default, # per libvirt-bin package installation yield net.start() net_attributes = yield net.get_attributes() port = get_open_port(net_attributes["ip"]["address"]) # Start zookeeper log.info("Starting zookeeper...") # Run zookeeper as the current user, unless we're being run as root # in which case run zookeeper as the 'zookeeper' user. zookeeper_user = None if os.geteuid() == 0: zookeeper_user = "******" zookeeper = Zookeeper(zookeeper_dir, port=port, host=net_attributes["ip"]["address"], user=zookeeper_user, group=zookeeper_user) yield zookeeper.start() # Starting provider storage server log.info("Starting storage server...") storage_server = StorageServer( pid_file=os.path.join(self._directory, "storage-server.pid"), storage_dir=os.path.join(self._directory, "files"), host=net_attributes["ip"]["address"], port=get_open_port(net_attributes["ip"]["address"]), log_file=os.path.join(self._directory, "storage-server.log")) yield storage_server.start() # Save the zookeeper start to provider storage. yield self.save_state({"zookeeper-instances": ["local"], "zookeeper-address": zookeeper.address}) # Initialize the zookeeper state log.debug("Initializing state...") admin_identity = make_identity( "admin:%s" % self.config["admin-secret"]) client = ZookeeperClient(zookeeper.address) yield client.connect() hierarchy = StateHierarchy(client, admin_identity, "local", "local") yield hierarchy.initialize() # Store user credentials from the running user try: public_key = get_user_authorized_keys(self.config) public_key = public_key.strip() except LookupError, e: raise ProviderError(str(e))