-
Notifications
You must be signed in to change notification settings - Fork 26
/
package.go
168 lines (141 loc) · 3.38 KB
/
package.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"text/template"
)
var (
configPath string
config Config
)
type (
ProjectConfig struct {
Dir *string `json:"dir"`
Installer *string `json:"installer"`
Location *string `json:"location"`
Env string `json:"env"`
}
Config struct {
Projects map[string]ProjectConfig `json:"projects"`
}
Project struct {
name string
config ProjectConfig
}
)
func NewProject(name string, config ProjectConfig) *Project {
p := &Project{name: name, config: config}
return p
}
func (project *Project) checkConfig() {
if project.config.Dir == nil {
panic(fmt.Errorf("missing parameter[dir] of the project[%s]", project.name))
}
if project.config.Installer == nil {
panic(fmt.Errorf("missing parameter[installer] of the project[%s]", project.name))
}
if project.config.Location == nil {
panic(fmt.Errorf("missing parameter[location] of the project[%s]", project.name))
}
}
func (project *Project) build() {
project.checkConfig()
script := `#!/bin/bash
set -e
cwd=$(pwd)
tmpdir=$(mktemp -d)
targetdir="$tmpdir/target"
mkdir -p $targetdir
datatar="$targetdir/data.tar.gz"
targettar="$tmpdir/target.tar.gz"
tar czf $datatar -C $cwd/{{.Dir}} .
cp {{.Installer}} $targetdir
tar cf $targettar -C $targetdir/ .
cat >> $tmpdir/setup.sh <<'EOF'
#!/bin/bash
PATH=/bin:/usr/bin
line=$(wc -l $0 | awk '{print $1}')
line=$((line - 12))
tmpdir=/home/zstack/zvr/target
rm -rf $tmpdir
mkdir -p $tmpdir
tail -n $line $0 | tar x -C $tmpdir
cd $tmpdir
{{.Env}} bash {{.InstallScript}} $@
ret=$?
rm -rf $tmpdir
exit $ret
EOF
mkdir -p $(dirname {{.BinaryPath}})
cat $tmpdir/setup.sh $targettar > {{.BinaryPath}}
chmod a+x {{.BinaryPath}}
rm -rf $tmpdir
`
binaryPath := fmt.Sprintf("%s/%s.bin", *project.config.Location, project.name)
context := map[string]string{
"Dir": *project.config.Dir,
"Installer": *project.config.Installer,
"BinaryPath": binaryPath,
"InstallScript": path.Base(*project.config.Installer),
"Env": project.config.Env,
}
tmpl, err := template.New("script").Parse(script)
if err != nil {
panic(err)
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, context)
if err != nil {
panic(err)
}
if out, err := exec.Command("sh", "-c", buf.String()).CombinedOutput(); err != nil {
panic(fmt.Errorf("failed to execute script:\n%s\n%s\n%s", buf.String(), out, err))
} else {
fmt.Println(string(out))
}
fmt.Println(fmt.Sprintf("successfully built the project [%s] to %s", project.name, binaryPath))
}
func init() {
flag.StringVar(&configPath, "conf", "", "path to the configuration file")
flag.Parse()
if flag.NArg() > 0 {
flag.Usage()
fmt.Printf("unknown options %v\n", flag.Args())
os.Exit(1)
}
if configPath == "" {
flag.Usage()
fmt.Printf("option [-conf] is required and cannot be an empty string\n")
os.Exit(1)
}
}
func build() {
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}()
f, err := ioutil.ReadFile(configPath)
if err != nil {
panic(fmt.Errorf("unable to read the config file[%s], %s", configPath, err))
}
config := Config{}
err = json.Unmarshal(f, &config)
if err != nil {
panic(fmt.Errorf("unable to JSON unmarshal the config file[%s], %s", configPath, err))
}
for key, value := range config.Projects {
p := NewProject(key, value)
p.build()
}
}
func main() {
build()
}