From 3817f66c297826aa97db836eaf396c1732fb4f27 Mon Sep 17 00:00:00 2001 From: Shailesh Gothi Date: Thu, 13 May 2021 16:46:20 -0700 Subject: [PATCH] Fixed update verification Previously version before update was not stored, so it was hard to verify if update happened or not. This change stores the version and compares it with updated version to report approiate logs. --- updater/aws.go | 29 +++++++++++++++++------------ updater/main.go | 7 ++++++- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/updater/aws.go b/updater/aws.go index 6a4780d..8b808db 100644 --- a/updater/aws.go +++ b/updater/aws.go @@ -19,6 +19,7 @@ const ( type instance struct { instanceID string containerInstanceID string + bottlerocketVersion string } type checkOutput struct { @@ -112,6 +113,7 @@ func (u *updater) filterAvailableUpdates(bottlerocketInstances []instance) ([]in continue } if output.UpdateState == "Available" { + inst.bottlerocketVersion = output.ActivePartition.Image.Version candidates = append(candidates, inst) } } @@ -237,33 +239,36 @@ func (u *updater) updateInstance(inst instance) error { } // verifyUpdate verifies if instance was properly updated -func (u *updater) verifyUpdate(inst instance) error { +func (u *updater) verifyUpdate(inst instance) (bool, error) { log.Println("Verifying update by checking there is no new version available to update" + " and validate the active version") ec2IDs := []string{inst.instanceID} updateStatus, err := u.sendCommand(ec2IDs, "apiclient update check") if err != nil { - return fmt.Errorf("failed to send update check command : %v", err) + return false, fmt.Errorf("failed to send update check command: %v", err) } updateResult, err := u.getCommandResult(updateStatus, inst.instanceID) if err != nil { - return fmt.Errorf("failed to get check command output: %w", err) + return false, fmt.Errorf("failed to get check command output: %w", err) } output, err := parseCommandOutput(updateResult) if err != nil { - return fmt.Errorf("failed to parse command output %s, manual verification required: %w", string(updateResult), err) + return false, fmt.Errorf("failed to parse command output %s, manual verification required: %w", string(updateResult), err) } - if output.UpdateState == "Available" { - return fmt.Errorf(" instance did not update, manual update advised") - } - log.Printf("Instance %#q updated successfully", inst) - if output.ActivePartition.Image.Version != "" { - log.Printf("Instance %#q running Bottlerocket: %s", inst, output.ActivePartition.Image.Version) + updatedVersion := output.ActivePartition.Image.Version + if updatedVersion == inst.bottlerocketVersion { + log.Printf("Container instance %q did not update, its current "+ + "version %s and updated version %s are same", inst.containerInstanceID, inst.bottlerocketVersion, updatedVersion) + return false, nil + } else if output.UpdateState == "Available" { + log.Printf("Container instance %q was updated to version %q successfully, however another newer version was recently released;"+ + " Instance will be updated to newer version in next iteration.", inst.containerInstanceID, updatedVersion) + return true, nil } else { - log.Printf("Unable to verify active version. Manual verification of %#q required.", inst) + log.Printf("Container instance %q updated to version %q", inst.containerInstanceID, updatedVersion) } - return nil + return true, nil } func (u *updater) sendCommand(instanceIDs []string, ssmCommand string) (string, error) { diff --git a/updater/main.go b/updater/main.go index 7d537b2..f911809 100644 --- a/updater/main.go +++ b/updater/main.go @@ -114,10 +114,15 @@ func _main() error { return fmt.Errorf("instance %#q failed to re-activate after update: %w", i, activateErr) } - err = u.verifyUpdate(i) + ok, err := u.verifyUpdate(i) if err != nil { log.Printf("Failed to verify update for instance %#q: %v", i, err) } + if !ok { + log.Printf("Update failed for instance %#q", i) + } else { + log.Printf("Instance %#q updated successfully!", i) + } } return nil }