2018-05-07 23:50:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-11-22 14:13:26 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-08-05 22:07:32 +00:00
|
|
|
"os"
|
2023-02-15 18:01:44 +00:00
|
|
|
"path/filepath"
|
2018-05-07 23:50:20 +00:00
|
|
|
|
2022-08-05 22:07:32 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/pkg/spec"
|
2022-09-19 17:53:44 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2021-03-13 00:42:38 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2018-05-07 23:50:20 +00:00
|
|
|
)
|
|
|
|
|
2021-03-13 00:42:38 +00:00
|
|
|
func applyCommand() *cli.Command {
|
2022-09-19 17:53:44 +00:00
|
|
|
var (
|
|
|
|
flFilename string
|
|
|
|
flForce bool
|
|
|
|
flDryRun bool
|
|
|
|
)
|
2021-03-13 00:42:38 +00:00
|
|
|
return &cli.Command{
|
2018-05-07 23:50:20 +00:00
|
|
|
Name: "apply",
|
|
|
|
Usage: "Apply files to declaratively manage osquery configurations",
|
|
|
|
UsageText: `fleetctl apply [options]`,
|
|
|
|
Flags: []cli.Flag{
|
2021-03-13 00:42:38 +00:00
|
|
|
&cli.StringFlag{
|
2018-05-07 23:50:20 +00:00
|
|
|
Name: "f",
|
2021-03-13 00:42:38 +00:00
|
|
|
EnvVars: []string{"FILENAME"},
|
2018-05-07 23:50:20 +00:00
|
|
|
Value: "",
|
|
|
|
Destination: &flFilename,
|
|
|
|
Usage: "A file to apply",
|
|
|
|
},
|
2022-09-19 17:53:44 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "force",
|
|
|
|
EnvVars: []string{"FORCE"},
|
|
|
|
Destination: &flForce,
|
|
|
|
Usage: "Force applying the file even if it raises validation errors (only supported for 'config' and 'team' specs)",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "dry-run",
|
|
|
|
EnvVars: []string{"DRY_RUN"},
|
|
|
|
Destination: &flDryRun,
|
|
|
|
Usage: "Do not apply the file, just validate it (only supported for 'config' and 'team' specs)",
|
|
|
|
},
|
2021-02-03 02:55:16 +00:00
|
|
|
configFlag(),
|
|
|
|
contextFlag(),
|
|
|
|
debugFlag(),
|
2018-05-07 23:50:20 +00:00
|
|
|
},
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
if flFilename == "" {
|
|
|
|
return errors.New("-f must be specified")
|
|
|
|
}
|
2022-08-05 22:07:32 +00:00
|
|
|
b, err := os.ReadFile(flFilename)
|
2018-05-07 23:50:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-16 18:28:13 +00:00
|
|
|
fleetClient, err := clientFromCLI(c)
|
2018-05-07 23:50:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-05 22:07:32 +00:00
|
|
|
specs, err := spec.GroupFromBytes(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logf := func(format string, a ...interface{}) {
|
|
|
|
fmt.Fprintf(c.App.Writer, format, a...)
|
|
|
|
}
|
2022-09-19 17:53:44 +00:00
|
|
|
|
|
|
|
opts := fleet.ApplySpecOptions{
|
|
|
|
Force: flForce,
|
|
|
|
DryRun: flDryRun,
|
|
|
|
}
|
2023-02-15 18:01:44 +00:00
|
|
|
baseDir := filepath.Dir(flFilename)
|
|
|
|
err = fleetClient.ApplyGroup(c.Context, specs, baseDir, logf, opts)
|
2018-05-07 23:50:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-28 19:28:07 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|