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

fixing skywire-cli reward freezing issue #1407

Merged
merged 1 commit into from
Nov 6, 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
46 changes: 11 additions & 35 deletions cmd/skywire-cli/commands/reward/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ var (
rewardAddress string
defaultRewardAddress string
output string
isUseRPC bool
useRPC bool
isRead bool
isRewarded bool
isDeleteFile bool
isAll bool
rpcFlagTxt string
readFlagTxt string
cHiddenFlags []string
)
Expand All @@ -48,27 +45,6 @@ func init() {
}
rewardCmd.Flags().BoolVarP(&isRead, "read", "r", false, "print the skycoin reward address & exit"+readFlagTxt)
cHiddenFlags = append(cHiddenFlags, "read")

//check if the visor is running

// _, err = net.DialTimeout("tcp", "localhost:3435", 5)
//the above was insufficient in practice

//TODO: re-implement this simple check for the visor running
// _, err := script.Exec(`skywire-cli visor pk`).String()
// if err == nil {
// useRPC = true
// } else {
useRPC = false
rpcFlagTxt = "default: false - visor is not running"
// }
if skyenv.IsRoot() {
useRPC = false
rpcFlagTxt = "default: false - root permissions available"
}
rewardCmd.Flags().BoolVarP(&isUseRPC, "userpc", "u", useRPC, "use the rpc of the running visor\n"+rpcFlagTxt)
cHiddenFlags = append(cHiddenFlags, "userpc")

rewardCmd.Flags().BoolVarP(&isDeleteFile, "delete", "d", false, "delete reward addresss file - opt out of rewards")
cHiddenFlags = append(cHiddenFlags, "delete")
rewardCmd.Flags().BoolVar(&isAll, "all", false, "show all flags")
Expand Down Expand Up @@ -164,20 +140,20 @@ var rewardCmd = &cobra.Command{
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("invalid address specified: %v", err))
}
//using the rpc of the running visor avoids needing sudo permissions
//true if visor is running
//false if sudo permissions exist
if isUseRPC {
client := clirpc.Client(cmd.Flags())
rewardaddress, err := client.SetRewardAddress(rewardAddress)
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
}
internal.PrintOutput(cmd.Flags(), rewardaddress, rewardaddress)

} else {
client, err := clirpc.Client(cmd.Flags())
if err != nil {
internal.Catch(cmd.Flags(), os.WriteFile(output, []byte(cAddr.String()), 0644)) //nolint
readRewardFile(cmd.Flags())
return
}
rewardaddress, err := client.SetRewardAddress(rewardAddress)
if err != nil {
internal.PrintError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
internal.Catch(cmd.Flags(), os.WriteFile(output, []byte(cAddr.String()), 0644)) //nolint
readRewardFile(cmd.Flags())
return
}
internal.PrintOutput(cmd.Flags(), rewardaddress, rewardaddress)
},
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/skywire-cli/commands/rpc/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ var (
)

// Client is used by other skywire-cli commands to query the visor rpc
func Client(cmdFlags *pflag.FlagSet) visor.API {
func Client(cmdFlags *pflag.FlagSet) (visor.API, error) {
const rpcDialTimeout = time.Second * 5
conn, err := net.DialTimeout("tcp", Addr, rpcDialTimeout)
if err != nil {
internal.PrintFatalError(cmdFlags, fmt.Errorf("RPC connection failed; is skywire running?: %v", err))
internal.PrintError(cmdFlags, fmt.Errorf("RPC connection failed; is skywire running?: %v", err))
return nil, err
}
return visor.NewRPCClient(logger, conn, visor.RPCPrefix, 0)
return visor.NewRPCClient(logger, conn, visor.RPCPrefix, 0), nil
}
55 changes: 46 additions & 9 deletions cmd/skywire-cli/commands/visor/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package clivisor
import (
"bytes"
"fmt"
"os"
"strconv"
"text/tabwriter"
"time"
Expand Down Expand Up @@ -50,7 +51,11 @@ var lsAppsCmd = &cobra.Command{
Short: "List apps",
Long: "\n List apps",
Run: func(cmd *cobra.Command, _ []string) {
states, err := clirpc.Client(cmd.Flags()).Apps()
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
states, err := rpcClient.Apps()
internal.Catch(cmd.Flags(), err)
var b bytes.Buffer
w := tabwriter.NewWriter(&b, 0, 0, 5, ' ', tabwriter.TabIndent)
Expand Down Expand Up @@ -97,7 +102,11 @@ var startAppCmd = &cobra.Command{
Long: "\n Launch app",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
internal.Catch(cmd.Flags(), clirpc.Client(cmd.Flags()).StartApp(args[0]))
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
internal.Catch(cmd.Flags(), rpcClient.StartApp(args[0]))
internal.PrintOutput(cmd.Flags(), "OK", "OK\n")
},
}
Expand All @@ -108,7 +117,11 @@ var stopAppCmd = &cobra.Command{
Long: "\n Halt app",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
internal.Catch(cmd.Flags(), clirpc.Client(cmd.Flags()).StopApp(args[0]))
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
internal.Catch(cmd.Flags(), rpcClient.StopApp(args[0]))
internal.PrintOutput(cmd.Flags(), "OK", "OK\n")
},
}
Expand All @@ -127,7 +140,11 @@ var setAppAutostartCmd = &cobra.Command{
default:
internal.Catch(cmd.Flags(), fmt.Errorf("invalid args[1] value: %s", args[1]))
}
internal.Catch(cmd.Flags(), clirpc.Client(cmd.Flags()).SetAutoStart(args[0], autostart))
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
internal.Catch(cmd.Flags(), rpcClient.SetAutoStart(args[0], autostart))
internal.PrintOutput(cmd.Flags(), "OK", "OK\n")
},
}
Expand All @@ -147,7 +164,11 @@ var setAppKillswitchCmd = &cobra.Command{
default:
internal.Catch(cmd.Flags(), fmt.Errorf("invalid args[1] value: %s", args[1]))
}
internal.Catch(cmd.Flags(), clirpc.Client(cmd.Flags()).SetAppKillswitch(args[0], killswitch))
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
internal.Catch(cmd.Flags(), rpcClient.SetAppKillswitch(args[0], killswitch))
internal.PrintOutput(cmd.Flags(), "OK", "OK\n")
},
}
Expand All @@ -167,7 +188,11 @@ var setAppSecureCmd = &cobra.Command{
default:
internal.Catch(cmd.Flags(), fmt.Errorf("invalid args[1] value: %s", args[1]))
}
internal.Catch(cmd.Flags(), clirpc.Client(cmd.Flags()).SetAppSecure(args[0], secure))
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
internal.Catch(cmd.Flags(), rpcClient.SetAppSecure(args[0], secure))
internal.PrintOutput(cmd.Flags(), "OK", "OK\n")
},
}
Expand All @@ -182,7 +207,11 @@ var setAppPasscodeCmd = &cobra.Command{
if args[1] == "remove" {
passcode = ""
}
internal.Catch(cmd.Flags(), clirpc.Client(cmd.Flags()).SetAppPassword(args[0], passcode))
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
internal.Catch(cmd.Flags(), rpcClient.SetAppPassword(args[0], passcode))
internal.PrintOutput(cmd.Flags(), "OK", "OK\n")
},
}
Expand All @@ -197,7 +226,11 @@ var setAppNetworkInterfaceCmd = &cobra.Command{
if args[1] == "remove" {
netifc = ""
}
internal.Catch(cmd.Flags(), clirpc.Client(cmd.Flags()).SetAppNetworkInterface(args[0], netifc))
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
internal.Catch(cmd.Flags(), rpcClient.SetAppNetworkInterface(args[0], netifc))
internal.PrintOutput(cmd.Flags(), "OK", "OK\n")
},
}
Expand All @@ -217,7 +250,11 @@ var appLogsSinceCmd = &cobra.Command{
t, err = time.Parse(time.RFC3339Nano, strTime)
internal.Catch(cmd.Flags(), err)
}
logs, err := clirpc.Client(cmd.Flags()).LogsSince(t, args[0])
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
logs, err := rpcClient.LogsSince(t, args[0])
internal.Catch(cmd.Flags(), err)
if len(logs) > 0 {
internal.PrintOutput(cmd.Flags(), logs, fmt.Sprintf("%v\n", logs))
Expand Down
7 changes: 6 additions & 1 deletion cmd/skywire-cli/commands/visor/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package clivisor

import (
"os"
"strings"

"github.com/spf13/cobra"
Expand All @@ -22,7 +23,11 @@ var execCmd = &cobra.Command{
Long: "\n Execute a command",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
out, err := clirpc.Client(cmd.Flags()).Exec(strings.Join(args, " "))
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
out, err := rpcClient.Exec(strings.Join(args, " "))
internal.Catch(cmd.Flags(), err)
// since the output of this command can be anything it is not formatted, so it's advisable to not use the `--json` flag for this one
internal.PrintOutput(cmd.Flags(), string(out), string(out))
Expand Down
15 changes: 11 additions & 4 deletions cmd/skywire-cli/commands/visor/hv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package clivisor

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/toqueteos/webbrowser"
Expand Down Expand Up @@ -60,8 +61,11 @@ var hvpkCmd = &cobra.Command{
}
hypervisors = conf.Hypervisors
} else {
client := clirpc.Client(cmd.Flags())
overview, err := client.Overview()
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
overview, err := rpcClient.Overview()
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
}
Expand All @@ -76,8 +80,11 @@ var chvpkCmd = &cobra.Command{
Short: "Public key of remote hypervisor(s)",
Long: "Public key of remote hypervisor(s) which are currently connected to",
Run: func(cmd *cobra.Command, _ []string) {
client := clirpc.Client(cmd.Flags())
overview, err := client.Overview()
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
overview, err := rpcClient.Overview()
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
}
Expand Down
21 changes: 16 additions & 5 deletions cmd/skywire-cli/commands/visor/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package clivisor
import (
"fmt"
"net/http"
"os"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -45,8 +46,11 @@ var pkCmd = &cobra.Command{
}
outputPK = conf.PK.Hex()
} else {
client := clirpc.Client(cmd.Flags())
overview, err := client.Overview()
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
overview, err := rpcClient.Overview()
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
}
Expand All @@ -67,7 +71,11 @@ var summaryCmd = &cobra.Command{
Short: "Summary of visor info",
Long: "\n Summary of visor info",
Run: func(cmd *cobra.Command, _ []string) {
summary, err := clirpc.Client(cmd.Flags()).Summary()
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
summary, err := rpcClient.Summary()
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
}
Expand Down Expand Up @@ -107,8 +115,11 @@ var buildInfoCmd = &cobra.Command{
Short: "Version and build info",
Long: "\n Version and build info",
Run: func(cmd *cobra.Command, _ []string) {
client := clirpc.Client(cmd.Flags())
overview, err := client.Overview()
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
overview, err := rpcClient.Overview()
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
}
Expand Down
Loading