-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
203 lines (181 loc) · 5.04 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"os"
"runtime"
"time"
"github.com/MainframeHQ/swarmer/admin"
"github.com/MainframeHQ/swarmer/util"
"github.com/docker/docker/client"
log "github.com/camronlevanger/logrus"
"github.com/go-errors/errors"
"gopkg.in/urfave/cli.v1"
"github.com/MainframeHQ/swarmer/cmd"
models "github.com/MainframeHQ/swarmer/models"
)
// DATAPATH is a best effort at finding the src directory of this project in order
// to make use of the Dockerfile and docker-compose.yml files etc..
var DATAPATH = os.Getenv("GOPATH") + "/src/github.com/MainframeHQ/swarmer/"
// APPNAME is aptly named.
const APPNAME = "swarmer"
func init() {
log.SetFormatter(
&log.TextFormatter{
DisableColors: true,
FullTimestamp: false,
},
)
if runtime.GOOS == "darwin" {
log.Debug("Mac OS detected")
} else if runtime.GOOS == "linux" {
log.Debug("Linux detected")
} else {
log.Fatal(APPNAME+" does not support the %s operating system at this time.", runtime.GOOS)
}
}
func main() {
path := DATAPATH + "docker"
config := models.Config{}
config.Path = path
var start *cmd.StartCommand
var stop *cmd.StopCommand
var status *cmd.StatusCommand
dockerClient, err := client.NewClientWithOpts(client.WithVersion("1.38"))
if err != nil {
panic("Must have Docker compatible with API 1.38: " + err.Error())
}
adminClient := admin.GetClient()
lookup := util.GetLookup()
parser := util.GetConfigParser()
app := cli.NewApp()
app.Name = APPNAME
app.Version = "0.1"
app.Usage = "Run a local, containerized Swarm cluster comprised of (N) peered nodes."
app.Compiled = time.Now()
app.Authors = []cli.Author{
{
Name: "Camron G. Levanger",
Email: "camron@mainframe.com",
},
}
app.Copyright = "(c) 2018 Mainframe"
app.EnableBashCompletion = true
app.Commands = []cli.Command{
{
Name: "start",
Aliases: []string{"s"},
Usage: "Start the Swarm cluster",
Action: func(c *cli.Context) error {
start = cmd.GetStartCommand(config, dockerClient, adminClient, lookup, parser)
err := start.Start(c)
return err
},
},
{
Name: "stop",
Aliases: []string{"t"},
Usage: "Stop the Swarm cluster",
Action: func(c *cli.Context) error {
stop = cmd.GetStopCommand(config, dockerClient)
err := stop.Stop(c)
return err
},
},
{
Name: "status",
Aliases: []string{"a"},
Usage: "Get a list of running nodes",
Action: func(c *cli.Context) error {
status = cmd.GetStatusCommand(config, dockerClient, adminClient)
err := status.Status(c)
return err
},
},
}
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "nodes, n",
Value: 1,
Usage: "how many swarm nodes to start",
EnvVar: "DEVCLUSTER_NODES",
Destination: &config.Nodes,
},
cli.StringFlag{
Name: "config, C",
Value: "",
Usage: "load a YAML or TOML config file rather than supplying args and flags",
EnvVar: "DEVCLUSTER_CONFIG",
Destination: &config.Config,
},
cli.StringFlag{
Name: "repo, r",
Value: "",
Usage: "URL to Git repository containing Swarm source to be built",
EnvVar: "DEVCLUSTER_REPO",
Destination: &config.Repo,
},
cli.StringFlag{
Name: "srcdir, d",
Value: "",
Usage: "build source from given directory rather than from Git repo",
EnvVar: "DEVCLUSTER_SRC",
Destination: &config.LocalSrc,
},
cli.StringFlag{
Name: "checkout, c",
Value: "",
Usage: "branch, tag, or hash to checkout from the Git repo",
EnvVar: "DEVCLUSTER_CHECKOUT",
Destination: &config.Checkout,
},
cli.StringFlag{
Name: "ens-api, e",
Value: "",
Usage: "this value is passed directly to Swarm ens-api flag",
EnvVar: "DEVCLUSTER_ENS",
Destination: &config.ENS,
},
cli.BoolFlag{
Name: "geth, g",
Usage: "run Geth as well as swarm",
EnvVar: "DEVCLUSTER_GETH",
Destination: &config.Geth,
},
cli.StringFlag{
Name: "docker_log, b",
Value: "docker_log",
Usage: "local logfile for Docker build logs",
EnvVar: "DEVCLUSTER_DOCKER_LOG",
Destination: &config.DockerLog,
},
cli.StringFlag{
Name: "swarm_log, s",
Value: "swarm_log",
Usage: "local logfile for Swarm logs",
EnvVar: "DEVCLUSTER_SWARM_LOG",
Destination: &config.SwarmLog,
},
cli.StringFlag{
Name: "add, a",
Value: "",
Usage: "add directory to the swarmer containers",
EnvVar: "DEVCLUSTER_ADD",
Destination: &config.Add,
},
cli.BoolFlag{
Name: "follow, f",
Usage: "remain attached and display Swarm logs",
EnvVar: "DEVCLUSTER_FOLLOW",
Destination: &config.Follow,
},
}
app.Action = func(c *cli.Context) error {
// this uses the start command as default if no command given
start = cmd.GetStartCommand(config, dockerClient, adminClient, lookup, parser)
err := start.Start(c)
return errors.Wrap(err, 1)
}
err = app.Run(os.Args)
if err != nil {
log.Fatalf("Error starting swarm nodes: %+v", err)
}
}