-
Notifications
You must be signed in to change notification settings - Fork 8
/
run.go
280 lines (233 loc) · 8.19 KB
/
run.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cluster
import (
"context"
"encoding/json"
"fmt"
"strings"
composecli "github.com/compose-spec/compose-go/cli"
"github.com/google/go-containerregistry/pkg/name"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh"
"gopkg.in/yaml.v3"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/providers/nscloud/api"
"namespacelabs.dev/foundation/internal/runtime/rtypes"
"namespacelabs.dev/foundation/std/tasks"
"namespacelabs.dev/go-ids"
)
func NewRunCmd() *cobra.Command {
run := &cobra.Command{
Use: "run",
Short: "Starts a container in an ephemeral environment, optionally exporting ports for public serving.",
Args: cobra.NoArgs,
}
image := run.Flags().String("image", "", "Which image to run.")
requestedName := run.Flags().String("name", "", "If no name is specified, one is generated.")
exportedPorts := run.Flags().Int32SliceP("publish", "p", nil, "Publish the specified ports.")
output := run.Flags().StringP("output", "o", "plain", "One of plain or json.")
on := run.Flags().String("on", "", "Run the container in the specified container, instead of creating a new one.")
env := run.Flags().StringToStringP("env", "e", map[string]string{}, "Pass these additional environment variables to the container.")
devmode := run.Flags().Bool("development", false, "If true, enables a few development facilities, including making containers optional.")
labels := run.Flags().StringToString("label", nil, "Create the environment with a set of labels.")
run.Flags().MarkHidden("label")
run.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
name := *requestedName
if *image == "" {
return fnerrors.New("--image is required")
}
if name == "" {
name = generateNameFromImage(*image)
}
if *devmode && *on != "" {
return fnerrors.New("--development can only be set when creating an environment (i.e. it can't be set when --on is specified)")
}
resp, err := createContainer(ctx, *on, *devmode, createContainerOpts{
Name: name,
Image: *image,
ExportedPorts: *exportedPorts,
Args: args,
Env: *env,
Labels: *labels,
})
if err != nil {
return err
}
return printResult(ctx, *output, resp)
})
return run
}
func NewRunComposeCmd() *cobra.Command {
run := &cobra.Command{
Use: "run-compose",
Short: "Starts a set of containers in an ephemeral environment.",
Args: cobra.NoArgs,
}
output := run.Flags().StringP("output", "o", "plain", "one of plain or json")
dir := run.Flags().String("dir", "", "If not specified, loads the compose project from the current working directory.")
devmode := run.Flags().Bool("development", false, "If true, enables a few development facilities, including making containers optional.")
run.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
resp, err := createCompose(ctx, *dir, *devmode)
if err != nil {
return err
}
return printResult(ctx, *output, resp)
})
return run
}
type createContainerOpts struct {
Name string
Image string
Args []string
Env map[string]string
Flags []string
ExportedPorts []int32
Labels map[string]string
}
func createContainer(ctx context.Context, target string, devmode bool, opts createContainerOpts) (*api.CreateContainersResponse, error) {
container := &api.ContainerRequest{
Name: opts.Name,
Image: opts.Image,
Args: opts.Args,
Env: opts.Env,
Flag: []string{"TERMINATE_ON_EXIT"},
}
for _, port := range opts.ExportedPorts {
container.ExportPort = append(container.ExportPort, &api.ContainerPort{
Proto: "tcp",
Port: port,
})
}
var labels []*api.LabelEntry
for key, value := range opts.Labels {
labels = append(labels, &api.LabelEntry{Name: key, Value: value})
}
if target == "" {
resp, err := tasks.Return(ctx, tasks.Action("nscloud.create-containers"), func(ctx context.Context) (*api.CreateContainersResponse, error) {
var response api.CreateContainersResponse
if err := api.Endpoint.CreateContainers.Do(ctx, api.CreateContainersRequest{
Container: []*api.ContainerRequest{container},
DevelopmentMode: devmode,
Label: labels,
}, fnapi.DecodeJSONResponse(&response)); err != nil {
return nil, err
}
return &response, nil
})
if err != nil {
return nil, err
}
if _, err := api.WaitCluster(ctx, api.Endpoint, resp.ClusterId, api.WaitClusterOpts{}); err != nil {
return nil, err
}
return resp, nil
}
return tasks.Return(ctx, tasks.Action("nscloud.start-containers"), func(ctx context.Context) (*api.CreateContainersResponse, error) {
var response api.StartContainersResponse
if err := api.Endpoint.StartContainers.Do(ctx, api.StartContainersRequest{
Id: target,
Container: []*api.ContainerRequest{container},
}, fnapi.DecodeJSONResponse(&response)); err != nil {
return nil, err
}
return &api.CreateContainersResponse{
Container: response.Container,
}, nil
})
}
func printResult(ctx context.Context, output string, resp *api.CreateContainersResponse) error {
switch output {
case "json":
d := json.NewEncoder(console.Stdout(ctx))
d.SetIndent("", " ")
return d.Encode(resp)
default:
if output != "" && output != "plain" {
fmt.Fprintf(console.Warnings(ctx), "unsupported output %q, defaulting to plain\n", output)
}
if resp.ClusterId != "" {
fmt.Fprintf(console.Stdout(ctx), "\n Created new ephemeral environment! (id: %s).\n", resp.ClusterId)
fmt.Fprintf(console.Stdout(ctx), "\n More at: %s\n", resp.ClusterUrl)
}
for _, ctr := range resp.Container {
fmt.Fprintf(console.Stdout(ctx), "\n Running %q\n", ctr.Name)
if len(ctr.ExportedPort) > 0 {
fmt.Fprintln(console.Stdout(ctx))
for _, port := range ctr.ExportedPort {
fmt.Fprintf(console.Stdout(ctx), " Exported %d/%s as https://%s\n", port.Port, port.Proto, port.IngressFqdn)
}
}
}
fmt.Fprintln(console.Stdout(ctx))
}
return nil
}
func createCompose(ctx context.Context, dir string, devmode bool) (*api.CreateContainersResponse, error) {
var optionsFn []composecli.ProjectOptionsFn
optionsFn = append(optionsFn,
composecli.WithOsEnv,
// composecli.WithEnvFile(o.EnvFile),
composecli.WithConfigFileEnv,
composecli.WithDefaultConfigPath,
composecli.WithDotEnv,
// composecli.WithName(o.Project),
)
if dir != "" {
optionsFn = append(optionsFn, composecli.WithWorkingDirectory(dir))
}
projectOptions, err := composecli.NewProjectOptions(nil, optionsFn...)
if err != nil {
return nil, err
}
project, err := composecli.ProjectFromOptions(projectOptions)
if err != nil {
return nil, err
}
projectYAML, err := yaml.Marshal(project)
if err != nil {
return nil, err
}
resp, err := tasks.Return(ctx, tasks.Action("nscloud.create-containers"), func(ctx context.Context) (*api.CreateContainersResponse, error) {
var response api.CreateContainersResponse
if err := api.Endpoint.CreateContainers.Do(ctx, api.CreateContainersRequest{
Compose: []*api.ComposeRequest{{Contents: projectYAML}},
DevelopmentMode: devmode,
}, fnapi.DecodeJSONResponse(&response)); err != nil {
return nil, err
}
return &response, nil
})
if err != nil {
return nil, err
}
if _, err := api.WaitCluster(ctx, api.Endpoint, resp.ClusterId, api.WaitClusterOpts{}); err != nil {
return nil, err
}
return resp, nil
}
func sshRun(ctx context.Context, sshcli *ssh.Client, io rtypes.IO, cmd string) error {
sess, err := sshcli.NewSession()
if err != nil {
return err
}
defer sess.Close()
sess.Stdin = io.Stdin
sess.Stdout = io.Stdout
sess.Stderr = io.Stderr
return sess.Run(cmd)
}
func generateNameFromImage(image string) string {
if tag, err := name.NewTag(image); err == nil {
p := strings.Split(tag.RepositoryStr(), "/")
last := p[len(p)-1]
if len(last) < 16 {
return fmt.Sprintf("%s-%s", last, ids.NewRandomBase32ID(3))
}
}
return ids.NewRandomBase32ID(6)
}