diff options
author | Scott Moser <smoser@ubuntu.com> | 2016-06-15 21:09:45 -0400 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2016-06-15 21:09:45 -0400 |
commit | 333130a7ac9df5e698d82dec2cb6e8e44ba1432b (patch) | |
tree | 0dd495cdf7b063527a147d2f0b6e9f6be5442f75 | |
parent | 4f989cc595775ab6b06fac604218d77910f3e4f4 (diff) | |
parent | 412d5d34c424da0f5c291e9f9e706098c541017c (diff) | |
download | vyos-cloud-init-333130a7ac9df5e698d82dec2cb6e8e44ba1432b.tar.gz vyos-cloud-init-333130a7ac9df5e698d82dec2cb6e8e44ba1432b.zip |
merge with trunk
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | cloudinit/cmd/__init__.py | 21 | ||||
-rwxr-xr-x | cloudinit/cmd/main.py (renamed from bin/cloud-init) | 46 | ||||
-rw-r--r-- | cloudinit/net/eni.py | 4 | ||||
-rw-r--r-- | cloudinit/sources/DataSourceGCE.py | 2 | ||||
-rwxr-xr-x | setup.py | 8 | ||||
-rw-r--r-- | tests/unittests/test_cli.py | 43 | ||||
-rwxr-xr-x | tools/run-pep8 | 5 | ||||
-rwxr-xr-x | tools/run-pyflakes | 2 |
9 files changed, 68 insertions, 64 deletions
@@ -122,7 +122,6 @@ - ConfigDrive: improved support for networking information from a network_data.json or older interfaces formated network_config. - Change missing Cheetah log warning to debug [Andrew Jorgensen] - - Remove trailing dot from GCE metadata URL (LP: #1581200) [Phil Roche] 0.7.6: - open 0.7.6 diff --git a/cloudinit/cmd/__init__.py b/cloudinit/cmd/__init__.py new file mode 100644 index 00000000..da124641 --- /dev/null +++ b/cloudinit/cmd/__init__.py @@ -0,0 +1,21 @@ +# vi: ts=4 expandtab +# +# Copyright (C) 2012 Canonical Ltd. +# Copyright (C) 2012 Hewlett-Packard Development Company, L.P. +# Copyright (C) 2012 Yahoo! Inc. +# +# Author: Scott Moser <scott.moser@canonical.com> +# Author: Juerg Haefliger <juerg.haefliger@hp.com> +# Author: Joshua Harlow <harlowja@yahoo-inc.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. diff --git a/bin/cloud-init b/cloudinit/cmd/main.py index 21c3a684..63621c1d 100755 --- a/bin/cloud-init +++ b/cloudinit/cmd/main.py @@ -25,19 +25,12 @@ import argparse import json import os import sys -import time import tempfile +import time import traceback -# This is more just for running from the bin folder so that -# cloud-init binary can find the cloudinit module -possible_topdir = os.path.normpath(os.path.join(os.path.abspath( - sys.argv[0]), os.pardir, os.pardir)) -if os.path.exists(os.path.join(possible_topdir, "cloudinit", "__init__.py")): - sys.path.insert(0, possible_topdir) - from cloudinit import patcher -patcher.patch() +patcher.patch() # noqa from cloudinit import log as logging from cloudinit import netinfo @@ -46,9 +39,10 @@ from cloudinit import sources from cloudinit import stages from cloudinit import templater from cloudinit import util +from cloudinit import version + from cloudinit import reporting from cloudinit.reporting import events -from cloudinit import version from cloudinit.settings import (PER_INSTANCE, PER_ALWAYS, PER_ONCE, CLOUD_CONFIG) @@ -188,7 +182,7 @@ def main_init(name, args): LOG.debug("Closing stdin") util.close_stdin() (outfmt, errfmt) = util.fixup_output(init.cfg, name) - except: + except Exception: util.logexc(LOG, "Failed to setup output redirection!") print_exc("Failed to setup output redirection!") if args.debug: @@ -325,7 +319,7 @@ def main_init(name, args): if outfmt_orig != outfmt or errfmt_orig != errfmt: LOG.warn("Stdout, stderr changing to (%s, %s)", outfmt, errfmt) (outfmt, errfmt) = util.fixup_output(mods.cfg, name) - except: + except Exception: util.logexc(LOG, "Failed to re-adjust output redirection!") logging.setupLogging(mods.cfg) @@ -367,7 +361,7 @@ def main_modules(action_name, args): LOG.debug("Closing stdin") util.close_stdin() util.fixup_output(mods.cfg, name) - except: + except Exception: util.logexc(LOG, "Failed to setup output redirection!") if args.debug: # Reset so that all the debug handlers are closed out @@ -430,7 +424,7 @@ def main_single(name, args): LOG.debug("Closing stdin") util.close_stdin() util.fixup_output(mods.cfg, None) - except: + except Exception: util.logexc(LOG, "Failed to setup output redirection!") if args.debug: # Reset so that all the debug handlers are closed out @@ -510,7 +504,7 @@ def status_wrapper(name, args, data_d=None, link_d=None): else: try: status = json.loads(util.load_file(status_path)) - except: + except Exception: pass if status is None: @@ -569,8 +563,12 @@ def status_wrapper(name, args, data_d=None, link_d=None): return len(v1[mode]['errors']) -def main(): - parser = argparse.ArgumentParser() +def main(sysv_args=None): + if sysv_args is not None: + parser = argparse.ArgumentParser(prog=sysv_args[0]) + sysv_args = sysv_args[1:] + else: + parser = argparse.ArgumentParser() # Top level args parser.add_argument('--version', '-v', action='version', @@ -646,7 +644,12 @@ def main(): ' pass to this module')) parser_single.set_defaults(action=('single', main_single)) - args = parser.parse_args() + args = parser.parse_args(args=sysv_args) + + try: + (name, functor) = args.action + except AttributeError: + parser.error('too few arguments') # Setup basic logging to start (until reinitialized) # iff in debug mode... @@ -656,9 +659,6 @@ def main(): # Setup signal handlers before running signal_handler.attach_handlers() - if not hasattr(args, 'action'): - parser.error('too few arguments') - (name, functor) = args.action if name in ("modules", "init"): functor = status_wrapper @@ -683,7 +683,3 @@ def main(): return util.log_time( logfunc=LOG.debug, msg="cloud-init mode '%s'" % name, get_uptime=True, func=functor, args=(name, args)) - - -if __name__ == '__main__': - sys.exit(main()) diff --git a/cloudinit/net/eni.py b/cloudinit/net/eni.py index a695f5ed..5a90eb32 100644 --- a/cloudinit/net/eni.py +++ b/cloudinit/net/eni.py @@ -391,8 +391,8 @@ class Renderer(object): iface['control'] = subnet.get('control', 'auto') if iface['mode'].endswith('6'): iface['inet'] += '6' - elif (iface['mode'] == 'static' - and ":" in subnet['address']): + elif (iface['mode'] == 'static' and + ":" in subnet['address']): iface['inet'] += '6' if iface['mode'].startswith('dhcp'): iface['mode'] = 'dhcp' diff --git a/cloudinit/sources/DataSourceGCE.py b/cloudinit/sources/DataSourceGCE.py index c660a350..9234d1f8 100644 --- a/cloudinit/sources/DataSourceGCE.py +++ b/cloudinit/sources/DataSourceGCE.py @@ -25,7 +25,7 @@ from cloudinit import util LOG = logging.getLogger(__name__) BUILTIN_DS_CONFIG = { - 'metadata_url': 'http://metadata.google.internal/computeMetadata/v1/' + 'metadata_url': 'http://metadata.google.internal./computeMetadata/v1/' } REQUIRED_FIELDS = ('instance-id', 'availability-zone', 'local-hostname') @@ -204,10 +204,14 @@ setuptools.setup( author_email='scott.moser@canonical.com', url='http://launchpad.net/cloud-init/', packages=setuptools.find_packages(exclude=['tests']), - scripts=['bin/cloud-init', - 'tools/cloud-init-per'], + scripts=['tools/cloud-init-per'], license='GPLv3', data_files=data_files, install_requires=requirements, cmdclass=cmdclass, + entry_points={ + 'console_scripts': [ + 'cloud-init = cloudinit.cmd.main:main' + ], + } ) diff --git a/tests/unittests/test_cli.py b/tests/unittests/test_cli.py index 163ce64c..5fa252f7 100644 --- a/tests/unittests/test_cli.py +++ b/tests/unittests/test_cli.py @@ -1,12 +1,10 @@ -import imp -import os import six -import sys from . import helpers as test_helpers -mock = test_helpers.mock -BIN_CLOUDINIT = "bin/cloud-init" +from cloudinit.cmd import main as cli + +mock = test_helpers.mock class TestCLI(test_helpers.FilesystemMockingTestCase): @@ -15,35 +13,22 @@ class TestCLI(test_helpers.FilesystemMockingTestCase): super(TestCLI, self).setUp() self.stderr = six.StringIO() self.patchStdoutAndStderr(stderr=self.stderr) - self.sys_exit = mock.MagicMock() - self.patched_funcs.enter_context( - mock.patch.object(sys, 'exit', self.sys_exit)) - - def _call_main(self): - self.patched_funcs.enter_context( - mock.patch.object(sys, 'argv', ['cloud-init'])) - cli = imp.load_module( - 'cli', open(BIN_CLOUDINIT), '', ('', 'r', imp.PY_SOURCE)) + + def _call_main(self, sysv_args=None): + if not sysv_args: + sysv_args = ['cloud-init'] try: - return cli.main() - except Exception: - pass + return cli.main(sysv_args=sysv_args) + except SystemExit as e: + return e.code - @test_helpers.skipIf(not os.path.isfile(BIN_CLOUDINIT), "no bin/cloudinit") def test_no_arguments_shows_usage(self): - self._call_main() - self.assertIn('usage: cloud-init', self.stderr.getvalue()) - - @test_helpers.skipIf(not os.path.isfile(BIN_CLOUDINIT), "no bin/cloudinit") - def test_no_arguments_exits_2(self): exit_code = self._call_main() - if self.sys_exit.call_count: - self.assertEqual(mock.call(2), self.sys_exit.call_args) - else: - self.assertEqual(2, exit_code) + self.assertIn('usage: cloud-init', self.stderr.getvalue()) + self.assertEqual(2, exit_code) - @test_helpers.skipIf(not os.path.isfile(BIN_CLOUDINIT), "no bin/cloudinit") def test_no_arguments_shows_error_message(self): - self._call_main() + exit_code = self._call_main() self.assertIn('cloud-init: error: too few arguments', self.stderr.getvalue()) + self.assertEqual(2, exit_code) diff --git a/tools/run-pep8 b/tools/run-pep8 index 086400fc..4bd0bbfb 100755 --- a/tools/run-pep8 +++ b/tools/run-pep8 @@ -1,8 +1,7 @@ #!/bin/bash -pycheck_dirs=( "cloudinit/" "bin/" "tests/" "tools/" ) -# FIXME: cloud-init modifies sys module path, pep8 does not like -# bin_files=( "bin/cloud-init" ) +pycheck_dirs=( "cloudinit/" "tests/" "tools/" ) + CR=" " [ "$1" = "-v" ] && { verbose="$1"; shift; } || verbose="" diff --git a/tools/run-pyflakes b/tools/run-pyflakes index 4bea17f4..b3759a94 100755 --- a/tools/run-pyflakes +++ b/tools/run-pyflakes @@ -3,7 +3,7 @@ PYTHON_VERSION=${PYTHON_VERSION:-2} CR=" " -pycheck_dirs=( "cloudinit/" "bin/" "tests/" "tools/" ) +pycheck_dirs=( "cloudinit/" "tests/" "tools/" ) set -f if [ $# -eq 0 ]; then |