Merge "Break early in build if docker-in-docker fails" into stable/brahmaputra
[fuel.git] / 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 # Enable sshd
44 systemctl enable sshd
45 systemctl start sshd
46
47 if [[ "$showmenu" == "yes" || "$showmenu" == "YES" ]]; then
48   fuelmenu
49   else
50   #Give user 15 seconds to enter fuelmenu or else continue
51   echo
52   echo -n "Press a key to enter Fuel Setup (or press ESC to skip)...   15"
53   countdown 15 & pid=$!
54   if ! read -s -n 1 -t 15 key; then
55     echo -e "\nSkipping Fuel Setup..."
56   else
57     { kill "$pid"; wait $!; } 2>/dev/null
58     case "$key" in
59       $'\e')  echo "Skipping Fuel Setup.."
60               ;;
61       *)      echo -e "\nEntering Fuel Setup..."
62               fuelmenu
63               ;;
64     esac
65   fi
66 fi
67
68 systemctl reload sshd
69
70 # Enable iptables
71 systemctl enable iptables.service
72 systemctl start iptables.service
73
74
75 if [ "$wait_for_external_config" == "yes" ]; then
76   wait_timeout=3000
77   pidfile=/var/lock/wait_for_external_config
78   echo -n "Waiting for external configuration (or press ESC to skip)...
79 $wait_timeout"
80   countdown $wait_timeout & countdown_pid=$!
81   exec -a wait_for_external_config sleep $wait_timeout & wait_pid=$!
82   echo $wait_pid > $pidfile
83   while ps -p $countdown_pid &> /dev/null && ps -p $wait_pid &>/dev/null; do
84     read -s -n 1 -t 2 key
85     case "$key" in
86       $'\e')   echo -e "\b\b\b\b abort on user input"
87                break
88                ;;
89       *)       ;;
90     esac
91   done
92   { kill $countdown_pid $wait_pid & wait $!; }
93   rm -f $pidfile
94 fi
95
96
97 #Reread /etc/sysconfig/network to inform puppet of changes
98 . /etc/sysconfig/network
99 hostname "$HOSTNAME"
100
101 # XXX: ssh keys which should be included into the bootstrap image are
102 # generated during containers deployment. However cobbler checkfs for
103 # a kernel and initramfs when creating a profile, which poses chicken
104 # and egg problem. Fortunately cobbler is pretty happy with empty files
105 # so it's easy to break the loop.
106 make_ubuntu_bootstrap_stub () {
107         local bootstrap_dir='/var/www/nailgun/bootstrap/ubuntu'
108         mkdir -p $bootstrap_dir
109         for item in linux initramfs.img; do
110                 touch "$bootstrap_dir/$item"
111         done
112 }
113
114 get_bootstrap_flavor () {
115         local ASTUTE_YAML='/etc/fuel/astute.yaml'
116         python <<-EOF
117         from fuelmenu.fuelmenu import Settings
118         conf = Settings().read("$ASTUTE_YAML").get('BOOTSTRAP', {})
119         print(conf.get('flavor', 'centos'))
120         EOF
121 }
122
123 # Actually build the bootstrap image
124 build_ubuntu_bootstrap () {
125         local ret=1
126         echo ${bs_progress_message} >&2
127         set_ui_bootstrap_error "${bs_progress_message}" >&2
128 # OPNFV modification to turn off biosdevname on the line below (extend-kopts)
129         if fuel-bootstrap -v --debug build --activate \
130         --extend-kopts "biosdevname=0 net.ifnames=0 debug ignore_loglevel log_buf_len=10M print_fatal_signals=1 LOGLEVEL=8" \
131         >>"$bs_build_log" 2>&1; then
132           ret=0
133           fuel notify --topic "done" --send "${bs_done_message}"
134         else
135           ret=1
136           set_ui_bootstrap_error "${bs_error_message}" >&2
137         fi
138         # perform hard-return from func
139         # this part will update input $1 variable
140         local  __resultvar=$1
141         eval $__resultvar="'${ret}'"
142         return $ret
143 }
144
145
146 # Create empty files to make cobbler happy
147 # (even if we don't use Ubuntu based bootstrap)
148 make_ubuntu_bootstrap_stub
149
150 service docker start
151
152 if [ -f /root/.build_images ]; then
153   #Fail on all errors
154   set -e
155   trap fail EXIT
156
157   echo "Loading Fuel base image for Docker..."
158   docker load -i /var/www/nailgun/docker/images/fuel-images.tar
159
160   echo "Building Fuel Docker images..."
161   WORKDIR=$(mktemp -d /tmp/docker-buildXXX)
162   SOURCE=/var/www/nailgun/docker
163   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')
164   RANDOM_PORT=$(docker port $REPO_CONT_ID 80 | cut -d':' -f2)
165
166   for imagesource in /var/www/nailgun/docker/sources/*; do
167     if ! [ -f "$imagesource/Dockerfile" ]; then
168       echo "Skipping ${imagesource}..."
169       continue
170     fi
171     image=$(basename "$imagesource")
172     cp -R "$imagesource" $WORKDIR/$image
173     mkdir -p $WORKDIR/$image/etc
174     cp -R /etc/puppet /etc/fuel $WORKDIR/$image/etc
175     sed -e "s/_PORT_/${RANDOM_PORT}/" -i $WORKDIR/$image/Dockerfile
176     sed -e 's/production:.*/production: "docker-build"/' -i $WORKDIR/$image/etc/fuel/version.yaml
177     docker build -t fuel/${image}_${FUEL_RELEASE} $WORKDIR/$image
178   done
179   docker rm -f $REPO_CONT_ID
180   rm -rf "$WORKDIR"
181
182   #Remove trap for normal deployment
183   trap - EXIT
184   set +e
185 else
186   echo "Loading docker images. (This may take a while)"
187   docker load -i /var/www/nailgun/docker/images/fuel-images.tar
188 fi
189
190 # apply puppet
191 puppet apply --detailed-exitcodes -d -v /etc/puppet/modules/nailgun/examples/host-only.pp
192 if [ $? -ge 4 ];then
193   fail
194 fi
195
196 rmdir /var/log/remote && ln -s /var/log/docker-logs/remote /var/log/remote
197
198 dockerctl check || fail
199 bash /etc/rc.local
200
201 if [ "`get_bootstrap_flavor`" = "ubuntu" ]; then
202         build_ubuntu_bootstrap || true
203 fi
204
205 ### OPNFV addition BEGIN
206 shopt -s nullglob
207 for script in /opt/opnfv/bootstrap/post.d/*.sh
208 do
209   echo "Post script: $script" >> /root/post.log 2>&1
210   $script >> /root/post.log 2>&1
211 done
212 shopt -u nullglob
213 ### OPNFV addition END
214
215 # Enable updates repository
216 cat > /etc/yum.repos.d/mos${FUEL_RELEASE}-updates.repo << EOF
217 [mos${FUEL_RELEASE}-updates]
218 name=mos${FUEL_RELEASE}-updates
219 baseurl=http://mirror.fuel-infra.org/mos-repos/centos/mos${FUEL_RELEASE}-centos6-fuel/updates/x86_64/
220 gpgcheck=0
221 skip_if_unavailable=1
222 EOF
223
224 # Enable security repository
225 cat > /etc/yum.repos.d/mos${FUEL_RELEASE}-security.repo << EOF
226 [mos${FUEL_RELEASE}-security]
227 name=mos${FUEL_RELEASE}-security
228 baseurl=http://mirror.fuel-infra.org/mos-repos/centos/mos${FUEL_RELEASE}-centos6-fuel/security/x86_64/
229 gpgcheck=0
230 skip_if_unavailable=1
231 EOF
232
233 #Check if repo is accessible
234 echo "Checking for access to updates repository..."
235 repourl=$(grep baseurl /etc/yum.repos.d/*updates* 2>/dev/null | cut -d'=' -f2- | head -1)
236 if urlaccesscheck check "$repourl" ; then
237   UPDATE_ISSUES=0
238 else
239   UPDATE_ISSUES=1
240 fi
241
242 if [ $UPDATE_ISSUES -eq 1 ]; then
243   message="There is an issue connecting to the Fuel update repository. \
244 Please fix your connection prior to applying any updates. \
245 Once the connection is fixed, we recommend reviewing and applying \
246 Maintenance Updates for this release of Mirantis OpenStack: \
247 https://docs.mirantis.com/openstack/fuel/fuel-${FUEL_RELEASE}/\
248 release-notes.html#maintenance-updates"
249   level="warning"
250 else
251   message="We recommend reviewing and applying Maintenance Updates \
252 for this release of Mirantis OpenStack: \
253 https://docs.mirantis.com/openstack/fuel/fuel-${FUEL_RELEASE}/\
254 release-notes.html#maintenance-updates"
255   level="done"
256 fi
257 echo
258 echo "*************************************************"
259 echo -e "${message}"
260 echo "*************************************************"
261 echo "Sending notification to Fuel UI..."
262 fuel notify --topic "${level}" --send "${message}"
263
264 # TODO(kozhukalov) If building of bootstrap image fails
265 # and if this image was supposed to be a default bootstrap image
266 # we need to warn a user about this and give her
267 # advice how to treat this.
268
269 echo "Fuel node deployment complete!"