From bb710269e6103dcd82e2c4a07b5060b87d0eba5a Mon Sep 17 00:00:00 2001 From: sjcsjc123 <1401189096@qq.com> Date: Sun, 29 Oct 2023 17:33:18 +0800 Subject: [PATCH] fix bug with start etcd Signed-off-by: sjcsjc123 <1401189096@qq.com> --- pkg/imagedistributor/weed_mount.go | 92 ++++++++++++++++++++++++++ utils/weed/interface.go | 34 ++++++++-- utils/weed/interface_test.go | 1 + utils/weed/test/main.go | 103 +++++++++++++++++++++++++++++ utils/weed/weed.go | 6 ++ utils/weed/weed_master.go | 1 + 6 files changed, 231 insertions(+), 6 deletions(-) create mode 100644 pkg/imagedistributor/weed_mount.go create mode 100644 utils/weed/test/main.go diff --git a/pkg/imagedistributor/weed_mount.go b/pkg/imagedistributor/weed_mount.go new file mode 100644 index 00000000000..995c503aa6a --- /dev/null +++ b/pkg/imagedistributor/weed_mount.go @@ -0,0 +1,92 @@ +package imagedistributor + +import ( + "context" + "fmt" + "github.com/sealerio/sealer/pkg/define/options" + "github.com/sealerio/sealer/pkg/imageengine" + v1 "github.com/sealerio/sealer/types/api/v1" + "github.com/sealerio/sealer/utils/os/fs" + "github.com/sealerio/sealer/utils/weed" + "path/filepath" + "strings" +) + +type weedMounter struct { + imageEngine imageengine.Interface + weedClient weed.Deployer +} + +func (w *weedMounter) Mount(imageName string, platform v1.Platform, dest string) (string, string, string, error) { + mountDir := filepath.Join(dest, + strings.ReplaceAll(imageName, "/", "_"), + strings.Join([]string{platform.OS, platform.Architecture, platform.Variant}, "_")) + + imageID, err := w.imageEngine.Pull(&options.PullOptions{ + Quiet: false, + PullPolicy: "missing", + Image: imageName, + Platform: platform.ToString(), + }) + if err != nil { + return "", "", "", err + } + + if err := fs.FS.MkdirAll(filepath.Dir(mountDir)); err != nil { + return "", "", "", err + } + + id, err := w.imageEngine.CreateWorkingContainer(&options.BuildRootfsOptions{ + DestDir: mountDir, + ImageNameOrID: imageID, + }) + + if err != nil { + return "", "", "", err + } + + // Upload the mounted files to the WeedFS cluster + err = w.weedClient.UploadFile(context.Background(), mountDir) + if err != nil { + return "", "", "", err + } + + return mountDir, id, imageID, nil +} + +func (w *weedMounter) Umount(dir, containerID string) error { + // Download the files from WeedFS cluster + err := w.weedClient.DownloadFile(context.Background(), dir, dir) + if err != nil { + return err + } + + // Umount the image and remove the working container + err = w.imageEngine.RemoveContainer(&options.RemoveContainerOptions{ + ContainerNamesOrIDs: []string{containerID}, + }) + if err != nil { + return err + } + + // Remove the mounted files from the WeedFS cluster + err = w.weedClient.RemoveFile(context.Background(), dir) + if err != nil { + return err + } + + // Remove the local mount directory + if err := fs.FS.RemoveAll(dir); err != nil { + return fmt.Errorf("failed to remove mount dir %s: %v", dir, err) + } + + return nil +} + +func NewWeedMounter(imageEngine imageengine.Interface, config *weed.Config) Mounter { + deployer := weed.NewDeployer(config) + return &weedMounter{ + imageEngine: imageEngine, + weedClient: deployer, + } +} diff --git a/utils/weed/interface.go b/utils/weed/interface.go index ba78f30aab5..fa98b083276 100644 --- a/utils/weed/interface.go +++ b/utils/weed/interface.go @@ -172,12 +172,7 @@ func (d *deployer) downloadWeed() error { if err != nil { return err } - weedDirName := fmt.Sprintf("weed_%s_%s", runtime.GOOS, runtime.GOARCH) - err = exec.Command("tar", "-xvf", EtcdDestination, "-C", extractFolder).Run() - if err != nil { - return err - } - return os.Rename(path.Join(extractFolder, weedDirName+"/weed"), path.Join(d.config.BinDir, WeedBinName)) + return exec.Command("tar", "-xvf", WeedDestination, "-C", d.config.BinDir).Run() } func (d *deployer) etcdPrepare() error { @@ -458,4 +453,31 @@ func check(config *Config) { logrus.Error("volume ip list is empty") os.Exit(1) } + //check if exist tar file + _, err := os.Stat(WeedDestination) + if err == nil { + _ = os.RemoveAll(WeedDestination) + } + _, err = os.Stat(EtcdDestination) + if err == nil { + _ = os.RemoveAll(EtcdDestination) + } + + // test + _, err = os.Stat(config.BinDir) + if err == nil { + _ = os.RemoveAll(config.BinDir) + } + _, err = os.Stat(config.DataDir) + if err == nil { + _ = os.RemoveAll(config.DataDir) + } + _, err = os.Stat(config.LogDir) + if err == nil { + _ = os.RemoveAll(config.LogDir) + } + _, err = os.Stat(config.PidDir) + if err == nil { + _ = os.RemoveAll(config.PidDir) + } } diff --git a/utils/weed/interface_test.go b/utils/weed/interface_test.go index a1edc516632..6ea2b3a6ffd 100644 --- a/utils/weed/interface_test.go +++ b/utils/weed/interface_test.go @@ -30,6 +30,7 @@ func TestDeployer_CreateEtcdCluster1(t *testing.T) { DataDir: "./test/data3", LogDir: "./test/log3", MasterIP: []string{"127.0.0.1:1111", "127.0.0.1:2222", "127.0.0.1:3333"}, + VolumeIP: []string{"127.0.0.1:4444", "127.0.0.1:5555", "127.0.0.1:6666"}, PidDir: "./test/pid3", CurrentIP: "127.0.0.1", PeerPort: 3333, diff --git a/utils/weed/test/main.go b/utils/weed/test/main.go new file mode 100644 index 00000000000..6a948463c8e --- /dev/null +++ b/utils/weed/test/main.go @@ -0,0 +1,103 @@ +package main + +import ( + "context" + "github.com/sealerio/sealer/utils/weed" + "github.com/sealerio/sealer/version" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "os" +) + +func main() { + rootCmd := cobra.Command{ + Use: "weed", + Short: "A tool to build, share and run any distributed applications.", + } + rootCmd.AddCommand(startCmd()) + rootCmd.AddCommand(writeCmd()) + rootCmd.AddCommand(downloadFileCmd()) + if err := rootCmd.Execute(); err != nil { + logrus.Errorf("sealer-%s: %v", version.GetSingleVersion(), err) + os.Exit(1) + } + +} + +var config = &weed.Config{} + +func startCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "start", + Short: "start to run a weed cluster", + RunE: func(cmd *cobra.Command, args []string) error { + deployer := weed.NewDeployer(config) + err := deployer.CreateEtcdCluster(context.Background()) + if err != nil { + return err + } + err = deployer.CreateWeedCluster(context.Background()) + if err != nil { + return err + } + return nil + }, + } + cmd.Flags().StringSliceVar(&config.MasterIP, "master-ip", []string{}, "master ip list") + cmd.Flags().StringSliceVar(&config.VolumeIP, "volume-ip", []string{}, "volume ip list") + cmd.Flags().StringVar(&config.LogDir, "log-dir", "", "log dir") + cmd.Flags().StringVar(&config.DataDir, "data-dir", "", "data dir") + cmd.Flags().StringVar(&config.PidDir, "pid-dir", "", "pid dir") + cmd.Flags().StringVar(&config.BinDir, "bin-dir", "", "bin dir") + cmd.Flags().StringVar(&config.EtcdConfigPath, "etcd-config-path", "", "etcd config path") + cmd.Flags().StringVar(&config.CurrentIP, "current-ip", "", "current ip") + cmd.Flags().IntVar(&config.PeerPort, "peer-port", 0, "peer port") + cmd.Flags().IntVar(&config.ClientPort, "client-port", 0, "client port") + cmd.Flags().IntVar(&config.WeedMasterPort, "weed-master-port", 0, "weed master port") + cmd.Flags().IntVar(&config.WeedVolumePort, "weed-volume-port", 0, "weed volume port") + cmd.Flags().BoolVar(&config.NeedMoreLocalNode, "need-more-local-node", false, "need more local node") + cmd.Flags().StringVar(&config.WeedMasterDir, "weed-master-dir", "", "weed master dir") + cmd.Flags().StringVar(&config.WeedVolumeDir, "weed-volume-dir", "", "weed volume dir") + cmd.Flags().StringVar(&config.DefaultReplication, "default-replication", "", "default replication") + cmd.Flags().StringVar(&config.WeedLogDir, "weed-log-dir", "", "weed log dir") + return cmd +} + +var dir string + +func writeCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "write", + Short: "write data to weed cluster", + RunE: func(cmd *cobra.Command, args []string) error { + deployer := weed.NewDeployer(config) + err := deployer.UploadFile(context.Background(), dir) + if err != nil { + return err + } + return nil + }, + } + cmd.Flags().StringVar(&dir, "dir", "", "dir") + return cmd +} + +var out string + +func downloadFileCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "download", + Short: "download data from weed cluster", + RunE: func(cmd *cobra.Command, args []string) error { + deployer := weed.NewDeployer(config) + err := deployer.DownloadFile(context.Background(), dir, out) + if err != nil { + return err + } + return nil + }, + } + cmd.Flags().StringVar(&dir, "dir", "", "dir") + cmd.Flags().StringVar(&out, "out", "", "out") + return cmd +} diff --git a/utils/weed/weed.go b/utils/weed/weed.go index 43f62c0ab82..a5bee824027 100644 --- a/utils/weed/weed.go +++ b/utils/weed/weed.go @@ -52,6 +52,12 @@ func downloadFile(url string, dest string) error { } defer resp.Body.Close() + // check if the destination folder exists + _, err = os.Stat(dest) + if err == nil { + _ = os.RemoveAll(dest) + } + out, err := os.Create(dest) if err != nil { return err diff --git a/utils/weed/weed_master.go b/utils/weed/weed_master.go index bf89adb30d9..9dd76939240 100644 --- a/utils/weed/weed_master.go +++ b/utils/weed/weed_master.go @@ -112,6 +112,7 @@ func (m *master) Start(ctx context.Context, binary string) error { func (m *master) BuildArgs(ctx context.Context, params ...interface{}) []string { return []string{ + "master", "-ip " + m.ip, "-port " + params[0].(string), "-mdir " + params[1].(string),