aboutsummaryrefslogtreecommitdiff
path: root/msci
blob: 50ae14aad363859f3cd897ec9e6ce9dde538c5c2 (plain) (blame)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env bash

if [ -z "$MSCI_HOME" ]; then
	echo "MSCI_HOME is not set"
	exit 1
fi

deps=("git" "jq" "openssl" "docker" "crontab")
for dep in "${deps[@]}"; do
	if ! command -v "$dep" >/dev/null 2>&1; then
		echo "command '$dep' not found"
		exit 1
	fi
done

set -e
shopt -s nullglob
mkdir -p "$MSCI_HOME"/projects \
	"$MSCI_HOME"/stdout/public \
	"$MSCI_HOME"/stdout/private \
	"$MSCI_HOME"/tmp
[ ! -f "$MSCI_HOME"/cron ] && touch "$MSCI_HOME"/cron

confirm_in() {
	read -rp "$1 (y/N): " confirm
	[[ $confirm == [yY]* ]] && return 0 || return 1
}

find_project() {
	if list_projects | grep -xFq -- "$1"; then
		return 0
	fi
	return 1
}

list_projects() {
	paths=("$MSCI_HOME"/projects/*)
	names=("${paths[@]##*/}")
	projects=("${names[@]%.json}")
	printf '%s\n' "${projects[@]}"
}

write_cron() {
	cfile="MSCI_HOME=$MSCI_HOME\n"
	for project in $(list_projects); do
		ppath="$MSCI_HOME"/projects/"$project".json
		schedule=$(jq -r '.cron' "$ppath")
		[ "$schedule" = 'null' ] && continue
		cfile="${cfile}$schedule $MSCI_HOME/msci run $project\n"
	done
	echo -e "$cfile" >"$MSCI_HOME"/tmp.cron
	if crontab "$MSCI_HOME"/tmp.cron; then
		mv -fv "$MSCI_HOME"/tmp.cron "$MSCI_HOME"/cron
	else
		rm -fv "$MSCI_HOME"/tmp.cron
	fi
}

run_project() {
	ppath="$MSCI_HOME"/projects/"$1".json
	repo_name="$1"
	repo_url=$(jq -r '.url' "$ppath")
	repo_path="$MSCI_HOME"/tmp/"$repo_name"
	echo "cloning repo: $repo_url" | tee -a "$2"
	git clone --quiet "$repo_url" "$repo_path"
	pushd "$repo_path" >/dev/null || return 1
	for job in "$repo_path"/.makeshiftci/*; do
		pname=$(jq -r '.name' "$job")
		echo "running job: $pname" | tee -a "$2"
		pimage=$(jq -r '.image' "$job")
		psecrets=$(jq -r '.secrets' "$job")
		prun=$(jq -r '.run[]' "$job")
		env_secrets=()
		secret_mounts=()
		if [ ! "$psecrets" = 'null' ]; then
			for secret_key in $(echo "$psecrets" | jq -r '. | keys[]'); do
				secret_value=$(echo "$psecrets" | jq -r ".$secret_key")
				secret_name=$(openssl rand -hex 16)
				secret_mounts+=("--mount=type=bind,source=$secret_value,target=/$secret_name,readonly")
				env_secrets+=("$secret_key=/$secret_name")
			done
		fi
		docker run --rm \
			"${env_secrets[@]/#/--env=}" \
			--mount type=bind,source="$repo_path",target=/"$repo_name" \
			"${secret_mounts[@]}" \
			--workdir="/$repo_name" \
			"$pimage" \
			sh -c "exec $prun" | tee -a "$2"
	done
	popd >/dev/null && rm -rf "$repo_path"
	echo "finished" | tee -a "$2"
}

create_project() {
	name="$1"
	if [[ ! $name =~ ^[A-Za-z0-9_]+$ ]]; then
		echo "allowed project file name regex: ^[A-Za-z0-9_]+$"
		return 1
	fi
	while true; do
		read -rp "project name: " pname
		[[ -n ${pname//[[:space:]]/} ]] && break
		echo "name can't be empty"
	done
	while true; do
		read -rp ".makeshiftci repo URL: " purl
		[[ -n ${purl//[[:space:]]/} ]] && break
		echo "URL can't be empty"
	done
	phidden=$(confirm_in "hidden?" && echo true || echo false)
	read -rp "cron (optional): " pcron
	jq -n \
		--arg pname "$pname" \
		--arg purl "$purl" \
		--argjson phidden "$phidden" \
		--arg pcron "$pcron" \
		'{ 
			name: $pname,
			url: $purl,
			hidden: $phidden,
			cron: (if $pcron == "" then null else $pcron end)
		}' \
		>"$MSCI_HOME"/projects/"$name".json
	[ -n "$pcron" ] && write_cron
	([ "$phidden" = "true" ] &&
		mkdir -p "$MSCI_HOME"/stdout/private/"$1") ||
		mkdir -p "$MSCI_HOME"/stdout/public/"$1"
}

edit_project() {
	name="$1"
	if ! find_project "$name"; then
		echo "project file doesn't exist"
		return 1
	fi
	ppath="$MSCI_HOME"/projects/"$name".json
	echo "editing $ppath"
	oldname=$(jq -r '.name' "$ppath")
	oldurl=$(jq -r '.url' "$ppath")
	washidden=$(jq '.hidden' "$ppath")
	keephidden=$([ "$washidden" = 'true' ] && echo "keep" || echo "make")
	oldcron=$(jq -r '.cron' "$ppath")
	echo "project name: $oldname"
	read -rp "new name (optional): " pname
	[ -z "${pname//[[:space:]]/}" ] && pname=$oldname
	echo ".makeshiftci repo URL: $oldurl"
	read -rp "new URL (optional): " purl
	[ -z "${purl//[[:space:]]/}" ] && purl=$oldurl
	echo "hidden: $washidden"
	phidden=$(confirm_in "$keephidden hidden?" && echo true || echo false)
	echo "cron: $oldcron"
	read -rp "new cron (optional): " pcron
	[ -z "${pcron//[[:space:]]/}" ] && pcron=$oldcron
	jq -n \
		--arg pname "$pname" \
		--arg purl "$purl" \
		--argjson phidden "$phidden" \
		--arg pcron "$pcron" \
		'{ 
			name: $pname,
			url: $purl,
			hidden: $phidden,
			cron: (if $pcron == "null" then null else $pcron end)
		}' \
		>"$ppath"
	[ ! "$pcron" = "$oldcron" ] && write_cron
	privout="$MSCI_HOME"/stdout/private/"$1"
	pubout="$MSCI_HOME"/stdout/public/"$1"
	(
		[ "$phidden" = "true" ] &&
			([ ! -d "$privout" ] && mv "$pubout" "$privout")
	) ||
		([ ! -d "$pubout" ] && mv "$privout" "$pubout")
}

case $1 in
--create)
	if [ -z "$2" ]; then
		echo "no project file name is supplied"
		exit 1
	fi
	if find_project "$2"; then
		echo "project file already exists"
		return 1
	fi
	create_project "$2"
	;;
--edit)
	if [ -z "$2" ]; then
		echo "no project file name is supplied"
		exit 1
	fi
	if ! find_project "$2"; then
		echo "project file doesn't exist"
		exit 1
	fi
	edit_project "$2"
	;;
--delete)
	if [ -z "$2" ]; then
		echo "no project file name is supplied"
		exit 1
	fi
	if ! find_project "$2"; then
		echo "project file doesn't exist"
		exit 1
	fi
	ppath="$MSCI_HOME"/projects/"$2".json
	cat "$ppath"
	confirm_in "delete '$2' (details above)?" &&
		rm -vf "$MSCI_HOME"/projects/"$2".json ||
		exit 0
	rm -rf "$MSCI_HOME"/stdout/**/"$2"
	write_cron
	;;
--run)
	if [ -z "$2" ]; then
		echo "no project file name is supplied"
		exit 1
	fi
	if ! find_project "$2"; then
		echo "project file doesn't exist"
		exit 1
	fi
	if [[ ! $2 =~ ^[A-Za-z0-9_]+$ ]]; then
		echo "project file name is not allowed, rename it"
		exit 1
	fi
	ppath="$MSCI_HOME"/projects/"$2".json
	phidden=$(jq -r '.hidden' "$ppath")
	stdout_type=$([ "$phidden" = 'false' ] && echo "public" || echo "private")
	stdout_path="$MSCI_HOME"/stdout/"$stdout_type"/"$2"
	last_run=$(
		find "$stdout_path" \
			-maxdepth 1 \
			-mindepth 1 \
			-type f -printf '%f\n' |
			grep -E '^[0-9]+$' |
			sort -n |
			tail -n 1
	)
	next_run=$(
		[ -z "$last_run" ] &&
			echo "1" ||
			echo $((last_run + 1))
	)
	stdout_path="$stdout_path"/"$next_run"
	run_project "$2" "$stdout_path"
	;;
--list)
	list_projects
	;;
--clean)
	case "$2" in
	--tmp)
		echo "cleaning tmp for all projects"
		rm -rf "$MSCI_HOME"/tmp/*
		;;
	--stdout)
		echo "cleaning stdout for all projects"
		rm -rf "$MSCI_HOME"/stdout/**/**/*
		;;
	--all)
		echo "cleaning stdout and tmp for all projects"
		rm -rf "$MSCI_HOME"/tmp/*
		rm -rf "$MSCI_HOME"/stdout/**/**/*
		;;
	*)
		if [ -z "$2" ]; then
			echo "no project file name is supplied"
			exit 1
		fi
		if ! find_project "$2"; then
			echo "project file doesn't exist"
			exit 1
		fi
		confirm_in "clear tmp?" &&
			rm -rf "$MSCI_HOME"/tmp/"$2" &&
			echo "cleared tmp for $2"
		confirm_in "clear stdout?" &&
			rm -rf "$MSCI_HOME"/stdout/"$2" &&
			echo "cleared stdout for $2"
		;;
	esac
	;;
*)
	echo "
usage: $(basename "$0") [OPTIONS]

options:
  <filename> is the name of a file:
    - matching the regex ^[A-Za-z0-9_]+\$
    - that is not necessarily the project name

  --create <filename>     create a new project

  --edit <filename>       edit a project

  --delete <filename>     delete a project and clean up

  --run <filename>        run a project

  --list                  list projects

  --clean <filename>|[SELECTION]
    if <filename>, prompts for cleaning a particular project
    selections:
      --tmp       clean tmp files, such as cloned repos
                  NOTE:
                    tmp files are automatically removed after
                    running, but they may persist in cases of errors
                    or unexpected behavior
                    only run this command in such cases

      --stdout    clean stdout
                  NOTE:
                    stdout is where the output for each run is stored
                    everything that was printed to stdout during the run
                    of any project, is stored under the directory by the
                    name of the project, in a file named as the run number
"
	exit 1
	;;
esac

# vim: set filetype=bash: