2024-05-18 10:59:54 +00:00
|
|
|
package server
|
2024-05-17 17:56:13 +00:00
|
|
|
|
|
|
|
import (
|
2024-05-22 20:40:06 +00:00
|
|
|
"bufio"
|
2024-05-17 17:56:13 +00:00
|
|
|
"fmt"
|
2024-05-23 22:37:27 +00:00
|
|
|
"log/slog"
|
2024-05-17 17:56:13 +00:00
|
|
|
"os"
|
2024-05-18 12:39:27 +00:00
|
|
|
"strconv"
|
2024-05-17 17:56:13 +00:00
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
2024-07-06 18:28:44 +00:00
|
|
|
|
|
|
|
"github.com/ditatompel/xmr-remote-nodes/internal/database"
|
|
|
|
"github.com/ditatompel/xmr-remote-nodes/internal/monero"
|
2024-05-17 17:56:13 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var probersCmd = &cobra.Command{
|
2024-05-17 18:42:31 +00:00
|
|
|
Use: "probers",
|
2024-05-18 11:42:32 +00:00
|
|
|
Short: "Add, edit, delete, and show registered probers",
|
2024-05-17 18:42:31 +00:00
|
|
|
Long: `Command to administer prober machines.
|
|
|
|
|
|
|
|
This command should only be run on the server which directly connect to the MySQL database.
|
|
|
|
`,
|
|
|
|
Run: func(cmd *cobra.Command, _ []string) {
|
2024-05-23 22:37:27 +00:00
|
|
|
if err := cmd.Help(); err != nil {
|
|
|
|
slog.Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-05-17 18:42:31 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var listProbersCmd = &cobra.Command{
|
|
|
|
Use: "list [search]",
|
|
|
|
Short: "Print registered probers",
|
|
|
|
Long: `Print list of registered prober machines.
|
2024-05-17 17:56:13 +00:00
|
|
|
|
|
|
|
Use [search] args to filter results by name or api key.
|
|
|
|
|
|
|
|
"sort-by" flag can be "id" or "last_submit_ts"
|
|
|
|
"sort-dir" flag can be "asc" or "desc"`,
|
|
|
|
Example: `# To sort probers by last submit time in ascending order that contains "sin1":
|
2024-05-17 18:42:31 +00:00
|
|
|
xmr-nodes probers list -s last_submit_ts -d asc sin1`,
|
2024-05-17 17:56:13 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if err := database.ConnectDB(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
sortBy, _ := cmd.Flags().GetString("sort-by")
|
|
|
|
sortDir, _ := cmd.Flags().GetString("sort-dir")
|
|
|
|
|
2024-05-27 00:17:27 +00:00
|
|
|
probersRepo := monero.NewProber()
|
|
|
|
probers, err := probersRepo.Probers(monero.QueryProbers{
|
2024-05-17 17:56:13 +00:00
|
|
|
Search: strings.Join(args, " "),
|
|
|
|
SortBy: sortBy,
|
|
|
|
SortDirection: sortDir,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(probers) == 0 {
|
|
|
|
fmt.Println("No probers found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
|
|
|
|
fmt.Fprintf(w, "ID\t| Name\t| Last Submit\t| API Key\n")
|
|
|
|
for _, prober := range probers {
|
|
|
|
fmt.Fprintf(w, "%d\t| %s\t| %s\t| %s\n",
|
2024-05-30 05:46:33 +00:00
|
|
|
prober.ID,
|
2024-05-17 17:56:13 +00:00
|
|
|
prober.Name,
|
2024-06-09 20:03:23 +00:00
|
|
|
time.Unix(prober.LastSubmitTS, 0).Format(time.RFC3339),
|
|
|
|
prober.APIKey,
|
2024-05-17 17:56:13 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-05-22 20:40:06 +00:00
|
|
|
func stringPrompt(label string) string {
|
|
|
|
var s string
|
|
|
|
r := bufio.NewReader(os.Stdin)
|
|
|
|
for {
|
|
|
|
fmt.Fprint(os.Stderr, label+" ")
|
|
|
|
s, _ = r.ReadString('\n')
|
|
|
|
if s != "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(s)
|
|
|
|
}
|
|
|
|
|
2024-05-17 18:42:31 +00:00
|
|
|
var addProbersCmd = &cobra.Command{
|
|
|
|
Use: "add [name]",
|
2024-05-18 11:42:32 +00:00
|
|
|
Short: "Add new prober",
|
2024-05-18 12:03:56 +00:00
|
|
|
Long: `Create new prober identified by [name] (if provided) along with an API key.
|
|
|
|
|
|
|
|
This command will display the prober name and API key when successfully executed.`,
|
2024-05-18 12:39:27 +00:00
|
|
|
Run: func(_ *cobra.Command, args []string) {
|
2024-05-18 12:03:56 +00:00
|
|
|
if err := database.ConnectDB(); err != nil {
|
2024-05-18 12:39:27 +00:00
|
|
|
fmt.Println(err)
|
|
|
|
return
|
2024-05-18 12:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proberName := ""
|
|
|
|
if len(args) > 0 {
|
|
|
|
proberName = strings.Join(args, " ")
|
|
|
|
} else {
|
|
|
|
proberName = stringPrompt("Prober Name:")
|
|
|
|
}
|
|
|
|
|
2024-05-27 00:17:27 +00:00
|
|
|
proberRepo := monero.NewProber()
|
2024-05-18 12:03:56 +00:00
|
|
|
prober, err := proberRepo.Add(proberName)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-09 20:03:23 +00:00
|
|
|
fmt.Printf("Name: %s\nAPI Key: %s\n", prober.Name, prober.APIKey)
|
2024-05-17 18:42:31 +00:00
|
|
|
},
|
|
|
|
}
|
2024-05-18 12:39:27 +00:00
|
|
|
|
2024-05-18 12:50:20 +00:00
|
|
|
var editProbersCmd = &cobra.Command{
|
|
|
|
Use: "edit",
|
|
|
|
Short: "Edit prober",
|
|
|
|
Long: `Edit prober name by id.`,
|
|
|
|
Run: func(cmd *cobra.Command, _ []string) {
|
|
|
|
if err := database.ConnectDB(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
proberId, err := strconv.Atoi(stringPrompt("Prober ID:"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Invalid ID:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
proberName := stringPrompt("Prober Name:")
|
2024-05-27 00:17:27 +00:00
|
|
|
proberRepo := monero.NewProber()
|
2024-05-18 12:50:20 +00:00
|
|
|
err = proberRepo.Edit(proberId, proberName)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to update prober:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Prober ID %d updated\n", proberId)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-05-18 12:39:27 +00:00
|
|
|
var deleteProbersCmd = &cobra.Command{
|
|
|
|
Use: "delete",
|
|
|
|
Short: "Delete prober",
|
|
|
|
Long: `Delete prober identified by id.`,
|
|
|
|
Run: func(_ *cobra.Command, _ []string) {
|
|
|
|
if err := database.ConnectDB(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
proberId, err := strconv.Atoi(stringPrompt("Prober ID:"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Invalid ID:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-27 00:17:27 +00:00
|
|
|
proberRepo := monero.NewProber()
|
2024-05-18 12:39:27 +00:00
|
|
|
err = proberRepo.Delete(proberId)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to delete prober:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Prober ID %d deleted\n", proberId)
|
|
|
|
},
|
|
|
|
}
|