Skip to content

Commit

Permalink
fix bug with start etcd
Browse files Browse the repository at this point in the history
Signed-off-by: sjcsjc123 <1401189096@qq.com>
  • Loading branch information
sjcsjc123 committed Oct 29, 2023
1 parent b650285 commit c676fa5
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 6 deletions.
34 changes: 28 additions & 6 deletions utils/weed/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
1 change: 1 addition & 0 deletions utils/weed/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
103 changes: 103 additions & 0 deletions utils/weed/test/main.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions utils/weed/weed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions utils/weed/weed_master.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit c676fa5

Please sign in to comment.