forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
miq_provision_virt_workflow.rb
1150 lines (937 loc) · 41.2 KB
/
miq_provision_virt_workflow.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
class MiqProvisionVirtWorkflow < MiqProvisionWorkflow
include DialogFieldValidation
def auto_placement_enabled?
get_value(@values[:placement_auto])
end
def initialize(values, requester, options = {})
initial_pass = values.blank?
initial_pass = true if options[:initial_pass] == true
instance_var_init(values, requester, options)
# Check if the caller passed the source VM as part of the initial call
if initial_pass == true
src_vm_id = get_value(@values[:src_vm_id])
if src_vm_id.present?
vm = VmOrTemplate.find_by(:id => src_vm_id)
@values[:src_vm_id] = [vm.id, vm.name] if vm.present?
end
end
unless options[:skip_dialog_load] == true
# If this is the first time we are called the values hash will be empty
# Also skip if we are being called from a web-service
@dialogs = get_pre_dialogs if initial_pass && options[:use_pre_dialog] != false
if @dialogs.nil?
@dialogs = get_dialogs
elsif options[:use_pre_dialog] != false
@running_pre_dialog = true
end
normalize_numeric_fields unless @dialogs.nil?
end
password_helper(@values, false) # Decrypt passwords in the hash for the UI
@last_vm_id = get_value(@values[:src_vm_id]) unless initial_pass == true
return if options[:skip_dialog_load] == true
set_default_values
update_field_visibility
if get_value(values[:service_template_request])
show_dialog(:requester, :hide, "disabled")
show_dialog(:purpose, :hide, "disabled")
end
end
def dialog_name_from_automate(message = 'get_dialog_name', extra_attrs = {})
super(message, [:request_type, :source_type, :target_type], extra_attrs)
end
def make_request(request, values, requester = nil, auto_approve = false)
if @running_pre_dialog == true
continue_request(values)
password_helper(values, true)
return nil
end
if request
request = MiqRequest.find(request) unless request.kind_of?(MiqRequest)
request.src_vm_id = get_value(values[:src_vm_id])
end
super
end
def refresh_field_values(values)
st = Time.now
new_src = get_value(values[:src_vm_id])
vm_changed = @last_vm_id != new_src
# NOTE: This makes a copy of the values hash so we have a copy of the object to modify
@values = values
get_source_and_targets(true)
# Update fields that should be updated when the Source VM changes
if vm_changed
set_on_vm_id_changed
get_source_and_targets(true)
end
# @values gets modified during this call
get_all_dialogs
update_custom_spec
values.merge!(@values)
# Update the display flag for fields based on current settings
update_field_visibility
@last_vm_id = get_value(@values[:src_vm_id])
_log.info("provision refresh completed in [#{Time.now - st}] seconds")
rescue => err
$log.log_backtrace(err)
raise
ensure
@allowed_vlan_cache = nil
end
def custom_sysprep_timezone(field, data_value)
set_value_from_list(:sysprep_timezone, field, "%03d" % data_value, @timezones)
end
def custom_sysprep_domain_name(field, data_value)
set_value_from_list(:sysprep_domain_name, field, data_value, nil, true)
end
def fields_to_clear
[:placement_host_name,
:placement_ds_name,
:placement_folder_name,
:placement_cluster_name,
:placement_rp_name,
:linked_clone,
:snapshot,
:placement_dc_name]
end
def set_on_vm_id_changed
src = get_source_and_targets
vm, ems = load_ar_obj(src[:vm]), src[:ems]
clear_field_values(fields_to_clear)
if vm.nil?
clear_field_values([:number_of_cpus, :number_of_sockets, :cores_per_socket, :vm_memory, :cpu_limit, :memory_limit, :cpu_reserve, :memory_reserve])
vm_description = nil
vlan = nil
show_dialog(:customize, :show, "disabled")
else
if vm.ext_management_system.nil?
raise _("Source VM [%{name}] does not belong to a Provider") % {:name => vm.name}
end
set_or_default_hardware_field_values(vm)
# Record the nic/lan setting on the template for validation checks at provision time.
@values[:src_vm_nics] = vm.hardware && vm.hardware.nics.collect(&:device_name).compact
@values[:src_vm_lans] = vm.lans.collect(&:name).compact
vlan = @values[:src_vm_lans].first
vm_description = vm.description
case vm.platform
when 'linux', 'windows' then show_dialog(:customize, :show, "enabled")
else show_dialog(:customize, :hide, "disabled")
end
# If the selected template switches EMS, update the value and invalidate the @ems_metadata_tree handle.
if get_value(@values[:src_ems_id]) != ems.id
@values[:src_ems_id] = [ems.id, ems.name]
@ems_metadata_tree = nil
end
end
# Update VM description
fields do |fn, f, _, _|
case fn
when :src_vm_id
f[:notes] = vm_description
when :vlan
get_field(:vlan)
vlan ||= Array(@values[fn]).first
set_value_from_list(fn, f, vlan, allowed_vlans)
end
end
end
def vm_name_preview(_options = {})
end
def update_field_visibility(options = {})
if request_type == :clone_to_template
show_dialog(:customize, :hide, "disabled")
end
update_field_display_values(options)
update_field_display_notes_values
update_field_read_only(options)
end
def update_field_read_only(options = {})
read_only = get_value(@values[:sysprep_custom_spec]).blank? ? false : !(get_value(@values[:sysprep_spec_override]) == true)
exclude_list = [:sysprep_spec_override, :sysprep_custom_spec, :sysprep_enabled, :sysprep_upload_file, :sysprep_upload_text]
fields(:customize) { |fn, f, _dn, _d| f[:read_only] = read_only unless exclude_list.include?(fn) }
return unless options[:read_only_fields]
fields(:hardware) { |fn, f, _dn, _d| f[:read_only] = true if options[:read_only_fields].include?(fn) }
end
def allowed_hosts_obj(options = {})
all_hosts = super
filter_allowed_hosts(all_hosts)
end
def filter_allowed_hosts(all_hosts)
filter_hosts_by_vlan_name(all_hosts)
end
def filter_hosts_by_vlan_name(all_hosts)
vlan_name = get_value(@values[:vlan])
return all_hosts unless vlan_name
_log.info("Filtering hosts with the following network: <#{vlan_name}>")
all_hosts.reject { |h| !h.lans.pluck(:name).include?(vlan_name) }
end
#
# Methods for populating lists of allowed values for a field
# => Input - A hash containing options specific to the called method
# => Output - A hash with the format: <value> => <value display name>
# => New methods can be added as as needed
#
def allowed_cat_entries(options)
rails_logger('allowed_cat_entries', 0)
@values["#{options[:prov_field_name]}_category".to_sym] = options[:category]
cat = Classification.lookup_by_name(options[:category].to_s)
result = cat ? cat.entries.each_with_object({}) { |e, h| h[e.name] = e.description } : {}
rails_logger('allowed_cat_entries', 1)
result
end
def allowed_vlans(options = {})
@allowed_vlan_cache ||= available_vlans_and_hosts(options)[0]
filter_by_tags(@allowed_vlan_cache, options)
end
def available_vlans_and_hosts(options = {})
@vlan_options ||= options
vlans = {}
src = get_source_and_targets
return vlans if src.blank?
hosts = get_selected_hosts(src)
unless @vlan_options[:vlans] == false
rails_logger('allowed_vlans', 0)
load_allowed_vlans(hosts, vlans)
rails_logger('allowed_vlans', 1)
end
return vlans, hosts
end
def load_allowed_vlans(hosts, vlans)
load_hosts_vlans(hosts, vlans)
end
def load_hosts_vlans(hosts, vlans)
Lan.joins(:switch => :hosts)
.where(:hosts => {:id => hosts.map(&:id)})
.where(:switches => {:shared => [nil, false]})
.distinct.pluck(:name)
.each_with_object(vlans) { |v, h| h[v] = v }
end
def filter_by_tags(target, options)
opt_filters = options[:tag_filters]
return target if opt_filters.blank?
filters = []
selected_cats = selected_tags_by_cat_and_name
if opt_filters.kind_of?(Hash)
opt_filters.each do |cat, f|
selected_tag = selected_cats[cat.to_s]
if selected_tag.nil?
# If no tags are selected check for a filter with a tag of nil to process
f.each { |fd| filters << fd if fd[:tag].nil? }
else
f.each do |fd|
selected_tag.each do |st|
filters << fd if fd[:tag]&.match?(st)
end
end
end
end
end
result = target.dup
filters.each do |f|
result.delete_if do |key, name|
test_str = f[:key] == :key ? key : name
f[:modifier] == "!" ? test_str =~ f[:filter] : test_str !~ f[:filter]
end
end
result
end
def selected_tags_by_cat_and_name
tag_ids = (Array.wrap(@values[:vm_tags]) + Array.wrap(@values[:pre_dialog_vm_tags])).uniq
return {} if tag_ids.blank?
# Collect the filter tags by category
allowed_tags_and_pre_tags.each_with_object({}) do |cat, hsh|
children = cat[:children].each_with_object({}) { |value, result| result[value.first] = value.last }
selected_ids = (children.keys & tag_ids)
hsh[cat[:name]] = selected_ids.collect { |t_id| children[t_id][:name] } if selected_ids.present?
end
end
def tag_symbol
:vm_tags
end
VM_OR_TEMPLATE_EXTRA_COLS = %i[mem_cpu cpu_total_cores v_total_snapshots allocated_disk_storage].freeze
def allowed_templates(options = {})
# Return pre-selected VM if we are called for cloning
if [:clone_to_vm, :clone_to_template].include?(request_type)
vm_or_template = VmOrTemplate.find_by(:id => get_value(@values[:src_vm_id]))
return [create_hash_struct_from_vm_or_template(vm_or_template, options)].compact
end
filter_id = get_value(@values[:vm_filter]).to_i
if filter_id == @allowed_templates_filter && (options[:tag_filters].blank? || (@values[:vm_tags] == @allowed_templates_tag_filters))
return @allowed_templates_cache
end
rails_logger('allowed_templates', 0)
# Select only the non-deprecated templates as some providers retain templates even if not currently in use
templates = MiqTemplate.non_deprecated.in_my_region
condition = allowed_template_condition
if options[:tag_filters].present?
tag_filters = options[:tag_filters].collect(&:to_s)
selected_tags = (Array.wrap(@values[:vm_tags].presence) + Array.wrap(@values[:pre_dialog_vm_tags].presence)).uniq
tag_conditions = []
# Collect the filter tags by category
if selected_tags.present?
allowed_tags_and_pre_tags.each do |cat|
if tag_filters.include?(cat[:name])
children_keys = cat[:children].each_with_object({}) { |t, h| h[t.first] = t.last }
conditions = (children_keys.keys & selected_tags).collect { |t_id| "#{cat[:name]}/#{children_keys[t_id][:name]}" }
end
tag_conditions << conditions if conditions.present?
end
end
if tag_conditions.present?
_log.info("Filtering VM templates with the following tag_filters: <#{tag_conditions.inspect}>")
templates = templates.where(condition).find_tags_by_grouping(tag_conditions, :ns => "/managed")
end
end
# Only select the columns we need
templates = templates.select(:id, :type, :name, :guid, :uid_ems, :ems_id, :cloud_tenant_id)
allowed_templates_list = source_vm_rbac_filter(templates, condition, VM_OR_TEMPLATE_EXTRA_COLS).to_a
@allowed_templates_filter = filter_id
@allowed_templates_tag_filters = @values[:vm_tags]
rails_logger('allowed_templates', 1)
log_allowed_template_list(allowed_templates_list)
MiqPreloader.preload(allowed_templates_list, [:hardware, :operating_system])
@_ems_allowed_templates_cache = {}
@allowed_templates_cache = allowed_templates_list.collect do |template|
create_hash_struct_from_vm_or_template(template, options)
end
@_ems_allowed_templates_cache = nil
@allowed_templates_cache
end
def allowed_template_condition
return ["vms.ems_id IS NOT NULL"] unless self.class.respond_to?(:provider_model)
{"vms.ems_id" => self.class.provider_model.pluck(:id)}
end
def source_vm_rbac_filter(vms, condition = nil, *extra_cols)
opts = {:user => @requester, :conditions => condition}
opts[:extra_cols] = extra_cols unless extra_cols.empty?
MiqSearch.filtered(get_value(@values[:vm_filter]).to_i, VmOrTemplate, vms, opts)
end
def allowed_provision_types(_options = {})
{}
end
def allowed_snapshots(_options = {})
result = {}
return result if (vm = get_source_vm).blank?
vm.snapshots.each { |ss| result[ss.id.to_s] = ss.current? ? "#{ss.name} (Active)" : ss.name }
result["__CURRENT__"] = _(" Use the snapshot that is active at time of provisioning") if result.present?
result
end
def allowed_tags(options = {})
return {} if (source = load_ar_obj(get_source_vm)).blank?
super(options.merge(:region_number => source.region_number))
end
def allowed_pxe_servers(_options = {})
return {} if (source = load_ar_obj(get_source_vm)).blank?
PxeServer.in_region(source.region_number).each_with_object({}) { |p, h| h[p.id] = p.name }
end
def get_source_vm
get_source_and_targets[:vm]
end
def get_source_and_targets(refresh = false)
return @target_resource if @target_resource && refresh == false
vm_id = get_value(@values[:src_vm_id])
rails_logger('get_source_and_targets', 0)
svm = VmOrTemplate.find_by(:id => vm_id)
if svm.nil?
@vm_snapshot_count = 0
return @target_resource = {}
end
@vm_snapshot_count = svm.v_total_snapshots
result = {}
result[:vm] = ci_to_hash_struct(svm)
result[:ems] = ci_to_hash_struct(svm.ext_management_system)
result
end
def resources_for_ui
auto_placement_enabled? ? {} : super
end
def allowed_customization_specs(_options = {})
src = get_source_and_targets
return [] if src.blank? || src[:ems].nil?
customization_type = get_value(@values[:sysprep_enabled])
return [] if customization_type.blank? || customization_type == 'disabled'
@customization_specs ||= {}
ems_id = src[:ems].id
unless @customization_specs.key?(ems_id)
rails_logger('allowed_customization_specs', 0)
@customization_specs[ems_id] = ci_to_hash_struct(load_ar_obj(src[:ems]).customization_specs)
rails_logger('allowed_customization_specs', 1)
end
result = @customization_specs[ems_id].dup
source_platform = src[:vm].platform.capitalize
result.delete_if { |cs| source_platform != cs.typ }
result.delete_if(&:is_sysprep_spec?) if customization_type == 'file'
result.delete_if { |cs| !cs.is_sysprep_spec? } if customization_type == 'fields'
result
end
def allowed_customization(_options = {})
src = get_source_and_targets
return {} if src.blank?
return {"fields" => "Specification"} if @values[:forced_sysprep_enabled] == 'fields'
result = {"disabled" => "<None>"}
case src[:vm].platform
when 'windows' then result.merge!("fields" => "Specification", "file" => "Sysprep Answer File")
when 'linux' then result["fields"] = "Specification"
end
result
end
def allowed_number_of_vms(options = {})
options = {:min => 1, :max => 50}.merge(options)
min, max = options[:min].to_i, options[:max].to_i
min = 1 if min < 1
max = min if max < 1
(min..max).index_with { |i| i.to_s }
end
def load_test_ous_data
return @ldap_ous unless @ldap_ous.nil?
ous = YAML.load_file("ous.yaml")
@ldap_ous = {}
ous.each { |ou| @ldap_ous[ou[0].dup] = ou[1].dup }
@ldap_ous
end
def allowed_organizational_units(_options = {})
{}
end
def allowed_ous_tree(_options = {})
hous = {}
ous = allowed_organizational_units
return ous if ous.blank?
dc_path = ous.keys.first.split(',').collect { |i| i.split("DC=")[1] }.compact.join(".")
ous.each { |ou| create_ou_tree(ou, hous[dc_path] ||= {}, ou[0].split(',')) }
# Re-adjust path for remove levels without OUs.
root, path = find_first_ou(hous[dc_path])
unless path.nil?
root_name = hous.keys[0]
new_name = "#{root_name} (#{path})"
hous[new_name] = root
hous.delete(root_name)
end
hous
end
def find_first_ou(hous, path = nil)
if hous.key?(:ou)
find_first_ou(hous[key], path)
else
key = hous.keys.first
if hous[key].key?(:ou)
return hous, path
else
path = path.nil? ? key : "#{path} / #{key}"
find_first_ou(hous[key], path)
end
end
end
def build_ou_path_name(ou)
path_name = ''
paths = ou[0].split(',').reverse
paths.each do |path|
parts = path.split('=')
next if parts.first == 'DC'
path_name = path_name.blank? ? parts.last : File.join(path_name, parts.last)
ou[1].replace(path_name)
end
end
def create_ou_tree(ou, h, path)
idx = path.pop
type, pathname = idx.split('=')
if type == "DC"
create_ou_tree(ou, h, path)
elsif path.blank?
entry = (h[pathname] ||= {})
entry[:path] = ou[0]
entry[:ou] = ou
else
create_ou_tree(ou, h[pathname] ||= {}, path)
end
end
def allowed_domains(options = {})
@domains ||= begin
domains = {}
if @values[:forced_sysprep_domain_name].blank?
Host.all.each do |host|
domain = host.domain.to_s.downcase
next if domain.blank? || domains.key?(domain)
# Filter by host platform or is proxy is active
next unless options[:platform].nil? || options[:platform].include?(host.platform)
next unless options[:active_proxy].nil? || host.is_proxy_active? == options[:active_proxy]
domains[domain] = domain
end
else
Array.wrap(@values[:forced_sysprep_domain_name].split('\n')).each { |d| domains[d] = d }
end
domains
end
end
def update_custom_spec
vm = get_source_vm
return if vm.nil?
if @customize_option.nil?
@current_spec = get_value(@values[:sysprep_custom_spec])
@customize_option = get_value(@values[:sysprep_enabled])
@custom_spec_override = get_value(@values[:sysprep_spec_override])
end
if @customization_specs.nil?
@customize_option = get_value(@values[:sysprep_enabled])
return
end
# Force selected customization spec to <None> if the Customization option changes
selected_spec = get_value(@values[:sysprep_custom_spec])
current_customize_option = get_value(@values[:sysprep_enabled])
current_spec_override = get_value(@values[:sysprep_spec_override])
if current_customize_option != @customize_option
@customize_option = current_customize_option
selected_spec = nil
@values[:sysprep_custom_spec] = [nil, nil]
@values[:sysprep_spec_override] = [false, 0]
end
return if @current_spec == selected_spec && @custom_spec_override == current_spec_override
_log.info("Custom spec changed from [#{@current_spec}] to [#{selected_spec}]. Customize option:[#{@customize_option}]")
if selected_spec
src = get_source_and_targets
ems_id = src[:ems].id
cs_data = @customization_specs[ems_id].detect { |s| s.name == selected_spec }
if cs_data.nil?
selected_spec_int = selected_spec.to_i
cs_data = @customization_specs[ems_id].detect { |s| s.id == selected_spec_int }
end
if cs_data
cs_data = load_ar_obj(cs_data)
if @customize_option == 'file'
@values[:sysprep_upload_text] = cs_data[:spec].fetch_path('identity', 'value')
end
# Call platform specific method
send(:"update_fields_from_spec_#{cs_data[:typ].downcase}", cs_data)
# Call generic networking method
update_fields_from_spec_networking(cs_data)
end
elsif @customize_option == 'file'
@values[:sysprep_upload_text] = nil
end
@current_spec = selected_spec
@custom_spec_override = current_spec_override
end
def update_fields_from_spec_windows(cs_data)
spec_hash = {}
spec = cs_data[:spec]
dialog = @dialogs.fetch_path(:dialogs, :customize)
collect_customization_spec_settings(spec, spec_hash, %w[identity guiUnattended],
[:sysprep_timezone, 'timeZone', :sysprep_auto_logon, 'autoLogon', :sysprep_auto_logon_count, 'autoLogonCount'])
collect_customization_spec_settings(spec, spec_hash, %w[identity identification],
[:sysprep_domain_name, 'joinDomain', :sysprep_domain_admin, 'domainAdmin', :sysprep_workgroup_name, 'joinWorkgroup'])
# PATH:[identity][userData][computerName][name] (VimString) = "VI25Test"
collect_customization_spec_settings(spec, spec_hash, %w[identity userData],
[:sysprep_organization, 'orgName', :sysprep_full_name, 'fullName', :sysprep_product_id, 'productId'])
collect_customization_spec_settings(spec, spec_hash, %w[identity licenseFilePrintData],
[:sysprep_server_license_mode, 'autoMode', :sysprep_per_server_max_connections, 'autoUsers'])
collect_customization_spec_settings(spec, spec_hash, ['options'],
[:sysprep_change_sid, 'changeSID', :sysprep_delete_accounts, 'deleteAccounts'])
spec_hash[:sysprep_identification] = spec_hash[:sysprep_domain_name].blank? ? 'workgroup' : 'domain'
spec_hash.each { |k, v| set_customization_field_from_spec(v, k, dialog) }
end
def update_fields_from_spec_linux(cs_data)
spec_hash = {}
spec = cs_data[:spec]
dialog = @dialogs.fetch_path(:dialogs, :customize)
collect_customization_spec_settings(spec, spec_hash, ['identity'],
[:linux_domain_name, 'domain', :linux_host_name, 'hostName'])
spec_hash.each { |k, v| set_customization_field_from_spec(v, k, dialog) }
end
def update_fields_from_spec_networking(cs_data)
spec_hash = {}
spec = cs_data[:spec]
dialog = @dialogs.fetch_path(:dialogs, :customize)
first_adapter = Array.wrap(spec['nicSettingMap']).first
if first_adapter.kind_of?(Hash)
adapter = first_adapter['adapter']
spec_hash[:dns_servers] = Array.wrap(adapter['dnsServerList']).join(', ')
spec_hash[:gateway] = Array.wrap(adapter['gateway']).join(', ')
spec_hash[:subnet_mask] = adapter['subnetMask'].to_s
spec_hash[:ip_addr] = adapter.fetch_path('ip', 'ipAddress').to_s
# Combine the WINS server fields into 1 comma separated field list
spec_hash[:wins_servers] = [adapter['primaryWINS'], adapter['secondaryWINS']].collect { |s| s.presence }.compact.join(', ')
end
# In Linux, DNS server settings are global, not per adapter
spec_hash[:dns_servers] = Array.wrap(spec.fetch_path('globalIPSettings', 'dnsServerList')).join(', ') if spec_hash[:dns_servers].blank?
spec_hash[:dns_suffixes] = Array.wrap(spec.fetch_path('globalIPSettings', 'dnsSuffixList')).join(', ')
spec_hash[:addr_mode] = spec_hash[:ip_addr].blank? ? 'dhcp' : 'static'
spec_hash.each { |k, v| set_customization_field_from_spec(v, k, dialog) }
end
def collect_customization_spec_settings(spec, spec_hash, spec_path, fields)
return unless (section = spec.fetch_path(spec_path))
fields.each_slice(2) { |dlg_field, prop| spec_hash[dlg_field] = section[prop] }
end
def set_customization_field_from_spec(data_value, dlg_field, dialog)
field_hash = dialog[:fields][dlg_field]
data_type = field_hash[:data_type]
cust_method = "custom_#{dlg_field}"
if respond_to?(cust_method)
send(cust_method, field_hash, data_value)
else
value = case data_type
when :boolean then data_value == "true"
when :integer then data_value.to_i_with_method
when :string then data_value.to_s
else data_value
end
if field_hash.key?(:values)
set_value_from_list(dlg_field, field_hash, value)
else
@values[dlg_field] = value
end
end
end
def target_type
request_type == :clone_to_template ? 'template' : 'vm'
end
def source_type
svm = get_source_vm
if svm.nil?
request_type == :template ? 'template' : 'unknown'
else
svm.template? ? 'template' : 'vm'
end
end
def self.from_ws(*args)
version = args.first.to_f
return from_ws_ver_1_0(*args) if version == 1.0
# Move optional arguments into the OpenStruct object
prov_options = OpenStruct.new(
:values => args[6],
:ems_custom_attributes => args[7],
:miq_custom_attributes => args[8]
)
prov_args = args[0, 6]
prov_args << prov_options
from_ws_ver_1_x(*prov_args)
end
def self.from_ws_ver_1_0(version, user, src_name, target_name, auto_approve, tags, additional_values)
_log.info("Web-service provisioning starting with interface version <#{version}> for user <#{user.userid}>")
values = {}
p = new(values, user, :use_pre_dialog => false)
src_name_down = src_name.downcase
src = p.allowed_templates.detect { |v| v.name.downcase == src_name_down }
raise _("Source template [%{name}] was not found") % {:name => src_name} if src.nil?
p = class_for_source(src.id).new(values, user, :use_pre_dialog => false)
# Populate required fields
p.init_from_dialog(values)
values[:src_vm_id] = [src.id, src.name]
p.refresh_field_values(values)
values[:vm_name] = target_name
values[:placement_auto] = [true, 1]
values[:owner_first_name] = user.userid
values[:owner_email] = user.userid
values[:owner_last_name] = user.userid
# Tags are passed as category|value|cat2|... Example: cc|001|environment|test
values[:vm_tags] = p.ws_tags(tags, :parse_ws_string_v1)
values[:ws_values] = p.ws_values(additional_values, :parse_ws_string_v1)
if p.validate(values) == false
errors = []
p.fields { |_fn, f, _dn, _d| errors << f[:error] unless f[:error].nil? }
raise _("Provision failed for the following reasons:\n%{errors}") % {:errors => errors.join("\n")}
end
p.make_request(nil, values, nil, auto_approve)
end
def ws_template_fields(values, fields, ws_values)
data = parse_ws_string(fields)
ws_values = parse_ws_string(ws_values)
placement_cluster_name = ws_values[:cluster]
if placement_cluster_name.present?
data[:placement_cluster_name] = placement_cluster_name.to_s.downcase
_log.info("placement_cluster_name:<#{data[:placement_cluster_name].inspect}>")
data[:data_centers] = EmsCluster.where("lower(name) = ?", data[:placement_cluster_name]).collect(&:v_parent_datacenter)
end
_log.info("data:<#{data.inspect}>")
src_name = data[:name].blank? ? nil : data[:name].downcase
src_guid = data[:guid].blank? ? nil : data[:guid].downcase
ems_guid = data[:ems_guid].blank? ? nil : data[:ems_guid].downcase
data_centers = data[:data_centers]
_log.info("VM Passed: <#{src_name}> <#{src_guid}> <#{ems_guid}> Datacenters:<#{data_centers.inspect}>")
if [:clone_to_vm, :clone_to_template].include?(request_type)
src = ws_find_template_or_vm(values, src_name, src_guid, ems_guid)
else
srcs = allowed_templates(:include_datacenter => true).find_all do |v|
_log.info("VM Detected: <#{v.name.downcase}> <#{v.guid}> <#{v.uid_ems}> Datacenter:<#{v.datacenter_name}>")
(src_name.nil? || src_name == v.name.downcase) && (src_guid.nil? || src_guid == v.guid) && (ems_guid.nil? || ems_guid == v.uid_ems) && (data_centers.nil? || data_centers.include?(v.datacenter_name))
end
if srcs.length > 1
raise _("Multiple source template were found from input data:<%{data}>") % {:data => data.inspect}
end
src = srcs.first
end
if src.nil?
raise _("No source template was found from input data:<%{data}>") % {:data => data.inspect}
end
_log.info("VM Found: <#{src.name}> <#{src.guid}> <#{src.uid_ems}> Datacenter:<#{src.datacenter_name}>")
src
end
def ws_find_template_or_vm(_values, src_name, src_guid, ems_guid)
scope = VmOrTemplate
scope = scope.where(:guid => src_guid) if src_guid.present?
scope = scope.where(:uid_ems => ems_guid) if ems_guid.present?
scope = scope.where(VmOrTemplate.arel_table[:name].lower.eq(src_name)) if src_name.present?
rbac_object = source_vm_rbac_filter(scope).first
create_hash_struct_from_vm_or_template(rbac_object, :include_datacenter => true) if rbac_object
end
def ws_vm_fields(values, fields)
data = parse_ws_string(fields)
_log.info("data:<#{data.inspect}>")
ws_service_fields(values, fields, data)
ws_hardware_fields(values, fields, data)
ws_network_fields(values, fields, data)
ws_customize_fields(values, fields, data)
ws_schedule_fields(values, fields, data)
ws_environment_fields(values, data)
data.each { |k, v| _log.warn("Unprocessed key <#{k}> with value <#{v.inspect}>") }
end
def ws_environment_fields(values, data)
# do not parse environment data unless :placement_auto is false
return unless data[:placement_auto].to_s == "false"
values[:placement_auto] = [false, 0]
return if (dlg_fields = get_ws_dialog_fields(dialog_name = :environment)).nil?
data.keys.each { |key| set_ws_field_value(values, key, data, dialog_name, dlg_fields) if dlg_fields.key?(key) }
end
def ws_service_fields(values, _fields, data)
return if (dlg_fields = get_ws_dialog_fields(dialog_name = :service)).nil?
# Process PXE settings by setting the server first then image, windows image and custom template
dlg_field = :pxe_server_id
if dlg_fields.key?(dlg_field) && (data.key?(dlg_field) || data.key?(:pxe_server))
set_ws_field_value_by_id_or_name(values, dlg_field, data, dialog_name, dlg_fields)
dlg_field = :pxe_image_id
get_field(dlg_field, dialog_name)
set_ws_field_value_by_id_or_name(values, dlg_field, data, dialog_name, dlg_fields, nil, "PxeImage")
# Windows images are also stored with the pxe_image values
set_ws_field_value_by_id_or_name(values, dlg_field, data, dialog_name, dlg_fields, :windows_image_id, "WindowsImage")
end
data.keys.each { |key| set_ws_field_value(values, key, data, dialog_name, dlg_fields) if dlg_fields.key?(key) }
end
def ws_hardware_fields(values, _fields, data)
ws_hardware_scsi_controller_fields(values, data)
ws_hardware_disk_fields(values, data)
ws_hardware_network_fields(values, data)
return if (dlg_fields = get_ws_dialog_fields(dialog_name = :hardware)).nil?
data.keys.each { |key| set_ws_field_value(values, key, data, dialog_name, dlg_fields) if dlg_fields.key?(key) }
end
def ws_hardware_network_fields(values, data)
parse_ws_hardware_fields(:networks, /^network(\d{1,2})$/, values, data) { |n, v, _i| n[:network] = v }
# Check and remove invalid networks specifications
if values[:networks].present?
values[:networks].delete_if do |d|
result = d[:network].blank?
_log.warn("Skipping network due to blank name: <#{d.inspect}>") if result == true
result
end
end
end
def ws_hardware_scsi_controller_fields(values, data)
parse_ws_hardware_fields(:ctrl_scsi, /^ctrlscsi(\d{1,2})$/, values, data) do |ctrl, value, idx|
ctrl.merge!(:busnumber => idx, :devicetype => value)
end
end
def ws_hardware_disk_fields(values, data)
parse_ws_hardware_fields(:disk_scsi, /^diskscsi(\d{1,2})$/, values, data) do |disk, value, _idx|
d_parms = value.split(':')
disk[:bus] = d_parms[0] || '*'
disk[:pos] = d_parms[1] || '*'
disk[:sizeInMB] = d_parms[2]
end
# Check and remove invalid disk specifications
if values[:disk_scsi].present?
values[:disk_scsi].delete_if do |d|
result = d[:sizeInMB].to_i == 0
_log.warn("Skipping disk due to invalid size: <#{d.inspect}>") if result == true
result
end
end
end
def parse_ws_hardware_fields(hw_key, regex_filter, values, data)
data.keys.each do |k|
key_name = k.to_s.split('.').first
next unless key_name =~ regex_filter
item_id = Regexp.last_match(1).to_i
v = data.delete(k)
_log.info("processing key <hardware:#{k}(#{v.class})> with value <#{v.inspect}>")
values[hw_key] ||= []
item = values[hw_key][item_id] ||= {}
key_names = k.to_s.split('.')[1..-1]
if key_names.length == 0
# Caller needs to parse the default value
yield(item, v, item_id)
elsif key_names.length == 1
item[key_names[0].to_sym] = v
elsif key_names.length > 1
item.store_path(*(key_names.collect(&:to_sym) << v))
end
end
values[hw_key].compact! unless values[hw_key].nil?
end
def ws_network_fields(values, _fields, data)
return if (dlg_fields = get_ws_dialog_fields(dialog_name = :network)).nil?
data.keys.each { |key| set_ws_field_value(values, key, data, dialog_name, dlg_fields) if dlg_fields.key?(key) }
end
def ws_customize_fields(values, _fields, data)
return if (dlg_fields = get_ws_dialog_fields(dialog_name = :customize)).nil?
key = :customization_template_id
if dlg_fields.key?(key) && (data.key?(key) || data.key?(:customization_template))
get_field(key, dialog_name)
set_ws_field_value_by_id_or_name(values, key, data, dialog_name, dlg_fields)
end
data.keys.each { |k| set_ws_field_value(values, k, data, dialog_name, dlg_fields) if dlg_fields.key?(k) }
end
def self.from_ws_ver_1_x(version, user, template_fields, vm_fields, requester, tags, options)
options = OpenStruct.new if options.nil?
_log.warn("Web-service provisioning starting with interface version <#{version}> by requester <#{user.userid}>")
init_options = {:use_pre_dialog => false, :request_type => request_type(parse_ws_string(template_fields)[:request_type]), :initial_pass => true}
data = parse_ws_string(requester)
user = update_requester_from_parameters(data, user)
p = new(values = {}, user, init_options)
src = p.ws_template_fields(values, template_fields, options.values)
raise _("Source template [%{name}] was not found") % {:name => src_name} if src.nil?
# Allow new workflow class to determine dialog name instead of using the stored value from the first call.
values.delete(:miq_request_dialog_name)
values[:placement_auto] = [true, 1]
values[:src_vm_id] = [src.id, src.name]
p = class_for_source(src.id).new(values, user, init_options)
# Populate required fields
p.init_from_dialog(values)
p.refresh_field_values(values)
p.ws_vm_fields(values, vm_fields)
p.ws_requester_fields(values, requester)
values[:vm_tags] = p.ws_tags(tags) # Tags are passed as category=value|cat2=value2... Example: cc=001|environment=test
values[:ws_values] = p.ws_values(options.values)
values[:ws_ems_custom_attributes] = p.ws_values(options.ems_custom_attributes, :parse_ws_string, :modify_key_name => false)
values[:ws_miq_custom_attributes] = p.ws_values(options.miq_custom_attributes, :parse_ws_string, :modify_key_name => false)
p.make_request(nil, values, nil, values[:auto_approve]).tap do |request|
p.raise_validate_errors if request == false
end
rescue => err
_log.error("<#{err}>")
raise err
end
private