-
Notifications
You must be signed in to change notification settings - Fork 155
/
updatestatus.go
1212 lines (1130 loc) · 39.1 KB
/
updatestatus.go
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
// Copyright (c) 2017-2018 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package zedmanager
import (
"fmt"
"github.com/lf-edge/eve/pkg/pillar/hypervisor"
"time"
"github.com/google/go-cmp/cmp"
"github.com/lf-edge/eve/pkg/pillar/types"
"github.com/satori/go.uuid"
)
// Update this AppInstanceStatus generate config updates to
// the microservices
func updateAIStatusUUID(ctx *zedmanagerContext, uuidStr string) {
log.Functionf("updateAIStatusUUID(%s)", uuidStr)
status := lookupAppInstanceStatus(ctx, uuidStr)
if status == nil {
log.Functionf("updateAIStatusUUID for %s: Missing AppInstanceStatus",
uuidStr)
return
}
config := lookupAppInstanceConfig(ctx, uuidStr, true)
if config == nil || (status.PurgeInprogress == types.BringDown) {
removeAIStatus(ctx, status)
return
}
changed := doUpdate(ctx, *config, status)
if changed {
log.Functionf("updateAIStatusUUID status change %d for %s",
status.State, uuidStr)
publishAppInstanceStatus(ctx, status)
publishAppInstanceSummary(ctx)
}
}
// Remove this AppInstanceStatus and generate config removes for
// the microservices
func removeAIStatusUUID(ctx *zedmanagerContext, uuidStr string) {
log.Functionf("removeAIStatusUUID(%s)", uuidStr)
status := lookupAppInstanceStatus(ctx, uuidStr)
if status == nil {
log.Functionf("removeAIStatusUUID for %s: Missing AppInstanceStatus",
uuidStr)
return
}
removeAIStatus(ctx, status)
}
func removeAIStatus(ctx *zedmanagerContext, status *types.AppInstanceStatus) {
uuidStr := status.Key()
uninstall := (status.PurgeInprogress != types.BringDown)
domainStatus := lookupDomainStatus(ctx, uuidStr)
// The VM has been just shutdown in a result of the purge&update command coming from the controller.
if !uninstall && domainStatus != nil && !domainStatus.Activated {
// We should do it before the doRemove is called, so that all the volumes are still available.
if status.SnapStatus.SnapshotOnUpgrade && len(status.SnapStatus.PreparedVolumesSnapshotConfigs) > 0 {
// Check whether there are snapshots to be deleted first (not to exceed the maximum number of snapshots).
if len(status.SnapStatus.SnapshotsToBeDeleted) > 0 {
triggerSnapshotDeletion(status.SnapStatus.SnapshotsToBeDeleted, ctx, status)
}
triggerSnapshots(ctx, status)
}
}
changed, done := doRemove(ctx, status, uninstall)
if changed {
log.Functionf("removeAIStatus status change for %s",
uuidStr)
publishAppInstanceStatus(ctx, status)
}
if !done {
if uninstall {
log.Functionf("removeAIStatus(%s) waiting for removal",
status.Key())
} else {
log.Functionf("removeAIStatus(%s): PurgeInprogress waiting for removal",
status.Key())
}
return
}
if uninstall {
log.Functionf("removeAIStatus(%s) remove done", uuidStr)
// Write out what we modified to AppInstanceStatus aka delete
unpublishAppInstanceStatus(ctx, status)
return
}
log.Functionf("removeAIStatus(%s): PurgeInprogress RecreateVolumes",
status.Key())
status.PurgeInprogress = types.RecreateVolumes
publishAppInstanceStatus(ctx, status)
config := lookupAppInstanceConfig(ctx, uuidStr, true)
if config != nil {
changed := purgeCmdDone(ctx, *config, status)
if changed {
publishAppInstanceStatus(ctx, status)
}
changed = doUpdate(ctx, *config, status)
if changed {
publishAppInstanceStatus(ctx, status)
publishAppInstanceSummary(ctx)
}
} else {
log.Errorf("removeAIStatus(%s): PurgeInprogress no config!",
status.Key())
}
}
// doUpdate will set checkFreedResources in context if some resources
// might have been freed up.
func doUpdate(ctx *zedmanagerContext,
config types.AppInstanceConfig,
status *types.AppInstanceStatus) bool {
uuidStr := status.Key()
changed := false
done := false
log.Functionf("doUpdate: UUID:%s, Name", uuidStr)
// Manage events necessitating VM shutdown (such as snapshot removal, rollback).
// This is different from instances where the VM is deactivated due to a purge&update
// command from the controller, which is taken care of in the removeAIStatus function.
domainStatus := lookupDomainStatus(ctx, uuidStr)
// Is the VM already shutdown?
if domainStatus != nil && !domainStatus.Activated {
// Trigger snapshot removal
// Note, that we do not restart the VM explicitly for the snapshot removal, we just wait for the next restart,
// which ends in this line of code.
if len(status.SnapStatus.SnapshotsToBeDeleted) > 0 {
triggerSnapshotDeletion(status.SnapStatus.SnapshotsToBeDeleted, ctx, status)
}
// Trigger the rollback process
if status.SnapStatus.HasRollbackRequest {
err := triggerRollback(ctx, status)
if err != nil {
errDesc := types.ErrorDescription{}
errDesc.ErrorTime = time.Now()
errStr := fmt.Sprintf("doUpdate(%s) triggerRollback failed: %s", uuidStr, err)
errDesc.Error = errStr
log.Error(errStr)
status.SnapStatus.HasRollbackRequest = false
errDesc.ErrorSeverity = types.ErrorSeverityWarning
setSnapshotStatusError(status, status.SnapStatus.ActiveSnapshot, errDesc)
status.SetErrorWithSourceAndDescription(errDesc, types.AppInstanceStatus{})
publishAppInstanceStatus(ctx, status)
return true
}
status.SnapStatus.HasRollbackRequest = false
publishAppInstanceStatus(ctx, status)
return true
}
}
// The existence of Config is interpreted to mean the
// AppInstance should be INSTALLED. Activate is checked separately.
changed, done = doInstall(ctx, config, status)
if !done {
return changed
}
// Are we doing a purge?
if status.PurgeInprogress == types.DownloadAndVerify {
log.Functionf("PurgeInprogress(%s) volumemgr done",
status.Key())
status.PurgeInprogress = types.BringDown
status.State = types.HALTING
changed = true
// Keep the old volumes in place
_, done := doRemove(ctx, status, false)
if !done {
log.Functionf("PurgeInprogress(%s) waiting for removal",
status.Key())
return changed
}
log.Functionf("PurgeInprogress(%s) bringing it up",
status.Key())
}
if status.PurgeInprogress == types.RecreateVolumes {
status.PurgeInprogress = types.BringUp
changed = true
}
c, done := doPrepare(ctx, config, status)
changed = changed || c
if !done {
return changed
}
// Check if we are still rolling back. Should not activate in that case.
if status.SnapStatus.RollbackInProgress {
log.Functionf("Rollback in progress for %s", uuidStr)
return changed
}
effectiveActivate := effectiveActivateCurrentProfile(config, ctx.currentProfile)
if !effectiveActivate {
if status.Activated || status.ActivateInprogress {
c := doInactivateHalt(ctx, config, status)
changed = changed || c
}
// Activated and ActivateInprogress flags may be changed during doInactivateHalt call
if !status.Activated && !status.ActivateInprogress {
// Since we are not activating we set the state to
// HALTED to indicate it is not running since it
// might have been halted before the device was rebooted
if status.State == types.INSTALLED || status.State == types.START_DELAYED {
status.State = types.HALTED
changed = true
}
}
log.Functionf("Waiting for config.Activate for %s", uuidStr)
return changed
}
log.Functionf("Have config.Activate for %s", uuidStr)
c = doActivate(ctx, uuidStr, config, status)
changed = changed || c
log.Functionf("doUpdate done for %s", uuidStr)
return changed
}
func triggerSnapshots(ctx *zedmanagerContext, status *types.AppInstanceStatus) {
log.Noticef("triggerSnapshots(%s)", status.Key())
timeTriggered := time.Now()
// Set time triggered for snapshots that are not triggered by time
for _, snapshot := range status.SnapStatus.RequestedSnapshots {
if snapshot.Snapshot.SnapshotType == types.SnapshotTypeAppUpdate && snapshot.TimeTriggered.IsZero() {
log.Noticef("Setting snapshot %s timeTriggered to %v", snapshot.Snapshot.SnapshotID, timeTriggered)
snapshot.TimeTriggered = timeTriggered
}
}
// trigger the snapshots. Use the list of prepared VolumeSnapshotConfigs for that
for _, volumesSnapshotConfig := range status.SnapStatus.PreparedVolumesSnapshotConfigs {
log.Noticef("Triggering snapshot %s", volumesSnapshotConfig.SnapshotID)
publishVolumesSnapshotConfig(ctx, &volumesSnapshotConfig)
removePreparedVolumesSnapshotConfig(status, volumesSnapshotConfig.SnapshotID)
}
publishAppInstanceStatus(ctx, status)
}
// triggerRollback triggers the rollback process. It also restores the volumeRefStatuses from the snapshot config and
// updates the list of volumeRefConfigs. It returns the config of the app instance to be rolled back to.
func triggerRollback(ctx *zedmanagerContext, status *types.AppInstanceStatus) error {
log.Noticef("Triggering rollback with snapshot %s", status.SnapStatus.ActiveSnapshot)
// lookup for VolumesSnapshotConfig in the channel
volumesSnapshotConfig := lookupVolumesSnapshotConfig(ctx, status.SnapStatus.ActiveSnapshot)
if volumesSnapshotConfig != nil {
// We have found the VolumesSnapshotConfig in the channel
// Switch the action to rollback
volumesSnapshotConfig.Action = types.VolumesSnapshotRollback
publishVolumesSnapshotConfig(ctx, volumesSnapshotConfig)
return nil
}
// We have not found the VolumesSnapshotConfig in the channel, maybe the system was rebooted
// Create a new one and publish it
volumesSnapshotConfig = &types.VolumesSnapshotConfig{
SnapshotID: status.SnapStatus.ActiveSnapshot,
VolumeIDs: make([]uuid.UUID, 0),
Action: types.VolumesSnapshotRollback,
AppUUID: status.UUIDandVersion.UUID,
}
for _, volumeRefConfig := range status.VolumeRefStatusList {
volumesSnapshotConfig.VolumeIDs = append(volumesSnapshotConfig.VolumeIDs, volumeRefConfig.VolumeID)
}
publishVolumesSnapshotConfig(ctx, volumesSnapshotConfig)
return nil
}
func triggerSnapshotDeletion(snapshotsToBeDeleted []types.SnapshotDesc, ctx *zedmanagerContext, status *types.AppInstanceStatus) {
for _, snapshot := range snapshotsToBeDeleted {
log.Noticef("Deleting snapshot %s", snapshot.SnapshotID)
volumesSnapshotConfig := lookupVolumesSnapshotConfig(ctx, snapshot.SnapshotID)
if volumesSnapshotConfig != nil {
// The snapshot has already been triggered, so we need to delete the config and notify volumemanager
log.Noticef("It has already been triggered, so deleting the config and notifying volumemanager")
volumesSnapshotConfig.Action = types.VolumesSnapshotDelete
unpublishVolumesSnapshotConfig(ctx, volumesSnapshotConfig)
// Remove the snapshot from the list of snapshots to be deleted and publish the status
removeSnapshotDescFromSlice(&status.SnapStatus.SnapshotsToBeDeleted, snapshot.SnapshotID)
publishAppInstanceStatus(ctx, status)
}
}
}
func doInstall(ctx *zedmanagerContext,
config types.AppInstanceConfig,
status *types.AppInstanceStatus) (bool, bool) {
uuidStr := status.Key()
log.Functionf("doInstall: UUID: %s", uuidStr)
allErrors := ""
var errorSource interface{}
var errorTime time.Time
var entities []*types.ErrorEntity
var severity types.ErrorSeverity
var retryCondition string //propagate only one
changed := false
if len(config.VolumeRefConfigList) != len(status.VolumeRefStatusList) {
errString := fmt.Sprintf("Mismatch in volumeRefConfig vs. Status length: %d vs %d",
len(config.VolumeRefConfigList),
len(status.VolumeRefStatusList))
if status.PurgeInprogress == types.NotInprogress {
log.Errorln(errString)
status.SetError(errString, time.Now())
return true, false
}
log.Warnln(errString)
}
// Removing VolumeRefConfig which are removed from the AppInstanceConfig
// and not used by any domain while purging. VolumeRefConfig which are
// not in AppInstanceConfig but used by some running domain will be
// removed as part of purgeCmdDone.
if status.PurgeInprogress == types.DownloadAndVerify {
domainVolMap := make(map[string]bool)
domainConfig := lookupDomainConfig(ctx, status.Key())
if domainConfig != nil {
for _, dc := range domainConfig.DiskConfigList {
domainVolMap[dc.VolumeKey] = true
}
}
removed := false
newVrs := []types.VolumeRefStatus{}
for i := range status.VolumeRefStatusList {
vrs := &status.VolumeRefStatusList[i]
_, ok := domainVolMap[vrs.Key()]
vrc := getVolumeRefConfigFromAIConfig(&config, *vrs)
if vrc != nil || ok {
newVrs = append(newVrs, *vrs)
continue
}
log.Functionf("Removing potentially bad VolumeRefStatus %v",
vrs)
if status.IsErrorSource(vrs.ErrorSourceType) {
log.Functionf("Removing error %s", status.Error)
status.ClearErrorWithSource()
}
MaybeRemoveVolumeRefConfig(ctx, config.UUIDandVersion.UUID,
vrs.VolumeID, vrs.GenerationCounter, vrs.LocalGenerationCounter)
if !vrs.PendingAdd {
vrs.PendingAdd = true
// Keep in VolumeRefStatus until we get an update
// from volumemgr
newVrs = append(newVrs, *vrs)
removed = true
}
}
log.Functionf("purge inactive (%s) volumeRefStatus from %d to %d",
config.Key(), len(status.VolumeRefStatusList), len(newVrs))
status.VolumeRefStatusList = newVrs
if removed {
log.Functionf("Waiting for bad VolumeRefStatus to go away for AppInst %s",
status.Key())
return removed, false
}
}
// Any VolumeRefStatus to add?
for _, vrc := range config.VolumeRefConfigList {
vrs := getVolumeRefStatusFromAIStatus(status, vrc)
if vrs != nil {
continue
}
if status.PurgeInprogress == types.NotInprogress {
errString := fmt.Sprintf(
"New volumeRefConfig (VolumeID: %s, GenerationCounter: %d, "+
"LocalGenerationCounter: %d) found. "+
"New Storage configs are not allowed unless purged",
vrc.VolumeID, vrc.GenerationCounter, vrc.LocalGenerationCounter)
log.Error(errString)
status.SetError(errString, time.Now())
return true, false
}
newVrs := types.VolumeRefStatus{
VolumeID: vrc.VolumeID,
GenerationCounter: vrc.GenerationCounter,
LocalGenerationCounter: vrc.LocalGenerationCounter,
RefCount: vrc.RefCount,
MountDir: vrc.MountDir,
PendingAdd: true,
State: types.INITIAL,
VerifyOnly: vrc.VerifyOnly,
}
log.Functionf("Adding new VolumeRefStatus %v", newVrs)
status.VolumeRefStatusList = append(status.VolumeRefStatusList, newVrs)
changed = true
}
if status.PurgeInprogress == types.NotInprogress || status.PurgeInprogress == types.RecreateVolumes {
for i := range config.VolumeRefConfigList {
vrc := &config.VolumeRefConfigList[i]
vrsPubSub := lookupVolumeRefStatus(ctx, vrc.Key())
if vrsPubSub == nil {
continue
}
if vrsPubSub.VerifyOnly && vrsPubSub.State == types.LOADED {
vrc.VerifyOnly = false
publishVolumeRefConfig(ctx, vrc)
}
}
}
for i := range status.VolumeRefStatusList {
vrs := &status.VolumeRefStatusList[i]
// install volume ref again if we had an error
if status.State < types.CREATED_VOLUME || status.PurgeInprogress != types.NotInprogress || vrs.HasError() {
c := doInstallVolumeRef(ctx, config, status, vrs)
if c {
changed = true
}
}
}
// Determine minimum state and errors across all of VolumeRefStatus
minState := types.MAXSTATE
for _, vrs := range status.VolumeRefStatusList {
if vrs.State < minState {
minState = vrs.State
}
if vrs.HasError() {
errorSource = vrs.ErrorSourceType
errorTime = vrs.ErrorTime
allErrors = appendError(allErrors, vrs.Error)
entities = append(entities, &types.ErrorEntity{EntityID: vrs.VolumeID.String(), EntityType: types.ErrorEntityVolume})
if vrs.ErrorSeverity > severity {
severity = vrs.ErrorSeverity
}
if vrs.ErrorRetryCondition != "" {
retryCondition = vrs.ErrorRetryCondition
}
}
}
if minState == types.MAXSTATE {
// No VolumeRefStatus
minState = types.INITIAL
}
if status.State >= types.BOOTING {
// Leave unchanged
} else {
status.State = minState
changed = true
}
if allErrors == "" {
status.ClearErrorWithSource()
} else if errorSource == nil {
status.SetError(allErrors, errorTime)
} else {
if retryCondition == "" {
// if no retry condition it is an error
severity = types.ErrorSeverityError
}
description := types.ErrorDescription{
Error: allErrors,
ErrorEntities: entities,
ErrorSeverity: severity,
ErrorRetryCondition: retryCondition,
ErrorTime: errorTime,
}
status.SetErrorWithSourceAndDescription(description, errorSource)
}
if allErrors != "" {
log.Errorf("Volumemgr error for %s: %s", uuidStr, allErrors)
return changed, false
}
if status.PurgeInprogress != types.DownloadAndVerify && status.PurgeInprogress != types.BringDown && minState < types.CREATED_VOLUME {
log.Functionf("Waiting for all new volumes for %s", uuidStr)
return changed, false
}
if status.PurgeInprogress == types.DownloadAndVerify && minState < types.LOADED {
log.Functionf("Waiting for all volumes to be loaded for %s", uuidStr)
return changed, false
}
log.Functionf("Done with volumes for %s", uuidStr)
log.Functionf("doInstall done for %s", uuidStr)
return changed, true
}
// If VolumeRefStatus was updated we return true
func doInstallVolumeRef(ctx *zedmanagerContext, config types.AppInstanceConfig,
status *types.AppInstanceStatus, vrs *types.VolumeRefStatus) bool {
changed := false
if vrs.PendingAdd {
MaybeAddVolumeRefConfig(ctx, config.UUIDandVersion.UUID,
vrs.VolumeID, vrs.GenerationCounter, vrs.LocalGenerationCounter,
vrs.MountDir, vrs.VerifyOnly)
vrs.PendingAdd = false
changed = true
}
log.Functionf("doInstallVolumeRef: VolumeRefStatus volumeID %s, "+
"generationCounter %d, localGenerationCounter %d",
vrs.VolumeID, vrs.GenerationCounter, vrs.LocalGenerationCounter)
// VolumeRefStatus in app instance status is updated with the volume
// ref status published from the volumemgr if status gets changed
pubsubVrs := lookupVolumeRefStatus(ctx, vrs.Key())
if pubsubVrs == nil {
log.Functionf("doInstallVolumeRef: Volumemgr VolumeRefStatus not found. key: %s", vrs.Key())
return changed
}
if !cmp.Equal(vrs, pubsubVrs) {
*vrs = *pubsubVrs
changed = true
log.Functionf("VolumeRefStatus updated for %s", vrs.Key())
}
return changed
}
func doPrepare(ctx *zedmanagerContext,
config types.AppInstanceConfig, status *types.AppInstanceStatus) (bool, bool) {
uuidStr := status.Key()
log.Functionf("doPrepare for %s", uuidStr)
changed := false
// Automatically move from VERIFIED to INSTALLED
if status.State >= types.BOOTING {
// Leave unchanged
} else {
status.State = types.INSTALLED
changed = true
}
changed = true
log.Functionf("doPrepare done for %s", uuidStr)
return changed, true
}
// doActivate - Returns if the status has changed. Doesn't publish any changes.
// It is caller's responsibility to publish.
func doActivate(ctx *zedmanagerContext, uuidStr string,
config types.AppInstanceConfig, status *types.AppInstanceStatus) bool {
log.Functionf("doActivate for %s", uuidStr)
changed := false
// Are we doing a restart and it came down?
switch status.RestartInprogress {
case types.BringDown:
// If !status.Activated e.g, due to error, then
// need to bring down first.
ds := lookupDomainStatus(ctx, config.Key())
if ds != nil {
if status.DomainName != ds.DomainName {
status.DomainName = ds.DomainName
changed = true
}
if status.BootTime != ds.BootTime {
log.Functionf("Update boottime to %s for %s",
ds.BootTime.Format(time.RFC3339Nano),
status.Key())
status.BootTime = ds.BootTime
changed = true
}
c := updateVifUsed(status, *ds)
if c {
changed = true
}
if !ds.Activated && !ds.HasError() {
log.Functionf("RestartInprogress(%s) came down - set bring up",
status.Key())
status.RestartInprogress = types.BringUp
changed = true
}
}
}
// Check that if we have sufficient memory
if !status.ActivateInprogress && !status.Activated &&
!ctx.globalConfig.GlobalValueBool(types.IgnoreMemoryCheckForApps) {
// If we have not yet calculated memory overhead - do it now
if status.MemOverhead == 0 {
// Get hypervisor
hyp, err := hypervisor.GetHypervisor(*ctx.hypervisorPtr)
if err != nil {
log.Fatalf("Cannot get hypervisor: %s", err)
}
status.MemOverhead, err = hyp.CountMemOverhead(status.DomainName, config.UUIDandVersion.UUID,
int64(config.FixedResources.Memory), int64(config.FixedResources.VMMMaxMem),
int64(config.FixedResources.MaxCpus), int64(config.FixedResources.VCpus), config.IoAdapterList,
ctx.assignableAdapters, ctx.globalConfig)
// We have to publish the status here, because we need to save the memory overhead value, it's used in getRemainingMemory
publishAppInstanceStatus(ctx, status)
}
remaining, latent, halting, err := getRemainingMemory(ctx)
if err != nil {
errStr := fmt.Sprintf("getRemainingMemory failed: %s\n",
err)
log.Errorf("doActivate(%s) failed: %s",
status.Key(), errStr)
description := types.ErrorDescription{
Error: errStr,
ErrorSeverity: types.ErrorSeverityNotice,
ErrorRetryCondition: "Will retry when information about memory will be available in zedmanager",
}
status.SetErrorWithSourceAndDescription(description,
types.AppInstanceConfig{})
status.MissingMemory = true
changed = true
return changed
}
need := uint64(config.FixedResources.Memory)<<10 + status.MemOverhead
if remaining < need {
var errStr string
var entities []*types.ErrorEntity
errSeverity := types.ErrorSeverityError
if remaining+halting < need {
errStr = fmt.Sprintf("Remaining memory bytes %d app instance needs %d",
remaining, need)
} else {
errStr = fmt.Sprintf("App instance needs %d bytes but only have %d; waiting for one or more halting app instances to free up %d bytes",
need, remaining, halting)
errSeverity = types.ErrorSeverityNotice
}
for _, st := range ctx.pubAppInstanceStatus.GetAll() {
status := st.(types.AppInstanceStatus)
if status.Activated || status.ActivateInprogress {
entities = append(entities, &types.ErrorEntity{EntityID: status.UUIDandVersion.UUID.String(), EntityType: types.ErrorEntityAppInstance})
}
}
retryCondition := ""
if len(entities) > 0 {
if errSeverity == types.ErrorSeverityError {
//reduce error severity due to domains with status.Activated || status.ActivateInprogress
errSeverity = types.ErrorSeverityWarning
}
retryCondition = "Retry will be triggered when one or more apps will shutdown"
}
log.Errorf("doActivate(%s) failed: %s",
status.Key(), errStr)
description := types.ErrorDescription{
Error: errStr,
ErrorSeverity: errSeverity,
ErrorRetryCondition: retryCondition,
ErrorEntities: entities,
}
status.SetErrorWithSourceAndDescription(description, types.AppInstanceConfig{})
status.MissingMemory = true
changed = true
return changed
}
if remaining < latent+need {
log.Warnf("Deploying %s memory %d kB remaining %d kB but latent memory use %d kB",
config.DisplayName, config.FixedResources.Memory,
remaining>>10, latent>>10)
} else {
log.Functionf("Deploying %s memory %d kB remaining %d kB latent %d kB",
config.DisplayName, config.FixedResources.Memory,
remaining>>10, latent>>10)
}
}
// Commit that we will be using memory and
// Track that we have cleanup work in case something fails
status.ActivateInprogress = true
// Make sure we have an AppNetworkConfig
MaybeAddAppNetworkConfig(ctx, config, status)
// Check AppNetworkStatus
ns := lookupAppNetworkStatus(ctx, uuidStr)
if ns == nil {
log.Functionf("Waiting for AppNetworkStatus for %s", uuidStr)
return changed
}
if ns.Pending() {
log.Functionf("Waiting for AppNetworkStatus !Pending for %s", uuidStr)
return changed
}
if ns.HasError() {
log.Errorf("Received error from zedrouter for %s: %s",
uuidStr, ns.Error)
status.SetErrorWithSource(ns.Error, types.AppNetworkStatus{},
ns.ErrorTime)
changed = true
return changed
}
if ns.AwaitNetworkInstance {
log.Functionf("Waiting for required network instances to arrive for %s", uuidStr)
status.State = types.AWAITNETWORKINSTANCE
changed = true
return changed
}
updateAppNetworkStatus(status, ns)
if !ns.Activated {
log.Functionf("Waiting for AppNetworkStatus Activated for %s", uuidStr)
return changed
}
if status.IsErrorSource(types.AppNetworkStatus{}) {
log.Functionf("Clearing zedrouter error %s", status.Error)
status.ClearErrorWithSource()
changed = true
}
log.Tracef("Done with AppNetworkStatus for %s", uuidStr)
// Do we try to activate an application earlier than it's configured to start?
if time.Now().Before(status.StartTime) {
// Check that we delay a not yet active VM or a VM in the bring-up state after restarting/purging
if !status.Activated || status.RestartInprogress == types.BringUp || status.PurgeInprogress == types.BringUp {
// If we try to activate it for the first time - mark is with the corresponding state
if status.State != types.START_DELAYED {
status.State = types.START_DELAYED
return true
}
// if the VM is already in the START_DELAYED state - just return from the doActivate now
return changed
}
// if the VM already active or in restarting/purging state - continue with the doActivate logic
}
// Make sure we have a DomainConfig
// We modify it below and then publish it
dc, err := MaybeAddDomainConfig(ctx, config, *status, ns)
if err != nil {
log.Errorf("Error from MaybeAddDomainConfig for %s: %s",
uuidStr, err)
status.SetErrorWithSource(err.Error(), types.DomainStatus{},
time.Now())
changed = true
log.Functionf("Waiting for DomainStatus Activated for %s",
uuidStr)
if dc != nil {
publishDomainConfig(ctx, dc)
}
return changed
}
// Check DomainStatus; update AppInstanceStatus if error
ds := lookupDomainStatus(ctx, uuidStr)
if ds == nil {
log.Functionf("Waiting for DomainStatus for %s", uuidStr)
publishDomainConfig(ctx, dc)
return changed
}
if status.DomainName != ds.DomainName {
status.DomainName = ds.DomainName
changed = true
}
if status.BootTime != ds.BootTime {
log.Functionf("Update boottime to %s for %s",
ds.BootTime.Format(time.RFC3339Nano), status.Key())
status.BootTime = ds.BootTime
changed = true
}
c := updateVifUsed(status, *ds)
if c {
changed = true
}
// Are we doing a restart?
if status.RestartInprogress == types.BringDown {
if dc.Activate {
log.Functionf("RestartInprogress(%s) Clear Activate",
status.Key())
dc.Activate = false
} else if !ds.Activated {
log.Functionf("RestartInprogress(%s) Set Activate",
status.Key())
status.RestartInprogress = types.BringUp
changed = true
dc.Activate = true
} else {
log.Functionf("RestartInprogress(%s) waiting for domain down",
status.Key())
}
}
publishDomainConfig(ctx, dc)
// Look for xen errors. Ignore if we are going down
if status.RestartInprogress != types.BringDown {
if ds.HasError() {
log.Errorf("Received error from domainmgr for %s: %s",
uuidStr, ds.Error)
status.SetErrorWithSourceAndDescription(ds.ErrorDescription, types.DomainStatus{})
changed = true
} else if status.IsErrorSource(types.DomainStatus{}) {
log.Functionf("Clearing domainmgr error %s", status.Error)
status.ClearErrorWithSource()
changed = true
}
} else {
if ds.HasError() {
log.Warnf("bringDown sees error from domainmgr for %s: %s",
uuidStr, ds.Error)
}
if status.IsErrorSource(types.DomainStatus{}) {
log.Functionf("Clearing domainmgr error %s", status.Error)
status.ClearErrorWithSource()
changed = true
}
}
if ds.State != status.State {
log.Functionf("Set State from DomainStatus from %d to %d",
status.State, ds.State)
status.State = ds.State
changed = true
}
// XXX compare with equal before setting changed?
status.IoAdapterList = ds.IoAdapterList
changed = true
if ds.State < types.BOOTING {
log.Functionf("Waiting for DomainStatus to BOOTING for %s",
uuidStr)
return changed
}
if ds.Pending() {
log.Functionf("Waiting for DomainStatus !Pending for %s", uuidStr)
return changed
}
log.Functionf("Done with DomainStatus for %s", uuidStr)
if !status.Activated {
status.Activated = true
status.ActivateInprogress = false
changed = true
}
// Are we doing a restart?
if status.RestartInprogress == types.BringUp {
if ds.Activated {
log.Functionf("RestartInprogress(%s) activated",
status.Key())
status.RestartInprogress = types.NotInprogress
status.State = types.RUNNING
changed = true
} else {
log.Functionf("RestartInprogress(%s) waiting for Activated",
status.Key())
}
}
if status.PurgeInprogress == types.BringUp {
if ds.Activated {
log.Functionf("PurgeInprogress(%s) activated",
status.Key())
status.PurgeInprogress = types.NotInprogress
status.State = types.RUNNING
changed = true
} else {
log.Functionf("PurgeInprogress(%s) waiting for Activated",
status.Key())
}
}
log.Functionf("doActivate done for %s", uuidStr)
return changed
}
// Check if VifUsed has changed and return true if it has
func updateVifUsed(statusPtr *types.AppInstanceStatus, ds types.DomainStatus) bool {
changed := false
for i := range statusPtr.AppNetAdapters {
adapterStatus := &statusPtr.AppNetAdapters[i]
net := ds.VifInfoByVif(adapterStatus.Vif)
if net != nil && net.VifUsed != adapterStatus.VifUsed {
log.Functionf("Found VifUsed %s for Vif %s", net.VifUsed, adapterStatus.Vif)
adapterStatus.VifUsed = net.VifUsed
changed = true
}
}
return changed
}
func purgeCmdDone(ctx *zedmanagerContext, config types.AppInstanceConfig,
status *types.AppInstanceStatus) bool {
log.Functionf("purgeCmdDone(%s) for %s", config.Key(), config.DisplayName)
changed := false
// Process the StorageStatusList items which are not in StorageConfigList
newVrs := []types.VolumeRefStatus{}
for i := range status.VolumeRefStatusList {
vrs := &status.VolumeRefStatusList[i]
vrc := getVolumeRefConfigFromAIConfig(&config, *vrs)
if vrc != nil {
newVrs = append(newVrs, *vrs)
continue
}
log.Functionf("purgeCmdDone(%s) unused volume ref %s "+
"generationCounter %d localGenerationCounter %d",
config.Key(), vrs.VolumeID, vrs.GenerationCounter, vrs.LocalGenerationCounter)
MaybeRemoveVolumeRefConfig(ctx, config.UUIDandVersion.UUID,
vrs.VolumeID, vrs.GenerationCounter, vrs.LocalGenerationCounter)
changed = true
}
log.Functionf("purgeCmdDone(%s) volumeRefStatus from %d to %d",
config.Key(), len(status.VolumeRefStatusList), len(newVrs))
status.VolumeRefStatusList = newVrs
// Update persistent counter
mapKey := types.UuidToNumKey{UUID: config.UUIDandVersion.UUID}
purgeCounter := int(config.PurgeCmd.Counter + config.LocalPurgeCmd.Counter)
err := ctx.appToPurgeCounterMap.Assign(mapKey, purgeCounter, false)
if err != nil {
log.Errorf("Failed to update persisted purge counter for app %s-%s: %v",
config.DisplayName, config.UUIDandVersion.UUID, err)
}
return changed
}
func doRemove(ctx *zedmanagerContext,
status *types.AppInstanceStatus, uninstall bool) (bool, bool) {
appInstID := status.UUIDandVersion.UUID
uuidStr := appInstID.String()
log.Functionf("doRemove for %s uninstall %t", appInstID, uninstall)
changed := false
done := false
c, done := doInactivate(ctx, appInstID, status)
changed = changed || c
if !done {
log.Functionf("doRemove waiting for inactivate for %s", uuidStr)
return changed, done
}
if !status.Activated {
c := doUnprepare(ctx, uuidStr, status)
changed = changed || c
if uninstall {
c, d := doUninstall(ctx, appInstID, status)
changed = changed || c
done = done || d
} else {
done = true
}
}
log.Functionf("doRemove done for %s", uuidStr)
return changed, done
}
func doInactivate(ctx *zedmanagerContext, appInstID uuid.UUID,
status *types.AppInstanceStatus) (bool, bool) {
uuidStr := appInstID.String()
log.Functionf("doInactivate for %s", uuidStr)
changed := false
done := false
uninstall := (status.PurgeInprogress != types.BringDown)
if uninstall {
log.Functionf("doInactivate uninstall for %s", uuidStr)
// First halt the domain by deleting
if lookupDomainConfig(ctx, uuidStr) != nil {
unpublishDomainConfig(ctx, uuidStr)
}
} else {
log.Functionf("doInactivate NOT uninstall for %s", uuidStr)
// First half the domain by clearing Activate
dc := lookupDomainConfig(ctx, uuidStr)
if dc == nil {
log.Warnf("doInactivate: No DomainConfig for %s",
uuidStr)
} else if dc.Activate {
log.Functionf("doInactivate: Clearing Activate for DomainConfig for %s",
uuidStr)
dc.Activate = false
publishDomainConfig(ctx, dc)
}
}
// Check if DomainStatus !Activated; update AppInstanceStatus if error
ds := lookupDomainStatus(ctx, uuidStr)
if ds != nil && (uninstall || ds.Activated) {
if uninstall {
log.Functionf("Waiting for DomainStatus removal for %s",
uuidStr)
} else {
log.Functionf("Waiting for DomainStatus !Activated for %s",
uuidStr)
}
// Update state
if status.DomainName != ds.DomainName {
status.DomainName = ds.DomainName
changed = true
}
if status.BootTime != ds.BootTime {
log.Functionf("Update boottime to %v for %s",
ds.BootTime.Format(time.RFC3339Nano),
status.Key())
status.BootTime = ds.BootTime
changed = true
}
c := updateVifUsed(status, *ds)
if c {
changed = true
}
// Look for errors
if ds.HasError() {
log.Errorf("Received error from domainmgr for %s: %s",
uuidStr, ds.Error)
status.SetErrorWithSource(ds.Error, types.DomainStatus{},