This enables imports in mcl code, and is one of last remaining blockers to using mgmt. Now we can start writing standalone modules, and adding standard library functions as needed. There's still lots to do, but this was a big missing piece. It was much harder to get right than I had expected, but I think it's solid! This unfortunately large commit is the result of some wild hacking I've been doing for the past little while. It's the result of a rebase that broke many "wip" commits that tracked my private progress, into something that's not gratuitously messy for our git logs. Since this was a learning and discovery process for me, I've "erased" the confusing git history that wouldn't have helped. I'm happy to discuss the dead-ends, and a small portion of that code was even left in for possible future use. This patch includes: * A change to the cli interface: You now specify the front-end explicitly, instead of leaving it up to the front-end to decide when to "activate". For example, instead of: mgmt run --lang code.mcl we now do: mgmt run lang --lang code.mcl We might rename the --lang flag in the future to avoid the awkward word repetition. Suggestions welcome, but I'm considering "input". One side-effect of this change, is that flags which are "engine" specific now must be specified with "run" before the front-end name. Eg: mgmt run --tmp-prefix lang --lang code.mcl instead of putting --tmp-prefix at the end. We also changed the GAPI slightly, but I've patched all code that used it. This also makes things consistent with the "deploy" command. * The deploys are more robust and let you deploy after a run This has been vastly improved and let's mgmt really run as a smart engine that can handle different workloads. If you don't want to deploy when you've started with `run` or if one comes in, you can use the --no-watch-deploy option to block new deploys. * The import statement exists and works! We now have a working `import` statement. Read the docs, and try it out. I think it's quite elegant how it fits in with `SetScope`. Have a look. As a result, we now have some built-in functions available in modules. This also adds the metadata.yaml entry-point for all modules. Have a look at the examples or the tests. The bulk of the patch is to support this. * Improved lang input parsing code: I re-wrote the parsing that determined what ran when we passed different things to --lang. Deciding between running an mcl file or raw code is now handled in a more intelligent, and re-usable way. See the inputs.go file if you want to have a look. One casualty is that you can't stream code from stdin *directly* to the front-end, it's encapsulated into a deploy first. You can still use stdin though! I doubt anyone will notice this change. * The scope was extended to include functions and classes: Go forth and import lovely code. All these exist in scopes now, and can be re-used! * Function calls actually use the scope now. Glad I got this sorted out. * There is import cycle detection for modules! Yes, this is another dag. I think that's #4. I guess they're useful. * A ton of tests and new test infra was added! This should make it much easier to add new tests that run mcl code. Have a look at TestAstFunc1 to see how to add more of these. As usual, I'll try to keep these commits smaller in the future!
389 lines
10 KiB
Go
389 lines
10 KiB
Go
// Mgmt
|
|
// Copyright (C) 2013-2018+ James Shubin and the project contributors
|
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package lib
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"sort"
|
|
|
|
"github.com/purpleidea/mgmt/bindata"
|
|
"github.com/purpleidea/mgmt/gapi"
|
|
// these imports are so that GAPIs register themselves in init()
|
|
_ "github.com/purpleidea/mgmt/lang"
|
|
_ "github.com/purpleidea/mgmt/langpuppet"
|
|
_ "github.com/purpleidea/mgmt/puppet"
|
|
_ "github.com/purpleidea/mgmt/yamlgraph"
|
|
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
// CLI is the entry point for using mgmt normally from the CLI.
|
|
func CLI(program, version string, flags Flags) error {
|
|
// test for sanity
|
|
if program == "" || version == "" {
|
|
return fmt.Errorf("program was not compiled correctly, see Makefile")
|
|
}
|
|
|
|
// All of these flags can be accessed in your GAPI implementation with
|
|
// the `c.Parent().Type` and `c.Parent().IsSet` functions. Their own
|
|
// flags can be accessed with `c.Type` and `c.IsSet` directly.
|
|
runFlags := []cli.Flag{
|
|
// common flags which all can use
|
|
|
|
// useful for testing multiple instances on same machine
|
|
cli.StringFlag{
|
|
Name: "hostname",
|
|
Value: "",
|
|
Usage: "hostname to use",
|
|
},
|
|
|
|
cli.StringFlag{
|
|
Name: "prefix",
|
|
Usage: "specify a path to the working prefix directory",
|
|
EnvVar: "MGMT_PREFIX",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "tmp-prefix",
|
|
Usage: "request a pseudo-random, temporary prefix to be used",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "allow-tmp-prefix",
|
|
Usage: "allow creation of a new temporary prefix if main prefix is unavailable",
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
Name: "no-watch",
|
|
Usage: "do not update graph under any switch events",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "no-config-watch",
|
|
Usage: "do not update graph on config switch events",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "no-stream-watch",
|
|
Usage: "do not update graph on stream switch events",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "no-deploy-watch",
|
|
Usage: "do not change deploys after an initial deploy",
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
Name: "noop",
|
|
Usage: "globally force all resources into no-op mode",
|
|
},
|
|
cli.IntFlag{
|
|
Name: "sema",
|
|
Value: -1,
|
|
Usage: "globally add a semaphore to all resources with this lock count",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "graphviz, g",
|
|
Value: "",
|
|
Usage: "output file for graphviz data",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "graphviz-filter, gf",
|
|
Value: "",
|
|
Usage: "graphviz filter to use",
|
|
},
|
|
cli.IntFlag{
|
|
Name: "converged-timeout, t",
|
|
Value: -1,
|
|
Usage: "after approximately this many seconds without activity, we're considered to be in a converged state",
|
|
EnvVar: "MGMT_CONVERGED_TIMEOUT",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "converged-timeout-no-exit",
|
|
Usage: "don't exit on converged-timeout",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "converged-status-file",
|
|
Value: "",
|
|
Usage: "file to append the current converged state to, mostly used for testing",
|
|
},
|
|
cli.IntFlag{
|
|
Name: "max-runtime",
|
|
Value: 0,
|
|
Usage: "exit after a maximum of approximately this many seconds",
|
|
EnvVar: "MGMT_MAX_RUNTIME",
|
|
},
|
|
|
|
// if empty, it will startup a new server
|
|
cli.StringSliceFlag{
|
|
Name: "seeds, s",
|
|
Value: &cli.StringSlice{}, // empty slice
|
|
Usage: "default etc client endpoint",
|
|
EnvVar: "MGMT_SEEDS",
|
|
},
|
|
// port 2379 and 4001 are common
|
|
cli.StringSliceFlag{
|
|
Name: "client-urls",
|
|
Value: &cli.StringSlice{},
|
|
Usage: "list of URLs to listen on for client traffic",
|
|
EnvVar: "MGMT_CLIENT_URLS",
|
|
},
|
|
// port 2380 and 7001 are common
|
|
cli.StringSliceFlag{
|
|
Name: "server-urls, peer-urls",
|
|
Value: &cli.StringSlice{},
|
|
Usage: "list of URLs to listen on for server (peer) traffic",
|
|
EnvVar: "MGMT_SERVER_URLS",
|
|
},
|
|
// port 2379 and 4001 are common
|
|
cli.StringSliceFlag{
|
|
Name: "advertise-client-urls",
|
|
Value: &cli.StringSlice{},
|
|
Usage: "list of URLs to listen on for client traffic",
|
|
EnvVar: "MGMT_ADVERTISE_CLIENT_URLS",
|
|
},
|
|
// port 2380 and 7001 are common
|
|
cli.StringSliceFlag{
|
|
Name: "advertise-server-urls, advertise-peer-urls",
|
|
Value: &cli.StringSlice{},
|
|
Usage: "list of URLs to listen on for server (peer) traffic",
|
|
EnvVar: "MGMT_ADVERTISE_SERVER_URLS",
|
|
},
|
|
cli.IntFlag{
|
|
Name: "ideal-cluster-size",
|
|
Value: -1,
|
|
Usage: "ideal number of server peers in cluster; only read by initial server",
|
|
EnvVar: "MGMT_IDEAL_CLUSTER_SIZE",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "no-server",
|
|
Usage: "do not start embedded etcd server (do not promote from client to peer)",
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
Name: "no-pgp",
|
|
Usage: "don't create pgp keys",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "pgp-key-path",
|
|
Value: "",
|
|
Usage: "path for instance key pair",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "pgp-identity",
|
|
Value: "",
|
|
Usage: "default identity used for generation",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "prometheus",
|
|
Usage: "start a prometheus instance",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "prometheus-listen",
|
|
Value: "",
|
|
Usage: "specify prometheus instance binding",
|
|
},
|
|
}
|
|
deployFlags := []cli.Flag{
|
|
// common flags which all can use
|
|
cli.StringSliceFlag{
|
|
Name: "seeds, s",
|
|
Value: &cli.StringSlice{}, // empty slice
|
|
Usage: "default etc client endpoint",
|
|
EnvVar: "MGMT_SEEDS",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "noop",
|
|
Usage: "globally force all resources into no-op mode",
|
|
},
|
|
cli.IntFlag{
|
|
Name: "sema",
|
|
Value: -1,
|
|
Usage: "globally add a semaphore to all resources with this lock count",
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
Name: "no-git",
|
|
Usage: "don't look at git commit id for safe deploys",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "force",
|
|
Usage: "force a new deploy, even if the safety chain would break",
|
|
},
|
|
}
|
|
getFlags := []cli.Flag{
|
|
// common flags which all can use
|
|
cli.BoolFlag{
|
|
Name: "noop",
|
|
Usage: "simulate the download (can't recurse)",
|
|
},
|
|
cli.IntFlag{
|
|
Name: "sema",
|
|
Value: -1, // maximum parallelism
|
|
Usage: "globally add a semaphore to downloads with this lock count",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "update",
|
|
Usage: "update all dependencies to the latest versions",
|
|
},
|
|
}
|
|
|
|
subCommandsRun := []cli.Command{} // run sub commands
|
|
subCommandsDeploy := []cli.Command{} // deploy sub commands
|
|
subCommandsGet := []cli.Command{} // get (download) sub commands
|
|
|
|
names := []string{}
|
|
for name := range gapi.RegisteredGAPIs {
|
|
names = append(names, name)
|
|
}
|
|
sort.Strings(names) // ensure deterministic order when parsing
|
|
for _, x := range names {
|
|
name := x // create a copy in this scope
|
|
fn := gapi.RegisteredGAPIs[name]
|
|
gapiObj := fn()
|
|
|
|
commandRun := cli.Command{
|
|
Name: name,
|
|
Usage: fmt.Sprintf("run using the `%s` frontend", name),
|
|
Action: func(c *cli.Context) error {
|
|
if err := run(c, name, gapiObj); err != nil {
|
|
log.Printf("run: error: %v", err)
|
|
//return cli.NewExitError(err.Error(), 1) // TODO: ?
|
|
return cli.NewExitError("", 1)
|
|
}
|
|
return nil
|
|
},
|
|
Flags: gapiObj.CliFlags(gapi.CommandRun),
|
|
}
|
|
subCommandsRun = append(subCommandsRun, commandRun)
|
|
|
|
commandDeploy := cli.Command{
|
|
Name: name,
|
|
Usage: fmt.Sprintf("deploy using the `%s` frontend", name),
|
|
Action: func(c *cli.Context) error {
|
|
if err := deploy(c, name, gapiObj); err != nil {
|
|
log.Printf("deploy: error: %v", err)
|
|
//return cli.NewExitError(err.Error(), 1) // TODO: ?
|
|
return cli.NewExitError("", 1)
|
|
}
|
|
return nil
|
|
},
|
|
Flags: gapiObj.CliFlags(gapi.CommandDeploy),
|
|
}
|
|
subCommandsDeploy = append(subCommandsDeploy, commandDeploy)
|
|
|
|
if _, ok := gapiObj.(gapi.GettableGAPI); ok {
|
|
commandGet := cli.Command{
|
|
Name: name,
|
|
Usage: fmt.Sprintf("get (download) using the `%s` frontend", name),
|
|
Action: func(c *cli.Context) error {
|
|
if err := get(c, name, gapiObj); err != nil {
|
|
log.Printf("get: error: %v", err)
|
|
//return cli.NewExitError(err.Error(), 1) // TODO: ?
|
|
return cli.NewExitError("", 1)
|
|
}
|
|
return nil
|
|
},
|
|
Flags: gapiObj.CliFlags(gapi.CommandGet),
|
|
}
|
|
subCommandsGet = append(subCommandsGet, commandGet)
|
|
}
|
|
}
|
|
|
|
app := cli.NewApp()
|
|
app.Name = program // App.name and App.version pass these values through
|
|
app.Version = version
|
|
app.Usage = "next generation config management"
|
|
app.Metadata = map[string]interface{}{ // additional flags
|
|
"flags": flags,
|
|
}
|
|
|
|
// if no app.Command is specified
|
|
app.Action = func(c *cli.Context) error {
|
|
// print the license
|
|
if c.Bool("license") {
|
|
license, err := bindata.Asset("../COPYING") // use go-bindata to get the bytes
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Printf("%s", license)
|
|
return nil
|
|
}
|
|
|
|
// print help if no flags are set
|
|
cli.ShowAppHelp(c)
|
|
return nil
|
|
}
|
|
|
|
// global flags
|
|
app.Flags = []cli.Flag{
|
|
cli.BoolFlag{
|
|
Name: "license",
|
|
Usage: "prints the software license",
|
|
},
|
|
}
|
|
|
|
app.Commands = []cli.Command{
|
|
//{
|
|
// Name: gapi.CommandTODO,
|
|
// Aliases: []string{"TODO"},
|
|
// Usage: "TODO",
|
|
// Action: TODO,
|
|
// Flags: TODOFlags,
|
|
//},
|
|
}
|
|
|
|
// run always requires a frontend to start the engine, but if you don't
|
|
// want a graph, you can use the `empty` frontend. The engine backend is
|
|
// agnostic to which frontend is running, in fact, you can deploy with
|
|
// multiple different frontends, one after another, on the same engine.
|
|
if len(subCommandsRun) > 0 {
|
|
commandRun := cli.Command{
|
|
Name: gapi.CommandRun,
|
|
Aliases: []string{"r"},
|
|
Usage: "run",
|
|
Subcommands: subCommandsRun,
|
|
Flags: runFlags,
|
|
}
|
|
app.Commands = append(app.Commands, commandRun)
|
|
}
|
|
|
|
if len(subCommandsDeploy) > 0 {
|
|
commandDeploy := cli.Command{
|
|
Name: gapi.CommandDeploy,
|
|
Aliases: []string{"d"},
|
|
Usage: "deploy",
|
|
Subcommands: subCommandsDeploy,
|
|
Flags: deployFlags,
|
|
}
|
|
app.Commands = append(app.Commands, commandDeploy)
|
|
}
|
|
|
|
if len(subCommandsGet) > 0 {
|
|
commandGet := cli.Command{
|
|
Name: gapi.CommandGet,
|
|
Aliases: []string{"g"},
|
|
Usage: "get",
|
|
Subcommands: subCommandsGet,
|
|
Flags: getFlags,
|
|
}
|
|
app.Commands = append(app.Commands, commandGet)
|
|
}
|
|
|
|
app.EnableBashCompletion = true
|
|
return app.Run(os.Args)
|
|
}
|