Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 1 | #!/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 | |
| 8 | usage () { |
Dmitry Pyzhov | 15c0ac4 | 2018-12-11 17:10:08 +0300 | [diff] [blame^] | 9 | 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 Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 10 | } |
| 11 | |
| 12 | ARGS=$(getopt \ |
Dmitry Pyzhov | 15c0ac4 | 2018-12-11 17:10:08 +0300 | [diff] [blame^] | 13 | -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 Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 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 | ;; |
Dmitry Pyzhov | 15c0ac4 | 2018-12-11 17:10:08 +0300 | [diff] [blame^] | 34 | -g|--gpg-key) |
| 35 | gpg_key="$2" |
| 36 | shift 2 |
| 37 | ;; |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 38 | -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 |
| 66 | done |
| 67 | |
| 68 | config_image=$1 |
| 69 | shift |
| 70 | |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 71 | |
| 72 | uuid=$(uuidgen) |
| 73 | if ! [ "$hostname" ]; then |
| 74 | hostname="$uuid" |
| 75 | fi |
| 76 | |
| 77 | trap 'rm -rf $config_dir' EXIT |
| 78 | config_dir=$(mktemp -t -d configXXXXXX) |
| 79 | |
Petr Ruzicka | 6982da3 | 2018-08-09 15:11:06 +0200 | [diff] [blame] | 80 | if [ "$ssh_key" ] && [ -f "$ssh_key" ]; then |
| 81 | echo "adding pubkey from $ssh_key" |
| 82 | cp $ssh_key $config_dir/root_auth_keys |
| 83 | fi |
| 84 | |
Dmitry Pyzhov | 15c0ac4 | 2018-12-11 17:10:08 +0300 | [diff] [blame^] | 85 | if [ "$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 |
| 89 | fi |
| 90 | |
Richard Felkl | 4c4829d | 2017-11-11 00:12:20 +0100 | [diff] [blame] | 91 | if [ "$user_data" ] && [ -f "$user_data" ]; then |
| 92 | echo "adding user data from $user_data" |
| 93 | cp $user_data $config_dir/user-data |
| 94 | else |
| 95 | touch $config_dir/user-data |
| 96 | fi |
| 97 | |
| 98 | if [ "$vendor_data" ] && [ -f "$vendor_data" ]; then |
| 99 | echo "adding vendor data from $vendor_data" |
| 100 | cp $vendor_data $config_dir/vendor-data |
| 101 | fi |
| 102 | |
| 103 | if [ "$model" ] ; then |
| 104 | echo "adding reclass model directory $model" |
| 105 | mkdir $config_dir/model |
| 106 | cp -r $model $config_dir/model/ |
| 107 | fi |
| 108 | |
| 109 | if [ "$mk_pipelines" ] ; then |
| 110 | echo "adding mk_pipelines directory $mk_pipelines" |
| 111 | mkdir $config_dir/mk_pipelines |
| 112 | cp -r $mk_pipelines $config_dir/mk-pipelines/ |
| 113 | fi |
| 114 | |
| 115 | if [ "$pipeline_library" ] ; then |
| 116 | echo "adding pipeline_library directory $pipeline_library" |
| 117 | mkdir $config_dir/pipeline_library |
| 118 | cp -r $pipeline_library $config_dir/pipeline-library/ |
| 119 | fi |
| 120 | |
| 121 | cat > $config_dir/meta-data <<-EOF |
| 122 | instance-id: $uuid |
| 123 | hostname: $hostname |
| 124 | local-hostname: $hostname |
| 125 | EOF |
| 126 | |
| 127 | #PS1="debug> " bash --norc |
| 128 | |
| 129 | echo "generating configuration image at $config_image" |
| 130 | if ! mkisofs -o $config_image -V cidata -r -J --quiet $config_dir; then |
| 131 | echo "ERROR: failed to create $config_image" >&2 |
| 132 | exit 1 |
| 133 | fi |
| 134 | |
| 135 | chmod a+r $config_image |
| 136 | |
| 137 | |
| 138 | |