leon / Compute.sh
Created at Wed Jun 17 10:29:07 WIB 2020
Create Compute
Compute.sh
Raw
#Function deploy

create_vm() {
        virt-clone -o template-centos7.7 -n $VM_NAME -f /data/vms/$VM_NAME.qcow2
        virsh start $VM_NAME

        sleep 60

        ssh [email protected] /bin/bash << EOF
        
        #For eth0
        sed -i "s/^IPADDR.*/IPADDR=$ADDRESS1/g" /etc/sysconfig/network-scripts/ifcfg-eth0
        sed -i "s/^GATEWAY.*/GATEWAY=$GATEWAY1/g" /etc/sysconfig/network-scripts/ifcfg-eth0
       
        #for eth1
        cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth1
        sed -i "s/^IPADDR.*/IPADDR=$ADDRESS2/g" /etc/sysconfig/network-scripts/ifcfg-eth1
        sed -i "s/^GATEWAY.*/GATEWAY=$GATEWAY2/g" /etc/sysconfig/network-scripts/ifcfg-eth1
        echo "$VM_NAME" > /etc/hostname
        echo "$ADDRESS1 $VM_NAME" >> /etc/hosts
        echo "$ADDRESS2 $VM_NAME" >> /etc/hosts
EOF

        virsh shutdown $VM_NAME
        sleep 30

        virsh detach-interface $VM_NAME --type network --config
        virsh attach-interface --domain $VM_NAME --type network --source $NETWORK_NAME1 --model virtio --config
        virsh attach-interface --domain $VM_NAME --type network --source $NETWORK_NAME2 --model virtio --config

        virsh start $VM_NAME
}

#Looping
for i in {1..2}
do
        #eth0
        VM_NAME=alora-compute$i
        ADDRESS1=10.30.30.6$i
        GATEWAY1=10.30.30.1
        NETWORK_NAME1=net-10.30.30
        
        #eth1
        ADDRESS2=10.40.40.6$i
        GATEWA2=10.40.40.1
        NETWORK_NAME2=net-10.40.40
create_vm
done