16
logger "VSP_VM_NAME: ${VSP_VM_NAME}"
17
logger "VSP_VM_DIR: ${VSP_VM_DIR}"
18
logger "VSP_VM_XML: ${VSP_VM_XML}"
19
logger "VSP_VM_IMAGE_NAME: ${VSP_VM_IMAGE_NAME}"
20
logger "VSP_VM_ORIG_IMAGE_NAME: ${VSP_VM_ORIG_IMAGE_NAME}"
21
logger "VSP_VM_IMAGE_DIR: ${VSP_VM_IMAGE_DIR}"
14
24
# Returns 0 if VSP is not running
15
25
function is_vsp_vm_running
35
45
function create_vsp_qemu_img
37
juju-log "Check and create the qemu-img"
47
logger "Check and create the qemu-img"
40
50
if [ $? -eq 0 ]; then
42
52
/usr/bin/qemu-img create -f qcow2 -b ${VSP_VM_IMAGE_DIR}/${VSP_VM_ORIG_IMAGE_NAME} ${VSP_VM_IMAGE_DIR}/${VSP_VM_IMAGE_NAME}
43
sudo cp $${VSP_VM_IMAGE_DIR}/${VSP_VM_ORIG_IMAGE_NAME} /var/lib/libvirt/images/${VSP_VM_ORIG_IMAGE_NAME}
53
sudo cp ${VSP_VM_IMAGE_DIR}/${VSP_VM_ORIG_IMAGE_NAME} /var/lib/libvirt/images/${VSP_VM_ORIG_IMAGE_NAME}
44
54
sudo mv ${VSP_VM_IMAGE_DIR}/${VSP_VM_IMAGE_NAME} /var/lib/libvirt/images/${VSP_VM_IMAGE_NAME}
46
juju-log "qemu-img create done"
56
logger "qemu-img create done"
51
61
# Returns 1 if it was not able to create it
52
62
function create_vsp_vm
54
juju-log "Check if ${VSP_VM_NAME} vm already exists"
64
logger "Check if ${VSP_VM_NAME} vm already exists"
56
66
if [ `/usr/bin/virsh list --all | grep -c ${VSP_VM_NAME}` -eq 1 ]; then
57
juju-log "vsp vm is already created"
67
logger "vsp vm is already created"
61
71
create_vsp_qemu_img
63
juju-log "Define vsp vm"
73
logger "Define vsp vm"
64
74
if [ ! -f ${VSP_VM_DIR}/${VSP_VM_XML} ]; then
65
juju-log "Error: ${VSP_VM_NAME} template does not exist"
75
logger "Error: ${VSP_VM_NAME} template does not exist"
68
78
/usr/bin/virsh define ${VSP_VM_DIR}/${VSP_VM_XML}
69
79
if [ "`sudo virsh list --all | awk '/shut off/{print $2}'`" != "${VSP_VM_NAME}" ]; then
70
juju-log "start: unable to define vsp vm"
80
logger "start: unable to define vsp vm"
75
85
function start_vsp_vm
77
juju-log "Get vsp vm state"
87
logger "Get vsp vm state"
78
88
state="`sudo virsh dominfo ${VSP_VM_NAME} | awk '/State:/' | cut -d: -f 2 | tr -d ' '`"
80
running) juju-log "vsp ${VSP_VM_NAME} is already runnning"
90
running) logger "vsp ${VSP_VM_NAME} is already runnning"
82
shut*) juju-log "need to start vsp ${VSP_VM_NAME}"
92
shut*) logger "need to start vsp ${VSP_VM_NAME}"
83
93
/usr/bin/virsh start ${VSP_VM_NAME}
84
94
if [ $? -eq 1 ]; then
85
juju-log "Error: vsp vm is shutdown but couldn't restart it"
95
logger "Error: vsp vm is shutdown but couldn't restart it"
89
*) juju-log "Unknown state($state) of vsp ${VSP_VM_NAME}"
99
*) logger "Unknown state($state) of vsp ${VSP_VM_NAME}"
95
104
function stop_vsp_vm
97
juju-log "stop: handling hook"
100
107
if [ $? -eq 1 ]; then
101
juju-log "Shutting down vsp vm"
108
logger "Shutting down vsp vm"
102
109
virsh shutdown ${VSP_VM_NAME}
107
113
is_vsp_vm_running
108
114
if [ $? -eq 1 ]; then
109
juju-log "Destroying vsp vm"
115
logger "Destroying vsp vm"
110
116
/usr/bin/virsh destroy ${VSP_VM_NAME}
111
117
/usr/bin/virsh undefine ${VSP_VM_NAME}