Autodeployment adaptation to Fuel 7
[fuel.git] / fuel / build / f_isoroot / f_bootstrap / bootstrap_admin_node.sh
1 #!/bin/bash
2 FUEL_RELEASE=$(grep release: /etc/fuel/version.yaml | cut -d: -f2 | tr -d '" ')
3
4 function countdown() {
5   local i
6   sleep 1
7   for ((i=$1-1; i>=1; i--)); do
8     printf '\b\b\b\b%04d' "$i"
9     sleep 1
10   done
11 }
12
13 function fail() {
14   echo "ERROR: Fuel node deployment FAILED! Check /var/log/puppet/bootstrap_admin_node.log for details" 1>&2
15   exit 1
16 }
17 # LANG variable is a workaround for puppet-3.4.2 bug. See LP#1312758 for details
18 export LANG=en_US.UTF8
19 export ADMIN_INTERFACE=eth0
20
21 showmenu="no"
22 if [ -f /etc/fuel/bootstrap_admin_node.conf ]; then
23   . /etc/fuel/bootstrap_admin_node.conf
24   echo "Applying admin interface '$ADMIN_INTERFACE'"
25 fi
26
27 echo "Applying default Fuel settings..."
28 set -x
29 fuelmenu --save-only --iface=$ADMIN_INTERFACE
30 set +x
31 echo "Done!"
32
33 ### OPNFV addition BEGIN
34 shopt -s nullglob
35 for script in /opt/opnfv/bootstrap/pre.d/*.sh
36 do
37   echo "Pre script: $script" >> /root/pre.log 2>&1
38   $script >> /root/pre.log 2>&1
39 done
40 shopt -u nullglob
41 ### OPNFV addition END
42
43 if [[ "$showmenu" == "yes" || "$showmenu" == "YES" ]]; then
44   fuelmenu
45   else
46   #Give user 15 seconds to enter fuelmenu or else continue
47   echo
48   echo -n "Press a key to enter Fuel Setup (or press ESC to skip)...   15"
49   countdown 15 & pid=$!
50   if ! read -s -n 1 -t 15 key; then
51     echo -e "\nSkipping Fuel Setup..."
52   else
53     { kill "$pid"; wait $!; } 2>/dev/null
54     case "$key" in
55       $'\e')  echo "Skipping Fuel Setup.."
56               ;;
57       *)      echo -e "\nEntering Fuel Setup..."
58               fuelmenu
59               ;;
60     esac
61   fi
62 fi
63
64 if [ "$wait_for_external_config" == "yes" ]; then
65   wait_timeout=3000
66   pidfile=/var/lock/wait_for_external_config
67   echo -n "Waiting for external configuration (or press ESC to skip)...
68 $wait_timeout"
69   countdown $wait_timeout & countdown_pid=$!
70   exec -a wait_for_external_config sleep $wait_timeout & wait_pid=$!
71   echo $wait_pid > $pidfile
72   while ps -p $countdown_pid &> /dev/null && ps -p $wait_pid &>/dev/null; do
73     read -s -n 1 -t 2 key
74     case "$key" in
75       $'\e')   echo -e "\b\b\b\b abort on user input"
76                break
77                ;;
78       *)       ;;
79     esac
80   done
81   { kill $countdown_pid $wait_pid & wait $!; }
82   rm -f $pidfile
83 fi
84
85
86 #Reread /etc/sysconfig/network to inform puppet of changes
87 . /etc/sysconfig/network
88 hostname "$HOSTNAME"
89
90 # XXX: ssh keys which should be included into the bootstrap image are
91 # generated during containers deployment. However cobbler checkfs for
92 # a kernel and initramfs when creating a profile, which poses chicken
93 # and egg problem. Fortunately cobbler is pretty happy with empty files
94 # so it's easy to break the loop.
95 make_ubuntu_bootstrap_stub () {
96         local bootstrap_dir='/var/www/nailgun/bootstrap/ubuntu'
97         mkdir -p $bootstrap_dir
98         for item in linux initramfs.img; do
99                 touch "$bootstrap_dir/$item"
100         done
101 }
102
103 get_bootstrap_flavor () {
104         local ASTUTE_YAML='/etc/fuel/astute.yaml'
105         python <<-EOF
106         from fuelmenu.fuelmenu import Settings
107         conf = Settings().read("$ASTUTE_YAML").get('BOOTSTRAP', {})
108         print(conf.get('flavor', 'centos'))
109         EOF
110 }
111
112 # Actually build the bootstrap image
113 build_ubuntu_bootstrap () {
114         local ret=1
115         local max_attempts=3
116         local config='/etc/fuel-bootstrap-image.conf'
117         local log='/var/log/fuel-bootstrap-image-build.log'
118         if ! grep -qE '^BOOTSTRAP_SSH_KEYS' "$config"; then
119                 # FIXME: config file generated by fuelmenu has no trailing newline
120                 echo >> "$config"
121                 cat >> "$config" <<-EOF
122                 BOOTSTRAP_SSH_KEYS=/root/.ssh/id_rsa.pub
123                 EOF
124         fi
125         for n in `seq 1 $max_attempts`; do
126                 echo "Bulding bootstrap image, attempt $n" >&2
127                 if fuel-bootstrap-image >>"$log" 2>&1; then
128                         ret=0
129                         fuel-bootstrap-image-set "ubuntu"
130                         break
131                 fi
132         done
133         if [ $ret -ne 0 ]; then
134                 warning="WARNING: failed to build the bootstrap image, see $log for details.
135 Perhaps your Internet connection is broken. Please fix the problem and run
136 \`fuel-bootstrap-image-set ubuntu\`"
137                 fuel notify --topic warning --send "$warning"
138         fi
139         return $ret
140 }
141
142
143 # Create empty files to make cobbler happy
144 # (even if we don't use Ubuntu based bootstrap)
145 make_ubuntu_bootstrap_stub
146
147 service docker start
148
149 if [ -f /root/.build_images ]; then
150   #Fail on all errors
151   set -e
152   trap fail EXIT
153
154   echo "Loading Fuel base image for Docker..."
155   docker load -i /var/www/nailgun/docker/images/fuel-images.tar
156
157   echo "Building Fuel Docker images..."
158   WORKDIR=$(mktemp -d /tmp/docker-buildXXX)
159   SOURCE=/var/www/nailgun/docker
160   REPO_CONT_ID=$(docker -D run -d -p 80 -v /var/www/nailgun:/var/www/nailgun fuel/centos sh -c 'mkdir /var/www/html/os;ln -sf /var/www/nailgun/centos/x86_64 /var/www/html/os/x86_64;/usr/sbin/apachectl -DFOREGROUND')
161   RANDOM_PORT=$(docker port $REPO_CONT_ID 80 | cut -d':' -f2)
162
163   for imagesource in /var/www/nailgun/docker/sources/*; do
164     if ! [ -f "$imagesource/Dockerfile" ]; then
165       echo "Skipping ${imagesource}..."
166       continue
167     fi
168     image=$(basename "$imagesource")
169     cp -R "$imagesource" $WORKDIR/$image
170     mkdir -p $WORKDIR/$image/etc
171     cp -R /etc/puppet /etc/fuel $WORKDIR/$image/etc
172     sed -e "s/_PORT_/${RANDOM_PORT}/" -i $WORKDIR/$image/Dockerfile
173     sed -e 's/production:.*/production: "docker-build"/' -i $WORKDIR/$image/etc/fuel/version.yaml
174     docker build -t fuel/${image}_${FUEL_RELEASE} $WORKDIR/$image
175   done
176   docker rm -f $REPO_CONT_ID
177   rm -rf "$WORKDIR"
178
179   #Remove trap for normal deployment
180   trap - EXIT
181   set +e
182 else
183   echo "Loading docker images. (This may take a while)"
184   docker load -i /var/www/nailgun/docker/images/fuel-images.tar
185 fi
186
187 # apply puppet
188 puppet apply --detailed-exitcodes -d -v /etc/puppet/modules/nailgun/examples/host-only.pp
189 if [ $? -ge 4 ];then
190   fail
191 fi
192
193 rmdir /var/log/remote && ln -s /var/log/docker-logs/remote /var/log/remote
194
195 dockerctl check || fail
196 bash /etc/rc.local
197
198 if [ "`get_bootstrap_flavor`" = "ubuntu" ]; then
199         build_ubuntu_bootstrap || true
200 fi
201
202 ### OPNFV addition BEGIN
203 shopt -s nullglob
204 for script in /opt/opnfv/bootstrap/post.d/*.sh
205 do
206   echo "Post script: $script" >> /root/post.log 2>&1
207   $script >> /root/post.log 2>&1
208 done
209 shopt -u nullglob
210 ### OPNFV addition END
211
212 # Enable updates repository
213 cat > /etc/yum.repos.d/mos${FUEL_RELEASE}-updates.repo << EOF
214 [mos${FUEL_RELEASE}-updates]
215 name=mos${FUEL_RELEASE}-updates
216 baseurl=http://mirror.fuel-infra.org/mos-repos/centos/mos${FUEL_RELEASE}-centos6-fuel/updates/x86_64/
217 gpgcheck=0
218 skip_if_unavailable=1
219 EOF
220
221 # Enable security repository
222 cat > /etc/yum.repos.d/mos${FUEL_RELEASE}-security.repo << EOF
223 [mos${FUEL_RELEASE}-security]
224 name=mos${FUEL_RELEASE}-security
225 baseurl=http://mirror.fuel-infra.org/mos-repos/centos/mos${FUEL_RELEASE}-centos6-fuel/security/x86_64/
226 gpgcheck=0
227 skip_if_unavailable=1
228 EOF
229
230 #Check if repo is accessible
231 echo "Checking for access to updates repository..."
232 repourl=$(grep baseurl /etc/yum.repos.d/*updates* 2>/dev/null | cut -d'=' -f2- | head -1)
233 if urlaccesscheck check "$repourl" ; then
234   UPDATE_ISSUES=0
235 else
236   UPDATE_ISSUES=1
237 fi
238
239 if [ $UPDATE_ISSUES -eq 1 ]; then
240   message="There is an issue connecting to the Fuel update repository. \
241 Please fix your connection prior to applying any updates. \
242 Once the connection is fixed, we recommend reviewing and applying \
243 Maintenance Updates for this release of Mirantis OpenStack: \
244 https://docs.mirantis.com/openstack/fuel/fuel-${FUEL_RELEASE}/\
245 release-notes.html#maintenance-updates"
246   level="warning"
247 else
248   message="We recommend reviewing and applying Maintenance Updates \
249 for this release of Mirantis OpenStack: \
250 https://docs.mirantis.com/openstack/fuel/fuel-${FUEL_RELEASE}/\
251 release-notes.html#maintenance-updates"
252   level="done"
253 fi
254 echo
255 echo "*************************************************"
256 echo -e "${message}"
257 echo "*************************************************"
258 echo "Sending notification to Fuel UI..."
259 fuel notify --topic "${level}" --send "${message}"
260
261 # TODO(kozhukalov) If building of bootstrap image fails
262 # and if this image was supposed to be a default bootstrap image
263 # we need to warn a user about this and give her
264 # advice how to treat this.
265
266 echo "Fuel node deployment complete!"