aboutsummaryrefslogtreecommitdiffstats
path: root/vpp-bootstrap/provision.sh
blob: 7b527824ee1ee581540c351472abd0805131dbf7 (plain)
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
#!/usr/bin/env bash
#
# Copyright (c) 2016 Intel Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

HOME_DIR="/home/$USER"
SSH_OPTIONS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
APT_PROXY_CONF="/etc/apt/apt.conf.d/01proxy"
ENV_FILE="/etc/environment"
UNAMER=$(uname -r)
DEV_BRIDGE="lxcbr1"

sudo sysctl -w vm.nr_hugepages=1024
HUGEPAGES=`sudo sysctl -n  vm.nr_hugepages`
if [ $HUGEPAGES != 1024 ]; then
    echo "ERROR: Unable to get 1024 hugepages, only got $HUGEPAGES.  Cannot finish."
    exit
fi

sudo apt-get -qq update
sudo apt-get -qq install -y --force-yes lxc bridge-utils

echo -e "lxc.network.name=veth0" | sudo tee -a /etc/lxc/default.conf
echo -e "lxc.network.type = veth" | sudo tee -a /etc/lxc/default.conf
echo -e "lxc.network.link = lxcbr1" | sudo tee -a /etc/lxc/default.conf
echo -e "lxc.network.flags = up" | sudo tee -a /etc/lxc/default.conf
echo -e "lxc.network.hwaddr = 00:17:3e:xx:xx:xx\n" | sudo tee -a /etc/lxc/default.conf
echo -e "lxc.network.name=veth_link1" | sudo tee -a /etc/lxc/default.conf

sudo lxc-checkconfig

sudo brctl addbr $DEV_BRIDGE
sudo ip link set $DEV_BRIDGE up

ssh-keygen -t rsa -b 1024 -N "" -f ~/.ssh/id_rsa
openssh_pubkey=`cat ~/.ssh/id_rsa.pub`

function lxc_exec() {

    cntr="$1"
    rCMD="$2"

    CMD="sudo lxc-attach -n $cntr  -- $rCMD"

    echo "$CMD"
    eval "${CMD}"
}

function get_field() {
    file="$1"
    field="$2"
    
    value=$(grep $field $file | awk -F : '{print $2}' | sed -e 's/^[ ]*//' | sed -e 's/kernver/"$UNAMER"/')
    echo $value
}

# LXC gives backend interfaces horrible names, give them a better name.
function rename_veth_interface() {

    local cntr="$1"
    local nifname="$2"

    ifr_index=`sudo lxc-attach -n $cntr -- ip -o link | tail -n 1 | awk -F : '{print $1}'`
    ifr_index=$((ifr_index+1))

    for dir in /sys/class/net/*/
    do
        ifindex=`cat $dir/ifindex`
        if [ $ifindex == $ifr_index ]
            then ifname=`basename $dir`
        fi
    done

    sudo ip link set $ifname down
    sudo ip link set $ifname name $nifname
    sudo ip link set $nifname up
}

for f in $(ls /vagrant/containers/*.cntr)
do
    i=$(basename $f | sed s/.cntr//)
    dist=$(get_field $f DIST)
    ver=$(get_field $f VER)
    packages=$(get_field $f PACKAGES)
    provision_file="/vagrant/containers/"$i".provision.sh"
 
    sudo lxc-create -t download -n $i -- --dist $dist --release $ver --arch amd64
    sudo lxc-start -n $i -d
	
    lxc_exec $i "resolvconf -d veth0"
    lxc_exec $i "dhclient veth0"

    lxc_exec $i "apt-get -qq install -y git openssh-server"
    lxc_exec $i "apt-get -qq update"

    lxc_exec $i "adduser --disabled-password --gecos \"\" $USER"

    lxc_exec $i "mkdir -p /root/.ssh/"
    lxc_exec $i "mkdir -p $HOME_DIR/.ssh/"

    lxc_exec $i "sh -c 'echo $openssh_pubkey >> /root/.ssh/authorized_keys'"
    lxc_exec $i "sh -c 'echo $openssh_pubkey >> $HOME_DIR/.ssh/authorized_keys'"

    lxc_exec $i "chmod 0600 /root/.ssh/authorized_keys"
    lxc_exec $i "chmod 0600 $HOME_DIR/.ssh/authorized_keys"

    lxc_exec $i "chown -R $USER.$USER $HOME_DIR/.ssh/"

    lxc_exec $i "sh -c 'echo \"%$USER ALL=(ALL) NOPASSWD: ALL\" > /etc/sudoers.d/10_$USER'"

    lxc_exec $i "update-alternatives --install /bin/sh sh /bin/bash 100"

    lxc_exec $i "apt-get -qq install $packages"

    ip_address=$(sudo lxc-ls -f | grep $i | awk '{print $3}')
    echo $ip_address $i | sudo tee -a /etc/hosts

    if [ -s $APT_PROXY_CONF ]
    then
        scp $SSH_OPTIONS $APT_PROXY_CONF root@$i:$APT_PROXY_CONF
    fi

    if [ -s $ENV_FILE ]
    then
        scp $SSH_OPTIONS $ENV_FILE root@$i:$ENV_FILE
    fi

#    backend_intr="link_"$cntr
#    rename_veth_interface $i $backend_intr
#    sudo brctl addif $DEV_BRIDGE $backend_intr

    if [ -s $provision_file ]
    then
       tmpname=$(mktemp)".sh"
       scp $SSH_OPTIONS $provision_file $USER@$i:$tmpname
       ssh $SSH_OPTIONS $USER@$i "sh -c $tmpname"
    fi	
done

echo "List of containers deployed in the dev environment:"
for f in $(ls /vagrant/containers/*.cntr)
do
     i=$(basename $f | sed s/.cntr//)
     desc=$(get_field $f DESC)
     echo $i":\t"$desc
done

echo "To get access the dev environment, type 'vagrant ssh'"