forked from harness/drone-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
155 lines (135 loc) · 2.98 KB
/
node.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"github.com/codegangsta/cli"
"github.com/drone/drone-go/drone"
)
var MachineCmd = cli.Command{
Name: "node",
Usage: "manage build nodes",
Subcommands: []cli.Command{
// Node List
{
Name: "ls",
Usage: "list all nodes",
Action: func(c *cli.Context) {
handle(c, NodeListCmd)
},
},
// Node Info
{
Name: "info",
Usage: "show node details",
Action: func(c *cli.Context) {
handle(c, NodeInfoCmd)
},
},
// Node Add
{
Name: "create",
Usage: "creates a node",
Action: func(c *cli.Context) {
handle(c, NodeCreateCmd)
},
Flags: []cli.Flag{
cli.StringFlag{
EnvVar: "DOCKER_HOST",
Name: "docker-host",
Usage: "docker daemon address",
Value: "",
},
cli.BoolFlag{
EnvVar: "DOCKER_TLS_VERIFY",
Name: "docker-tls-verify",
Usage: "docker daemon supports tlsverify",
},
cli.StringFlag{
EnvVar: "DOCKER_CERT_PATH",
Name: "docker-cert-path",
Usage: "docker certificate directory",
Value: "",
},
},
},
// Node Delete
{
Name: "rm",
Usage: "remove a node",
Action: func(c *cli.Context) {
handle(c, NodeDelCmd)
},
},
},
}
func NodeInfoCmd(c *cli.Context, client drone.Client) error {
id, err := strconv.ParseInt(c.Args().Get(0), 0, 64)
if err != nil {
return fmt.Errorf("Invalid or missing node id. Must be an integer")
}
node, err := client.Node(id)
if err != nil {
return fmt.Errorf("Endpoint is not yet supported")
}
fmt.Println(node.Addr)
return nil
}
func NodeListCmd(c *cli.Context, client drone.Client) error {
nodes, err := client.NodeList()
if err != nil {
return err
}
for _, node := range nodes {
fmt.Println(node.ID, node.Addr)
}
return nil
}
func NodeDelCmd(c *cli.Context, client drone.Client) error {
id, err := strconv.ParseInt(c.Args().Get(0), 0, 64)
if err != nil {
return fmt.Errorf("Invalid or missing node id. Must be an integer")
}
err = client.NodeDel(id)
if err != nil {
return err
}
fmt.Printf("Successfully removed node %d\n", id)
return nil
}
func NodeCreateCmd(c *cli.Context, client drone.Client) error {
node := drone.Node{
Addr: c.String("docker-host"),
Arch: "linux_amd64",
}
cert, _ := ioutil.ReadFile(filepath.Join(
c.String("docker-cert-path"),
"cert.pem",
))
key, _ := ioutil.ReadFile(filepath.Join(
c.String("docker-cert-path"),
"key.pem",
))
ca, _ := ioutil.ReadFile(filepath.Join(
c.String("docker-cert-path"),
"ca.pem",
))
if len(cert) == 0 || len(key) == 0 {
return fmt.Errorf("Error reading cert.pem or key.pem from %s",
c.String("docker-cert-path"))
}
node.Cert = string(cert)
node.Key = string(key)
// only use the certificate authority if tls verify
// is enabled for this docker host.
if c.Bool("docker-tls-verify") {
node.CA = string(ca)
}
_, err := client.NodePost(&node)
if err != nil {
return err
}
fmt.Printf("Successfully added %s\n", node.Addr)
return nil
}