-
Notifications
You must be signed in to change notification settings - Fork 900
/
Copy pathvm.rb
159 lines (135 loc) · 4.22 KB
/
vm.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
class Vm < VmOrTemplate
default_scope { where(:template => false) }
virtual_has_one :supported_consoles, :class_name => "Hash"
extend InterRegionApiMethodRelay
include CustomActionsMixin
include CiFeatureMixin
include ExternalUrlMixin
include AuthenticationMixin
include Operations
def self.base_model
Vm
end
def self.corresponding_model
if self == Vm
MiqTemplate
else
module_parent::Template
end
end
class << self; alias_method :corresponding_template_model, :corresponding_model; end
delegate :corresponding_model, :to => :class
alias_method :corresponding_template_model, :corresponding_model
def validate_remote_console_vmrc_support
raise(MiqException::RemoteConsoleNotSupportedError,
_("VMRC remote console is not supported on %{vendor}.") % {:vendor => vendor})
end
def validate_native_console_support
raise(MiqException::RemoteConsoleNotSupportedError,
_("NATIVE remote console is not supported on %{vendor}.") % {:vendor => vendor})
end
def add_to_service(service)
service.add_resource!(self)
end
def enforce_single_service_parent?
true
end
def self.find_all_by_mac_address_and_hostname_and_ipaddress(mac_address, hostname, ipaddress)
return [] if mac_address.blank? && hostname.blank? && ipaddress.blank?
include = [:vm_or_template]
references = []
conds = [["hardwares.vm_or_template_id IS NOT NULL"]]
if mac_address
conds[0] << "guest_devices.address = ?"
conds << mac_address
include << :nics
references << :guest_devices
end
if hostname
conds[0] << "networks.hostname = ?"
conds << hostname
include << :networks
references << :networks
end
if ipaddress
conds[0] << "networks.ipaddress = ?"
conds << ipaddress
include << :networks
references << :networks
end
conds[0] = "(#{conds[0].join(" AND ")})"
Hardware.includes(include.uniq)
.references(references.uniq)
.where(conds)
.map(&:vm_or_template).select { |vm| vm.kind_of?(Vm) }
end
def running_processes
pl = {}
if (reason = unsupported_reason(:collect_running_processes))
_log.warn(reason)
raise reason
end
begin
require 'win32/miq-wmi'
cred = my_zone_obj.auth_user_pwd(:windows_domain)
ipaddresses.each do |ipaddr|
break if pl.present?
_log.info("Running processes for VM:[#{id}:#{name}] IP:[#{ipaddr}] Logon:[#{cred[0]}]")
begin
wmi = WMIHelper.connectServer(ipaddr, *cred)
pl = MiqProcess.process_list_all(wmi) unless wmi.nil?
rescue => wmi_err
_log.warn(wmi_err.to_s)
end
_log.info("Running processes for VM:[#{id}:#{name}] Count:[#{pl.length}]")
end
rescue => err
_log.log_backtrace(err)
end
pl
end
def remote_console_url=(url, user_id)
SystemConsole.where(:vm_id => id).each(&:destroy)
console = SystemConsole.create!(
:vm_id => id,
:user => User.find_by(:userid => user_id),
:protocol => 'url',
:url => url,
:url_secret => SecureRandom.hex
)
console.id
end
def supported_consoles
{
:html5 => support_hash(:html5_console, :launch_html5_console),
:vmrc => support_hash(:vmrc_console, :launch_vmrc_console),
:native => support_hash(:native_console, :launch_native_console)
}
end
def allow_retire_request_creation?
MiqRequest.where(:source_type => "Vm", :source_id => id).with_type("VmRetireRequest").find_each do |r|
next if r.request_state == "finished" || r.status == "Error"
warn_message = "MiqRequest with id:#{r.id} to retire Vm name:'#{name}' id:#{id} already created"
if r.request_pending_approval?
_log.warn("#{warn_message} but not approved yet")
return false
end
end
true
end
def self.display_name(number = 1)
n_('VM and Instance', 'VMs and Instances', number)
end
private
def support_hash(visible, launch)
reason = unsupported_reason(launch)
{
:visible => supports?(visible),
:enabled => !reason,
:message => reason
}
end
private_class_method def self.refresh_association
:vms
end
end