-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.machinesrc
72 lines (56 loc) · 1.54 KB
/
.machinesrc
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
#!/bin/bash
CONFIG_FILE="./machine.config"
OLD_PROMPT="$PS1"
MACHINE_ID="$(pwd | base64 | sha256sum | grep -oP '[a-z0-9]+')"
MACHINE_STORAGE_LOCATION="/tmp/$MACHINE_ID/data"
read_key() {
local key="$1"
while IFS= read -r line; do
if [[ "$line" =~ ^$key=(.*)$ ]]; then
echo "${BASH_REMATCH[1]}"
break
fi
done < "$CONFIG_FILE"
}
connect_container() {
storage="$1"
image_name="$2"
container_running=$(docker ps -a --format "{{.Names}}" | grep $MACHINE_ID)
if [ -n "$container_running" ]; then
docker start $MACHINE_ID
docker exec -it $MACHINE_ID /bin/sh
else
docker run --name $MACHINE_ID --volume=$MACHINE_STORAGE_LOCATION:$storage -it --tty $image_name
fi
}
show_info() {
image_name="$1"
echo ""
echo -e "\033[1;37mOS INFO\033[0m"
echo ""
echo -e "\033[1;36mID:\033[0m\t\t\t$MACHINE_ID"
echo -e "\033[1;36mIMAGE:\033[0m\t\t\t$image_name"
echo -e "\033[1;36mSTORAGE LOCATION:\033[0m\t$MACHINE_STORAGE_LOCATION"
echo ""
}
remove_container() {
docker container stop $MACHINE_ID
docker container rm $MACHINE_ID
}
machine_prompt() {
local prompt="$OLD_PROMPT"
if [ -e "$CONFIG_FILE" ]; then
prompt="💻 $OLD_PROMPT"
machine_name="$(read_key name)"
image_name="$(read_key image)"
storage="$(read_key storage)"
mkdir -p "$MACHINE_STORAGE_LOCATION"
alias os_connect="connect_container $storage $image_name"
alias os_info="show_info $image_name"
alias os_remove="remove_container"
else
prompt="$OLD_PROMPT"
fi
PS1="$prompt"
}
PROMPT_COMMAND=machine_prompt