Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BSD: upgrade support #305

Merged
merged 2 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cloudinit/distros/bsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -97,6 +97,10 @@ def package_command(self, command, args=None, pkgs=None):
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
goneri marked this conversation as resolved.
Show resolved Hide resolved
cmd = self.pkg_cmd_upgrade_prefix

if args and isinstance(args, str):
cmd.append(args)
Expand Down
1 change: 1 addition & 0 deletions cloudinit/distros/freebsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions cloudinit/distros/netbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
goneri marked this conversation as resolved.
Show resolved Hide resolved
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]
Expand Down
17 changes: 17 additions & 0 deletions tests/unittests/test_distros/test_netbsd.py
Original file line number Diff line number Diff line change
@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent, can we add an assert about what if being checked by os.path.exists

Suggested change
assert distro.pkg_cmd_upgrade_prefix == expectation
assert distro.pkg_cmd_upgrade_prefix == expectation
assert [mock.call('/usr/pkg/bin/pkgin')] == m_os.path.exists.call_args_list

assert [mock.call('/usr/pkg/bin/pkgin')] == m_os.path.exists.call_args_list