-
Notifications
You must be signed in to change notification settings - Fork 0
FEATURE: Add zk start, stop, status, list, delete commands #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
f1v3-dev
wants to merge
1
commit into
develop
Choose a base branch
from
f1v3/zk-command
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,17 @@ | ||
| package zk | ||
|
|
||
| import "github.com/spf13/cobra" | ||
| import ( | ||
| "github.com/jam2in/arcusctl/internal/zk" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var deleteCmd = &cobra.Command{ | ||
| Use: "delete <ensemble-name>", | ||
| Short: "Delete a ZooKeeper ensemble", | ||
| Args: cobra.ExactArgs(1), | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| // TODO: delete ๊ตฌํ | ||
| if err := zk.Delete(args[0]); err != nil { | ||
| panic(err) | ||
| } | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,17 @@ | ||
| package zk | ||
|
|
||
| import "github.com/spf13/cobra" | ||
| import ( | ||
| "github.com/jam2in/arcusctl/internal/zk" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var listCmd = &cobra.Command{ | ||
| Use: "list", | ||
| Short: "List ZooKeeper ensembles", | ||
| Args: cobra.NoArgs, | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| // TODO: list ๊ตฌํ | ||
| if err := zk.List(); err != nil { | ||
| panic(err) | ||
| } | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,17 @@ | ||
| package zk | ||
|
|
||
| import "github.com/spf13/cobra" | ||
| import ( | ||
| "github.com/jam2in/arcusctl/internal/zk" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var statusCmd = &cobra.Command{ | ||
| Use: "status <ensemble-name>", | ||
| Short: "Show status of a ZooKeeper ensemble", | ||
| Args: cobra.ExactArgs(1), | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| // TODO: status ๊ตฌํ | ||
| if err := zk.Status(args[0]); err != nil { | ||
| panic(err) | ||
| } | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package zk | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/jam2in/arcusctl/internal" | ||
| "github.com/jam2in/arcusctl/internal/ssh" | ||
| "github.com/jam2in/arcusctl/internal/store" | ||
| "github.com/jam2in/arcusctl/internal/topology" | ||
| ) | ||
|
|
||
| func Delete(ensembleName string) error { | ||
| _, topo, err := loadEnsemble(ensembleName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := verifyTopology(topo); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := verifyAllStopped(topo); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fmt.Printf("This will remove ZooKeeper ensemble %q from all servers.\n", ensembleName) | ||
| if !internal.Confirm("Are you sure you want to proceed? (y/N): ") { | ||
| fmt.Println("Aborted.") | ||
| return nil | ||
| } | ||
|
|
||
| hostsMap := groupServersByHost(topo.Servers) | ||
| for host, servers := range hostsMap { | ||
| fmt.Printf("Removing files on %s...\n", host) | ||
| if err := removeHostFiles(host, servers, topo); err != nil { | ||
| return fmt.Errorf("remove files on %s: %w", host, err) | ||
| } | ||
| } | ||
|
|
||
| if err := store.DeleteZK(ensembleName); err != nil { | ||
| return fmt.Errorf("delete metadata: %w", err) | ||
| } | ||
|
|
||
| fmt.Printf("ZooKeeper ensemble %q deleted.\n", ensembleName) | ||
| return nil | ||
| } | ||
|
|
||
| func verifyTopology(topo *topology.ZKTopology) error { | ||
| for _, server := range topo.Servers { | ||
| confDir := fmt.Sprintf("%s/conf_myid_%d", topo.Path, server.MyID) | ||
| if err := ssh.Run(server.Host(), fmt.Sprintf("test -d %s", confDir)); err != nil { | ||
| return fmt.Errorf("topology mismatch: %s not found on %s", confDir, server.Host()) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func verifyAllStopped(topo *topology.ZKTopology) error { | ||
| for _, server := range topo.Servers { | ||
| confPath := zkConfigPath(topo.Path, server.MyID) | ||
| cmd := fmt.Sprintf("pgrep -f 'QuorumPeerMain.*%s' > /dev/null 2>&1", confPath) | ||
| if err := ssh.Run(server.Host(), cmd); err == nil { | ||
| return fmt.Errorf("server %s (myid=%d) is still running. stop the ensemble before delete", | ||
| server.Host(), server.MyID) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func groupServersByHost(servers []topology.ZKServer) map[string][]topology.ZKServer { | ||
| hosts := map[string][]topology.ZKServer{} | ||
| for _, server := range servers { | ||
| hosts[server.Host()] = append(hosts[server.Host()], server) | ||
| } | ||
| return hosts | ||
| } | ||
|
|
||
| func removeHostFiles(host string, servers []topology.ZKServer, topo *topology.ZKTopology) error { | ||
| paths := []string{topo.Path} | ||
| for _, server := range servers { | ||
| paths = append(paths, fmt.Sprintf("%s/zk%d", server.Config.DataDir, server.MyID)) | ||
| } | ||
|
|
||
| cmd := "rm -rf " + strings.Join(paths, " ") | ||
| return ssh.Run(host, cmd) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package zk | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/jam2in/arcusctl/internal/store" | ||
| "github.com/jam2in/arcusctl/internal/topology" | ||
| ) | ||
|
|
||
| func loadEnsemble(name string) (*store.ZKMeta, *topology.ZKTopology, error) { | ||
| meta, err := store.LoadZKMeta(name) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("load ZooKeeper metadata %q: %w", name, err) | ||
| } | ||
|
|
||
| topo, err := store.LoadZKTopology(name) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("load ZooKeeper topology %q: %w", name, err) | ||
| } | ||
|
|
||
| if topo.Name != name { | ||
| return nil, nil, fmt.Errorf("ZooKeeper topology name mismatch: got %q, want %q", topo.Name, name) | ||
| } | ||
|
|
||
| mergeServerConfigs(topo) | ||
|
|
||
| if err := topo.Validate(); err != nil { | ||
| return nil, nil, fmt.Errorf("invalid ZooKeeper topology %q: %w", name, err) | ||
| } | ||
|
|
||
| return meta, topo, nil | ||
| } | ||
|
|
||
| func mergeServerConfigs(topo *topology.ZKTopology) { | ||
| for i := range topo.Servers { | ||
| merged := mergeConfig(topo.GlobalConfig, topo.Servers[i].Config) | ||
| topo.Servers[i].Config = &merged | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package zk | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "text/tabwriter" | ||
|
|
||
| "github.com/jam2in/arcusctl/internal/store" | ||
| ) | ||
|
|
||
| func List() error { | ||
| names, err := store.ListZK() | ||
| if err != nil { | ||
| return fmt.Errorf("list ZooKeeper ensembles: %w", err) | ||
| } | ||
|
|
||
| if len(names) == 0 { | ||
| fmt.Println("No ZooKeeper ensemble found.") | ||
| return nil | ||
| } | ||
|
|
||
| w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) | ||
| fmt.Fprintln(w, "NAME\tVERSION\tDEPLOYED AT") | ||
| for _, name := range names { | ||
| meta, err := store.LoadZKMeta(name) | ||
| if err != nil { | ||
| fmt.Fprintf(w, "%s\t<error>\t%v\n", name, err) | ||
| continue | ||
| } | ||
| fmt.Fprintf(w, "%s\t%s\t%s\n", meta.Name, meta.Version, meta.DeployedAt.Format("2006-01-02 15:04:05")) | ||
| } | ||
| return w.Flush() | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด ํจ์๋ ํ์ผ ์ฝ์ด์์ ๊ฐ์ฒดํํ๋ ๊ฒ์ด๋ผ store ํจํค์ง์์ ์ ๊ณตํ๋ ๊ฒ ๋์ ๊ฒ ๊ฐ์๋ฐ ์ด๋ค๊ฐ์?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ผ์ ์ฝ์ด์์ ๊ฐ์ฒดํํ๋ ํจ์(
store.LoadZkMeta,store.LoadZkTopology)์ ํธ์ถํ๊ณ ์๋ ํํ์ด๊ธด ํ์ง๋ง, ZooKeeper์ ๊ด๋ จ๋ ์ค์ ๋ณํฉ(์ ์ญ ์ค์ + ๊ฐ๋ณ ์ค์ )๊ณผ ๊ฒ์ฆ ๊ฐ์ zk ๋๋ฉ์ธ ๊ท์น๋ ๋ด๊ณ ์์ต๋๋ค.์ด๋ฐ ๋๋ฉ์ธ ๋ก์ง์ ์ ์์ค
store๋ณด๋คzk์ ๋๋ ํ์ฌ ๊ตฌ์กฐ๊ฐ ๋ ๊ด์ฐฎ๋ค๊ณ ๋ณด์ฌ์ง๋๋ค.