-
Notifications
You must be signed in to change notification settings - Fork 8
/
package.sh
executable file
·158 lines (135 loc) · 3.72 KB
/
package.sh
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
#!/bin/bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
BUILD="build"
CONFIG="${SCRIPT_DIR}/packaging/nfpm.yaml"
DIST="dist"
NAME="xk6-disruptor"
ARCH=$(go env GOARCH)
OS=$(go env GOOS)
function usage() {
cat << EOF
creates a package for a target architecture and operating system
$0 [OPTIONS] COMMAND
Commands:
build Builds the binary for the given architecture and operating system
pack Packages the binary for the given architecture and operating system in the given format.
all Builds and packs all supported combinations of architecture, operating system and package format
options:
-a, --arch: target architecture (valid option amd64, arm64. Defaults to GOARCH)
-b, --build: directory for building binaries (defaults to 'build. Created if it does not exist)
-c, --config: nfpm config file (defaults to packaging/nfpm.yaml)
-d, --dist: directory to place packages (defaults to 'dist'. Created if it does not exist)
-n, --name: package base name. Defaults to 'xk6-disruptor'
-o, --os: target operating systems (valid options linux, darwing, windows. Defaults to GOOS)
-p, --pkg: package format (valid options: deb, rpm, tgz)
-r, --release: release version used for packages (can be different from version for development releases)
-v, --version: xk6-disruptor version in semver format
-y, --binary: name of the binary (default is name-os-arch)
EOF
}
# Prints an error message the usage help and exits with error
function error () {
echo $1
usage
exit 1
}
while [[ $# -gt 0 ]]; do
case $1 in
-a|--arch)
ARCH="$2"
if [[ ! $ARCH =~ amd64|arm64 ]]; then
error "supported architectures are 'amd64' and 'arm64'"
fi
shift
;;
-b|--build)
BUILD="$2"
shift
;;
-c|--config)
CONFIG="$2"
shift
;;
-d|--dist)
DIST="$2"
shift
;;
-o|--os)
OS="$2"
if [[ ! $OS =~ linux|darwin|windows ]]; then
error "supported operating systems are 'linux', 'darwin' and 'windows'"
fi
shift
;;
-p|--pkg)
PKG="$2"
if [[ ! $PKG =~ deb|rpm|tgz|zip ]]; then
error "supported package formats are 'deb', 'rpm', 'tgz' and 'zip'"
fi
shift
;;
-r|--release)
RELEASE="$2"
shift
;;
-y|--binary)
BINARY="$2"
shift
;;
*)
error "Unknown option $1"
;;
esac
shift
done
if [[ ! -e $BUILD ]]; then
mkdir -p $BUILD
fi
if [[ ! -e $DIST ]]; then
mkdir -p $DIST
fi
## make all paths absolute
BUILD=$(realpath $BUILD)
DIST=$(realpath $DIST)
CONFIG=$(realpath $CONFIG)
if [[ -z $OS ]]; then
error "target operating system is required"
fi
if [[ -z $ARCH ]]; then
error "target architecture is required"
fi
if [[ -z $PKG ]]; then
error "package format is required"
fi
if [[ -z $RELEASE ]]; then
error "release is required"
fi
if [[ -z $BINARY ]]; then
BINARY=$NAME-$OS-$ARCH
fi
case $PKG in
deb|rpm)
if [[ ! $OS == "linux" ]]; then
error "unsoported operating system for package format $PKG: $OS"
fi
if [[ ! $ARCH == "amd64" ]]; then
error "unsoported architecture for package format $PKG: $ARCH"
fi
(
# nfpm does not support variable substitution in paths so we must run in the build directory
pushd $BUILD
export PKG_VERSION=$RELEASE
TARGET="$DIST/$NAME-$RELEASE-$OS-$ARCH.$PKG"
nfpm package --config $CONFIG --packager $PKG --target $TARGET
popd
)
;;
tgz)
tar -zcf "$DIST/$NAME-${RELEASE}-${OS}-${ARCH}.tar.gz" -C $BUILD $BINARY
;;
zip)
zip -rq9j "$DIST/$NAME-${RELEASE}-${OS}-${ARCH}.zip" "$BUILD/$BINARY"
;;
*)
error "invalid package format only 'deb', 'rpm' and 'tgz' are accepted"
esac