Skip to content

Commit

Permalink
refactor env keys (#4066) (#4076)
Browse files Browse the repository at this point in the history
* feat: add SYNC_WORKDIR env key



* refactor: remove SEALOS_SCP_CHECKSUM env key



---------

Signed-off-by: fengxsong <[email protected]>
Co-authored-by: fengxsong <[email protected]>
  • Loading branch information
sealos-ci-robot and fengxsong authored Oct 11, 2023
1 parent 4242e7c commit b8affb0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 56 deletions.
18 changes: 16 additions & 2 deletions pkg/apply/applydrivers/apply_drivers_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"os"
"strconv"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/version"
Expand All @@ -31,6 +32,7 @@ import (
"github.com/labring/sealos/pkg/constants"
"github.com/labring/sealos/pkg/exec"
"github.com/labring/sealos/pkg/ssh"
"github.com/labring/sealos/pkg/system"
v2 "github.com/labring/sealos/pkg/types/v1beta1"
"github.com/labring/sealos/pkg/utils/confirm"
"github.com/labring/sealos/pkg/utils/iputils"
Expand Down Expand Up @@ -273,6 +275,12 @@ func (c *Applier) deleteCluster() error {
}

func (c *Applier) syncWorkdir() {
if v, _ := system.Get(system.SyncWorkDirEnvKey); v != "" {
vb, _ := strconv.ParseBool(v)
if !vb {
return
}
}
workDir := constants.ClusterDir(c.ClusterDesired.Name)
logger.Debug("sync workdir: %s", workDir)
ipList := c.ClusterDesired.GetMasterIPAndPortList()
Expand All @@ -295,8 +303,14 @@ func (c *Applier) syncWorkdir() {
// save cluster to file after apply
func (c *Applier) saveClusterFile() {
clusterPath := constants.Clusterfile(c.ClusterDesired.Name)
logger.Debug("save objects into local: %s, objects: %v", clusterPath, c.getWriteBackObjects())
saveErr := yaml.MarshalFile(clusterPath, c.getWriteBackObjects()...)
objects := c.getWriteBackObjects()
if logger.IsDebugMode() {
out, err := yaml.MarshalConfigs(objects...)
if err == nil {
logger.Debug("save objects into local: %s, objects: %s", clusterPath, string(out))
}
}
saveErr := yaml.MarshalFile(clusterPath, objects...)
if saveErr != nil {
logger.Error("failed to serialize into file: %s error, %s", clusterPath, saveErr)
}
Expand Down
49 changes: 1 addition & 48 deletions pkg/ssh/scp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@ import (
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/labring/sealos/pkg/system"

"github.com/pkg/sftp"
"github.com/schollz/progressbar/v3"
"golang.org/x/crypto/ssh"

"github.com/labring/sealos/pkg/utils/file"
"github.com/labring/sealos/pkg/utils/hash"
"github.com/labring/sealos/pkg/utils/logger"
"github.com/labring/sealos/pkg/utils/progress"
)
Expand Down Expand Up @@ -198,20 +194,6 @@ func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progre
}
}
} else {
fn := func(host string, name string) bool {
exists, err := checkIfRemoteFileExists(client, name)
if err != nil {
logger.Error("failed to detect remote file exists: %v", err)
}
return exists
}
if isCheckFileMD5() && fn(host, dest) {
rfp, _ := client.Stat(dest)
if lfp.Size() == rfp.Size() && hash.FileDigest(src) == c.RemoteSha256Sum(host, dest) {
logger.Debug("remote dst %s already exists and is the latest version, skip copying process", dest)
return nil
}
}
lf, err := os.Open(filepath.Clean(src))
if err != nil {
return fmt.Errorf("failed to open: %v", err)
Expand Down Expand Up @@ -240,37 +222,8 @@ func (c *Client) doCopy(client *sftp.Client, host, src, dest string, epu *progre
if err = client.PosixRename(destTmp, dest); err != nil {
return fmt.Errorf("failed to rename %s to %s: %v", destTmp, dest, err)
}
if isCheckFileMD5() {
dh := c.RemoteSha256Sum(host, dest)
if dh == "" {
// when ssh connection failed, remote sha256 is default to "", so ignore it.
return nil
}
sh := hash.FileDigest(src)
if sh != dh {
return fmt.Errorf("sha256 sum not match %s(%s) != %s(%s), maybe network corruption?", src, sh, dest, dh)
}
}

_ = epu.Add(1)
}
return nil
}

func checkIfRemoteFileExists(client *sftp.Client, fp string) (bool, error) {
_, err := client.Stat(fp)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}

func isCheckFileMD5() bool {
if v, err := system.Get(system.ScpChecksumConfigKey); err == nil {
boolVal, _ := strconv.ParseBool(v)
return boolVal
}
return true
}
12 changes: 6 additions & 6 deletions pkg/system/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ var configOptions = []ConfigOption{
Description: `the log level to be used in buildah modules, either "trace", "debug", "info", "warn", "error", "fatal", or "panic".`,
OSEnv: BuildahLogLevelConfigKey,
},
{
Key: ScpChecksumConfigKey,
Description: "whether to check the md5sum value is consistent during the copy process.",
DefaultValue: "false",
},
{
Key: ContainerStorageConfEnvKey,
Description: "path of container storage config file, setting this env will override the default location",
OSEnv: ContainerStorageConfEnvKey,
},
{
Key: SyncWorkDirEnvKey,
Description: "whether to sync runtime root dir to all master nodes for backup purpose",
DefaultValue: "true",
},
}

const (
Expand All @@ -101,7 +101,7 @@ const (
BuildahFormatConfigKey = "BUILDAH_FORMAT"
BuildahLogLevelConfigKey = "BUILDAH_LOG_LEVEL"
ContainerStorageConfEnvKey = "CONTAINERS_STORAGE_CONF"
ScpChecksumConfigKey = "SCP_CHECKSUM"
SyncWorkDirEnvKey = "SYNC_WORKDIR"
)

func (*envSystemConfig) getValueOrDefault(key string) (*ConfigOption, error) {
Expand Down

0 comments on commit b8affb0

Please sign in to comment.