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 Jun 11, 2024
1 parent 0085153 commit d68578a
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,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

<% if host_param_true?('use_graphical_installer') -%>
Expand All @@ -387,10 +389,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,20 @@
<%#
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 /root/first_boot_script.sh
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,29 @@
<%#
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)
-%>
<% if has_systemd -%>
<%= save_to_file('/etc/systemd/system/first_boot_service.service', snippet('first_boot_service')) %>
<%= save_to_file('/root/first_boot_script.sh', snippet('built', :variables => { :endpoint => 'built', :method => 'POST', :body_file => '/root/install.post.log' })) %>

systemctl enable first_boot_service
<% else -%>
<%= save_to_file('/root/first_boot_script.sh', snippet('built', :variables => { :endpoint => 'built', :method => 'POST', :body_file => '/root/install.post.log' }) +
"\nmv /root/first_boot_script.sh /root/first_boot_script.disabled") %>
<%= save_to_file('/etc/init.d/first_boot_setup', snippet('first_boot_initd')) %>

chmod +x /etc/init.d/first_boot_setup
chkconfig --add /etc/init.d/first_boot_setup
chkconfig --level 2345 first_boot_setup on
<% end -%>

chmod +x /root/first_boot_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<%#
kind: snippet
name: first_boot_initd
model: ProvisioningTemplate
snippet: true
description: |
Post replacement initd script
-%>
#! /bin/bash

### BEGIN INIT INFO
# Provides: RedHat
# Required-Start: $local_fs $network
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Execute first boot script
# Description: Execute first boot script which executes after machine is successfully built for the first time.
### 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 $?
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,35 @@ systemctl enable ansible-callback



cat << EOF-782969c0 > /etc/systemd/system/first_boot_service.service
[Unit]
Description=Initial setup callback
Wants=network-online.target
After=network-online.target

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

[Install]
WantedBy=multi-user.target
EOF-782969c0
cat << EOF-76fe21d9 > /root/first_boot_script.sh
if [ -x /usr/bin/curl ]; then
/usr/bin/curl -o /dev/null --noproxy \* -H 'Content-Type: text/plain' --data @/root/install.post.log --silent 'http://foreman.example.com/unattended/built'
elif [ -x /usr/bin/wget ]; then
/usr/bin/wget -q -O /dev/null --no-proxy --method POST --header 'Content-Type: text/plain' --body-file=/root/install.post.log 'http://foreman.example.com/unattended/built'
else
wget -q -O /dev/null --header 'Content-Type: text/plain' 'http://foreman.example.com/unattended/built'
fi
EOF-76fe21d9

systemctl enable first_boot_service

chmod +x /root/first_boot_script.sh


touch /tmp/foreman_built

chvt 1
Expand All @@ -179,16 +208,7 @@ cp -vf /tmp/*.pre.*.log /mnt/sysimage/root/
%post --erroronfail --log=/root/install-callhome.post.log


if test -f /tmp/foreman_built; then
echo "calling home: build is done!"
if [ -x /usr/bin/curl ]; then
/usr/bin/curl -o /dev/null --noproxy \* -H 'Content-Type: text/plain' --data @/root/install.post.log --silent 'http://foreman.example.com/unattended/built'
elif [ -x /usr/bin/wget ]; then
/usr/bin/wget -q -O /dev/null --no-proxy --method POST --header 'Content-Type: text/plain' --body-file=/root/install.post.log 'http://foreman.example.com/unattended/built'
else
wget -q -O /dev/null --header 'Content-Type: text/plain' 'http://foreman.example.com/unattended/built'
fi
else
if ! test -f /tmp/foreman_built; then
echo "calling home: build failed!"
if [ -x /usr/bin/curl ]; then
/usr/bin/curl -o /dev/null --noproxy \* -H 'Content-Type: text/plain' --data @/root/install.post.log --silent 'http://foreman.example.com/unattended/failed'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,35 @@ systemctl enable ansible-callback



cat << EOF-782969c0 > /etc/systemd/system/first_boot_service.service
[Unit]
Description=Initial setup callback
Wants=network-online.target
After=network-online.target

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

[Install]
WantedBy=multi-user.target
EOF-782969c0
cat << EOF-76fe21d9 > /root/first_boot_script.sh
if [ -x /usr/bin/curl ]; then
/usr/bin/curl -o /dev/null --noproxy \* -H 'Content-Type: text/plain' --data @/root/install.post.log --silent 'http://foreman.example.com/unattended/built'
elif [ -x /usr/bin/wget ]; then
/usr/bin/wget -q -O /dev/null --no-proxy --method POST --header 'Content-Type: text/plain' --body-file=/root/install.post.log 'http://foreman.example.com/unattended/built'
else
wget -q -O /dev/null --header 'Content-Type: text/plain' 'http://foreman.example.com/unattended/built'
fi
EOF-76fe21d9

systemctl enable first_boot_service

chmod +x /root/first_boot_script.sh


touch /tmp/foreman_built

chvt 1
Expand All @@ -179,16 +208,7 @@ cp -vf /tmp/*.pre.*.log /mnt/sysimage/root/
%post --erroronfail --log=/root/install-callhome.post.log


if test -f /tmp/foreman_built; then
echo "calling home: build is done!"
if [ -x /usr/bin/curl ]; then
/usr/bin/curl -o /dev/null --noproxy \* -H 'Content-Type: text/plain' --data @/root/install.post.log --silent 'http://foreman.example.com/unattended/built'
elif [ -x /usr/bin/wget ]; then
/usr/bin/wget -q -O /dev/null --no-proxy --method POST --header 'Content-Type: text/plain' --body-file=/root/install.post.log 'http://foreman.example.com/unattended/built'
else
wget -q -O /dev/null --header 'Content-Type: text/plain' 'http://foreman.example.com/unattended/built'
fi
else
if ! test -f /tmp/foreman_built; then
echo "calling home: build failed!"
if [ -x /usr/bin/curl ]; then
/usr/bin/curl -o /dev/null --noproxy \* -H 'Content-Type: text/plain' --data @/root/install.post.log --silent 'http://foreman.example.com/unattended/failed'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,35 @@ systemctl enable ansible-callback



cat << EOF-782969c0 > /etc/systemd/system/first_boot_service.service
[Unit]
Description=Initial setup callback
Wants=network-online.target
After=network-online.target

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

[Install]
WantedBy=multi-user.target
EOF-782969c0
cat << EOF-76fe21d9 > /root/first_boot_script.sh
if [ -x /usr/bin/curl ]; then
/usr/bin/curl -o /dev/null --noproxy \* -H 'Content-Type: text/plain' --data @/root/install.post.log --silent 'http://foreman.example.com/unattended/built'
elif [ -x /usr/bin/wget ]; then
/usr/bin/wget -q -O /dev/null --no-proxy --method POST --header 'Content-Type: text/plain' --body-file=/root/install.post.log 'http://foreman.example.com/unattended/built'
else
wget -q -O /dev/null --header 'Content-Type: text/plain' 'http://foreman.example.com/unattended/built'
fi
EOF-76fe21d9

systemctl enable first_boot_service

chmod +x /root/first_boot_script.sh


touch /tmp/foreman_built

chvt 1
Expand All @@ -179,16 +208,7 @@ cp -vf /tmp/*.pre.*.log /mnt/sysimage/root/
%post --erroronfail --log=/root/install-callhome.post.log


if test -f /tmp/foreman_built; then
echo "calling home: build is done!"
if [ -x /usr/bin/curl ]; then
/usr/bin/curl -o /dev/null --noproxy \* -H 'Content-Type: text/plain' --data @/root/install.post.log --silent 'http://foreman.example.com/unattended/built'
elif [ -x /usr/bin/wget ]; then
/usr/bin/wget -q -O /dev/null --no-proxy --method POST --header 'Content-Type: text/plain' --body-file=/root/install.post.log 'http://foreman.example.com/unattended/built'
else
wget -q -O /dev/null --header 'Content-Type: text/plain' 'http://foreman.example.com/unattended/built'
fi
else
if ! test -f /tmp/foreman_built; then
echo "calling home: build failed!"
if [ -x /usr/bin/curl ]; then
/usr/bin/curl -o /dev/null --noproxy \* -H 'Content-Type: text/plain' --data @/root/install.post.log --silent 'http://foreman.example.com/unattended/failed'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,35 @@ systemctl enable ansible-callback



cat << EOF-782969c0 > /etc/systemd/system/first_boot_service.service
[Unit]
Description=Initial setup callback
Wants=network-online.target
After=network-online.target

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

[Install]
WantedBy=multi-user.target
EOF-782969c0
cat << EOF-76fe21d9 > /root/first_boot_script.sh
if [ -x /usr/bin/curl ]; then
/usr/bin/curl -o /dev/null --noproxy \* -H 'Content-Type: text/plain' --data @/root/install.post.log --silent 'http://foreman.example.com/unattended/built'
elif [ -x /usr/bin/wget ]; then
/usr/bin/wget -q -O /dev/null --no-proxy --method POST --header 'Content-Type: text/plain' --body-file=/root/install.post.log 'http://foreman.example.com/unattended/built'
else
wget -q -O /dev/null --header 'Content-Type: text/plain' 'http://foreman.example.com/unattended/built'
fi
EOF-76fe21d9

systemctl enable first_boot_service

chmod +x /root/first_boot_script.sh


touch /tmp/foreman_built

chvt 1
Expand All @@ -179,16 +208,7 @@ cp -vf /tmp/*.pre.*.log /mnt/sysimage/root/
%post --erroronfail --log=/root/install-callhome.post.log


if test -f /tmp/foreman_built; then
echo "calling home: build is done!"
if [ -x /usr/bin/curl ]; then
/usr/bin/curl -o /dev/null --noproxy \* -H 'Content-Type: text/plain' --data @/root/install.post.log --silent 'http://foreman.example.com/unattended/built'
elif [ -x /usr/bin/wget ]; then
/usr/bin/wget -q -O /dev/null --no-proxy --method POST --header 'Content-Type: text/plain' --body-file=/root/install.post.log 'http://foreman.example.com/unattended/built'
else
wget -q -O /dev/null --header 'Content-Type: text/plain' 'http://foreman.example.com/unattended/built'
fi
else
if ! test -f /tmp/foreman_built; then
echo "calling home: build failed!"
if [ -x /usr/bin/curl ]; then
/usr/bin/curl -o /dev/null --noproxy \* -H 'Content-Type: text/plain' --data @/root/install.post.log --silent 'http://foreman.example.com/unattended/failed'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,35 @@ systemctl enable ansible-callback



cat << EOF-782969c0 > /etc/systemd/system/first_boot_service.service
[Unit]
Description=Initial setup callback
Wants=network-online.target
After=network-online.target

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

[Install]
WantedBy=multi-user.target
EOF-782969c0
cat << EOF-76fe21d9 > /root/first_boot_script.sh
if [ -x /usr/bin/curl ]; then
/usr/bin/curl -o /dev/null --noproxy \* -H 'Content-Type: text/plain' --data @/root/install.post.log --silent 'http://foreman.example.com/unattended/built'
elif [ -x /usr/bin/wget ]; then
/usr/bin/wget -q -O /dev/null --no-proxy --method POST --header 'Content-Type: text/plain' --body-file=/root/install.post.log 'http://foreman.example.com/unattended/built'
else
wget -q -O /dev/null --header 'Content-Type: text/plain' 'http://foreman.example.com/unattended/built'
fi
EOF-76fe21d9

systemctl enable first_boot_service

chmod +x /root/first_boot_script.sh


touch /tmp/foreman_built

chvt 1
Expand All @@ -179,16 +208,7 @@ cp -vf /tmp/*.pre.*.log /mnt/sysimage/root/
%post --erroronfail --log=/root/install-callhome.post.log


if test -f /tmp/foreman_built; then
echo "calling home: build is done!"
if [ -x /usr/bin/curl ]; then
/usr/bin/curl -o /dev/null --noproxy \* -H 'Content-Type: text/plain' --data @/root/install.post.log --silent 'http://foreman.example.com/unattended/built'
elif [ -x /usr/bin/wget ]; then
/usr/bin/wget -q -O /dev/null --no-proxy --method POST --header 'Content-Type: text/plain' --body-file=/root/install.post.log 'http://foreman.example.com/unattended/built'
else
wget -q -O /dev/null --header 'Content-Type: text/plain' 'http://foreman.example.com/unattended/built'
fi
else
if ! test -f /tmp/foreman_built; then
echo "calling home: build failed!"
if [ -x /usr/bin/curl ]; then
/usr/bin/curl -o /dev/null --noproxy \* -H 'Content-Type: text/plain' --data @/root/install.post.log --silent 'http://foreman.example.com/unattended/failed'
Expand Down
Loading

0 comments on commit d68578a

Please sign in to comment.