-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_container.sh
executable file
·101 lines (83 loc) · 3.02 KB
/
create_container.sh
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
#!/usr/bin/env bash
# This script can be used to deploy containers on an agent. It will create the
# container, add a user account (and make them sudo optionally), set up the IP
# address, and set up the permissions on the jumpbox for access. It accepts 4 or
# 5 arguments:
# AGENT - the hostname of the remote server that will host the container
# CONTAINER_NAME - name of the container to add
# USERNAME - name of a user to add
# USER_KEY - name of the user's public key (.pub is added automatically)
# SCRIPT - Optional: path to a script to run after the user has been added.
# Assumptions
# - Temporary files are created in the local directory
# PREAMBLE ---------------------------------------------------------------------
# the configuration file specifies the global parameters
root_dir=$(dirname "$0")
source $root_dir/lxd.conf
agent=$1
container=$2
username=$3
user_key=$4
if [[ $# == 5 ]]; then
postscript=$5
fi
#input validation
if [[ "$#" != 4 && "$#" != 5 ]]; then
echo "Syntax: script AGENT CONTAINER_NAME USERNAME USER_KEY [SCRIPT]"
echo "e.g. create_container.sh agent-7 cont_a1 sharmava0 sharmava0.pub"
exit 1
fi
#check if the key for the agent exists
if [ ! -f $KEY_PATH/$agent.pub ]; then
echo "$agent.pub not found"
exit 1
fi
if [ ! -f $KEY_PATH/jumpbox.pub ]; then
echo "jumpbox.pub not found at $KEY_PATH"
exit 1
fi
if [ ! -f $JUMP_TEMPLATE_PATH ]; then
echo "jumpbox template not found"
exit 1
fi
if [ ! -f $KEY_PATH/$user_key ]; then
echo "User key $KEY_PATH/$user_key not found"
exit 1
fi
if [[ "$container" =~ [^a-zA-z0-9-] ]]; then
echo "Invalid container name: can only contain letters, numbers and dashes"
exit 1
fi
# Main -------------------------------------------------------------------------
# launch container
lxc launch $IMAGE $agent:$container
# get the container IP address
unset IP
while [[ -z $IP ]]; do
echo "Waiting for temporary IP..."
sleep 1
IP=$(lxc info $agent:$container | grep -Eo '10.84.[0-9]{1,3}.[0-9]{1,3}')
done
# setup container - after IP is set
lxc exec $agent:$container -- ifconfig eth0 mtu 1300 # needed for ssh tunneling bug
dhcp_addr=$(ssh $agent "awk -F \"\\\"\" '/LXD_IPV4_ADDR/{print \$2}' /etc/default/lxd-bridge")
lxc exec $agent:$container -- sh -c "echo \"supersede dhcp-server-identifier $dhcp_addr;\" | sudo tee --append /etc/dhcp/dhclient.conf" > /dev/null
lxc exec $agent:$container -- ifdown eth0
lxc exec $agent:$container -- ifup eth0
# get the new container IP address
unset IP
while [[ -z $IP ]]; do
echo "Waiting for updated IP..."
sleep 1
IP=$(lxc info $agent:$container | grep -Eo '10.84.[0-9]{1,3}.[0-9]{1,3}')
done
# set up a static IP for the container
ip_addr=$IP
ssh $agent 'echo "dhcp-host='$container','$ip_addr'" | sudo tee --append /etc/default/'$DNS_FILE > /dev/null
# install default apps
lxc exec $agent:$container -- apt-get -qq update
lxc exec $agent:$container -- apt-get -qq install $(cat $DEFAULT_APPS)
if [ "$#" -eq 5 ]
then
./$postscript $agent $container $username $user_key
fi