summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGonéri Le Bouder <goneri@lebouder.net>2020-05-04 16:58:35 -0400
committerGitHub <noreply@github.com>2020-05-04 14:58:35 -0600
commitf9b393bb4f15258de230884949e543e0f62f9abb (patch)
tree1993e292c7ebe7f659fe53b6798a7aee603766f5
parent59dd290ee7986f76247bfd26d18cbcc586777812 (diff)
downloadcloud-init-git-f9b393bb4f15258de230884949e543e0f62f9abb.tar.gz
bsd: upgrade support (#305)
Implement the upgrade support: - FreeBSD: using `pkg upgrade` - NetBSD: with `pkgin`
-rw-r--r--cloudinit/distros/bsd.py8
-rw-r--r--cloudinit/distros/freebsd.py1
-rw-r--r--cloudinit/distros/netbsd.py14
-rw-r--r--tests/unittests/test_distros/test_netbsd.py17
4 files changed, 35 insertions, 5 deletions
diff --git a/cloudinit/distros/bsd.py b/cloudinit/distros/bsd.py
index efc73d5d..37cf93bf 100644
--- a/cloudinit/distros/bsd.py
+++ b/cloudinit/distros/bsd.py
@@ -18,9 +18,9 @@ class BSD(distros.Distro):
group_add_cmd_prefix = []
pkg_cmd_install_prefix = []
pkg_cmd_remove_prefix = []
- # There is no need to update the package cache on NetBSD and OpenBSD
- # TODO neither freebsd nor netbsd handles a command 'upgrade'
+ # There is no update/upgrade on OpenBSD
pkg_cmd_update_prefix = None
+ pkg_cmd_upgrade_prefix = None
def __init__(self, name, cfg, paths):
super().__init__(name, cfg, paths)
@@ -97,6 +97,10 @@ class BSD(distros.Distro):
if not self.pkg_cmd_update_prefix:
return
cmd = self.pkg_cmd_update_prefix
+ elif command == 'upgrade':
+ if not self.pkg_cmd_upgrade_prefix:
+ return
+ cmd = self.pkg_cmd_upgrade_prefix
if args and isinstance(args, str):
cmd.append(args)
diff --git a/cloudinit/distros/freebsd.py b/cloudinit/distros/freebsd.py
index 71a6195b..b3a4ad67 100644
--- a/cloudinit/distros/freebsd.py
+++ b/cloudinit/distros/freebsd.py
@@ -25,6 +25,7 @@ class Distro(cloudinit.distros.bsd.BSD):
pkg_cmd_install_prefix = ["pkg", "install"]
pkg_cmd_remove_prefix = ["pkg", "remove"]
pkg_cmd_update_prefix = ["pkg", "update"]
+ pkg_cmd_upgrade_prefix = ["pkg", "upgrade"]
def _select_hostname(self, hostname, fqdn):
# Should be FQDN if available. See rc.conf(5) in FreeBSD
diff --git a/cloudinit/distros/netbsd.py b/cloudinit/distros/netbsd.py
index 6ae60943..ecc8239a 100644
--- a/cloudinit/distros/netbsd.py
+++ b/cloudinit/distros/netbsd.py
@@ -21,10 +21,18 @@ class NetBSD(cloudinit.distros.bsd.BSD):
"""
ci_sudoers_fn = '/usr/pkg/etc/sudoers.d/90-cloud-init-users'
-
group_add_cmd_prefix = ["groupadd"]
- pkg_cmd_install_prefix = ["pkg_add", "-U"]
- pkg_cmd_remove_prefix = ['pkg_delete']
+
+ def __init__(self, name, cfg, paths):
+ super().__init__(name, cfg, paths)
+ if os.path.exists("/usr/pkg/bin/pkgin"):
+ self.pkg_cmd_install_prefix = ['pkgin', '-y', 'install']
+ self.pkg_cmd_remove_prefix = ['pkgin', '-y', 'remove']
+ self.pkg_cmd_update_prefix = ['pkgin', '-y', 'update']
+ self.pkg_cmd_upgrade_prefix = ['pkgin', '-y', 'full-upgrade']
+ else:
+ self.pkg_cmd_install_prefix = ['pkg_add', '-U']
+ self.pkg_cmd_remove_prefix = ['pkg_delete']
def _get_add_member_to_group_cmd(self, member_name, group_name):
return ['usermod', '-G', group_name, member_name]
diff --git a/tests/unittests/test_distros/test_netbsd.py b/tests/unittests/test_distros/test_netbsd.py
new file mode 100644
index 00000000..11a68d2a
--- /dev/null
+++ b/tests/unittests/test_distros/test_netbsd.py
@@ -0,0 +1,17 @@
+import cloudinit.distros.netbsd
+
+import pytest
+import unittest.mock as mock
+
+
+@pytest.mark.parametrize('with_pkgin', (True, False))
+@mock.patch("cloudinit.distros.netbsd.os")
+def test_init(m_os, with_pkgin):
+ print(with_pkgin)
+ m_os.path.exists.return_value = with_pkgin
+ cfg = {}
+
+ distro = cloudinit.distros.netbsd.NetBSD("netbsd", cfg, None)
+ expectation = ['pkgin', '-y', 'full-upgrade'] if with_pkgin else None
+ assert distro.pkg_cmd_upgrade_prefix == expectation
+ assert [mock.call('/usr/pkg/bin/pkgin')] == m_os.path.exists.call_args_list