/
main.go
84 lines (70 loc) · 1.73 KB
/
main.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
package main
import (
"os"
"time"
"github.com/Sirupsen/logrus"
"github.com/cloudnautique/dynamic-drone-nodes/version"
"github.com/codegangsta/cli"
"github.com/drone/drone-go/drone"
)
const (
metadataURL = "http://rancher-metadata/2015-12-19"
)
func main() {
app := cli.NewApp()
app.Name = "dynamic-drone-nodes"
app.Usage = "Dynamically add and remove Drone CI nodes"
app.Action = appInit
app.Version = version.VERSION
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "drone-token",
Usage: "API token for Drone CI",
EnvVar: "DRONE_TOKEN",
},
cli.StringFlag{
Name: "drone-url",
Usage: "URL for the Drone CI server",
EnvVar: "DRONE_URL",
},
cli.IntFlag{
Name: "poll-interval",
Usage: "Interval in (s) to poll dynamic pool",
Value: 300,
},
}
app.Run(os.Args)
}
func appInit(c *cli.Context) {
droneToken := c.String("drone-token")
if droneToken == "" {
logrus.Fatal("Drone API token is missing")
}
droneURL := c.String("drone-url")
if droneURL == "" {
logrus.Fatal("Drone CI server URL is missing")
}
if len(c.Args()) < 0 {
logrus.Fatal("No path to watch found")
}
path := c.Args()[0]
droneClient := &Drone{drone.NewClientToken(droneURL, droneToken)}
poolClient, err := NewPoolClient(metadataURL)
if err != nil {
logrus.Fatal(err)
}
run(droneClient, poolClient, path, c.Int("poll-interval"))
}
func run(droneClient *Drone, poolClient DynamicNodePool, path string, interval int) {
logrus.Infof("Path: %s", path)
for {
poolNodes, err := poolClient.ListNodes(path)
if err != nil {
logrus.Fatal(err)
}
if err = droneClient.ReconcileNodeDifferences(convertNodeToMap(poolNodes)); err != nil {
logrus.Fatal(err)
}
time.Sleep(time.Duration(interval) * time.Second)
}
}