Skip to content

Commit 518c5e1

Browse files
author
Joshua Anderson
committed
ref(deisctl): remove global variables in backend package
1 parent c074db5 commit 518c5e1

9 files changed

Lines changed: 57 additions & 61 deletions

File tree

deisctl/backend/fleet/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func doCreate(c *FleetClient, unit *schema.Unit, wg *sync.WaitGroup, outchan cha
6060
outerLoop:
6161
for {
6262
time.Sleep(250 * time.Millisecond)
63-
unitStates, err := cAPI.UnitStates()
63+
unitStates, err := c.Fleet.UnitStates()
6464
if err != nil {
6565
errchan <- err
6666
}

deisctl/backend/fleet/destroy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func doDestroy(c *FleetClient, target string, wg *sync.WaitGroup, outchan chan s
4646
outerLoop:
4747
for {
4848
time.Sleep(250 * time.Millisecond)
49-
unitStates, err := cAPI.UnitStates()
49+
unitStates, err := c.Fleet.UnitStates()
5050
if err != nil {
5151
errchan <- err
5252
}

deisctl/backend/fleet/fleet.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package fleet
22

3-
import "github.com/coreos/fleet/client"
3+
import (
4+
"github.com/coreos/fleet/client"
5+
"github.com/coreos/fleet/machine"
6+
)
47

58
// FleetClient used to wrap Fleet API calls
69
type FleetClient struct {
710
Fleet client.API
11+
12+
// used to cache MachineStates
13+
machineStates map[string]*machine.MachineState
814
}
915

1016
// NewClient returns a client used to communicate with Fleet
@@ -14,7 +20,5 @@ func NewClient() (*FleetClient, error) {
1420
if err != nil {
1521
return nil, err
1622
}
17-
// set global client
18-
cAPI = client
1923
return &FleetClient{Fleet: client}, nil
2024
}

deisctl/backend/fleet/journal.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ func (c *FleetClient) Journal(target string) (err error) {
1111
return
1212
}
1313
for _, unit := range units {
14-
runJournal(unit)
14+
runJournal(c, unit)
1515
}
1616
return
1717
}
1818

1919
// runJournal tails the systemd journal for a given unit
20-
func runJournal(name string) (exit int) {
21-
machineID, err := findUnit(name)
20+
func runJournal(c *FleetClient, name string) (exit int) {
21+
machineID, err := findUnit(c, name)
2222

2323
if err != nil {
2424
return 1
2525
}
2626

2727
command := fmt.Sprintf("journalctl --unit %s --no-pager -n 40 -f", name)
28-
return runCommand(command, machineID)
28+
return runCommand(c, command, machineID)
2929
}

deisctl/backend/fleet/list_unit_files.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,43 @@ const (
1818

1919
var (
2020
listUnitFilesFields = map[string]unitToField{
21-
"unit": func(u schema.Unit, full bool) string {
21+
"unit": func(c *FleetClient, u schema.Unit, full bool) string {
2222
return u.Name
2323
},
24-
"global": func(u schema.Unit, full bool) string {
24+
"global": func(c *FleetClient, u schema.Unit, full bool) string {
2525
return strconv.FormatBool(suToGlobal(u))
2626
},
27-
"dstate": func(u schema.Unit, full bool) string {
27+
"dstate": func(c *FleetClient, u schema.Unit, full bool) string {
2828
if u.DesiredState == "" {
2929
return "-"
3030
}
3131
return u.DesiredState
3232
},
33-
"tmachine": func(u schema.Unit, full bool) string {
33+
"tmachine": func(c *FleetClient, u schema.Unit, full bool) string {
3434
if suToGlobal(u) || u.MachineID == "" {
3535
return "-"
3636
}
37-
ms := cachedMachineState(u.MachineID)
37+
ms := cachedMachineState(c, u.MachineID)
3838
if ms == nil {
3939
ms = &machine.MachineState{ID: u.MachineID}
4040
}
4141

4242
return machineFullLegend(*ms, full)
4343
},
44-
"state": func(u schema.Unit, full bool) string {
44+
"state": func(c *FleetClient, u schema.Unit, full bool) string {
4545
if suToGlobal(u) || u.CurrentState == "" {
4646
return "-"
4747
}
4848
return u.CurrentState
4949
},
50-
"hash": func(u schema.Unit, full bool) string {
50+
"hash": func(c *FleetClient, u schema.Unit, full bool) string {
5151
uf := schema.MapSchemaUnitOptionsToUnitFile(u.Options)
5252
if !full {
5353
return uf.Hash().Short()
5454
}
5555
return uf.Hash().String()
5656
},
57-
"desc": func(u schema.Unit, full bool) string {
57+
"desc": func(c *FleetClient, u schema.Unit, full bool) string {
5858
uf := schema.MapSchemaUnitOptionsToUnitFile(u.Options)
5959
d := uf.Description()
6060
if d == "" {
@@ -65,14 +65,14 @@ var (
6565
}
6666
)
6767

68-
type unitToField func(u schema.Unit, full bool) string
68+
type unitToField func(c *FleetClient, u schema.Unit, full bool) string
6969

7070
// ListUnitFiles prints all Deis-related unit files to Stdout
7171
func (c *FleetClient) ListUnitFiles() (err error) {
7272
var sortable sort.StringSlice
7373
units := make(map[string]*schema.Unit, 0)
7474

75-
us, err := cAPI.Units()
75+
us, err := c.Fleet.Units()
7676
if err != nil {
7777
return err
7878
}
@@ -84,12 +84,12 @@ func (c *FleetClient) ListUnitFiles() (err error) {
8484
}
8585
}
8686
sortable.Sort()
87-
printUnitFiles(units, sortable)
87+
printUnitFiles(c, units, sortable)
8888
return
8989
}
9090

9191
// printUnitFiles writes unit files to stdout using a tabwriter
92-
func printUnitFiles(units map[string]*schema.Unit, sortable sort.StringSlice) {
92+
func printUnitFiles(client *FleetClient, units map[string]*schema.Unit, sortable sort.StringSlice) {
9393
cols := strings.Split(defaultListUnitFilesFields, ",")
9494
for _, s := range cols {
9595
if _, ok := listUnitsFields[s]; !ok {
@@ -101,7 +101,7 @@ func printUnitFiles(units map[string]*schema.Unit, sortable sort.StringSlice) {
101101
var f []string
102102
u := units[name]
103103
for _, c := range cols {
104-
f = append(f, listUnitFilesFields[c](*u, false))
104+
f = append(f, listUnitFilesFields[c](client, *u, false))
105105
}
106106
fmt.Fprintln(out, strings.Join(f, "\t"))
107107
}

deisctl/backend/fleet/list_units.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,46 @@ const (
2222
defaultListUnitsFields = "unit,machine,load,active,sub"
2323
)
2424

25-
type usToField func(us *schema.UnitState, full bool) string
25+
type usToField func(c *FleetClient, us *schema.UnitState, full bool) string
2626

2727
var (
2828
out *tabwriter.Writer
2929
listUnitsFields = map[string]usToField{
30-
"unit": func(us *schema.UnitState, full bool) string {
30+
"unit": func(c *FleetClient, us *schema.UnitState, full bool) string {
3131
if us == nil {
3232
return "-"
3333
}
3434
return us.Name
3535
},
36-
"load": func(us *schema.UnitState, full bool) string {
36+
"load": func(c *FleetClient, us *schema.UnitState, full bool) string {
3737
if us == nil {
3838
return "-"
3939
}
4040
return us.SystemdLoadState
4141
},
42-
"active": func(us *schema.UnitState, full bool) string {
42+
"active": func(c *FleetClient, us *schema.UnitState, full bool) string {
4343
if us == nil {
4444
return "-"
4545
}
4646
return us.SystemdActiveState
4747
},
48-
"sub": func(us *schema.UnitState, full bool) string {
48+
"sub": func(c *FleetClient, us *schema.UnitState, full bool) string {
4949
if us == nil {
5050
return "-"
5151
}
5252
return us.SystemdSubState
5353
},
54-
"machine": func(us *schema.UnitState, full bool) string {
54+
"machine": func(c *FleetClient, us *schema.UnitState, full bool) string {
5555
if us == nil || us.MachineID == "" {
5656
return "-"
5757
}
58-
ms := cachedMachineState(us.MachineID)
58+
ms := cachedMachineState(c, us.MachineID)
5959
if ms == nil {
6060
ms = &machine.MachineState{ID: us.MachineID}
6161
}
6262
return machineFullLegend(*ms, full)
6363
},
64-
"hash": func(us *schema.UnitState, full bool) string {
64+
"hash": func(c *FleetClient, us *schema.UnitState, full bool) string {
6565
if us == nil || us.Hash == "" {
6666
return "-"
6767
}
@@ -77,7 +77,7 @@ var (
7777
func (c *FleetClient) ListUnits() (err error) {
7878
var states []*schema.UnitState
7979

80-
unitStates, err := cAPI.UnitStates()
80+
unitStates, err := c.Fleet.UnitStates()
8181
if err != nil {
8282
return err
8383
}
@@ -90,12 +90,12 @@ func (c *FleetClient) ListUnits() (err error) {
9090
}
9191
}
9292
}
93-
printUnits(states)
93+
printUnits(c, states)
9494
return
9595
}
9696

9797
// printUnits writes units to stdout using a tabwriter
98-
func printUnits(states []*schema.UnitState) {
98+
func printUnits(client *FleetClient, states []*schema.UnitState) {
9999
cols := strings.Split(defaultListUnitsFields, ",")
100100
for _, s := range cols {
101101
if _, ok := listUnitsFields[s]; !ok {
@@ -106,7 +106,7 @@ func printUnits(states []*schema.UnitState) {
106106
for _, us := range states {
107107
var f []string
108108
for _, c := range cols {
109-
f = append(f, listUnitsFields[c](us, false))
109+
f = append(f, listUnitsFields[c](client, us, false))
110110
}
111111
fmt.Fprintln(out, strings.Join(f, "\t"))
112112
}

deisctl/backend/fleet/registry.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/coreos/fleet/client"
1111
"github.com/coreos/fleet/etcd"
12-
"github.com/coreos/fleet/machine"
1312
"github.com/coreos/fleet/pkg"
1413
"github.com/coreos/fleet/registry"
1514
"github.com/coreos/fleet/ssh"
@@ -41,13 +40,6 @@ recommended to upgrade fleetctl to prevent incompatibility issues.
4140
`
4241
)
4342

44-
// global API client used by commands
45-
var cAPI client.API
46-
47-
// used to cache MachineStates
48-
var machineStates map[string]*machine.MachineState
49-
var requestTimeout = time.Duration(10) * time.Second
50-
5143
func getTunnelFlag() string {
5244
tun := Flags.Tunnel
5345
if tun != "" && !strings.Contains(tun, ":") {

deisctl/backend/fleet/ssh.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (c *FleetClient) SSH(name string) (err error) {
1818

1919
timeout := time.Duration(Flags.SSHTimeout*1000) * time.Millisecond
2020

21-
ms, err := machineState(name)
21+
ms, err := machineState(c, name)
2222
if err != nil {
2323
return err
2424
}
@@ -31,13 +31,13 @@ func (c *FleetClient) SSH(name string) (err error) {
3131
return err
3232
}
3333

34-
machID, err := findUnit(units[0])
34+
machID, err := findUnit(c, units[0])
3535

3636
if err != nil {
3737
return err
3838
}
3939

40-
ms, err = machineState(machID)
40+
ms, err = machineState(c, machID)
4141

4242
if err != nil || ms == nil {
4343
return err
@@ -62,15 +62,15 @@ func (c *FleetClient) SSH(name string) (err error) {
6262

6363
// runCommand will attempt to run a command on a given machine. It will attempt
6464
// to SSH to the machine if it is identified as being remote.
65-
func runCommand(cmd string, machID string) (retcode int) {
65+
func runCommand(c *FleetClient, cmd string, machID string) (retcode int) {
6666
var err error
6767
if machine.IsLocalMachineID(machID) {
6868
retcode, err = runLocalCommand(cmd)
6969
if err != nil {
7070
fmt.Printf("Error running local command: %v\n", err)
7171
}
7272
} else {
73-
ms, err := machineState(machID)
73+
ms, err := machineState(c, machID)
7474
if err != nil || ms == nil {
7575
fmt.Printf("Error getting machine IP: %v\n", err)
7676
} else {
@@ -125,8 +125,8 @@ func runRemoteCommand(cmd string, addr string, timeout time.Duration) (exit int,
125125
}
126126

127127
// findUnits returns the machine ID of a running unit
128-
func findUnit(name string) (machID string, err error) {
129-
u, err := cAPI.Unit(name)
128+
func findUnit(c *FleetClient, name string) (machID string, err error) {
129+
u, err := c.Fleet.Unit(name)
130130
if err != nil {
131131
return "", fmt.Errorf("Error retrieving Unit %s: %v", name, err)
132132
}
@@ -142,8 +142,8 @@ func findUnit(name string) (machID string, err error) {
142142
return u.MachineID, nil
143143
}
144144

145-
func machineState(machID string) (*machine.MachineState, error) {
146-
machines, err := cAPI.Machines()
145+
func machineState(c *FleetClient, machID string) (*machine.MachineState, error) {
146+
machines, err := c.Fleet.Machines()
147147
if err != nil {
148148
return nil, err
149149
}
@@ -158,16 +158,16 @@ func machineState(machID string) (*machine.MachineState, error) {
158158
// cachedMachineState makes a best-effort to retrieve the MachineState of the given machine ID.
159159
// It memoizes MachineState information for the life of a fleetctl invocation.
160160
// Any error encountered retrieving the list of machines is ignored.
161-
func cachedMachineState(machID string) (ms *machine.MachineState) {
162-
if machineStates == nil {
163-
machineStates = make(map[string]*machine.MachineState)
164-
ms, err := cAPI.Machines()
161+
func cachedMachineState(c *FleetClient, machID string) (ms *machine.MachineState) {
162+
if c.machineStates == nil {
163+
c.machineStates = make(map[string]*machine.MachineState)
164+
ms, err := c.Fleet.Machines()
165165
if err != nil {
166166
return nil
167167
}
168168
for i, m := range ms {
169-
machineStates[m.ID] = &ms[i]
169+
c.machineStates[m.ID] = &ms[i]
170170
}
171171
}
172-
return machineStates[machID]
172+
return c.machineStates[machID]
173173
}

deisctl/backend/fleet/status.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ func (c *FleetClient) Status(target string) (err error) {
1212
return
1313
}
1414
for _, unit := range units {
15-
printUnitStatus(unit)
15+
printUnitStatus(c, unit)
1616
fmt.Println()
1717
}
1818
return
1919
}
2020

2121
// printUnitStatus displays the systemd status for a given unit
22-
func printUnitStatus(name string) int {
23-
u, err := cAPI.Unit(name)
22+
func printUnitStatus(c *FleetClient, name string) int {
23+
u, err := c.Fleet.Unit(name)
2424
if suToGlobal(*u) {
2525
fmt.Fprintf(os.Stderr, "Unable to get status for global unit %s. Check the status on the host using systemctl.\n", name)
2626
return 1
@@ -37,5 +37,5 @@ func printUnitStatus(name string) int {
3737
return 1
3838
}
3939
cmd := fmt.Sprintf("systemctl status -l %s", name)
40-
return runCommand(cmd, u.MachineID)
40+
return runCommand(c, cmd, u.MachineID)
4141
}

0 commit comments

Comments
 (0)