Skip to content

Commit 5e09af7

Browse files
committed
Add licensing information and use extra functions to return the status.
1 parent 6b33456 commit 5e09af7

File tree

1 file changed

+53
-29
lines changed

1 file changed

+53
-29
lines changed

modules/drbd

+53-29
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22
#
33
# DRBD module for ansible
44
#
5+
# Copyright (C) 2013, GC3, University of Zurich. All rights
6+
# reserved.
7+
#
8+
# This program is free software; you can redistribute it and/or modify it
9+
# under the terms of the GNU General Public License as published by the
10+
# Free Software Foundation; either version 2 of the License, or (at your
11+
# option) any later version.
12+
#
13+
# This program is distributed in the hope that it will be useful, but
14+
# WITHOUT ANY WARRANTY; without even the implied warranty of
15+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
# General Public License for more details.
17+
#
18+
# You should have received a copy of the GNU General Public License along
19+
# with this program; if not, write to the Free Software Foundation, Inc.,
20+
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21+
#
22+
#
523
# module: drdb
624
# short_description: This module will configure a drbd resource
725
# description:
@@ -89,19 +107,42 @@
89107
# examples:
90108
# - code: drbd name=myresource disk=/dev/sdb device=/dev/drbd1 address0=10.0.0.1:7789 address1=10.0.0.2:7789
91109

92-
function missing_argument(){
110+
function json_fail(){
111+
msg="$*"
93112
cat <<EOF
94113
{
95114
"failed" : true,
96-
"msg" : "Missing required arguments '$1'"
115+
"msg" : "$msg"
97116
}
98117
EOF
99118
exit 1
100119
}
101120

102-
[ -f $1 ] && source $1 || eval $*
121+
function json_changed(){
122+
msg="$*"
123+
cat <<EOF
124+
{
125+
"changed": true,
126+
"failed": false,
127+
"msg": "$msg"
128+
}
129+
EOF
130+
}
131+
132+
function json_ok(){
133+
msg="$*"
134+
cat <<EOF
135+
{
136+
"changed": false,
137+
"failed": false,
138+
"msg": "$msg"
139+
}
140+
EOF
141+
}
103142

143+
trap "json_fail 'We got killed!'" SIGINT SIGTERM
104144

145+
[ -f $1 ] && source $1 || eval $*
105146

106147
[ -z "$device0" ] && device0=$device
107148
[ -z "$device1" ] && device1=$device
@@ -117,7 +158,6 @@ EOF
117158
[ -z "$metadisk1" ] && metadisk1=$metadisk
118159

119160

120-
121161
[ -z "$name" ] && missing_argument name
122162
[ -z "$disk0" ] && missing_argument disk0
123163
[ -z "$disk1" ] && missing_argument disk1
@@ -126,7 +166,7 @@ EOF
126166
[ -z "$address0" ] && missing_argument address0
127167
[ -z "$address1" ] && missing_argument address1
128168

129-
tmp=$(tempfile)
169+
tmp=$(mktemp)
130170
cat > $tmp <<EOF
131171
resource $name {
132172
on $peer0 {
@@ -147,31 +187,15 @@ EOF
147187
cmp -s $tmp $dest
148188
if [ $? -eq 0 ]
149189
then
150-
cat <<EOF
151-
{
152-
"changed": false,
153-
"ok": true
154-
}
155-
EOF
190+
rm -rf $tmp
191+
json_ok "File is already latest version"
156192
else
157193
cp -f $tmp $dest
194+
if [ $? -ne 0 ]
195+
then
196+
rm -f $tmp
197+
json_fail "Error creating DRBD configuration file $dest"
198+
fi
158199
rm -f $tmp
159-
cat <<EOF
160-
{
161-
"ok": true,
162-
"changed": true,
163-
"failed": false
164-
}
165-
EOF
200+
json_changed "File has been updated"
166201
fi
167-
168-
# - failed=True, indicating that the action failed
169-
# - skipped=True, indicating a pre-condition for running the task was already
170-
# met
171-
# - changed=True/False, indicating the task changed/didn't change something,
172-
# used to trigger notifiers and coloring of the output.
173-
# - ansible_facts=<dict>, containing custom facts that will be usable as
174-
# variables in following tasks
175-
# - stdout=<str>, containing text output. Will be split into lines that can be
176-
# used if register'd
177-
# - msg, used for e.g. failure messages

0 commit comments

Comments
 (0)