forked from dragonflyoss/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·190 lines (166 loc) · 4.78 KB
/
build.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/bin/bash
set -o nounset
set -o errexit
set -o pipefail
DFGET_BINARY_NAME=dfget
DFCACHE_BINARY_NAME=dfcache
DFSTORE_BINARY_NAME=dfstore
SCHEDULER_BINARY_NAME=scheduler
MANAGER_BINARY_NAME=manager
PKG=d7y.io/dragonfly/v2
BUILD_IMAGE=golang:1.21.1-alpine3.17
VERSION=$(git rev-parse --short HEAD)
BUILD_TIME=$(date -u '+%Y-%m-%dT%H:%M:%SZ')
CGO_ENABLED=${CGO_ENABLED:-0}
GOPROXY=${GOPROXY:-}
GOTAGS=${GOTAGS:-}
GOGCFLAGS=${GOGCFLAGS:-}
GOLDFLAGS="-X d7y.io/dragonfly/v2/version.GitCommit=${VERSION}"
GOLDFLAGS="${GOLDFLAGS} -X d7y.io/dragonfly/v2/version.BuildTime=${BUILD_TIME}"
GOLDFLAGS="${GOLDFLAGS} -X \"d7y.io/dragonfly/v2/version.Gotags=${GOTAGS:-none}\""
GOLDFLAGS="${GOLDFLAGS} -X \"d7y.io/dragonfly/v2/version.GoVersion=$(go version | grep -o 'go[^ ].*')\""
GOLDFLAGS="${GOLDFLAGS} -X \"d7y.io/dragonfly/v2/version.Gogcflags=${GOGCFLAGS:-none}\""
CUR_DIR=$(cd "$(dirname "$0")" && pwd)
cd "${CUR_DIR}" || return
BUILD_SOURCE_HOME=$(cd ".." && pwd)
. ./env.sh
BUILD_PATH=bin/${GOOS}_${GOARCH}
USE_DOCKER=${USE_DOCKER:-"0"}
create-dirs() {
cd "${BUILD_SOURCE_HOME}" || return
mkdir -p .go/src/${PKG} .go/bin .cache
mkdir -p "${BUILD_PATH}"
}
build-local() {
test -f "${BUILD_SOURCE_HOME}/${BUILD_PATH}/$1" && rm -f "${BUILD_SOURCE_HOME}/${BUILD_PATH}/$1"
cd "${BUILD_SOURCE_HOME}/cmd/$2" || return
go build -tags="${GOTAGS}" -ldflags="${GOLDFLAGS}" -gcflags="${GOGCFLAGS}" -o="${BUILD_SOURCE_HOME}/${BUILD_PATH}/$1"
chmod a+x "${BUILD_SOURCE_HOME}/${BUILD_PATH}/$1"
echo "BUILD: $2 in ${BUILD_SOURCE_HOME}/${BUILD_PATH}/$1"
}
build-dfget-local() {
build-local ${DFGET_BINARY_NAME} dfget
}
build-dfcache-local() {
build-local ${DFCACHE_BINARY_NAME} dfcache
}
build-dfstore-local() {
build-local ${DFSTORE_BINARY_NAME} dfstore
}
build-scheduler-local() {
build-local ${SCHEDULER_BINARY_NAME} scheduler
}
build-manager-local() {
build-local ${MANAGER_BINARY_NAME} manager
}
build-docker() {
cd "${BUILD_SOURCE_HOME}" || return
docker run \
--rm \
-ti \
-u "$(id -u)":"$(id -g)" \
-v "$(pwd)"/.go:/go \
-v "$(pwd)":/go/src/${PKG} \
-v "$(pwd)"/"${BUILD_PATH}":/go/bin \
-v "$(pwd)"/.cache:/.cache \
-e GOOS="${GOOS}" \
-e GOARCH="${GOARCH}" \
-e CGO_ENABLED="${CGO_ENABLED}" \
-e GO111MODULE=on \
-e GOPROXY="${GOPROXY}" \
-e GOTAGS="${GOTAGS}" \
-e GOGCFLAGS="${GOGCFLAGS}" \
-w /go/src/${PKG} \
${BUILD_IMAGE} \
go build -o "/go/bin/$1" ./cmd/"$2"
echo "BUILD: $1 in ${BUILD_SOURCE_HOME}/${BUILD_PATH}/$1"
}
build-dfget-docker() {
build-docker ${DFGET_BINARY_NAME} dfget
}
build-dfcache-docker() {
build-docker ${DFCACHE_BINARY_NAME} dfcache
}
build-dfstore-docker() {
build-docker ${DFSTORE_BINARY_NAME} dfstore
}
build-scheduler-docker() {
build-docker ${SCHEDULER_BINARY_NAME} scheduler
}
build-manager-docker() {
build-docker ${MANAGER_BINARY_NAME} manager
}
build-manager-console() {
set -x
CONSOLE_DIR=$(echo $CUR_DIR | sed 's#hack#manager/console#')
MANAGER_DIR=$(echo $CUR_DIR | sed 's#hack#manager#')
CONSOLE_ASSETS=$CONSOLE_DIR/dist/*
MANAGER_ASSETS_DIR=$MANAGER_DIR/dist
docker run --workdir=/build \
--rm -v ${CONSOLE_DIR}:/build node:18-alpine \
sh -c "yarn install --network-timeout 1000000 && yarn build"
cp -r $CONSOLE_ASSETS $MANAGER_ASSETS_DIR
}
main() {
create-dirs
if [[ "1" == "${USE_DOCKER}" ]]; then
echo "Begin to build with docker."
case "${1-}" in
dfget)
build-dfget-docker
;;
dfcache)
build-dfcache-docker
;;
dfstore)
build-dfstore-docker
;;
scheduler)
build-scheduler-docker
;;
manager)
build-manager-docker
;;
manager-console)
build-manager-console
;;
*)
build-dfget-docker
build-dfcache-docker
build-dfstore-docker
build-scheduler-docker
build-manager-docker
;;
esac
else
echo "Begin to build in the local environment."
case "${1-}" in
dfget)
build-dfget-local
;;
dfcache)
build-dfcache-local
;;
dfstore)
build-dfstore-local
;;
scheduler)
build-scheduler-local
;;
manager)
build-manager-local
;;
manager-console)
build-manager-console
;;
*)
build-dfget-local
build-dfcache-local
build-dfstore-local
build-scheduler-local
build-manager-local
;;
esac
fi
}
main "$@"