/
generate-tarball.go
235 lines (205 loc) · 8.71 KB
/
generate-tarball.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package cmd
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"v.io/x/lib/cmdline"
)
var (
cmdSingle = &cmdline.Command{
Name: "single",
Short: "Generates an alluxio tarball",
Long: "Generates an alluxio tarball",
Runner: cmdline.RunnerFunc(single),
}
hadoopDistributionFlag string
targetFlag string
mvnArgsFlag string
webappDir = "core/server/common/src/main/webapp"
webappWar = "assembly/webapp.war"
)
func init() {
cmdSingle.Flags.StringVar(&hadoopDistributionFlag, "hadoop-distribution", "hadoop-2.2", "the hadoop distribution to build this Alluxio distribution tarball")
cmdSingle.Flags.StringVar(&targetFlag, "target", fmt.Sprintf("alluxio-%v.tar.gz", versionMarker),
fmt.Sprintf("an optional target name for the generated tarball. The default is alluxio-%v.tar.gz. The string %q will be substituted with the built version. "+
`Note that trailing ".tar.gz" will be stripped to determine the name for the Root directory of the generated tarball`, versionMarker, versionMarker))
cmdSingle.Flags.StringVar(&mvnArgsFlag, "mvn-args", "", `a comma-separated list of additional Maven arguments to build with, e.g. -mvn-args "-Pspark,-Dhadoop.version=2.2.0"`)
}
func single(_ *cmdline.Env, _ []string) error {
if err := generateTarball(hadoopDistributionFlag); err != nil {
return err
}
return nil
}
func replace(path, old, new string) {
data, err := ioutil.ReadFile(path)
if err != nil {
fmt.Fprintf(os.Stderr, "ReadFile() failed: %v\n", err)
os.Exit(1)
}
data = bytes.Replace(data, []byte(old), []byte(new), -1)
if err := ioutil.WriteFile(path, data, os.FileMode(0644)); err != nil {
fmt.Fprintf(os.Stderr, "WriteFile() failed: %v\n", err)
os.Exit(1)
}
}
func mkdir(path string) {
if err := os.MkdirAll(path, os.FileMode(0755)); err != nil {
fmt.Fprintf(os.Stderr, "MkdirAll() failed: %v\n", err)
os.Exit(1)
}
}
func chdir(path string) {
if err := os.Chdir(path); err != nil {
fmt.Fprintf(os.Stderr, "Chdir() failed: %v\n", err)
os.Exit(1)
}
}
func symlink(oldname, newname string) {
if err := os.Symlink(oldname, newname); err != nil {
fmt.Fprintf(os.Stderr, "Symlink(%v, %v) failed: %v\n", oldname, newname, err)
os.Exit(1)
}
}
func getCommonMvnArgs(hadoopDistribution string) []string {
args := []string{"clean", "install", "-DskipTests", "-Dfindbugs.skip", "-Dmaven.javadoc.skip", "-Dcheckstyle.skip"}
if mvnArgsFlag != "" {
for _, arg := range strings.Split(mvnArgsFlag, ",") {
args = append(args, arg)
}
}
if hadoopDistribution != "" {
hadoopVersion := hadoopDistributions[hadoopDistribution]
args = append(args, fmt.Sprintf("-Dhadoop.version=%v", hadoopVersion), fmt.Sprintf("-P%v", hadoopVersion.hadoopProfile()))
if hadoopVersion.major >= 2 && hadoopVersion.minor >= 4 {
args = append(args, "-Pyarn")
}
}
return args
}
func getVersion() (string, error) {
versionLine := run("grepping for the version", "grep", "-m1", "<version>", "pom.xml")
re := regexp.MustCompile(".*<version>(.*)</version>.*")
match := re.FindStringSubmatch(versionLine)
if len(match) < 2 {
return "", errors.New("failed to find version")
}
return match[1], nil
}
func addAdditionalFiles(srcPath, dstPath, version string) {
chdir(srcPath)
pathsToCopy := []string{
"bin/alluxio",
"bin/alluxio-masters.sh",
"bin/alluxio-mount.sh",
"bin/alluxio-start.sh",
"bin/alluxio-stop.sh",
"bin/alluxio-workers.sh",
fmt.Sprintf("client/alluxio-%v-client.jar", version),
"conf/alluxio-env.sh.template",
"conf/alluxio-site.properties.template",
"conf/core-site.xml.template",
"conf/log4j.properties",
"conf/masters",
"conf/metrics.properties.template",
"conf/workers",
"integration/docker/Dockerfile",
"integration/docker/entrypoint.sh",
"integration/docker/bin/alluxio-master.sh",
"integration/docker/bin/alluxio-proxy.sh",
"integration/docker/bin/alluxio-worker.sh",
"integration/fuse/bin/alluxio-fuse",
"integration/kubernetes/alluxio-journal-volume.yaml.template",
"integration/kubernetes/alluxio-master.yaml.template",
"integration/kubernetes/alluxio-worker.yaml.template",
"integration/kubernetes/conf/alluxio.properties.template",
"integration/mesos/bin/alluxio-env-mesos.sh",
"integration/mesos/bin/alluxio-mesos-start.sh",
"integration/mesos/bin/alluxio-master-mesos.sh",
"integration/mesos/bin/alluxio-mesos-stop.sh",
"integration/mesos/bin/alluxio-worker-mesos.sh",
"integration/mesos/bin/common.sh",
fmt.Sprintf("lib/alluxio-underfs-gcs-%v.jar", version),
fmt.Sprintf("lib/alluxio-underfs-hdfs-%v.jar", version),
fmt.Sprintf("lib/alluxio-underfs-local-%v.jar", version),
fmt.Sprintf("lib/alluxio-underfs-oss-%v.jar", version),
fmt.Sprintf("lib/alluxio-underfs-s3a-%v.jar", version),
fmt.Sprintf("lib/alluxio-underfs-swift-%v.jar", version),
fmt.Sprintf("lib/alluxio-underfs-wasb-%v.jar", version),
"libexec/alluxio-config.sh",
}
for _, path := range pathsToCopy {
mkdir(filepath.Join(dstPath, filepath.Dir(path)))
run(fmt.Sprintf("adding %v", path), "mv", path, filepath.Join(dstPath, path))
}
// Create empty directories for default UFS and Docker integration.
mkdir(filepath.Join(dstPath, "underFSStorage"))
mkdir(filepath.Join(dstPath, "integration/docker/conf"))
// Add links for previous jar locations for backwards compatibility
for _, jar := range []string{"client", "server"} {
oldLocation := filepath.Join(dstPath, "assembly/client/target", fmt.Sprintf("alluxio-assembly-%v-%v-jar-with-dependencies.jar", jar, version))
mkdir(filepath.Dir(oldLocation))
symlink(fmt.Sprintf("../../alluxio-%v-%v.jar", jar, version), oldLocation)
}
mkdir(filepath.Join(dstPath, "assembly/server/target"))
}
func generateTarball(hadoopDistribution string) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
srcPath, err := ioutil.TempDir("", "alluxio")
if err != nil {
return fmt.Errorf("Failed to create temp directory: %v", err)
}
_, file, _, ok := runtime.Caller(0)
if !ok {
return errors.New("Failed to determine file of the go script")
}
// Relative path from this class to the root directory.
repoPath := filepath.Join(filepath.Dir(file), "../../../../../../")
run(fmt.Sprintf("copying source from %v to %v", repoPath, srcPath), "cp", "-R", repoPath+"/.", srcPath)
chdir(srcPath)
run("running git clean -fdx", "git", "clean", "-fdx")
version, err := getVersion()
if err != nil {
return err
}
// Update the web app location.
replace("core/common/src/main/java/alluxio/PropertyKey.java", webappDir, webappWar)
// Update the assembly jar paths.
replace("libexec/alluxio-config.sh", "assembly/client/target/alluxio-assembly-client-${VERSION}-jar-with-dependencies.jar", "assembly/alluxio-client-${VERSION}.jar")
replace("libexec/alluxio-config.sh", "assembly/server/target/alluxio-assembly-server-${VERSION}-jar-with-dependencies.jar", "assembly/alluxio-server-${VERSION}.jar")
// Update the FUSE jar path
replace("integration/fuse/bin/alluxio-fuse", "target/alluxio-integration-fuse-${VERSION}-jar-with-dependencies.jar", "alluxio-fuse-${VERSION}.jar")
mvnArgs := getCommonMvnArgs(hadoopDistribution)
run("compiling repo", "mvn", mvnArgs...)
tarball := strings.Replace(targetFlag, versionMarker, version, 1)
dstDir := strings.TrimSuffix(filepath.Base(tarball), ".tar.gz")
dstPath := filepath.Join(cwd, dstDir)
run(fmt.Sprintf("removing any existing %v", dstPath), "rm", "-rf", dstPath)
fmt.Printf("Creating %s:\n", tarball)
// Create the directory for the server jar.
mkdir(filepath.Join(dstPath, "assembly"))
// Create directories for the client jar.
mkdir(filepath.Join(dstPath, "client"))
mkdir(filepath.Join(dstPath, "logs"))
// Create directories for the fuse connector
mkdir(filepath.Join(dstPath, "integration", "fuse"))
run("adding Alluxio client assembly jar", "mv", fmt.Sprintf("assembly/client/target/alluxio-assembly-client-%v-jar-with-dependencies.jar", version), filepath.Join(dstPath, "assembly", fmt.Sprintf("alluxio-client-%v.jar", version)))
run("adding Alluxio server assembly jar", "mv", fmt.Sprintf("assembly/server/target/alluxio-assembly-server-%v-jar-with-dependencies.jar", version), filepath.Join(dstPath, "assembly", fmt.Sprintf("alluxio-server-%v.jar", version)))
run("adding Alluxio FUSE jar", "mv", fmt.Sprintf("integration/fuse/target/alluxio-integration-fuse-%v-jar-with-dependencies.jar", version), filepath.Join(dstPath, "integration", "fuse", fmt.Sprintf("alluxio-fuse-%v.jar", version)))
// Condense the webapp into a single .war file.
run("jarring up webapp", "jar", "-cf", filepath.Join(dstPath, webappWar), "-C", webappDir, ".")
addAdditionalFiles(srcPath, dstPath, version)
chdir(cwd)
run("creating the distribution tarball", "tar", "-czvf", tarball, dstDir)
run("removing the temporary repositories", "rm", "-rf", srcPath, dstPath)
return nil
}