Fixes for fleetctl preview (#295)

- Better documentation and error for missing docker-compose.
- Handle case of no existing config file.
- Make logs directory world-writable (to allow writes from inside container).

Fixes #286
This commit is contained in:
Zach Wasserman
2021-02-13 08:41:46 -08:00
committed by GitHub
parent ea2454a0d9
commit f95aa37646
3 changed files with 24 additions and 15 deletions
+7 -4
View File
@@ -63,20 +63,23 @@ func makeConfigIfNotExists(fp string) error {
return err
}
func readConfig(fp string) (c configFile, err error) {
func readConfig(fp string) (configFile, error) {
var c configFile
b, err := ioutil.ReadFile(fp)
if err != nil {
return
return c, err
}
err = yaml.Unmarshal(b, &c)
if err := yaml.Unmarshal(b, &c); err != nil {
return c, errors.Wrap(err, "unmarshal config")
}
if c.Contexts == nil {
c.Contexts = map[string]Context{
"default": Context{},
}
}
return
return c, nil
}
func writeConfig(fp string, c configFile) error {
+16 -10
View File
@@ -57,8 +57,15 @@ This command will create a directory fleet-preview in the current working direct
return err
}
// Make sure the logs directory is writable, otherwise the Fleet
// server errors on startup. This can be a problem when running on
// Linux with a non-root user inside the container.
if err := os.Chmod(filepath.Join(previewDir, "logs"), 0777); err != nil {
return errors.Wrap(err, "make logs writable")
}
fmt.Println("Starting Docker containers...")
out, err := exec.Command("docker-compose", "up", "-d", "mysql01", "redis01", "fleet01").CombinedOutput()
out, err := exec.Command("docker-compose", "up", "-d", "--remove-orphans", "mysql01", "redis01", "fleet01").CombinedOutput()
if err != nil {
fmt.Println(string(out))
return errors.Errorf("Failed to run docker-compose")
@@ -100,7 +107,9 @@ This command will create a directory fleet-preview in the current working direct
config, err := readConfig(configPath)
if err != nil {
// No existing config
config.Contexts["default"] = contextConfig
config.Contexts = map[string]Context{
"default": contextConfig,
}
} else {
fmt.Println("Configured fleetctl in the 'preview' context to avoid overwriting existing config.")
context = "preview"
@@ -142,12 +151,9 @@ This command will create a directory fleet-preview in the current working direct
return errors.New("Expected 1 active enroll secret")
}
if err := os.Chdir(filepath.Join(previewDir, "osquery")); err != nil {
return errors.Wrap(err, "Error getting preview osquery directory")
}
fmt.Println("Starting simulated hosts...")
cmd := exec.Command("docker-compose", "up", "-d")
cmd := exec.Command("docker-compose", "up", "-d", "--remove-orphans")
cmd.Dir = filepath.Join(previewDir, "osquery")
cmd.Env = append(cmd.Env,
"ENROLL_SECRET="+secrets.Secrets[0].Secret,
"FLEET_URL="+address,
@@ -284,12 +290,12 @@ func waitStartup() error {
func checkDocker() error {
// Check installed
if _, err := exec.LookPath("docker-compose"); err != nil {
return errors.New("Docker is required for the fleetctl preview experience.\n\nPlease install Docker (https://docs.docker.com/get-docker/).")
}
if _, err := exec.LookPath("docker"); err != nil {
return errors.New("Docker is required for the fleetctl preview experience.\n\nPlease install Docker (https://docs.docker.com/get-docker/).")
}
if _, err := exec.LookPath("docker-compose"); err != nil {
return errors.New("Docker Compose is required for the fleetctl preview experience.\n\nPlease install Docker Compose (https://docs.docker.com/compose/install/).")
}
// Check running
if err := exec.Command("docker", "info").Run(); err != nil {