Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enter: start default services before executing subcommand #257

Merged
merged 15 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions internals/cli/cmd_enter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"path/filepath"
"strings"
"text/template"
"time"

. "gopkg.in/check.v1"

Expand Down Expand Up @@ -242,3 +243,40 @@ func (s *PebbleSuite) TestEnterHelpCommandHelpArg(c *C) {
c.Check(stdout, Matches, "(?s).*\\bThe help command displays information about commands\\..*")
c.Check(exitCode, Equals, 0)
}

// TestEnterSubCommandWaits checks that the subcommand in enter
// starts **after** the default services have started. It utilizes
// the modification time of /proc/[PID] directory for checking.
func (s *PebbleSuite) TestEnterSubCommandWaits(c *C) {
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
layerTemplate := dumbDedent(`
services:
stat:
override: replace
command: /bin/sh -c 'stat -c "%y" /proc/$$ > $PEBBLE/stat; sleep 1;'
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
startup: enabled
`)
layerPath := filepath.Join(s.pebbleDir, "layers", "001-stat.yaml")
writeTemplate(layerPath, layerTemplate, nil)

cmd := []string{"pebble", "enter", "--run", "exec", "/bin/sh", "-c", "stat -c '%y' /proc/$$"}
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
restore := fakeArgs(cmd...)
defer restore()

exitCode := cli.PebbleMain()
c.Check(exitCode, Equals, 0)
// stderr is written to stdout buffer because of "combine stderr" mode,
// see cmd/pebble/cmd_exec.go:163
c.Check(s.Stderr(), Equals, "")
stdout := s.Stdout()

layout := "2006-01-02 15:04:05.000000000 -0700"
subCmdExecTime, err := time.Parse(layout, strings.TrimSpace(stdout))
c.Check(err, IsNil)

svcOut, err := ioutil.ReadFile(filepath.Join(s.pebbleDir, "stat"))
c.Check(err, IsNil)
svcStartTime, err := time.Parse(layout, strings.TrimSpace(string(svcOut)))
c.Check(err, IsNil)

c.Check(svcStartTime.Before(subCmdExecTime), Equals, true)
}
33 changes: 27 additions & 6 deletions internals/cli/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ func runDaemon(rcmd *cmdRun, ch chan os.Signal, ready chan<- func()) error {

logger.Debugf("activation done in %v", time.Now().Truncate(time.Millisecond).Sub(t0))

var autoStartReady chan error
var stop chan struct{}

notifyReady := func() {
stop = make(chan struct{}, 1)
ready <- func() { close(stop) }
close(ready)
}

if !rcmd.Hold {
servopts := client.ServiceOptions{}
changeID, err := rcmd.client.AutoStart(&servopts)
Expand All @@ -225,13 +234,20 @@ func runDaemon(rcmd *cmdRun, ch chan os.Signal, ready chan<- func()) error {
} else {
logger.Noticef("Started default services with change %s.", changeID)
}
}

var stop chan struct{}
if ready != nil {
stop = make(chan struct{}, 1)
ready <- func() { close(stop) }
close(ready)
benhoyt marked this conversation as resolved.
Show resolved Hide resolved
if ready != nil {
// wait for the default services to start
autoStartReady = make(chan error, 1)
go func() {
waitCmd := waitMixin{
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
hideProgress: true,
}
_, err := waitCmd.wait(rcmd.client, changeID)
autoStartReady <- err
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
}()
}
} else if ready != nil {
notifyReady()
}

out:
Expand All @@ -249,6 +265,11 @@ out:
d.SetDegradedMode(nil)
tic.Stop()
}
case chgErr := <-autoStartReady:
if chgErr != nil {
logger.Noticef("Error starting default services: %v", chgErr)
}
notifyReady()
case <-stop:
break out
}
Expand Down
19 changes: 15 additions & 4 deletions internals/cli/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var (
)

type waitMixin struct {
NoWait bool `long:"no-wait"`
NoWait bool `long:"no-wait"`
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
hideProgress bool
}

var waitArgsHelp = map[string]string{
Expand All @@ -46,19 +47,24 @@ func (wmx waitMixin) wait(cli *client.Client, id string) (*client.Change, error)
return nil, noWait
}

change, err := Wait(cli, id)
change, err := Wait(cli, id, &WaitOptions{hideProgress: wmx.hideProgress})
if err != nil {
return nil, err
}
return change, nil
}

type WaitOptions struct {
// Do not display a progress bar.
hideProgress bool
}

// Wait polls the progress of a change and displays a progress bar.
//
// This function blocks until the change is done or fails.
// If the change has numeric progress information, the information is
// displayed as a progress bar.
func Wait(cli *client.Client, changeID string) (*client.Change, error) {
func Wait(cli *client.Client, changeID string, opts *WaitOptions) (*client.Change, error) {
// Intercept sigint
sigs := make(chan os.Signal, 2)
signal.Notify(sigs, os.Interrupt)
Expand All @@ -74,7 +80,12 @@ func Wait(cli *client.Client, changeID string) (*client.Change, error) {
}
}()

pb := progress.MakeProgressBar()
var pb progress.Meter
if opts.hideProgress {
pb = progress.NullMeter{}
} else {
pb = progress.MakeProgressBar()
}
defer func() {
pb.Finished()
// Next two are not strictly needed for CLI, but
Expand Down