#!/usr/bin/perl

use strict;
use warnings;

# Generate the kickstart templates for a cluster automatically since they're mostly identical.

my $cluster_name = "aster";
my $starting_host_number = 1;
my $number_of_hosts = 20;
my $starting_ip_byte4 = 10;

my $outside_1g_interface = "eno8303";
my $outside_1g_network = "10.27.1";
my $outside_1g_gateway = "10.27.1.1";
my $outside_1g_netmask = "255.255.255.0";

my $inside_10g_interface_a = "ens1f0";
my $inside_10g_network_a = "192.168.53";
my $inside_10g_netmask_a = "255.255.255.0";

my $inside_10g_interface_b = "ens1f1";
my $inside_10g_network_b = "192.168.54";
my $inside_10g_netmask_b = "255.255.255.0";

open my $boot_cmd_fh, '>', $cluster_name . "_boot_cmds.sh" or die "$cluster_name" . "_boot_cmds.sh: $!";

for (my $host_number =0; $host_number < $number_of_hosts; $host_number++) {
    printf $boot_cmd_fh "ip=%s.%d::%s:%s:%s%d.cs.pitt.edu:%s:none nameserver=10.10.10.10 nameserver=10.10.20.10 inst.ks=http://install.cs.pitt.edu/alma9/kickstart/%s%d.cfg\n", $outside_1g_network, $starting_ip_byte4 + $host_number, $outside_1g_gateway, $outside_1g_netmask, $cluster_name, $starting_host_number + $host_number, $outside_1g_interface, $cluster_name, $starting_host_number + $host_number;

    my $output_filename = sprintf("%s%d", $cluster_name, $starting_host_number + $host_number);
    open my $fh, '>', $output_filename or die "$output_filename: $!";

    printf $fh "# Pitt CS Kickstart file for AlmaLinux 9 on %s%d.cs.pitt.edu\n", $cluster_name, $starting_host_number + $host_number;
    printf $fh "\n";
    printf $fh "#version=RHEL9\n";
    printf $fh "ignoredisk --only-use=sda\n";
    printf $fh "# Partition clearing information\n";
    printf $fh "clearpart --all --initlabel\n";
    printf $fh "# Disk partitions and logical volumes\n";
    printf $fh "part /boot/efi --fstype=vfat --size=640 --ondisk=sda\n";
    printf $fh "part /boot --fstype=xfs --size=1074 --ondisk=sda\n";
    printf $fh "part pv.sda --grow --size=1 --ondisk=sda\n";
    printf $fh "volgroup vg_cen9 --pesize=4096 pv.sda\n";
    printf $fh "logvol / --fstype=ext4 --name=lv_root --vgname=vg_cen9 --size=1024 --maxsize=51200\n";
    printf $fh "logvol /usr --fstype=ext4 --name=lv_usr --vgname=vg_cen9 --size=24576\n";
    printf $fh "logvol /tmp --fstype=ext4 --name=lv_tmp --vgname=vg_cen9 --size=16384\n";
    printf $fh "logvol /var --fstype=ext4 --name=lv_var --vgname=vg_cen9 --size=32768\n";
    printf $fh "logvol /opt --fstype=ext4 --name=lv_opt --vgname=vg_cen9 --size=16384\n";
    printf $fh "logvol /home --fstype=ext4 --name=lv_home --vgname=vg_cen9 --size=524288\n";
    printf $fh "logvol swap --name=lv_swap --vgname=vg_cen9 --size=32768\n";
    printf $fh "# Authentication\n";
    printf $fh "authselect select winbind with-mkhomedir\n";
    printf $fh "# Turn off security-enhanced Linux\n";
    printf $fh "selinux --disabled\n";
    printf $fh "# Boot loader\n";
    printf $fh "bootloader --location=mbr --append=\"crashkernel=auto rhgb quiet consoleblank=0\" --boot-drive=sda\n";
    printf $fh "\n";
    printf $fh "# Use graphical install\n";
    printf $fh "#graphical\n";
    printf $fh "# Use text install\n";
    printf $fh "text\n";
    printf $fh "##repo --name=\"BaseOS\" --baseurl=http://mirror-int.cs.pitt.edu/almalinux/9/BaseOS/x86_64/kickstart/ --cost=100\n";
    printf $fh "##repo --name=\"AppStream\" --baseurl=http://mirror-int.cs.pitt.edu/almalinux/9/AppStream/x86_64/kickstart/\n";
    printf $fh "# Use our mirror for installation\n";
    printf $fh "##url --url=http://mirror-int.cs.pitt.edu/almalinux/9/BaseOS/x86_64/kickstart\n";
    printf $fh "# Keyboard layouts\n";
    printf $fh "keyboard --xlayouts='us'\n";
    printf $fh "# System language\n";
    printf $fh "lang en_US.UTF-8\n";
    printf $fh "# Network information\n";
    printf $fh "network --bootproto static --device=%s --ip=%s.%d --netmask=%s --gateway=%s --nameserver=10.10.10.10,10.10.20.10 --noipv6 --onboot=on --activate\n", $outside_1g_interface, $outside_1g_network, $starting_ip_byte4 + $host_number, $outside_1g_netmask, $outside_1g_gateway;
    printf $fh "network --bootproto static --device=%s --ip=%s.%d --netmask=%s --onboot=on --activate\n", $inside_10g_interface_a, $inside_10g_network_a, $starting_ip_byte4 + $host_number, $inside_10g_netmask_a;
    printf $fh "network --bootproto static --device=%s --ip=%s.%d --netmask=%s --onboot=on --activate\n", $inside_10g_interface_b, $inside_10g_network_b, $starting_ip_byte4 + $host_number, $inside_10g_netmask_b;
    printf $fh "network --hostname=%s%d.cs.pitt.edu\n", $cluster_name, $starting_host_number + $host_number;
    printf $fh "# Root password\n";
    printf $fh "rootpw --iscrypted \$6\$bXw3FW8Ly0b6J.V9\$oceMRKq950IpKhhTP8QW5xn9PD1ILk8z5rhKPmOk.gB8OinGR2HAhtA8ynlVVQ6/gRdEECwxEU.hpolvXaant1\n";
    printf $fh "# X Window System configuration information\n";
    printf $fh "#xconfig --startxonboot\n";
    printf $fh "# Disable the initial setup after first boot\n";
    printf $fh "firstboot --disabled\n";
    printf $fh "# Disable EULA prompt after first boot\n";
    printf $fh "eula --agreed\n";
    printf $fh "# System services\n";
    printf $fh "services --enabled=\"chronyd\"\n";
    printf $fh "# System timezone\n";
    printf $fh "timezone America/New_York\n";
    printf $fh "\n";
    printf $fh "\n";
    printf $fh "\n";
    printf $fh "\n";
    printf $fh "%%packages\n";
    printf $fh "%%end\n";
    printf $fh "#%%pre\n";
    printf $fh "#DISKS=\"/dev/sda\"\n";
    printf $fh "#for DISK in \${DISKS}; do\n";
    printf $fh "#    parted -s \${DISK} print 2>&1 |grep \"unrecognised disk label\" > /dev/null\n";
    printf $fh "#    if [ \$? -eq 0 ]; then\n";
    printf $fh "#        parted -s \${DISK} mklabel gpt\n";
    printf $fh "#    fi\n";
    printf $fh "#done\n";
    printf $fh "#%%end\n";
    printf $fh "%%post\n";
    printf $fh "( yum -y install conda dkms htop imake iperf kstart picosat-libs python3-conda python3-conda-package-handling python3-cpuinfo python3-cytoolz python3-frozendict python3-pyOpenSSL python3-pycosat python3-toolz python3-tqdm qt5-qtwebkit qt5-qtwebkit-devel tofrodos\n";
    printf $fh "  yum-config-manager --enable crb\n";
    printf $fh "  yum makecache\n";
    printf $fh "  yum install swig\n";
    printf $fh "  mv /etc/hosts /etc/hosts.orig\n";
    printf $fh "  cat > /etc/hosts << \\EOFEOF\n";
    printf $fh "127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4\n";
    printf $fh "::1         localhost localhost.localdomain localhost6 localhost6.localdomain6\n";
    printf $fh "\n";
    for (my $host_a = 0; $host_a < $number_of_hosts; $host_a++) {
        printf $fh "%s.%d    %s%d-10gb-a\n", $inside_10g_network_a, $starting_ip_byte4 + $host_a, $cluster_name, $starting_host_number + $host_a;
    }
    printf $fh "\n";
    for (my $host_b = 0; $host_b < $number_of_hosts; $host_b++) {
        printf $fh "%s.%d    %s%d-10gb-b\n", $inside_10g_network_b, $starting_ip_byte4 + $host_b, $cluster_name, $starting_host_number + $host_b;
    }
    printf $fh "EOFEOF\n";
    printf $fh "\n";
    printf $fh "  echo \"babay        ALL=(ALL)       ALL\" > /etc/sudoers.d/babay\n";
    printf $fh "  cat > /etc/sudoers.d/students << \\EOFEOF\n";
    printf $fh "hun13        ALL=(ALL)       ALL\n";
    printf $fh "ara128       ALL=(ALL)       ALL\n";
    printf $fh "sahiti       ALL=(ALL)       ALL\n";
    printf $fh "EOFEOF\n";
    printf $fh "  chmod 440 /etc/sudoers.d/babay /etc/sudoers.d/students\n";
    printf $fh "\n";
    printf $fh "mv /etc/security/pam_winbind.conf /etc/security/pam_winbind.conf.orig\n";
    printf $fh "sed -e \"s/^;krb5_auth =.*/krb5_auth = yes/\" -e \"s/^;krb5_ccache_type =.*/krb5_ccache_type = FILE:\/tmp\/krb5cc_%%u/\" /etc/security/pam_winbind.conf.orig > /etc/security/pam_winbind.conf\n";
    printf $fh "\n";
    printf $fh "mv /etc/samba/smb.conf /etc/samba/smb.conf.orig\n";
    printf $fh "  cat > /etc/samba/smb.conf << \\EOFEOF\n";
    printf $fh "[global]\n";
    printf $fh "   workgroup = PITT\n";
    printf $fh "   server string = %%h server (Samba, Ubuntu)\n";
    printf $fh "   log file = /var/log/samba/log.%%m\n";
    printf $fh "   max log size = 1000\n";
    printf $fh "   logging = file\n";
    printf $fh "   panic action = /usr/share/samba/panic-action %%d\n";
    printf $fh "   server role = auto\n";
    printf $fh "   security = ads\n";
    printf $fh "   realm = UNIV.PITT.EDU\n";
    printf $fh "   password server = pitt-proddc-01.cssd.pitt.edu\n";
    printf $fh "   template homedir = /home/%%U\n";
    printf $fh "EOFEOF\n";
    printf $fh "echo \"   netbios name =  CS-%s%d\" >> /etc/samba/smb.conf\n", uc($cluster_name), $starting_host_number + $host_number;
    printf $fh "  cat >> /etc/samba/smb.conf << \\EOFEOF\n";
    printf $fh "# Parameters suggested by CSSD\n";
    printf $fh "   idmap uid = 16777216-33554431\n";
    printf $fh "   idmap gid = 16777216-33554431\n";
    printf $fh "   template shell = /bin/bash\n";
    printf $fh "   winbind use default domain = true\n";
    printf $fh "   winbind offline logon = false\n";
    printf $fh "   winbind nested groups = yes\n";
    printf $fh "   encrypt passwords = yes\n";
    printf $fh "   kerberos encryption types = all\n";
    printf $fh "   require strong key = yes\n";
    printf $fh "   allow nt4 crypto = yes\n";
    printf $fh "EOFEOF\n";
    printf $fh "  cat >> /etc/yum.repos.d/SCI.repo << \\EOFEOF\n";
    printf $fh "[sci]\n";
    printf $fh "name=SCI packages for AlmaLinux 9 - \$basearch\n";
    printf $fh "baseurl=http://install.cs.pitt.edu/alma9/\$basearch\n";
    printf $fh "enabled=1\n";
    printf $fh "EOFEOF\n";
    printf $fh "mv /etc/krb5.conf /etc/krb5.conf.orig\n";
    printf $fh "  cat > /etc/krb5.conf << \\EOFEOF\n";
    printf $fh "# To opt out of the system crypto-policies configuration of krb5, remove the\n";
    printf $fh "# symlink at /etc/krb5.conf.d/crypto-policies which will not be recreated.\n";
    printf $fh "includedir /etc/krb5.conf.d/\n";
    printf $fh "\n";
    printf $fh "[logging]\n";
    printf $fh "    default = FILE:/var/log/krb5libs.log\n";
    printf $fh "    kdc = FILE:/var/log/krb5kdc.log\n";
    printf $fh "    admin_server = FILE:/var/log/kadmind.log\n";
    printf $fh "\n";
    printf $fh "[libdefaults]\n";
    printf $fh "    dns_lookup_realm = false\n";
    printf $fh "    dns_lookup_kdc = false\n";
    printf $fh "    ticket_lifetime = 25h\n";
    printf $fh "    renew_lifetime = 8d\n";
    printf $fh "    clockskew = 300\n";
    printf $fh "    forwardable = true\n";
    printf $fh "    rdns = false\n";
    printf $fh "    pkinit_anchors = FILE:/etc/pki/tls/certs/ca-bundle.crt\n";
    printf $fh "    spake_preauth_groups = edwards25519\n";
    printf $fh "    dns_canonicalize_hostname = fallback\n";
    printf $fh "    qualify_shortname = \"\"\n";
    printf $fh "    default_realm = UNIV.PITT.EDU\n";
    printf $fh "    default_ccache_name = KEYRING:persistent:%%{uid}\n";
    printf $fh "\n";
    printf $fh "[realms]\n";
    printf $fh "    UNIV.PITT.EDU = {\n";
    printf $fh "     kdc = pitt-proddc-01.cssd.pitt.edu\n";
    printf $fh "     kdc = pitt-proddc-02.cssd.pitt.edu\n";
    printf $fh "     kdc = pitt-proddc-03.cssd.pitt.edu\n";
    printf $fh "     kdc = pitt-proddc-04.cssd.pitt.edu\n";
    printf $fh "     kdc = pitt-proddc-05.cssd.pitt.edu\n";
    printf $fh "     admin_server = pitt-proddc-01.cssd.pitt.edu\n";
    printf $fh "     default_domain = univ.pitt.edu\n";
    printf $fh "    }\n";
    printf $fh "\n";
    printf $fh "[domain_realm]\n";
    printf $fh "    .univ.pitt.edu = UNIV.PITT.EDU\n";
    printf $fh "    univ.pitt.edu = UNIV.PITT.EDU\n";
    printf $fh "\n";
    printf $fh "[appdefaults]\n";
    printf $fh "    pam = {\n";
    printf $fh "     ticket_lifetime = 25h\n";
    printf $fh "     renew_lifetime = 8d\n";
    printf $fh "     forwardable = true\n";
    printf $fh "     debug = false\n";
    printf $fh "     ignore_afs = true\n";
    printf $fh "     retain_after_close = false\n";
    printf $fh "    }\n";
    printf $fh "EOFEOF\n";
    printf $fh "  sync\n";
    printf $fh ") 1>/root/post_install.log 2>&1\n";
    printf $fh "%%end\n";
    printf $fh "\n";
    printf $fh "%%addon com_redhat_kdump --enable --reserve-mb='auto'\n";
    printf $fh "\n";
    printf $fh "%%end\n";
}
