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

feat: worker view #148

Merged
merged 3 commits into from
Nov 18, 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/AlecAivazis/survey/v2 v2.3.5
github.com/MakeNowJust/heredoc/v2 v2.0.1
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.16.0
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.16.1
github.com/briandowns/spinner v1.19.0
github.com/google/uuid v1.3.0
github.com/hashicorp/go-multierror v1.1.1
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,8 @@ github.com/MakeNowJust/heredoc/v2 v2.0.1 h1:rlCHh70XXXv7toz95ajQWOWQnN4WNLt0TdpZ
github.com/MakeNowJust/heredoc/v2 v2.0.1/go.mod h1:6/2Abh5s+hc3g9nbWLe9ObDIOhaRrqsyY9MWy+4JdRM=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.14.0 h1:BMFtEXFvzp+m942qRBfautdPE4V+jzWBFLSB2ShTDGc=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.14.0/go.mod h1:2j9rwRfb5qUs9PEJ3W331W84kRaNge5bed4D7JR1ruU=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.15.1 h1:AJ/UhXqHpFZOK2EtdSBvqTseIDb9VxqXS9Rosneco8w=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.15.1/go.mod h1:2j9rwRfb5qUs9PEJ3W331W84kRaNge5bed4D7JR1ruU=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.16.0 h1:Ndr4YhSshyQT9rkOZ+o3xNAEz77/AY9klVOwULGFli4=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.16.0/go.mod h1:2j9rwRfb5qUs9PEJ3W331W84kRaNge5bed4D7JR1ruU=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.16.1 h1:F5LgixmKnsYBduOAScsmLyWtZpNuKkqJkafspzeYg6w=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.16.1/go.mod h1:2j9rwRfb5qUs9PEJ3W331W84kRaNge5bed4D7JR1ruU=
github.com/briandowns/spinner v1.19.0 h1:s8aq38H+Qju89yhp89b4iIiMzMm8YN3p6vGpwyh/a8E=
github.com/briandowns/spinner v1.19.0/go.mod h1:mQak9GHqbspjC/5iUx3qMlIho8xBS/ppAL/hX5SmPJU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
8 changes: 4 additions & 4 deletions pkg/cmd/target/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ func ListRun(opts *ListOptions) error {
TenantTags []string `json:"TenantTags"`
}

environmentMap, err := GetEnvironmentMap(opts, err)
environmentMap, err := GetEnvironmentMap(opts)
if err != nil {
return err
}

tenantMap, err := GetTenantMap(opts, err)
tenantMap, err := GetTenantMap(opts)
if err != nil {
return err
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func resolveEntities(keys []string, lookup map[string]string) []Entity {
return entities
}

func GetEnvironmentMap(opts *ListOptions, err error) (map[string]string, error) {
func GetEnvironmentMap(opts *ListOptions) (map[string]string, error) {
environmentMap := make(map[string]string)
allEnvs, err := opts.Client.Environments.GetAll()
if err != nil {
Expand All @@ -134,7 +134,7 @@ func GetEnvironmentMap(opts *ListOptions, err error) (map[string]string, error)
return environmentMap, nil
}

func GetTenantMap(opts *ListOptions, err error) (map[string]string, error) {
func GetTenantMap(opts *ListOptions) (map[string]string, error) {
tenantMap := make(map[string]string)
allEnvs, err := opts.Client.Tenants.GetAll()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewCmdDeploymentTarget(f factory.Factory) *cobra.Command {
cmd.AddCommand(cmdAzureWebApp.NewCmdAzureWebApp(f))
cmd.AddCommand(cmdDelete.NewCmdDelete(f))
cmd.AddCommand(cmdList.NewCmdList(f))
cmd.AddCommand(cmdView.NewCmdList(f))
cmd.AddCommand(cmdView.NewCmdView(f))

return cmd
}
2 changes: 1 addition & 1 deletion pkg/cmd/target/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/spf13/cobra"
)

func NewCmdList(f factory.Factory) *cobra.Command {
func NewCmdView(f factory.Factory) *cobra.Command {
flags := shared.NewViewFlags()
cmd := &cobra.Command{
Use: "view",
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/worker/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func ListRun(opts *ListOptions) error {
WorkerPools []Entity `json:"WorkerPools"`
}

workerPoolMap, err := GetWorkerPoolMap(opts, err)
workerPoolMap, err := GetWorkerPoolMap(opts)
if err != nil {
return err
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func resolveEntities(keys []string, lookup map[string]string) []Entity {
return entities
}

func GetWorkerPoolMap(opts *ListOptions, err error) (map[string]string, error) {
func GetWorkerPoolMap(opts *ListOptions) (map[string]string, error) {
workerPoolMap := make(map[string]string)
allEnvs, err := opts.Client.WorkerPools.GetAll()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/worker/listening-tentacle/listening-tentacle.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

cmdCreate "github.com/OctopusDeploy/cli/pkg/cmd/worker/listening-tentacle/create"
cmdList "github.com/OctopusDeploy/cli/pkg/cmd/worker/listening-tentacle/list"
cmdView "github.com/OctopusDeploy/cli/pkg/cmd/worker/listening-tentacle/view"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/spf13/cobra"
Expand All @@ -20,6 +21,7 @@ func NewCmdListeningTentacle(f factory.Factory) *cobra.Command {

cmd.AddCommand(cmdCreate.NewCmdCreate(f))
cmd.AddCommand(cmdList.NewCmdList(f))
cmd.AddCommand(cmdView.NewCmdView(f))

return cmd
}
56 changes: 56 additions & 0 deletions pkg/cmd/worker/listening-tentacle/view/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package view

import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd"
"github.com/OctopusDeploy/cli/pkg/cmd/worker/shared"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/machinescommon"
"github.com/OctopusDeploy/cli/pkg/usage"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/spf13/cobra"
)

func NewCmdView(f factory.Factory) *cobra.Command {
flags := shared.NewViewFlags()
cmd := &cobra.Command{
Args: usage.ExactArgs(1),
Use: "view {<name> | <id>}",
Short: "View a Listening Tentacle worker in an instance of Octopus Deploy",
Long: "View a Listening Tentacle worker in an instance of Octopus Deploy.",
Example: fmt.Sprintf(heredoc.Doc(`
$ %s worker listening-tentacle view 'WindowsWorker'
$ %s worker listening-tentacle view Machines-100
`), constants.ExecutableName, constants.ExecutableName),
RunE: func(c *cobra.Command, args []string) error {
opts := shared.NewViewOptions(flags, cmd.NewDependencies(f, c), args)
return ViewRun(opts)
},
}

machinescommon.RegisterWebFlag(cmd, flags.WebFlags)

return cmd
}

func ViewRun(opts *shared.ViewOptions) error {
return shared.ViewRun(opts, contributeEndpoint, "Listening Tentacle")
}

func contributeEndpoint(opts *shared.ViewOptions, end machines.IEndpoint) ([]*shared.DataRow, error) {
data := []*shared.DataRow{}

endpoint := end.(*machines.ListeningTentacleEndpoint)
data = append(data, shared.NewDataRow("URI", endpoint.URI.String()))
data = append(data, shared.NewDataRow("Tentacle version", endpoint.TentacleVersionDetails.Version))

proxyData, err := shared.ContributeProxy(opts, endpoint.ProxyID)
if err != nil {
return nil, err
}
data = append(data, proxyData...)

return data, nil
}
2 changes: 2 additions & 0 deletions pkg/cmd/worker/polling-tentacle/polling-tentacle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

cmdList "github.com/OctopusDeploy/cli/pkg/cmd/worker/polling-tentacle/list"
cmdView "github.com/OctopusDeploy/cli/pkg/cmd/worker/polling-tentacle/view"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/spf13/cobra"
Expand All @@ -18,6 +19,7 @@ func NewCmdPollingTentacle(f factory.Factory) *cobra.Command {
}

cmd.AddCommand(cmdList.NewCmdList(f))
cmd.AddCommand(cmdView.NewCmdView(f))

return cmd
}
50 changes: 50 additions & 0 deletions pkg/cmd/worker/polling-tentacle/view/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package view

import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd"
"github.com/OctopusDeploy/cli/pkg/cmd/worker/shared"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/machinescommon"
"github.com/OctopusDeploy/cli/pkg/usage"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/spf13/cobra"
)

func NewCmdView(f factory.Factory) *cobra.Command {
flags := shared.NewViewFlags()
cmd := &cobra.Command{
Args: usage.ExactArgs(1),
Use: "view {<name> | <id>}",
Short: "View a Polling Tentacle worker in an instance of Octopus Deploy",
Long: "View a Polling Tentacle worker in an instance of Octopus Deploy.",
Example: fmt.Sprintf(heredoc.Doc(`
$ %s worker polling-tentacle view 'WindowsWorker'
$ %s worker polling-tentacle view Machines-100
`), constants.ExecutableName, constants.ExecutableName),
RunE: func(c *cobra.Command, args []string) error {
opts := shared.NewViewOptions(flags, cmd.NewDependencies(f, c), args)
return ViewRun(opts)
},
}

machinescommon.RegisterWebFlag(cmd, flags.WebFlags)

return cmd
}

func ViewRun(opts *shared.ViewOptions) error {
return shared.ViewRun(opts, contributeEndpoint, "Polling Tentacle")
}

func contributeEndpoint(opts *shared.ViewOptions, workerEndpoint machines.IEndpoint) ([]*shared.DataRow, error) {
data := []*shared.DataRow{}

endpoint := workerEndpoint.(*machines.PollingTentacleEndpoint)
data = append(data, shared.NewDataRow("URI", endpoint.URI.String()))
data = append(data, shared.NewDataRow("Tentacle version", endpoint.TentacleVersionDetails.Version))

return data, nil
}
144 changes: 144 additions & 0 deletions pkg/cmd/worker/shared/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package shared

import (
"fmt"
"github.com/OctopusDeploy/cli/pkg/cmd"
"github.com/OctopusDeploy/cli/pkg/machinescommon"
"github.com/OctopusDeploy/cli/pkg/output"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"strings"
)

type DataRow struct {
Name string
Value string
}

func NewDataRow(name string, value string) *DataRow {
return &DataRow{
Name: name,
Value: value,
}
}

type ContributeEndpointCallback func(opts *ViewOptions, endpoint machines.IEndpoint) ([]*DataRow, error)

type ViewFlags struct {
*machinescommon.WebFlags
}

type ViewOptions struct {
*cmd.Dependencies
IdOrName string
*ViewFlags
}

func NewViewFlags() *ViewFlags {
return &ViewFlags{
WebFlags: machinescommon.NewWebFlags(),
}
}

func NewViewOptions(viewFlags *ViewFlags, dependencies *cmd.Dependencies, args []string) *ViewOptions {
return &ViewOptions{
ViewFlags: viewFlags,
Dependencies: dependencies,
IdOrName: args[0],
}
}

func ViewRun(opts *ViewOptions, contributeEndpoint ContributeEndpointCallback, description string) error {
var worker, err = opts.Client.Workers.GetByIdentifier(opts.IdOrName)
if err != nil {
return err
}

data := []*DataRow{}

data = append(data, NewDataRow("Name", fmt.Sprintf("%s %s", output.Bold(worker.Name), output.Dimf("(%s)", worker.GetID()))))
data = append(data, NewDataRow("Health status", getHealthStatus(worker)))
data = append(data, NewDataRow("Current status", worker.StatusSummary))

workerPoolMap, err := GetWorkerPoolMap(opts)
workerPoolNames := resolveValues(worker.WorkerPoolIDs, workerPoolMap)
data = append(data, NewDataRow("Worker Pools", formatAsList(workerPoolNames)))

if contributeEndpoint != nil {
newRows, err := contributeEndpoint(opts, worker.Endpoint)
if err != nil {
return err
}
for _, r := range newRows {
data = append(data, r)
}
}

t := output.NewTable(opts.Out)
for _, row := range data {
t.AddRow(row.Name, row.Value)
}
t.Print()

fmt.Fprintf(opts.Out, "\n")
machinescommon.DoWebForWorkers(worker, opts.Dependencies, opts.WebFlags, description)
return nil

return nil
}

func ContributeProxy(opts *ViewOptions, proxyID string) ([]*DataRow, error) {
if proxyID != "" {
proxy, err := opts.Client.Proxies.GetById(proxyID)
if err != nil {
return nil, err
}
return []*DataRow{NewDataRow("Proxy", proxy.GetName())}, nil
}

return []*DataRow{NewDataRow("Proxy", "None")}, nil
}

func ContributeAccount(opts *ViewOptions, accountID string) ([]*DataRow, error) {
account, err := opts.Client.Accounts.GetByID(accountID)
if err != nil {
return nil, err
}
data := []*DataRow{NewDataRow("Account", account.GetName())}
return data, nil
}

func getHealthStatus(worker *machines.Worker) string {
switch worker.HealthStatus {
case "Healthy":
return output.Green(worker.HealthStatus)
case "Unhealthy":
return output.Red(worker.HealthStatus)
default:
return output.Yellow(worker.HealthStatus)
}
}

func GetWorkerPoolMap(opts *ViewOptions) (map[string]string, error) {
workerPoolMap := make(map[string]string)

allEnvs, err := opts.Client.WorkerPools.GetAll()
if err != nil {
return nil, err
}
for _, e := range allEnvs {
workerPoolMap[e.ID] = e.Name
}
return workerPoolMap, nil
}

func formatAsList(items []string) string {
return strings.Join(items, ", ")
}

func resolveValues(keys []string, lookup map[string]string) []string {
var values []string
for _, key := range keys {
values = append(values, lookup[key])
}
return values
}
2 changes: 2 additions & 0 deletions pkg/cmd/worker/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

cmdCreate "github.com/OctopusDeploy/cli/pkg/cmd/worker/ssh/create"
cmdList "github.com/OctopusDeploy/cli/pkg/cmd/worker/ssh/list"
cmdView "github.com/OctopusDeploy/cli/pkg/cmd/worker/ssh/view"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/spf13/cobra"
Expand All @@ -20,6 +21,7 @@ func NewCmdSsh(f factory.Factory) *cobra.Command {

cmd.AddCommand(cmdCreate.NewCmdCreate(f))
cmd.AddCommand(cmdList.NewCmdList(f))
cmd.AddCommand(cmdView.NewCmdView(f))

return cmd
}
Loading