khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 17 | package main |
| 18 | |
| 19 | import ( |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 20 | "context" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 21 | "fmt" |
| 22 | "os" |
| 23 | "os/signal" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 24 | "syscall" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 25 | "time" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 26 | |
| 27 | "github.com/opencord/voltha-go/rw_core/config" |
| 28 | c "github.com/opencord/voltha-go/rw_core/core" |
| 29 | "github.com/opencord/voltha-go/rw_core/utils" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame^] | 30 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 31 | "github.com/opencord/voltha-lib-go/v4/pkg/probe" |
| 32 | "github.com/opencord/voltha-lib-go/v4/pkg/version" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 33 | ) |
| 34 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 35 | func waitForExit(ctx context.Context) int { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 36 | signalChannel := make(chan os.Signal, 1) |
| 37 | signal.Notify(signalChannel, |
| 38 | syscall.SIGHUP, |
| 39 | syscall.SIGINT, |
| 40 | syscall.SIGTERM, |
| 41 | syscall.SIGQUIT) |
| 42 | |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 43 | s := <-signalChannel |
| 44 | switch s { |
| 45 | case syscall.SIGHUP, |
| 46 | syscall.SIGINT, |
| 47 | syscall.SIGTERM, |
| 48 | syscall.SIGQUIT: |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 49 | logger.Infow(ctx, "closing-signal-received", log.Fields{"signal": s}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 50 | return 0 |
| 51 | default: |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 52 | logger.Infow(ctx, "unexpected-signal-received", log.Fields{"signal": s}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 53 | return 1 |
| 54 | } |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 55 | } |
| 56 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 57 | func printBanner() { |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 58 | fmt.Println(` `) |
| 59 | fmt.Println(` ______ ______ `) |
| 60 | fmt.Println(`| _ \ \ / / ___|___ _ __ ___ `) |
| 61 | fmt.Println(`| |_) \ \ /\ / / | / _ \| '__/ _ \`) |
| 62 | fmt.Println(`| _ < \ V V /| |__| (_) | | | __/`) |
| 63 | fmt.Println(`|_| \_\ \_/\_/ \____\___/|_| \___|`) |
| 64 | fmt.Println(` `) |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 65 | } |
| 66 | |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 67 | func printVersion() { |
| 68 | fmt.Println("VOLTHA Read-Write Core") |
| 69 | fmt.Println(version.VersionInfo.String(" ")) |
| 70 | } |
| 71 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 72 | func main() { |
| 73 | start := time.Now() |
| 74 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 75 | ctx := context.Background() |
| 76 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 77 | cf := config.NewRWCoreFlags() |
| 78 | cf.ParseCommandArguments() |
| 79 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 80 | // Set the instance ID as the hostname |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 81 | var instanceID string |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 82 | hostName := utils.GetHostName() |
| 83 | if len(hostName) > 0 { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 84 | instanceID = hostName |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 85 | } else { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 86 | logger.Fatal(ctx, "HOSTNAME not set") |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 87 | } |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 88 | |
Don Newton | 8eca462 | 2020-02-10 16:44:48 -0500 | [diff] [blame] | 89 | realMain() |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 90 | |
| 91 | logLevel, err := log.StringToLogLevel(cf.LogLevel) |
| 92 | if err != nil { |
| 93 | panic(err) |
| 94 | } |
| 95 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 96 | //Setup default logger - applies for packages that do not have specific logger set |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 97 | if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": instanceID}); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 98 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 99 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 100 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 101 | // Update all loggers (provisioned via init) with a common field |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 102 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": instanceID}); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 103 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 104 | } |
| 105 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 106 | // Update all loggers to log level specified as input parameter |
Rohan Agrawal | 7f72f0c | 2020-01-14 12:05:51 +0000 | [diff] [blame] | 107 | log.SetAllLogLevel(logLevel) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 108 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 109 | defer func() { |
| 110 | err := log.CleanUp() |
| 111 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 112 | logger.Errorw(ctx, "unable-to-flush-any-buffered-log-entries", log.Fields{"error": err}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 113 | } |
| 114 | }() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 115 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 116 | // Print version / build information and exit |
David K. Bainbridge | f430cd5 | 2019-05-28 15:00:35 -0700 | [diff] [blame] | 117 | if cf.DisplayVersionOnly { |
| 118 | printVersion() |
| 119 | return |
| 120 | } |
| 121 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 122 | // Print banner if specified |
| 123 | if cf.Banner { |
| 124 | printBanner() |
| 125 | } |
| 126 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 127 | logger.Infow(ctx, "rw-core-config", log.Fields{"config": *cf}) |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 128 | |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 129 | // Create a context adding the status update channel |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 130 | ctx, cancel := context.WithCancel(context.Background()) |
| 131 | defer cancel() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 132 | |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 133 | /* |
| 134 | * Create and start the liveness and readiness container management probes. This |
| 135 | * is done in the main function so just in case the main starts multiple other |
| 136 | * objects there can be a single probe end point for the process. |
| 137 | */ |
| 138 | p := &probe.Probe{} |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 139 | go p.ListenAndServe(ctx, cf.ProbeAddress) |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 140 | |
| 141 | // Add the probe to the context to pass to all the services started |
| 142 | probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p) |
| 143 | |
Girish Kumar | f8d4f8d | 2020-08-18 11:45:30 +0000 | [diff] [blame] | 144 | closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(cf.TraceEnabled, cf.TraceAgentAddress, cf.LogCorrelationEnabled) |
Girish Kumar | 33470e8 | 2020-06-15 13:53:13 +0000 | [diff] [blame] | 145 | if err != nil { |
| 146 | logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err}) |
| 147 | } else { |
| 148 | defer closer.Close() |
| 149 | } |
| 150 | |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 151 | // create and start the core |
| 152 | core := c.NewCore(probeCtx, instanceID, cf) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 153 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 154 | code := waitForExit(ctx) |
| 155 | logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 156 | |
| 157 | // Cleanup before leaving |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 158 | core.Stop() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 159 | |
| 160 | elapsed := time.Since(start) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 161 | logger.Infow(ctx, "rw-core-run-time", log.Fields{"core": instanceID, "time": elapsed / time.Second}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 162 | } |