-
Notifications
You must be signed in to change notification settings - Fork 1
/
pistachio-manager
executable file
·405 lines (373 loc) · 10.8 KB
/
pistachio-manager
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/bin/bash
# Pistachio Linux's Distrobox management script
# Licensed under GPLv3
VERSION="1.0.3"
# help functions for pistachio-manager
ShowHelp()
{
echo "pistachio-manager allows you to manage Distrobox containers in Pistachio Linux."
echo
echo "Usage: pistachio-manager [command]"
echo "install - Install a package in a container."
echo "create - create a Distrobox container with Pistachio Linux compatibility."
echo "enter - enter a Distrobox container."
echo "remove - remove a Distrobox container."
echo "uninstall - uninstall a package from a container."
echo "version - show the version of pistachio-manager."
echo "create-terminal-profile - create a Windows Terminal profile for a container."
}
ShowInstallHelp()
{
echo "pistachio-manager install allows you install a package inside of a Distrobox container."
echo
echo "Usage: pistachio-manager install [container] [package]"
echo "container - The name of the container to install the package in."
echo "package - The name of the package to install."
}
ShowUninstallHelp()
{
echo "pistachio-manager uninstall allows you to uninstall a package from a Distrobox container."
echo
echo "Usage: pistachio-manager uninstall [container] [package]"
echo "container - The name of the container to uninstall the package from."
echo "package - The name of the package to uninstall."
}
ShowCreateHelp()
{
echo "pistachio-manager create allows you to create a Distrobox container with Pistachio Linux compatibility."
echo
echo "Usage: pistachio-manager create [distro]"
echo "distro - The name of the container to create. Valid options are \"arch\", \"debian\" and \"fedora\". Case sensitive."
echo "You can also not supply a distro name to pick one with an interactive menu."
}
ShowRemoveHelp()
{
echo "pistachio-manager remove allows you to remove a Distrobox container."
echo
echo "Usage: pistachio-manager remove [container]"
echo "container - The name of the container to remove."
}
ShowCreateTerminalProfileHelp()
{
echo "pistachio-manager create-terminal-profile allows you to create a Windows Terminal profile for a Distrobox container."
echo
echo "Usage: pistachio-manager create-terminal-profile [container]"
echo "container - The name of the container to create a profile for."
}
PistachioIsntAnOption()
{
if [ $1 = "pistachiolinux" -o $1 = "pistachio-linux" ]
then
echo "Pistachio Linux is not a valid option."
exit 1
fi
}
# checks for dependencies
DependencyCheck()
{
if ! command -v distrobox &> /dev/null; then
echo "distrobox is not installed. Please report this issue on GitHub."
fi
if ! command -v podman &> /dev/null; then
echo "podman is not installed. Please report this issue on GitHub."
fi
}
CreateContainerTwo()
{
distro=$1
image=$2
if [ $(podman ps -a | grep $distro | wc -l ) -eq 0 ]
then
echo "Creating container $distro..."
distrobox create $distro --image $image
if [ $? -ne 0 ]
then
echo "Failed to create container $distro."
exit 1
else
echo "Container $distro created."
echo -e "Do you want to create a Windows Terminal profile for $distro? (y/n): \c"
read answer
if [ $answer = "y" ]
then
CreateTerminalProfile $distro
fi
fi
else
echo "Container $distro already exists."
fi
}
CreateContainer()
{
param=$1
DependencyCheck
if [ $param = "arch" ]
then
CreateContainerTwo $param "docker.io/library/archlinux:latest"
fi
if [ $param = "debian" ]
then
CreateContainerTwo $param "debian:latest"
fi
if [ $param = "fedora" ]
then
CreateContainerTwo $param "fedora:latest"
fi
if [ $param != "arch" -a $param != "debian" -a $param != "fedora" ]
then
echo "Invalid distro $param. Valid options are \"arch\", \"debian\" and \"fedora\". Want more distros? Create an issue on GitHub."
exit 1
fi
}
CheckContainerExists()
{
param=$1
if [ $(podman ps -a | grep $param | wc -l) -eq 0 ]
then
echo "Container $param does not exist."
exit 1
fi
}
EnterContainer()
{
param=$1
echo "Entering container $param..."
distrobox enter $param
}
CreateTerminalProfile()
{
param=$1
echo "Creating terminal profile for $param..."
CheckContainerExists $param
echo -e "What's your Windows username?: \c"
read username
profileTemplate='{"profiles": [ { "name": "'$1' (on Pistachio)", "commandline": "wsl.exe -d PistachioLinux pistachio-manager enter '$1'", "colorScheme": "Campbell", "icon": "ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.png" }]}'
fragmentPath="/mnt/c/Users/$username/AppData/Local/Microsoft/Windows Terminal/Fragments/PistachioLinux/$1.json"
if [ ! -d "/mnt/c/Users/$username/AppData/Local/Microsoft/Windows Terminal/Fragments/PistachioLinux" ]
then
sudo mkdir "/mnt/c/Users/$username/AppData/Local/Microsoft/Windows Terminal/Fragments/PistachioLinux"
fi
if [[ ! -f $fragmentPath ]]
then
sudo touch "/mnt/c/Users/$username/AppData/Local/Microsoft/Windows Terminal/Fragments/PistachioLinux/$1.json"
sudo echo $profileTemplate | jq '.' > "/mnt/c/Users/$username/AppData/Local/Microsoft/Windows Terminal/Fragments/PistachioLinux/$1.json"
echo "Profile $1 created. Restart Windows Terminal to see it in the profile dropdown."
else
echo "Profile already exists."
fi
}
if [[ "$EUID" = 0 ]]; then
echo "Do not run pistachio-manager as root!"
exit 1
fi
# pistachio-manager
if [ $# -eq 0 ]
then
ShowHelp
fi
# pistachio-manager install
if [ "$1" = "install" ]
then
if [ $# -eq 1 ]
then
ShowInstallHelp
fi
if [ $# -eq 2 ]
then
echo "Not enough arguments."
ShowInstallHelp
fi
if [ $# -eq 3 ]
then
DependencyCheck
PistachioIsntAnOption $2
echo "Installing $3 in $2..."
# check if container exists using podman
if [ $(podman ps -a | grep $2 | wc -l) -eq 0 ]
then
echo "Container $2 does not exist. Try creating it!"
exit 1
else
# container exists, install package
if [ $2 = "arch" ]
then
if ! podman exec -it $2 pacman;
then
echo "Container $2 is not running. Starting it..."
podman start $2
sleep 1
fi
podman exec -it $2 pacman -S $3
fi
if [ $2 = "debian" ]
then
if ! podman exec -it $2 apt;
then
echo "Container $2 is not running. Starting it..."
podman start $2
sleep 1
fi
podman exec $2 apt install -y $3
fi
if [ $2 = "fedora" ]
then
if ! podman exec -it $2 dnf;
then
echo "Container $2 is not running. Starting it..."
podman start $2
sleep 1
fi
podman exec $2 dnf install -y $3
fi
echo "Could not figure out which package manager to use for \"$2\". You will have to install $3 in $2 manually."
exit 1
fi
fi
fi
# pistachio-manager create
if [ "$1" = "create" ]
then
if [ $# -eq 1 ]
then
# open a whiptail dialog to ask which distro to create
distro=$(whiptail --backtitle "Pistachio Manager" --title "Create a container" --menu "Choose which Linux distribution this container will use:" 15 60 4 \
"arch" "Arch Linux" \
"debian" "Debian" \
"fedora" "Fedora" 3>&1 1>&2 2>&3)
# check if the user pressed cancel
if [ $? -eq 1 ]
then
exit 1
fi
# check if the user pressed enter
if [ $? -eq 0 ]
then
CreateContainer $distro
fi
fi
if [ $# -eq 2 ]
then
CreateContainer $2
fi
fi
# pistachio-manager remove
if [ "$1" = "remove" ]
then
if [ $# -eq 1 ]
then
ShowRemoveHelp
fi
if [ $# -eq 2 ]
then
PistachioIsntAnOption $2
if [ $(podman ps -a | grep $2 | wc -l) -eq 0 ]
then
echo "Container $2 does not exist."
exit 1
else
echo "Removing container $2..."
podman rm -f $2
echo "Container $2 removed."
fi
fi
fi
# pistachio-manager uninstall
if [ "$1" = "uninstall" ]
then
if [ $# -eq 1 ]
then
ShowUninstallHelp
fi
if [ $# -eq 2 ]
then
PistachioIsntAnOption $2
if [ $(podman ps -a | grep $2 | wc -l) -eq 0 ]
then
echo "Container $2 does not exist."
exit 1
else
echo "Uninstalling container $2..."
podman rm $2
echo "Container $2 uninstalled."
fi
fi
fi
# pistachio-manager help
if [ "$1" = "help" ]
then
if [ $# -eq 1 ]
then
ShowHelp
fi
if [ $# -eq 2 ]
then
if [ $2 = "install" ]
then
ShowInstallHelp
fi
if [ $2 = "create" ]
then
ShowCreateHelp
fi
if [ $2 = "enter" ]
then
ShowEnterHelp
fi
if [ $2 = "remove" ]
then
ShowRemoveHelp
fi
if [ $2 = "uninstall" ]
then
ShowUninstallHelp
fi
if [ $2 = "version" ]
then
ShowVersionHelp
fi
if [ $2 = "create-terminal-profile" ]
then
ShowCreateTerminalProfileHelp
fi
fi
fi
# pistachio-manager version
if [ "$1" = "version" ]
then
echo "pistachio-manager version $VERSION"
exit 0
fi
# pistachio-manager create-terminal-profile
if [ "$1" = "create-terminal-profile" ]
then
if [ $# -eq 1 ]
then
ShowCreateTerminalProfileHelp
fi
if [ $# -eq 2 ]
then
PistachioIsntAnOption $2
CheckContainerExists $2
CreateTerminalProfile $2
fi
fi
# pistachio-manager enter
if [ "$1" = "enter" ]
then
if [ $# -eq 1 ]
then
ShowEnterHelp
fi
if [ $# -eq 2 ]
then
PistachioIsntAnOption $2
CheckContainerExists $2
EnterContainer $2
fi
fi
# if command is not recognized
if [ "$1" != "install" ] && [ "$1" != "create" ] && [ "$1" != "enter" ] && [ "$1" != "help" ] && [ "$1" != "" ] && [ "$1" != "remove" ] && [ "$1" != "uninstall" ] && [ "$1" != "version" ] && [ "$1" != "create-terminal-profile" ]
then
echo "Command \"$1\" is not a pistachio-manager command."
ShowHelp
fi