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
+14 -3
View File
@@ -17,11 +17,12 @@ const (
GoOSWindows = "windows"
GoOSLinux = "linux"
ServerBinary = "grafana-server"
CLIBinary = "grafana-cli"
BackendBinary = "grafana"
ServerBinary = "grafana-server"
CLIBinary = "grafana-cli"
)
var binaries = []string{ServerBinary, CLIBinary}
var binaries = []string{BackendBinary, ServerBinary, CLIBinary}
func logError(message string, err error) int {
log.Println(message, err)
@@ -64,6 +65,16 @@ func RunCmd() int {
case "setup":
setup(opts.goos)
case "build-backend":
if !opts.isDev {
clean(opts)
}
if err := doBuild("grafana", "./pkg/cmd/grafana", opts); err != nil {
log.Println(err)
return 1
}
case "build-srv", "build-server":
if !opts.isDev {
clean(opts)
+10 -7
View File
@@ -31,12 +31,9 @@ func Package(c *cli.Context) error {
}
cfg := config.Config{
NumWorkers: c.Int("jobs"),
NumWorkers: c.Int("jobs"),
SignPackages: c.Bool("sign"),
}
if err := gpg.LoadGPGKeys(&cfg); err != nil {
return cli.Exit(err, 1)
}
defer gpg.RemoveGPGFiles(cfg)
ctx := context.Background()
@@ -57,8 +54,14 @@ func Package(c *cli.Context) error {
log.Printf("Packaging Grafana version %q, version mode %s, %s edition, variants %s", metadata.GrafanaVersion, releaseMode.Mode,
edition, strings.Join(variantStrs, ","))
if err := gpg.Import(cfg); err != nil {
return cli.Exit(err, 1)
if cfg.SignPackages {
if err := gpg.LoadGPGKeys(&cfg); err != nil {
return cli.Exit(err, 1)
}
defer gpg.RemoveGPGFiles(cfg)
if err := gpg.Import(cfg); err != nil {
return cli.Exit(err, 1)
}
}
p := syncutil.NewWorkerPool(cfg.NumWorkers)
+1
View File
@@ -16,4 +16,5 @@ type Config struct {
PullEnterprise bool
NetworkConcurrency bool
PackageVersion string
SignPackages bool
}
+1 -1
View File
@@ -12,7 +12,7 @@ import (
"github.com/grafana/grafana/pkg/build/golangutils"
)
var binaries = []string{"grafana-server", "grafana-cli"}
var binaries = []string{"grafana", "grafana-server", "grafana-cli"}
const (
SuffixEnterprise2 = "-enterprise2"
+30 -62
View File
@@ -75,9 +75,13 @@ func PackageGrafana(
if err := packageGrafana(ctx, edition, version, grafanaDir, variants, shouldSign, p); err != nil {
return err
}
if err := signRPMPackages(edition, cfg, grafanaDir); err != nil {
return err
if cfg.SignPackages {
if err := signRPMPackages(edition, cfg, grafanaDir); err != nil {
return err
}
}
if err := checksumPackages(grafanaDir, edition); err != nil {
return err
}
@@ -282,6 +286,7 @@ func shaFile(fpath string) error {
// createPackage creates a Linux package.
func createPackage(srcDir string, options linuxPackageOptions) error {
binary := "grafana"
cliBinary := "grafana-cli"
serverBinary := "grafana-server"
@@ -310,10 +315,15 @@ func createPackage(srcDir string, options linuxPackageOptions) error {
}
}
if err := fs.CopyFile(options.cliBinaryWrapperSrc, filepath.Join(packageRoot, "usr", "sbin", cliBinary)); err != nil {
if err := fs.CopyFile(filepath.Join(options.wrapperFilePath, binary),
filepath.Join(packageRoot, "usr", "sbin", binary)); err != nil {
return err
}
if err := fs.CopyFile(filepath.Join(srcDir, "bin", serverBinary),
if err := fs.CopyFile(filepath.Join(options.wrapperFilePath, cliBinary),
filepath.Join(packageRoot, "usr", "sbin", cliBinary)); err != nil {
return err
}
if err := fs.CopyFile(filepath.Join(options.wrapperFilePath, serverBinary),
filepath.Join(packageRoot, "usr", "sbin", serverBinary)); err != nil {
return err
}
@@ -329,19 +339,17 @@ func createPackage(srcDir string, options linuxPackageOptions) error {
if err := fs.CopyRecursive(srcDir, filepath.Join(packageRoot, options.homeDir)); err != nil {
return err
}
homeBinDir := filepath.Join(packageRoot, options.homeBinDir)
if err := os.RemoveAll(homeBinDir); err != nil {
return fmt.Errorf("failed to remove %q: %w", homeBinDir, err)
}
//nolint
if err := os.MkdirAll(homeBinDir, 0o755); err != nil {
return fmt.Errorf("failed to make directory %q: %w", homeBinDir, err)
}
// The grafana-cli binary is exposed through a wrapper to ensure a proper
// configuration is in place. To enable that, we need to store the original
// binary in a separate location to avoid conflicts.
if err := fs.CopyFile(filepath.Join(srcDir, "bin", cliBinary), filepath.Join(homeBinDir, cliBinary)); err != nil {
return err
// remove unneeded binaries, these are exposed via wrappers that provide the needed configuration
for _, fileName := range []string{
cliBinary,
cliBinary + ".md5",
serverBinary,
serverBinary + ".md5",
} {
if err := os.Remove(filepath.Join(packageRoot, options.homeBinDir, fileName)); err != nil {
return fmt.Errorf("failed to remove %q: %w", filepath.Join(options.homeBinDir, fileName), err)
}
}
if err := executeFPM(options, packageRoot, srcDir); err != nil {
@@ -475,43 +483,6 @@ func copyBinaries(grafanaDir, tmpDir string, args grafana.BuildArgs, edition con
return nil
}
// copyScripts copies scripts from grafanaDir into tmpDir.
func copyScripts(grafanaDir, tmpDir string) error {
//nolint
if err := os.MkdirAll(filepath.Join(tmpDir, "scripts"), 0o755); err != nil {
return fmt.Errorf("failed to create dir %q: %w", filepath.Join(tmpDir, "scripts"), err)
}
scriptsDir := filepath.Join(grafanaDir, "scripts")
infos, err := os.ReadDir(scriptsDir)
if err != nil {
return fmt.Errorf("failed to list files in %q: %w", scriptsDir, err)
}
for _, file := range infos {
info, err := file.Info()
if err != nil {
return err
}
if info.IsDir() {
continue
}
if info.Mode()&os.ModeSymlink != 0 {
continue
}
path := ""
path = filepath.Join(scriptsDir, info.Name())
if err := fs.CopyFile(path, filepath.Join(tmpDir, "scripts", info.Name())); err != nil {
return fmt.Errorf("failed to copy %q to %q: %w", path, tmpDir, err)
}
}
return nil
}
// copyConfFiles copies configuration files from grafanaDir into tmpDir.
func copyConfFiles(grafanaDir, tmpDir string) error {
//nolint:gosec
@@ -717,9 +688,6 @@ func realPackageVariant(ctx context.Context, v config.Variant, edition config.Ed
if err := copyBinaries(grafanaDir, tmpDir, args, edition); err != nil {
return err
}
if err := copyScripts(grafanaDir, tmpDir); err != nil {
return err
}
if err := copyConfFiles(grafanaDir, tmpDir); err != nil {
return err
}
@@ -773,7 +741,7 @@ func realPackageVariant(ctx context.Context, v config.Variant, edition config.Ed
initdScriptSrc: filepath.Join(grafanaDir, "packaging", "deb", "init.d", "grafana-server"),
defaultFileSrc: filepath.Join(grafanaDir, "packaging", "deb", "default", "grafana-server"),
systemdFileSrc: filepath.Join(grafanaDir, "packaging", "deb", "systemd", "grafana-server.service"),
cliBinaryWrapperSrc: filepath.Join(grafanaDir, "packaging", "wrappers", "grafana-cli"),
wrapperFilePath: filepath.Join(grafanaDir, "packaging", "wrappers"),
depends: []string{"adduser", "libfontconfig1"},
}); err != nil {
return err
@@ -807,7 +775,7 @@ func realPackageVariant(ctx context.Context, v config.Variant, edition config.Ed
initdScriptSrc: filepath.Join(grafanaDir, "packaging", "rpm", "init.d", "grafana-server"),
defaultFileSrc: filepath.Join(grafanaDir, "packaging", "rpm", "sysconfig", "grafana-server"),
systemdFileSrc: filepath.Join(grafanaDir, "packaging", "rpm", "systemd", "grafana-server.service"),
cliBinaryWrapperSrc: filepath.Join(grafanaDir, "packaging", "wrappers", "grafana-cli"),
wrapperFilePath: filepath.Join(grafanaDir, "packaging", "wrappers"),
// chkconfig is depended on since our systemd service wraps a SysV init script, and that requires chkconfig
depends: []string{"/sbin/service", "chkconfig", "fontconfig", "freetype", "urw-fonts"},
}); err != nil {
@@ -889,7 +857,7 @@ type linuxPackageOptions struct {
initdScriptSrc string
defaultFileSrc string
systemdFileSrc string
cliBinaryWrapperSrc string
wrapperFilePath string
depends []string
}
@@ -929,7 +897,7 @@ func createZip(srcDir, version, variantStr, sfx, grafanaDir string) error {
return fmt.Errorf("failed to create %q: %w", fpath, err)
}
defer func() {
if err := tgt.Close(); err != nil {
if err := tgt.Close(); err != nil && !errors.Is(err, os.ErrClosed) {
log.Println(err)
}
}()
@@ -1045,7 +1013,7 @@ func createTarball(srcDir, version, variantStr, sfx, grafanaDir string) error {
return fmt.Errorf("failed to create %q: %w", fpath, err)
}
defer func() {
if err := tgt.Close(); err != nil {
if err := tgt.Close(); err != nil && !errors.Is(err, os.ErrClosed) {
log.Println(err)
}
}()
+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)
}
+65
View File
@@ -0,0 +1,65 @@
package cmd
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"syscall"
)
func RunGrafanaCmd(subCmd string) int {
curr, err := os.Executable()
if err != nil {
fmt.Println("Error locating executable:", err)
return 1
}
executable := "grafana"
if runtime.GOOS == "windows" {
executable += ".exe"
}
binary := filepath.Join(filepath.Dir(filepath.Clean(curr)), executable)
if _, err := os.Stat(binary); err != nil {
binary, err = exec.LookPath(executable)
if err != nil {
fmt.Printf("Error locating %s: %s\n", executable, err)
return 1
}
}
// windows doesn't support syscall.Exec so we just run the main binary as a command
if runtime.GOOS == "windows" {
// bypassing gosec G204 because we need to build the command programmatically
// nolint:gosec
cmd := exec.Command(binary, append([]string{subCmd}, os.Args[1:]...)...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Env = os.Environ()
err := cmd.Run()
if err == nil {
return 0
}
var exitError *exec.ExitError
if errors.As(err, &exitError) {
return exitError.ExitCode()
}
return 1
}
args := append([]string{"grafana", subCmd}, os.Args[1:]...)
// bypassing gosec G204 because we need to build the command programmatically
// nolint:gosec
execErr := syscall.Exec(binary, args, os.Environ())
if execErr != nil {
fmt.Printf("Error running %s: %s\n", binary, execErr)
return 1
}
return 0
}