-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.sh
executable file
·166 lines (149 loc) · 4.15 KB
/
setup.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
159
160
161
162
163
164
165
166
#!/bin/bash
set -e -o pipefail -o noclobber -o nounset
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
echo '`getopt --test` failed in this environment.'
exit 1
fi
OPTIONS=h
LONGOPTS=help,listen-address:,pkg-name:,bin-path:,systemd-path:,systemd-after:,systemd-requires:,systemd-args:
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
exit 2
fi
eval set -- "$PARSED"
### DEFAULTS ###
PLATFORMS=("windows/amd64" "linux/amd64")
PKG_NAME="fah-exporter"
BIN_PATH="/usr/bin"
SYSTEMD_PATH="/etc/systemd/system"
LISTEN_ADDRESS="0.0.0.0:9659"
EXTRA_AFTER=""
EXTRA_REQUIRES=""
EXTRA_ARGS=""
function print_help () {
# Using a here doc with standard out.
cat <<-END
Usage $0: COMMAND [OPTIONS]
Commands:
build-all Build for all platforms (${PLATFORMS[*]})
install Build and install binary
systemd Create and install systemd socket and service files
pacman-build Copy required files to build a pacman package from local files
Options:
-h --help Show this message
--listen-address Listen address (default $LISTEN_ADDRESS)
--pkg-name Change package name (default $PKG_NAME)
--bin-path Path where the binary is installed (default $BIN_PATH)
--systemd-path Path where systemd units are installed (default $SYSTEMD_PATH)
--systemd-after Add to After in systemd service (default network.target)
--systemd-requires Add to Requires in systemd service (default network.target)
--systemd-args Add extra arguments to unit file
END
}
while true; do
case "$1" in
-h|--help)
print_help
exit 0
;;
--pkg-name)
PKG_NAME="$2"
shift 2
;;
--listen-address)
LISTEN_ADDRESS="$2"
shift 2
;;
--bin-path)
BIN_PATH="$2"
shift 2
;;
--systemd-path)
SYSTEMD_PATH="$2"
shift 2
;;
--systemd-after)
EXTRA_AFTER="$2"
shift 2
;;
--systemd-requires)
EXTRA_REQUIRES="$2"
shift 2
;;
--systemd-args)
EXTRA_ARGS="$2"
shift 2
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
if [[ $# -ne 1 ]]; then
echo "$0: A command is required."
exit 4
fi
PKG_PATH="$BIN_PATH/$PKG_NAME"
SOCKET_FILE="$SYSTEMD_PATH/$PKG_NAME.socket"
SERVICE_FILE="$SYSTEMD_PATH/$PKG_NAME.service"
case "$1" in
build-all)
for platform in "${PLATFORMS[@]}"
do
IFS="/" read -r -a platform_split <<< "$platform"
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}
output_name=$PKG_NAME'-'$GOOS'-'$GOARCH
if [ "$GOOS" = "windows" ]; then
output_name+='.exe'
fi
echo "Building $output_name"
if ! env GOOS="$GOOS" GOARCH="$GOARCH" go build -o $output_name; then
echo 'Build failed'
exit 1
fi
done
;;
install)
go build -o "$PKG_PATH"
;;
systemd)
set +e
echo -e "\n########## Systemd socket ##########\n"
cat <<EOF | tee "$SOCKET_FILE"
[Socket]
ListenStream=$LISTEN_ADDRESS
BindIPv6Only=both
[Install]
WantedBy=sockets.target
EOF
echo -e "\n########## Systemd service ##########\n"
cat <<EOF | tee "$SERVICE_FILE"
[Unit]
Description=$PKG_NAME service
After=network.target $EXTRA_AFTER
Requires=network.target $EXTRA_REQUIRES
[Service]
ExecStart=$PKG_PATH -systemd $EXTRA_ARGS
EOF
;;
pacman-build)
rm -rf ./build
mkdir -p ./build/src/"$PKG_NAME"
rsync -a ./ ./build/src/"$PKG_NAME" --exclude build --exclude PKGBUILD
cp -f ./PKGBUILD ./build/
cd ./build
makepkg --noextract
;;
*)
echo "Unrecognized command: $1"
print_help
exit 2
;;
esac