lxc-awesome-ephemeral 0.0.1.beta1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/bin/lxc-awesome-ephemeral +3 -0
- data/bin/lxc-awesome-ephemeral.sh +533 -0
- data/lxc-awesome-ephemeral.gemspec +16 -0
- metadata +55 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Sean Porter
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# LXC Awesome Ephemeral
|
2
|
+
|
3
|
+
A Ruby gem to encapsulate [lxc-awesome-ephemeral](https://github.com/hw-cookbooks/lxc/blob/master/files/default/lxc-awesome-ephemeral).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'lxc-awesome-ephemeral'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install lxc-awesome-ephemeral
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
`lxc-awesome-ephemeral --help`
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,533 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
|
3
|
+
# (C) Copyright Canonical 2011,2012
|
4
|
+
|
5
|
+
# What lxc container to clone
|
6
|
+
LXC_BASE=""
|
7
|
+
# $2 is a path to bind mount e.g. /tmp/foo.
|
8
|
+
LXC_BIND=""
|
9
|
+
UNION="overlayfs"
|
10
|
+
|
11
|
+
usage() {
|
12
|
+
echo "usage: lxc-awesome-ephemeral [-I ipaddress] [-G gateway] [-N netmask] [-D size] [-z rdir] [-U uniontype] [-d|--daemon] [-h] [-b bdir] [-u user] [-S key] [-c clean] -o orig [-n name] -- [COMMAND [ARGS...]]"
|
13
|
+
}
|
14
|
+
|
15
|
+
help() {
|
16
|
+
usage
|
17
|
+
echo
|
18
|
+
echo "Runs an ephemeral (one-off) container"
|
19
|
+
echo
|
20
|
+
echo "Options:"
|
21
|
+
echo "orig : name of the original container"
|
22
|
+
echo "name : name of new container"
|
23
|
+
echo "bdir : directory to bind mount into container"
|
24
|
+
echo "user : the user to connect to the container as"
|
25
|
+
echo "key : the path to the SSH key to use to connect"
|
26
|
+
echo "size : size of virtual device in M"
|
27
|
+
echo "rdir : host directory to store rootfs overlay"
|
28
|
+
echo "ipaddress : static ipv4 to use instead of dhcp"
|
29
|
+
echo "clean : cleanup directories"
|
30
|
+
echo "-d : run in the background"
|
31
|
+
echo "-c : clean up directories created for an ephemeral container"
|
32
|
+
echo "-n : name for the ephemeral container"
|
33
|
+
echo "-U : type of union (aufs or overlayfs)"
|
34
|
+
echo " Default is overlayfs"
|
35
|
+
echo "-D : block device for rootfs overlay"
|
36
|
+
echo "-z : directory to use for rootfs overlay"
|
37
|
+
echo "-I : ip address to use"
|
38
|
+
echo "-G : gateway to use"
|
39
|
+
echo "-N : netmask to use"
|
40
|
+
echo
|
41
|
+
echo "if a COMMAND is given, then the container will run only as long"
|
42
|
+
echo "as the command runs. If no COMMAND is given, this command will"
|
43
|
+
echo "wait until the container is shut down. In the case of --cleanup,"
|
44
|
+
echo "any provided command will be ignored"
|
45
|
+
}
|
46
|
+
|
47
|
+
shortoptions='hb:o:u:D:z:I:G:N:S:dU:n:c'
|
48
|
+
longoptions='help,orig:,bdir:,user:,clean,device:,directory:,ipaddress:,gateway:,netmask:,ssh-key:,name:,daemon,union:'
|
49
|
+
|
50
|
+
LXC_RUNNING=0
|
51
|
+
LXC_MOUNTED=0
|
52
|
+
DAEMON=0
|
53
|
+
CLEAN=0
|
54
|
+
|
55
|
+
cleanup_dirs()
|
56
|
+
{
|
57
|
+
# echo "umounting ephemeral_bind_dir $EPHEMERAL_BIND_DIR" >&2
|
58
|
+
sudo umount $EPHEMERAL_BIND_DIR
|
59
|
+
# echo "umounting lxc_dir $LXC_DIR" >&2
|
60
|
+
sudo umount $LXC_DIR/rootfs
|
61
|
+
# echo "umounting overlay" >&2
|
62
|
+
if [ ! $HOST_OVERLAY_DIRECTORY ]; then
|
63
|
+
sudo umount $OVERLAY_DIR
|
64
|
+
fi
|
65
|
+
# remove all contents of the content dir
|
66
|
+
sudo rmdir $LXC_DIR/ephemeralbind
|
67
|
+
sudo rmdir $LXC_DIR/rootfs
|
68
|
+
sudo rm -f $LXC_DIR/*
|
69
|
+
|
70
|
+
# echo "rming lxc_dir $LXC_DIR" >&2
|
71
|
+
sudo rmdir $LXC_DIR
|
72
|
+
# echo "rming overlay dir $OVERLAY_DIR" >&2
|
73
|
+
if [ $HOST_OVERLAY_DIRECTORY ]; then
|
74
|
+
if [ $VIRT_DIR ]; then
|
75
|
+
sudo umount $VIRT_MNT
|
76
|
+
sudo rmdir $VIRT_MNT
|
77
|
+
sudo rm $VIRT_DEV
|
78
|
+
fi
|
79
|
+
sudo rm -rf $HOST_OVERLAY_DIRECTORY
|
80
|
+
else
|
81
|
+
sudo rmdir $OVERLAY_DIR
|
82
|
+
fi
|
83
|
+
}
|
84
|
+
|
85
|
+
create_virt_device() {
|
86
|
+
echo "Creating ephemeral virtual device for rootfs (${OVERLAY_DEVICE}M)"
|
87
|
+
VIRT_DIR="/tmp/lxc-virt-devs"
|
88
|
+
VIRT_IMG_DIR="${VIRT_DIR}/imgs"
|
89
|
+
VIRT_MNT_DIR="${VIRT_DIR}/mnt"
|
90
|
+
VIRT_DEV="${VIRT_IMG_DIR}/${LXC_NAME}"
|
91
|
+
VIRT_MNT="${VIRT_MNT_DIR}/${LXC_NAME}"
|
92
|
+
sudo mkdir -p $VIRT_DIR
|
93
|
+
sudo mkdir -p $VIRT_IMG_DIR
|
94
|
+
sudo mkdir -p $VIRT_MNT
|
95
|
+
# Create empty disk
|
96
|
+
dd if=/dev/zero of=$VIRT_DEV bs=1k seek=${OVERLAY_DEVICE}k count=1 > /dev/null
|
97
|
+
# format device
|
98
|
+
echo "y" | mkfs -t ext4 $VIRT_DEV > /dev/null
|
99
|
+
# mount loopback
|
100
|
+
mount -o loop $VIRT_DEV $VIRT_MNT
|
101
|
+
HOST_OVERLAY_DIRECTORY=$VIRT_MNT
|
102
|
+
}
|
103
|
+
|
104
|
+
cleanup() {
|
105
|
+
if [ $LXC_RUNNING -eq 1 ]; then
|
106
|
+
sudo lxc-stop -n $LXC_NAME
|
107
|
+
fi
|
108
|
+
if [ $LXC_MOUNTED -eq 1 ]; then
|
109
|
+
cleanup_dirs
|
110
|
+
fi
|
111
|
+
exit 1
|
112
|
+
}
|
113
|
+
|
114
|
+
do_mount() {
|
115
|
+
lower=$1
|
116
|
+
if [ $OVERLAY_DEVICE ]; then
|
117
|
+
create_virt_device
|
118
|
+
upper=$HOST_OVERLAY_DIRECTORY
|
119
|
+
echo "Using local block device for overlay mounted at: ${HOST_OVERLAY_DIRECTORY}"
|
120
|
+
elif [ $HOST_OVERLAY_DIRECTORY ]; then
|
121
|
+
mkdir -p $HOST_OVERLAY_DIRECTORY
|
122
|
+
mkdir "$HOST_OVERLAY_DIRECTORY/$LXC_NAME"
|
123
|
+
HOST_OVERLAY_DIRECTORY="$HOST_OVERLAY_DIRECTORY/$LXC_NAME"
|
124
|
+
upper=$HOST_OVERLAY_DIRECTORY
|
125
|
+
echo "Using local directory for overlay: ${HOST_OVERLAY_DIRECTORY}"
|
126
|
+
else
|
127
|
+
upper=$2
|
128
|
+
fi
|
129
|
+
target=$3
|
130
|
+
if [ $UNION = "aufs" ]; then
|
131
|
+
sudo mount -t aufs -o br=${upper}=rw:${lower}=ro,noplink none ${target}
|
132
|
+
else
|
133
|
+
sudo mount -t overlayfs -oupperdir=${upper},lowerdir=${lower} none ${target}
|
134
|
+
fi
|
135
|
+
}
|
136
|
+
|
137
|
+
trap cleanup SIGTERM SIGINT SIGQUIT
|
138
|
+
|
139
|
+
getopt=$(getopt -o $shortoptions --longoptions $longoptions -- "$@")
|
140
|
+
if [ $? != 0 ]; then
|
141
|
+
usage
|
142
|
+
exit 1;
|
143
|
+
fi
|
144
|
+
|
145
|
+
eval set -- "$getopt"
|
146
|
+
|
147
|
+
while true; do
|
148
|
+
case "$1" in
|
149
|
+
-h|--help)
|
150
|
+
help
|
151
|
+
exit 1
|
152
|
+
;;
|
153
|
+
-o|--orig)
|
154
|
+
shift
|
155
|
+
LXC_BASE=$1
|
156
|
+
shift
|
157
|
+
;;
|
158
|
+
-n|--name)
|
159
|
+
shift
|
160
|
+
LXC_NAME=$1
|
161
|
+
shift
|
162
|
+
;;
|
163
|
+
-D|--device)
|
164
|
+
shift
|
165
|
+
OVERLAY_DEVICE=$1
|
166
|
+
shift
|
167
|
+
;;
|
168
|
+
-z|--directory)
|
169
|
+
shift
|
170
|
+
HOST_OVERLAY_DIRECTORY=$1
|
171
|
+
shift
|
172
|
+
;;
|
173
|
+
-b|--bdir)
|
174
|
+
shift
|
175
|
+
LXC_BIND=$1
|
176
|
+
shift
|
177
|
+
;;
|
178
|
+
-u|--user)
|
179
|
+
shift
|
180
|
+
LXC_USER=$1
|
181
|
+
shift
|
182
|
+
;;
|
183
|
+
-S|--ssh-key)
|
184
|
+
shift
|
185
|
+
LXC_KEY="-i $1"
|
186
|
+
shift
|
187
|
+
;;
|
188
|
+
-c|--clean)
|
189
|
+
CLEAN=1
|
190
|
+
shift
|
191
|
+
;;
|
192
|
+
-d|--detach)
|
193
|
+
DAEMON=1
|
194
|
+
shift
|
195
|
+
;;
|
196
|
+
-I|--ipaddress)
|
197
|
+
shift
|
198
|
+
CUSTOM_IPADDRESS=$1
|
199
|
+
shift
|
200
|
+
;;
|
201
|
+
-G|--gateway)
|
202
|
+
shift
|
203
|
+
CUSTOM_GATEWAY=$1
|
204
|
+
shift
|
205
|
+
;;
|
206
|
+
-N|--netmask)
|
207
|
+
shift
|
208
|
+
CUSTOM_NETMASK=$1
|
209
|
+
shift
|
210
|
+
;;
|
211
|
+
-U|--union)
|
212
|
+
shift
|
213
|
+
UNION=$1
|
214
|
+
shift
|
215
|
+
;;
|
216
|
+
--)
|
217
|
+
shift
|
218
|
+
break;;
|
219
|
+
*)
|
220
|
+
echo $1
|
221
|
+
usage
|
222
|
+
exit 1
|
223
|
+
;;
|
224
|
+
esac
|
225
|
+
done
|
226
|
+
|
227
|
+
COMMAND=$@
|
228
|
+
COMMAND_LENGTH=$#
|
229
|
+
LXC_USER=${LXC_USER:-`id -un`}
|
230
|
+
|
231
|
+
# validation
|
232
|
+
|
233
|
+
if [ -z $LXC_BASE ]; then
|
234
|
+
echo "original container must be specified"
|
235
|
+
usage
|
236
|
+
exit 1
|
237
|
+
fi
|
238
|
+
if [ ! -d /var/lib/lxc/$LXC_BASE ] ; then
|
239
|
+
echo "no such lxc container $LXC_BASE"
|
240
|
+
exit 1
|
241
|
+
fi
|
242
|
+
|
243
|
+
if [ "$UNION" != "overlayfs" -a "$UNION" != "aufs" ]; then
|
244
|
+
echo "Invalid option for union: choose overlayfs or aufs."
|
245
|
+
exit 1
|
246
|
+
fi
|
247
|
+
|
248
|
+
decide_dirs()
|
249
|
+
{
|
250
|
+
if [ -z "$LXC_NAME" ] ; then
|
251
|
+
echo "You can only use the --clean option if the --name option is also specified "
|
252
|
+
fi
|
253
|
+
EPHEMERAL_BIND_DIR=/var/lib/lxc/$LXC_NAME/ephemeralbind
|
254
|
+
# echo "umounting ephemeral_bind_dir $EPHEMERAL_BIND_DIR" >&2
|
255
|
+
LXC_DIR=/var/lib/lxc/$LXC_NAME
|
256
|
+
if [ -z $HOST_OVERLAY_DIRECTORY ] && [ -z $OVERLAY_DEVICE ] ; then
|
257
|
+
OVERLAY_DIR=/tmp/lxc-$LXC_NAME
|
258
|
+
fi
|
259
|
+
}
|
260
|
+
|
261
|
+
setup_container()
|
262
|
+
{
|
263
|
+
echo "Setting up ephemeral container..."
|
264
|
+
if [ $HOST_OVERLAY_DIRECTORY ]; then
|
265
|
+
echo " -- Using local directory for overlay: ${HOST_OVERLAY_DIRECTORY}"
|
266
|
+
elif [ $OVERLAY_DEVICE ]; then
|
267
|
+
echo " -- Using overlay virtual block device"
|
268
|
+
fi
|
269
|
+
|
270
|
+
if [ -z $LXC_NAME ] ; then
|
271
|
+
LXC_DIR=`sudo mktemp -d --tmpdir=/var/lib/lxc $LXC_BASE-temp-XXXXXXX`
|
272
|
+
LXC_NAME=`basename $LXC_DIR`
|
273
|
+
else
|
274
|
+
`sudo mkdir /var/lib/lxc/$LXC_NAME`
|
275
|
+
LXC_DIR=/var/lib/lxc/$LXC_NAME
|
276
|
+
fi
|
277
|
+
|
278
|
+
if [ -z $HOST_OVERLAY_DIRECTORY ] && [ -z $OVERLAY_DEVICE ] ; then
|
279
|
+
mkdir /tmp/lxc-$LXC_NAME
|
280
|
+
OVERLAY_DIR=/tmp/lxc-$LXC_NAME
|
281
|
+
sudo mount -t tmpfs none $OVERLAY_DIR
|
282
|
+
fi
|
283
|
+
|
284
|
+
|
285
|
+
sudo chmod 755 ${LXC_DIR}
|
286
|
+
sudo mkdir ${LXC_DIR}/rootfs
|
287
|
+
do_mount "/var/lib/lxc/$LXC_BASE/rootfs" "${OVERLAY_DIR}" ${LXC_DIR}/rootfs
|
288
|
+
EPHEMERAL_BIND_DIR=$LXC_DIR/ephemeralbind
|
289
|
+
sudo mkdir $EPHEMERAL_BIND_DIR
|
290
|
+
sudo mount -t tmpfs none $EPHEMERAL_BIND_DIR
|
291
|
+
LXC_MOUNTED=1
|
292
|
+
{
|
293
|
+
d1=/var/lib/lxc/${LXC_BASE}
|
294
|
+
for f in ${d1}/*; do
|
295
|
+
if [ -f $f ]; then
|
296
|
+
sudo cp $f $LXC_DIR/
|
297
|
+
fi
|
298
|
+
done
|
299
|
+
}
|
300
|
+
|
301
|
+
# Update the ephemeral lxc's configuration to reflect the new container name.
|
302
|
+
# Check all the places known distros keep hostnames.
|
303
|
+
# FIXME: should we sanity check the hostname to make sure it contains no bad chars?
|
304
|
+
for file in $LXC_DIR/fstab $LXC_DIR/config \
|
305
|
+
$LXC_DIR/rootfs/etc/hostname \
|
306
|
+
$LXC_DIR/rootfs/etc/hosts \
|
307
|
+
$LXC_DIR/rootfs/etc/sysconfig/network \
|
308
|
+
$LXC_DIR/rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
|
309
|
+
do
|
310
|
+
if test -f "$file"
|
311
|
+
then
|
312
|
+
sudo sed -i -e "s/$LXC_BASE/$LXC_NAME/" $file
|
313
|
+
fi
|
314
|
+
done
|
315
|
+
|
316
|
+
dist=`cat $LXC_DIR/rootfs/etc/issue | head -n 1 | cut -f1 -d\ `
|
317
|
+
if [ $dist = 'CentOS' ] ; then
|
318
|
+
DISTRO="EL"
|
319
|
+
else
|
320
|
+
DISTRO="DEBIAN"
|
321
|
+
fi
|
322
|
+
|
323
|
+
# special tweaks for the centos family of distributions
|
324
|
+
if [ $DISTRO = "EL" ] ; then
|
325
|
+
cat <<EOF > $LXC_DIR/rootfs/etc/sysconfig/network
|
326
|
+
NETWORKING=yes
|
327
|
+
HOSTNAME=$LXC_NAME
|
328
|
+
EOF
|
329
|
+
echo "hostname $LXC_NAME" >> $LXC_DIR/rootfs/etc/rc.local
|
330
|
+
fi
|
331
|
+
|
332
|
+
# Update the fstab to have all bind mounts be ephemeral.
|
333
|
+
sudo cp $LXC_DIR/fstab $LXC_DIR/fstab.old
|
334
|
+
while read line; do
|
335
|
+
# Pull out the second field of the current line of fstab info
|
336
|
+
path=`echo -n $line | awk '{print $2}'`
|
337
|
+
# If LXC_BIND is not set, or the mount destination of this line is not
|
338
|
+
# LXC_BIND...
|
339
|
+
if [ -z "$LXC_BIND" -o "`readlink -f $path`" != "`readlink -f $LXC_DIR/rootfs$LXC_BIND`" ];
|
340
|
+
then
|
341
|
+
# ...then we should write some form of this line.
|
342
|
+
# If this line is a bind...
|
343
|
+
if [ "`echo -n $line | awk '{print $4}'`" = "bind" ]; then
|
344
|
+
# ...we should rewrite it as an overlay.
|
345
|
+
source=`echo -n $line | awk '{print $1}'`
|
346
|
+
upperdir=$EPHEMERAL_BIND_DIR$source
|
347
|
+
sudo mkdir -p $upperdir
|
348
|
+
sudo chown `sudo stat -c '%U.%G' $source` $upperdir
|
349
|
+
if [ $UNION = "overlayfs" ]; then
|
350
|
+
echo "none $path overlayfs upperdir=$upperdir,lowerdir=$source 0 0";
|
351
|
+
else
|
352
|
+
echo "none $path aufs br=${upperdir}=rw:${lowerdir}=ro,noplink 0 0";
|
353
|
+
fi
|
354
|
+
else
|
355
|
+
# Otherwise, we can pass it through unchanged.
|
356
|
+
echo "$line";
|
357
|
+
fi
|
358
|
+
fi
|
359
|
+
done < $LXC_DIR/fstab.old | sudo tee $LXC_DIR/fstab >/dev/null
|
360
|
+
|
361
|
+
# If LXC_BIND is defined, add it to fstab.
|
362
|
+
if [ -n "$LXC_BIND" ]; then
|
363
|
+
sudo mkdir -p $LXC_DIR/rootfs$LXC_BIND
|
364
|
+
echo "$LXC_BIND $LXC_DIR/rootfs$LXC_BIND none bind 0 0" | sudo tee -a $LXC_DIR/fstab >/dev/null
|
365
|
+
fi
|
366
|
+
|
367
|
+
# update the ephemeral container's MAC address (lifted from lxc-clone)
|
368
|
+
c=$LXC_DIR/config
|
369
|
+
# change hwaddrs
|
370
|
+
sudo mv ${c} ${c}.old
|
371
|
+
(
|
372
|
+
while read line; do
|
373
|
+
if [ "${line:0:18}" = "lxc.network.hwaddr" ]; then
|
374
|
+
echo "lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')"
|
375
|
+
else
|
376
|
+
echo "$line"
|
377
|
+
fi
|
378
|
+
done
|
379
|
+
) < ${c}.old | sudo tee ${c} >/dev/null
|
380
|
+
sudo rm -f ${c}.old
|
381
|
+
|
382
|
+
if [ $CUSTOM_IPADDRESS ]; then
|
383
|
+
if [ -z $CUSTOM_GATEWAY ]; then
|
384
|
+
CUSTOM_GATEWAY=`echo $CUSTOM_IPADDRESS | sed 's/[0-9]\+$/1/'`
|
385
|
+
fi
|
386
|
+
if [ -z $CUSTOM_NETMASK ]; then
|
387
|
+
CUSTOM_NETMASK='255.255.255.0'
|
388
|
+
fi
|
389
|
+
write_custom_networking
|
390
|
+
fi
|
391
|
+
}
|
392
|
+
|
393
|
+
get_ip()
|
394
|
+
{
|
395
|
+
# Get init's PID
|
396
|
+
PID=$(sudo lxc-info -n $1 -p | awk '{print $2}')
|
397
|
+
[ "$PID" = "-1" ] && return 1
|
398
|
+
|
399
|
+
# Get some unique path
|
400
|
+
DST=$(sudo mktemp -u --tmpdir=/run/netns/)
|
401
|
+
NAME=$(basename $DST)
|
402
|
+
|
403
|
+
# Prepare the /run/netns entry for "ip netns"
|
404
|
+
sudo mkdir -p /run/netns
|
405
|
+
sudo ln -s /proc/$PID/ns/net $DST
|
406
|
+
|
407
|
+
# Grab all the public globally routed IPv4 and IPv6 addresses
|
408
|
+
(sudo ip netns exec $NAME ip -4 addr show scope global && \
|
409
|
+
sudo ip netns exec $NAME ip -6 addr show scope global) | grep inet | while read line; do
|
410
|
+
ip=$(echo $line | awk '{print $2}' | cut -d '/' -f1)
|
411
|
+
echo "$ip"
|
412
|
+
done
|
413
|
+
|
414
|
+
sudo rm $DST
|
415
|
+
}
|
416
|
+
|
417
|
+
start_container()
|
418
|
+
{
|
419
|
+
echo "Starting up the container..."
|
420
|
+
sudo lxc-start -n $LXC_NAME -d
|
421
|
+
sudo lxc-wait -s RUNNING -n $LXC_NAME
|
422
|
+
LXC_RUNNING=1
|
423
|
+
|
424
|
+
if [ $COMMAND_LENGTH -gt 0 ]; then
|
425
|
+
# When lxc-attach support arrives in the kernel, we can switch to
|
426
|
+
# that.
|
427
|
+
# Meanwhile, we use get_ip to wait for container's network to be up
|
428
|
+
# and to obtain the ip address, then we can ssh to the lxc.
|
429
|
+
TRIES=60
|
430
|
+
FAILED=1
|
431
|
+
|
432
|
+
# Repeatedly try to connect over SSH until we either succeed
|
433
|
+
# or time out.
|
434
|
+
for i in $(seq 1 $TRIES); do
|
435
|
+
# We call get_ip inside the loop to ensure the correct ip
|
436
|
+
# is retrieved even in the case the DHCP ip assignment
|
437
|
+
# changes during the process.
|
438
|
+
IP_ADDRESS=$(get_ip $LXC_NAME)
|
439
|
+
if [ -z "$IP_ADDRESS" ]; then
|
440
|
+
sleep 1
|
441
|
+
continue
|
442
|
+
fi
|
443
|
+
|
444
|
+
# Iterate through all the addresses (if multiple)
|
445
|
+
for ip in $IP_ADDRESS; do
|
446
|
+
ssh -n -o StrictHostKeyChecking=no \
|
447
|
+
-o UserKnownHostsFile=/dev/null \
|
448
|
+
$LXC_KEY $LXC_USER@$IP_ADDRESS -- "$COMMAND"
|
449
|
+
SSH_RET=$?
|
450
|
+
if [ ! 255 -eq $SSH_RET ]; then
|
451
|
+
# If ssh returns 255 then its connection failed.
|
452
|
+
# Anything else is either success (status 0) or a
|
453
|
+
# failure from whatever we ran over the SSH connection.
|
454
|
+
# In those cases we want to stop looping, so we break
|
455
|
+
# here
|
456
|
+
return $SSH_RET
|
457
|
+
fi
|
458
|
+
done
|
459
|
+
sleep 1
|
460
|
+
done
|
461
|
+
|
462
|
+
echo "could not get IP address - aborting." >&2
|
463
|
+
return 255
|
464
|
+
else
|
465
|
+
sudo lxc-wait -n $LXC_NAME -s RUNNING
|
466
|
+
echo "$LXC_NAME is running"
|
467
|
+
echo "You connect with the command:"
|
468
|
+
echo " sudo lxc-console -n $LXC_NAME"
|
469
|
+
fi
|
470
|
+
}
|
471
|
+
|
472
|
+
write_custom_networking()
|
473
|
+
{
|
474
|
+
if [ $DISTRO = "EL" ] ; then
|
475
|
+
cat <<EOF > $LXC_DIR/rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
|
476
|
+
DEVICE=eth0
|
477
|
+
BOOTPROTO=static
|
478
|
+
NETMASK=$CUSTOM_NETMASK
|
479
|
+
IPADDR=$CUSTOM_IPADDRESS
|
480
|
+
ONBOOT=yes
|
481
|
+
TYPE=Ethernet
|
482
|
+
USERCTL=yes
|
483
|
+
PEERDNS=yes
|
484
|
+
IPV6INIT=no
|
485
|
+
GATEWAY=$CUSTOM_GATEWAY
|
486
|
+
EOF
|
487
|
+
else
|
488
|
+
cat <<EOF > $LXC_DIR/rootfs/etc/network/interfaces
|
489
|
+
auto lo
|
490
|
+
iface lo inet loopback
|
491
|
+
auto eth0
|
492
|
+
iface eth0 inet static
|
493
|
+
address $CUSTOM_IPADDRESS
|
494
|
+
netmask $CUSTOM_NETMASK
|
495
|
+
gateway $CUSTOM_GATEWAY
|
496
|
+
EOF
|
497
|
+
fi
|
498
|
+
}
|
499
|
+
|
500
|
+
stop_container()
|
501
|
+
{
|
502
|
+
echo "Stopping lxc" >&2
|
503
|
+
sudo lxc-stop -n $LXC_NAME
|
504
|
+
sleep 2
|
505
|
+
LXC_RUNNING=0
|
506
|
+
cleanup_dirs
|
507
|
+
}
|
508
|
+
|
509
|
+
handle_container()
|
510
|
+
{
|
511
|
+
setup_container
|
512
|
+
start_container
|
513
|
+
sudo lxc-wait -n $LXC_NAME -s STOPPED
|
514
|
+
RET=$?
|
515
|
+
stop_container
|
516
|
+
exit $RET
|
517
|
+
}
|
518
|
+
|
519
|
+
if [ $CLEAN -eq 1 ]; then
|
520
|
+
decide_dirs
|
521
|
+
stop_container
|
522
|
+
RET=$?
|
523
|
+
exit $RET
|
524
|
+
fi
|
525
|
+
|
526
|
+
if [ $DAEMON -eq 1 ]; then
|
527
|
+
setup_container
|
528
|
+
start_container
|
529
|
+
RET=$?
|
530
|
+
exit $RET
|
531
|
+
fi
|
532
|
+
|
533
|
+
handle_container
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = "lxc-awesome-ephemeral"
|
7
|
+
gem.version = "0.0.1.beta1"
|
8
|
+
gem.authors = ["Sean Porter"]
|
9
|
+
gem.email = ["portertech@gmail.com"]
|
10
|
+
gem.description = "lxc-awesome-ephemeral"
|
11
|
+
gem.summary = "lxc-awesome-ephemeral"
|
12
|
+
gem.homepage = "https://github.com/portertech/lxc-awesome-ephemeral"
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split($/)
|
15
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lxc-awesome-ephemeral
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: 6
|
5
|
+
version: 0.0.1.beta1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sean Porter
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-13 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: lxc-awesome-ephemeral
|
15
|
+
email:
|
16
|
+
- portertech@gmail.com
|
17
|
+
executables:
|
18
|
+
- lxc-awesome-ephemeral
|
19
|
+
- lxc-awesome-ephemeral.sh
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- bin/lxc-awesome-ephemeral
|
29
|
+
- bin/lxc-awesome-ephemeral.sh
|
30
|
+
- lxc-awesome-ephemeral.gemspec
|
31
|
+
homepage: https://github.com/portertech/lxc-awesome-ephemeral
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>'
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 1.3.1
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.24
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: lxc-awesome-ephemeral
|
55
|
+
test_files: []
|