diff --git a/pkg/ignition/builder.go b/pkg/ignition/builder.go index 14087f54..20da5c4b 100644 --- a/pkg/ignition/builder.go +++ b/pkg/ignition/builder.go @@ -97,7 +97,10 @@ func (b *ignitionBuilder) GenerateConfig() (config ignition_config_types_32.Conf config.Ignition.Version = "3.2.0" config.Storage.Files = []ignition_config_types_32.File{b.IronicAgentConf()} config.Storage.Files = append(config.Storage.Files, netFiles...) - config.Systemd.Units = []ignition_config_types_32.Unit{b.IronicAgentService(len(netFiles) > 0)} + config.Systemd.Units = []ignition_config_types_32.Unit{ + b.IronicAgentService(len(netFiles) > 0), + b.hostNameFixService(), + } if b.ironicAgentPullSecret != "" { config.Storage.Files = append(config.Storage.Files, b.authFile()) diff --git a/pkg/ignition/builder_test.go b/pkg/ignition/builder_test.go index 2ead4eaf..85fa2294 100644 --- a/pkg/ignition/builder_test.go +++ b/pkg/ignition/builder_test.go @@ -18,7 +18,7 @@ func TestGenerateStructure(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "3.2.0", ignition.Ignition.Version) - assert.Len(t, ignition.Systemd.Units, 1) + assert.Len(t, ignition.Systemd.Units, 2) assert.Len(t, ignition.Storage.Files, 2) assert.Len(t, ignition.Passwd.Users, 0) @@ -41,7 +41,7 @@ func TestGenerateWithMoreFields(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "3.2.0", ignition.Ignition.Version) - assert.Len(t, ignition.Systemd.Units, 1) + assert.Len(t, ignition.Systemd.Units, 2) assert.Len(t, ignition.Storage.Files, 5) assert.Len(t, ignition.Passwd.Users, 1) diff --git a/pkg/ignition/service_config.go b/pkg/ignition/service_config.go index 9b108645..0083eb37 100644 --- a/pkg/ignition/service_config.go +++ b/pkg/ignition/service_config.go @@ -39,7 +39,7 @@ Restart=on-failure RestartSec=5 StartLimitIntervalSec=0 ExecStartPre=/bin/podman pull %s %s -ExecStart=/bin/podman run --rm --privileged --network host --mount type=bind,src=/etc/ironic-python-agent.conf,dst=/etc/ironic-python-agent/ignition.conf --mount type=bind,src=/dev,dst=/dev --mount type=bind,src=/sys,dst=/sys --mount type=bind,src=/run/dbus/system_bus_socket,dst=/run/dbus/system_bus_socket --mount type=bind,src=/,dst=/mnt/coreos --mount type=bind,src=/run/udev,dst=/run/udev --ipc=host --env "IPA_COREOS_IP_OPTIONS=%s" --env IPA_COREOS_COPY_NETWORK=%v --name ironic-agent %s +ExecStart=/bin/podman run --rm --privileged --network host --mount type=bind,src=/etc/ironic-python-agent.conf,dst=/etc/ironic-python-agent/ignition.conf --mount type=bind,src=/dev,dst=/dev --mount type=bind,src=/sys,dst=/sys --mount type=bind,src=/run/dbus/system_bus_socket,dst=/run/dbus/system_bus_socket --mount type=bind,src=/,dst=/mnt/coreos --mount type=bind,src=/run/udev,dst=/run/udev --ipc=host --uts=host --env "IPA_COREOS_IP_OPTIONS=%s" --env IPA_COREOS_COPY_NETWORK=%v --name ironic-agent %s [Install] WantedBy=multi-user.target ` @@ -59,3 +59,33 @@ func (b *ignitionBuilder) authFile() ignition_config_types_32.File { FileEmbedded1: ignition_config_types_32.FileEmbedded1{Contents: ignition_config_types_32.Resource{Source: &source}}, } } + +func (b *ignitionBuilder) hostNameFixService() ignition_config_types_32.Unit { + script := fmt.Sprintf(` +if [[ "$(hostnamectl hostname)" =~ ^localhost(\.localdomain)?$ ]]; then + echo Localhost detected, setting hostname to %[1]s; + hostnamectl set-hostname --static --transient "%[1]s"; + echo "%[1]s" > /etc/hostname; +fi`, b.hostname) + + script = strings.Replace(script, "\n", " ", -1) + + unitTemplate := `[Unit] +Description=Fix hostname if it is localhost +After=network-online.target +Before=ironic-agent.service +Wants=network-online.target +[Service] +Type=oneshot +ExecStart=/bin/bash -c '%s' +[Install] +WantedBy=multi-user.target +` + contents := fmt.Sprintf(unitTemplate, script) + + return ignition_config_types_32.Unit{ + Name: "hostname-fix.service", + Enabled: pointer.BoolPtr(true), + Contents: &contents, + } +} diff --git a/pkg/ignition/service_config_test.go b/pkg/ignition/service_config_test.go index a22ab9eb..f635ace9 100644 --- a/pkg/ignition/service_config_test.go +++ b/pkg/ignition/service_config_test.go @@ -60,7 +60,7 @@ func TestIronicAgentService(t *testing.T) { want: ignition_config_types_32.Unit{ Name: "ironic-agent.service", Enabled: pointer.BoolPtr(true), - Contents: pointer.StringPtr("[Unit]\nDescription=Ironic Agent\nAfter=network-online.target\nWants=network-online.target\n[Service]\nEnvironment=\"HTTP_PROXY=\"\nEnvironment=\"HTTPS_PROXY=\"\nEnvironment=\"NO_PROXY=\"\nTimeoutStartSec=0\nRestart=on-failure\nRestartSec=5\nStartLimitIntervalSec=0\nExecStartPre=/bin/podman pull http://example.com/foo:latest --tls-verify=false --authfile=/etc/authfile.json\nExecStart=/bin/podman run --rm --privileged --network host --mount type=bind,src=/etc/ironic-python-agent.conf,dst=/etc/ironic-python-agent/ignition.conf --mount type=bind,src=/dev,dst=/dev --mount type=bind,src=/sys,dst=/sys --mount type=bind,src=/run/dbus/system_bus_socket,dst=/run/dbus/system_bus_socket --mount type=bind,src=/,dst=/mnt/coreos --mount type=bind,src=/run/udev,dst=/run/udev --ipc=host --env \"IPA_COREOS_IP_OPTIONS=ip=dhcp6\" --env IPA_COREOS_COPY_NETWORK=false --name ironic-agent http://example.com/foo:latest\n[Install]\nWantedBy=multi-user.target\n"), + Contents: pointer.StringPtr("[Unit]\nDescription=Ironic Agent\nAfter=network-online.target\nWants=network-online.target\n[Service]\nEnvironment=\"HTTP_PROXY=\"\nEnvironment=\"HTTPS_PROXY=\"\nEnvironment=\"NO_PROXY=\"\nTimeoutStartSec=0\nRestart=on-failure\nRestartSec=5\nStartLimitIntervalSec=0\nExecStartPre=/bin/podman pull http://example.com/foo:latest --tls-verify=false --authfile=/etc/authfile.json\nExecStart=/bin/podman run --rm --privileged --network host --mount type=bind,src=/etc/ironic-python-agent.conf,dst=/etc/ironic-python-agent/ignition.conf --mount type=bind,src=/dev,dst=/dev --mount type=bind,src=/sys,dst=/sys --mount type=bind,src=/run/dbus/system_bus_socket,dst=/run/dbus/system_bus_socket --mount type=bind,src=/,dst=/mnt/coreos --mount type=bind,src=/run/udev,dst=/run/udev --ipc=host --uts=host --env \"IPA_COREOS_IP_OPTIONS=ip=dhcp6\" --env IPA_COREOS_COPY_NETWORK=false --name ironic-agent http://example.com/foo:latest\n[Install]\nWantedBy=multi-user.target\n"), }, }, { @@ -69,7 +69,7 @@ func TestIronicAgentService(t *testing.T) { want: ignition_config_types_32.Unit{ Name: "ironic-agent.service", Enabled: pointer.BoolPtr(true), - Contents: pointer.StringPtr("[Unit]\nDescription=Ironic Agent\nAfter=network-online.target\nWants=network-online.target\n[Service]\nEnvironment=\"HTTP_PROXY=\"\nEnvironment=\"HTTPS_PROXY=\"\nEnvironment=\"NO_PROXY=\"\nTimeoutStartSec=0\nRestart=on-failure\nRestartSec=5\nStartLimitIntervalSec=0\nExecStartPre=/bin/podman pull http://example.com/foo:latest --tls-verify=false\nExecStart=/bin/podman run --rm --privileged --network host --mount type=bind,src=/etc/ironic-python-agent.conf,dst=/etc/ironic-python-agent/ignition.conf --mount type=bind,src=/dev,dst=/dev --mount type=bind,src=/sys,dst=/sys --mount type=bind,src=/run/dbus/system_bus_socket,dst=/run/dbus/system_bus_socket --mount type=bind,src=/,dst=/mnt/coreos --mount type=bind,src=/run/udev,dst=/run/udev --ipc=host --env \"IPA_COREOS_IP_OPTIONS=ip=dhcp6\" --env IPA_COREOS_COPY_NETWORK=false --name ironic-agent http://example.com/foo:latest\n[Install]\nWantedBy=multi-user.target\n"), + Contents: pointer.StringPtr("[Unit]\nDescription=Ironic Agent\nAfter=network-online.target\nWants=network-online.target\n[Service]\nEnvironment=\"HTTP_PROXY=\"\nEnvironment=\"HTTPS_PROXY=\"\nEnvironment=\"NO_PROXY=\"\nTimeoutStartSec=0\nRestart=on-failure\nRestartSec=5\nStartLimitIntervalSec=0\nExecStartPre=/bin/podman pull http://example.com/foo:latest --tls-verify=false\nExecStart=/bin/podman run --rm --privileged --network host --mount type=bind,src=/etc/ironic-python-agent.conf,dst=/etc/ironic-python-agent/ignition.conf --mount type=bind,src=/dev,dst=/dev --mount type=bind,src=/sys,dst=/sys --mount type=bind,src=/run/dbus/system_bus_socket,dst=/run/dbus/system_bus_socket --mount type=bind,src=/,dst=/mnt/coreos --mount type=bind,src=/run/udev,dst=/run/udev --ipc=host --uts=host --env \"IPA_COREOS_IP_OPTIONS=ip=dhcp6\" --env IPA_COREOS_COPY_NETWORK=false --name ironic-agent http://example.com/foo:latest\n[Install]\nWantedBy=multi-user.target\n"), }, }, { @@ -80,7 +80,7 @@ func TestIronicAgentService(t *testing.T) { want: ignition_config_types_32.Unit{ Name: "ironic-agent.service", Enabled: pointer.BoolPtr(true), - Contents: pointer.StringPtr("[Unit]\nDescription=Ironic Agent\nAfter=network-online.target\nWants=network-online.target\n[Service]\nEnvironment=\"HTTP_PROXY=\"\nEnvironment=\"HTTPS_PROXY=\"\nEnvironment=\"NO_PROXY=\"\nTimeoutStartSec=0\nRestart=on-failure\nRestartSec=5\nStartLimitIntervalSec=0\nExecStartPre=/bin/podman pull http://example.com/foo:latest --tls-verify=false --authfile=/etc/authfile.json\nExecStart=/bin/podman run --rm --privileged --network host --mount type=bind,src=/etc/ironic-python-agent.conf,dst=/etc/ironic-python-agent/ignition.conf --mount type=bind,src=/dev,dst=/dev --mount type=bind,src=/sys,dst=/sys --mount type=bind,src=/run/dbus/system_bus_socket,dst=/run/dbus/system_bus_socket --mount type=bind,src=/,dst=/mnt/coreos --mount type=bind,src=/run/udev,dst=/run/udev --ipc=host --env \"IPA_COREOS_IP_OPTIONS=ip=dhcp6\" --env IPA_COREOS_COPY_NETWORK=true --name ironic-agent http://example.com/foo:latest\n[Install]\nWantedBy=multi-user.target\n"), + Contents: pointer.StringPtr("[Unit]\nDescription=Ironic Agent\nAfter=network-online.target\nWants=network-online.target\n[Service]\nEnvironment=\"HTTP_PROXY=\"\nEnvironment=\"HTTPS_PROXY=\"\nEnvironment=\"NO_PROXY=\"\nTimeoutStartSec=0\nRestart=on-failure\nRestartSec=5\nStartLimitIntervalSec=0\nExecStartPre=/bin/podman pull http://example.com/foo:latest --tls-verify=false --authfile=/etc/authfile.json\nExecStart=/bin/podman run --rm --privileged --network host --mount type=bind,src=/etc/ironic-python-agent.conf,dst=/etc/ironic-python-agent/ignition.conf --mount type=bind,src=/dev,dst=/dev --mount type=bind,src=/sys,dst=/sys --mount type=bind,src=/run/dbus/system_bus_socket,dst=/run/dbus/system_bus_socket --mount type=bind,src=/,dst=/mnt/coreos --mount type=bind,src=/run/udev,dst=/run/udev --ipc=host --uts=host --env \"IPA_COREOS_IP_OPTIONS=ip=dhcp6\" --env IPA_COREOS_COPY_NETWORK=true --name ironic-agent http://example.com/foo:latest\n[Install]\nWantedBy=multi-user.target\n"), }, }, }