← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~smoser/cloud-init:feature/curtin-centos3 into cloud-init:master

 

Scott Moser has proposed merging ~smoser/cloud-init:feature/curtin-centos3 into cloud-init:master.

Commit message:
FIXME: Templatize systemd service files

Requested reviews:
  cloud-init commiters (cloud-init-dev)
Related bugs:
  Bug #1704872 in cloud-init: "sysconfig use subnet prefix and set DEFROUTE key"
  https://bugs.launchpad.net/cloud-init/+bug/1704872

For more details, see:
https://code.launchpad.net/~smoser/cloud-init/+git/cloud-init/+merge/327743
-- 
Your team cloud-init commiters is requested to review the proposed merge of ~smoser/cloud-init:feature/curtin-centos3 into cloud-init:master.
diff --git a/cloudinit/net/sysconfig.py b/cloudinit/net/sysconfig.py
index ad8c268..de6601a 100644
--- a/cloudinit/net/sysconfig.py
+++ b/cloudinit/net/sysconfig.py
@@ -10,7 +10,8 @@ from cloudinit.distros.parsers import resolv_conf
 from cloudinit import util
 
 from . import renderer
-from .network_state import subnet_is_ipv6, net_prefix_to_ipv4_mask
+from .network_state import (
+    is_ipv6_addr, net_prefix_to_ipv4_mask, subnet_is_ipv6)
 
 
 def _make_header(sep='#'):
@@ -308,20 +309,13 @@ class Renderer(renderer.Renderer):
             elif subnet_type == 'static':
                 if subnet_is_ipv6(subnet):
                     ipv6_index = ipv6_index + 1
-                    if 'netmask' in subnet and str(subnet['netmask']) != "":
-                        ipv6_cidr = (subnet['address'] +
-                                     '/' +
-                                     str(subnet['netmask']))
-                    else:
-                        ipv6_cidr = subnet['address']
+                    ipv6_cidr = "%s/%s" % (subnet['address'], subnet['prefix'])
                     if ipv6_index == 0:
                         iface_cfg['IPV6ADDR'] = ipv6_cidr
                     elif ipv6_index == 1:
                         iface_cfg['IPV6ADDR_SECONDARIES'] = ipv6_cidr
                     else:
-                        iface_cfg['IPV6ADDR_SECONDARIES'] = (
-                            iface_cfg['IPV6ADDR_SECONDARIES'] +
-                            " " + ipv6_cidr)
+                        iface_cfg['IPV6ADDR_SECONDARIES'] += " " + ipv6_cidr
                 else:
                     ipv4_index = ipv4_index + 1
                     suff = "" if ipv4_index == 0 else str(ipv4_index)
@@ -330,7 +324,11 @@ class Renderer(renderer.Renderer):
                         net_prefix_to_ipv4_mask(subnet['prefix'])
 
                 if 'gateway' in subnet:
-                    iface_cfg['GATEWAY'] = subnet['gateway']
+                    iface_cfg['DEFROUTE'] = True
+                    if is_ipv6_addr(subnet['gateway']):
+                        iface_cfg['IPV6_DEFAULTGW'] = subnet['gateway']
+                    else:
+                        iface_cfg['GATEWAY'] = subnet['gateway']
 
     @classmethod
     def _render_subnet_routes(cls, iface_cfg, route_cfg, subnets):
diff --git a/packages/redhat/cloud-init.spec.in b/packages/redhat/cloud-init.spec.in
index 9f75c4b..6ab0d20 100644
--- a/packages/redhat/cloud-init.spec.in
+++ b/packages/redhat/cloud-init.spec.in
@@ -115,11 +115,6 @@ rm -rf $RPM_BUILD_ROOT%{python_sitelib}/tests
 mkdir -p $RPM_BUILD_ROOT/%{_sharedstatedir}/cloud
 mkdir -p $RPM_BUILD_ROOT/%{_libexecdir}/%{name}
 
-%if "%{init_system}" == "systemd"
-mkdir -p         $RPM_BUILD_ROOT/%{_unitdir}
-cp -p systemd/*  $RPM_BUILD_ROOT/%{_unitdir}
-%endif
-
 %clean
 rm -rf $RPM_BUILD_ROOT
 
diff --git a/setup.py b/setup.py
index bce06ad..1197ece 100755
--- a/setup.py
+++ b/setup.py
@@ -87,8 +87,8 @@ def read_requires():
     return str(deps).splitlines()
 
 
-def render_cloud_cfg():
-    """render cloud.cfg into a tmpdir under same dir as setup.py
+def render_tmpl(template):
+    """render template into a tmpdir under same dir as setup.py
 
     This is rendered to a temporary directory under the top level
     directory with the name 'cloud.cfg'.  The reason for not just rendering
@@ -99,16 +99,21 @@ def render_cloud_cfg():
     # older versions of tox use bdist (xenial), and then install from there.
     # newer versions just use install.
     if not (sys.argv[1] == 'install' or sys.argv[1].startswith('bdist*')):
-        return 'config/cloud.cfg.tmpl'
+        return template
+
+    tmpl_ext = ".tmpl"
+    # we may get passed a non-template file, just pass it back
+    if not template.endswith(tmpl_ext):
+        return template
+
     topdir = os.path.dirname(sys.argv[0])
     tmpd = tempfile.mkdtemp(dir=topdir)
     atexit.register(shutil.rmtree, tmpd)
-    fpath = os.path.join(tmpd, 'cloud.cfg')
-    tiny_p([sys.executable, './tools/render-cloudcfg',
-            'config/cloud.cfg.tmpl', fpath])
-    # relpath is relative to setup.py
-    relpath = os.path.join(os.path.basename(tmpd), 'cloud.cfg')
-    return relpath
+    bname = os.path.basename(template).rstrip(tmpl_ext)
+    fpath = os.path.join(tmpd, bname)
+    tiny_p([sys.executable, './tools/render-cloudcfg', template, fpath])
+    # return path relative to setup.py
+    return os.path.join(os.path.basename(tmpd), bname)
 
 
 INITSYS_FILES = {
@@ -116,8 +121,10 @@ INITSYS_FILES = {
     'sysvinit_freebsd': [f for f in glob('sysvinit/freebsd/*') if is_f(f)],
     'sysvinit_deb': [f for f in glob('sysvinit/debian/*') if is_f(f)],
     'sysvinit_openrc': [f for f in glob('sysvinit/gentoo/*') if is_f(f)],
-    'systemd': [f for f in (glob('systemd/*.service') +
-                            glob('systemd/*.target')) if is_f(f)],
+    'systemd': [render_tmpl(f)
+                for f in (glob('systemd/*.tmpl') +
+                          glob('systemd/*.service') +
+                          glob('systemd/*.target')) if is_f(f)],
     'systemd.generators': [f for f in glob('systemd/*-generator') if is_f(f)],
     'upstart': [f for f in glob('upstart/*') if is_f(f)],
 }
@@ -195,7 +202,7 @@ if not in_virtualenv():
         INITSYS_ROOTS[k] = "/" + INITSYS_ROOTS[k]
 
 data_files = [
-    (ETC + '/cloud', [render_cloud_cfg()]),
+    (ETC + '/cloud', [render_tmpl("config/cloud.cfg.tmpl")]),
     (ETC + '/cloud/cloud.cfg.d', glob('config/cloud.cfg.d/*')),
     (ETC + '/cloud/templates', glob('templates/*')),
     (USR_LIB_EXEC + '/cloud-init', ['tools/ds-identify',
diff --git a/systemd/cloud-config.service b/systemd/cloud-config.service
deleted file mode 100644
index 3309e08..0000000
--- a/systemd/cloud-config.service
+++ /dev/null
@@ -1,16 +0,0 @@
-[Unit]
-Description=Apply the settings specified in cloud-config
-After=network-online.target cloud-config.target
-Wants=network-online.target cloud-config.target
-
-[Service]
-Type=oneshot
-ExecStart=/usr/bin/cloud-init modules --mode=config
-RemainAfterExit=yes
-TimeoutSec=0
-
-# Output needs to appear in instance console output
-StandardOutput=journal+console
-
-[Install]
-WantedBy=cloud-init.target
diff --git a/systemd/cloud-config.service.tmpl b/systemd/cloud-config.service.tmpl
new file mode 100644
index 0000000..bdee3ce
--- /dev/null
+++ b/systemd/cloud-config.service.tmpl
@@ -0,0 +1,17 @@
+## template:jinja
+[Unit]
+Description=Apply the settings specified in cloud-config
+After=network-online.target cloud-config.target
+Wants=network-online.target cloud-config.target
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/cloud-init modules --mode=config
+RemainAfterExit=yes
+TimeoutSec=0
+
+# Output needs to appear in instance console output
+StandardOutput=journal+console
+
+[Install]
+WantedBy=cloud-init.target
diff --git a/systemd/cloud-final.service b/systemd/cloud-final.service
deleted file mode 100644
index 66f5b8f..0000000
--- a/systemd/cloud-final.service
+++ /dev/null
@@ -1,18 +0,0 @@
-[Unit]
-Description=Execute cloud user/final scripts
-After=network-online.target cloud-config.service rc-local.service multi-user.target
-Wants=network-online.target cloud-config.service
-Before=apt-daily.service
-
-[Service]
-Type=oneshot
-ExecStart=/usr/bin/cloud-init modules --mode=final
-RemainAfterExit=yes
-TimeoutSec=0
-KillMode=process
-
-# Output needs to appear in instance console output
-StandardOutput=journal+console
-
-[Install]
-WantedBy=cloud-init.target
diff --git a/systemd/cloud-final.service.tmpl b/systemd/cloud-final.service.tmpl
new file mode 100644
index 0000000..fc01b89
--- /dev/null
+++ b/systemd/cloud-final.service.tmpl
@@ -0,0 +1,22 @@
+## template:jinja
+[Unit]
+Description=Execute cloud user/final scripts
+After=network-online.target cloud-config.service rc-local.service
+{% if variant in ["ubuntu", "unknown", "debian"] %}
+After=multi-user.target
+{% endif %}
+Wants=network-online.target cloud-config.service
+Before=apt-daily.service
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/cloud-init modules --mode=final
+RemainAfterExit=yes
+TimeoutSec=0
+KillMode=process
+
+# Output needs to appear in instance console output
+StandardOutput=journal+console
+
+[Install]
+WantedBy=cloud-init.target
diff --git a/systemd/cloud-init-local.service b/systemd/cloud-init-local.service
deleted file mode 100644
index 7ee43ed..0000000
--- a/systemd/cloud-init-local.service
+++ /dev/null
@@ -1,24 +0,0 @@
-[Unit]
-Description=Initial cloud-init job (pre-networking)
-DefaultDependencies=no
-Wants=network-pre.target
-After=systemd-remount-fs.service
-Before=NetworkManager.service
-Before=network-pre.target
-Before=shutdown.target
-Before=sysinit.target
-Conflicts=shutdown.target
-RequiresMountsFor=/var/lib/cloud
-
-[Service]
-Type=oneshot
-ExecStart=/usr/bin/cloud-init init --local
-ExecStart=/bin/touch /run/cloud-init/network-config-ready
-RemainAfterExit=yes
-TimeoutSec=0
-
-# Output needs to appear in instance console output
-StandardOutput=journal+console
-
-[Install]
-WantedBy=cloud-init.target
diff --git a/systemd/cloud-init-local.service.tmpl b/systemd/cloud-init-local.service.tmpl
new file mode 100644
index 0000000..ff9c644
--- /dev/null
+++ b/systemd/cloud-init-local.service.tmpl
@@ -0,0 +1,29 @@
+## template:jinja
+[Unit]
+Description=Initial cloud-init job (pre-networking)
+{% if variant in ["ubuntu", "unknown", "debian"] %}
+DefaultDependencies=no
+{% endif %}
+Wants=network-pre.target
+After=systemd-remount-fs.service
+Before=NetworkManager.service
+Before=network-pre.target
+Before=shutdown.target
+{% if variant in ["ubuntu", "unknown", "debian"] %}
+Before=sysinit.target
+Conflicts=shutdown.target
+{% endif %}
+RequiresMountsFor=/var/lib/cloud
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/cloud-init init --local
+ExecStart=/bin/touch /run/cloud-init/network-config-ready
+RemainAfterExit=yes
+TimeoutSec=0
+
+# Output needs to appear in instance console output
+StandardOutput=journal+console
+
+[Install]
+WantedBy=cloud-init.target
diff --git a/systemd/cloud-init.service b/systemd/cloud-init.service
deleted file mode 100644
index 39acc20..0000000
--- a/systemd/cloud-init.service
+++ /dev/null
@@ -1,27 +0,0 @@
-[Unit]
-Description=Initial cloud-init job (metadata service crawler)
-DefaultDependencies=no
-Wants=cloud-init-local.service
-Wants=sshd-keygen.service
-Wants=sshd.service
-After=cloud-init-local.service
-After=systemd-networkd-wait-online.service
-After=networking.service
-Before=network-online.target
-Before=sshd-keygen.service
-Before=sshd.service
-Before=sysinit.target
-Before=systemd-user-sessions.service
-Conflicts=shutdown.target
-
-[Service]
-Type=oneshot
-ExecStart=/usr/bin/cloud-init init
-RemainAfterExit=yes
-TimeoutSec=0
-
-# Output needs to appear in instance console output
-StandardOutput=journal+console
-
-[Install]
-WantedBy=cloud-init.target
diff --git a/systemd/cloud-init.service.tmpl b/systemd/cloud-init.service.tmpl
new file mode 100644
index 0000000..2c71889
--- /dev/null
+++ b/systemd/cloud-init.service.tmpl
@@ -0,0 +1,35 @@
+## template:jinja
+[Unit]
+Description=Initial cloud-init job (metadata service crawler)
+DefaultDependencies=no
+Wants=cloud-init-local.service
+Wants=sshd-keygen.service
+Wants=sshd.service
+After=cloud-init-local.service
+After=systemd-networkd-wait-online.service
+{% if variant in ["ubuntu", "unknown", "debian"] %}
+After=networking.service
+{% endif %}
+{% if variant in ["centos", "fedora", "redhat"] %}
+After=network.service
+{% endif %}
+Before=network-online.target
+Before=sshd-keygen.service
+Before=sshd.service
+{% if variant in ["ubuntu", "unknown", "debian"] %}
+Before=sysinit.target
+Conflicts=shutdown.target
+{% endif %}
+Before=systemd-user-sessions.service
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/cloud-init init
+RemainAfterExit=yes
+TimeoutSec=0
+
+# Output needs to appear in instance console output
+StandardOutput=journal+console
+
+[Install]
+WantedBy=cloud-init.target
diff --git a/tests/unittests/test_distros/test_netconfig.py b/tests/unittests/test_distros/test_netconfig.py
index dffe178..2f505d9 100644
--- a/tests/unittests/test_distros/test_netconfig.py
+++ b/tests/unittests/test_distros/test_netconfig.py
@@ -476,10 +476,11 @@ NETWORKING=yes
 # Created by cloud-init on instance boot automatically, do not edit.
 #
 BOOTPROTO=none
+DEFROUTE=yes
 DEVICE=eth0
+GATEWAY=192.168.1.254
 IPADDR=192.168.1.5
 NETMASK=255.255.255.0
-GATEWAY=192.168.1.254
 NM_CONTROLLED=no
 ONBOOT=yes
 TYPE=Ethernet
@@ -626,10 +627,11 @@ IPV6_AUTOCONF=no
 # Created by cloud-init on instance boot automatically, do not edit.
 #
 BOOTPROTO=none
+DEFROUTE=yes
 DEVICE=eth0
 IPV6ADDR=2607:f0d0:1002:0011::2/64
-GATEWAY=2607:f0d0:1002:0011::1
 IPV6INIT=yes
+IPV6_DEFAULTGW=2607:f0d0:1002:0011::1
 NM_CONTROLLED=no
 ONBOOT=yes
 TYPE=Ethernet
diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py
index 76721ba..2224271 100644
--- a/tests/unittests/test_net.py
+++ b/tests/unittests/test_net.py
@@ -298,7 +298,7 @@ DEVICE=eth0
 GATEWAY=172.19.3.254
 HWADDR=fa:16:3e:ed:9a:59
 IPADDR=172.19.1.34
-IPV6ADDR=2001:DB8::10
+IPV6ADDR=2001:DB8::10/64
 IPV6ADDR_SECONDARIES="2001:DB9::10/64 2001:DB10::10/64"
 IPV6INIT=yes
 IPV6_DEFAULTGW=2001:DB8::1
@@ -1161,6 +1161,7 @@ USERCTL=no
 # Created by cloud-init on instance boot automatically, do not edit.
 #
 BOOTPROTO=none
+DEFROUTE=yes
 DEVICE=interface0
 GATEWAY=10.0.2.2
 HWADDR=52:54:00:12:34:00

Follow ups