Skip to content

Commit

Permalink
fix(openbsd): services & build tool
Browse files Browse the repository at this point in the history
    * Added net/activator for `ifconfig` that is used with OpenBSD
    * Added sysvinit scripts for OpenBSD
    * Updated build-on-openbsd tool with additional dependencies and
      better logic
  • Loading branch information
CodeBleu committed Dec 4, 2023
1 parent 6e04a2c commit bdbbd64
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 5 deletions.
27 changes: 27 additions & 0 deletions cloudinit/net/activators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Dict, Iterable, List, Optional, Type, Union

from cloudinit import subp, util
from cloudinit.net.ifconfig import available as ifconfig_available
from cloudinit.net.eni import available as eni_available
from cloudinit.net.netplan import available as netplan_available
from cloudinit.net.network_manager import available as nm_available
Expand Down Expand Up @@ -101,6 +102,30 @@ def bring_down_interface(device_name: str) -> bool:
cmd = ["ifdown", device_name]
return _alter_interface(cmd, device_name)

class IfConfigActivator(NetworkActivator):
@staticmethod
def available(target: Optional[str] = None) -> bool:
"""Return true if ifconfig can be used on this system."""
return ifconfig_available(target=target)

@staticmethod
def bring_up_interface(device_name: str) -> bool:
"""Bring up interface using ifconfig <dev> up.
Return True is successful, otherwise return False
"""
cmd = ["ifconfig", device_name, "up"]
return _alter_interface(cmd, device_name)

@staticmethod
def bring_down_interface(device_name: str) -> bool:
"""Bring up interface using ifconfig <dev> down.
Return True is successful, otherwise return False
"""
cmd = ["ifconfig", device_name, "down"]
return _alter_interface(cmd, device_name)


class NetworkManagerActivator(NetworkActivator):
@staticmethod
Expand Down Expand Up @@ -217,13 +242,15 @@ def bring_down_interface(device_name: str) -> bool:
# version to encompass both seems overkill at this point
DEFAULT_PRIORITY = [
"eni",
"ifconfig",
"netplan",
"network-manager",
"networkd",
]

NAME_TO_ACTIVATOR: Dict[str, Type[NetworkActivator]] = {
"eni": IfUpDownActivator,
"ifconfig": IfConfigActivator,
"netplan": NetplanActivator,
"network-manager": NetworkManagerActivator,
"networkd": NetworkdActivator,
Expand Down
21 changes: 21 additions & 0 deletions cloudinit/net/ifconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This file is part of cloud-init. See LICENSE file for license information.

import copy
import glob
import logging
import os
import re
from typing import Optional

from cloudinit import subp, util
from cloudinit.net import ParserError, renderer, subnet_is_ipv6
from cloudinit.net.network_state import NetworkState

LOG = logging.getLogger(__name__)

def available(target=None):
expected = ["ifconfig"]
search = ["/sbin"]
if not subp.which(expected, search=search, target=target):
return False
return True
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ def render_tmpl(template, mode=None, is_yaml=False):
for f in glob("sysvinit/netbsd/*")
if is_f(f)
],
"sysvinit_openbsd": lambda: [
render_tmpl(f, mode=0o755)
for f in glob("sysvinit/openbsd/*")
if is_f(f)
],
"sysvinit_deb": lambda: [f for f in glob("sysvinit/debian/*") if is_f(f)],
"sysvinit_openrc": lambda: [
f for f in glob("sysvinit/gentoo/*") if is_f(f)
Expand All @@ -156,6 +161,7 @@ def render_tmpl(template, mode=None, is_yaml=False):
"sysvinit": "etc/rc.d/init.d",
"sysvinit_freebsd": "usr/local/etc/rc.d",
"sysvinit_netbsd": "usr/local/etc/rc.d",
"sysvinit_openbsd": "etc/rc.d",
"sysvinit_deb": "etc/init.d",
"sysvinit_openrc": "etc/init.d",
"systemd": pkg_config_read("systemd", "systemdsystemunitdir"),
Expand Down
36 changes: 36 additions & 0 deletions sysvinit/openbsd/cloudconfig.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## template:jinja
#!/bin/ksh

# PROVIDE: cloudconfig
# REQUIRE: cloudinit
# BEFORE: sshd


daemon="cloud-init"
daemon_execdir="{{prefix}}/bin"
daemon_flags="modules --mode config"
daemon_rtable=0
daemon_timeout=1
daemon_user=root

. /etc/rc.d/rc.subr

rc_bg="YES" # (undefined or "YES")
rc_usercheck="YES" # (undefined or "NO")

rc_start() {
test -e /etc/cloud/cloud-init.disabled \
&& warn "cloud-init disabled by cloud-init.disabled file" \
&& exit 0
rc_exec "${daemon_execdir}/${daemon} ${daemon_flags}"
}

rc_check() {
pgrep -f "${daemon}" >/dev/null
}

rc_stop() {
pkill -f "${daemon}" >/dev/null
}

rc_cmd "$1"
34 changes: 34 additions & 0 deletions sysvinit/openbsd/cloudfinal.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## template:jinja
#!/bin/ksh

# PROVIDE: cloudfinal
# REQUIRE: LOGIN cloudconfig

daemon="cloud-init"
daemon_execdir="{{prefix}}/bin"
daemon_flags="modules --mode final"
daemon_rtable=0
daemon_timeout=1
daemon_user=root

. /etc/rc.d/rc.subr

rc_bg="YES" # (undefined or "YES")
rc_usercheck="YES" # (undefined or "NO")

rc_start() {
test -e /etc/cloud/cloud-init.disabled \
&& warn "cloud-init disabled by cloud-init.disabled file" \
&& exit 0
rc_exec "${daemon_execdir}/${daemon} ${daemon_flags}"
}

rc_check() {
pgrep -f "${daemon}" >/dev/null
}

rc_stop() {
pkill -f "${daemon}" >/dev/null
}

rc_cmd "$1"
34 changes: 34 additions & 0 deletions sysvinit/openbsd/cloudinit.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## template:jinja
#!/bin/ksh

# PROVIDE: cloudinit
# REQUIRE: cloudinitlocal

daemon="cloud-init"
daemon_execdir="{{prefix}}/bin"
daemon_flags="init"
daemon_rtable=0
daemon_timeout=1
daemon_user=root

. /etc/rc.d/rc.subr

rc_bg="YES" # (undefined or "YES")
rc_usercheck="YES" # (undefined or "NO")

rc_start() {
test -e /etc/cloud/cloud-init.disabled \
&& warn "cloud-init disabled by cloud-init.disabled file" \
&& exit 0
rc_exec "${daemon_execdir}/${daemon} ${daemon_flags}"
}

rc_check() {
pgrep -f "${daemon}" >/dev/null
}

rc_stop() {
pkill -f "${daemon}" >/dev/null
}

rc_cmd "$1"
37 changes: 37 additions & 0 deletions sysvinit/openbsd/cloudinitlocal.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## template:jinja
#!/bin/ksh

# PROVIDE: cloudinitlocal
# REQUIRE: NETWORKING

# After NETWORKING because we don't want staticroute to wipe
# the route set by the DHCP client toward the meta-data server.

daemon="cloud-init"
daemon_execdir="{{prefix}}/bin"
daemon_flags="init -l"
daemon_rtable=0
daemon_timeout=1
daemon_user=root

. /etc/rc.d/rc.subr

rc_bg="YES" # (undefined or "YES")
rc_usercheck="YES"

rc_start() {
test -e /etc/cloud/cloud-init.disabled \
&& warn "cloud-init disabled by cloud-init.disabled file" \
&& exit 0
rc_exec "${daemon_execdir}/${daemon} ${daemon_flags}"
}

rc_check() {
pgrep -f "${daemon}" >/dev/null
}

rc_stop() {
pkill -f "${daemon}" >/dev/null
}

rc_cmd "$1"
27 changes: 22 additions & 5 deletions tools/build-on-openbsd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

fail() { echo "FAILED:" "$@" 1>&2; exit 1; }

PYTHON=${PYTHON:-python3}
if [ ! $(which ${PYTHON}) ]; then
echo "Please install python first."
exit 1
fi

# Check dependencies:
depschecked=/tmp/c-i.dependencieschecked
pkgs="
Expand All @@ -16,12 +22,23 @@ pkgs="
py3-setuptools
py3-yaml
sudo--
wget
"
[ -f "$depschecked" ] || pkg_add "${pkgs}" || fail "install packages"

touch $depschecked
[ -f $depschecked ] || echo "Installing the following packages: $pkgs"; output=$(pkg_add -zI $pkgs 2>&1)


if echo "$output" | grep -q -e "Can't find" -e "Ambiguous"; then
echo "Failed to find or install one or more packages"
echo "Failed Package(s):"
echo "$output"
exit 1
else
echo Successfully installed packages
touch $depschecked

python3 setup.py build
python3 setup.py install -O1 --distro openbsd --skip-build
python3 setup.py build
python3 setup.py install -O1 --distro openbsd --skip-build --init-system sysvinit_openbsd

echo "Installation completed."
echo "Installation completed."
fi

0 comments on commit bdbbd64

Please sign in to comment.