/
main.go
144 lines (126 loc) · 3.44 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
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
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/codegangsta/cli"
"github.com/coreos/go-semver/semver"
"github.com/fatih/color"
"github.com/garyburd/redigo/redis"
"github.com/octoblu/go-simple-etcd-client/etcdclient"
"github.com/octoblu/governator/deployer"
De "github.com/tj/go-debug"
)
var debug = De.Debug("governator:main")
func main() {
app := cli.NewApp()
app.Name = "governator"
app.Version = version()
app.Action = run
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "etcd-uri, e",
EnvVar: "GOVERNATOR_ETCD_URI",
Usage: "Etcd server to deploy to",
},
cli.StringFlag{
Name: "redis-uri, r",
EnvVar: "GOVERNATOR_REDIS_URI",
Usage: "Redis server to pull deployments from",
},
cli.StringFlag{
Name: "redis-queue, q",
EnvVar: "GOVERNATOR_REDIS_QUEUE",
Usage: "Redis queue to pull deployments from",
},
cli.StringFlag{
Name: "deploy-state-uri",
EnvVar: "DEPLOY_STATE_URI",
Usage: "Deploy state uri, it should include authentication.",
},
cli.StringFlag{
Name: "cluster",
EnvVar: "CLUSTER",
Usage: "The current running cluster",
},
}
app.Run(os.Args)
}
func run(context *cli.Context) {
etcdURI, redisURI, redisQueue, deployStateUri, cluster := getOpts(context)
etcdClient := getEtcdClient(etcdURI)
redisConn := getRedisConn(redisURI)
theDeployer := deployer.New(etcdClient, redisConn, redisQueue, deployStateUri, cluster)
sigTerm := make(chan os.Signal)
signal.Notify(sigTerm, syscall.SIGTERM)
sigTermReceived := false
go func() {
<-sigTerm
fmt.Println("SIGTERM received, waiting to exit")
sigTermReceived = true
}()
for {
if sigTermReceived {
fmt.Println("I'll be back.")
os.Exit(0)
}
debug("theDeployer.Run()")
err := theDeployer.Run()
if err != nil {
log.Panic("Run error", err)
}
time.Sleep(1 * time.Second)
}
}
func getOpts(context *cli.Context) (string, string, string, string, string) {
etcdURI := context.String("etcd-uri")
redisURI := context.String("redis-uri")
redisQueue := context.String("redis-queue")
deployStateUri := context.String("deploy-state-uri")
cluster := context.String("cluster")
if etcdURI == "" || redisURI == "" || redisQueue == "" || deployStateUri == "" || cluster == "" {
cli.ShowAppHelp(context)
if etcdURI == "" {
color.Red(" Missing required flag --etcd-uri or GOVERNATOR_ETCD_URI")
}
if redisURI == "" {
color.Red(" Missing required flag --redis-uri or GOVERNATOR_REDIS_URI")
}
if redisQueue == "" {
color.Red(" Missing required flag --redis-queue or GOVERNATOR_REDIS_QUEUE")
}
if deployStateUri == "" {
color.Red(" Missing required flag --deploy-state-uri or DEPLOY_STATE_URI")
}
if cluster == "" {
color.Red(" Missing required flag --cluster or CLUSTER")
}
os.Exit(1)
}
return etcdURI, redisURI, redisQueue, deployStateUri, cluster
}
func getEtcdClient(etcdURI string) etcdclient.EtcdClient {
etcdClient, err := etcdclient.Dial(etcdURI)
if err != nil {
log.Panicln("Error with etcdclient.New", err.Error())
}
return etcdClient
}
func getRedisConn(redisURI string) redis.Conn {
redisConn, err := redis.DialURL(redisURI)
if err != nil {
log.Panicln("Error with redis.DialURL", err.Error())
}
return redisConn
}
func version() string {
version, err := semver.NewVersion(VERSION)
if err != nil {
errorMessage := fmt.Sprintf("Error with version number: %v", VERSION)
log.Panicln(errorMessage, err.Error())
}
return version.String()
}