Skip to content

Commit

Permalink
Fixes #35378 - Add systemd first boot service for host provisioning
Browse files Browse the repository at this point in the history
  • Loading branch information
Dyrkon committed Aug 29, 2023
1 parent df3a0b0 commit 3fbedce
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ sed -e 's/DEFAULTKERNEL=kernel-uek/DEFAULTKERNEL=kernel/g' -i /etc/sysconfig/ker

<%= snippet 'insights' if host_param_true?('host_registration_insights') && os_major < 9 -%>

<%= snippet 'first_boot_setup' %>

touch /tmp/foreman_built

chvt 1
Expand All @@ -382,10 +384,7 @@ The last post section halts Anaconda to prevent endless loop in case HTTP reques

<%= snippet 'eject_cdrom' -%>

if test -f /tmp/foreman_built; then
echo "calling home: build is done!"
<%= indent(2, skip1: true) { snippet('built', :variables => { :endpoint => 'built', :method => 'POST', :body_file => '/root/install.post.log' }) } -%>
else
if ! test -f /tmp/foreman_built; then
echo "calling home: build failed!"
<%= indent(2, skip1: true) { snippet('built', :variables => { :endpoint => 'failed', :method => 'POST', :body_file => '/root/install.post.log' }) } -%>
fi
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<%#
kind: snippet
name: first_boot_service
model: ProvisioningTemplate
snippet: true
description: |
Post replacement service
-%>
[Unit]
Description=Initial setup callback
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/bin/bash /tmp/first_boot_script.sh
RemainAfterExit=true
Type=oneshot
ExecStartPost=/usr/bin/systemctl disable first_boot_service

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<%#
kind: snippet
name: first_boot_setup
model: ProvisioningTemplate
snippet: true
description: |
Post replacement for both systemd and non-systemd platforms
-%>
<%
os_major = @host.operatingsystem.major.to_i
rhel_compatible = @host.operatingsystem.family == 'Redhat' && @host.operatingsystem.name != 'Fedora'
has_systemd = (@host.operatingsystem.name == 'Fedora' && os_major >= 20) || (rhel_compatible && os_major >= 7) || (@host.operatingsystem.name == 'Ubuntu' && os_major >= 15) || (@host.operatingsystem.name == 'Debian' && os_major >= 8)
initd_script = """
#! /bin/bash

### BEGIN INIT INFO
# Provides: foo
# Required-Start: $local_fs $network
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: foo service
# Description: Run Foo service
### END INIT INFO

# Source function library
. /etc/rc.d/init.d/functions

##Service start/stop functions##
start() {
/tmp/first_boot_script
chkconfig --del first_boot_setup
}

start

exit 5
esac
exit $?
"""
%>
<% if has_systemd -%>
<%= save_to_file('/etc/systemd/system/first_boot_service.service', snippet('first_boot_service')) %>
<%= save_to_file('/tmp/first_boot_script', snippet('built', :variables => { :endpoint => 'built', :method => 'POST', :body_file => '/root/install.post.log' })) %>
systemctl enable first_boot_service
chmod +x /tmp/first_boot_script
<% else %>
<%= save_to_file('/tmp/first_boot_script', snippet('built', :variables => { :endpoint => 'built', :method => 'POST', :body_file => '/root/install.post.log' }) +
"\nmv /tmp/first_boot_script /tmp/first_boot_script.disabled") %>
<%= save_to_file('/etc/init.d/first_boot_setup', initd_script) %>
chmod +x /tmp/first_boot_script
chmod +x /etc/init.d/first_boot_setup
chkconfig --add /etc/init.d/first_boot_setup
chkconfig --level 2345 first_boot_setup on
<% end %>

0 comments on commit 3fbedce

Please sign in to comment.