-
Notifications
You must be signed in to change notification settings - Fork 0
/
agents.go
140 lines (127 loc) · 3.38 KB
/
agents.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* *******************************************************************************
* * Copyright (c) 2023 Datasance Teknoloji A.S.
* *
* * This program and the accompanying materials are made available under the
* * terms of the Eclipse Public License v. 2.0 which is available at
* * http://www.eclipse.org/legal/epl-2.0
* *
* * SPDX-License-Identifier: EPL-2.0
* *******************************************************************************
*
*/
package get
import (
"fmt"
"time"
"github.com/datasance/potctl/internal/config"
rsc "github.com/datasance/potctl/internal/resource"
clientutil "github.com/datasance/potctl/internal/util/client"
"github.com/datasance/potctl/pkg/util"
"github.com/datasance/iofog-go-sdk/v3/pkg/client"
)
type agentExecutor struct {
namespace string
showDetached bool
}
func newAgentExecutor(namespace string, showDetached bool) *agentExecutor {
a := &agentExecutor{}
a.namespace = namespace
a.showDetached = showDetached
return a
}
func (exe *agentExecutor) GetName() string {
return ""
}
func (exe *agentExecutor) Execute() error {
if exe.showDetached {
printDetached()
table, err := generateDetachedAgentOutput()
if err != nil {
return err
}
return print(table)
}
printNamespace(exe.namespace)
table, err := generateAgentOutput(exe.namespace)
if err != nil {
return err
}
// Flush occurs in generateAgentOutput
return print(table)
}
func generateDetachedAgentOutput() (table [][]string, err error) {
detachedAgents := config.GetDetachedAgents()
// Make an index of agents the client knows about and pre-process any info
agentsToPrint := make([]client.AgentInfo, len(detachedAgents))
for idx := range detachedAgents {
agentsToPrint[idx] = client.AgentInfo{
Name: detachedAgents[idx].GetName(),
IPAddressExternal: detachedAgents[idx].GetHost(),
}
}
return tabulateAgents(agentsToPrint)
}
func generateAgentOutput(namespace string) (table [][]string, err error) {
agents := []client.AgentInfo{}
// Update local cache based on Controller
if err = clientutil.SyncAgentInfo(namespace); err != nil && !rsc.IsNoControlPlaneError(err) {
return
}
// Get Agents from Controller
if err == nil {
agents, err = clientutil.GetBackendAgents(namespace)
if err != nil {
return
}
}
return tabulateAgents(agents)
}
func tabulateAgents(agentInfos []client.AgentInfo) (table [][]string, err error) {
// Generate table and headers
table = make([][]string, len(agentInfos)+1)
headers := []string{
"AGENT",
"STATUS",
"AGE",
"UPTIME",
"VERSION",
"ADDR",
}
table[0] = append(table[0], headers...)
// Populate rows
for idx := range agentInfos {
agent := &agentInfos[idx]
// if UUID is empty, we assume the agent is not provisioned
if agent.UUID == "" {
row := []string{
agent.Name,
"not provisioned",
"-",
"-",
"-",
agent.IPAddressExternal,
}
table[idx+1] = append(table[idx+1], row...)
} else {
age := "-"
if backendAge, err := util.ElapsedRFC(agent.CreatedTimeRFC3339, util.NowRFC()); err == nil {
age = backendAge
}
uptime := time.Duration(agent.UptimeMs) * time.Millisecond
row := []string{
agent.Name,
agent.DaemonStatus,
age,
util.FormatDuration(uptime),
agent.Version,
agent.Host,
}
table[idx+1] = append(table[idx+1], row...)
}
}
return table, err
}
func printDetached() {
fmt.Printf("DETACHED RESOURCES\n\n")
}