-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage.go
213 lines (179 loc) · 4.92 KB
/
stage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package command
import (
"fmt"
"github.com/dodo-cli/dodo-core/pkg/plugin"
api "github.com/dodo-cli/dodo-stage/api/v1alpha1"
"github.com/dodo-cli/dodo-stage/pkg/config"
"github.com/dodo-cli/dodo-stage/pkg/plugin/stage"
log "github.com/hashicorp/go-hclog"
"github.com/oclaussen/go-gimme/configfiles"
"github.com/oclaussen/go-gimme/ssh"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
func New(m plugin.Manager) *Command {
cmd := &cobra.Command{
Use: "stage",
Short: "Manage stages",
TraverseChildren: true,
SilenceUsage: true,
}
cmd.AddCommand(NewListCommand(m))
cmd.AddCommand(NewUpCommand(m))
cmd.AddCommand(NewDownCommand(m))
cmd.AddCommand(NewSSHCommand(m))
return &Command{cmd: cmd}
}
func NewListCommand(m plugin.Manager) *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "List stages",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
filenames := []string{}
configfiles.GimmeConfigFiles(&configfiles.Options{
Name: "dodo",
Extensions: []string{"yaml", "yml", "json"},
IncludeWorkingDirectories: true,
Filter: func(configFile *configfiles.ConfigFile) bool {
filenames = append(filenames, configFile.Path)
return false
},
})
stages, err := config.GetAllStages(filenames...)
if err != nil {
log.L().Error(err.Error())
}
for name, conf := range stages {
s, err := loadPlugin(m, conf.Type)
if err != nil {
return err
}
current, err := s.GetStage(name)
if err != nil {
return err
}
fmt.Printf("%s (%v)", name, current.Available)
}
return nil
},
}
}
func NewUpCommand(m plugin.Manager) *cobra.Command {
return &cobra.Command{
Use: "up",
Short: "Create or start a stage",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := loadStageConfig(args[0])
if err != nil {
return err
}
s, err := loadPlugin(m, conf.Type)
if err != nil {
return err
}
current, err := s.GetStage(args[0])
if err != nil {
return err
}
if !current.Exist {
return s.CreateStage(conf)
}
return s.StartStage(args[0])
},
}
}
type downOptions struct {
remove bool
volumes bool
force bool
}
func NewDownCommand(m plugin.Manager) *cobra.Command {
var opts downOptions
cmd := &cobra.Command{
Use: "down",
Short: "Remove or pause a stage",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := loadStageConfig(args[0])
if err != nil {
return err
}
s, err := loadPlugin(m, conf.Type)
if err != nil {
return err
}
if opts.remove {
return s.DeleteStage(args[0], opts.force, opts.volumes)
}
return s.StopStage(args[0])
},
}
flags := cmd.Flags()
flags.BoolVarP(&opts.remove, "rm", "", false, "remove the stage instead of pausing")
flags.BoolVarP(&opts.volumes, "volumes", "", false, "when used with '--rm', also delete persistent volumes")
flags.BoolVarP(&opts.force, "force", "f", false, "when used with '--rm', don't stop on errors")
return cmd
}
func NewSSHCommand(m plugin.Manager) *cobra.Command {
return &cobra.Command{
Use: "ssh",
Short: "login to the stage",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := loadStageConfig(args[0])
if err != nil {
return err
}
s, err := loadPlugin(m, conf.Type)
if err != nil {
return err
}
current, err := s.GetStage(args[0])
if err != nil {
return err
}
if !current.Available {
return errors.New("stage is not up")
}
return ssh.GimmeShell(&ssh.Options{
Host: current.SshOptions.Hostname,
Port: int(current.SshOptions.Port),
User: current.SshOptions.Username,
IdentityFileGlobs: []string{current.SshOptions.PrivateKeyFile},
NonInteractive: true,
})
},
}
}
func loadStageConfig(name string) (*api.Stage, error) {
filenames := []string{}
configfiles.GimmeConfigFiles(&configfiles.Options{
Name: "dodo",
Extensions: []string{"yaml", "yml", "json"},
IncludeWorkingDirectories: true,
Filter: func(configFile *configfiles.ConfigFile) bool {
filenames = append(filenames, configFile.Path)
return false
},
})
stages, err := config.GetAllStages(filenames...)
if err != nil {
log.L().Error(err.Error())
}
if conf, ok := stages[name]; ok {
conf.Name = name // TODO: figure out where to set defaults like this
return conf, nil
}
return nil, fmt.Errorf("could not find any configuration for stage '%s'", name)
}
func loadPlugin(m plugin.Manager, name string) (stage.Stage, error) {
for _, p := range m.GetPlugins(stage.Type.String()) {
s := p.(stage.Stage)
if info := s.PluginInfo(); info.Name.Name == name {
return s, nil
}
}
return nil, fmt.Errorf("could not find any stage plugin for type '%s'", name)
}