zrok/cmd/zrok/enable.go

99 lines
2.6 KiB
Go
Raw Normal View History

2022-07-25 23:05:44 +02:00
package main
import (
2022-08-03 20:25:27 +02:00
"fmt"
httptransport "github.com/go-openapi/runtime/client"
2022-11-30 17:43:00 +01:00
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/environment"
2022-08-03 20:25:27 +02:00
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
2022-07-26 22:00:59 +02:00
"github.com/openziti-test-kitchen/zrok/zrokdir"
2022-08-03 20:25:27 +02:00
"github.com/shirou/gopsutil/v3/host"
2022-07-25 23:05:44 +02:00
"github.com/spf13/cobra"
2022-08-05 22:03:08 +02:00
user2 "os/user"
2022-07-25 23:05:44 +02:00
)
func init() {
2022-08-10 21:27:41 +02:00
rootCmd.AddCommand(newEnableCommand().cmd)
2022-07-25 23:05:44 +02:00
}
2022-08-10 21:27:41 +02:00
type enableCommand struct {
description string
cmd *cobra.Command
2022-07-25 23:05:44 +02:00
}
2022-08-10 21:27:41 +02:00
func newEnableCommand() *enableCommand {
cmd := &cobra.Command{
Use: "enable <token>",
Short: "Enable an environment for zrok",
Args: cobra.ExactArgs(1),
}
command := &enableCommand{cmd: cmd}
cmd.Flags().StringVarP(&command.description, "description", "d", "<user>@<hostname>", "Description of this environment")
cmd.Run = command.run
return command
}
func (cmd *enableCommand) run(_ *cobra.Command, args []string) {
2023-01-09 20:16:08 +01:00
zrd, err := zrokdir.Load()
if err != nil {
panic(err)
}
2022-07-26 22:00:59 +02:00
token := args[0]
2022-07-25 23:05:44 +02:00
2022-08-05 22:03:08 +02:00
hostName, hostDetail, err := getHost()
2022-08-03 20:25:27 +02:00
if err != nil {
panic(err)
}
2022-08-05 22:03:08 +02:00
user, err := user2.Current()
if err != nil {
panic(err)
}
hostDetail = fmt.Sprintf("%v; %v", user.Username, hostDetail)
2022-08-10 21:27:41 +02:00
if cmd.description == "<user>@<hostname>" {
cmd.description = fmt.Sprintf("%v@%v", user.Username, hostName)
2022-08-05 22:03:08 +02:00
}
2022-08-03 20:25:27 +02:00
2023-01-09 20:16:08 +01:00
zrok, err := zrd.Client()
if err != nil {
panic(err)
}
auth := httptransport.APIKeyAuth("X-TOKEN", "header", token)
2022-11-30 17:43:00 +01:00
req := environment.NewEnableParams()
2022-08-03 20:25:27 +02:00
req.Body = &rest_model_zrok.EnableRequest{
2022-08-10 21:27:41 +02:00
Description: cmd.description,
2022-08-05 22:03:08 +02:00
Host: hostDetail,
2022-08-03 20:25:27 +02:00
}
2022-11-30 17:43:00 +01:00
resp, err := zrok.Environment.Enable(req, auth)
2022-07-25 23:05:44 +02:00
if err != nil {
2022-09-26 19:55:15 +02:00
if !panicInstead {
showError("the zrok service returned an error", err)
}
2022-07-25 23:05:44 +02:00
panic(err)
}
apiEndpoint, _ := zrd.ApiEndpoint()
zrd.Env = &zrokdir.Environment{Token: token, ZId: resp.Payload.Identity, ApiEndpoint: apiEndpoint}
2023-01-09 20:16:08 +01:00
if err := zrd.Save(); err != nil {
2022-09-26 19:55:15 +02:00
if !panicInstead {
showError("there was an error saving the new environment", err)
}
2022-07-26 23:17:37 +02:00
panic(err)
}
if err := zrokdir.SaveZitiIdentity("backend", resp.Payload.Cfg); err != nil {
2022-09-26 19:55:15 +02:00
if !panicInstead {
showError("there was an error writing the environment file", err)
}
panic(err)
}
2022-09-23 15:09:21 +02:00
fmt.Printf("zrok environment '%v' enabled for '%v'\n", resp.Payload.Identity, token)
2022-07-25 23:05:44 +02:00
}
2022-08-03 20:25:27 +02:00
2022-08-05 22:03:08 +02:00
func getHost() (string, string, error) {
2022-08-03 20:25:27 +02:00
info, err := host.Info()
if err != nil {
2022-08-05 22:03:08 +02:00
return "", "", err
2022-08-03 20:25:27 +02:00
}
thisHost := fmt.Sprintf("%v; %v; %v; %v; %v; %v; %v",
info.Hostname, info.OS, info.Platform, info.PlatformFamily, info.PlatformVersion, info.KernelVersion, info.KernelArch)
2022-08-05 22:03:08 +02:00
return info.Hostname, thisHost, nil
2022-08-03 20:25:27 +02:00
}