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