-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathonepush
More file actions
executable file
·89 lines (64 loc) · 2.58 KB
/
Copy pathonepush
File metadata and controls
executable file
·89 lines (64 loc) · 2.58 KB
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
#!/usr/bin/ruby
$LOAD_PATH << '/usr/lib/one/ruby/'
Gem.paths = {
'GEM_PATH' => '/usr/share/one/gems-dist'
}
require 'base64'
require 'json'
require 'nokogiri'
require 'opennebula'
def get_vm_host(vm_id)
c = OpenNebula::Client.new
vm_pool = OpenNebula::VirtualMachinePool.new(c)
rc = vm_pool.info_all!
raise rc.message if OpenNebula.is_error?(rc)
vm_pool.retrieve_xmlelements("VM[ID=#{vm_id}]/HISTORY_RECORDS/HISTORY[last()]/HOSTNAME")[0] \
|| abort("Machine #{vm_id} does not exist or is not running")
vm_pool.retrieve_xmlelements("VM[ID=#{vm_id}]/HISTORY_RECORDS/HISTORY[last()]/HOSTNAME")[0].text
end
def query_vm_agent(vm_id, query, host = 'localhost')
cmd = "virsh -c 'qemu+ssh://#{host}/system' qemu-agent-command one-#{vm_id} '#{query.to_json}'"
out = `#{cmd}`
JSON.parse(out)
end
def guest_change_permissions(vm_id, remote_path, permissions)
host = get_vm_host(vm_id)
exec_chmod = { 'execute' => 'guest-exec',
'arguments' => {
'path' => '/bin/chmod',
'arg' => [permissions, remote_path]
}
}
query_vm_agent(vm_id, exec_chmod, host)
end
def guest_file_write(vm_id, local_path, remote_path)
host = get_vm_host(vm_id)
openfile = { 'execute' => 'guest-file-open',
'arguments' => {
'path' => remote_path,
'mode' => 'w'
}
}
handle = query_vm_agent(vm_id, openfile, host)['return']
abort("Couldn't open file #{remote_path} in the guest") unless handle >= 0
writefile = { 'execute' => 'guest-file-write',
'arguments' => {
'handle' => handle,
'buf-b64' => Base64.encode64(File.open(local_path, 'rb').read)
}
}
wstatus = query_vm_agent(vm_id, writefile, host)['return']
puts("#{wstatus['count']} bytes written from #{local_path} to VM #{vm_id} on #{remote_path}")
closefile = { 'execute' => 'guest-file-close',
'arguments' => {'handle' => handle }
}
status = query_vm_agent(vm_id, closefile, host)
end
ARGV.length != 3 && abort("Usage: #{ARGV[0]} VM_ID SOURCE_FILE DESTINATION_FILE")
VM_ID, SRC_FILE, DST_FILE = ARGV[0], ARGV[1], ARGV[2]
VM_ID =~ %r{[0-9]+} || abort("#{VM_ID} is not a valid ID for a VM")
File.exist?(SRC_FILE) || abort("File #{SRC_FILE} does not exist")
DST_FILE =~ %r{^(\/[A-z0-9_-]+)+} || abort("Destination file #{DST_FILE} does not look like a valid path")
SRC_PERMISSIONS = (format('%04o', File.stat(SRC_FILE).mode))[3..]
guest_file_write(VM_ID, SRC_FILE, DST_FILE)
guest_change_permissions(VM_ID, DST_FILE, SRC_PERMISSIONS)