forked from hortonworks/cloudbreak-deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployer.go
155 lines (136 loc) · 3.02 KB
/
deployer.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
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"fmt"
"hash"
"io"
"log"
"os"
"runtime"
"strings"
v "github.com/hashicorp/go-version"
"github.com/progrium/go-basher"
"github.com/skratchdot/open-golang/open"
)
var Version string
var GitRevision string
func version() (v string) {
if GitRevision == "" {
v = Version
} else {
v = fmt.Sprintf("%s-%s", Version, GitRevision)
}
return
}
func fatal(msg string) {
println("!!", msg)
os.Exit(2)
}
func BinVersion(args []string) {
fmt.Println(version())
}
// Copied from https://github.com/progrium/go-basher/blob/master/basher.go#L37
// to be able to add exporting DEBUG and TRACE, as we don't inherit anything
// from parent bash
func application(
funcs map[string]func([]string),
scripts []string,
loader func(string) ([]byte, error),
copyEnv bool) {
bashPath := ".deps/bin/bash-" + runtime.GOOS
RestoreAsset(".", bashPath)
bash, err := basher.NewContext(bashPath, os.Getenv("DEBUG") != "")
bash.Export("DEBUG", os.Getenv("DEBUG"))
bash.Export("TRACE", os.Getenv("TRACE"))
bash.Export("HOME", os.Getenv("HOME"))
bash.Export("CBD_DEFAULT_PROFILE", os.Getenv("CBD_DEFAULT_PROFILE"))
for _, e := range os.Environ() {
if strings.HasPrefix(e, "DOCKER_") {
v := strings.Split(e, "=")
bash.Export(v[0], v[1])
}
}
if err != nil {
log.Fatal(err)
}
for name, fn := range funcs {
bash.ExportFunc(name, fn)
}
if bash.HandleFuncs(os.Args) {
os.Exit(0)
}
for _, script := range scripts {
bash.Source(script, loader)
}
if copyEnv {
bash.CopyEnv()
}
status, err := bash.Run("main", os.Args[1:])
if err != nil {
log.Fatal(err)
}
os.Exit(status)
}
func OpenBrowser(args []string) {
err := open.Start(args[0])
if err != nil {
fatal("Can't open browser: '" + err.Error())
}
}
func VersionCompare(args []string) {
v0, err := v.NewVersion(args[0])
if err != nil {
fatal("Can't parse version string" + err.Error())
}
v1, err := v.NewVersion(args[1])
if err != nil {
fatal("Can't parse version string" + err.Error())
}
fmt.Println(v0.Compare(v1))
}
func Checksum(args []string) {
if len(args) < 1 {
fatal("No algorithm specified")
}
var h hash.Hash
switch args[0] {
case "md5":
h = md5.New()
case "sha1":
h = sha1.New()
case "sha256":
h = sha256.New()
default:
fatal("Algorithm '" + args[0] + "' is unsupported")
}
io.Copy(h, os.Stdin)
fmt.Printf("%x\n", h.Sum(nil))
}
func main() {
if len(os.Args) == 2 && os.Args[1] == "--version" {
fmt.Println("CloudBreak Deployer:", version())
os.Exit(0)
}
application(map[string]func([]string){
"checksum": Checksum,
"bin-version": BinVersion,
"browse": OpenBrowser,
"version-compare": VersionCompare,
}, []string{
"include/aws-roles.bash",
"include/circle.bash",
"include/cloudbreak.bash",
"include/compose.bash",
"include/cmd.bash",
"include/color.bash",
"include/deployer.bash",
"include/deps.bash",
"include/docker.bash",
"include/env.bash",
"include/fn.bash",
"include/migrate.bash",
"include/module.bash",
}, Asset, false)
}