Skip to content

Commit

Permalink
playbook(memcache): support instances and instances_sequence
Browse files Browse the repository at this point in the history
Signed-off-by: EricWai <51902911+EricWai@users.noreply.github.com>
  • Loading branch information
EricWai committed Dec 18, 2023
2 parents d4d789a + a703f1e commit 50983f7
Show file tree
Hide file tree
Showing 17 changed files with 749 additions and 22 deletions.
4 changes: 3 additions & 1 deletion cli/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type CurveAdm struct {
clusterName string // current cluster name
clusterTopologyData string // cluster topology
clusterPoolData string // cluster pool
clusterType string // cluster type like develop, production, etc.
monitor storage.Monitor
}

Expand Down Expand Up @@ -195,7 +196,7 @@ func (curveadm *CurveAdm) init() error {
curveadm.clusterTopologyData = cluster.Topology
curveadm.clusterPoolData = cluster.Pool
curveadm.monitor = monitor

curveadm.clusterType = cluster.Type
return nil
}

Expand Down Expand Up @@ -276,6 +277,7 @@ func (curveadm *CurveAdm) ClusterUUId() string { return curveadm.c
func (curveadm *CurveAdm) ClusterName() string { return curveadm.clusterName }
func (curveadm *CurveAdm) ClusterTopologyData() string { return curveadm.clusterTopologyData }
func (curveadm *CurveAdm) ClusterPoolData() string { return curveadm.clusterPoolData }
func (curveadm *CurveAdm) ClusterType() string { return curveadm.clusterType }
func (curveadm *CurveAdm) Monitor() storage.Monitor { return curveadm.monitor }

func (curveadm *CurveAdm) GetHost(name string) (*hosts.HostConfig, error) {
Expand Down
30 changes: 26 additions & 4 deletions cli/command/cluster/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,18 @@ var (
CHECK_TOPOLOGY_PLAYBOOK_STEPS = []int{
playbook.CHECK_TOPOLOGY,
}
SUPPORTED_DEPLOY_TYPES = []string{
"production",
"test",
"develop",
}
)

type addOptions struct {
name string
descriotion string
description string
filename string
deployType string
}

func NewAddCommand(curveadm *cli.CurveAdm) *cobra.Command {
Expand All @@ -63,6 +69,9 @@ func NewAddCommand(curveadm *cli.CurveAdm) *cobra.Command {
Short: "Add cluster",
Args: utils.ExactArgs(1),
Example: ADD_EXAMPLE,
PreRunE: func(cmd *cobra.Command, args []string) error {
return checkAddOptions(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
options.name = args[0]
return runAdd(curveadm, options)
Expand All @@ -71,9 +80,9 @@ func NewAddCommand(curveadm *cli.CurveAdm) *cobra.Command {
}

flags := cmd.Flags()
flags.StringVarP(&options.descriotion, "description", "m", "", "Description for cluster")
flags.StringVarP(&options.description, "description", "m", "", "Description for cluster")
flags.StringVarP(&options.filename, "topology", "f", "", "Specify the path of topology file")

flags.StringVar(&options.deployType, "type", "develop", "Specify the type of cluster")
return cmd
}

Expand Down Expand Up @@ -134,6 +143,19 @@ func checkTopology(curveadm *cli.CurveAdm, data string, options addOptions) erro
return pb.Run()
}

func checkAddOptions(cmd *cobra.Command) error {
deployType, err := cmd.Flags().GetString("deploy-type")
if err != nil {
return err
}
for _, t := range SUPPORTED_DEPLOY_TYPES {
if deployType == t {
return nil
}
}
return errno.ERR_UNSUPPORT_DEPLOY_TYPE.F("deploy type: %s", deployType)
}

func runAdd(curveadm *cli.CurveAdm, options addOptions) error {
// 1) check wether cluster already exist
name := options.name
Expand Down Expand Up @@ -163,7 +185,7 @@ func runAdd(curveadm *cli.CurveAdm, options addOptions) error {

// 4) insert cluster (with topology) into database
uuid := uuid.NewString()
err = storage.InsertCluster(name, uuid, options.descriotion, data)
err = storage.InsertCluster(name, uuid, options.description, data, options.deployType)
if err != nil {
return errno.ERR_INSERT_CLUSTER_FAILED.E(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/cluster/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func importCluster(storage *storage.Storage, dbfile, name string) error {
}

// insert cluster
err = storage.InsertCluster(name, cluster.UUId, cluster.Description, cluster.Topology)
err = storage.InsertCluster(name, cluster.UUId, cluster.Description, cluster.Topology, cluster.Type)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions cli/command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ func displayDeployTitle(curveadm *cli.CurveAdm, dcs []*topology.DeployConfig) {
curveadm.WriteOutln("Cluster Name : %s", curveadm.ClusterName())
curveadm.WriteOutln("Cluster Kind : %s", dcs[0].GetKind())
curveadm.WriteOutln("Cluster Services: %s", serviceStats(dcs))
curveadm.WriteOutln("Cluster Type : %s", curveadm.ClusterType())
curveadm.WriteOutln("")
}

Expand Down
71 changes: 66 additions & 5 deletions cli/command/enter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ package command

import (
"github.com/opencurve/curveadm/cli/cli"
comm "github.com/opencurve/curveadm/internal/common"
"github.com/opencurve/curveadm/internal/configure/topology"
"github.com/opencurve/curveadm/internal/errno"
"github.com/opencurve/curveadm/internal/playbook"
"github.com/opencurve/curveadm/internal/task/task/common"
"github.com/opencurve/curveadm/internal/tools"
"github.com/opencurve/curveadm/internal/utils"
"github.com/spf13/cobra"
)

var (
ATTACH_LEADER_OR_RANDOM_CONTAINER = []int{playbook.ATTACH_LEADER_OR_RANDOM_CONTAINER}
)

type enterOptions struct {
id string
}
Expand All @@ -43,8 +50,11 @@ func NewEnterCommand(curveadm *cli.CurveAdm) *cobra.Command {
cmd := &cobra.Command{
Use: "enter ID",
Short: "Enter service container",
Args: utils.ExactArgs(1),
Args: utils.RequiresMaxArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return nil
}
options.id = args[0]
return curveadm.CheckId(options.id)
},
Expand All @@ -57,32 +67,83 @@ func NewEnterCommand(curveadm *cli.CurveAdm) *cobra.Command {
return cmd
}

func genLeaderOrRandomPlaybook(curveadm *cli.CurveAdm,
dcs []*topology.DeployConfig) (*playbook.Playbook, error) {
if len(dcs) == 0 {
return nil, errno.ERR_NO_SERVICES_MATCHED
}

steps := ATTACH_LEADER_OR_RANDOM_CONTAINER
pb := playbook.NewPlaybook(curveadm)
for _, step := range steps {
pb.AddStep(&playbook.PlaybookStep{
Type: step,
Configs: dcs,
ExecOptions: playbook.ExecOptions{
SilentSubBar: true,
SilentMainBar: true,
SkipError: true,
},
})
}
return pb, nil
}

func checkOrGetId(curveadm *cli.CurveAdm, dcs []*topology.DeployConfig, options enterOptions) (string, error) {
id := options.id
if id != "" {
return id, nil
}
pb, err := genLeaderOrRandomPlaybook(curveadm, dcs)
if err != nil {
return "", err
}
// run playground
err = pb.Run()
if err != nil {
return "", err
}
// get leader or random container id
value := curveadm.MemStorage().Get(comm.LEADER_OR_RANDOM_ID)
if value == nil {
return "", errno.ERR_NO_LEADER_OR_RANDOM_CONTAINER_FOUND
}
id = value.(common.Leader0rRandom).Id
return id, nil
}

func runEnter(curveadm *cli.CurveAdm, options enterOptions) error {
// 1) parse cluster topology
dcs, err := curveadm.ParseTopology()
if err != nil {
return err
}

// 2) filter service
// 2) check id options
id, err := checkOrGetId(curveadm, dcs, options)
if err != nil {
return err
}

// 3) filter service
dcs = curveadm.FilterDeployConfig(dcs, topology.FilterOption{
Id: options.id,
Id: id,
Role: "*",
Host: "*",
})
if len(dcs) == 0 {
return errno.ERR_NO_SERVICES_MATCHED
}

// 3) get container id
// 4) get container id
dc := dcs[0]
serviceId := curveadm.GetServiceId(dc.GetId())
containerId, err := curveadm.GetContainerId(serviceId)
if err != nil {
return err
}

// 4) attch remote container
// 5) attach remote container
home := dc.GetProjectLayout().ServiceRootDir
return tools.AttachRemoteContainer(curveadm, dc.GetHost(), containerId, home)
}
Loading

0 comments on commit 50983f7

Please sign in to comment.