The cirros image was rebuilt against the 3.13.0-83 kernel, drivers e1000e, igbvf...
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / src / bin / cirros-per
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##*/} [ options ] frequency name cmd [args]
9
10    run cmd with given arguments for the given frequency.
11    frequency is:
12      always:    run every time
13      always-ds: run every time, but only if there is a datasource
14      boot:      run only once per boot
15      instance:  run once per instance (first boot)
16      once:      run once ever (first boot with or without datasource)
17    name is the name of this thing.  subsequent runs of will be
18      run with the given frequency based on state kept in 'name'
19    
20    options:
21         --dry-run  : only report, do not update results
22    -v | --verbose  : be more verbose
23 EOF
24 }
25
26 cirros_per() {
27         local short_opts="hv"
28         local long_opts="help,dry-run,verbose"
29         local getopt_out=""
30         getopt_out=$(getopt --name "${0##*/}" \
31                 --options "${short_opts}" --long "${long_opts}" -- "$@") &&
32                 eval set -- "${getopt_out}" ||
33                 { bad_Usage; return; }
34
35         local dryrun=false cur="" next="" VERBOSITY="$VERBOSITY"
36         local freq="" name="" state_d="" marker=""
37
38         while [ $# -ne 0 ]; do
39                 cur=${1}; next=${2};
40                 case "$cur" in
41                         -h|--help) Usage ; exit 0;;
42                         -v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
43                            --dry-run) dryrun=true;;
44                         --) shift; break;;
45                 esac
46                 shift;
47         done
48         [ $# -ge 3 ] || { bad_Usage "must provide frequency, name, cmd"; return; }
49
50         freq="$1"
51         name="$2"
52         shift 2;
53
54         [ ! -r "$CONFIG" ] || . "$CONFIG" ||
55                 fail "failed to read $CONFIG"
56
57         case "$freq" in
58                 always)
59                         state_d="${STATE_D}";
60                         marker="always.always.${name}"
61                         ;;
62                 always-ds)
63                         state_d="${STATE_D}";
64                         marker="always-ds.always.${name}"
65                         assert_datasource ||
66                                 { error "no datasource found"; return 1; }
67                         ;;
68                 instance)
69                         state_d="${STATE_D}";
70                         ds_get_item "instance-id" ||
71                                 { error "failed to get instance-id of datasource"; return 1; }
72                         iid="${_RET}"
73                         marker="instance.${iid}.${name}"
74                         ;;
75                 once)
76                         state_d="${STATE_D}";
77                         marker="once.once.${name}"
78                         ;;
79                 boot)
80                         state_d="${BOOT_STATE_D}"
81                         marker="boot.boot.${name}"
82                         ;;
83                 *) fail "bad frequency $freq";;
84         esac
85
86         debug 2 "using state_dir=${state_d}, marker=${marker}"
87
88         [ -d "$state_d" ] || mkdir -p "$state_d" ||
89                 { error "failed to create state_dir ${state_d}"; return 1; }
90
91         if [ "${freq#always}" = "$freq" -a -f "${state_d}/${marker}" ]; then
92                 { error "${name} already run per ${freq}"; return 0; }
93         fi
94
95         if $dryrun; then
96                 error "run" "$@"
97                 return 0
98         fi
99
100         local ret=0 uptime="" idle=""
101         debug 2 "[$freq]" "$@"
102         "$@"
103         ret=$?
104
105         read_uptime
106         echo "[${_RET}]" "$@" > "${state_d}/${marker}"
107
108         return "$ret"
109 }
110
111 cirros_per "$@"
112
113 # vi: ts=4 noexpandtab