Skip to content

Commit

Permalink
feat: support start cluster cmd (#27)
Browse files Browse the repository at this point in the history
Signed-off-by: shilinlee <836160610@qq.com>
  • Loading branch information
shilinlee authored Dec 5, 2023
1 parent 484a77f commit 0f85b04
Show file tree
Hide file tree
Showing 35 changed files with 1,556 additions and 129 deletions.
17 changes: 17 additions & 0 deletions cmd/cluster/display.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cluster

import (
"github.com/openGemini/gemix/pkg/cluster/manager"
"github.com/spf13/cobra"
)

func shellCompGetClusterName(cm *manager.Manager, toComplete string) ([]string, cobra.ShellCompDirective) {
var result []string
//clusters, _ := cm.GetClusterList()
//for _, c := range clusters {
// if strings.HasPrefix(c.Name, toComplete) {
// result = append(result, c.Name)
// }
//}
return result, cobra.ShellCompDirectiveNoFileComp
}
1 change: 1 addition & 0 deletions cmd/cluster/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func init() {
installCmd(),
installCmd2(),
startCmd,
startCmd2(),
stopCmd,
uninstallCmd,
statusCmd,
Expand Down
4 changes: 1 addition & 3 deletions cmd/cluster/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ import (
"github.com/spf13/cobra"
)

var startOpts utils.StartOptions

// startCmd represents the start command
var startCmd = &cobra.Command{
Use: "start <cluster-name>",
Short: "Start an openGemini cluster",
Long: `Start an openGemini cluster based on configuration files and version numbers.`,
Long: `Start an openGemini cluster`,
Run: func(cmd *cobra.Command, args []string) {
var ops utils.ClusterOptions
var err error
Expand Down
60 changes: 60 additions & 0 deletions cmd/cluster/start2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2023 Huawei Cloud Computing Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cluster

import (
"github.com/openGemini/gemix/utils"
"github.com/spf13/cobra"
)

var startOpts utils.StartOptions

func startCmd2() *cobra.Command {
var (
initPasswd bool
)

var cmd = &cobra.Command{
Use: "start2 <cluster-name>",
Short: "Start an openGemini cluster",
Long: `Start an openGemini cluster`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return cmd.Help()
}

clusterName := args[0]
err := cm.StartCluster(clusterName, gOpt)
if err != nil {
return err
}

// TODO: init password
return nil
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
case 0:
return shellCompGetClusterName(cm, toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
},
}
cmd.Flags().BoolVar(&initPasswd, "init", false, "Initialize a secure root password for the database")
cmd.Flags().StringSliceVarP(&gOpt.Roles, "role", "R", nil, "Only start specified roles")
cmd.Flags().StringSliceVarP(&gOpt.Nodes, "node", "N", nil, "Only start specified nodes")
return cmd
}
41 changes: 41 additions & 0 deletions embed/templates/systemd/system.service.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[Unit]
Description={{.ServiceName}} service
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
{{- if .MemoryLimit}}
MemoryLimit={{.MemoryLimit}}
{{- end}}
{{- if .CPUQuota}}
CPUQuota={{.CPUQuota}}
{{- end}}
{{- if .IOReadBandwidthMax}}
IOReadBandwidthMax={{.IOReadBandwidthMax}}
{{- end}}
{{- if .IOWriteBandwidthMax}}
IOWriteBandwidthMax={{.IOWriteBandwidthMax}}
{{- end}}
{{- if .LimitCORE}}
LimitCORE={{.LimitCORE}}
{{- end}}
LimitNOFILE=1000000
LimitSTACK=10485760

{{- if .GrantCapNetRaw}}
AmbientCapabilities=CAP_NET_RAW
{{- end}}
User={{.User}}
ExecStart=/bin/bash -c '{{.DeployDir}}/scripts/run_{{.ServiceName}}.sh'

{{- if .Restart}}
Restart={{.Restart}}
{{else}}
Restart=always
{{end}}
RestartSec=15s
{{- if .DisableSendSigkill}}
SendSIGKILL=no
{{- end}}

[Install]
WantedBy=multi-user.target
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/charmbracelet/lipgloss v0.9.1
github.com/creasty/defaults v1.7.0
github.com/fatih/color v1.16.0
github.com/google/uuid v1.3.0
github.com/joomcode/errorx v1.1.1
github.com/olekukonko/tablewriter v0.0.5
github.com/pkg/errors v0.9.1
Expand All @@ -21,7 +22,9 @@ require (
go.uber.org/zap v1.26.0
golang.org/x/crypto v0.15.0
golang.org/x/mod v0.14.0
golang.org/x/sync v0.1.0
golang.org/x/term v0.14.0
golang.org/x/text v0.14.0
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -48,8 +51,6 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/goleak v1.2.1 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ github.com/dchest/bcrypt_pbkdf v0.0.0-20150205184540-83f37f9c154a h1:saTgr5tMLFn
github.com/dchest/bcrypt_pbkdf v0.0.0-20150205184540-83f37f9c154a/go.mod h1:Bw9BbhOJVNR+t0jCqx2GC6zv0TGBsShs56Y3gfSCvl0=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/joomcode/errorx v1.1.1 h1:/LFG/qSk1gUTuZjs+qlyOJEpcVjD9DXgBNFhdZkQrjY=
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/manager/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type hostInfo struct {
}

// getAllUniqueHosts gets all the instance
func getAllUniqueHosts(topo *spec.Specification) map[string]hostInfo {
func getAllUniqueHosts(topo spec.Topology) map[string]hostInfo {
// monitor
uniqueHosts := make(map[string]hostInfo) // host -> ssh-port, os, arch
topo.IterInstance(func(inst spec.Instance) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/manager/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

// buildDownloadCompTasks build download component tasks
func buildDownloadCompTasks(clusterVersion string, topo *spec.Specification) []*task.StepDisplay {
func buildDownloadCompTasks(clusterVersion string, topo spec.Topology) []*task.StepDisplay {
var tasks []*task.StepDisplay
uniqueTasks := make(map[string]struct{})

Expand All @@ -48,7 +48,7 @@ func buildDownloadCompTasks(clusterVersion string, topo *spec.Specification) []*
func buildInitConfigTasks(
m *Manager,
clustername string,
topo *spec.Specification,
topo spec.Topology,
base *spec.BaseMeta,
gOpt operator.Options,
) []*task.StepDisplay {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/manager/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package manager
import "github.com/openGemini/gemix/pkg/cluster/spec"

// checkConflict checks cluster conflict
func checkConflict(m *Manager, clusterName string, topo *spec.Specification) error {
func checkConflict(m *Manager, clusterName string, topo spec.Topology) error {
//clusterList, err := m.specManager.GetAllClusters()
//if err != nil {
// return err
Expand Down
8 changes: 4 additions & 4 deletions pkg/cluster/manager/install2.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,10 @@ func (m *Manager) Install(
}

// FIXME: remove me if you finish
//err = m.specManager.SaveMeta(clusterName, metadata)
//if err != nil {
// return err
//}
err = m.specManager.SaveMeta(clusterName, metadata)
if err != nil {
return err
}

hint := color.New(color.FgBlue).Sprintf("%s start %s", "gemix cluster", clusterName)
fmt.Printf("Cluster `%s` deployed successfully, you can start it with command: `%s`\n", clusterName, hint)
Expand Down
97 changes: 45 additions & 52 deletions pkg/cluster/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
"strings"

"github.com/fatih/color"
operator "github.com/openGemini/gemix/pkg/cluster/operation"
"github.com/openGemini/gemix/pkg/cluster/spec"
"github.com/openGemini/gemix/pkg/cluster/task"
"github.com/openGemini/gemix/pkg/gui"
"github.com/pkg/errors"
"go.uber.org/zap"
Expand All @@ -42,26 +44,26 @@ func NewManager(sysName string, specManager *spec.SpecManager, logger *zap.Logge
}
}

//func (m *Manager) meta(name string) (metadata spec.Metadata, err error) {
// exist, err := m.specManager.Exist(name)
// if err != nil {
// return nil, err
// }
//
// if !exist {
// return nil, perrs.Errorf("%s cluster `%s` not exists", m.sysName, name)
// }
//
// metadata = m.specManager.NewMetadata()
// err = m.specManager.Metadata(name, metadata)
// if err != nil {
// return metadata, err
// }
//
// return metadata, nil
//}
func (m *Manager) meta(name string) (metadata spec.Metadata, err error) {
exist, err := m.specManager.Exist(name)
if err != nil {
return nil, err
}

if !exist {
return nil, errors.Errorf("%s cluster `%s` not exists", m.sysName, name)
}

func (m *Manager) confirmTopology(clusterName, version string, topo *spec.Specification) error {
metadata = m.specManager.NewMetadata() // TODO: 没有可用信息
err = m.specManager.Metadata(name, metadata)
if err != nil {
return metadata, err
}

return metadata, nil
}

func (m *Manager) confirmTopology(clusterName, version string, topo spec.Topology) error {
fmt.Println("Please confirm your topology:")

cyan := color.New(color.FgCyan, color.Bold)
Expand Down Expand Up @@ -95,44 +97,35 @@ func (m *Manager) confirmTopology(clusterName, version string, topo *spec.Specif
return gui.PromptForConfirmOrAbortError("Do you want to continue? [y/N]: ")
}

//func (m *Manager) sshTaskBuilder(name string, topo spec.Topology, user string, gOpt operator.Options) (*task.Builder, error) {
// var p *tui.SSHConnectionProps = &tui.SSHConnectionProps{}
// if gOpt.SSHType != executor.SSHTypeNone && len(gOpt.SSHProxyHost) != 0 {
// var err error
// if p, err = tui.ReadIdentityFileOrPassword(gOpt.SSHProxyIdentity, gOpt.SSHProxyUsePassword); err != nil {
// return nil, err
// }
// }
//
// return task.NewBuilder(m.logger).
// SSHKeySet(
// m.specManager.Path(name, "ssh", "id_rsa"),
// m.specManager.Path(name, "ssh", "id_rsa.pub"),
// ).
// ClusterSSH(
// topo,
// user,
// gOpt.SSHTimeout,
// gOpt.OptTimeout,
// gOpt.SSHProxyHost,
// gOpt.SSHProxyPort,
// gOpt.SSHProxyUser,
// p.Password,
// p.IdentityFile,
// p.IdentityFilePassphrase,
// gOpt.SSHProxyTimeout,
// gOpt.SSHType,
// topo.BaseTopo().GlobalOptions.SSHType,
// ), nil
//}
func (m *Manager) sshTaskBuilder(name string, topo spec.Topology, user string, gOpt operator.Options) (*task.Builder, error) {
//var p *gui.SSHConnectionProps = &gui.SSHConnectionProps{}
//if gOpt.SSHType != executor.SSHTypeNone && len(gOpt.SSHProxyHost) != 0 {
// var err error
// if p, err = gui.ReadIdentityFileOrPassword(gOpt.SSHProxyIdentity, gOpt.SSHProxyUsePassword); err != nil {
// return nil, err
// }
//}

return task.NewBuilder(m.logger).
SSHKeySet(
m.specManager.Path(name, "ssh", "id_rsa"),
m.specManager.Path(name, "ssh", "id_rsa.pub"),
).
ClusterSSH(
topo,
user,
gOpt.SSHTimeout,
gOpt.OptTimeout,
), nil
}

// fillHost full host cpu-arch and kernel-name
func (m *Manager) fillHost(s *gui.SSHConnectionProps, topo *spec.Specification, user string) error {
func (m *Manager) fillHost(s *gui.SSHConnectionProps, topo spec.Topology, user string) error {
hostArchOrOS := map[string]string{}
topo.IterInstance(func(instance spec.Instance) {
insOS := instance.OS()
if insOS == "" {
insOS = topo.GlobalOptions.OS
insOS = topo.BaseTopo().GlobalOptions.OS
}
hostArchOrOS[instance.GetHost()] = insOS
})
Expand All @@ -144,7 +137,7 @@ func (m *Manager) fillHost(s *gui.SSHConnectionProps, topo *spec.Specification,
topo.IterInstance(func(instance spec.Instance) {
insArch := instance.Arch()
if insArch == "" {
insArch = topo.GlobalOptions.Arch
insArch = topo.BaseTopo().GlobalOptions.Arch
}
hostArchOrOS[instance.GetHost()] = insArch
})
Expand Down
Loading

0 comments on commit 0f85b04

Please sign in to comment.