-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
197 lines (183 loc) · 4.53 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
package main
import (
"fmt"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/urfave/cli"
"os"
"path/filepath"
)
func checkVariables() error {
backend := viper.GetString("BACKEND")
if backend == "openstack" {
viper.SetEnvPrefix("OS")
viper.AutomaticEnv()
u := viper.GetString("USERNAME")
p := viper.GetString("PASSWORD")
t := viper.GetString("TENANT_ID")
if (u == "") || (p == "") || (t == "") {
return errors.New("Missing secret configuration value(s).")
}
} else if backend == "aws" {
viper.SetEnvPrefix("AWS")
viper.AutomaticEnv()
k := viper.GetString("USERKEY")
s := viper.GetString("SECRET")
if (k == "") || (s == "") {
return errors.New("Missing secret configuration value(s).")
}
}
return nil
}
func starthere(jobname, config_dir, amiid, region string) int {
var vm TunirVM
var commands []string
var result ResultSet
currentruninfo := make(map[string]string)
vmdict := make(map[string]TunirVM)
commandfile := filepath.Join(config_dir, fmt.Sprintf("%s.txt", jobname))
if _, err := os.Stat(commandfile); os.IsNotExist(err) {
fmt.Println("Missing commands file for job:", jobname)
return 100
}
viper.SetConfigName(jobname)
viper.AddConfigPath(config_dir)
err := viper.ReadInConfig()
if err != nil {
fmt.Println("No configuration file loaded - using defaults")
}
err = checkVariables()
if err != nil {
fmt.Println(err)
return 111
}
viper.SetDefault("PORT", "22")
viper.SetDefault("USER", "fedora")
viper.SetDefault("NUMBER", 1)
viper.SetDefault("NODELETE", false)
viper.SetDefault("AWS_AMI", amiid)
viper.SetDefault("AWS_REGION", region)
backend := viper.GetString("BACKEND")
fmt.Println("Starts a new Tunir Job.\n")
if backend == "openstack" {
number := viper.GetInt("NUMBER")
for i := 1; i <= number; i++ {
vmname := fmt.Sprintf("gotun-%d", i)
vm, err = BootInstanceOS(vmname)
if err != nil {
// We do not have an instance
fmt.Println("We do not have an instance.")
goto ERROR_NOIP
}
// First 180 seconds timeout for the vm to come up
err = Poll(180, vm)
if err != nil {
fmt.Println("Failed to ssh into the vm.")
name := fmt.Sprintf("vm%d", i)
vmdict[name] = vm
goto ERROR_NOIP
}
// All good, add in the dict
name := fmt.Sprintf("vm%d", i)
vmdict[name] = vm
}
} else if backend == "bare" {
localvms := viper.GetStringMapString("VMS")
for k := range localvms {
vm = TunirVM{IP: localvms[k], KeyFile: viper.GetString("key"),
Port: viper.GetString("PORT")}
vmdict[k] = vm
}
} else if backend == "aws" {
vm, err = BootInstanceAWS()
if err != nil {
// We do not have an instance
fmt.Println("We do not have an instance.")
goto ERROR_NOIP
}
err = Poll(300, vm)
if err != nil {
fmt.Println("Failed to ssh into the vm.")
vmdict["vm1"] = vm
goto ERROR_NOIP
}
vmdict["vm1"] = vm
}
// Now time to dump IP/SSH information on a file.
for k := range vmdict {
vm = vmdict[k]
ip, _ := vm.GetDetails()
currentruninfo[k] = ip
}
currentruninfo["user"] = viper.GetString("USER")
currentruninfo["keyfile"] = viper.GetString("key")
writeIPinformation(currentruninfo)
commands = ReadCommands(commandfile)
result = ExecuteTests(commands, vmdict)
ERROR_NOIP:
if backend == "openstack" || backend == "aws" {
// Time to destroy the server
// Do it over a loop
if viper.GetBool("NODELETE") == false {
for k := range vmdict {
vm = vmdict[k]
vm.Delete()
}
}
}
printResultSet(result)
if !result.Status {
return 200
}
return 0
}
func createApp() *cli.App {
app := cli.NewApp()
app.EnableBashCompletion = true
app.Name = "gotun"
app.Version = "0.1.0"
app.Usage = "The Tunir in golang."
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "job",
Value: "",
Usage: "the job name",
},
cli.StringFlag{
Name: "config-dir",
Value: "",
Usage: "the directory having configuration (default current)",
},
cli.StringFlag{
Name: "ami-id",
Value: "",
Usage: "the AMI ID for AWS jobs",
},
cli.StringFlag{
Name: "region",
Value: "",
Usage: "Region name for AWS based job",
},
}
app.Action = func(c *cli.Context) error {
file_path := c.GlobalString("job")
config_dir := c.GlobalString("config-dir")
amiid := c.GlobalString("ami-id")
region := c.GlobalString("region")
if config_dir == "" {
config_dir = "./"
}
if file_path != "" {
exitcode := starthere(file_path, config_dir, amiid, region)
os.Exit(exitcode)
}
return nil
}
return app
}
func main() {
app := createApp()
if err := app.Run(os.Args); err != nil {
check(err)
}
}