From fbc01e536cdd023a954703fdc3bbe76df1d9ed1a Mon Sep 17 00:00:00 2001 From: Ben Arblaster Date: Sun, 18 Jan 2015 01:39:35 +0000 Subject: freebsd: add pkg support --- cloudinit/distros/freebsd.py | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'cloudinit/distros') diff --git a/cloudinit/distros/freebsd.py b/cloudinit/distros/freebsd.py index f1b4a256..10f9e7e0 100644 --- a/cloudinit/distros/freebsd.py +++ b/cloudinit/distros/freebsd.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import os + from StringIO import StringIO import re @@ -29,6 +31,8 @@ from cloudinit import util from cloudinit.distros import net_util from cloudinit.distros.parsers.resolv_conf import ResolvConf +from cloudinit.settings import PER_INSTANCE + LOG = logging.getLogger(__name__) @@ -367,13 +371,34 @@ class Distro(distros.Distro): LOG.warn("Error running %s: %s", cmd, err) def install_packages(self, pkglist): - return + self.update_package_sources() + self.package_command('install', pkgs=pkglist) - def package_command(self, cmd, args=None, pkgs=None): - return + def package_command(self, command, args=None, pkgs=None): + if pkgs is None: + pkgs = [] + + e = os.environ.copy() + e['ASSUME_ALWAYS_YES'] = 'YES' + + cmd = ['pkg'] + if args and isinstance(args, str): + cmd.append(args) + elif args and isinstance(args, list): + cmd.extend(args) + + if command: + cmd.append(command) + + pkglist = util.expand_package_list('%s-%s', pkgs) + cmd.extend(pkglist) + + # Allow the output of this to flow outwards (ie not be captured) + util.subp(cmd, env=e, capture=False) def set_timezone(self, tz): return def update_package_sources(self): - return + self._runner.run("update-sources", self.package_command, + ["update"], freq=PER_INSTANCE) -- cgit v1.2.3 From 93f5af9f5075a416c65c1d0350c374e16f32f0d5 Mon Sep 17 00:00:00 2001 From: Ben Arblaster Date: Tue, 20 Jan 2015 00:52:04 +0000 Subject: More FreeBSD improvements - Implement set_passwd - Implement set_timezone - Use /bin/tcsh as default user shell (FreeBSD default) - Change default username to freebsd - Enable set-passwords, package-update-upgrade-install and timezone modules - Remove trailing whitespace --- cloudinit/distros/freebsd.py | 20 ++++++++++++++++---- config/cloud.cfg-freebsd | 10 +++++----- 2 files changed, 21 insertions(+), 9 deletions(-) (limited to 'cloudinit/distros') diff --git a/cloudinit/distros/freebsd.py b/cloudinit/distros/freebsd.py index 10f9e7e0..c59a074b 100644 --- a/cloudinit/distros/freebsd.py +++ b/cloudinit/distros/freebsd.py @@ -238,9 +238,21 @@ class Distro(distros.Distro): util.logexc(LOG, "Failed to create user %s", name) raise e - # TODO: def set_passwd(self, user, passwd, hashed=False): - return False + cmd = ['pw', 'usermod', user] + + if hashed: + cmd.append('-H') + else: + cmd.append('-h') + + cmd.append('0') + + try: + util.subp(cmd, passwd, logstring="chpasswd for %s" % user) + except Exception as e: + util.logexc(LOG, "Failed to set password for %s", user) + raise e def lock_passwd(self, name): try: @@ -394,10 +406,10 @@ class Distro(distros.Distro): cmd.extend(pkglist) # Allow the output of this to flow outwards (ie not be captured) - util.subp(cmd, env=e, capture=False) + util.subp(cmd, env=e, capture=False) def set_timezone(self, tz): - return + distros.set_etc_timezone(tz=tz, tz_file=self._find_tz_file(tz)) def update_package_sources(self): self._runner.run("update-sources", self.package_command, diff --git a/config/cloud.cfg-freebsd b/config/cloud.cfg-freebsd index 5ac181ff..be664f5d 100644 --- a/config/cloud.cfg-freebsd +++ b/config/cloud.cfg-freebsd @@ -49,10 +49,10 @@ cloud_config_modules: # - mounts - ssh-import-id - locale -# - set-passwords -# - package-update-upgrade-install + - set-passwords + - package-update-upgrade-install # - landscape -# - timezone + - timezone # - puppet # - chef # - salt-minion @@ -80,9 +80,9 @@ cloud_final_modules: system_info: distro: freebsd default_user: - name: beastie + name: freebsd lock_passwd: True gecos: FreeBSD groups: [wheel] sudo: ["ALL=(ALL) NOPASSWD:ALL"] - shell: /bin/sh + shell: /bin/tcsh -- cgit v1.2.3 From 638191cd07ba1afe5ea61ff5268351ab77541139 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Wed, 30 Mar 2016 20:16:46 -0400 Subject: fix adding of users without a group revision 1179 regressed adding a user that did not have a 'groups' entry present. This should handle that correctly, making 'add_user' able to take: a.) groups="group1,group2" b.) groups=["group1", "group2"] c.) groups=None d.) no groups parameter LP: #1562918 --- cloudinit/distros/__init__.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'cloudinit/distros') diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index 418421b9..5563ae43 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -362,15 +362,18 @@ class Distro(object): redact_opts = ['passwd'] + # support kwargs having groups=[list] or groups="g1,g2" groups = kwargs.get('groups') if groups: if isinstance(groups, (list, tuple)): + # kwargs.items loop below wants a comma delimeted string + # that can go right through to the command. kwargs['groups'] = ",".join(groups) else: groups = groups.split(",") - if create_groups: - for group in kwargs.get('groups').split(","): + if create_groups and groups: + for group in groups: if not util.is_group(group): self.create_group(group) LOG.debug("created group %s for user %s", name, group) -- cgit v1.2.3 From 6a660b490ee6384055d2afb07f8cac1628168ba2 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Tue, 5 Apr 2016 20:43:05 -0400 Subject: write_files: fix decompression of content When provided with gzipped data, an exception would be raised because of a conversion to string. This fixes the issue and adds a test for write_files. LP: #1565638 --- cloudinit/config/cc_write_files.py | 4 +- cloudinit/distros/__init__.py | 5 +- .../test_handler/test_handler_write_files.py | 112 +++++++++++++++++++++ 3 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 tests/unittests/test_handler/test_handler_write_files.py (limited to 'cloudinit/distros') diff --git a/cloudinit/config/cc_write_files.py b/cloudinit/config/cc_write_files.py index 4b03ea91..351cfc8c 100644 --- a/cloudinit/config/cc_write_files.py +++ b/cloudinit/config/cc_write_files.py @@ -92,10 +92,10 @@ def decode_perms(perm, default, log): def extract_contents(contents, extraction_types): - result = str(contents) + result = contents for t in extraction_types: if t == 'application/x-gzip': - result = util.decomp_gzip(result, quiet=False) + result = util.decomp_gzip(result, quiet=False, decode=False) elif t == 'application/base64': result = base64.b64decode(result) elif t == UNKNOWN_ENC: diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index 418421b9..12983c0a 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -933,7 +933,10 @@ def set_etc_timezone(tz, tz_file=None, tz_conf="/etc/timezone", # This ensures that the correct tz will be used for the system if tz_local and tz_file: # use a symlink if there exists a symlink or tz_local is not present - if os.path.islink(tz_local) or not os.path.exists(tz_local): + islink = os.path.islink(tz_local) + if islink or not os.path.exists(tz_local): + if islink: + util.del_file(tz_local) os.symlink(tz_file, tz_local) else: util.copy(tz_file, tz_local) diff --git a/tests/unittests/test_handler/test_handler_write_files.py b/tests/unittests/test_handler/test_handler_write_files.py new file mode 100644 index 00000000..f1c7f7b4 --- /dev/null +++ b/tests/unittests/test_handler/test_handler_write_files.py @@ -0,0 +1,112 @@ +from cloudinit import util +from cloudinit import log as logging +from cloudinit.config.cc_write_files import write_files + +from ..helpers import FilesystemMockingTestCase + +import base64 +import gzip +import shutil +import six +import tempfile + +LOG = logging.getLogger(__name__) + +YAML_TEXT = """ +write_files: + - encoding: gzip + content: !!binary | + H4sIAIDb/U8C/1NW1E/KzNMvzuBKTc7IV8hIzcnJVyjPL8pJ4QIA6N+MVxsAAAA= + path: /usr/bin/hello + permissions: '0755' + - content: !!binary | + Zm9vYmFyCg== + path: /wark + permissions: '0755' + - content: | + hi mom line 1 + hi mom line 2 + path: /tmp/message +""" + +YAML_CONTENT_EXPECTED = { + '/usr/bin/hello': "#!/bin/sh\necho hello world\n", + '/wark': "foobar\n", + '/tmp/message': "hi mom line 1\nhi mom line 2\n", +} + + +class TestWriteFiles(FilesystemMockingTestCase): + def setUp(self): + super(TestWriteFiles, self).setUp() + self.tmp = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, self.tmp) + + def test_simple(self): + self.patchUtils(self.tmp) + expected = "hello world\n" + filename = "/tmp/my.file" + write_files( + "test_simple", [{"content": expected, "path": filename}], LOG) + self.assertEqual(util.load_file(filename), expected) + + def test_yaml_binary(self): + self.patchUtils(self.tmp) + data = util.load_yaml(YAML_TEXT) + write_files("testname", data['write_files'], LOG) + for path, content in YAML_CONTENT_EXPECTED.items(): + self.assertEqual(util.load_file(path), content) + + def test_all_decodings(self): + self.patchUtils(self.tmp) + + # build a 'files' array that has a dictionary of encodings + # for 'gz', 'gzip', 'gz+base64' ... + data = b"foobzr" + utf8_valid = b"foobzr" + utf8_invalid = b'ab\xaadef' + files = [] + expected = [] + + gz_aliases = ('gz', 'gzip') + gz_b64_aliases = ('gz+base64', 'gzip+base64', 'gz+b64', 'gzip+b64') + b64_aliases = ('base64', 'b64') + + datum = (("utf8", utf8_valid), ("no-utf8", utf8_invalid)) + for name, data in datum: + gz = (_gzip_bytes(data), gz_aliases) + gz_b64 = (base64.b64encode(_gzip_bytes(data)), gz_b64_aliases) + b64 = (base64.b64encode(data), b64_aliases) + for content, aliases in (gz, gz_b64, b64): + for enc in aliases: + cur = {'content': content, + 'path': '/tmp/file-%s-%s' % (name, enc), + 'encoding': enc} + files.append(cur) + expected.append((cur['path'], data)) + + write_files("test_decoding", files, LOG) + + for path, content in expected: + self.assertEqual(util.load_file(path, decode=False), content) + + # make sure we actually wrote *some* files. + flen_expected = ( + len(gz_aliases + gz_b64_aliases + b64_aliases) * len(datum)) + self.assertEqual(len(expected), flen_expected) + + +def _gzip_bytes(data): + buf = six.BytesIO() + fp = None + try: + fp = gzip.GzipFile(fileobj=buf, mode="wb") + fp.write(data) + fp.close() + return buf.getvalue() + finally: + if fp: + fp.close() + + +# vi: ts=4 expandtab -- cgit v1.2.3 From 2c95e4cf2a61d13de72833c79d04648ba1687ef9 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Wed, 6 Apr 2016 11:03:55 -0400 Subject: support adding the primary group also --- cloudinit/distros/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'cloudinit/distros') diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index 5563ae43..71da7ec5 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -371,6 +371,10 @@ class Distro(object): kwargs['groups'] = ",".join(groups) else: groups = groups.split(",") + + primary_group = kwargs.get('primary_group') + if primary_group: + groups.append(primary_group) if create_groups and groups: for group in groups: -- cgit v1.2.3