Improvements to fleetctl preview (#495)

- Download configuration on every run. This ensures that users always
  get the latest version.
- Pull latest Docker containers on every run.
- Add stop and reset commands (for cleanup).

Fixes #378
This commit is contained in:
Zach Wasserman 2021-03-19 16:45:21 -07:00 committed by GitHub
parent d3dc7cfc53
commit a62277b9b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,11 +27,14 @@ const (
func previewCommand() *cli.Command { func previewCommand() *cli.Command {
return &cli.Command{ return &cli.Command{
Name: "preview", Name: "preview",
Usage: "Set up a preview deployment of the Fleet server", Usage: "Start a preview deployment of the Fleet server",
UsageText: `Set up a preview deployment of the Fleet server using Docker and docker-compose. Docker tools must be available in the environment. Description: `Start a preview deployment of the Fleet server using Docker and docker-compose. Docker tools must be available in the environment.
This command will create a directory fleet-preview in the current working directory. Configurations can be modified in that directory.`, Use the stop and reset subcommands to manage the server and dependencies once started.`,
Subcommands: []*cli.Command{}, Subcommands: []*cli.Command{
previewStopCommand(),
previewResetCommand(),
},
Flags: []cli.Flag{ Flags: []cli.Flag{
configFlag(), configFlag(),
contextFlag(), contextFlag(),
@ -42,20 +45,19 @@ This command will create a directory fleet-preview in the current working direct
return err return err
} }
// Download files if necessary // Download files every time to ensure the user gets the most up to date versions
previewDir := previewDirectory() previewDir := previewDirectory()
if _, err := os.Stat( fmt.Printf("Downloading dependencies into %s...\n", previewDir)
filepath.Join(previewDir, "docker-compose.yml"), if err := downloadFiles(); err != nil {
); err != nil { return errors.Wrap(err, "Error downloading dependencies")
fmt.Printf("Downloading dependencies into %s...\n", previewDir)
if err := downloadFiles(); err != nil {
return errors.Wrap(err, "Error downloading dependencies")
}
} }
if err := os.Chdir(previewDir); err != nil { if err := os.Chdir(previewDir); err != nil {
return err return err
} }
if _, err := os.Stat("docker-compose.yml"); err != nil {
return errors.Wrap(err, "docker-compose file not found in preview directory")
}
// Make sure the logs directory is writable, otherwise the Fleet // Make sure the logs directory is writable, otherwise the Fleet
// server errors on startup. This can be a problem when running on // server errors on startup. This can be a problem when running on
@ -64,8 +66,15 @@ This command will create a directory fleet-preview in the current working direct
return errors.Wrap(err, "make logs writable") return errors.Wrap(err, "make logs writable")
} }
fmt.Println("Pulling Docker dependencies...")
out, err := exec.Command("docker-compose", "pull", "mysql01", "redis01", "fleet01").CombinedOutput()
if err != nil {
fmt.Println(string(out))
return errors.Errorf("Failed to run docker-compose")
}
fmt.Println("Starting Docker containers...") fmt.Println("Starting Docker containers...")
out, err := exec.Command("docker-compose", "up", "-d", "--remove-orphans", "mysql01", "redis01", "fleet01").CombinedOutput() out, err = exec.Command("docker-compose", "up", "-d", "--remove-orphans", "mysql01", "redis01", "fleet01").CombinedOutput()
if err != nil { if err != nil {
fmt.Println(string(out)) fmt.Println(string(out))
return errors.Errorf("Failed to run docker-compose") return errors.Errorf("Failed to run docker-compose")
@ -90,7 +99,10 @@ This command will create a directory fleet-preview in the current working direct
token, err := fleet.Setup(username, username, password, "Fleet Preview") token, err := fleet.Setup(username, username, password, "Fleet Preview")
if err != nil { if err != nil {
if e, ok := err.(service.SetupAlreadyErr); !(ok && e.SetupAlready()) { switch errors.Cause(err).(type) {
case service.SetupAlreadyErr:
// Ignore this error
default:
return errors.Wrap(err, "Error setting up Fleet") return errors.Wrap(err, "Error setting up Fleet")
} }
} }
@ -304,3 +316,73 @@ func checkDocker() error {
return nil return nil
} }
func previewStopCommand() *cli.Command {
return &cli.Command{
Name: "stop",
Usage: "Stop the Fleet preview server and dependencies",
Flags: []cli.Flag{
configFlag(),
contextFlag(),
debugFlag(),
},
Action: func(c *cli.Context) error {
if err := checkDocker(); err != nil {
return err
}
previewDir := previewDirectory()
if err := os.Chdir(previewDir); err != nil {
return err
}
if _, err := os.Stat("docker-compose.yml"); err != nil {
return errors.Wrap(err, "docker-compose file not found in preview directory")
}
out, err := exec.Command("docker-compose", "stop").CombinedOutput()
if err != nil {
fmt.Println(string(out))
return errors.Errorf("Failed to run docker-compose stop")
}
fmt.Println("Fleet preview server and dependencies stopped. Start again with fleetctl preview.")
return nil
},
}
}
func previewResetCommand() *cli.Command {
return &cli.Command{
Name: "reset",
Usage: "Reset the Fleet preview server and dependencies",
Flags: []cli.Flag{
configFlag(),
contextFlag(),
debugFlag(),
},
Action: func(c *cli.Context) error {
if err := checkDocker(); err != nil {
return err
}
previewDir := previewDirectory()
if err := os.Chdir(previewDir); err != nil {
return err
}
if _, err := os.Stat("docker-compose.yml"); err != nil {
return errors.Wrap(err, "docker-compose file not found in preview directory")
}
out, err := exec.Command("docker-compose", "rm", "-sf").CombinedOutput()
if err != nil {
fmt.Println(string(out))
return errors.Errorf("Failed to run docker-compose rm -sf")
}
fmt.Println("Fleet preview server and dependencies reset. Start again with fleetctl preview.")
return nil
},
}
}