-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
justfile
155 lines (110 loc) · 2.9 KB
/
justfile
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
##
# Build Tasks
#
# This requires "just" >= 0.5.4.
# See https://github.com/casey/just for more details.
#
# USAGE:
# just --list
# just <task>
##
src_dir := justfile_directory() + "/docker"
repo := "https://github.com/Blobfolio/righteous-sandbox.git"
docker_sig := "/opt/righteous-sandbox.version"
main_image := "righteous/sandbox:bookworm"
instance_name := "righteous_sandbox_bookworm"
## ##
# Usage! #
## ##
# Launch Sandbox.
launch DIR="": _build-if
#!/usr/bin/env bash
_dir=""
if [ -z "{{ DIR }}" ]; then
_dir="{{ invocation_directory() }}"
else
_dir="$( realpath "{{ DIR }}" )"
if [ ! -d "${_dir}" ]; then
just _error "Invalid share directory."
exit 1
fi
fi
docker run \
--rm \
-v "${_dir}":/share \
-it \
--name "{{ instance_name }}" \
"{{ main_image }}"
# Initialization task example.
@_launch-init: _only_docker
# Print something so we know it worked.
fyi warning "This is being executed from Righteous Sandbox's source directory."
## ##
# Image Management #
## ##
# Build Sandbox.
@build: _requirements
# Build!
cd "{{ src_dir }}" \
&& docker build \
--compress \
-t "{{ main_image }}" \
-f Dockerfile .
# Build Sandbox, but only if missing.
@_build-if: _requirements
[ -n "$( docker images | \
grep "righteous/sandbox" | \
grep "bookworm" )" ] || just rebuild
# Rebuild Environment.
@rebuild: _requirements
just _header "Rebuilding {{ main_image }}."
# Make sure the image is removed.
just remove
# Force an update of Debian.
docker pull debian:bookworm
# Build it.
just build
# Remove Environment.
@remove: _requirements
[ -z "$( docker images | \
grep "righteous/sandbox" | \
grep "bookworm" )" ] || docker rmi "{{ main_image }}"
# Pull sources from master and then rebuild.
@update: _requirements
# Update self.
git pull
# Update container.
just rebuild
## ##
# INTERNAL #
## ##
# General Requirements.
@_requirements:
# Docker should exist and be running.
[ $( command -v docker ) ] || just _die "Docker is required."
# Git is required.
[ $( command -v git ) ] || just _die "Git is required."
# And these should not be run from within the container.
just _no_docker
# Tasks Not Allowed Inside Docker.
@_no_docker:
[ ! -f "{{ docker_sig }}" ] || just _die "This task is meant to be run on a local machine."
# Only Allowed Inside Docker.
@_only_docker:
[ -f "{{ docker_sig }}" ] || just _die "This task is meant to be run *inside* a container."
# Fix file/directory ownership.
@_fix_chown PATH:
[ ! -e "{{ PATH }}" ] || chown -R --reference="{{ justfile() }}" "{{ PATH }}"
## ##
# NOTIFICATIONS #
## ##
# Task header.
@_header TASK:
echo "\e[34;1m[Task] \e[0;1m{{ TASK }}\e[0m"
# Echo an error.
@_error COMMENT:
>&2 echo "\e[31;1m[Error] \e[0;1m{{ COMMENT }}\e[0m"
# Error and exit.
@_die COMMENT:
just _error "{{ COMMENT }}"
exit 1