Skip to content

Commit 7ad20a0

Browse files
authored
Merge pull request #37 from cgwalters/container-attach
disk: Simplify logging with attach API, use tty
2 parents 7a1ad2d + 83f65df commit 7ad20a0

File tree

1 file changed

+11
-55
lines changed

1 file changed

+11
-55
lines changed

pkg/bootc/bootc_disk.go

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -336,62 +336,15 @@ func (p *BootcDisk) runInstallContainer(quiet bool, config DiskImageConfig) (err
336336
logrus.Debugf("Started install container")
337337

338338
var exitCode int32
339-
if quiet {
340-
//wait for the container to finish
341-
logrus.Debugf("Waiting for container completion")
342-
exitCode, err = containers.Wait(p.Ctx, p.bootcInstallContainerId, nil)
343-
if err != nil {
344-
return fmt.Errorf("failed to wait for container: %w", err)
345-
}
346-
} else {
347-
// stream logs to stdout and stderr
348-
stdOut := make(chan string)
349-
stdErr := make(chan string)
350-
logErrors := make(chan error)
351-
352-
var wg sync.WaitGroup
353-
go func() {
354-
follow := true
355-
defer close(stdOut)
356-
defer close(stdErr)
357-
trueV := true
358-
err = containers.Logs(p.Ctx, p.bootcInstallContainerId, &containers.LogOptions{Follow: &follow, Stdout: &trueV, Stderr: &trueV}, stdOut, stdErr)
359-
if err != nil {
360-
logErrors <- err
361-
}
362-
363-
close(logErrors)
364-
}()
365-
366-
wg.Add(1)
367-
go func() {
368-
for str := range stdOut {
369-
fmt.Print(str)
370-
}
371-
wg.Done()
372-
}()
373-
374-
wg.Add(1)
375-
go func() {
376-
for str := range stdErr {
377-
fmt.Fprintf(os.Stderr, "%s", str)
378-
}
379-
wg.Done()
380-
}()
381-
382-
//wait for the container to finish
383-
logrus.Debugf("Waiting for container completion (streaming output)")
384-
exitCode, err = containers.Wait(p.Ctx, p.bootcInstallContainerId, nil)
385-
if err != nil {
386-
return fmt.Errorf("failed to wait for container: %w", err)
387-
}
388-
389-
if err := <-logErrors; err != nil {
390-
return fmt.Errorf("failed to get logs: %w", err)
339+
if !quiet {
340+
attachOpts := new(containers.AttachOptions).WithStream(true)
341+
if err := containers.Attach(p.Ctx, p.bootcInstallContainerId, os.Stdin, os.Stdout, os.Stderr, nil, attachOpts); err != nil {
342+
return fmt.Errorf("attaching: %w", err)
391343
}
392-
393-
// Ensure the streams are done
394-
wg.Wait()
344+
}
345+
exitCode, err = containers.Wait(p.Ctx, p.bootcInstallContainerId, nil)
346+
if err != nil {
347+
return fmt.Errorf("failed to wait for container: %w", err)
395348
}
396349

397350
if exitCode != 0 {
@@ -424,13 +377,16 @@ func (p *BootcDisk) createInstallContainer(config DiskImageConfig, tempLosetup s
424377
}
425378
bootcInstallArgs = append(bootcInstallArgs, "/output/"+filepath.Base(p.file.Name()))
426379

380+
// Allocate pty so we can show progress bars, spinners etc.
381+
trueDat := true
427382
s := &specgen.SpecGenerator{
428383
ContainerBasicConfig: specgen.ContainerBasicConfig{
429384
Command: bootcInstallArgs,
430385
PidNS: specgen.Namespace{NSMode: specgen.Host},
431386
Remove: &autoRemove,
432387
Annotations: map[string]string{"io.podman.annotations.label": "type:unconfined_t"},
433388
Env: targetEnv,
389+
Terminal: &trueDat,
434390
},
435391
ContainerStorageConfig: specgen.ContainerStorageConfig{
436392
Image: p.ImageNameOrId,

0 commit comments

Comments
 (0)