This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
attach.go
183 lines (159 loc) · 5.03 KB
/
attach.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
// Copyright 2016 The rkt Authors
//
// 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 main
import (
"fmt"
"strings"
"github.com/rkt/rkt/common"
pkgPod "github.com/rkt/rkt/pkg/pod"
"github.com/rkt/rkt/stage0"
"github.com/rkt/rkt/store/imagestore"
"github.com/rkt/rkt/store/treestore"
"github.com/spf13/cobra"
)
var (
cmdAttach = &cobra.Command{
Use: "attach [--app=APPNAME] [--mode=MODE] UUID",
Short: "Attach to an app running within a rkt pod",
Long: `UUID should be the UUID of a running pod.`,
Run: ensureSuperuser(runWrapper(runAttach)),
}
flagAttachMode string
)
func init() {
if common.IsExperimentEnabled("attach") {
cmdRkt.AddCommand(cmdAttach)
cmdAttach.Flags().StringVar(&flagAppName, "app", "", "name of the app to enter within the specified pod")
cmdAttach.Flags().StringVar(&flagAttachMode, "mode", "auto", "attach mode")
}
}
func runAttach(cmd *cobra.Command, args []string) (exit int) {
if len(args) < 1 {
cmd.Usage()
return 254
}
uuid := args[0]
p, err := pkgPod.PodFromUUIDString(getDataDir(), uuid)
if err != nil {
stderr.PrintE("problem retrieving pod", err)
return 254
}
defer p.Close()
if p.State() != pkgPod.Running {
stderr.Printf("pod %q isn't currently running", p.UUID)
return 254
}
podPID, err := p.ContainerPid1()
if err != nil {
stderr.PrintE(fmt.Sprintf("unable to determine the pid for pod %q", p.UUID), err)
return 254
}
appName, err := getAppName(p)
if err != nil {
stderr.PrintE("unable to determine app name", err)
return 254
}
s, err := imagestore.NewStore(storeDir())
if err != nil {
stderr.PrintE("cannot open store", err)
return 254
}
ts, err := treestore.NewStore(treeStoreDir(), s)
if err != nil {
stderr.PrintE("cannot open store", err)
return 254
}
stage1TreeStoreID, err := p.GetStage1TreeStoreID()
if err != nil {
stderr.PrintE("error getting stage1 treeStoreID", err)
return 254
}
// prepare stage1/attach flags
stage1RootFS := ts.GetRootFS(stage1TreeStoreID)
attachArgs, err := createStage1AttachFlags(flagAttachMode)
if err != nil {
stderr.PrintE("invalid attach mode", err)
return 254
}
attachArgs = append(attachArgs,
fmt.Sprintf("--app=%s", appName),
fmt.Sprintf("--debug=%t", globalFlags.Debug),
)
if err = stage0.Attach(p.Path(), podPID, *appName, stage1RootFS, uuid, attachArgs); err != nil {
stderr.PrintE("attach failed", err)
return 254
}
// not reached when stage0.Attach execs /enter
return 0
}
// createStage1AttachFlags parses an attach stage0 CLI "--mode" flag and
// returns options suited for stage1/attach entrypoint invocation
func createStage1AttachFlags(attachMode string) ([]string, error) {
attachArgs := []string{}
// list mode: just print endpoints
if attachMode == "list" {
attachArgs = append(attachArgs, "--action=list")
return attachArgs, nil
}
// auto-attach mode: stage1-attach will figure out endpoints
if attachMode == "auto" || attachMode == "" {
attachArgs = append(attachArgs, "--action=auto-attach")
return attachArgs, nil
}
// custom-attach mode: user specified endpoints
var customEndpoints struct {
TTYIn bool
TTYOut bool
Stdin bool
Stdout bool
Stderr bool
}
attachArgs = append(attachArgs, "--action=custom-attach")
// parse comma-separated endpoints for custom attach
eps := strings.Split(attachMode, ",")
for _, e := range eps {
switch e {
case "stdin":
customEndpoints.Stdin = true
case "stdout":
customEndpoints.Stdout = true
case "stderr":
customEndpoints.Stderr = true
case "tty":
customEndpoints.TTYIn = true
customEndpoints.TTYOut = true
case "tty-in":
customEndpoints.TTYIn = true
case "tty-out":
customEndpoints.TTYOut = true
default:
return nil, fmt.Errorf("unknown endpoint %q", e)
}
}
// check that the resulting attach mode is sane
if !(customEndpoints.TTYIn || customEndpoints.TTYOut || customEndpoints.Stdin || customEndpoints.Stdout || customEndpoints.Stderr) {
return nil, fmt.Errorf("mode must specify at least one endpoint to attach")
}
if (customEndpoints.TTYIn || customEndpoints.TTYOut) && (customEndpoints.Stdin || customEndpoints.Stdout || customEndpoints.Stderr) {
return nil, fmt.Errorf("incompatible endpoints %q, cannot simultaneously attach TTY and streams", attachMode)
}
attachArgs = append(attachArgs,
fmt.Sprintf("--tty-in=%t", customEndpoints.TTYIn),
fmt.Sprintf("--tty-out=%t", customEndpoints.TTYOut),
fmt.Sprintf("--stdin=%t", customEndpoints.Stdin),
fmt.Sprintf("--stdout=%t", customEndpoints.Stdout),
fmt.Sprintf("--stderr=%t", customEndpoints.Stderr),
)
return attachArgs, nil
}