Skip to content

Commit

Permalink
feature: sync workdir (labring#3903)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostloda authored and bxy4543 committed Oct 13, 2023
1 parent 7c7b010 commit bd5a6c3
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions pkg/apply/applydrivers/apply_drivers_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/labring/sealos/pkg/client-go/kubernetes"
"github.com/labring/sealos/pkg/clusterfile"
"github.com/labring/sealos/pkg/constants"
"github.com/labring/sealos/pkg/ssh"
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 @@ -79,20 +80,14 @@ type Applier struct {
}

func (c *Applier) Apply() error {
clusterPath := constants.Clusterfile(c.ClusterDesired.Name)
// clusterErr and appErr should not appear in the same time
var clusterErr, appErr error
// save cluster to file after apply
defer func() {
switch clusterErr.(type) {
case *processor.CheckError, *processor.PreProcessError:
return
}
logger.Debug("save objects into local: %s, objects: %v", clusterPath, c.getWriteBackObjects())
saveErr := yaml.MarshalFile(clusterPath, c.getWriteBackObjects()...)
if saveErr != nil {
logger.Error("failed to serialize into file: %s error, %s", clusterPath, saveErr)
}
c.applyAfter()
}()
c.initStatus()
if c.ClusterCurrent == nil || c.ClusterCurrent.CreationTimestamp.IsZero() {
Expand Down Expand Up @@ -273,3 +268,30 @@ func (c *Applier) deleteCluster() error {
logger.Info("succeeded in deleting current cluster")
return nil
}

func (c *Applier) syncWorkdir() {
workDir := constants.ClusterDir(c.ClusterDesired.Name)
sshCmd, err := ssh.NewExecCmdFromRoles(c.ClusterDesired, v2.MASTER)
if err != nil {
logger.Error("failed to sync workdir: %v", err)
}
err = sshCmd.RunCopy(workDir, workDir)
if err != nil {
logger.Error("failed to sync workdir: %s error, %v", workDir, err)
}
}

// 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()...)
if saveErr != nil {
logger.Error("failed to serialize into file: %s error, %s", clusterPath, saveErr)
}
}

func (c *Applier) applyAfter() {
c.saveClusterFile()
c.syncWorkdir()
}

0 comments on commit bd5a6c3

Please sign in to comment.