Merge "Templates for configuration guide" 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 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         echo ${bs_progress_message} >&2
116         set_ui_bootstrap_error "${bs_progress_message}" >&2
117 # OPNFV modification to turn off biosdevname on the line below (extend-kopts)
118         if fuel-bootstrap -v --debug build --activate \
119         --extend-kopts "biosdevname=0 net.ifnames=0 debug ignore_loglevel log_buf_len=10M print_fatal_signals=1 LOGLEVEL=8" \
120         >>"$bs_build_log" 2>&1; then
121           ret=0
122           fuel notify --topic "done" --send "${bs_done_message}"
123         else
124           ret=1
125           set_ui_bootstrap_error "${bs_error_message}" >&2
126         fi
127         # perform hard-return from func
128         # this part will update input $1 variable
129         local  __resultvar=$1
130         eval $__resultvar="'${ret}'"
131         return $ret
132 }
133
134
135 # Create empty files to make cobbler happy
136 # (even if we don't use Ubuntu based bootstrap)
137 make_ubuntu_bootstrap_stub
138
139 service docker start
140
141 if [ -f /root/.build_images ]; then
142   #Fail on all errors
143   set -e
144   trap fail EXIT
145
146   echo "Loading Fuel base image for Docker..."
147   docker load -i /var/www/nailgun/docker/images/fuel-images.tar
148
149   echo "Building Fuel Docker images..."
150   WORKDIR=$(mktemp -d /tmp/docker-buildXXX)
151   SOURCE=/var/www/nailgun/docker
152   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')
153   RANDOM_PORT=$(docker port $REPO_CONT_ID 80 | cut -d':' -f2)
154
155   for imagesource in /var/www/nailgun/docker/sources/*; do
156     if ! [ -f "$imagesource/Dockerfile" ]; then
157       echo "Skipping ${imagesource}..."
158       continue
159     fi
160     image=$(basename "$imagesource")
161     cp -R "$imagesource" $WORKDIR/$image
162     mkdir -p $WORKDIR/$image/etc
163     cp -R /etc/puppet /etc/fuel $WORKDIR/$image/etc
164     sed -e "s/_PORT_/${RANDOM_PORT}/" -i $WORKDIR/$image/Dockerfile
165     sed -e 's/production:.*/production: "docker-build"/' -i $WORKDIR/$image/etc/fuel/version.yaml
166     docker build -t fuel/${image}_${FUEL_RELEASE} $WORKDIR/$image
167   done
168   docker rm -f $REPO_CONT_ID
169   rm -rf "$WORKDIR"
170
171   #Remove trap for normal deployment
172   trap - EXIT
173   set +e
174 else
175   echo "Loading docker images. (This may take a while)"
176   docker load -i /var/www/nailgun/docker/images/fuel-images.tar
177 fi
178
179 # apply puppet
180 puppet apply --detailed-exitcodes -d -v /etc/puppet/modules/nailgun/examples/host-only.pp
181 if [ $? -ge 4 ];then
182   fail
183 fi
184
185 rmdir /var/log/remote && ln -s /var/log/docker-logs/remote /var/log/remote
186
187 dockerctl check || fail
188 bash /etc/rc.local
189
190 if [ "`get_bootstrap_flavor`" = "ubuntu" ]; then
191         build_ubuntu_bootstrap || true
192 fi
193
194 ### OPNFV addition BEGIN
195 shopt -s nullglob
196 for script in /opt/opnfv/bootstrap/post.d/*.sh
197 do
198   echo "Post script: $script" >> /root/post.log 2>&1
199   $script >> /root/post.log 2>&1
200 done
201 shopt -u nullglob
202 ### OPNFV addition END
203
204 # Enable updates repository
205 cat > /etc/yum.repos.d/mos${FUEL_RELEASE}-updates.repo << EOF
206 [mos${FUEL_RELEASE}-updates]
207 name=mos${FUEL_RELEASE}-updates
208 baseurl=http://mirror.fuel-infra.org/mos-repos/centos/mos${FUEL_RELEASE}-centos6-fuel/updates/x86_64/
209 gpgcheck=0
210 skip_if_unavailable=1
211 EOF
212
213 # Enable security repository
214 cat > /etc/yum.repos.d/mos${FUEL_RELEASE}-security.repo << EOF
215 [mos${FUEL_RELEASE}-security]
216 name=mos${FUEL_RELEASE}-security
217 baseurl=http://mirror.fuel-infra.org/mos-repos/centos/mos${FUEL_RELEASE}-centos6-fuel/security/x86_64/
218 gpgcheck=0
219 skip_if_unavailable=1
220 EOF
221
222 #Check if repo is accessible
223 echo "Checking for access to updates repository..."
224 repourl=$(grep baseurl /etc/yum.repos.d/*updates* 2>/dev/null | cut -d'=' -f2- | head -1)
225 if urlaccesscheck check "$repourl" ; then
226   UPDATE_ISSUES=0
227 else
228   UPDATE_ISSUES=1
229 fi
230
231 if [ $UPDATE_ISSUES -eq 1 ]; then
232   message="There is an issue connecting to the Fuel update repository. \
233 Please fix your connection prior to applying any updates. \
234 Once the connection is fixed, we recommend reviewing and applying \
235 Maintenance Updates for this release of Mirantis OpenStack: \
236 https://docs.mirantis.com/openstack/fuel/fuel-${FUEL_RELEASE}/\
237 release-notes.html#maintenance-updates"
238   level="warning"
239 else
240   message="We recommend reviewing and applying Maintenance Updates \
241 for this release of Mirantis OpenStack: \
242 https://docs.mirantis.com/openstack/fuel/fuel-${FUEL_RELEASE}/\
243 release-notes.html#maintenance-updates"
244   level="done"
245 fi
246 echo
247 echo "*************************************************"
248 echo -e "${message}"
249 echo "*************************************************"
250 echo "Sending notification to Fuel UI..."
251 fuel notify --topic "${level}" --send "${message}"
252
253 # TODO(kozhukalov) If building of bootstrap image fails
254 # and if this image was supposed to be a default bootstrap image
255 # we need to warn a user about this and give her
256 # advice how to treat this.
257
258 echo "Fuel node deployment complete!"