Removed fuel from Genesis master since fuel has moved to repo fuel.
[genesis.git] / compass / deploy / compass_vm.sh
1 compass_vm_dir=$WORK_DIR/vm/compass
2 rsa_file=$compass_vm_dir/boot.rsa
3 ssh_args="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $rsa_file"
4 function tear_down_compass() {
5     sudo virsh destroy compass > /dev/null 2>&1
6     sudo virsh undefine compass > /dev/null 2>&1
7
8     sudo umount $compass_vm_dir/old > /dev/null 2>&1
9     sudo umount $compass_vm_dir/new > /dev/null 2>&1
10
11     sudo rm -rf $compass_vm_dir
12     
13     log_info "tear_down_compass success!!!"
14 }
15
16 function install_compass_core() {
17     local inventory_file=$compass_vm_dir/inventory.file
18     log_info "install_compass_core enter"
19     sed -i "s/mgmt_next_ip:.*/mgmt_next_ip: ${COMPASS_SERVER}/g" $WORK_DIR/installer/compass-install/install/group_vars/all
20     echo "compass_nodocker ansible_ssh_host=$MGMT_IP ansible_ssh_port=22" > $inventory_file
21     PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook -e pipeline=true --private-key=$rsa_file --user=root --connection=ssh --inventory-file=$inventory_file $WORK_DIR/installer/compass-install/install/compass_nodocker.yml
22     exit_status=$?
23     rm $inventory_file
24     log_info "install_compass_core exit"
25     if [[ $exit_status != 0 ]];then
26         /bin/false
27     fi
28 }
29
30 function wait_ok() {
31     log_info "wait_compass_ok enter"
32     retry=0
33     until timeout 1s ssh $ssh_args root@$MGMT_IP "exit" 2>/dev/null
34     do
35         log_progress "os install time used: $((retry*100/$1))%"
36         sleep 1
37         let retry+=1
38         if [[ $retry -ge $1 ]];then
39             log_error "os install time out"
40             tear_down_compass
41             exit 1
42         fi
43     done
44
45     log_warn "os install time used: 100%"
46     log_info "wait_compass_ok exit"
47 }
48
49 function launch_compass() {
50     local old_mnt=$compass_vm_dir/old
51     local new_mnt=$compass_vm_dir/new
52     local old_iso=$WORK_DIR/iso/centos.iso 
53     local new_iso=$compass_vm_dir/centos.iso 
54
55     log_info "launch_compass enter"
56     tear_down_compass
57     
58     set -e
59     mkdir -p $compass_vm_dir $old_mnt
60     sudo mount -o loop $old_iso $old_mnt
61     cd $old_mnt;find .|cpio -pd $new_mnt;cd -
62
63     sudo umount $old_mnt
64
65     chmod 755 -R $new_mnt
66     sed -i -e "s/REPLACE_MGMT_IP/$MGMT_IP/g" -e "s/REPLACE_MGMT_NETMASK/$MGMT_MASK/g" -e "s/REPLACE_INSTALL_IP/$COMPASS_SERVER/g" -e "s/REPLACE_INSTALL_NETMASK/$INSTALL_MASK/g" -e "s/REPLACE_GW/$MGMT_GW/g" $new_mnt/isolinux/isolinux.cfg
67    
68     sudo ssh-keygen -f $new_mnt/bootstrap/boot.rsa -t rsa -N ''
69     cp $new_mnt/bootstrap/boot.rsa $rsa_file
70
71     rm -rf $new_mnt/.rr_moved $new_mnt/rr_moved
72     sudo mkisofs -quiet -r -J -R -b isolinux/isolinux.bin  -no-emul-boot -boot-load-size 4 -boot-info-table -hide-rr-moved -x "lost+found:" -o $new_iso $new_mnt
73
74     rm -rf $old_mnt $new_mnt
75     
76     qemu-img create -f qcow2 $compass_vm_dir/disk.img 100G
77     
78     # create vm xml
79     sed -e "s/REPLACE_MEM/$COMPASS_VIRT_MEM/g" \
80         -e "s/REPLACE_CPU/$COMPASS_VIRT_CPUS/g" \
81         -e "s#REPLACE_IMAGE#$compass_vm_dir/disk.img#g" \
82         -e "s#REPLACE_ISO#$compass_vm_dir/centos.iso#g" \
83         -e "s/REPLACE_NET_MGMT/mgmt/g" \
84         -e "s/REPLACE_BRIDGE_INSTALL/br_install/g" \
85         $COMPASS_DIR/deploy/template/vm/compass.xml \
86         > $WORK_DIR/vm/compass/libvirt.xml
87     
88     sudo virsh define $compass_vm_dir/libvirt.xml
89     sudo virsh start compass
90     
91     if ! wait_ok 300;then
92         log_error "install os timeout"
93         exit 1
94     fi
95
96     if ! install_compass_core;then
97         log_error "install compass core failed"
98         exit 1
99     fi
100     
101     set +e
102     log_info "launch_compass exit"
103 }