This repository has been archived by the owner on Jan 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlego
executable file
·158 lines (139 loc) · 5.31 KB
/
lego
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
#!/bin/bash
# The config file lets you specify what command to run in the container
# Allow the conf file to not exist by making sure that the LEGO_RUN_COMMAND variable has a default value.
LEGO_RUN_COMMAND="/bin/bash"
# Load up lego config file
source /etc/lego.conf
# Prepare volume arguments to pass to docker run
LEGO_VOLUMES_ARG=""
for volume in ${LEGO_VOLUMES}; do
LEGO_VOLUMES_ARG="${LEGO_VOLUMES_ARG} -v ${volume}"
done
# Convert the LEGO_CONTAINER_NAME_IGNORE value to a string that can be
# passed to grep -vE to ignore those containers.
first="y"
for i in ${LEGO_CONTAINER_NAME_IGNORE}; do
if [ "${first}" == "y" ]; then
LEGO_CONTAINER_NAME_IGNORE_GREP="(${i}"
first=""
else
LEGO_CONTAINER_NAME_IGNORE_GREP="${LEGO_CONTAINER_NAME_IGNORE_GREP}|${i}"
fi
done
LEGO_CONTAINER_NAME_IGNORE_GREP="${LEGO_CONTAINER_NAME_IGNORE_GREP})"
# Convert the LEGO_IMAGE_NAME_IGNORE value to a string that can be
# passed to grep -vE to ignore those images.
first="y"
for i in ${LEGO_IMAGE_NAME_IGNORE}; do
if [ "${first}" == "y" ]; then
LEGO_IMAGE_NAME_IGNORE_GREP="(${i}"
first=""
else
LEGO_IMAGE_NAME_IGNORE_GREP="${LEGO_IMAGE_NAME_IGNORE_GREP}|${i}"
fi
done
LEGO_IMAGE_NAME_IGNORE_GREP="${LEGO_IMAGE_NAME_IGNORE_GREP})"
Usage() {
echo "Usage: $0 (command) [arg] ..."
echo ""
echo "Commands:"
echo " list - List defined base and work chroots"
echo " new (base-chroot) (name) [...] - Create a new persistent chroot and run it."
echo " The container name will be like ${USER}-name"
echo " Any arguments after the name will be passed as arguments to docker run"
echo " end (name) - Remove the named work chroot"
echo " run (name | base-chroot) [...] - If provided a work-chroot, start the container and connect to a bash shell inside."
echo " If provided a base-chroot, start the indicated base-chroot and automatically remove it when exited."
echo " Any arguments after the name will be passed as arguments to docker run"
echo " promote (name) - Promote the specified chroot so that new work-chroots can be made based off of it."
echo " demote (name) - Demote the specified chroot so that new work-chroots can no longer be made based off of it."
echo ""
echo "Additional arguments to always pass to docker run can be configured in /etc/lego.conf."
echo "This is useful for volumes and the like. See README for more information."
exit 1
}
RunningChroots() {
docker ps --format '{{.Names}}' | grep -vE "${LEGO_CONTAINER_NAME_IGNORE_GREP}"
}
WorkChroots() {
docker ps -a --format '{{.Names}}' | grep -vE "${LEGO_CONTAINER_NAME_IGNORE_GREP}"
}
BaseChroots() {
# Print all images in the format {image name}:{version}, remove jenkins, registry, and ubuntu images, and remove :latest text
docker images --format '{{.Repository}}:{{.Tag}}' | grep -vE "${LEGO_IMAGE_NAME_IGNORE_GREP}" | sed 's|:latest||'
}
COMMAND=$1
case $COMMAND in
list)
echo "Definted base-chroots (from docker images):"
BaseChroots | sed 's|^| |'
echo ""
echo "Defined work-chroots (from docker ps -a):"
WorkChroots | sed 's|^| |'
exit 0
;;
new)
BASE=$2
NAME=$3
shift
shift
shift
if [ "${NAME}" = "" ] || [ "${BASE}" = "" ]; then Usage; fi
echo "Creating container with the name ${USER}-${NAME} and running it."
echo "This container will still be around when you exit so please remember to end it."
echo ""
echo docker run -it --name \"${USER}-${NAME}\" --user builder ${LEGO_VOLUMES_ARG} $@ \"${BASE}\" \"${LEGO_RUN_COMMAND}\"
docker run -it --name "${USER}-${NAME}" --user builder ${LEGO_VOLUMES_ARG} $@ "${BASE}" "${LEGO_RUN_COMMAND}"
exit 0
;;
end)
NAME=$2
if [ "${NAME}" = "" ]; then Usage; fi
echo docker rm \"${NAME}\"
if docker rm "${NAME}"; then
echo "Removed container with the name ${NAME}"
fi
exit 0
;;
run)
NAME=$2
shift
shift
if [ "${NAME}" = "" ]; then Usage; fi
if RunningChroots | grep -q "^${NAME}\$"; then
echo "Running another shell inside running chroot ${NAME}"
echo docker exec -it \"${NAME}\" \"${LEGO_RUN_COMMAND}\"
docker exec -it "${NAME}" "${LEGO_RUN_COMMAND}"
elif WorkChroots | grep -q "^${NAME}\$"; then
echo "Running persistent chroot ${NAME}"
echo docker start -i \"${NAME}\"
docker start -i "${NAME}"
elif BaseChroots | grep -q "^${NAME}\$"; then
echo "Starting transient chroot of base ${NAME}."
echo "WARNING: This chroot will be automatically cleaned up when you exit."
echo docker run --rm -it --name \"${USER}-TRANSIENT\" --user builder ${LEGO_VOLUMES_ARG} $@ \"${NAME}\" \"${LEGO_RUN_COMMAND}\"
docker run --rm -it --name "${USER}-TRANSIENT" --user builder ${LEGO_VOLUMES_ARG} $@ "${NAME}" "${LEGO_RUN_COMMAND}"
fi
exit 0
;;
promote)
NAME=$2
if [ "${NAME}" = "" ]; then Usage; fi
BASE="$(docker ps -a --format '{{.Image}}' --filter name="${NAME}")"
echo docker commit ${NAME} \"${BASE}:${NAME}\"
docker commit ${NAME} "${BASE}:${NAME}"
;;
demote)
NAME=$2
if [ "${NAME}" = "" ]; then Usage; fi
BASE="$(docker ps -a --format '{{.Image}}' --filter name="${NAME}")"
echo docker rmi \"${BASE}:${NAME}\"
docker rmi "${BASE}:${NAME}"
;;
*)
Usage
;;
'')
Usage
;;
esac