Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix config update #1159

Merged
merged 5 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions cmd/skywire-cli/commands/config/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func init() {
genConfigCmd.Flags().BoolVarP(&stdout, "stdout", "n", false, "write config to stdout")
hiddenflags = append(hiddenflags, "stdout")
genConfigCmd.Flags().StringVarP(&output, "out", "o", "", "output config: "+skyenv.ConfigName)
genConfigCmd.Flags().BoolVarP(&pkgEnv, "pkg", "p", false, "use paths for package: "+skyenv.SkywirePath)
genConfigCmd.Flags().BoolVarP(&pkgEnv, "pkg", "p", false, skyenv.Ptext)
homepath := skyenv.HomePath()
if homepath != "" {
genConfigCmd.Flags().BoolVarP(&usrEnv, "user", "u", false, "use paths for user space: "+homepath)
Expand Down Expand Up @@ -204,15 +204,10 @@ var genConfigCmd = &cobra.Command{
}
//fetch the service endpoints
services = visorconfig.Fetch(mLog, serviceConfURL, stdout)
// skywire-cli config gen -ip || skywire-cli config gen -p
if !stdout && outunset && (selectedOS == "linux") {
if pkgEnv {
if hypervisor {
//default config hypervisor
configName = skyenv.Skywirejson
} else {
configName = skyenv.Skywirevisorjson
}
// skywire-cli config gen -p
if !stdout && outunset {
if pkgEnv && (selectedOS == "linux") {
configName = skyenv.Configjson
confPath = skyenv.SkywirePath + "/" + configName
}
if usrEnv {
Expand Down
119 changes: 97 additions & 22 deletions cmd/skywire-cli/commands/config/update/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package update

import (
"encoding/json"
"os"
"os/user"
"path/filepath"
"strings"

Expand Down Expand Up @@ -38,56 +40,76 @@ var (
setPublicAutoconnect string
serviceConfURL string
minHops int
svcconf = strings.ReplaceAll(utilenv.ServiceConfAddr, "http://", "") //skyenv.DefaultServiceConfAddr
testconf = strings.ReplaceAll(utilenv.TestServiceConfAddr, "http://", "") //skyenv.DefaultServiceConfAddr
conf *visorconfig.V1
svcconf = strings.ReplaceAll(utilenv.ServiceConfAddr, "http://", "")
testconf = strings.ReplaceAll(utilenv.TestServiceConfAddr, "http://", "")
root bool
usr bool
hiddenflags []string
)

var logger = logging.MustGetLogger("skywire-cli")

func init() {
usrLvl, err := user.Current()
if err != nil {
panic(err)
}
if usrLvl.Username == "root" {
root = true
}
RootCmd.Flags().SortFlags = false
RootCmd.Flags().BoolVarP(&updateEndpoints, "endpoints", "a", false, "update server endpoints")
RootCmd.Flags().StringVarP(&serviceConfURL, "url", "b", "", "service config URL: "+svcconf)
RootCmd.Flags().BoolVarP(&testEnv, "testenv", "t", false, "use test deployment: "+testconf)
RootCmd.Flags().StringVar(&setPublicAutoconnect, "public-autoconn", "", "change public autoconnect configuration")
RootCmd.Flags().IntVar(&minHops, "set-minhop", -1, "change min hops value")
RootCmd.PersistentFlags().StringVarP(&input, "input", "i", "", "path of input config file.")
hiddenflags = append(hiddenflags, "input")
RootCmd.PersistentFlags().StringVarP(&output, "output", "o", "", "config file to output")
RootCmd.PersistentFlags().BoolVarP(&pkg, "pkg", "p", false, "read from /opt/skywire/skywire.json")
if root {
if _, err := os.Stat(skyenv.SkywirePath + "/" + skyenv.Configjson); err == nil {
RootCmd.PersistentFlags().BoolVarP(&pkg, "pkg", "p", false, "update package config "+skyenv.SkywirePath+"/"+skyenv.Configjson)
hiddenflags = append(hiddenflags, "pkg")
}
}
if !root {
if _, err := os.Stat(skyenv.HomePath() + "/" + skyenv.ConfigName); err == nil {
RootCmd.PersistentFlags().BoolVarP(&usr, "user", "u", false, "update config at: $HOME/"+skyenv.ConfigName)
}
}

for _, j := range hiddenflags {
RootCmd.Flags().MarkHidden(j) //nolint
}
}

// RootCmd contains commands that update the config
var RootCmd = &cobra.Command{
Use: "update",
Short: "update a config file",
PreRun: func(_ *cobra.Command, _ []string) {
//set default output filename
if output == "" {
output = skyenv.ConfigName
}
var err error
if output, err = filepath.Abs(output); err != nil {
logger.WithError(err).Fatal("Invalid config output.")
if updateEndpoints && (serviceConfURL == "") {
if !testEnv {
serviceConfURL = svcconf
} else {
serviceConfURL = testconf
}
}
setDefaults()
checkConfig()
},
Run: func(cmd *cobra.Command, _ []string) {
mLog := logging.NewMasterLogger()
mLog.SetLevel(logrus.InfoLevel)
if cmd.Flags().Changed("serviceConfURL") {
updateEndpoints = true
}
if input == "" {
input = output
}
conf, ok := visorconfig.ReadFile(input)
if ok != nil {
mLog.WithError(ok).Fatal("Failed to parse config.")
}

conf = initUpdate()
if updateEndpoints {
if testEnv {
serviceConfURL = testconf
}
mLog := logging.NewMasterLogger()
mLog.SetLevel(logrus.InfoLevel)
services := visorconfig.Fetch(mLog, serviceConfURL, stdout)
conf.Dmsg = &dmsgc.DmsgConfig{
Discovery: services.DmsgDiscovery, //utilenv.DefaultDmsgDiscAddr,
Expand All @@ -107,7 +129,6 @@ var RootCmd = &cobra.Command{
Addr: services.UptimeTracker, //utilenv.DefaultUptimeTrackerAddr,
}
conf.StunServers = services.StunServers //utilenv.GetStunServers()

}

switch setPublicAutoconnect {
Expand All @@ -120,7 +141,6 @@ var RootCmd = &cobra.Command{
default:
logger.Fatal("Unrecognized public autoconnect value: ", setPublicAutoconnect)
}

if minHops >= 0 {
conf.Routing.MinHops = uint16(minHops)
}
Expand Down Expand Up @@ -169,3 +189,58 @@ func saveConfig(conf *visorconfig.V1) {
}
logger.Infof("Updated file '%s' to: %s", output, j)
}

func initUpdate() (conf *visorconfig.V1) {
mLog := logging.NewMasterLogger()
mLog.SetLevel(logrus.InfoLevel)
if input == "" {
input = output
}
conf, ok := visorconfig.ReadFile(input)
if ok != nil {
mLog.WithError(ok).Fatal("Failed to parse config.")
}
cc, err := visorconfig.NewCommon(mLog, output, &conf.SK)
if err != nil {
mLog.WithError(ok).Fatal("Failed to regenerate config.")
}
conf.Common = cc
return conf
}

func checkConfig() {
//set default output filename
if output == "" {
output = skyenv.ConfigName
}
var err error
if output, err = filepath.Abs(output); err != nil {
logger.WithError(err).Fatal("Invalid config output.")
}
if _, err := os.Stat(output); err != nil {
logger.WithError(err).Fatal("Invalid config output.")
}
if (input != output) && (input != "") {
if input, err = filepath.Abs(input); err != nil {
logger.WithError(err).Fatal("Invalid config input.")
}
if _, err := os.Stat(input); err != nil {
logger.WithError(err).Fatal("Invalid config input.")
}
}
}

func setDefaults() {
if (input != "") && (output == "") {
output = input
}
if pkg {
output = skyenv.SkywirePath + "/" + skyenv.Configjson
input = skyenv.SkywirePath + "/" + skyenv.Configjson
}
if usr {
output = skyenv.HomePath() + "/" + skyenv.ConfigName
input = skyenv.HomePath() + "/" + skyenv.ConfigName
}

}
72 changes: 16 additions & 56 deletions cmd/skywire-cli/commands/config/update/update.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package update

import (
"path/filepath"
"strings"

"github.com/sirupsen/logrus"
coinCipher "github.com/skycoin/skycoin/src/cipher"
"github.com/skycoin/skycoin/src/util/logging"
"github.com/spf13/cobra"

"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire/pkg/visor/visorconfig"
)

var pkg bool
Expand Down Expand Up @@ -50,18 +46,11 @@ var hyperVisorUpdateCmd = &cobra.Command{
Use: "hv",
Short: "update hypervisor config",
PreRun: func(_ *cobra.Command, _ []string) {
var err error
if output, err = filepath.Abs(output); err != nil {
logger.WithError(err).Fatal("Invalid config output.")
}
setDefaults()
checkConfig()
},
Run: func(_ *cobra.Command, _ []string) {
mLog := logging.NewMasterLogger()
mLog.SetLevel(logrus.InfoLevel)
conf, ok := visorconfig.ReadFile(input)
if ok != nil {
mLog.WithError(ok).Fatal("Failed to parse config.")
}
conf = initUpdate()
if addHypervisorPKs != "" {
keys := strings.Split(addHypervisorPKs, ",")
for _, key := range keys {
Expand All @@ -83,18 +72,11 @@ var skySocksClientUpdateCmd = &cobra.Command{
Use: "sc",
Short: "update skysocks-client config",
PreRun: func(_ *cobra.Command, _ []string) {
var err error
if output, err = filepath.Abs(output); err != nil {
logger.WithError(err).Fatal("Invalid config output.")
}
setDefaults()
checkConfig()
},
Run: func(_ *cobra.Command, _ []string) {
mLog := logging.NewMasterLogger()
mLog.SetLevel(logrus.InfoLevel)
conf, ok := visorconfig.ReadFile(input)
if ok != nil {
mLog.WithError(ok).Fatal("Failed to parse config.")
}
conf = initUpdate()
if addSkysocksClientSrv != "" {
keyParsed, err := coinCipher.PubKeyFromHex(strings.TrimSpace(addSkysocksClientSrv))
if err != nil {
Expand All @@ -113,18 +95,12 @@ var skySocksServerUpdateCmd = &cobra.Command{
Use: "ss",
Short: "update skysocks-server config",
PreRun: func(_ *cobra.Command, _ []string) {
var err error
if output, err = filepath.Abs(output); err != nil {
logger.WithError(err).Fatal("Invalid config output.")
}
setDefaults()
checkConfig()
},
Run: func(_ *cobra.Command, _ []string) {
mLog := logging.NewMasterLogger()
mLog.SetLevel(logrus.InfoLevel)
conf, ok := visorconfig.ReadFile(input)
if ok != nil {
mLog.WithError(ok).Fatal("Failed to parse config.")
}

conf = initUpdate()
if skysocksPasscode != "" {
changeAppsConfig(conf, "skysocks", "--passcode", skysocksPasscode)
}
Expand All @@ -139,19 +115,11 @@ var vpnClientUpdateCmd = &cobra.Command{
Use: "vpnc",
Short: "update vpn-client config",
PreRun: func(_ *cobra.Command, _ []string) {
var err error
if output, err = filepath.Abs(output); err != nil {
logger.WithError(err).Fatal("Invalid config output.")
}
setDefaults()
checkConfig()
},
Run: func(_ *cobra.Command, _ []string) {
mLog := logging.NewMasterLogger()
mLog.SetLevel(logrus.InfoLevel)

conf, ok := visorconfig.ReadFile(input)
if ok != nil {
mLog.WithError(ok).Fatal("Failed to parse config.")
}
conf = initUpdate()
switch setVPNClientKillswitch {
case "true":
changeAppsConfig(conf, "vpn-client", "--killswitch", setVPNClientKillswitch)
Expand Down Expand Up @@ -184,19 +152,11 @@ var vpnServerUpdateCmd = &cobra.Command{
Use: "vpns",
Short: "update vpn-server config",
PreRun: func(_ *cobra.Command, _ []string) {
var err error
if output, err = filepath.Abs(output); err != nil {
logger.WithError(err).Fatal("Invalid config output.")
}
setDefaults()
checkConfig()
},
Run: func(_ *cobra.Command, _ []string) {
mLog := logging.NewMasterLogger()
mLog.SetLevel(logrus.InfoLevel)

conf, ok := visorconfig.ReadFile(input)
if ok != nil {
mLog.WithError(ok).Fatal("Failed to parse config.")
}
conf = initUpdate()
if addVPNServerPasscode != "" {
changeAppsConfig(conf, "vpn-server", "--passcode", addVPNServerPasscode)
}
Expand Down
Loading