mirror of
https://github.com/rancher/rke.git
synced 2025-08-28 03:10:56 +00:00
Move snapshot remove logic to rke-tools
This commit is contained in:
parent
72fd42b8d2
commit
2006a61712
@ -5,7 +5,6 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -500,31 +499,36 @@ func RestoreEtcdSnapshot(ctx context.Context, etcdHost *hosts.Host, prsMap map[s
|
|||||||
|
|
||||||
func RunEtcdSnapshotRemove(ctx context.Context, etcdHost *hosts.Host, prsMap map[string]v3.PrivateRegistry, etcdSnapshotImage string, name string, cleanupRestore bool, es v3.ETCDService) error {
|
func RunEtcdSnapshotRemove(ctx context.Context, etcdHost *hosts.Host, prsMap map[string]v3.PrivateRegistry, etcdSnapshotImage string, name string, cleanupRestore bool, es v3.ETCDService) error {
|
||||||
log.Infof(ctx, "[etcd] Removing snapshot [%s] from host [%s]", name, etcdHost.Address)
|
log.Infof(ctx, "[etcd] Removing snapshot [%s] from host [%s]", name, etcdHost.Address)
|
||||||
|
|
||||||
compressedPath := fmt.Sprintf("/backup/%s.%s", name, EtcdSnapshotCompressedExtension)
|
|
||||||
uncompressedPath := fmt.Sprintf("/backup/%s", name)
|
|
||||||
// Make sure we have a safe path to remove
|
|
||||||
for _, p := range []string{compressedPath, uncompressedPath} {
|
|
||||||
if safePath, err := filepath.Match("/backup/*", p); err != nil || !safePath {
|
|
||||||
return fmt.Errorf("invalid or malformed snapshot path [%s]: %v", p, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
imageCfg := &container.Config{
|
imageCfg := &container.Config{
|
||||||
Image: etcdSnapshotImage,
|
Image: etcdSnapshotImage,
|
||||||
Env: es.ExtraEnv,
|
Env: es.ExtraEnv,
|
||||||
|
Cmd: []string{
|
||||||
|
"/opt/rke-tools/rke-etcd-backup",
|
||||||
|
"etcd-backup",
|
||||||
|
"delete",
|
||||||
|
"--name", name,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
if cleanupRestore {
|
if cleanupRestore {
|
||||||
// Since we have to support compressed and uncompressed versions of snapshots.
|
imageCfg.Cmd = append(imageCfg.Cmd, "--cleanup")
|
||||||
// We can't remove the uncompressed snapshot unless we are sure the compressed
|
}
|
||||||
// is there, hence the complex check. The || true is to get a 0 exist status even if -f is false.
|
if es.BackupConfig != nil && es.BackupConfig.S3BackupConfig != nil {
|
||||||
imageCfg.Cmd = []string{
|
s3cmd := []string{
|
||||||
"sh", "-c", fmt.Sprintf("[ -f %s ] && rm -f %s || true", compressedPath, uncompressedPath),
|
"--s3-backup",
|
||||||
|
"--s3-endpoint=" + es.BackupConfig.S3BackupConfig.Endpoint,
|
||||||
|
"--s3-accessKey=" + es.BackupConfig.S3BackupConfig.AccessKey,
|
||||||
|
"--s3-secretKey=" + es.BackupConfig.S3BackupConfig.SecretKey,
|
||||||
|
"--s3-bucketName=" + es.BackupConfig.S3BackupConfig.BucketName,
|
||||||
|
"--s3-region=" + es.BackupConfig.S3BackupConfig.Region,
|
||||||
}
|
}
|
||||||
} else {
|
if es.BackupConfig.S3BackupConfig.CustomCA != "" {
|
||||||
imageCfg.Cmd = []string{
|
caStr := base64.StdEncoding.EncodeToString([]byte(es.BackupConfig.S3BackupConfig.CustomCA))
|
||||||
"sh", "-c", fmt.Sprintf("rm -f %s %s", compressedPath, uncompressedPath),
|
s3cmd = append(s3cmd, "--s3-endpoint-ca="+caStr)
|
||||||
}
|
}
|
||||||
|
if es.BackupConfig.S3BackupConfig.Folder != "" {
|
||||||
|
s3cmd = append(s3cmd, "--s3-folder="+es.BackupConfig.S3BackupConfig.Folder)
|
||||||
|
}
|
||||||
|
imageCfg.Cmd = append(imageCfg.Cmd, s3cmd...)
|
||||||
}
|
}
|
||||||
|
|
||||||
hostCfg := &container.HostConfig{
|
hostCfg := &container.HostConfig{
|
||||||
@ -533,7 +537,6 @@ func RunEtcdSnapshotRemove(ctx context.Context, etcdHost *hosts.Host, prsMap map
|
|||||||
},
|
},
|
||||||
RestartPolicy: container.RestartPolicy{Name: "no"},
|
RestartPolicy: container.RestartPolicy{Name: "no"},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := docker.DoRemoveContainer(ctx, etcdHost.DClient, EtcdSnapshotRemoveContainerName, etcdHost.Address); err != nil {
|
if err := docker.DoRemoveContainer(ctx, etcdHost.DClient, EtcdSnapshotRemoveContainerName, etcdHost.Address); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user