diff options
-rwxr-xr-x | bin/cloud-init | 48 | ||||
-rw-r--r-- | cloudinit/util.py | 4 |
2 files changed, 29 insertions, 23 deletions
diff --git a/bin/cloud-init b/bin/cloud-init index dc480901..78f8600d 100755 --- a/bin/cloud-init +++ b/bin/cloud-init @@ -419,7 +419,7 @@ def main_single(name, args): return 0 -def status_wrapper(args, data_d=None, link_d=None): +def status_wrapper(name, args, data_d=None, link_d=None): if data_d is None: data_d = os.path.normpath("/var/lib/cloud/data") if link_d is None: @@ -434,13 +434,15 @@ def status_wrapper(args, data_d=None, link_d=None): (_name, functor) = args.action - if args.name: + if name == "init": if args.local: mode = "init-local" else: mode = "init" - elif args.name == "modules": + elif name == "modules": mode = "modules-%s" % args.mode + else: + raise ValueError("unknown name: %s" % name) modes = ('init', 'init-local', 'modules-config', 'modules-final') @@ -457,40 +459,40 @@ def status_wrapper(args, data_d=None, link_d=None): if status is None: nullstatus = { 'errors': [], - 'state': None, 'start': None, 'end': None, } status = {'v1': {}} - for mode in modes: - status['v1'][mode] = nullstatus.copy() + for m in modes: + status['v1'][m] = nullstatus.copy() status['v1']['datasource'] = None - status['stage'] = mode v1 = status['v1'] + v1['stage'] = mode v1[mode]['start'] = time.time() util.write_file(status_path, json.dumps(status)) - util.sym_link(os.path.relpath(os.path.status_path, link_d), status_link) + util.sym_link(os.path.relpath(status_path, link_d), status_link, + force=True) try: - ret = functor(args) + ret = functor(name, args) + if mode in ('init', 'init-local'): + (datasource, errors) = ret + if datasource is not None: + v1['datasource'] = datasource + v1[mode]['errors'] = errors + else: + errors = ret + v1[mode]['errors'] = ret + except Exception as e: v1[mode]['errors'] = [str(e)] v1[mode]['finished'] = time.time() v1['stage'] = None - if mode in ('init', 'init-local'): - (datasource, errors) = ret - if datasource is not None: - v1['datasource'] = datasource - v1[mode]['errors'] = errors - else: - errors = ret - v1[mode]['errors'] = ret - util.write_file(status_path, json.dumps(status)) if mode == "modules-final": @@ -503,8 +505,8 @@ def status_wrapper(args, data_d=None, link_d=None): finished = {'datasource': v1['datasource'], 'errors': errors} util.write_file(result_path, json.dumps(finished)) - util.sym_link(os.path.relpath(os.path.result_path, link_d), - result_link) + util.sym_link(os.path.relpath(result_path, link_d), result_link, + force=True) return len(v1[mode]['errors']) @@ -540,7 +542,7 @@ def main(): default=False) # This is used so that we can know which action is selected + # the functor to use to run this subcommand - parser_init.set_defaults(action=('init', status_wrapper)) + parser_init.set_defaults(action=('init', main_init)) # These settings are used for the 'config' and 'final' stages parser_mod = subparsers.add_parser('modules', @@ -551,7 +553,7 @@ def main(): "to use (default: %(default)s)"), default='config', choices=('init', 'config', 'final')) - parser_mod.set_defaults(action=('modules', status_wrapper)) + parser_mod.set_defaults(action=('modules', main_modules)) # These settings are used when you want to query information # stored in the cloud-init data objects/directories/files @@ -592,6 +594,8 @@ def main(): signal_handler.attach_handlers() (name, functor) = args.action + if name in ("modules", "init"): + functor = status_wrapper return util.log_time(logfunc=LOG.debug, msg="cloud-init mode '%s'" % name, get_uptime=True, func=functor, args=(name, args)) diff --git a/cloudinit/util.py b/cloudinit/util.py index 87b0c853..06039ee2 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -1395,8 +1395,10 @@ def get_builtin_cfg(): return obj_copy.deepcopy(CFG_BUILTIN) -def sym_link(source, link): +def sym_link(source, link, force=False): LOG.debug("Creating symbolic link from %r => %r", link, source) + if force and os.path.exists(link): + del_file(link) os.symlink(source, link) |