Statistics
| Branch: | Tag: | Revision:

one / src / mad / sh / scripts_common.sh @ 3c3b2e98

History | View | Annotate | Download (3.71 KB)

1
# -------------------------------------------------------------------------- #
2
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
3
#                                                                            #
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
5
# not use this file except in compliance with the License. You may obtain    #
6
# a copy of the License at                                                   #
7
#                                                                            #
8
# http://www.apache.org/licenses/LICENSE-2.0                                 #
9
#                                                                            #
10
# Unless required by applicable law or agreed to in writing, software        #
11
# distributed under the License is distributed on an "AS IS" BASIS,          #
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
13
# See the License for the specific language governing permissions and        #
14
# limitations under the License.                                             #
15
#--------------------------------------------------------------------------- #
16

    
17
# Paths for utilities
18
AWK=awk
19
BASH=/bin/bash
20
CUT=cut
21
DATE=/bin/date
22
DD=/bin/dd
23
LVCREATE=/sbin/lvcreate
24
LVREMOVE=/sbin/lvremove
25
LVS=/sbin/lvs
26
MD5SUM=/usr/bin/md5sum
27
MKFS=/sbin/mkfs
28
MKISOFS=/usr/bin/mkisofs
29
MKSWAP=/sbin/mkswap
30
SCP=/usr/bin/scp
31
SED=/bin/sed
32
SSH=/usr/bin/ssh
33
SUDO=/usr/bin/sudo
34
WGET=/usr/bin/wget
35

    
36
# Used for log messages
37
SCRIPT_NAME=`basename $0`
38

    
39
# Formats date for logs
40
function log_date
41
{
42
    $DATE +"%a %b %d %T %Y"
43
}
44

    
45
# Logs a message, alias to log_info
46
function log
47
{
48
    log_info "$1"
49
}
50

    
51
# Log function that knows how to deal with severities and adds the
52
# script name
53
function log_function
54
{
55
    echo "$1: $SCRIPT_NAME: $2" 1>&2
56
}
57

    
58
# Logs an info message
59
function log_info
60
{
61
    log_function "INFO" "$1"
62
}
63

    
64
# Logs an error message
65
function log_error
66
{
67
    log_function "ERROR" "$1"
68
}
69

    
70
# Logs a debug message
71
function log_debug
72
{
73
    log_function "DEBUG" "$1"
74
}
75

    
76
# This function is used to pass error message to the mad
77
function error_message
78
{
79
    (
80
        echo "ERROR MESSAGE --8<------"
81
        echo "$1"
82
        echo "ERROR MESSAGE ------>8--"
83
    ) 1>&2
84
}
85

    
86
# Executes a command, if it fails returns error message and exits
87
# If a second parameter is present it is used as the error message when
88
# the command fails
89
function exec_and_log
90
{
91
    message=$2
92
    output=`$1 2>&1 1>/dev/null`
93
    code=$?
94
    if [ "x$code" != "x0" ]; then
95
        log_error "Command \"$1\" failed."
96
        log_error "$output"
97
        if [ -z "$message" ]; then
98
            error_message "$output"
99
        else
100
            error_message "$message"
101
        fi
102
        exit $code
103
    fi
104
    log "Executed \"$1\"."
105
}
106

    
107

    
108

    
109
# Like exec_and_log but the first argument is the number of seconds
110
# before here is timeout and kills the command
111
#
112
# NOTE: if the command is killed because a timeout the exit code
113
# will be 143 = 128+15 (SIGHUP)
114
function timeout_exec_and_log
115
{
116
    TIMEOUT=$1
117
    shift
118

    
119
    CMD="$1"
120

    
121
    exec_and_log "$CMD" &
122
    CMD_PID=$!
123

    
124
    # timeout process
125
    (
126
        sleep $TIMEOUT
127
        kill $CMD_PID 2>/dev/null
128
        log_error "Timeout executing $CMD"
129
        error_message "Timeout executing $CMD"
130
        exit -1
131
    ) &
132
    TIMEOUT_PID=$!
133

    
134
    # stops the execution until the command finalizes
135
    wait $CMD_PID 2>/dev/null
136
    CMD_CODE=$?
137

    
138
    # if the script reaches here the command finished before it
139
    # consumes timeout seconds so we can kill timeout process
140
    kill $TIMEOUT_PID 2>/dev/null 1>/dev/null
141
    wait $TIMEOUT_PID 2>/dev/null
142

    
143
    # checks the exit code of the command and exits if it is not 0
144
    if [ "x$CMD_CODE" != "x0" ]; then
145
        exit $CMD_CODE
146
    fi
147
}