MCPcopy Create free account
hub / github.com/docker/buildx / RunMonitor

Function RunMonitor

monitor/monitor.go:124–280  ·  view source on GitHub ↗

RunMonitor provides an interactive session for running and managing containers via specified IO.

(ctx context.Context, invokeConfig *build.InvokeConfig, rCtx *build.ResultHandle, stdin io.ReadCloser, stdout, stderr io.WriteCloser, progress *progress.Printer)

Source from the content-addressed store, hash-verified

122
123// RunMonitor provides an interactive session for running and managing containers via specified IO.
124func RunMonitor(ctx context.Context, invokeConfig *build.InvokeConfig, rCtx *build.ResultHandle, stdin io.ReadCloser, stdout, stderr io.WriteCloser, progress *progress.Printer) error {
125 progress.Pause()
126 defer progress.Resume()
127
128 defer stdin.Close()
129
130 monitorIn, monitorOut := ioset.Pipe()
131 defer func() {
132 monitorIn.Close()
133 }()
134 monitorEnableCh := make(chan struct{})
135 monitorDisableCh := make(chan struct{})
136 monitorOutCtx := ioset.MuxOut{
137 Out: monitorOut,
138 EnableHook: func() { monitorEnableCh <- struct{}{} },
139 DisableHook: func() { monitorDisableCh <- struct{}{} },
140 }
141
142 containerIn, containerOut := ioset.Pipe()
143 defer func() {
144 containerIn.Close()
145 }()
146 containerOutCtx := ioset.MuxOut{
147 Out: containerOut,
148 // send newline to hopefully get the prompt; TODO: better UI (e.g. reprinting the last line)
149 EnableHook: func() { containerOut.Stdin.Write([]byte("\n")) },
150 DisableHook: func() {},
151 }
152
153 invokeForwarder := ioset.NewForwarder()
154 invokeForwarder.SetIn(&containerIn)
155 m := &monitor{
156 rCtx: rCtx,
157 processes: processes.NewManager(),
158 invokeIO: invokeForwarder,
159 muxIO: ioset.NewMuxIO(ioset.In{
160 Stdin: io.NopCloser(stdin),
161 Stdout: nopCloser{stdout},
162 Stderr: nopCloser{stderr},
163 }, []ioset.MuxOut{monitorOutCtx, containerOutCtx}, 1, func(prev int, res int) string {
164 if prev == 0 && res == 0 {
165 // No toggle happened because container I/O isn't enabled.
166 return "Process isn't attached (previous \"exec\" exited?). Use \"attach\" for attaching or \"rollback\" or \"exec\" for running new one.\n"
167 }
168 return "Switched IO\n"
169 }),
170 }
171 m.ctx, m.cancel = context.WithCancelCause(context.Background())
172
173 defer func() {
174 if err := m.Close(); err != nil {
175 logrus.Warnf("close error: %v", err)
176 }
177 }()
178
179 // Start container automatically
180 fmt.Fprintf(stdout, "Launching interactive container. Press Ctrl-a-c to switch to monitor console\n")
181 invokeConfig.Rollback = false

Callers 1

RunMethod · 0.85

Calls 15

SetInMethod · 0.95
CloseMethod · 0.95
RollbackMethod · 0.95
SetOutMethod · 0.95
closeMethod · 0.95
PipeFunction · 0.92
NewForwarderFunction · 0.92
NewManagerFunction · 0.92
NewMuxIOFunction · 0.92
NewReloadCmdFunction · 0.92
NewRollbackCmdFunction · 0.92
NewAttachCmdFunction · 0.92

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…