@@ -26,13 +26,20 @@ package command
26
26
27
27
import (
28
28
"github.com/opencurve/curveadm/cli/cli"
29
+ comm "github.com/opencurve/curveadm/internal/common"
29
30
"github.com/opencurve/curveadm/internal/configure/topology"
30
31
"github.com/opencurve/curveadm/internal/errno"
32
+ "github.com/opencurve/curveadm/internal/playbook"
33
+ "github.com/opencurve/curveadm/internal/task/task/common"
31
34
"github.com/opencurve/curveadm/internal/tools"
32
35
"github.com/opencurve/curveadm/internal/utils"
33
36
"github.com/spf13/cobra"
34
37
)
35
38
39
+ var (
40
+ ATTACH_LEADER_OR_RANDOM_CONTAINER = []int {playbook .ATTACH_LEADER_OR_RANDOM_CONTAINER }
41
+ )
42
+
36
43
type enterOptions struct {
37
44
id string
38
45
}
@@ -43,8 +50,11 @@ func NewEnterCommand(curveadm *cli.CurveAdm) *cobra.Command {
43
50
cmd := & cobra.Command {
44
51
Use : "enter ID" ,
45
52
Short : "Enter service container" ,
46
- Args : utils .ExactArgs (1 ),
53
+ Args : utils .RequiresMaxArgs (1 ),
47
54
PreRunE : func (cmd * cobra.Command , args []string ) error {
55
+ if len (args ) == 0 {
56
+ return nil
57
+ }
48
58
options .id = args [0 ]
49
59
return curveadm .CheckId (options .id )
50
60
},
@@ -57,32 +67,83 @@ func NewEnterCommand(curveadm *cli.CurveAdm) *cobra.Command {
57
67
return cmd
58
68
}
59
69
70
+ func genLeaderOrRandomPlaybook (curveadm * cli.CurveAdm ,
71
+ dcs []* topology.DeployConfig ) (* playbook.Playbook , error ) {
72
+ if len (dcs ) == 0 {
73
+ return nil , errno .ERR_NO_SERVICES_MATCHED
74
+ }
75
+
76
+ steps := ATTACH_LEADER_OR_RANDOM_CONTAINER
77
+ pb := playbook .NewPlaybook (curveadm )
78
+ for _ , step := range steps {
79
+ pb .AddStep (& playbook.PlaybookStep {
80
+ Type : step ,
81
+ Configs : dcs ,
82
+ ExecOptions : playbook.ExecOptions {
83
+ SilentSubBar : true ,
84
+ SilentMainBar : true ,
85
+ SkipError : true ,
86
+ },
87
+ })
88
+ }
89
+ return pb , nil
90
+ }
91
+
92
+ func checkOrGetId (curveadm * cli.CurveAdm , dcs []* topology.DeployConfig , options enterOptions ) (string , error ) {
93
+ id := options .id
94
+ if id != "" {
95
+ return id , nil
96
+ }
97
+ pb , err := genLeaderOrRandomPlaybook (curveadm , dcs )
98
+ if err != nil {
99
+ return "" , err
100
+ }
101
+ // run playground
102
+ err = pb .Run ()
103
+ if err != nil {
104
+ return "" , err
105
+ }
106
+ // get leader or random container id
107
+ value := curveadm .MemStorage ().Get (comm .LEADER_OR_RANDOM_ID )
108
+ if value == nil {
109
+ return "" , errno .ERR_NO_LEADER_OR_RANDOM_CONTAINER_FOUND
110
+ }
111
+ id = value .(common.Leader0rRandom ).Id
112
+ return id , nil
113
+ }
114
+
60
115
func runEnter (curveadm * cli.CurveAdm , options enterOptions ) error {
61
116
// 1) parse cluster topology
62
117
dcs , err := curveadm .ParseTopology ()
63
118
if err != nil {
64
119
return err
65
120
}
66
121
67
- // 2) filter service
122
+ // 2) check id options
123
+ id , err := checkOrGetId (curveadm , dcs , options )
124
+ if err != nil {
125
+ return err
126
+ }
127
+
128
+ // 3) filter service
68
129
dcs = curveadm .FilterDeployConfig (dcs , topology.FilterOption {
69
- Id : options . id ,
130
+ Id : id ,
70
131
Role : "*" ,
71
132
Host : "*" ,
72
133
})
73
134
if len (dcs ) == 0 {
74
135
return errno .ERR_NO_SERVICES_MATCHED
75
136
}
76
137
77
- // 3 ) get container id
138
+ // 4 ) get container id
78
139
dc := dcs [0 ]
79
140
serviceId := curveadm .GetServiceId (dc .GetId ())
80
141
containerId , err := curveadm .GetContainerId (serviceId )
81
142
if err != nil {
82
143
return err
83
144
}
84
145
85
- // 4) attch remote container
146
+ // 5) attach remote container
86
147
home := dc .GetProjectLayout ().ServiceRootDir
87
148
return tools .AttachRemoteContainer (curveadm , dc .GetHost (), containerId , home )
88
149
}
0 commit comments