162 lines
12 KiB
Bash
162 lines
12 KiB
Bash
#!/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
|
|
|