states: Break on error, retry states up to 5 times
[fuel.git] / mcp / scripts / create-config-drive.sh
1 #!/bin/bash -e
2 ##############################################################################
3 # Copyright (c) 2017 Mirantis Inc. and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 # This will generate a openstack-style config drive image suitable for
11 # use with cloud-init.  You may optionally pass in an ssh public key
12 # (using the -k/--ssh-key option) and a user-data blog (using the
13 # -u/--user-data option).
14
15 CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
16
17 usage () {
18         echo "usage: ${0##*/}: [--ssh-key <pubkey>] [--vendor-data <file>] [--user-data <file>] [--hostname <hostname>] <imagename>"
19 }
20
21 ARGS=$(getopt \
22         -o k:u:v:h: \
23         --long help,hostname:,ssh-key:,user-data:,vendor-data: -n "${0##*/}" \
24         -- "$@")
25
26 # shellcheck disable=SC2181
27 if [ $? -ne 0 ]; then
28         usage >&2
29         exit 2
30 fi
31
32 eval set -- "$ARGS"
33
34 while :; do
35         case "$1" in
36                 --help)
37                         usage
38                         exit 0
39                         ;;
40                 -k|--ssh-key)
41                         ssh_key="$2"
42                         shift 2
43                         ;;
44                 -u|--user-data)
45                         user_data="$2"
46                         shift 2
47                         ;;
48                 -v|--vendor-data)
49                         vendor_data="$2"
50                         shift 2
51                         ;;
52                 -h|--hostname)
53                         hostname="$2"
54                         shift 2
55                         ;;
56                 --)     shift
57                         break
58                         ;;
59         esac
60 done
61
62 config_image=$1
63 shift
64
65 if [ "${ssh_key}" ] && [ -f "${ssh_key}" ]; then
66         echo "adding pubkey from ${ssh_key}"
67         ssh_key_data=$(cat "${ssh_key}")
68 fi
69
70 uuid=$(uuidgen)
71 if ! [ "${hostname}" ]; then
72         hostname="${uuid}"
73 fi
74
75 trap 'rm -rf $config_dir' EXIT
76 config_dir=$(mktemp -t -d configXXXXXX)
77
78 if [ "${user_data}" ] && [ -f "${user_data}" ]; then
79         echo "adding user data from ${user_data}"
80         cp "${user_data}" "${config_dir}/user-data"
81 else
82         touch "${config_dir}/user-data"
83 fi
84
85 if [ "${vendor_data}" ] && [ -f "${vendor_data}" ]; then
86         echo "adding vendor data from ${vendor_data}"
87         cp "${vendor_data}" "${config_dir}/vendor-data"
88 fi
89
90 cat > "${config_dir}/meta-data" <<-EOF
91 instance-id: ${uuid}
92 hostname: ${hostname}
93 local-hostname: ${hostname}
94 EOF
95
96 if [ "${ssh_key_data}" ]; then
97         cat >> "${config_dir}/meta-data" <<-EOF
98         public-keys:
99           - |
100             ${ssh_key_data}
101         EOF
102 fi
103
104 #PS1="debug> " bash --norc
105
106 echo "generating configuration image at ${config_image}"
107 if ! mkisofs -o "${config_image}" -V cidata -r -J --quiet "${config_dir}"; then
108         echo "ERROR: failed to create ${config_image}" >&2
109         exit 1
110 fi
111 chmod a+r "${config_image}"
112