Server: Switch from separate server & cli to a unified grafana binary (#58286)

* avoid the need for a second bulky binary for grafana-cli

* look for grafana-server in $PATH as well as same directory

* implement unified "grafana" command

* update dockerfiles, fix grafana-cli -v

* update packaging to work with single binary

- add wrapper scripts for grafana and grafana-server
- update and sync package files
- implement --sign flag of build package command
- stop packaging scripts folder, they are not useful for end users
- add support for --configOverrides in server command
- remove unused nfpm.yaml config file

* windows support
This commit is contained in:
Dan Cech
2022-11-22 11:53:43 -05:00
committed by GitHub
parent 824a562b03
commit de99ce139c
28 changed files with 480 additions and 330 deletions
+16 -46
View File
@@ -1,11 +1,9 @@
package commands
import (
"fmt"
"os"
"runtime"
"github.com/fatih/color"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
@@ -13,18 +11,10 @@ import (
)
// RunCLI is the entrypoint for the grafana-cli command. It returns the exit code for the grafana-cli program.
func RunCLI(version string) int {
setupLogging()
app := &cli.App{
Name: "Grafana CLI",
Authors: []*cli.Author{
{
Name: "Grafana Project",
Email: "hello@grafana.com",
},
},
Version: version,
func CLICommand(version string) *cli.Command {
return &cli.Command{
Name: "cli",
Usage: "run the grafana cli",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pluginsDir",
@@ -64,39 +54,19 @@ func RunCLI(version string) int {
Name: "config",
Usage: "Path to config file",
},
cli.VersionFlag,
},
Commands: Commands,
CommandNotFound: cmdNotFound,
}
Subcommands: Commands,
Before: func(c *cli.Context) error {
// backward-compatible handling for cli version flag
if c.Bool("version") {
cli.ShowVersion(c)
os.Exit(0)
}
app.Before = func(c *cli.Context) error {
services.Init(version, c.Bool("insecure"), c.Bool("debug"))
return nil
}
if err := app.Run(os.Args); err != nil {
logger.Errorf("%s: %s %s\n", color.RedString("Error"), color.RedString("✗"), err)
return 1
}
return 0
}
func setupLogging() {
for _, f := range os.Args {
if f == "-d" || f == "--debug" || f == "-debug" {
logger.SetDebug(true)
}
logger.SetDebug(c.Bool("debug"))
services.Init(version, c.Bool("insecure"), c.Bool("debug"))
return nil
},
}
}
func cmdNotFound(c *cli.Context, command string) {
fmt.Printf(
"%s: '%s' is not a %s command. See '%s --help'.\n",
c.App.Name,
command,
c.App.Name,
os.Args[0],
)
os.Exit(1)
}
+2 -1
View File
@@ -78,7 +78,8 @@ func initCfg(cmd *utils.ContextCommandLine) (*setting.Cfg, error) {
cfg, err := setting.NewCfgFromArgs(setting.CommandLineArgs{
Config: cmd.ConfigFile(),
HomePath: cmd.HomePath(),
Args: append(configOptions, cmd.Args().Slice()...), // tailing arguments have precedence over the options string
// tailing arguments have precedence over the options string
Args: append(configOptions, cmd.Args().Slice()...),
})
if err != nil {
+2 -5
View File
@@ -3,12 +3,9 @@ package main
import (
"os"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/commands"
"github.com/grafana/grafana/pkg/util/cmd"
)
// Version is overridden by build flags
var version = "main"
func main() {
os.Exit(commands.RunCLI(version))
os.Exit(cmd.RunGrafanaCmd("cli"))
}
+24 -8
View File
@@ -13,6 +13,7 @@ import (
"runtime/debug"
"runtime/trace"
"strconv"
"strings"
"syscall"
"time"
@@ -32,6 +33,7 @@ type ServerOptions struct {
Commit string
BuildBranch string
BuildStamp string
Args []string
}
type exitWithCode struct {
@@ -54,6 +56,8 @@ func RunServer(opt ServerOptions) int {
pidFile = serverFs.String("pidfile", "", "path to pid file")
packaging = serverFs.String("packaging", "unknown", "describes the way Grafana was installed")
configOverrides = serverFs.String("configOverrides", "", "Configuration options to override defaults as a string. e.g. cfg:default.paths.log=/dev/null")
v = serverFs.Bool("v", false, "prints current version and exits")
vv = serverFs.Bool("vv", false, "prints current version, all dependencies and exits")
profile = serverFs.Bool("profile", false, "Turn on pprof profiling")
@@ -63,7 +67,7 @@ func RunServer(opt ServerOptions) int {
tracingFile = serverFs.String("tracing-file", "trace.out", "Define tracing output file")
)
if err := serverFs.Parse(os.Args[1:]); err != nil {
if err := serverFs.Parse(opt.Args); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
return 1
}
@@ -108,7 +112,7 @@ func RunServer(opt ServerOptions) int {
}()
}
if err := executeServer(*configFile, *homePath, *pidFile, *packaging, traceDiagnostics, opt); err != nil {
if err := executeServer(*configFile, *homePath, *pidFile, *packaging, *configOverrides, traceDiagnostics, opt); err != nil {
code := 1
var ewc exitWithCode
if errors.As(err, &ewc) {
@@ -124,7 +128,7 @@ func RunServer(opt ServerOptions) int {
return 0
}
func executeServer(configFile, homePath, pidFile, packaging string, traceDiagnostics *tracingDiagnostics, opt ServerOptions) error {
func executeServer(configFile, homePath, pidFile, packaging, configOverrides string, traceDiagnostics *tracingDiagnostics, opt ServerOptions) error {
defer func() {
if err := log.Close(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to close log: %s\n", err)
@@ -185,11 +189,23 @@ func executeServer(configFile, homePath, pidFile, packaging string, traceDiagnos
fmt.Println("Grafana server is running with elevated privileges. This is not recommended")
}
s, err := server.Initialize(setting.CommandLineArgs{
Config: configFile, HomePath: homePath, Args: serverFs.Args(),
}, server.Options{
PidFile: pidFile, Version: opt.Version, Commit: opt.Commit, BuildBranch: opt.BuildBranch,
}, api.ServerOptions{})
configOptions := strings.Split(configOverrides, " ")
s, err := server.Initialize(
setting.CommandLineArgs{
Config: configFile,
HomePath: homePath,
// tailing arguments have precedence over the options string
Args: append(configOptions, serverFs.Args()...),
},
server.Options{
PidFile: pidFile,
Version: opt.Version,
Commit: opt.Commit,
BuildBranch: opt.BuildBranch,
},
api.ServerOptions{},
)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to start grafana. error: %s\n", err.Error())
return err
+2 -13
View File
@@ -3,20 +3,9 @@ package main
import (
"os"
"github.com/grafana/grafana/pkg/cmd/grafana-server/commands"
"github.com/grafana/grafana/pkg/util/cmd"
)
// The following variables cannot be constants, since they can be overridden through the -X link flag
var version = "9.2.0"
var commit = "NA"
var buildBranch = "main"
var buildstamp string
func main() {
os.Exit(commands.RunServer(commands.ServerOptions{
Version: version,
Commit: commit,
BuildBranch: buildBranch,
BuildStamp: buildstamp,
}))
os.Exit(cmd.RunGrafanaCmd("server"))
}
+68
View File
@@ -0,0 +1,68 @@
package main
import (
"fmt"
"os"
"github.com/fatih/color"
gcli "github.com/grafana/grafana/pkg/cmd/grafana-cli/commands"
gsrv "github.com/grafana/grafana/pkg/cmd/grafana-server/commands"
"github.com/urfave/cli/v2"
)
// The following variables cannot be constants, since they can be overridden through the -X link flag
var version = "9.2.0"
var commit = "NA"
var buildBranch = "main"
var buildstamp string
func main() {
app := &cli.App{
Name: "grafana",
Usage: "Grafana server and command line interface",
Authors: []*cli.Author{
{
Name: "Grafana Project",
Email: "hello@grafana.com",
},
},
Version: version,
Commands: []*cli.Command{
gcli.CLICommand(version),
{
Name: "server",
Usage: "server <server options>",
Action: func(context *cli.Context) error {
os.Exit(gsrv.RunServer(gsrv.ServerOptions{
Version: version,
Commit: commit,
BuildBranch: buildBranch,
BuildStamp: buildstamp,
Args: context.Args().Slice(),
}))
return nil
},
SkipFlagParsing: true,
},
},
CommandNotFound: cmdNotFound,
}
if err := app.Run(os.Args); err != nil {
fmt.Printf("%s: %s %s\n", color.RedString("Error"), color.RedString("✗"), err)
os.Exit(1)
}
os.Exit(0)
}
func cmdNotFound(c *cli.Context, command string) {
fmt.Printf(
"%s: '%s' is not a %s command. See '%s --help'.\n",
c.App.Name,
command,
c.App.Name,
os.Args[0],
)
os.Exit(1)
}