forked from openshift-pipelines/pipeline-service
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.sh
executable file
·177 lines (158 loc) · 4.67 KB
/
run.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
#!/usr/bin/env bash
# Copyright 2022 The pipelines-service Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
usage() {
echo "
Usage:
${0##*/} [options]
Run a container with the development environment.
Optional arguments:
--dev
Build and run a local image
-f, --force
Force the creation of a new container
-d, --debug
Activate tracing/debug mode.
-h, --help
Display this message.
Example:
${0##*/}
" >&2
}
init() {
SCRIPT_DIR=$(
cd "$(dirname "$0")" >/dev/null
pwd
)
PROJECT_DIR=$(
cd "$SCRIPT_DIR" >/dev/null
git rev-parse --show-toplevel
)
DEV_MODE="0"
IMAGE_NAME="quay.io/redhat-pipeline-service/devenv:main"
LOCAL_IMAGE_NAME="$IMAGE_NAME"
CONTAINER_NAME=$(pwd | sed -e 's:/:__:g' -e 's:[^a-zA-Z0-9_-]:-:g' | cut -c3-)
detect_container_engine
}
parse_args() {
while [[ $# -gt 0 ]]; do
case $1 in
--dev)
DEV_MODE="1"
;;
-f | --force)
$CONTAINER_ENGINE rm -f "$CONTAINER_NAME" &>/dev/null || true
;;
-d | --debug)
set -x
;;
-h | --help)
usage
exit 0
;;
*)
echo "Unknown argument: $1"
usage
exit 1
;;
esac
shift
done
}
detect_container_engine() {
CONTAINER_ENGINE="${CONTAINER_ENGINE:-}"
if [[ -n "${CONTAINER_ENGINE}" ]]; then
return
fi
# Check if docker should be used
if ! command -v podman >/dev/null; then
CONTAINER_ENGINE="docker"
return
fi
if [[ "$OSTYPE" == "darwin"* && -z "$(podman ps)" ]]; then
# Podman machine is not started
CONTAINER_ENGINE="docker"
return
fi
if [[ "$OSTYPE" == "darwin"* && -z "$(podman system connection ls --format=json)" ]]; then
CONTAINER_ENGINE="docker"
return
fi
# Default container engine is podman
CONTAINER_ENGINE="podman"
}
get_image_name() {
DEPENDENCIES_SHA=$(
cat "$SCRIPT_DIR/Dockerfile" "$PROJECT_DIR/shared/hack/install.sh" "$PROJECT_DIR/shared/config/dependencies.sh" |
sha256sum | cut -c1-8
)
IMAGE_NAME="pipeline-service/devenv:$DEPENDENCIES_SHA"
LOCAL_IMAGE_NAME="localhost/$IMAGE_NAME"
}
build_image() {
if [ "$("${CONTAINER_ENGINE[@]}" images --filter "reference=$LOCAL_IMAGE_NAME" --noheading | wc -l)" = "0" ]; then
echo "[Building container]"
$CONTAINER_ENGINE build -f "$SCRIPT_DIR/Dockerfile" --label "name=$IMAGE_NAME" -t "$IMAGE_NAME" "$PROJECT_DIR"
fi
}
start_container() {
if ! $CONTAINER_ENGINE ps --filter "name=$CONTAINER_NAME" | grep -q "$IMAGE_NAME"; then
if ! $CONTAINER_ENGINE ps -a --filter "name=$CONTAINER_NAME" | grep -q "$IMAGE_NAME"; then
echo "[Starting container]"
$CONTAINER_ENGINE rm -f "$CONTAINER_NAME" >/dev/null 2>&1 || true
if [ "$DEV_MODE" = "0" ]; then
CONTAINER_ENGINE_OPTS=("--pull" "always")
fi
$CONTAINER_ENGINE run \
--detach \
--entrypoint '["bash", "-c", "sleep infinity"]' \
--name "$CONTAINER_NAME" \
--privileged \
--volume "$PROJECT_DIR:/workspace" \
"${CONTAINER_ENGINE_OPTS[@]}" \
"$LOCAL_IMAGE_NAME" >/dev/null 2>&1
else
echo "[Restarting container]"
$CONTAINER_ENGINE start "$CONTAINER_NAME" >/dev/null 2>&1
fi
fi
}
open_shell() {
echo "[Opening shell in container]"
$CONTAINER_ENGINE exec -it "$CONTAINER_NAME" /bin/bash
}
stop_container() {
# Stop container when the last shell exits
if ! $CONTAINER_ENGINE exec "$CONTAINER_NAME" /bin/sh -c "ps -ef | grep ' /bin/bash$'"; then
echo "[Stopping container]"
$CONTAINER_ENGINE stop "$CONTAINER_NAME" >/dev/null
fi
}
main() {
init
parse_args "$@"
if [ "$DEV_MODE" != "0" ]; then
get_image_name
build_image
fi
start_container
open_shell
stop_container
}
if [ "${BASH_SOURCE[0]}" == "$0" ]; then
main "$@"
fi