1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.arvados.org/arvados.git/lib/cmd"
15 "git.arvados.org/arvados.git/lib/config"
16 "git.arvados.org/arvados.git/sdk/go/arvados"
17 "git.arvados.org/arvados.git/sdk/go/ctxlog"
18 "github.com/coreos/go-systemd/daemon"
19 "github.com/ghodss/yaml"
20 "github.com/sirupsen/logrus"
21 log "github.com/sirupsen/logrus"
28 // Config specifies server configuration.
32 cluster *arvados.Cluster
35 func newConfig(logger logrus.FieldLogger, arvCfg *arvados.Config) *Config {
37 var cls *arvados.Cluster
39 if cls, err = arvCfg.GetCluster(""); err != nil {
43 cfg.Cache.config = &cfg.cluster.Collections.WebDAVCache
44 cfg.Cache.cluster = cls
45 cfg.Cache.logger = logger
50 // MakeArvadosClient returns an error if this env var isn't
51 // available as a default token (even if we explicitly set a
52 // different token before doing anything with the client). We
53 // set this dummy value during init so it doesn't clobber the
54 // one used by "run test servers".
55 if os.Getenv("ARVADOS_API_TOKEN") == "" {
56 os.Setenv("ARVADOS_API_TOKEN", "xxx")
59 log.SetFormatter(&log.JSONFormatter{
60 TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00",
64 func configure(logger log.FieldLogger, args []string) (*Config, error) {
65 flags := flag.NewFlagSet(args[0], flag.ContinueOnError)
67 loader := config.NewLoader(os.Stdin, logger)
68 loader.SetupFlags(flags)
70 dumpConfig := flags.Bool("dump-config", false,
71 "write current configuration to stdout and exit")
72 getVersion := flags.Bool("version", false,
73 "print version information and exit.")
76 args = loader.MungeLegacyConfigArgs(logger, args[1:], "-legacy-keepweb-config")
77 if ok, code := cmd.ParseFlags(flags, prog, args, "", os.Stderr); !ok {
79 } else if *getVersion {
80 fmt.Printf("%s %s\n", args[0], version)
84 arvCfg, err := loader.Load()
88 cfg := newConfig(logger, arvCfg)
91 out, err := yaml.Marshal(cfg)
95 _, err = os.Stdout.Write(out)
102 initLogger := log.StandardLogger()
103 logger := initLogger.WithField("PID", os.Getpid())
104 cfg, err := configure(logger, os.Args)
107 } else if cfg == nil {
110 logger = logger.WithField("ClusterID", cfg.cluster.ClusterID)
111 logger.Printf("keep-web %s started", version)
112 ctx := ctxlog.Context(context.Background(), logger)
114 if ext := ".txt"; mime.TypeByExtension(ext) == "" {
115 log.Warnf("cannot look up MIME type for %q -- this probably means /etc/mime.types is missing -- clients will see incorrect content types", ext)
118 os.Setenv("ARVADOS_API_HOST", cfg.cluster.Services.Controller.ExternalURL.Host)
119 srv := &server{Config: cfg}
120 if err := srv.Start(ctx, initLogger); err != nil {
123 if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
124 logger.Printf("Error notifying init daemon: %v", err)
126 logger.Println("Listening at", srv.Addr)
127 if err := srv.Wait(); err != nil {