From 5c90ccfd74d28d817fab50079f0233cd8a555d3a Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Fri, 29 Aug 2014 23:18:44 +0100 Subject: Clarify that multiple images can be deployed at once by `morph deploy` --- morphlib/plugins/deploy_plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'morphlib/plugins/deploy_plugin.py') diff --git a/morphlib/plugins/deploy_plugin.py b/morphlib/plugins/deploy_plugin.py index 61b8145e..b55478ad 100644 --- a/morphlib/plugins/deploy_plugin.py +++ b/morphlib/plugins/deploy_plugin.py @@ -43,7 +43,7 @@ class DeployPlugin(cliapp.Plugin): pass def deploy(self, args): - '''Deploy a built system image. + '''Deploy a built system image or a set of images. Command line arguments: -- cgit v1.2.1 From 864dff6aa72615a85e3c7180cfd65cacb66867da Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Fri, 29 Aug 2014 23:18:45 +0100 Subject: Add `morph upgrade` command, deprecate `morph deploy --upgrade` The arguments to `morph deploy` can get quite long, any way we can make it shorter and clearer is useful. We can also avoid having the strange --no-upgrade flag in future. --- morphlib/plugins/deploy_plugin.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'morphlib/plugins/deploy_plugin.py') diff --git a/morphlib/plugins/deploy_plugin.py b/morphlib/plugins/deploy_plugin.py index b55478ad..f3e43fa8 100644 --- a/morphlib/plugins/deploy_plugin.py +++ b/morphlib/plugins/deploy_plugin.py @@ -32,12 +32,15 @@ class DeployPlugin(cliapp.Plugin): group_deploy = 'Deploy Options' self.app.settings.boolean(['upgrade'], 'specify that you want to upgrade an ' - 'existing cluster of systems rather than do ' - 'an initial deployment', + 'existing cluster. Deprecated: use the ' + '`morph upgrade` command instead', group=group_deploy) self.app.add_subcommand( 'deploy', self.deploy, arg_synopsis='CLUSTER [DEPLOYMENT...] [SYSTEM.KEY=VALUE]') + self.app.add_subcommand( + 'upgrade', self.upgrade, + arg_synopsis='CLUSTER [DEPLOYMENT...] [SYSTEM.KEY=VALUE]') def disable(self): pass @@ -300,8 +303,8 @@ class DeployPlugin(cliapp.Plugin): if cluster_morphology['kind'] != 'cluster': raise cliapp.AppException( - "Error: morph deploy is only supported for cluster" - " morphologies.") + "Error: morph deployment commands are only supported for " + "cluster morphologies.") # parse the rest of the args all_subsystems = set() @@ -432,6 +435,25 @@ class DeployPlugin(cliapp.Plugin): finally: self.app.status_prefix = old_status_prefix + def upgrade(self, args): + '''Upgrade an existing set of instances using built images. + + See `morph help deploy` for documentation. + + ''' + + if not args: + raise cliapp.AppException( + 'Too few arguments to upgrade command (see `morph help ' + 'deploy`)') + + if self.app.settings['upgrade']: + raise cliapp.AppException( + 'Running `morph upgrade --upgrade` does not make sense.') + + self.app.settings['upgrade'] = True + self.deploy(args) + def check_deploy(self, root_repo_dir, ref, deployment_type, location, env): # Run optional write check extension. These are separate from the write # extension because it may be several minutes before the write -- cgit v1.2.1 From 6fba43005c52930ce0175df18122264c2f193448 Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Fri, 29 Aug 2014 23:18:46 +0100 Subject: deploy: Allow extensions to write to Morph log file This is achieved by passing it the write end of a pipe, so that the extension has somewhere to write debug messages without clobbering either stdout or stderr. Previously deployment extensions could only display status messages on stdout, which is good for telling the user what is happening but is not useful when trying to do post-mortem debugging, when more information is usually required. This uses the asyncore asynchronous event framework, which is rather specific to sockets, but can be made to work with file descriptors, and has the advantage that it's part of the python standard library. It relies on polling file descriptors, but there's a trick with pipes to use them as a notification that a process has exited: 1. Ensure the write-end of the pipe is only open in the process you want to know when it exits 2. Make sure the pipe's FDs are set in non-blocking read/write mode 3. Call select or poll on the read-end of the file descriptor 4. If select/poll says you can read from that FD, but you get an EOF, then the process has closed it, and if you know it doesn't do that in normal operation, then the process has terminated. It took a few weird hacks to get the async_chat module to unregister its event handlers on EOF, but the result is an event loop that is asleep until the kernel tells it that it has to do something. --- morphlib/plugins/deploy_plugin.py | 53 ++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 23 deletions(-) (limited to 'morphlib/plugins/deploy_plugin.py') diff --git a/morphlib/plugins/deploy_plugin.py b/morphlib/plugins/deploy_plugin.py index f3e43fa8..a80079fa 100644 --- a/morphlib/plugins/deploy_plugin.py +++ b/morphlib/plugins/deploy_plugin.py @@ -14,18 +14,19 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -import cliapp -import contextlib import json +import logging import os import shutil -import stat +import sys import tarfile import tempfile import uuid +import cliapp import morphlib + class DeployPlugin(cliapp.Plugin): def enable(self): @@ -540,33 +541,39 @@ class DeployPlugin(cliapp.Plugin): self.app.status(msg='Cleaning up') shutil.rmtree(deploy_private_tempdir) + def _report_extension_stdout(self, line): + self.app.status(msg=line.replace('%s', '%%')) + def _report_extension_stderr(self, error_list): + def cb(line): + error_list.append(line) + sys.stderr.write('%s\n' % line) + return cb + def _report_extension_logger(self, name, kind): + return lambda line: logging.debug('%s%s: %s', name, kind, line) def _run_extension(self, gd, name, kind, args, env): '''Run an extension. - + The ``kind`` should be either ``.configure`` or ``.write``, depending on the kind of extension that is sought. - + The extension is found either in the git repository of the system morphology (repo, ref), or with the Morph code. - + ''' - with morphlib.extensions.get_extension_filename( - name, kind) as ext_filename: - self.app.status(msg='Running extension %(name)s%(kind)s', - name=name, kind=kind) - self.app.runcmd( - [ext_filename] + args, - ['sh', - '-c', - 'while read l; do echo `date "+%F %T"` "$1$l"; done', - '-', - '%s[%s]' % (self.app.status_prefix, name + kind)], - cwd=gd.dirname, env=env, stdout=None, stderr=None) - - def _is_executable(self, filename): - st = os.stat(filename) - mask = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH - return (stat.S_IMODE(st.st_mode) & mask) != 0 + error_list = [] + with morphlib.extensions.get_extension_filename(name, kind) as fn: + ext = morphlib.extensions.ExtensionSubprocess( + report_stdout=self._report_extension_stdout, + report_stderr=self._report_extension_stderr(error_list), + report_logger=self._report_extension_logger(name, kind), + ) + returncode = ext.run(fn, args, env=env, cwd=gd.dirname) + if returncode == 0: + logging.info('%s%s succeeded', name, kind) + else: + message = '%s%s failed with code %s: %s' % ( + name, kind, returncode, '\n'.join(error_list)) + raise cliapp.AppException(message) def create_metadata(self, system_artifact, root_repo_dir, deployment_type, location, env): -- cgit v1.2.1