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