File: /home/prepare_for_snapshot.pl
#!/usr/local/cpanel/3rdparty/bin/perl
# Copyright 2024 WebPros International, LLC
# All rights reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited.
#
use strict;
use warnings;
use Cpanel::Binaries ();
use Cpanel::ImagePrep ();
use Cpanel::Slurper ();
# The purpose of this script is to take a cPanel machine and prepare it
# by cleaning all cPanel/WHM per-instance secrets and keys so it can be
# snapshotted by packer. This will also install a script that will autorun
# upcp on the first boot.
#################################################
#
# install_post_script () - installs a run on boot script.
# The run on boot script does 2 things:
# * Run upcp to get this vm up to date quickly
# * Remove this run on boot script, so this only runs once on the first time
sub install_post_script() {
my $on_boot_dir = '/usr/local/cpanel/libexec/on_boot';
if ( !-d $on_boot_dir ) {
die "install_post_script cannot write to $on_boot_dir\n";
}
my $script = "$on_boot_dir/00_run_onetime_vm_scripts.sh";
my $bash_bin = Cpanel::Binaries::path('bash');
my $unlink_bin = Cpanel::Binaries::path('unlink');
Cpanel::Slurper::write(
$script,
<<"EOF"
#!${bash_bin}
set -x
/usr/local/cpanel/scripts/upcp
# auto remove itself on first run
$unlink_bin $script
EOF
, 0700
);
Cpanel::ImagePrep::common()->regular_logmsg('Installed run_onetime_vm_scripts on boot.');
return 1;
}
install_post_script();
# Run the script that removes secrets and other cPanel specific per-instance data
system("/scripts/snapshot_prep --yes");
# Clean out artifacts (probably not necessary but doing anyway)
unlink "/etc/cpsources.conf";
Cpanel::Slurper::write(
"/etc/cpupdate.conf",
<<"EOF"
CPANEL=release
RPMUP=daily
SARULESUP=daily
STAGING_DIR=/usr/local/cpanel
UPDATES=daily
EOF
, 0644
);
# Because the pre and post snapshot scripts are in ULC, if any new secrets or files that
# need to be cleaned are discovered or if the logic changes, the changes to do so are merged
# to the current working branch which is different from the current RELEASE cPanel version which
# is what we deploy to cloud providers. We likely still want to clean these in the version we will
# deploy so we do that here
unlink "/etc/cpanel/TIERS.json";