blob: fae1a81da62e7e8527db849f7ba355a9d71b5514 [file] [log] [blame]
Richard Felkl4c4829d2017-11-11 00:12:20 +01001#!/bin/sh
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
8usage () {
Dmitry Pyzhov15c0ac42018-12-11 17:10:08 +03009 echo "usage: ${0##*/}: [--ssh-key <pubkey>] [--gpg-key <file>] [--vendor-data <file>] [--user-data <file>] [--hostname <hostname>] [--model <model>] [--mk-pipelines <mk-pipelines>] [--pipeline-library <pipeline-library>] <imagename>"
Richard Felkl4c4829d2017-11-11 00:12:20 +010010}
11
12ARGS=$(getopt \
Dmitry Pyzhov15c0ac42018-12-11 17:10:08 +030013 -o k:g:u:v:h:m:mp:p \
14 --long help,hostname:,ssh-key:,gpg-key:,user-data:,vendor-data:,model:,mk-pipelines:,pipeline-library: -n ${0##*/} \
Richard Felkl4c4829d2017-11-11 00:12:20 +010015 -- "$@")
16
17if [ $? -ne 0 ]; then
18 usage >&2
19 exit 2
20fi
21
22eval set -- "$ARGS"
23
24while :; 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 ;;
Dmitry Pyzhov15c0ac42018-12-11 17:10:08 +030034 -g|--gpg-key)
35 gpg_key="$2"
36 shift 2
37 ;;
Richard Felkl4c4829d2017-11-11 00:12:20 +010038 -u|--user-data)
39 user_data="$2"
40 shift 2
41 ;;
42 -v|--vendor-data)
43 vendor_data="$2"
44 shift 2
45 ;;
46 -h|--hostname)
47 hostname="$2"
48 shift 2
49 ;;
50 -m|--model)
51 model="$2"
52 shift 2
53 ;;
54 -mp|--mk-pipelines)
55 mk_pipelines="$2"
56 shift 2
57 ;;
58 -p|--pipeline-library)
59 pipeline_library="$2"
60 shift 2
61 ;;
62 --) shift
63 break
64 ;;
65 esac
66done
67
68config_image=$1
69shift
70
Richard Felkl4c4829d2017-11-11 00:12:20 +010071
72uuid=$(uuidgen)
73if ! [ "$hostname" ]; then
74 hostname="$uuid"
75fi
76
77trap 'rm -rf $config_dir' EXIT
78config_dir=$(mktemp -t -d configXXXXXX)
79
Petr Ruzicka6982da32018-08-09 15:11:06 +020080if [ "$ssh_key" ] && [ -f "$ssh_key" ]; then
81 echo "adding pubkey from $ssh_key"
82 cp $ssh_key $config_dir/root_auth_keys
83fi
84
Dmitry Pyzhov15c0ac42018-12-11 17:10:08 +030085if [ "$gpg_key" ] && [ -f "$gpg_key" ]; then
86 echo "adding gpg key from $gpg_key"
87 mkdir $config_dir/gpg
88 cp $gpg_key $config_dir/gpg/salt_master_pillar.asc
89fi
90
Richard Felkl4c4829d2017-11-11 00:12:20 +010091if [ "$user_data" ] && [ -f "$user_data" ]; then
92 echo "adding user data from $user_data"
93 cp $user_data $config_dir/user-data
94else
95 touch $config_dir/user-data
96fi
97
98if [ "$vendor_data" ] && [ -f "$vendor_data" ]; then
99 echo "adding vendor data from $vendor_data"
100 cp $vendor_data $config_dir/vendor-data
101fi
102
103if [ "$model" ] ; then
104 echo "adding reclass model directory $model"
105 mkdir $config_dir/model
106 cp -r $model $config_dir/model/
107fi
108
109if [ "$mk_pipelines" ] ; then
Dmitry Pyzhovef9e19e2018-12-10 16:27:29 +0300110 echo "adding mk-pipelines directory $mk_pipelines"
Richard Felkl4c4829d2017-11-11 00:12:20 +0100111 cp -r $mk_pipelines $config_dir/mk-pipelines/
112fi
113
114if [ "$pipeline_library" ] ; then
Dmitry Pyzhovef9e19e2018-12-10 16:27:29 +0300115 echo "adding pipeline-library directory $pipeline_library"
Richard Felkl4c4829d2017-11-11 00:12:20 +0100116 cp -r $pipeline_library $config_dir/pipeline-library/
117fi
118
119cat > $config_dir/meta-data <<-EOF
120instance-id: $uuid
121hostname: $hostname
122local-hostname: $hostname
123EOF
124
125#PS1="debug> " bash --norc
126
127echo "generating configuration image at $config_image"
128if ! mkisofs -o $config_image -V cidata -r -J --quiet $config_dir; then
129 echo "ERROR: failed to create $config_image" >&2
130 exit 1
131fi
132
133chmod a+r $config_image
134
135
136