summaryrefslogtreecommitdiffstats
path: root/distribution/reservesys/reservesys.sh
blob: 7e1851d3ae67fe404c9d17fcef6f4faab4a2b0c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/sh

#
# Trap signal 15, when the user is done we will be killed
# If we timeout from either local or external watchdog
# we will have a warn/fail recorded anyway.
#
trap 'exit 0' 15

# Functions
RprtRslt()
{
    ONE=$1
    TWO=$2
    THREE=$3

    # File the results in the database
    rstrnt-report-result $ONE $TWO $THREE
}

MOTD()
{
    FILE=/etc/motd
    cp $FILE $FILE.orig
    if selinuxenabled &>/dev/null ; then
        restorecon $FILE.orig
    fi

    local admonition=
    if [ -n "$BEAKER_RESERVATION_POLICY_URL" ] ; then
        admonition="
 Please ensure that you adhere to the reservation policy
  for Beaker systems:
  ${BEAKER_RESERVATION_POLICY_URL}"
    fi

    cat <<END > $FILE
**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **
                 This System is reserved by $RSTRNT_OWNER

 To return this system early. You can run the command: return2beaker.sh
  Ensure you have your logs off the system before returning to Beaker

 To extend your reservation time. You can run the command:
  extendtesttime.sh
 This is an interactive script. You will be prompted for how many
  hours you would like to extend the reservation.${admonition}

 You should verify the watchdog was updated succesfully after
  you extend your reservation.
  ${BEAKER}recipes/$RSTRNT_RECIPEID

 For ssh, kvm, serial and power control operations please look here:
  ${BEAKER}view/$HOSTNAME

 For the default root password, see:
  ${BEAKER}prefs/

      Beaker Test information:
                         HOSTNAME=$HOSTNAME
                            JOBID=$RSTRNT_JOBID
                         RECIPEID=$RSTRNT_RECIPEID
                           DISTRO=$RSTRNT_OSDISTRO
                     ARCHITECTURE=$RSTRNT_OSARCH

      Job Whiteboard: $BEAKER_JOB_WHITEBOARD

      Recipe Whiteboard: $BEAKER_RECIPE_WHITEBOARD
**  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **  **
END
}

RETURNSCRIPT()
{
    SCRIPT=/usr/bin/return2beaker.sh

    echo "#!/bin/sh"                           > $SCRIPT
    echo "killall reservesys.sh" >> $SCRIPT
    echo "/bin/echo Going on..." >> $SCRIPT

    chmod 777 $SCRIPT
}

EXTENDTESTTIME()
{
SCRIPT2=/usr/bin/extendtesttime.sh

cat > $SCRIPT2 <<-EOF
howmany()
{
if [ -n "\$1" ]; then
  RESPONSE="\$1"
else
  echo "How many hours would you like to extend the reservation."
  echo "             Must be between 1 and 99                   "
  read RESPONSE
fi
validint "\$RESPONSE" 1 99
echo "Extending reservation time \$RESPONSE"
EXTRESTIME=\$(echo \$RESPONSE)h
}

validint()
{
# validate first field.
number="\$1"; min="\$2"; max="\$3"

if [ -z "\$number" ] ; then
echo "You didn't enter anything."
exit 1
fi

if [ "\${number%\${number#?}}" = "-" ] ; then # first char '-' ?
testvalue="\${number#?}" # all but first character
else
testvalue="\$number"
fi

nodigits="\$(echo \$testvalue | sed 's/[[:digit:]]//g')"

if [ ! -z "\$nodigits" ] ; then
echo "Invalid number format! Only digits, no commas, spaces, etc."
exit 1
fi

if [ ! -z "\$min" ] ; then
if [ "\$number" -lt "\$min" ] ; then
echo "Your value is too small: smallest acceptable value is \$min"
exit 1
fi
fi
if [ ! -z "\$max" ] ; then
if [ "\$number" -gt "\$max" ] ; then
echo "Your value is too big: largest acceptable value is \$max"
exit 1
fi
fi

return 0
}

howmany "\$1"

export HOSTNAME=$HOSTNAME
export HARNESS_PREFIX=$HARNESS_PREFIX
export RSTRNT_RECIPEID=$RSTRNT_RECIPEID
rstrnt-adjust-watchdog \$EXTRESTIME
EOF

chmod 777 $SCRIPT2
}

NOTIFY()
{
    /sbin/service sendmail start
    local msg=$(mktemp)

cat > $msg <<-EOF
To: $RSTRNT_OWNER
Subject: [Beaker Machine Reserved] $HOSTNAME
X-Beaker-test: $RSTRNT_TASKNAME

EOF
    cat /etc/motd >>$msg
    cat $msg | sendmail -t
    \rm -f $msg
}

echo "***** Start of reservesys test *****"

if [ -n "$RESERVEBY" ]; then
    RSTRNT_OWNER=$RESERVEBY
fi

BUILD_()
{
    # build the /etc/motd file
    echo "***** Building /etc/motd *****"
    MOTD

    # send email to the submitter
    echo "***** Sending email to $RSTRNT_OWNER *****"
    NOTIFY

    # build /usr/bin/extendtesttime.sh script to allow user
    #  to extend the time time.
    echo "***** Building /usr/bin/extendtesttime.sh *****"
    EXTENDTESTTIME

    # build /usr/bin/return2beaker.sh script to allow user
    #  to return the system to Beaker early.
    echo "***** Building /usr/bin/return2beaker.sh *****"
    RETURNSCRIPT
}

# Only do initial setup once at beginning
if [ -n "$RSTRNT_REBOOTCOUNT" ]; then
    if [ $RSTRNT_REBOOTCOUNT -eq 0 ]; then
        if [ -n "$RESERVE_IF_FAIL" ]; then
            ./recipe_status
            if [ $? -eq 0 ]; then
                RprtRslt $TEST PASS 0
                exit 0
            fi
        fi
    
        BUILD_
    
        RprtRslt $RSTRNT_TASKNAME PASS 0
    fi
fi

# Run another copy of restraint on a different port
# This makes it easy to develop/debug new tasks.

restraintd --port 8082


# We stay running in restraint..
while (true); do
    sleep 5
done

echo "***** End of reservesys test *****"