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