/
one_vmm_kvm.rb
executable file
·195 lines (158 loc) · 7.74 KB
/
one_vmm_kvm.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2002-2009, Distributed Systems Architecture Group, Universidad #
# Complutense de Madrid (dsa-research.org) #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# Set up the environment for the driver #
# ---------------------------------------------------------------------------- #
ONE_LOCATION = ENV["ONE_LOCATION"]
if !ONE_LOCATION
RUBY_LIB_LOCATION = "/usr/lib/one/ruby"
ETC_LOCATION = "/etc/one/"
else
RUBY_LIB_LOCATION = ONE_LOCATION + "/lib/ruby"
ETC_LOCATION = ONE_LOCATION + "/etc/"
end
if ENV["LIBVIRT_DEFAULT_URI"]
LIBVIRT_URI = ENV["LIBVIRT_DEFAULT_URI"]
else
LIBVIRT_URI = "qemu:///system"
end
$: << RUBY_LIB_LOCATION
require 'pp'
require "VirtualMachineDriver"
# ---------------------------------------------------------------------------- #
# The main class for the LibVirt driver #
# ---------------------------------------------------------------------------- #
class LibVirtDriver < VirtualMachineDriver
# ------------------------------------------------------------------------ #
# Libvirt commands constants #
# ------------------------------------------------------------------------ #
QEMU_PROTOCOL = "qemu+ssh"
LIBVIRT = {
:create => "virsh --connect #{LIBVIRT_URI} create",
:shutdown => "virsh --connect #{LIBVIRT_URI} shutdown",
:cancel => "virsh --connect #{LIBVIRT_URI} destroy",
:save => "virsh --connect #{LIBVIRT_URI} save",
:restore => "virsh --connect #{LIBVIRT_URI} restore",
:migrate => "virsh --connect #{LIBVIRT_URI} migrate --live",
:poll => "virsh --connect #{LIBVIRT_URI} dominfo"
}
# ------------------------------------------------------------------------ #
# LibvirtDriver constructor #
# ------------------------------------------------------------------------ #
def initialize()
super(15,true)
end
# ------------------------------------------------------------------------ #
# DEPLOY action, sends the deployment file to remote host #
# ------------------------------------------------------------------------ #
def deploy(id, host, remote_dfile, not_used)
local_dfile = get_local_deployment_file(remote_dfile)
if !local_dfile || File.zero?(local_dfile)
send_message(ACTION[:deploy],RESULT[:failure],id,
"Can not open deployment file #{local_dfile}")
return
end
tmp = File.new(local_dfile)
domain = tmp.read
tmp.close()
images_path = File.dirname remote_dfile
cmd = "mkdir -p #{images_path} && cat > #{remote_dfile} && " \
"#{LIBVIRT[:create]} #{remote_dfile}"
deploy_exe = SSHCommand.run("'#{cmd}'", host, log_method(id), domain)
if deploy_exe.code != 0
send_message(ACTION[:deploy],RESULT[:failure],id)
elsif deploy_exe.stdout.match(/^Domain (.*) created from .*$/)
send_message(ACTION[:deploy],RESULT[:success],id,$1)
else
send_message(ACTION[:deploy],RESULT[:failure],id,
"Domain id not found in #{LIBVIRT[:create]} output.")
end
end
# ------------------------------------------------------------------------ #
# Basic Domain Management Operations #
# ------------------------------------------------------------------------ #
def shutdown(id, host, deploy_id, not_used)
cmd="#{LIBVIRT[:shutdown]} #{deploy_id} && " \
"while [ $(#{LIBVIRT[:poll]} #{deploy_id} > /dev/null 2>&1; " \
"echo $?) -eq \"0\" ]; do sleep 2; done ; sleep 4"
execution=SSHCommand.run('bash', host, log_method(id), cmd)
if execution.code !=0
send_message(ACTION[:shutdown], RESULT[:failure], id)
else
send_message(ACTION[:shutdown], RESULT[:success], id)
end
end
def cancel(id, host, deploy_id, not_used)
ssh_action("#{LIBVIRT[:cancel]} #{deploy_id}", id, host, :cancel)
end
def save(id, host, deploy_id, file)
ssh_action("'touch #{file};#{LIBVIRT[:save]} #{deploy_id} #{file}'",id,host,:save)
end
def restore(id, host, deploy_id, file)
ssh_action("#{LIBVIRT[:restore]} #{file}", id, host, :restore)
end
def migrate(id, host, deploy_id, dest_host)
cmd = "#{LIBVIRT[:migrate]} #{deploy_id} "\
"#{QEMU_PROTOCOL}://#{dest_host}/system"
ssh_action(cmd, id, host, :migrate)
end
# ------------------------------------------------------------------------ #
# Get info from the Libvirt Domain #
# ------------------------------------------------------------------------ #
def poll(id, host, deploy_id, not_used)
exe = SSHCommand.run("#{LIBVIRT[:poll]} #{deploy_id}", host,
log_method(id))
if exe.code != 0
result = :failure
info = "-"
if exe.stderr.match(/^error: failed to get domain '#{deploy_id}'/)
info = "#{POLL_ATTRIBUTE[:state]}=#{VM_STATE[:deleted]}"
result = :success
end
send_message(ACTION[:poll], RESULT[result], id, info)
return
end
info = ""
exe.stdout.each_line {|line|
columns=line.split(":").collect {|c| c.strip }
case columns[0]
when 'Used memory'
info << " #{POLL_ATTRIBUTE[:usedmemory]}=" \
<< (columns[1].to_i).to_s
when 'State'
case columns[1]
when "running","blocked","shutdown","dying"
state = VM_STATE[:active]
when "paused"
state = VM_STATE[:paused]
when "crashed"
state = VM_STATE[:error]
else
state = VM_STATE[:unknown]
end
info << " #{POLL_ATTRIBUTE[:state]}=#{state}"
end
}
send_message(ACTION[:poll], RESULT[:success], id, info)
end
end
# ---------------------------------------------------------------------------- #
# LibvirtDriver Main program
# ---------------------------------------------------------------------------- #
kvm_driver = LibVirtDriver.new
kvm_driver.start_driver