From ec7bb1a98f1294864c991ae0393a0818f7a9bab3 Mon Sep 17 00:00:00 2001 From: "Alexandre Martins (a.k.a Maral Afris)" Date: Wed, 22 Feb 2017 21:18:23 +0100 Subject: [PATCH 1/3] Add blocking create --- machine/machine.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/machine/machine.py b/machine/machine.py index 27bc937..76e0418 100644 --- a/machine/machine.py +++ b/machine/machine.py @@ -38,6 +38,24 @@ def _run(self, cmd, raise_error=True): raise RuntimeError("cmd returned error %s: %s" % (error_code, stderr.decode('utf-8').strip())) return stdout.decode('utf-8'), stderr.decode('utf-8'), error_code + def _run_blocking(self, cmd, raise_error=True): + """ + Run a docker-machine command, optionally raise error if error code != 0 + Args: + cmd (List[str]): a list of the docker-machine command with the arguments to run + raise_error (bool): raise an exception on non 0 return code + Returns: + tuple: stdout, stderr, error_code + """ + cmd = [self.path] + cmd + p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE) + stdout, stderr = p.communicate() + error_code = p.wait() + + if raise_error and error_code: + raise RuntimeError("cmd returned error %s: %s" % (error_code, stderr.decode('utf-8').strip())) + return stdout.decode('utf-8'), stderr.decode('utf-8'), error_code + def _match(self, cmd, regexp): """ Run cmd and match regular expression regexp on it, return results. @@ -68,6 +86,26 @@ def version(self): match = self._match(cmd, regexp) return match.group(1) + def create(self, name, driver='virtualbox', blocking=True): + """ + Create a docker machine using the provided name and driver + NOTE: This takes a loooooong time + + Args: + name (str): the name to give to the machine (must be unique) + driver: the driver to use to create the machine + blocking (bool): should wait for completion before exiting + + Returns: + int: error code from the run + """ + cmd = ['create', '--driver', driver, name] + if blocking: + stdout, stderr, errorcode = self._run_blocking(cmd) + else: + stdout, stderr, errorcode = self._run(cmd) + return errorcode + def config(self, machine="default"): """ Returns the docker configuration for the given machine. From 5b7eadc09d64777dc816d3195b4b96f9bcc55fb6 Mon Sep 17 00:00:00 2001 From: "Alexandre Martins (a.k.a Maral Afris)" Date: Mon, 27 Feb 2017 12:33:55 +0100 Subject: [PATCH 2/3] Add ssh command run --- machine/machine.py | 15 +++++++++++++++ tests/test_commands.py | 6 ++++++ 2 files changed, 21 insertions(+) diff --git a/machine/machine.py b/machine/machine.py index 76e0418..a0c56e1 100644 --- a/machine/machine.py +++ b/machine/machine.py @@ -364,3 +364,18 @@ def scp(self, source, destination, recursive=False): cmd = ["scp"] + r + [source, destination] stdout, _, _ = self._run(cmd) return stdout.split() + + def ssh(self, machine, cmd): + """ + Run a command on a machine through docker-machine ssh + + Args: + machine (str): machine name + cmd (str): command to run + + Returns: + List[str]: output of the ssh command + """ + ssh_cmd = ['ssh', machine, cmd] + stdout, _, _ = self._run(ssh_cmd) + return stdout.split() diff --git a/tests/test_commands.py b/tests/test_commands.py index 80ea1e0..f9f74f0 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -81,6 +81,12 @@ def test_scp(self): destination = "%s:." % TEST_MACHINE self.machine.scp(source, destination) + def test_ssh_echo(self): + self.assertTrue(self.machine.exists(machine=TEST_MACHINE)) + ret = self.machine.ssh(TEST_MACHINE, 'echo \"Hello\"') + if ret != ['Hello']: + raise RuntimeError + def test_start(self): self.machine.stop(machine=TEST_MACHINE) self.machine.start(machine=TEST_MACHINE) From 5d06aa84916d3273ecbde3025bfed3e131746855 Mon Sep 17 00:00:00 2001 From: "Alexandre Martins (a.k.a Maral Afris)" Date: Mon, 27 Feb 2017 15:05:00 +0100 Subject: [PATCH 3/3] Change test after review --- tests/test_commands.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index f9f74f0..08400fa 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -33,7 +33,7 @@ def tearDownClass(cls): cls.machine.rm(machine=TEMPORARY_MACHINE) except RuntimeError: pass - + def setUp(self): if not self.machine.status(machine=TEST_MACHINE): self.machine.start(machine=TEST_MACHINE) @@ -83,9 +83,7 @@ def test_scp(self): def test_ssh_echo(self): self.assertTrue(self.machine.exists(machine=TEST_MACHINE)) - ret = self.machine.ssh(TEST_MACHINE, 'echo \"Hello\"') - if ret != ['Hello']: - raise RuntimeError + self.assertEqual(self.machine.ssh(TEST_MACHINE, 'echo \"Hi\"'), ['Hi']) def test_start(self): self.machine.stop(machine=TEST_MACHINE)