f05d1f526e50d3fe26021318ef75e4ea9ba68baf
[joid.git] / ci / 02-maasdeploy.sh
1 #!/bin/bash
2 #placeholder for deployment script.
3 set -ex
4
5 virtinstall=0
6
7 case "$1" in
8     'intelpod5' )
9         cp maas/intel/pod5/deployment.yaml ./deployment.yaml
10         ;;
11     'intelpod6' )
12         cp maas/intel/pod6/deployment.yaml ./deployment.yaml
13         ;;
14     'orangepod2' )
15         cp maas/orange/pod2/deployment.yaml ./deployment.yaml
16         ;;
17     'attvirpod1' )
18         cp maas/att/virpod1/deployment.yaml ./deployment.yaml
19         ;;
20     'juniperpod1' )
21         cp maas/juniper/pod1/deployment.yaml ./deployment.yaml
22         ;;
23     * )
24         virtinstall=1
25         cp maas/default/deployment.yaml ./deployment.yaml
26         ;;
27 esac
28
29 #just make sure the ssh keys added into maas for the current user
30 sed --i "s@/home/ubuntu@$HOME@g" ./deployment.yaml
31 sed --i "s@qemu+ssh://ubuntu@qemu+ssh://$USER@g" ./deployment.yaml
32
33 #make sure no password asked during the deployment. 
34
35 echo "$USER ALL=(ALL) NOPASSWD:ALL" > 90-joid-init
36
37 if [ -e /etc/sudoers.d/90-joid-init ]; then
38     sudo cp /etc/sudoers.d/90-joid-init 91-joid-init
39     sudo chown $USER:$USER 91-joid-init
40     sudo cat 90-joid-init >> 91-joid-init
41     sudo chown root:root 91-joid-init
42     sudo mv 91-joid-init /etc/sudoers.d/
43 else
44     sudo chown root:root 90-joid-init
45     sudo mv 90-joid-init /etc/sudoers.d/
46 fi
47
48 echo "... Deployment of maas Started ...."
49
50 if [ ! -e $HOME/.ssh/id_rsa ]; then
51     ssh-keygen -N '' -f $HOME/.ssh/id_rsa
52 fi
53
54 if [ ! -e /var/lib/libvirt/images ]; then
55
56     sudo apt-get install libvirt-bin -y
57     sudo adduser $USER libvirtd
58     sudo virsh pool-define-as default --type dir --target /var/lib/libvirt/images/
59     sudo virsh pool-start default
60     sudo virsh pool-autostart default
61
62 fi
63
64 sudo apt-add-repository ppa:maas-deployers/stable -y
65 sudo apt-add-repository ppa:juju/stable -y
66 sudo apt-add-repository ppa:maas/stable -y
67 sudo apt-get update -y
68 sudo apt-get install openssh-server git maas-deployer juju juju-deployer maas-cli python-pip -y
69 sudo pip install shyaml
70 juju init -f
71
72 cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys
73
74 if [ "$virtinstall" -eq 1 ]; then
75     sudo virsh net-dumpxml default > default-net-org.xml
76     sudo sed -i '/dhcp/d' default-net-org.xml
77     sudo sed -i '/range/d' default-net-org.xml
78     sudo virsh net-define default-net-org.xml
79     sudo virsh net-destroy default
80     sudo virsh net-start default
81 fi
82
83 #Below function will mark the interfaces in Auto mode to enbled by MAAS
84 enableautomode() {
85     listofnodes=`maas maas nodes list | grep system_id | cut -d '"' -f 4`
86     for nodes in $listofnodes
87     do
88         maas maas interface link-subnet $nodes $1  mode=$2 subnet=$3
89     done
90 }
91
92 #Below function will mark the interfaces in Auto mode to enbled by MAAS
93 # using hostname of the node added into MAAS
94
95 enableautomodebyname() {
96     if [ ! -z "$4" ]; then
97         for i in `seq 1 7`;
98         do
99             nodes=`maas maas nodes list hostname=node$i-$4 | grep system_id | cut -d '"' -f 4`
100             if [ ! -z "$nodes" ]; then
101                 maas maas interface link-subnet $nodes $1  mode=$2 subnet=$3
102             fi
103        done
104     fi
105 }
106
107 #Below function will create vlan and update interface with the new vlan
108 # will return the vlan id created
109 crvlanupdsubnet() {
110     newvlanid=`maas maas vlans create $2 name=$3 vid=$4 | grep resource | cut -d '/' -f 6 `
111     maas maas subnet update $5 vlan=$newvlanid
112     eval "$1"="'$newvlanid'"
113 }
114
115 #Below function will create interface with new vlan and bind to physical interface
116 crnodevlanint() {
117     listofnodes=`maas maas nodes list | grep system_id | cut -d '"' -f 4`
118
119     for nodes in $listofnodes
120     do
121         parentid=`maas maas interface read $nodes eth2 | grep interfaces | cut -d '/' -f 8`
122         maas maas interfaces create-vlan $nodes vlan=$1 parent=$parentid
123      done
124  }
125
126 sudo maas-deployer -c deployment.yaml -d --force
127
128 sudo chown $USER:$USER environments.yaml
129
130 echo "... Deployment of maas finish ...."
131
132 maas_ip=`grep " ip_address" deployment.yaml | cut -d " "  -f 10`
133 apikey=`grep maas-oauth: environments.yaml | cut -d "'" -f 2`
134 maas login maas http://${maas_ip}/MAAS/api/1.0 ${apikey}
135 maas maas boot-source update 1 url="http://maas.ubuntu.com/images/ephemeral-v2/daily/"
136 #maas maas boot-source-selections create 1 os="ubuntu" release="precise" arches="amd64" subarches="*" labels="*"
137 maas maas boot-resources import
138 maas maas sshkeys new key="`cat $HOME/.ssh/id_rsa.pub`"
139
140 #Added the Qtip public to run the Qtip test after install on bare metal nodes.
141 maas maas sshkeys new key="`cat ./maas/sshkeys/QtipKey.pub`"
142
143 #adding compute and control nodes VM to MAAS for deployment purpose.
144 if [ "$virtinstall" -eq 1 ]; then
145     # create two more VMs to do the deployment.
146     sudo virt-install --connect qemu:///system --name node1-control --ram 8192 --vcpus 4 --disk size=120,format=qcow2,bus=virtio,io=native,pool=default --network bridge=virbr0,model=virtio --network bridge=virbr0,model=virtio --boot network,hd,menu=off --noautoconsole --vnc --print-xml | tee node1-control
147
148     sudo virt-install --connect qemu:///system --name node2-compute --ram 8192 --vcpus 4 --disk size=120,format=qcow2,bus=virtio,io=native,pool=default --network bridge=virbr0,model=virtio --network bridge=virbr0,model=virtio --boot network,hd,menu=off --noautoconsole --vnc --print-xml | tee node2-compute
149
150     node1controlmac=`grep  "mac address" node1-control | head -1 | cut -d "'" -f 2`
151     node2computemac=`grep  "mac address" node2-compute | head -1 | cut -d "'" -f 2`
152
153     sudo virsh -c qemu:///system define --file node1-control
154     sudo virsh -c qemu:///system define --file node2-compute
155
156     maas maas tags new name='control'
157     maas maas tags new name='compute'
158
159     controlnodeid=`maas maas nodes new autodetect_nodegroup='yes' name='node1-control' tags='control' hostname='node1-control' power_type='virsh' mac_addresses=$node1controlmac power_parameters_power_address='qemu+ssh://'$USER'@192.168.122.1/system' architecture='amd64/generic' power_parameters_power_id='node1-control' | grep system_id | cut -d '"' -f 4 `
160
161     maas maas tag update-nodes control add=$controlnodeid
162
163     computenodeid=`maas maas nodes new autodetect_nodegroup='yes' name='node2-compute' tags='compute' hostname='node2-compute' power_type='virsh' mac_addresses=$node2computemac power_parameters_power_address='qemu+ssh://'$USER'@192.168.122.1/system' architecture='amd64/generic' power_parameters_power_id='node2-compute' | grep system_id | cut -d '"' -f 4 `
164
165     maas maas tag update-nodes compute add=$computenodeid
166
167 fi
168
169 # Enable vlan interfaces with maas
170 case "$1" in
171     'intelpod5' )
172         maas refresh
173         crvlanupdsubnet vlan721 1 "DataNetwork" 721 2 || true
174         crvlanupdsubnet vlan724 2 "PublicNetwork" 724 3 || true
175         crnodevlanint $vlan721 || true
176         crnodevlanint $vlan724 || true
177         enableautomodebyname eth2.721 AUTO "10.4.9.0/24" compute || true
178         enableautomodebyname eth2.721 AUTO "10.4.9.0/24" control || true
179         ;;
180     'intelpod6' )
181         maas refresh
182         enableautomodebyname eth1 AUTO "10.4.9.0/24" compute || true
183         enableautomodebyname eth1 AUTO "10.4.9.0/24" control || true
184         ;;
185     'orangepod2' )
186         maas refresh
187         enableautomodebyname eth4 AUTO "192.168.22.0/24" compute || true
188         enableautomodebyname eth5 AUTO "192.168.12.0/24" compute || true
189         enableautomodebyname eth2 AUTO "192.168.22.0/24" control || true
190         enableautomodebyname eth3 AUTO "192.168.12.0/24" control || true
191         ;;
192     'attvirpod1' )
193         ;;
194     'juniperpod1' )
195         ;;
196 esac
197
198 echo " .... MAAS deployment finished successfully ...."
199
200 #echo "... Deployment of opnfv release Started ...."
201 #python deploy.py $maas_ip
202