-
Notifications
You must be signed in to change notification settings - Fork 3
/
root.go
328 lines (280 loc) · 9.4 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
Copyright © 2020 MARLIN TEAM <info@marlin.pro>
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 cmd
import (
"errors"
"fmt"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
"github.com/marlinprotocol/ctl2/modules/util"
"github.com/marlinprotocol/ctl2/types"
"github.com/marlinprotocol/ctl2/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/marlinprotocol/ctl2/modules/registry"
log "github.com/sirupsen/logrus"
"github.com/inconshreveable/go-update"
"github.com/marlinprotocol/ctl2/cmd/beacon"
"github.com/marlinprotocol/ctl2/cmd/cp"
"github.com/marlinprotocol/ctl2/cmd/gateway"
"github.com/marlinprotocol/ctl2/cmd/relay"
)
var cfgFile string
var logLevel string
var skipRegistrySync, forcefulRegistrySync bool
var skipMarlinctlUpdateCheck bool
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "marlinctl",
Short: "Marlinctl provides a command line interface for setting up the different components of the Marlin network.",
Long: `Marlinctl provides a command line interface for setting up the different components of the Marlin network.
It can spawn up beacons, gateways, relays on various platforms and runtimes.`,
Version: version.RootCmdVersion,
Run: func(cmd *cobra.Command, args []string) {
// NOP for upgrade
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
lvl, err := log.ParseLevel(logLevel)
if err != nil {
log.Error("Invalid loglevel: ", logLevel)
os.Exit(1)
}
log.SetLevel(lvl)
// Uncomment following line to show CMD tree
// showCmdTree(cmd.Root(), "")
err = readConfig()
var configuredRegistries []types.Registry
err = viper.UnmarshalKey("registries", &configuredRegistries)
if err != nil {
log.Error("Error reading registries from cfg file: ", err)
os.Exit(1)
}
registry.SetupGlobalRegistry(configuredRegistries)
currentTime := time.Now().Unix()
lastSyncTime := viper.GetTime("last_registry_sync").Unix()
if skipRegistrySync == false && (currentTime-lastSyncTime) > 15*60 || forcefulRegistrySync {
err = registry.GlobalRegistry.Sync()
if err != nil {
log.Error("Error while syncing registry: " + err.Error())
os.Exit(1)
}
viper.Set("last_registry_sync", time.Now())
err := viper.WriteConfig()
if err != nil {
log.Error("Error while writing sync time to state: " + err.Error())
os.Exit(1)
}
} else {
log.Debug("Skipping registry sync procedure. Metrics: curr: ", currentTime, " lst: ", lastSyncTime, " skip,force: ", skipRegistrySync, " ", forcefulRegistrySync)
}
if !skipMarlinctlUpdateCheck {
hasUpgraded, err := checkMarlinctlUpdates()
if err != nil {
log.Error("Error while upgrading marlinctl: " + err.Error())
os.Exit(1)
}
if hasUpgraded {
log.Info("Starting upgraded marlinctl")
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
fmt.Println("Unable to start updated cli. Retry running same command ", err)
}
err = cmd.Wait()
if err != nil {
fmt.Println("error occured: ", err)
}
os.Exit(2)
}
} else {
log.Debug("Skipping marlinctl update check")
}
err = util.ChownRmarlinctlDir()
if err != nil {
log.Error("Error while chowning .marlin " + err.Error())
os.Exit(1)
}
},
}
func showCmdTree(cmd *cobra.Command, lvl string) {
fmt.Println(lvl + "-" + cmd.Use)
for _, c := range cmd.Commands() {
showCmdTree(c, lvl+" |")
}
}
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
RootCmd.AddCommand(gateway.GatewayCmd)
RootCmd.AddCommand(beacon.BeaconCmd)
RootCmd.AddCommand(relay.RelayCmd)
RootCmd.AddCommand(cp.CpCmd)
RootCmd.PersistentFlags().BoolVar(&skipRegistrySync, "skip-sync", false, "skip registry sync during run")
RootCmd.PersistentFlags().BoolVar(&forcefulRegistrySync, "registry-sync", false, "forceful registry sync from remote. May be used to check for upgrades.")
RootCmd.PersistentFlags().BoolVar(&skipMarlinctlUpdateCheck, "skip-update-check", false, "skip update check during run")
RootCmd.PersistentFlags().StringVar(&logLevel, "loglevel", "info", "marlinctl loglevel (default is INFO)")
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.marlin/ctl/state.yaml)")
}
// initConfig reads in config file and ENV variables if set.
func readConfig() error {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
home, err := util.GetUser()
if err != nil {
return err
}
viper.SetConfigFile(home.HomeDir + "/.marlin/ctl/state.yaml")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
err := viper.ReadInConfig()
if err == nil {
var cfgVersionOnDisk = viper.GetInt("config_version")
if cfgVersionOnDisk != version.CfgVersion {
return errors.New("Cannot use the given config file as it does not match marlinctl's cfgversion. Wanted " + strconv.Itoa(version.CfgVersion) + " but found " + strconv.Itoa(cfgVersionOnDisk))
}
} else {
log.Warning("No config file available on local machine. Creating default for you.")
err = setupDefaultConfig()
if err != nil {
return err
}
home, err := util.GetUser()
if err != nil {
return err
}
viper.SetConfigFile(home.HomeDir + "/.marlin/ctl/state.yaml")
return viper.ReadInConfig()
}
return nil
}
// VIPER defaults ------------------------
var defaultReleaseUpstreams = []types.Registry{
types.Registry{
Name: "public",
Link: "https://github.com/marlinprotocol/releases.git",
Branch: "public",
Enabled: true,
},
types.Registry{
Name: "beta",
Link: "https://github.com/marlinprotocol/releases.git",
Branch: "beta",
Enabled: true,
},
types.Registry{
Name: "alpha",
Link: "https://github.com/marlinprotocol/releases.git",
Branch: "alpha",
Enabled: false,
},
types.Registry{
Name: "dev",
Link: "https://github.com/marlinprotocol/releases.git",
Branch: "dev",
Enabled: false,
},
}
// --------------------------------------
func setupDefaultConfig() error {
home, err := util.GetUser()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
location := home.HomeDir + "/.marlin/ctl/state.yaml"
lSplice := strings.Split(location, "/")
var dirPath string
for i := 0; i < len(lSplice)-1; i++ {
dirPath = dirPath + "/" + lSplice[i]
}
err = util.CreateDirPathIfNotExists(dirPath)
if err != nil {
log.Error("Error while creating directory ", dirPath, " ", err.Error())
}
viper.SetConfigFile(location)
for i := 0; i < len(defaultReleaseUpstreams); i++ {
defaultReleaseUpstreams[i].Local = home.HomeDir + "/.marlin/ctl/registries/" + defaultReleaseUpstreams[i].Branch
}
var defaultProjectRuntime = "linux-amd64.supervisor"
if runtime.GOOS+"-"+runtime.GOARCH != "linux-amd64" {
return errors.New("don't know how to service non linux-amd64 system as of now.")
}
viper.Set("config_version", version.CfgVersion)
viper.Set("homedir", home.HomeDir+"/.marlin/ctl/storage")
viper.Set("registries", defaultReleaseUpstreams)
viper.Set("marlinctl", types.Project{
Subscription: []string{"public"},
UpdatePolicy: "minor",
CurrentVersion: version.ApplicationVersion,
Storage: home.HomeDir + "/.marlin/ctl/storage/projects/marlinctl",
Runtime: runtime.GOOS + "-" + runtime.GOARCH,
ForcedRuntime: false,
AdditionalInfo: map[string]interface{}{
"defaultprojectruntime": defaultProjectRuntime,
"defaultprojectupdatepolicy": "minor",
},
})
err = viper.WriteConfig()
if err != nil {
log.Error("Error while writing config file to ", location, " ", err.Error())
}
log.Info("Default marlinctl config written to disk successfully to ", location)
return nil
}
func checkMarlinctlUpdates() (bool, error) {
ver, err := registry.GlobalRegistry.GetVersionToRun("marlinctl", "", "")
if err != nil {
return false, err
}
if version.ApplicationVersion == ver.Version {
log.Debug("Latest marlinctl described upstream is current marlinctl's version. No updates to do.")
return false, nil
}
log.Info("MarlinCTL needs to upgrade, going from ", version.ApplicationVersion, " to ", ver.Version)
executableURL := ver.RunnerData.(map[string]interface{})["executable"].(string)
executableChecksum := ver.RunnerData.(map[string]interface{})["checksum"].(string)
tempDownloadLoc := "/tmp/marlinctl.tempdownload." + strconv.FormatInt(time.Now().Unix(), 10)
log.Debug("Downloading marlinctl to ", tempDownloadLoc)
err = util.DownloadFile(tempDownloadLoc, executableURL)
if err != nil {
return false, err
}
err = util.VerifyChecksum(tempDownloadLoc, executableChecksum)
if err != nil {
return false, err
}
log.Debug("Patching start")
updateFile, err := os.Open(tempDownloadLoc)
if err != nil {
return false, nil
}
defer updateFile.Close()
err = update.Apply(updateFile, update.Options{})
if err != nil {
return false, err
}
log.Debug("Patching complete")
return true, nil
}