96282494525705d5ca64d3af08fdefc2831c1d51
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / src / sbin / cirros-userdata
1 #!/bin/sh
2
3 . ${CIRROS_LIB:=/lib/cirros/shlib_cirros} ||
4         { echo "failed to read ${CIRROS_LIB}" 1>&2; exit 1; }
5
6 Usage() {
7         cat <<EOF
8 Usage: ${0##*/} [file]
9
10    handle the user-data present in file.
11    If no file is present, retrieve 'user-data' from datasource.
12
13    if no file is given, and no datasource is found, exit silently.
14    
15    options:
16         --dry-run  : only report, do not update results
17    -v | --verbose  : be more verbose
18 EOF
19 }
20
21 cirros_userdata() {
22         local short_opts="hv"
23         local long_opts="help,dry-run,verbose"
24         local getopt_out=""
25         getopt_out=$(getopt --name "${0##*/}" \
26                 --options "${short_opts}" --long "${long_opts}" -- "$@") &&
27                 eval set -- "${getopt_out}" ||
28                 { bad_Usage; return; }
29
30         local dryrun=false cur="" next="" VERBOSITY="$VERBOSITY"
31
32         while [ $# -ne 0 ]; do
33                 cur=${1}; next=${2};
34                 case "$cur" in
35                         -h|--help) Usage ; exit 0;;
36                         -v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
37                         --dry-run) dryrun=true;;
38                         --) shift; break;;
39                 esac
40                 shift;
41         done
42
43         if [ $# -eq 0 ]; then
44                 assert_datasource || exit 0
45                 ds_has_item user-data ||
46                         { debug 1 "no userdata for datasource"; return 0; }
47                 ds_get_item_path user-data || { error "failed to get user-data"; return 1; }
48                 set -- "$_RET"
49         fi
50
51         local tempf="" ret="" failures=0
52         for cur in "$@"; do
53                 [ -f "$@" ] || { error "$cur is not a file"; return 1; }
54                 if [ -x "$cur" ]; then
55                         "$cur"
56                         ret=$?
57                         debug 1 "$cur returned $ret"
58                 elif [ "$(head -c 2 "$cur" )" = "#!" ]; then
59                         $dryrun && { error "execute ${cur}"; continue; }
60                         if [ -z "$tempf" ]; then
61                                 tempf=$(mktemp "${TMPDIR:-/tmp}/${0##*/}.XXXXXX") ||
62                                         { error "failed to make tempdir"; return 1; }
63                         fi
64                         cat "$cur" > "$tempf" && chmod 700 "$cur" || {
65                                 error "failed to copy $cur to make executable";
66                                 rm -f "$tempf";
67                                 return 1;
68                         }
69                         "$cur"
70                         ret=$?
71                         debug 2 "$cur returned $ret"
72                 else
73                         ret=0
74                         debug 1 "$cur was not '#!' or executable"
75                 fi
76                 [ $ret -eq 0 ] || failures=$(($failures+1))
77         done
78         rm -f "$tempf"
79
80         return $failures
81 }
82
83 cirros_userdata "$@"
84
85 # vi: ts=4 noexpandtab