From 572b6ccd6afcd335f6c7934e73875136b6918259 Mon Sep 17 00:00:00 2001 From: Beth Date: Sun, 23 Mar 2025 06:24:25 -0500 Subject: [PATCH] merged create and create_alma --- scripts/create.sh | 117 +++++++++++++++++------------ scripts/create_alma.sh | 162 ----------------------------------------- 2 files changed, 69 insertions(+), 210 deletions(-) delete mode 100644 scripts/create_alma.sh diff --git a/scripts/create.sh b/scripts/create.sh index 2de38d2..ba8fa2b 100644 --- a/scripts/create.sh +++ b/scripts/create.sh @@ -2,9 +2,9 @@ cd "$(dirname "$(readlink -f "$0")")" #change to prompts w/ default values # set defaults -cpu=1 -memory=1024 -root_disk=10 +cpu=1 +memory=1024 +root_disk=10 variant="archlinux" path="default" dhcp_opts="" @@ -16,7 +16,7 @@ usage="$(basename "$0") [options] -n server_name where: -n name Name of new vm/server (required, caps allowed, not fqdn) -c number Numver of CPU cores (default $cpu) - -r number Ram in MB (default $memory) + -r number Ram in MB (default $memory) -d size Root disk size in GB (default 10) -v variant Linux variant (default archlinux - note should add list of optional variants) -p image_path Path to image (default /virtual/disks/[name].qcow2) @@ -26,7 +26,7 @@ where: -b branch Specify branch of build_ansible to use (defult prod) -a attach Attach existing extra disk (use full path to disK, default none)" # process flags -while getopts "h:n:c:r:d:v:p:m:s:i:b:a:" flag +while getopts "h:n:c:r:d:v:p:m:s:i:b:" flag do case "$flag" in n) name=${OPTARG};; @@ -47,7 +47,7 @@ done dhcp_opts+=" -m $mac" # get name -shift $(($OPTIND - 1)) +shift $(($OPTIND - 1)) if [[ -z "$name" ]];then echo "Server name undefined";echo "$usage";exit;fi @@ -60,54 +60,67 @@ img=`./shared_scripts/download_install_image.sh $variant` #update DHCP ./shared_scripts/update_dhcp.sh $dhcp_opts $lname -case "$variant" in - "arch") - - ;; - "alma") - - ;; -esac -# create cloud-init - replace by grabbing these directly from git -mkdir -p /tmp/cloud-init -echo "#cloud-config -users: - - name: root - ssh_authorized_keys: - - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILG56T1k766Cru14kyXI8WXSJwlO2sVhmH6JeRWy+mfe root@host.actcur.com" > /tmp/cloud-init/user-data - -# ensure correct variant name -if [[ "$variant" -eq "alma" ]];then variant="almalinux9";fi - # determine if extra disk should be attached if [ -n "$attach" ]; then attach="--disk ${attach},discard=unmap";fi -# create new base VM -virt-install --name=$name --vcpus=$cpu --memory=$memory --disk path=$path,size=$root_disk,sparse=true,discard=unmap,bus=scsi ${attach} --os-variant=$variant --network bridge=br1,mac=$mac --memballoon model=virtio,autodeflate=on --cdrom=$img --graphics vnc --cloud-init user-data="/tmp/cloud-init/user-data" --autoconsole=none +case "$variant" in + "arch") + # create cloud-init - replace by grabbing these directly from git + mkdir -p /tmp/cloud-init + echo "#cloud-config + users: + - name: root + ssh_authorized_keys: + - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILG56T1k766Cru14kyXI8WXSJwlO2sVhmH6JeRWy+mfe root@host.actcur.com" > /tmp/cloud-init/user-data -#wait until server is available -echo "Waiting for server to be available" -while true -do - sleep 1 - ping -c 1 ${lname}.actcur.com 2>&1 > /dev/null - if [ $? -eq 0 ]; then break;fi -done -echo "server up, waiting for ssh" + # create new base VM + virt-install --name=$name --vcpus=$cpu --memory=$memory --disk path=$path,size=$root_disk,sparse=true,discard=unmap,bus=scsi ${attach} --os-variant=$variant --network bridge=br1,mac=$mac --memballoon model=virtio,autodeflate=on --cdrom=$img --graphics vnc --cloud-init user-data="/tmp/cloud-init/user-data" --autoconsole=none -sleep 30 + #wait until server is available + echo "Waiting for server to be available" + while true + do + sleep 1 + ping -c 1 ${lname}.actcur.com 2>&1 > /dev/null + if [ $? -eq 0 ]; then break;fi + done + echo "server up, waiting for ssh" -sed "s/REPLACE_HOSTNAME/${lname}.actcur.com/g" ../base_files/user_configuration.json > /tmp/user_configuration.json -rs=$((root_disk*1024-206)) -sed -i "s/REPLACE_SIZE/${rs}/g" /tmp/user_configuration.json + sleep 30 + + sed "s/REPLACE_HOSTNAME/${lname}.actcur.com/g" ../base_files/user_configuration.json > /tmp/user_configuration.json + rs=$((root_disk*1024-206)) + sed -i "s/REPLACE_SIZE/${rs}/g" /tmp/user_configuration.json + + ssh-keygen -R ${lname}.actcur.com + ssh-keyscan ${lname}.actcur.com >> ~/.ssh/known_hosts + scp -i $ssh_key_dir/id_arch_builder /tmp/user_configuration.json ${lname}.actcur.com:/tmp/user_configuration.json + ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "systemctl mask reflector.service; echo 'Server = https://pkg.actcur.com/repo/archlinux/\$repo/os/\$arch' > /etc/pacman.d/mirrorlist" + ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "archinstall --config /tmp/user_configuration.json --silent" + ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "ln -s /usr/lib/systemd/system/sshd.service /mnt/archinstall/etc/systemd/system/multi-user.target.wants/sshd.service" + ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "cp -r /root/.ssh/ /mnt/archinstall/root/;reboot" + ;; + "alma") + # set up kickstart file + sed "s/REPLACE_HOSTNAME/${lname}.actcur.com/g" ../base_files/kickstart.cfg > /tmp/kickstart.cfg + + # create new VM + virt-install --name=$name --vcpus=$cpu --memory=$memory --disk path=$path,size=$root_disk,sparse=true,discard=unmap,bus=scsi --os-variant=almalinux9 --network bridge=br1,mac=$mac --memballoon model=virtio,autodeflate=on --location=$img --graphics vnc --initrd-inject /tmp/kickstart.cfg --extra-args "inst.ks=file:/kickstart.cfg" --autoconsole=none + + echo "server up, waiting for install to complete" + + sleep 60 + + # wait for install to complete and server to shut down/reboot + while true + do + sleep 1 + ping -c 1 ${lname}.actcur.com 2>&1 > /dev/null + if [ $? -ne 0 ]; then break;fi + done + ;; +esac -ssh-keygen -R ${lname}.actcur.com -ssh-keyscan ${lname}.actcur.com >> ~/.ssh/known_hosts -scp -i $ssh_key_dir/id_arch_builder /tmp/user_configuration.json ${lname}.actcur.com:/tmp/user_configuration.json -ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "systemctl mask reflector.service; echo 'Server = https://pkg.actcur.com/repo/archlinux/\$repo/os/\$arch' > /etc/pacman.d/mirrorlist" -ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "archinstall --config /tmp/user_configuration.json --silent" -ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "ln -s /usr/lib/systemd/system/sshd.service /mnt/archinstall/etc/systemd/system/multi-user.target.wants/sshd.service" -ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "cp -r /root/.ssh/ /mnt/archinstall/root/;reboot" sleep 10 virsh start $name @@ -127,7 +140,15 @@ ssh-keygen -R ${lname}.actcur.com ssh-keyscan ${lname}.actcur.com | grep -v "#" | grep "ecdsa-sha2-nistp256" >> ~/.ssh/known_hosts # install some dependencies -ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "pacman -Sy --noconfirm ansible-core git" +case "$variant" in + "arch") + ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "pacman -Sy --noconfirm ansible-core git" + ;; + "alma") + ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "dnf config-manager --set-enabled crb;yum install -y ansible-core git epel-release" + ;; +esac + ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "ansible-galaxy collection install community.general" ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "ansible-galaxy collection install community.crypto" ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "ansible-galaxy collection install ansible.posix" diff --git a/scripts/create_alma.sh b/scripts/create_alma.sh deleted file mode 100644 index dc2b14f..0000000 --- a/scripts/create_alma.sh +++ /dev/null @@ -1,162 +0,0 @@ -#!/bin/bash -cd "$(dirname "$(readlink -f "$0")")" -#change to prompts w/ default values -# set defaults -cpu=1 -memory=1024 -root_disk=10 -variant="archlinux" -path="default" -dhcp_opts="" -ssh_key_dir="../identity_files" -mac=52:54:00:`openssl rand -hex 1`:`openssl rand -hex 1`:`openssl rand -hex 1` -branch="prod" - -usage="$(basename "$0") [options] -n server_name -where: - -n name Name of new vm/server (required, caps allowed, not fqdn) - -c number Numver of CPU cores (default $cpu) - -r number Ram in MB (default $memory) - -d size Root disk size in GB (default 10) - -v variant Linux variant (default archlinux - note should add list of optional variants) - -p image_path Path to image (default /virtual/disks/[name].qcow2) - -m mac_addr Specify mac address to use - if not specified, will use 52:54:00:*:*:* - -s subnet Subnet to place server on (default to 172.16.99.xx) - -i ip_address Specify IP address to assign (default to next available in subnet) - -b branch Specify branch of build_ansible to use (defult prod) - -a attach Attach existing extra disk (use full path to disK, default none)" -# process flags -while getopts "h:n:c:r:d:v:p:m:s:i:b:" flag -do - case "$flag" in - n) name=${OPTARG};; - c) cpu=${OPTARG};; - r) memory=${OPTARG};; - d) root_disk=${OPTARG};; - v) variant=${OPTARG};; - p) path=${OPTARG};; - m) mac=${OPTARG};; - s) subnet=${OPTARG};dhcp_opts+=" -s $subnet";; - i) ip=${OPTARG};dhcp_opts+=" -i $ip";; - b) branch=${OPTARG};; - a) attach=${OPTARG};; - h) ;& - *) echo "$usage";exit;; - esac -done -dhcp_opts+=" -m $mac" - -# get name -shift $(($OPTIND - 1)) - -if [[ -z "$name" ]];then echo "Server name undefined";echo "$usage";exit;fi - -lname=`echo $name | tr '[:upper:]' '[:lower:]'` - -if [[ "$path" -eq "default" ]];then path=/virtual/disks/${name}.qcow2;fi - -img=`./shared_scripts/download_install_image.sh $variant` - -#update DHCP -./shared_scripts/update_dhcp.sh $dhcp_opts $lname - -case "$variant" in - "arch") - # create cloud-init - replace by grabbing these directly from git - mkdir -p /tmp/cloud-init - echo "#cloud-config - users: - - name: root - ssh_authorized_keys: - - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILG56T1k766Cru14kyXI8WXSJwlO2sVhmH6JeRWy+mfe root@host.actcur.com" > /tmp/cloud-init/user-data - - # create new VM - virt-install --name=$name --vcpus=$cpu --memory=$memory --disk path=$path,size=$root_disk,sparse=true,discard=unmap,bus=scsi --os-variant=arch --network bridge=br1,mac=$mac --memballoon model=virtio,autodeflate=on --cdrom=$img --graphics vnc --cloud-init user-data="/tmp/cloud-init/user-data" --autoconsole=none - - #wait until server is available - echo "Waiting for server to be available" - while true - do - sleep 1 - ping -c 1 ${lname}.actcur.com 2>&1 > /dev/null - if [ $? -eq 0 ]; then break;fi - done - echo "server up, waiting for ssh" - - sleep 30 - - sed "s/REPLACE_HOSTNAME/${lname}.actcur.com/g" ../base_files/user_configuration.json > /tmp/user_configuration.json - rs=$((root_disk*1024-206)) - sed -i "s/REPLACE_SIZE/${rs}/g" /tmp/user_configuration.json - - ssh-keygen -R ${lname}.actcur.com - ssh-keyscan ${lname}.actcur.com | grep -v "#" | grep "ecdsa-sha2-nistp256" >> ~/.ssh/known_hosts - - scp -i $ssh_key_dir/id_arch_builder /tmp/user_configuration.json ${lname}.actcur.com:/tmp/user_configuration.json - ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "archinstall --config /tmp/user_configuration.json --silent" - ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "ln -s /usr/lib/systemd/system/sshd.service /mnt/archinstall/etc/systemd/system/multi-user.target.wants/sshd.service" - ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "cp -r /root/.ssh/ /mnt/archinstall/root/;reboot" - ;; - "alma") - # set up kickstart file - sed "s/REPLACE_HOSTNAME/${lname}.actcur.com/g" ../base_files/kickstart.cfg > /tmp/kickstart.cfg - - # create new VM - virt-install --name=$name --vcpus=$cpu --memory=$memory --disk path=$path,size=$root_disk,sparse=true,discard=unmap,bus=scsi --os-variant=almalinux9 --network bridge=br1,mac=$mac --memballoon model=virtio,autodeflate=on --location=$img --graphics vnc --initrd-inject /tmp/kickstart.cfg --extra-args "inst.ks=file:/kickstart.cfg" --autoconsole=none - - echo "server up, waiting for install to complete" - - sleep 60 - - # wait for install to complete and server to shut down/reboot - while true - do - sleep 1 - ping -c 1 ${lname}.actcur.com 2>&1 > /dev/null - if [ $? -ne 0 ]; then break;fi - done - ;; -esac - -sleep 10 -virsh start $name - -#wait until server is available -echo "Waiting for server to be available" -while true -do - sleep 1 - ping -c 1 ${lname}.actcur.com 2>&1 > /dev/null - if [ $? -eq 0 ]; then break;fi -done -echo "server up, waiting for ssh" - -sleep 30 - -ssh-keygen -R ${lname}.actcur.com -ssh-keyscan ${lname}.actcur.com | grep -v "#" | grep "ecdsa-sha2-nistp256" >> ~/.ssh/known_hosts - -exit - -# install some dependencies -case "$variant" in - "arch") - ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "pacman -Sy --noconfirm ansible-core git" - ;; - "alma") - ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "dnf config-manager --set-enabled crb;yum install -y ansible-core git epel-release" - ;; -esac -ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "ansible-galaxy collection install community.general" -ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "ansible-galaxy collection install community.crypto" -ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "ansible-galaxy collection install ansible.posix" - -# run ansible -ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "mkdir /ansible/; curl -k 'https://git.actcur.com/actcur-ansible/playbook-builder/raw/branch/$branch/build_ansible.sh' -H 'accept: application/raw' -o /ansible/build_ansible.sh;echo "$branch" > /ansible/branch;/bin/bash /ansible/build_ansible.sh" - -ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "/bin/bash /ansible/build_ansible.sh" - -ssh -i $ssh_key_dir/id_arch_builder ${lname}.actcur.com "git config --global user.email 'ejparker@actcur.com';git config --global user.name 'Beth Parker'" - -systemctl restart nfs-server -