Created
September 6, 2017 01:48
-
-
Save lukassup/1c412baa274c8d62e64cc381b80085ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # vim: set noet sw=4 sts=4 ts=4: | |
| RELEASE='Fedora-Atomic-26-20170821.0' | |
| CHKSUM='Fedora-CloudImages-26-20170821.0-x86_64-CHECKSUM' | |
| IMAGE="$RELEASE.x86_64.qcow2" | |
| SUBNET="88" | |
| printf -v MAC_PREFIX '52:54:00:%02x:00' "$SUBNET" | |
| DOMAIN="cluster" | |
| HOST_PASS='atomic' | |
| HOST_VOL=10G | |
| HOST_MEM=1024 | |
| HOST_CPU=1 | |
| log_info() { | |
| if [[ $TERM =~ color ]]; then | |
| >&2 printf '\e[1;32m==>\e[0m \e[1m%s\e[0m\n' "$*" | |
| else | |
| >&2 printf '==> %s\n' "$*" | |
| fi | |
| } | |
| __setup_download() { | |
| log_info 'Importing Fedora GPG keyring' | |
| curl -L 'https://getfedora.org/static/fedora.gpg' | gpg --import | |
| log_info 'Downloading GPG signed image checksums' | |
| curl -LO "https://download.fedoraproject.org/pub/alt/atomic/stable/$RELEASE/CloudImages/x86_64/images/$CHKSUM" | |
| log_info 'Verifying checksum signature' | |
| gpg --verify-files "$CHKSUM" | |
| log_info 'Verifying image checksums (checking for previous download)' | |
| sha256sum -c --ignore-missing "$CHKSUM" && { | |
| log_info 'Image already downloaded' | |
| return | |
| } | |
| log_info 'Downloading Fedora Atomic host cloud image' | |
| curl -LO "https://download.fedoraproject.org/pub/alt/atomic/stable/$RELEASE/CloudImages/x86_64/images/$IMAGE" | |
| log_info 'Verifying image checksum' | |
| sha256sum -c --ignore-missing "$CHKSUM" | |
| } | |
| __setup_sshkeys() { | |
| log_info 'Setting up SSH keypair...' | |
| ssh-keygen -t rsa -b 4096 -C 'cluster-key' -N '' -q -f id_rsa | |
| log_info 'SSH keypair created. Public key:' | |
| pubkey="$(<id_rsa.pub)" | |
| echo "$pubkey" | |
| } | |
| __setup_network() { | |
| log_info "Setting up cluster network (192.168.$SUBNET.0/24)" | |
| sudo virsh net-uuid cluster-network &>/dev/null && { | |
| log_info 'Network already created. Skipping...' | |
| return | |
| } | |
| cat > cluster-network.xml <<-EOF | |
| <network ipv6="yes"> | |
| <name>cluster-network</name> | |
| <domain name="$DOMAIN" localOnly="no" /> | |
| <mac address="$MAC_PREFIX:fe" /> | |
| <bridge name="cluster0" stp="on" delay="0" /> | |
| <forward mode="nat" /> | |
| <ip address="192.168.$SUBNET.254" netmask="255.255.255.0" localPtr="yes"> | |
| <dhcp> | |
| <range start="192.168.$SUBNET.11" end="192.168.$SUBNET.254" /> | |
| <host mac="$MAC_PREFIX:01" name="node01.$DOMAIN" ip="192.168.$SUBNET.1" /> | |
| <host mac="$MAC_PREFIX:02" name="node02.$DOMAIN" ip="192.168.$SUBNET.2" /> | |
| <host mac="$MAC_PREFIX:03" name="node03.$DOMAIN" ip="192.168.$SUBNET.3" /> | |
| <host mac="$MAC_PREFIX:04" name="node04.$DOMAIN" ip="192.168.$SUBNET.4" /> | |
| <host mac="$MAC_PREFIX:05" name="node05.$DOMAIN" ip="192.168.$SUBNET.5" /> | |
| <host mac="$MAC_PREFIX:06" name="node06.$DOMAIN" ip="192.168.$SUBNET.6" /> | |
| <host mac="$MAC_PREFIX:07" name="node07.$DOMAIN" ip="192.168.$SUBNET.7" /> | |
| <host mac="$MAC_PREFIX:08" name="node08.$DOMAIN" ip="192.168.$SUBNET.8" /> | |
| <host mac="$MAC_PREFIX:09" name="node09.$DOMAIN" ip="192.168.$SUBNET.9" /> | |
| <host mac="$MAC_PREFIX:10" name="node10.$DOMAIN" ip="192.168.$SUBNET.10" /> | |
| </dhcp> | |
| </ip> | |
| </network> | |
| EOF | |
| sudo virsh net-create --file cluster-network.xml | |
| } | |
| __setup_vms() { | |
| log_info 'Setting up virtual machines' | |
| read -e -p 'Enter the number of Atomic hosts: ' -i 1 num_hosts | |
| local host_ips=() | |
| for (( host=1; host<=num_hosts; host++ )); do | |
| # zero-pad the host number up to 2 digits (e.g. 01...99) | |
| local _host | |
| printf -v _host '%02d' "$host" | |
| mkdir -pv "$_host" | |
| pushd "$_host" | |
| # Step 1: create the cloud-init meta-data file | |
| cat > meta-data <<-EOF | |
| instance-id: atomic-host$_host | |
| local-hostname: node$_host.$DOMAIN | |
| EOF | |
| # Step 2: create the cloud-init user-data file | |
| cat > user-data <<-EOF | |
| #cloud-config | |
| package_upgrade: true | |
| password: $HOST_PASS | |
| ssh_pwauth: true | |
| chpasswd: { expire: false } | |
| ssh_authorized_keys: | |
| - $pubkey | |
| EOF | |
| # Step 3: create the cloud-init ISO image for each machine | |
| genisoimage -output init.iso -volid cidata -joliet -rock user-data meta-data | |
| # Step 4: create a thinly provisioned snapshot for each machine | |
| qemu-img create -b "../$IMAGE" -f qcow2 "$_host.qcow2" | |
| qemu-img resize "$_host.qcow2" "$HOST_VOL" | |
| # Step 5: install each virtual machine | |
| local _name | |
| printf -v _name 'f26-atomic%02d' "$host" | |
| local _mac | |
| # Convert host number to hex MAC address | |
| printf -v _mac "$MAC_PREFIX:%02x" "$host" | |
| sudo virt-install \ | |
| -n "$_name" \ | |
| --os-type=Linux \ | |
| --os-variant=fedora25 \ | |
| --ram="$HOST_MEM" \ | |
| --vcpus="$HOST_CPU" \ | |
| --disk path="$_host.qcow2",bus=virtio \ | |
| --cdrom "$(pwd)"/init.iso \ | |
| --network network=cluster-network,model=virtio,mac="$_mac" \ | |
| --graphics none \ | |
| --noautoconsole | |
| host_ips+=("192.168.$SUBNET.$host") | |
| popd | |
| done | |
| log_info "Done setting up $num_hosts virtual machine(s). Ready to login:" | |
| for host_ip in ${host_ips[@]}; do | |
| echo -e "\tssh -i id_rsa fedora@$host_ip" | |
| done | |
| } | |
| __setup_download | |
| __setup_sshkeys | |
| __setup_network | |
| __setup_vms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment