X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=lib%2Fagent%2Fagent.go;h=989c9a0a954fa520ee66ee733c5add53758db85a;hb=89563054ea6305c7270dcc6ab6fa5b286eb5f742;hp=29b0622c9734cbbdc4e8f8bbea0ada95561ff3f9;hpb=97ca1f277dc8b6973d6fa67add5593a9c395ce60;p=src%2Fxds%2Fxds-agent.git diff --git a/lib/agent/agent.go b/lib/agent/agent.go index 29b0622..989c9a0 100644 --- a/lib/agent/agent.go +++ b/lib/agent/agent.go @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2017 "IoT.bzh" + * Author Sebastien Douheret + * + * 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 agent import ( @@ -11,27 +28,29 @@ import ( "time" "github.com/Sirupsen/logrus" - "github.com/codegangsta/cli" "github.com/iotbzh/xds-agent/lib/syncthing" "github.com/iotbzh/xds-agent/lib/xdsconfig" + "github.com/urfave/cli" ) const cookieMaxAge = "3600" // Context holds the Agent context structure type Context struct { - ProgName string - Config *xdsconfig.Config - Log *logrus.Logger - SThg *st.SyncThing - SThgCmd *exec.Cmd - SThgInotCmd *exec.Cmd - - webServer *WebServer + ProgName string + Config *xdsconfig.Config + Log *logrus.Logger + LogLevelSilly bool + LogSillyf func(format string, args ...interface{}) + SThg *st.SyncThing + SThgCmd *exec.Cmd + SThgInotCmd *exec.Cmd + + webServer *WebServer xdsServers map[string]*XdsServer - sessions *Sessions - events *Events - projects *Projects + sessions *Sessions + events *Events + projects *Projects Exit chan os.Signal } @@ -53,15 +72,26 @@ func NewAgent(cliCtx *cli.Context) *Context { } log.Formatter = &logrus.TextFormatter{} + // Support silly logging (printed on log.debug) + sillyVal, sillyLog := os.LookupEnv("XDS_LOG_SILLY") + logSilly := sillyLog && sillyVal == "1" + sillyFunc := func(format string, args ...interface{}) { + if logSilly { + log.Debugf("SILLY: "+format, args...) + } + } + // Define default configuration ctx := Context{ - ProgName: cliCtx.App.Name, - Log: log, - Exit: make(chan os.Signal, 1), + ProgName: cliCtx.App.Name, + Log: log, + LogLevelSilly: logSilly, + LogSillyf: sillyFunc, + Exit: make(chan os.Signal, 1), - webServer: nil, + webServer: nil, xdsServers: make(map[string]*XdsServer), - events: nil, + events: nil, } // register handler on SIGTERM / exit @@ -102,7 +132,10 @@ func (ctx *Context) Run() (int, error) { ctx._logPrint("Logging file for HTTP requests: %s\n", logFileHTTPReq) } - // Create syncthing instance when section "syncthing" is present in config.json + // Create events management + ctx.events = NewEvents(ctx) + + // Create syncthing instance when section "syncthing" is present in agent-config.json if ctx.Config.FileConf.SThgConf != nil { ctx.SThg = st.NewSyncThing(ctx.Config, ctx.Log) } @@ -156,9 +189,6 @@ func (ctx *Context) Run() (int, error) { // Sessions manager ctx.sessions = NewClientSessions(ctx, cookieMaxAge) - // Create events management - ctx.events = NewEvents(ctx) - // Create projects management ctx.projects = NewProjects(ctx, ctx.SThg)