-
Notifications
You must be signed in to change notification settings - Fork 228
USHIFT-1083 Backup data if system was healthy #1874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6f41471
simple backup on boot
pmtk fe53949
move prerun to separate file
pmtk adc4a1c
use boot_id for backup name
pmtk f8400fe
rename green red scripts
pmtk bd76391
green/red: check if root
pmtk f746089
add var-lib dirs to spec
pmtk 5483c25
reorganize prerun implementation
pmtk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -ex | ||
|
|
||
| HEALTH=healthy | ||
|
|
||
| SCRIPT_NAME=$(basename "$0") | ||
| if [ "$(id -u)" -ne 0 ] ; then | ||
| echo "The '${SCRIPT_NAME}' script must be run with the 'root' user privileges" | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [ ! -f /run/ostree-booted ]; then | ||
| echo "System is not booted with ostree" | ||
| exit 0 | ||
| fi | ||
|
|
||
| mkdir -p /var/lib/microshift-backups | ||
|
|
||
| boot=$(tr -d '-' < /proc/sys/kernel/random/boot_id) | ||
| deploy=$(rpm-ostree status --booted --jsonpath='$.deployments[0].id' | jq -r '.[0]') | ||
| jq \ | ||
| --null-input \ | ||
| --arg health "${HEALTH}" \ | ||
| --arg deploy "${deploy}" \ | ||
| --arg boot "${boot}" \ | ||
| '{ "health": $health, "deployment_id": $deploy, "boot_id": $boot }' > /var/lib/microshift-backups/health.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -ex | ||
|
|
||
| HEALTH=unhealthy | ||
|
|
||
| SCRIPT_NAME=$(basename "$0") | ||
| if [ "$(id -u)" -ne 0 ] ; then | ||
| echo "The '${SCRIPT_NAME}' script must be run with the 'root' user privileges" | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [ ! -f /run/ostree-booted ]; then | ||
| echo "System is not booted with ostree" | ||
| exit 0 | ||
| fi | ||
|
|
||
| mkdir -p /var/lib/microshift-backups | ||
|
|
||
| boot=$(tr -d '-' < /proc/sys/kernel/random/boot_id) | ||
| deploy=$(rpm-ostree status --booted --jsonpath='$.deployments[0].id' | jq -r '.[0]') | ||
| jq \ | ||
| --null-input \ | ||
| --arg health "${HEALTH}" \ | ||
| --arg deploy "${deploy}" \ | ||
| --arg boot "${boot}" \ | ||
| '{ "health": $health, "deployment_id": $deploy, "boot_id": $boot }' > /var/lib/microshift-backups/health.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| package prerun | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/openshift/microshift/pkg/admin/data" | ||
| "github.com/openshift/microshift/pkg/config" | ||
| "github.com/openshift/microshift/pkg/util" | ||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| var ( | ||
| errHealthFileDoesNotExist = errors.New("health file does not exist") | ||
| ) | ||
|
|
||
| type HealthInfo struct { | ||
| Health string `json:"health"` | ||
| DeploymentID string `json:"deployment_id"` | ||
| BootID string `json:"boot_id"` | ||
| } | ||
|
|
||
| func (hi *HealthInfo) BackupName() data.BackupName { | ||
| return data.BackupName(fmt.Sprintf("%s_%s", hi.DeploymentID, hi.BootID)) | ||
| } | ||
|
|
||
| func (hi *HealthInfo) IsHealthy() bool { | ||
| return hi.Health == "healthy" | ||
| } | ||
|
|
||
| func Perform() error { | ||
| health, err := getHealthInfo() | ||
| if err != nil { | ||
| if errors.Is(err, errHealthFileDoesNotExist) { | ||
| klog.InfoS("Health file does not exist - skipping backup") | ||
| return nil | ||
| } | ||
| klog.ErrorS(err, "Failed to load health from disk") | ||
| return err | ||
| } | ||
| klog.InfoS("Loaded health info from the disk", "health", health) | ||
|
|
||
| if isCurr, err := containsCurrentBootID(health.BootID); err != nil { | ||
| return err | ||
| } else if isCurr { | ||
| klog.InfoS("Health file contains current boot - skipping backup") | ||
| return nil | ||
| } | ||
|
|
||
| if !health.IsHealthy() { | ||
| klog.InfoS("System was not healthy - skipping backup") | ||
| return nil | ||
| } | ||
|
|
||
| dataManager, err := data.NewManager(config.BackupsDir) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| existingBackups, err := dataManager.GetBackupList() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // get list of already existing backups for deployment ID persisted in health file | ||
| // after creating backup, the list will be used to remove older backups | ||
| // (so only the most recent one for specific deployment is kept) | ||
| backupsForDeployment := getExistingBackupsForTheDeployment(existingBackups, health.DeploymentID) | ||
|
|
||
| newBackupName := health.BackupName() | ||
| if backupAlreadyExists(backupsForDeployment, newBackupName) { | ||
| klog.InfoS("Backup already exists", "name", newBackupName) | ||
| return nil | ||
| } | ||
|
|
||
| if err := dataManager.Backup(newBackupName); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| removeOldBackups(dataManager, backupsForDeployment) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func containsCurrentBootID(id string) (bool, error) { | ||
| path := "/proc/sys/kernel/random/boot_id" | ||
| content, err := os.ReadFile(path) | ||
| if err != nil { | ||
| klog.ErrorS(err, "Failed to read file", "path", path) | ||
| return false, fmt.Errorf("reading file %s failed: %w", path, err) | ||
| } | ||
| currentBootID := strings.ReplaceAll(strings.TrimSpace(string(content)), "-", "") | ||
| klog.InfoS("Comparing boot IDs", "current", currentBootID, "toCompare", id) | ||
| return id == currentBootID, nil | ||
| } | ||
|
|
||
| func getHealthInfo() (*HealthInfo, error) { | ||
| path := "/var/lib/microshift-backups/health.json" | ||
| if exists, err := util.PathExists(path); err != nil { | ||
| return nil, err | ||
| } else if !exists { | ||
| return nil, errHealthFileDoesNotExist | ||
| } | ||
|
|
||
| content, err := os.ReadFile(path) | ||
| if err != nil { | ||
| klog.ErrorS(err, "Failed to read file", "path", path) | ||
| return nil, err | ||
| } | ||
|
|
||
| health := &HealthInfo{} | ||
| if err := json.Unmarshal(content, &health); err != nil { | ||
| klog.ErrorS(err, "Failed to unmarshal file to json", "content", string(content)) | ||
| return nil, err | ||
| } | ||
| return health, nil | ||
| } | ||
|
|
||
| func getExistingBackupsForTheDeployment(existingBackups []data.BackupName, deployID string) []data.BackupName { | ||
| existingDeploymentBackups := make([]data.BackupName, 0) | ||
|
|
||
| for _, existingBackup := range existingBackups { | ||
| if strings.HasPrefix(string(existingBackup), deployID) { | ||
| existingDeploymentBackups = append(existingDeploymentBackups, existingBackup) | ||
| } | ||
| } | ||
|
|
||
| return existingDeploymentBackups | ||
| } | ||
|
|
||
| func backupAlreadyExists(existingBackups []data.BackupName, name data.BackupName) bool { | ||
| for _, backup := range existingBackups { | ||
| if backup == name { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func removeOldBackups(dataManager data.Manager, backups []data.BackupName) { | ||
| for _, b := range backups { | ||
| klog.InfoS("Removing older backup", "name", b) | ||
| if err := dataManager.RemoveBackup(b); err != nil { | ||
| klog.ErrorS(err, "Failed to remove backup", "name", b) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.