Skip to content

Commit

Permalink
dispatcher: initial Go commit and env setup (#2192)
Browse files Browse the repository at this point in the history
  • Loading branch information
scrye authored Dec 11, 2018
1 parent 5593821 commit 1ab44bf
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 13 deletions.
4 changes: 2 additions & 2 deletions go/border/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func main() {
os.Exit(1)
}
defer env.CleanupLog()
if err := env.LogSvcStarted(common.BR, *id); err != nil {
if err := env.LogAppStarted(common.BR, *id); err != nil {
log.Crit("LogSvcStart failed", "err", err)
log.Flush()
os.Exit(1)
Expand Down Expand Up @@ -103,7 +103,7 @@ func setupSignals() {
go func() {
defer log.LogPanicAndExit()
<-sig
env.LogSvcStopped(common.BR, *id)
env.LogAppStopped(common.BR, *id)
profile.Stop()
log.Flush()
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion go/cert_srv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func realMain() int {
return 1
}
defer env.CleanupLog()
defer env.LogSvcStopped(common.CS, config.General.ID)
defer env.LogAppStopped(common.CS, config.General.ID)
// Setup the state and the messenger
if err := setup(); err != nil {
log.Crit("Setup failed", "err", err)
Expand Down
2 changes: 1 addition & 1 deletion go/cert_srv/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func setupBasic() error {
if err := env.InitLogging(&config.Logging); err != nil {
return err
}
return env.LogSvcStarted(common.CS, config.General.ID)
return env.LogAppStarted(common.CS, config.General.ID)
}

// setup initializes the config and sets the messenger.
Expand Down
30 changes: 30 additions & 0 deletions go/godispatcher/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2018 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package config contains the configuration of the SCION dispatcher.
package config

import "github.com/scionproto/scion/go/lib/common"

type Config struct {
// ID of the Dispatcher (required)
ID string
}

func (cfg Config) Validate() error {
if cfg.ID == "" {
return common.NewBasicError("ID must be set", nil)
}
return nil
}
43 changes: 43 additions & 0 deletions go/godispatcher/internal/config/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2018 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package config

const Sample = `
[dispatcher]
# ID of the Dispatcher (required)
ID = "disp"
[logging]
[logging.file]
# Location of the logging file.
Path = "/var/log/scion/dispatcher.log"
# File logging level (trace|debug|info|warn|error|crit) (default debug)
Level = "debug"
# Max size of log file in MiB (default 50)
# Size = 50
# Max age of log file in days (default 7)
# MaxAge = 7
# How frequently to flush to the log file, in seconds. If 0, all messages
# are immediately flushed. If negative, messages are never flushed
# automatically. (default 5)
FlushInterval = 5
[logging.console]
# Console logging level (trace|debug|info|warn|error|crit) (default crit)
Level = "crit"
`
73 changes: 73 additions & 0 deletions go/godispatcher/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2018 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"flag"
"fmt"
"os"

"github.com/BurntSushi/toml"

"github.com/scionproto/scion/go/godispatcher/internal/config"
"github.com/scionproto/scion/go/lib/env"
"github.com/scionproto/scion/go/lib/log"
)

type Config struct {
Logging env.Logging
Metrics env.Metrics
Dispatcher config.Config
}

var (
cfg Config
)

func main() {
os.Exit(realMain())
}

func realMain() int {
env.AddFlags()
flag.Parse()
if returnCode, ok := env.CheckFlags(config.Sample); !ok {
return returnCode
}
if err := setupBasic(); err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
defer env.CleanupLog()
defer env.LogAppStopped("Dispatcher", cfg.Dispatcher.ID)

fatalC := make(chan error, 1)
cfg.Metrics.StartPrometheus(fatalC)
err := <-fatalC
// Prometheus encountered a fatal error, thus we exit.
log.Crit("Unable to listen and serve", "err", err)
return 1
}

func setupBasic() error {
if _, err := toml.DecodeFile(env.ConfigFile(), &cfg); err != nil {
return err
}
if err := env.InitLogging(&cfg.Logging); err != nil {
return err
}
env.LogAppStarted("Dispatcher", cfg.Dispatcher.ID)
return nil
}
7 changes: 4 additions & 3 deletions go/lib/env/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ func setupFileLogging(cfg *Logging) error {
return nil
}

// LogSvcStarted should be called by services as soon as logging is initialized.
func LogSvcStarted(svcType, elemId string) error {
// LogAppStarted should be called by applications as soon as logging is
// initialized.
func LogAppStarted(svcType, elemId string) error {
inDocker, err := RunsInDocker()
if err != nil {
return common.NewBasicError("Unable to determine if running in docker", err)
Expand All @@ -133,7 +134,7 @@ func VersionInfo() string {
)
}

func LogSvcStopped(svcType, elemId string) {
func LogAppStopped(svcType, elemId string) {
log.Info(fmt.Sprintf("=====================> Service stopped %s %s", svcType, elemId))
}

Expand Down
4 changes: 2 additions & 2 deletions go/path_srv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func realMain() int {
return 1
}
defer env.CleanupLog()
defer env.LogSvcStopped(common.PS, config.General.ID)
defer env.LogAppStopped(common.PS, config.General.ID)
if err := setup(); err != nil {
log.Crit("Setup failed", "err", err)
return 1
Expand Down Expand Up @@ -193,7 +193,7 @@ func setupBasic() error {
if err := env.InitLogging(&config.Logging); err != nil {
return err
}
return env.LogSvcStarted(common.PS, config.General.ID)
return env.LogAppStarted(common.PS, config.General.ID)
}

func setup() error {
Expand Down
4 changes: 2 additions & 2 deletions go/sciond/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func realMain() int {
return 1
}
defer env.CleanupLog()
defer env.LogSvcStopped("SD", config.General.ID)
defer env.LogAppStopped("SD", config.General.ID)
if err := setup(); err != nil {
log.Crit("Setup failed", "err", err)
return 1
Expand Down Expand Up @@ -170,7 +170,7 @@ func setupBasic() error {
if err := env.InitLogging(&config.Logging); err != nil {
return err
}
return env.LogSvcStarted("SD", config.General.ID)
return env.LogAppStarted("SD", config.General.ID)
}

func setup() error {
Expand Down
4 changes: 2 additions & 2 deletions go/sig/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func realMain() int {
return 1
}
defer env.CleanupLog()
defer env.LogSvcStopped("SIG", cfg.Sig.ID)
defer env.LogAppStopped("SIG", cfg.Sig.ID)
if err := validateConfig(); err != nil {
log.Crit("Validation of config failed", "err", err)
return 1
Expand Down Expand Up @@ -126,7 +126,7 @@ func setupBasic() error {
if err := env.InitLogging(&cfg.Logging); err != nil {
return err
}
return env.LogSvcStarted("SIG", cfg.Sig.ID)
return env.LogAppStarted("SIG", cfg.Sig.ID)
}

func validateConfig() error {
Expand Down

0 comments on commit 1ab44bf

Please sign in to comment.