-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
162 lines (135 loc) · 4.8 KB
/
main.go
File metadata and controls
162 lines (135 loc) · 4.8 KB
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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/mkobaly/esdeploy/elastic"
"log"
"github.com/alecthomas/kingpin"
"github.com/fatih/color"
)
var (
version string
)
var (
app = kingpin.New("esdeploy", "A command-line deployment tool to version ElasticSearch.")
appUser = app.Flag("username", "Username to authenticate with").Short('u').String()
appPassword = app.Flag("password", "Password to authenticat with").Short('p').String()
appInsecure = app.Flag("insecure", "Ignore SSL certificate warnings").Short('k').Bool()
drCmd = app.Command("dryrun", "Only lists out changes that would be made to ElasticSearch.")
drURL = drCmd.Arg("url", "Elastic Search URL to run against").Required().String()
drPath = drCmd.Flag("folder", "Folder containing schema js files").Short('f').Default(".").String()
validateCmd = app.Command("validate", "Performs a validation of all files to ensure they are properly formatted")
validatePath = validateCmd.Flag("folder", "Folder containing schema js files").Short('f').Default(".").String()
deployCmd = app.Command("deploy", "Deploy elastic search changes")
dURL = deployCmd.Arg("url", "Elastic Search URL to run against").Required().String()
dPath = deployCmd.Flag("folder", "Folder containing schema js files").Short('f').Default(".").String()
dSilent = deployCmd.Flag("silent", "Don't prompt for confirmation, run silently").Short('s').Bool()
dShards = deployCmd.Flag("shards", "Default number of shards to use for new indexes if tokenized {{shards}}").Default("5").String()
dReplicas = deployCmd.Flag("replicas", "Default number of shard replicas if tokenized {{replicas}}").Default("1").String()
seedCmd = app.Command("seed", "Seed elastic search with data stored in json files")
seedURL = seedCmd.Arg("url", "Elastic Search URL to run against").Required().String()
seedPath = seedCmd.Flag("folder", "Folder containing json data files").Short('f').Default(".").String()
versionCmd = app.Command("version", "Display version of esdeploy")
)
func main() {
var cred elastic.Creds
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case versionCmd.FullCommand():
color.Cyan("version %v", version)
os.Exit(0)
//Validation
case validateCmd.FullCommand():
if *validatePath == "" {
*validatePath, _ = os.Getwd()
}
exit := 0
color.Cyan("Running validation against folder %v", *validatePath)
esRunner := elastic.NewRunner(*validatePath, nil)
results := esRunner.Validate()
for _, r := range results {
if !r.IsValid {
color.Red("FILE INVALID: %s", r.File)
exit = 1
continue
}
color.Green("File Valid: %s", r.File)
}
color.Cyan("Validation completed")
os.Exit(exit)
//Dry run
case drCmd.FullCommand():
if *drPath == "" {
*drPath, _ = os.Getwd()
}
color.Cyan("Running dry run against %v", *drURL)
color.Cyan("Folder containing schema files is %v", *drPath)
schemaChanger := elastic.NewEsSchemaChanger(*drURL, cred, *appInsecure)
esRunner := elastic.NewRunner(*drPath, schemaChanger)
results, err := esRunner.DryRun()
if err != nil {
log.Fatal(err)
}
for _, r := range results {
color.Green("%v", r)
}
color.Cyan("Dry Run completed")
//Full deployment
case deployCmd.FullCommand():
if *dPath == "" {
*dPath, _ = os.Getwd()
}
if *appUser != "" && *appPassword != "" {
cred = elastic.Creds{Username: *appUser, Password: *appPassword}
}
color.Cyan("About to perform deployment against %v", *dURL)
color.Cyan("Folder containing schema files is %v", *dPath)
if *dSilent == false {
color.Yellow("Do you want to proceed? Yes(Y) or No(N)")
var input string
fmt.Scanln(&input)
if strings.ToUpper(input) == "N" {
os.Exit(0)
}
}
schemaChanger := elastic.NewEsSchemaChanger(*dURL, cred, *appInsecure)
esRunner := elastic.NewRunner(*dPath, schemaChanger)
shards, err := strconv.Atoi(*dShards)
if err != nil {
shards = -1
}
replicas, err := strconv.Atoi(*dReplicas)
if err != nil {
replicas = -1
}
results, err := esRunner.Deploy(shards, replicas)
if err != nil {
for _, r := range results {
color.Red("%v", r)
}
color.Red(err.Error())
os.Exit(1)
}
for _, r := range results {
color.Green("%v", r)
}
color.Cyan("Deploy completed")
//Seed data
case seedCmd.FullCommand():
if *seedPath == "" {
*seedPath, _ = os.Getwd()
}
color.Cyan("Seeding data against %v", *seedURL)
color.Cyan("Folder containing data files is %v", *seedPath)
seeder := elastic.NewSeeder(*seedPath, *seedURL, cred)
results, err := seeder.Seed()
if err != nil {
log.Fatal(err)
}
for _, r := range results {
color.Green("%v", r)
}
color.Cyan("Seeding completed")
}
}