forked from JettPlay1/LinGam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.sh
More file actions
executable file
·106 lines (86 loc) · 2.99 KB
/
builder.sh
File metadata and controls
executable file
·106 lines (86 loc) · 2.99 KB
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
#!/bin/bash
network_name="isolated_network"
if docker network ls | grep -q "$network_name"; then
echo "Network $network_name exists."
else
echo "Network $network_name does not exist. Creating the network..."
docker network create "$network_name"
if [ $? -eq 0 ]; then
echo "Network $network_name created successfully."
else
echo "Failed to create network $network_name."
exit 1
fi
fi
for dir in */; do
taskname=$(basename "$dir" | tr '[:upper:]' '[:lower:]')
if [ -f "$dir/config.sh" ]; then
source "$dir/config.sh"
if [ -f "$dir/pre_install.sh" ]; then
echo "Running pre_install.sh..."
(cd "$dir" && bash pre_install.sh)
if [ $? -ne 0 ]; then
echo "pre_install.sh failed for $taskname."
continue
fi
else
echo "Not found pre_install.sh"
fi
if id "$taskname" &>/dev/null; then
echo "User $taskname exists. Deleting user and home directory."
sudo sed -i "/^$taskname:/d" /etc/passwd
sudo userdel -r "$taskname" > /dev/null
else
echo "User $taskname does not exist."
fi
if getent group "$taskname" &>/dev/null; then
echo "Group $taskname exists. Deleting group."
sudo groupdel "$taskname"
else
echo "Group $taskname does not exist."
fi
if [ -d "/home/$taskname" ]; then
echo "Home directory /home/$taskname exists. Deleting home directory."
sudo rm -rf "/home/$taskname"
else
echo "Home directory /home/$taskname does not exist."
fi
build_command="docker build -t $taskname $dir > /dev/null"
eval "$build_command"
if [ $? -ne 0 ]; then
echo "Docker build failed for $taskname."
continue
fi
script_path="/opt/shells/$taskname.sh"
echo "#!/bin/sh" > "$script_path"
command="/usr/bin/docker run -it --rm"
if [[ "$privileged_mode" =~ ^(True|true|1)$ ]]; then
command="$command --privileged"
fi
if [[ "$isolated_network" =~ ^(True|true|1)$ ]]; then
command="$command --network $network_name"
fi
if [ -n "$options" ]; then
command="$command $options"
fi
command="$command $taskname"
echo "$command" >> "$script_path"
chmod +x "$script_path"
(
flock -x 200
sudo groupadd "$taskname"
) 200>/var/lock/groupadd.lock
(
flock -x 200
sudo useradd -m -s "$script_path" -g "$taskname" "$taskname"
) 200>/var/lock/useradd.lock
echo "$taskname:$taskname" | sudo chpasswd
sudo usermod -aG docker "$taskname"
(
flock -x 200
sudo usermod -aG "$taskname" "$taskname"
) 200>/var/lock/usermod.lock
else
echo "config.sh file not found for $taskname"
fi
done