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