forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBackupManagerImpl.java
More file actions
1587 lines (1405 loc) · 79.4 KB
/
BackupManagerImpl.java
File metadata and controls
1587 lines (1405 loc) · 79.4 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
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
package org.apache.cloudstack.backup;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import org.apache.cloudstack.api.ApiCommandResourceType;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.InternalIdentity;
import org.apache.cloudstack.api.command.admin.backup.DeleteBackupOfferingCmd;
import org.apache.cloudstack.api.command.admin.backup.ImportBackupOfferingCmd;
import org.apache.cloudstack.api.command.admin.backup.ListBackupProviderOfferingsCmd;
import org.apache.cloudstack.api.command.admin.backup.ListBackupProvidersCmd;
import org.apache.cloudstack.api.command.admin.backup.UpdateBackupOfferingCmd;
import org.apache.cloudstack.api.command.user.backup.AssignVirtualMachineToBackupOfferingCmd;
import org.apache.cloudstack.api.command.user.backup.CreateBackupCmd;
import org.apache.cloudstack.api.command.user.backup.CreateBackupScheduleCmd;
import org.apache.cloudstack.api.command.user.backup.DeleteBackupCmd;
import org.apache.cloudstack.api.command.user.backup.DeleteBackupScheduleCmd;
import org.apache.cloudstack.api.command.user.backup.ListBackupOfferingsCmd;
import org.apache.cloudstack.api.command.user.backup.ListBackupScheduleCmd;
import org.apache.cloudstack.api.command.user.backup.ListBackupsCmd;
import org.apache.cloudstack.api.command.user.backup.RemoveVirtualMachineFromBackupOfferingCmd;
import org.apache.cloudstack.api.command.user.backup.RestoreBackupCmd;
import org.apache.cloudstack.api.command.user.backup.RestoreVolumeFromBackupAndAttachToVMCmd;
import org.apache.cloudstack.api.command.user.backup.UpdateBackupScheduleCmd;
import org.apache.cloudstack.api.command.user.backup.repository.AddBackupRepositoryCmd;
import org.apache.cloudstack.api.command.user.backup.repository.DeleteBackupRepositoryCmd;
import org.apache.cloudstack.api.command.user.backup.repository.ListBackupRepositoriesCmd;
import org.apache.cloudstack.backup.dao.BackupDao;
import org.apache.cloudstack.backup.dao.BackupOfferingDao;
import org.apache.cloudstack.backup.dao.BackupScheduleDao;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.framework.config.ConfigKey;
import org.apache.cloudstack.framework.jobs.AsyncJobDispatcher;
import org.apache.cloudstack.framework.jobs.AsyncJobManager;
import org.apache.cloudstack.framework.jobs.impl.AsyncJobVO;
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
import org.apache.cloudstack.managed.context.ManagedContextTimerTask;
import org.apache.cloudstack.poll.BackgroundPollManager;
import org.apache.cloudstack.poll.BackgroundPollTask;
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import com.amazonaws.util.CollectionUtils;
import com.cloud.alert.AlertManager;
import com.cloud.api.ApiDispatcher;
import com.cloud.api.ApiGsonHelper;
import com.cloud.configuration.Resource;
import com.cloud.dc.DataCenter;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.event.ActionEvent;
import com.cloud.event.ActionEventUtils;
import com.cloud.event.EventTypes;
import com.cloud.event.EventVO;
import com.cloud.event.UsageEventUtils;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.PermissionDeniedException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.hypervisor.Hypervisor;
import com.cloud.hypervisor.HypervisorGuru;
import com.cloud.hypervisor.HypervisorGuruManager;
import com.cloud.projects.Project;
import com.cloud.storage.ScopeType;
import com.cloud.storage.Snapshot;
import com.cloud.storage.Volume;
import com.cloud.storage.VolumeApiService;
import com.cloud.storage.VolumeVO;
import com.cloud.storage.dao.DiskOfferingDao;
import com.cloud.storage.dao.VolumeDao;
import com.cloud.user.Account;
import com.cloud.user.AccountManager;
import com.cloud.user.AccountService;
import com.cloud.user.DomainManager;
import com.cloud.user.ResourceLimitService;
import com.cloud.user.User;
import com.cloud.utils.DateUtil;
import com.cloud.utils.Pair;
import com.cloud.utils.Ternary;
import com.cloud.utils.component.ComponentContext;
import com.cloud.utils.component.ManagerBase;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GlobalLock;
import com.cloud.utils.db.JoinBuilder;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.db.TransactionCallback;
import com.cloud.utils.db.TransactionCallbackNoReturn;
import com.cloud.utils.db.TransactionLegacy;
import com.cloud.utils.db.TransactionStatus;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.fsm.NoTransitionException;
import com.cloud.vm.VMInstanceVO;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachineManager;
import com.cloud.vm.dao.UserVmDao;
import com.cloud.vm.dao.VMInstanceDao;
import com.google.gson.Gson;
public class BackupManagerImpl extends ManagerBase implements BackupManager {
@Inject
private BackupDao backupDao;
@Inject
private BackupScheduleDao backupScheduleDao;
@Inject
private BackupOfferingDao backupOfferingDao;
@Inject
private VMInstanceDao vmInstanceDao;
@Inject
private AccountService accountService;
@Inject
private AccountManager accountManager;
@Inject
private DomainManager domainManager;
@Inject
private VolumeDao volumeDao;
@Inject
private DataCenterDao dataCenterDao;
@Inject
private BackgroundPollManager backgroundPollManager;
@Inject
private HostDao hostDao;
@Inject
private HypervisorGuruManager hypervisorGuruManager;
@Inject
private PrimaryDataStoreDao primaryDataStoreDao;
@Inject
private DiskOfferingDao diskOfferingDao;
@Inject
private UserVmDao userVmDao;
@Inject
private ApiDispatcher apiDispatcher;
@Inject
private AsyncJobManager asyncJobManager;
@Inject
private VirtualMachineManager virtualMachineManager;
@Inject
private VolumeApiService volumeApiService;
@Inject
private ResourceLimitService resourceLimitMgr;
@Inject
private AlertManager alertManager;
private AsyncJobDispatcher asyncJobDispatcher;
private Timer backupTimer;
private Date currentTimestamp;
private static Map<String, BackupProvider> backupProvidersMap = new HashMap<>();
private List<BackupProvider> backupProviders;
public AsyncJobDispatcher getAsyncJobDispatcher() {
return asyncJobDispatcher;
}
public void setAsyncJobDispatcher(final AsyncJobDispatcher dispatcher) {
asyncJobDispatcher = dispatcher;
}
@Override
public List<BackupOffering> listBackupProviderOfferings(final Long zoneId) {
if (zoneId == null || zoneId < 1) {
throw new CloudRuntimeException("Invalid zone ID passed");
}
validateForZone(zoneId);
final Account account = CallContext.current().getCallingAccount();
if (!accountService.isRootAdmin(account.getId())) {
throw new PermissionDeniedException("Parameter external can only be specified by a Root Admin, permission denied");
}
final BackupProvider backupProvider = getBackupProvider(zoneId);
logger.debug("Listing external backup offerings for the backup provider configured for zone {}", dataCenterDao.findById(zoneId));
return backupProvider.listBackupOfferings(zoneId);
}
@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_BACKUP_IMPORT_OFFERING, eventDescription = "importing backup offering", async = true)
public BackupOffering importBackupOffering(final ImportBackupOfferingCmd cmd) {
validateForZone(cmd.getZoneId());
final BackupOffering existingOffering = backupOfferingDao.findByExternalId(cmd.getExternalId(), cmd.getZoneId());
if (existingOffering != null) {
throw new CloudRuntimeException("A backup offering with external ID " + cmd.getExternalId() + " already exists");
}
if (backupOfferingDao.findByName(cmd.getName(), cmd.getZoneId()) != null) {
throw new CloudRuntimeException("A backup offering with the same name already exists in this zone");
}
final BackupProvider provider = getBackupProvider(cmd.getZoneId());
if (!provider.isValidProviderOffering(cmd.getZoneId(), cmd.getExternalId())) {
throw new CloudRuntimeException("Backup offering '" + cmd.getExternalId() + "' does not exist on provider " + provider.getName() + " on zone " + cmd.getZoneId());
}
final BackupOfferingVO offering = new BackupOfferingVO(cmd.getZoneId(), cmd.getExternalId(), provider.getName(),
cmd.getName(), cmd.getDescription(), cmd.getUserDrivenBackups());
final BackupOfferingVO savedOffering = backupOfferingDao.persist(offering);
if (savedOffering == null) {
throw new CloudRuntimeException("Unable to create backup offering: " + cmd.getExternalId() + ", name: " + cmd.getName());
}
logger.debug("Successfully created backup offering " + cmd.getName() + " mapped to backup provider offering " + cmd.getExternalId());
return savedOffering;
}
@Override
public Pair<List<BackupOffering>, Integer> listBackupOfferings(final ListBackupOfferingsCmd cmd) {
final Long offeringId = cmd.getOfferingId();
final Long zoneId = cmd.getZoneId();
final String keyword = cmd.getKeyword();
if (offeringId != null) {
BackupOfferingVO offering = backupOfferingDao.findById(offeringId);
if (offering == null) {
throw new CloudRuntimeException("Offering ID " + offeringId + " does not exist");
}
return new Pair<>(Collections.singletonList(offering), 1);
}
final Filter searchFilter = new Filter(BackupOfferingVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
SearchBuilder<BackupOfferingVO> sb = backupOfferingDao.createSearchBuilder();
sb.and("zone_id", sb.entity().getZoneId(), SearchCriteria.Op.EQ);
sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
CallContext ctx = CallContext.current();
final Account caller = ctx.getCallingAccount();
if (Account.Type.NORMAL == caller.getType()) {
sb.and("user_backups_allowed", sb.entity().isUserDrivenBackupAllowed(), SearchCriteria.Op.EQ);
}
final SearchCriteria<BackupOfferingVO> sc = sb.create();
if (zoneId != null) {
sc.setParameters("zone_id", zoneId);
}
if (keyword != null) {
sc.setParameters("name", "%" + keyword + "%");
}
if (Account.Type.NORMAL == caller.getType()) {
sc.setParameters("user_backups_allowed", true);
}
Pair<List<BackupOfferingVO>, Integer> result = backupOfferingDao.searchAndCount(sc, searchFilter);
return new Pair<>(new ArrayList<>(result.first()), result.second());
}
@Override
public boolean deleteBackupOffering(final Long offeringId) {
final BackupOfferingVO offering = backupOfferingDao.findById(offeringId);
if (offering == null) {
throw new CloudRuntimeException("Could not find a backup offering with id: " + offeringId);
}
if (vmInstanceDao.listByZoneWithBackups(offering.getZoneId(), offering.getId()).size() > 0) {
throw new CloudRuntimeException("Backup offering is assigned to VMs, remove the assignment(s) in order to remove the offering.");
}
validateForZone(offering.getZoneId());
return backupOfferingDao.remove(offering.getId());
}
public static String createVolumeInfoFromVolumes(List<VolumeVO> vmVolumes) {
List<Backup.VolumeInfo> list = new ArrayList<>();
for (VolumeVO vol : vmVolumes) {
list.add(new Backup.VolumeInfo(vol.getUuid(), vol.getPath(), vol.getVolumeType(), vol.getSize()));
}
return new Gson().toJson(list.toArray(), Backup.VolumeInfo[].class);
}
@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_BACKUP_OFFERING_ASSIGN, eventDescription = "assign VM to backup offering", async = true)
public boolean assignVMToBackupOffering(Long vmId, Long offeringId) {
final VMInstanceVO vm = findVmById(vmId);
if (!Arrays.asList(VirtualMachine.State.Running, VirtualMachine.State.Stopped, VirtualMachine.State.Shutdown).contains(vm.getState())) {
throw new CloudRuntimeException("VM is not in running or stopped state");
}
validateForZone(vm.getDataCenterId());
accountManager.checkAccess(CallContext.current().getCallingAccount(), null, true, vm);
if (vm.getBackupOfferingId() != null) {
throw new CloudRuntimeException("VM already is assigned to a backup offering, please remove the VM from its previous offering");
}
final BackupOfferingVO offering = backupOfferingDao.findById(offeringId);
if (offering == null) {
throw new CloudRuntimeException("Provided backup offering does not exist");
}
final BackupProvider backupProvider = getBackupProvider(offering.getProvider());
if (backupProvider == null) {
throw new CloudRuntimeException("Failed to get the backup provider for the zone, please contact the administrator");
}
return transactionAssignVMToBackupOffering(vm, offering, backupProvider) != null;
}
private VMInstanceVO transactionAssignVMToBackupOffering(VMInstanceVO vm, BackupOfferingVO offering, BackupProvider backupProvider) {
return Transaction.execute(TransactionLegacy.CLOUD_DB, new TransactionCallback<VMInstanceVO>() {
@Override
public VMInstanceVO doInTransaction(final TransactionStatus status) {
try {
long vmId = vm.getId();
vm.setBackupOfferingId(offering.getId());
vm.setBackupVolumes(createVolumeInfoFromVolumes(volumeDao.findByInstance(vmId)));
if (!backupProvider.assignVMToBackupOffering(vm, offering)) {
throw new CloudRuntimeException("Failed to assign the VM to the backup offering, please try removing the assignment and try again.");
}
if (!vmInstanceDao.update(vmId, vm)) {
backupProvider.removeVMFromBackupOffering(vm);
throw new CloudRuntimeException("Failed to update VM assignment to the backup offering in the DB, please try again.");
}
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_BACKUP_OFFERING_ASSIGN, vm.getAccountId(), vm.getDataCenterId(), vmId,
"Backup-" + vm.getHostName() + "-" + vm.getUuid(), vm.getBackupOfferingId(), null, null, Backup.class.getSimpleName(), vm.getUuid());
logger.debug(String.format("VM [%s] successfully added to Backup Offering [%s].", ReflectionToStringBuilderUtils.reflectOnlySelectedFields(vm,
"uuid", "instanceName", "backupOfferingId", "backupVolumes"), ReflectionToStringBuilderUtils.reflectOnlySelectedFields(offering,
"uuid", "name", "externalId", "provider")));
} catch (Exception e) {
String msg = String.format("Failed to assign VM [%s] to the Backup Offering [%s], using provider [name: %s, class: %s], due to: [%s].",
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(vm, "uuid", "instanceName", "backupOfferingId", "backupVolumes"),
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(offering, "uuid", "name", "externalId", "provider"),
backupProvider.getName(), backupProvider.getClass().getSimpleName(), e.getMessage());
logger.error(msg);
logger.debug(msg, e);
return null;
}
return vm;
}
});
}
@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_BACKUP_OFFERING_REMOVE, eventDescription = "remove VM from backup offering", async = true)
public boolean removeVMFromBackupOffering(final Long vmId, final boolean forced) {
final VMInstanceVO vm = vmInstanceDao.findByIdIncludingRemoved(vmId);
if (vm == null) {
throw new CloudRuntimeException(String.format("Can't find any VM with ID: [%s].", vmId));
}
validateForZone(vm.getDataCenterId());
accountManager.checkAccess(CallContext.current().getCallingAccount(), null, true, vm);
final BackupOfferingVO offering = backupOfferingDao.findById(vm.getBackupOfferingId());
if (offering == null) {
throw new CloudRuntimeException("No previously configured backup offering found for the VM");
}
final BackupProvider backupProvider = getBackupProvider(offering.getProvider());
if (backupProvider == null) {
throw new CloudRuntimeException("Failed to get the backup provider for the zone, please contact the administrator");
}
if (!forced && backupProvider.willDeleteBackupsOnOfferingRemoval()) {
String message = String.format("To remove VM [id: %s, name: %s] from Backup Offering [id: %s, name: %s] using the provider [%s], please specify the "
+ "forced:true option to allow the deletion of all jobs and backups for this VM or remove the backups that this VM has with the backup "
+ "offering.", vm.getUuid(), vm.getInstanceName(), offering.getUuid(), offering.getName(), backupProvider.getClass().getSimpleName());
throw new CloudRuntimeException(message);
}
boolean result = false;
try {
result = backupProvider.removeVMFromBackupOffering(vm);
vm.setBackupOfferingId(null);
vm.setBackupVolumes(null);
vm.setBackupExternalId(null);
if (result && backupProvider.willDeleteBackupsOnOfferingRemoval()) {
final List<Backup> backups = backupDao.listByVmId(null, vm.getId());
for (final Backup backup : backups) {
backupDao.remove(backup.getId());
}
}
if ((result || forced) && vmInstanceDao.update(vm.getId(), vm)) {
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_BACKUP_OFFERING_REMOVE, vm.getAccountId(), vm.getDataCenterId(), vm.getId(),
"Backup-" + vm.getHostName() + "-" + vm.getUuid(), vm.getBackupOfferingId(), null, null,
Backup.class.getSimpleName(), vm.getUuid());
final List<BackupScheduleVO> backupSchedules = backupScheduleDao.listByVM(vm.getId());
for(BackupSchedule backupSchedule: backupSchedules) {
backupScheduleDao.remove(backupSchedule.getId());
}
result = true;
}
} catch (Exception e) {
logger.error("Exception caught when trying to remove VM [{}] from the backup offering [{}] due to: [{}].", vm, offering, e.getMessage(), e);
}
return result;
}
@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_BACKUP_SCHEDULE_CONFIGURE, eventDescription = "configuring VM backup schedule")
public BackupSchedule configureBackupSchedule(CreateBackupScheduleCmd cmd) {
final Long vmId = cmd.getVmId();
final DateUtil.IntervalType intervalType = cmd.getIntervalType();
final String scheduleString = cmd.getSchedule();
final TimeZone timeZone = TimeZone.getTimeZone(cmd.getTimezone());
final Integer maxBackups = cmd.getMaxBackups();
if (intervalType == null) {
throw new CloudRuntimeException("Invalid interval type provided");
}
final VMInstanceVO vm = findVmById(vmId);
validateForZone(vm.getDataCenterId());
accountManager.checkAccess(CallContext.current().getCallingAccount(), null, true, vm);
if (vm.getBackupOfferingId() == null) {
throw new CloudRuntimeException("Cannot configure backup schedule for the VM without having any backup offering");
}
if (maxBackups != null && maxBackups <= 0) {
throw new InvalidParameterValueException(String.format("maxBackups [%s] for instance %s should be greater than 0.", maxBackups, vm.getName()));
}
Backup.Type backupType = Backup.Type.valueOf(intervalType.name());
int intervalMaxBackups = backupType.getMax();
if (maxBackups != null && maxBackups > intervalMaxBackups) {
throw new InvalidParameterValueException(String.format("maxBackups [%s] for instance %s exceeds limit [%s] for interval type [%s].", maxBackups, vm.getName(),
intervalMaxBackups, intervalType));
}
Account owner = accountManager.getAccount(vm.getAccountId());
long accountLimit = resourceLimitMgr.findCorrectResourceLimitForAccount(owner, Resource.ResourceType.backup, null);
long domainLimit = resourceLimitMgr.findCorrectResourceLimitForDomain(domainManager.getDomain(owner.getDomainId()), Resource.ResourceType.backup, null);
if (maxBackups != null && !accountManager.isRootAdmin(owner.getId()) && ((accountLimit != -1 && maxBackups > accountLimit) || (domainLimit != -1 && maxBackups > domainLimit))) {
String message = "domain/account";
if (owner.getType() == Account.Type.PROJECT) {
message = "domain/project";
}
throw new InvalidParameterValueException("Max number of backups shouldn't exceed the " + message + " level backup limit");
}
final BackupOffering offering = backupOfferingDao.findById(vm.getBackupOfferingId());
if (offering == null || !offering.isUserDrivenBackupAllowed()) {
throw new CloudRuntimeException("The selected backup offering does not allow user-defined backup schedule");
}
if (maxBackups == null && !"veeam".equals(offering.getProvider())) {
throw new CloudRuntimeException("Please specify the maximum number of buckets to retain.");
}
if (maxBackups != null && "veeam".equals(offering.getProvider())) {
throw new CloudRuntimeException("The maximum backups to retain cannot be configured through CloudStack for Veeam. Retention is managed directly in Veeam based on the settings specified when creating the backup job.");
}
final String timezoneId = timeZone.getID();
if (!timezoneId.equals(cmd.getTimezone())) {
logger.warn("Using timezone: " + timezoneId + " for running this snapshot policy as an equivalent of " + cmd.getTimezone());
}
Date nextDateTime = null;
try {
nextDateTime = DateUtil.getNextRunTime(intervalType, cmd.getSchedule(), timezoneId, null);
} catch (Exception e) {
throw new InvalidParameterValueException("Invalid schedule: " + cmd.getSchedule() + " for interval type: " + cmd.getIntervalType());
}
final BackupScheduleVO schedule = backupScheduleDao.findByVMAndIntervalType(vmId, intervalType);
if (schedule == null) {
return backupScheduleDao.persist(new BackupScheduleVO(vmId, intervalType, scheduleString, timezoneId, nextDateTime, maxBackups));
}
schedule.setScheduleType((short) intervalType.ordinal());
schedule.setSchedule(scheduleString);
schedule.setTimezone(timezoneId);
schedule.setScheduledTimestamp(nextDateTime);
schedule.setMaxBackups(maxBackups);
backupScheduleDao.update(schedule.getId(), schedule);
return backupScheduleDao.findById(schedule.getId());
}
@Override
public List<BackupSchedule> listBackupSchedule(final Long vmId) {
final VMInstanceVO vm = findVmById(vmId);
validateForZone(vm.getDataCenterId());
accountManager.checkAccess(CallContext.current().getCallingAccount(), null, true, vm);
return backupScheduleDao.listByVM(vmId).stream().map(BackupSchedule.class::cast).collect(Collectors.toList());
}
@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_BACKUP_SCHEDULE_DELETE, eventDescription = "deleting VM backup schedule")
public boolean deleteBackupSchedule(DeleteBackupScheduleCmd cmd) {
Long vmId = cmd.getVmId();
Long id = cmd.getId();
if (ObjectUtils.allNull(vmId, id)) {
throw new InvalidParameterValueException("Either instance ID or ID of backup schedule needs to be specified.");
}
if (Objects.nonNull(id)) {
BackupSchedule schedule = backupScheduleDao.findById(id);
if (schedule == null) {
throw new InvalidParameterValueException("Could not find the requested backup schedule.");
}
checkCallerAccessToBackupScheduleVm(schedule.getVmId());
return backupScheduleDao.remove(schedule.getId());
}
checkCallerAccessToBackupScheduleVm(vmId);
return deleteAllVmBackupSchedules(vmId);
}
/**
* Checks if the backup framework is enabled for the zone in which the VM with specified ID is allocated and
* if the caller has access to the VM.
*
* @param vmId The ID of the virtual machine to check access for
* @throws PermissionDeniedException if the caller doesn't have access to the VM
* @throws CloudRuntimeException if the backup framework is disabled
*/
protected void checkCallerAccessToBackupScheduleVm(long vmId) {
VMInstanceVO vm = findVmById(vmId);
validateForZone(vm.getDataCenterId());
accountManager.checkAccess(CallContext.current().getCallingAccount(), null, true, vm);
}
/**
* Deletes all backup schedules associated with a specific VM.
*
* @param vmId The ID of the virtual machine whose backup schedules should be deleted
* @return true if all backup schedules were successfully deleted, false if any deletion failed
*/
protected boolean deleteAllVmBackupSchedules(long vmId) {
List<BackupScheduleVO> vmBackupSchedules = backupScheduleDao.listByVM(vmId);
boolean success = true;
for (BackupScheduleVO vmBackupSchedule : vmBackupSchedules) {
success = success && backupScheduleDao.remove(vmBackupSchedule.getId());
}
return success;
}
private void postCreateScheduledBackup(Backup.Type backupType, Long vmId) {
DateUtil.IntervalType intervalType = DateUtil.IntervalType.valueOf(backupType.name());
final BackupScheduleVO schedule = backupScheduleDao.findByVMAndIntervalType(vmId, intervalType);
if (schedule == null) {
return;
}
Integer maxBackups = schedule.getMaxBackups();
if (maxBackups == null) {
return;
}
List<BackupVO> backups = backupDao.listBackupsByVMandIntervalType(vmId, backupType);
while (backups.size() > maxBackups) {
BackupVO oldestBackup = backups.get(0);
if (deleteBackup(oldestBackup.getId(), false)) {
ActionEventUtils.onCompletedActionEvent(User.UID_SYSTEM, oldestBackup.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_VM_BACKUP_DELETE,
"Successfully deleted oldest backup: " + oldestBackup.getId(), oldestBackup.getId(), ApiCommandResourceType.Backup.toString(), 0);
}
backups.remove(oldestBackup);
}
}
@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_BACKUP_CREATE, eventDescription = "creating VM backup", async = true)
public boolean createBackup(final Long vmId, final Long scheduleId) throws ResourceAllocationException {
final VMInstanceVO vm = findVmById(vmId);
validateForZone(vm.getDataCenterId());
accountManager.checkAccess(CallContext.current().getCallingAccount(), null, true, vm);
if (vm.getBackupOfferingId() == null) {
throw new CloudRuntimeException("VM has not backup offering configured, cannot create backup before assigning it to a backup offering");
}
final BackupOffering offering = backupOfferingDao.findById(vm.getBackupOfferingId());
if (offering == null) {
throw new CloudRuntimeException("VM backup offering not found");
}
if (!offering.isUserDrivenBackupAllowed()) {
throw new CloudRuntimeException("The assigned backup offering does not allow ad-hoc user backup");
}
Backup.Type type = getBackupType(scheduleId);
Account owner = accountManager.getAccount(vm.getAccountId());
try {
resourceLimitMgr.checkResourceLimit(owner, Resource.ResourceType.backup);
} catch (ResourceAllocationException e) {
if (type != Backup.Type.MANUAL) {
String msg = "Backup resource limit exceeded for account id : " + owner.getId() + ". Failed to create backup";
logger.warn(msg);
alertManager.sendAlert(AlertManager.AlertType.ALERT_TYPE_UPDATE_RESOURCE_COUNT, 0L, 0L, msg, "Backup resource limit exceeded for account id : " + owner.getId()
+ ". Failed to create backups; please use updateResourceLimit to increase the limit");
}
throw e;
}
Long backupSize = 0L;
for (final Volume volume: volumeDao.findByInstance(vmId)) {
if (Volume.State.Ready.equals(volume.getState())) {
Long volumeSize = volumeApiService.getVolumePhysicalSize(volume.getFormat(), volume.getPath(), volume.getChainInfo());
if (volumeSize == null) {
volumeSize = volume.getSize();
}
backupSize += volumeSize;
}
}
try {
resourceLimitMgr.checkResourceLimit(owner, Resource.ResourceType.backup_storage, backupSize);
} catch (ResourceAllocationException e) {
if (type != Backup.Type.MANUAL) {
String msg = "Backup storage space resource limit exceeded for account id : " + owner.getId() + ". Failed to create backup";
logger.warn(msg);
alertManager.sendAlert(AlertManager.AlertType.ALERT_TYPE_UPDATE_RESOURCE_COUNT, 0L, 0L, msg, "Backup storage space resource limit exceeded for account id : " + owner.getId()
+ ". Failed to create backups; please use updateResourceLimit to increase the limit");
}
throw e;
}
ActionEventUtils.onStartedActionEvent(User.UID_SYSTEM, vm.getAccountId(),
EventTypes.EVENT_VM_BACKUP_CREATE, "creating backup for VM ID:" + vm.getUuid(),
vmId, ApiCommandResourceType.VirtualMachine.toString(),
true, 0);
final BackupProvider backupProvider = getBackupProvider(offering.getProvider());
if (backupProvider != null) {
Pair<Boolean, Backup> result = backupProvider.takeBackup(vm);
if (!result.first()) {
throw new CloudRuntimeException("Failed to create VM backup");
}
Backup backup = result.second();
if (backup != null) {
BackupVO vmBackup = backupDao.findById(result.second().getId());
vmBackup.setBackupIntervalType((short) type.ordinal());
backupDao.update(vmBackup.getId(), vmBackup);
resourceLimitMgr.incrementResourceCount(vm.getAccountId(), Resource.ResourceType.backup);
resourceLimitMgr.incrementResourceCount(vm.getAccountId(), Resource.ResourceType.backup_storage, backup.getSize());
}
if (type != Backup.Type.MANUAL) {
postCreateScheduledBackup(type, vm.getId());
}
return true;
}
throw new CloudRuntimeException("Failed to create VM backup");
}
@Override
public Pair<List<Backup>, Integer> listBackups(final ListBackupsCmd cmd) {
final Long id = cmd.getId();
final Long vmId = cmd.getVmId();
final Long zoneId = cmd.getZoneId();
final Account caller = CallContext.current().getCallingAccount();
final String keyword = cmd.getKeyword();
List<Long> permittedAccounts = new ArrayList<Long>();
if (vmId != null) {
VMInstanceVO vm = vmInstanceDao.findByIdIncludingRemoved(vmId);
if (vm != null) {
accountManager.checkAccess(caller, null, true, vm);
}
}
final Ternary<Long, Boolean, Project.ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, Project.ListProjectResourcesCriteria>(cmd.getDomainId(),
cmd.isRecursive(), null);
accountManager.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
final Long domainId = domainIdRecursiveListProject.first();
final Boolean isRecursive = domainIdRecursiveListProject.second();
final Project.ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
final Filter searchFilter = new Filter(BackupVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
SearchBuilder<BackupVO> sb = backupDao.createSearchBuilder();
accountManager.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
sb.and("idIN", sb.entity().getId(), SearchCriteria.Op.IN);
sb.and("vmId", sb.entity().getVmId(), SearchCriteria.Op.EQ);
sb.and("zoneId", sb.entity().getZoneId(), SearchCriteria.Op.EQ);
if (keyword != null) {
SearchBuilder<VMInstanceVO> vmSearch = vmInstanceDao.createSearchBuilder();
vmSearch.and("name", vmSearch.entity().getHostName(), SearchCriteria.Op.LIKE);
sb.groupBy(sb.entity().getId());
sb.join("vmSearch", vmSearch, sb.entity().getVmId(), vmSearch.entity().getId(), JoinBuilder.JoinType.INNER);
}
SearchCriteria<BackupVO> sc = sb.create();
accountManager.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
if (id != null) {
sc.setParameters("id", id);
}
if (vmId != null) {
sc.setParameters("vmId", vmId);
}
if (zoneId != null) {
sc.setParameters("zoneId", zoneId);
}
if (keyword != null) {
sc.setJoinParameters("vmSearch", "name", "%" + keyword + "%");
}
Pair<List<BackupVO>, Integer> result = backupDao.searchAndCount(sc, searchFilter);
return new Pair<>(new ArrayList<>(result.first()), result.second());
}
public boolean importRestoredVM(long zoneId, long domainId, long accountId, long userId,
String vmInternalName, Hypervisor.HypervisorType hypervisorType, Backup backup) {
VirtualMachine vm = null;
HypervisorGuru guru = hypervisorGuruManager.getGuru(hypervisorType);
try {
vm = guru.importVirtualMachineFromBackup(zoneId, domainId, accountId, userId, vmInternalName, backup);
} catch (final Exception e) {
logger.error(String.format("Failed to import VM [vmInternalName: %s] from backup restoration [%s] with hypervisor [type: %s] due to: [%s].", vmInternalName,
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(backup, "id", "uuid", "vmId", "externalId", "backupType"), hypervisorType, e.getMessage()), e);
ActionEventUtils.onCompletedActionEvent(User.UID_SYSTEM, vm.getAccountId(), EventVO.LEVEL_ERROR, EventTypes.EVENT_VM_BACKUP_RESTORE,
String.format("Failed to import VM %s from backup %s with hypervisor [type: %s]", vmInternalName, backup.getUuid(), hypervisorType),
vm.getId(), ApiCommandResourceType.VirtualMachine.toString(),0);
throw new CloudRuntimeException("Error during vm backup restoration and import: " + e.getMessage());
}
if (vm == null) {
String message = String.format("Failed to import restored VM %s with hypervisor type %s using backup of VM ID %s",
vmInternalName, hypervisorType, backup.getVmId());
logger.error(message);
ActionEventUtils.onCompletedActionEvent(User.UID_SYSTEM, vm.getAccountId(), EventVO.LEVEL_ERROR, EventTypes.EVENT_VM_BACKUP_RESTORE,
message, vm.getId(), ApiCommandResourceType.VirtualMachine.toString(),0);
} else {
ActionEventUtils.onCompletedActionEvent(User.UID_SYSTEM, vm.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_VM_BACKUP_RESTORE,
String.format("Restored VM %s from backup %s", vm.getUuid(), backup.getUuid()),
vm.getId(), ApiCommandResourceType.VirtualMachine.toString(),0);
}
return vm != null;
}
@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_BACKUP_RESTORE, eventDescription = "restoring VM from backup", async = true)
public boolean restoreBackup(final Long backupId) {
final BackupVO backup = backupDao.findById(backupId);
if (backup == null) {
throw new CloudRuntimeException("Backup " + backupId + " does not exist");
}
validateForZone(backup.getZoneId());
final VMInstanceVO vm = vmInstanceDao.findByIdIncludingRemoved(backup.getVmId());
if (vm == null) {
throw new CloudRuntimeException("VM ID " + backup.getVmId() + " couldn't be found on existing or removed VMs");
}
accountManager.checkAccess(CallContext.current().getCallingAccount(), null, true, vm);
if (vm.getRemoved() == null && !vm.getState().equals(VirtualMachine.State.Stopped) &&
!vm.getState().equals(VirtualMachine.State.Destroyed)) {
throw new CloudRuntimeException("Existing VM should be stopped before being restored from backup");
}
// This is done to handle historic backups if any with Veeam / Networker plugins
List<Backup.VolumeInfo> backupVolumes = CollectionUtils.isNullOrEmpty(backup.getBackedUpVolumes()) ?
vm.getBackupVolumeList() : backup.getBackedUpVolumes();
List<VolumeVO> vmVolumes = volumeDao.findByInstance(vm.getId());
if (vmVolumes.size() != backupVolumes.size()) {
throw new CloudRuntimeException("Unable to restore VM with the current backup as the backup has different number of disks as the VM");
}
BackupOffering offering = backupOfferingDao.findByIdIncludingRemoved(vm.getBackupOfferingId());
String errorMessage = "Failed to find backup offering of the VM backup.";
if (offering == null) {
logger.warn(errorMessage);
}
logger.debug("Attempting to get backup offering from VM backup");
offering = backupOfferingDao.findByIdIncludingRemoved(backup.getBackupOfferingId());
if (offering == null) {
throw new CloudRuntimeException(errorMessage);
}
String backupDetailsInMessage = ReflectionToStringBuilderUtils.reflectOnlySelectedFields(backup, "uuid", "externalId", "vmId", "type", "status", "date");
tryRestoreVM(backup, vm, offering, backupDetailsInMessage);
updateVolumeState(vm, Volume.Event.RestoreSucceeded, Volume.State.Ready);
updateVmState(vm, VirtualMachine.Event.RestoringSuccess, VirtualMachine.State.Stopped);
return importRestoredVM(vm.getDataCenterId(), vm.getDomainId(), vm.getAccountId(), vm.getUserId(),
vm.getInstanceName(), vm.getHypervisorType(), backup);
}
/**
* Tries to restore a VM from a backup. <br/>
* First update the VM state to {@link VirtualMachine.Event#RestoringRequested} and its volume states to {@link Volume.Event#RestoreRequested}, <br/>
* and then try to restore the backup. <br/>
*
* If restore fails, then update the VM state to {@link VirtualMachine.Event#RestoringFailed}, and its volumes to {@link Volume.Event#RestoreFailed} and throw an {@link CloudRuntimeException}.
*/
protected void tryRestoreVM(BackupVO backup, VMInstanceVO vm, BackupOffering offering, String backupDetailsInMessage) {
try {
updateVmState(vm, VirtualMachine.Event.RestoringRequested, VirtualMachine.State.Restoring);
updateVolumeState(vm, Volume.Event.RestoreRequested, Volume.State.Restoring);
ActionEventUtils.onStartedActionEvent(User.UID_SYSTEM, vm.getAccountId(), EventTypes.EVENT_VM_BACKUP_RESTORE,
String.format("Restoring VM %s from backup %s", vm.getUuid(), backup.getUuid()),
vm.getId(), ApiCommandResourceType.VirtualMachine.toString(),
true, 0);
final BackupProvider backupProvider = getBackupProvider(offering.getProvider());
if (!backupProvider.restoreVMFromBackup(vm, backup)) {
ActionEventUtils.onCompletedActionEvent(User.UID_SYSTEM, vm.getAccountId(), EventVO.LEVEL_ERROR, EventTypes.EVENT_VM_BACKUP_RESTORE,
String.format("Failed to restore VM %s from backup %s", vm.getInstanceName(), backup.getUuid()),
vm.getId(), ApiCommandResourceType.VirtualMachine.toString(),0);
throw new CloudRuntimeException("Error restoring VM from backup with uuid " + backup.getUuid());
}
// The restore process is executed by a backup provider outside of ACS, I am using the catch-all (Exception) to
// ensure that no provider-side exception is missed. Therefore, we have a proper handling of exceptions, and rollbacks if needed.
} catch (Exception e) {
logger.error(String.format("Failed to restore backup [%s] due to: [%s].", backupDetailsInMessage, e.getMessage()), e);
updateVolumeState(vm, Volume.Event.RestoreFailed, Volume.State.Ready);
updateVmState(vm, VirtualMachine.Event.RestoringFailed, VirtualMachine.State.Stopped);
throw new CloudRuntimeException(String.format("Error restoring VM from backup [%s].", backupDetailsInMessage));
}
}
private Backup.Type getBackupType(Long scheduleId) {
if (scheduleId.equals(Snapshot.MANUAL_POLICY_ID)) {
return Backup.Type.MANUAL;
} else {
BackupScheduleVO scheduleVO = backupScheduleDao.findById(scheduleId);
DateUtil.IntervalType intvType = scheduleVO.getScheduleType();
return getBackupType(intvType);
}
}
private Backup.Type getBackupType(DateUtil.IntervalType intvType) {
if (intvType.equals(DateUtil.IntervalType.HOURLY)) {
return Backup.Type.HOURLY;
} else if (intvType.equals(DateUtil.IntervalType.DAILY)) {
return Backup.Type.DAILY;
} else if (intvType.equals(DateUtil.IntervalType.WEEKLY)) {
return Backup.Type.WEEKLY;
} else if (intvType.equals(DateUtil.IntervalType.MONTHLY)) {
return Backup.Type.MONTHLY;
}
return null;
}
/**
* Tries to update the state of given VM, given specified event
* @param vm The VM to update its state
* @param event The event to update the VM state
* @param next The desired state, just needed to add more context to the logs
*/
private void updateVmState(VMInstanceVO vm, VirtualMachine.Event event, VirtualMachine.State next) {
logger.debug(String.format("Trying to update state of VM [%s] with event [%s].", vm, event));
Transaction.execute(TransactionLegacy.CLOUD_DB, (TransactionCallback<VMInstanceVO>) status -> {
try {
if (!virtualMachineManager.stateTransitTo(vm, event, vm.getHostId())) {
throw new CloudRuntimeException(String.format("Unable to change state of VM [%s] to [%s].", vm, next));
}
} catch (NoTransitionException e) {
String errMsg = String.format("Failed to update state of VM [%s] with event [%s] due to [%s].", vm, event, e.getMessage());
logger.error(errMsg, e);
throw new RuntimeException(errMsg);
}
return null;
});
}
/**
* Tries to update all volume states of given VM, given specified event
* @param vm The VM to which the volumes belong
* @param event The event to update the volume states
* @param next The desired state, just needed to add more context to the logs
*/
private void updateVolumeState(VMInstanceVO vm, Volume.Event event, Volume.State next) {
Transaction.execute(TransactionLegacy.CLOUD_DB, (TransactionCallback<VolumeVO>) status -> {
for (VolumeVO volume : volumeDao.findIncludingRemovedByInstanceAndType(vm.getId(), null)) {
tryToUpdateStateOfSpecifiedVolume(volume, event, next);
}
return null;
});
}
/**
* Tries to update the state of just one volume using any passed {@link Volume.Event}. Throws an {@link RuntimeException} when fails.
* @param volume The volume to update it state
* @param event The event to update the volume state
* @param next The desired state, just needed to add more context to the logs
*
*/
private void tryToUpdateStateOfSpecifiedVolume(VolumeVO volume, Volume.Event event, Volume.State next) {
logger.debug(String.format("Trying to update state of volume [%s] with event [%s].", volume, event));
try {
if (!volumeApiService.stateTransitTo(volume, event)) {
throw new CloudRuntimeException(String.format("Unable to change state of volume [%s] to [%s].", volume, next));
}
} catch (NoTransitionException e) {
String errMsg = String.format("Failed to update state of volume [%s] with event [%s] due to [%s].", volume, event, e.getMessage());
logger.error(errMsg, e);
throw new RuntimeException(errMsg);
}
}
private Backup.VolumeInfo getVolumeInfo(List<Backup.VolumeInfo> backedUpVolumes, String volumeUuid) {
for (Backup.VolumeInfo volInfo : backedUpVolumes) {
if (volInfo.getUuid().equals(volumeUuid)) {
return volInfo;
}
}
return null;
}
@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_BACKUP_RESTORE, eventDescription = "restoring VM from backup", async = true)
public boolean restoreBackupVolumeAndAttachToVM(final String backedUpVolumeUuid, final Long backupId, final Long vmId) throws Exception {
if (StringUtils.isEmpty(backedUpVolumeUuid)) {
throw new CloudRuntimeException("Invalid volume ID passed");
}
final BackupVO backup = backupDao.findById(backupId);
if (backup == null) {
throw new CloudRuntimeException("Provided backup not found");
}
validateForZone(backup.getZoneId());
final VMInstanceVO vm = findVmById(vmId);
accountManager.checkAccess(CallContext.current().getCallingAccount(), null, true, vm);
if (vm.getBackupOfferingId() != null && !BackupEnableAttachDetachVolumes.value()) {
throw new CloudRuntimeException("The selected VM has backups, cannot restore and attach volume to the VM.");
}
if (backup.getZoneId() != vm.getDataCenterId()) {
throw new CloudRuntimeException("Cross zone backup restoration of volume is not allowed");
}
final VMInstanceVO vmFromBackup = vmInstanceDao.findByIdIncludingRemoved(backup.getVmId());
if (vmFromBackup == null) {
throw new CloudRuntimeException("VM reference for the provided VM backup not found");
}
accountManager.checkAccess(CallContext.current().getCallingAccount(), null, true, vmFromBackup);
final BackupOffering offering = backupOfferingDao.findByIdIncludingRemoved(backup.getBackupOfferingId());
if (offering == null) {
throw new CloudRuntimeException("Failed to find VM backup offering");
}
BackupProvider backupProvider = getBackupProvider(offering.getProvider());
VolumeVO backedUpVolume = volumeDao.findByUuid(backedUpVolumeUuid);
Pair<HostVO, StoragePoolVO> restoreInfo;
if (!"nas".equals(offering.getProvider())) {
restoreInfo = getRestoreVolumeHostAndDatastore(vm);
} else {
restoreInfo = getRestoreVolumeHostAndDatastoreForNas(vm, backedUpVolume);
}
HostVO host = restoreInfo.first();
StoragePoolVO datastore = restoreInfo.second();
logger.debug("Asking provider to restore volume {} from backup {} (with external" +
" ID {}) and attach it to VM: {}", backedUpVolumeUuid, backup, backup.getExternalId(), vm);
logger.debug(String.format("Trying to restore volume using host private IP address: [%s].", host.getPrivateIpAddress()));
String[] hostPossibleValues = {host.getPrivateIpAddress(), host.getName()};
String[] datastoresPossibleValues = {datastore.getUuid(), datastore.getName()};
Pair<Boolean, String> result = restoreBackedUpVolume(backedUpVolumeUuid, backup, backupProvider, hostPossibleValues, datastoresPossibleValues, vm);
if (BooleanUtils.isFalse(result.first())) {
throw new CloudRuntimeException(String.format("Error restoring volume [%s] of VM [%s] to host [%s] using backup provider [%s] due to: [%s].",
backedUpVolumeUuid, vm.getUuid(), host.getUuid(), backupProvider.getName(), result.second()));
}
if (!attachVolumeToVM(vm.getDataCenterId(), result.second(), vmFromBackup.getBackupVolumeList(),
backedUpVolumeUuid, vm, datastore.getUuid(), backup)) {
throw new CloudRuntimeException(String.format("Error attaching volume [%s] to VM [%s]." + backedUpVolumeUuid, vm.getUuid()));
}
return true;
}
protected Pair<Boolean, String> restoreBackedUpVolume(final String backedUpVolumeUuid, final BackupVO backup, BackupProvider backupProvider, String[] hostPossibleValues,
String[] datastoresPossibleValues, VMInstanceVO vm) {
Pair<Boolean, String> result = new Pair<>(false, "");
for (String hostData : hostPossibleValues) {
for (String datastoreData : datastoresPossibleValues) {
logger.debug(String.format("Trying to restore volume [UUID: %s], using host [%s] and datastore [%s].",