forked from virtuozzo/backup-plugin-veeam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedule_create.rb
140 lines (112 loc) · 3.99 KB
/
schedule_create.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
Backups::Plugin.hook helpers: %i[client_helper task_helper query_helper uid_helper schedule_params_helper] do
def call(schedule, virtual_server)
new_name = "#{schedule.period.capitalize} #{schedule.id}@#{virtual_server.label}"
# Find template job
template_job_id = get_job_id(
backup_resource.advanced_options[:vsphere_template_job_name]
)
unless template_job_id
return error('Unable to find template job. Please check the correctness of Backup Resource options')
end
unless backup_repository_uid
return error('Unable to find backup repository. Please check the correctness of Backup Resource options')
end
# Get target VM references
vm_to_add_ref = vm_ref(virtual_server)
return error('Unable to find the VM host in the Veeam infrastructure') unless vm_to_add_ref
virtual_server.metadata[:veeam_uid] = vm_to_add_ref[:obj_ref]
# Clone template job with a new name
task_path =
task_path(api_post("jobs/#{template_job_id}?action=clone", clone_params(new_name).to_xml))
task_poller(task_path).run
# Get cloned job ID
job_id = get_job_id(new_name)
return error('Clone job task has been failed') unless job_id
virtual_server.metadata[:veeam_related_job_ids] ||= []
virtual_server.metadata[:veeam_related_job_ids] << job_id
# Get ID of VM which we have to delete
vm_to_delete =
api_get("jobs/#{job_id}/includes").dig(:ObjectsInJob, :ObjectInJob, :ObjectInJobId)
# Add a target VM to the job
task_path =
task_path(
api_post("jobs/#{job_id}/includes", add_vm_params(vm_to_add_ref).to_xml)
)
task_poller(task_path).run
# Remove redundant VM
task_path =
task_path(api_delete("jobs/#{job_id}/includes/#{vm_to_delete}"))
task_poller(task_path).run
# Edit & enable schedule
task_path =
task_path(api_put("jobs/#{job_id}?action=edit", schedule_params(schedule).to_xml))
task_poller(task_path).run
end
private
def get_job_id(job_name)
uid_to_identifier(
:Job,
api_get(build_query(:job, { name: "\"#{job_name}\"" }, entities: false))
.dig(:QueryResult, :Refs, :Ref, :UID)
)
end
def backup_repository_uid
@backup_repository_uid ||=
api_get(
build_query(
:repository,
{ name: "\"#{backup_resource.advanced_options[:backup_repository_name]}\"" },
entities: false
)
)
.dig(:QueryResult, :Refs, :Ref, :UID)
end
def vcenter_instance_uuid(virtual_server)
if virtual_server.vcloud?
virtual_server.vcenter
else
virtual_server.compute
end.instance_uuid
end
def clone_params(name)
Nokogiri::XML::Builder.new(encoding: 'utf-8') do |xml|
xml.JobCloneSpec(
'xmlns' => 'http://www.veeam.com/ent/v1.0',
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
) do
xml.BackupJobCloneInfo do
xml.JobName name
xml.FolderName name
xml.RepositoryUid backup_repository_uid
end
end
end
end
def add_vm_params(vm_to_add_ref)
Nokogiri::XML::Builder.new(encoding: 'utf-8') do |xml|
xml.CreateObjectInJobSpec(
'xmlns' => 'http://www.veeam.com/ent/v1.0',
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
) do
xml.HierarchyObjRef vm_to_add_ref[:obj_ref]
xml.HierarchyObjName vm_to_add_ref[:obj_name]
end
end
end
def vm_ref(virtual_server)
hierarchy_root_id =
uid_to_identifier(
:HierarchyRoot,
api_get(
build_query(:hierarchyroot, { uniqueid: "\"#{vcenter_instance_uuid(virtual_server)}\"" }, entities: false)
).dig(:QueryResult, :Refs, :Ref, :UID)
)
return unless hierarchy_root_id
{
obj_ref: "urn:VMware:Vm:#{hierarchy_root_id}.#{virtual_server.vcenter_moref}",
obj_name: virtual_server.label
}
end
end