Statistics
| Branch: | Tag: | Revision:

one / src / tm_mad / tm_common.sh @ 9af844e5

History | View | Annotate | Download (4.36 KB)

1
# -------------------------------------------------------------------------- #
2
# Copyright 2002-2010, 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
if [ -z "$ONE_LOCATION" ]; then
18
    ONE_LOCAL_VAR=/var/lib/one
19
else
20
    ONE_LOCAL_VAR=$ONE_LOCATION/var
21
fi
22

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

    
42
function get_vmdir
43
{
44
    VMDIR=`grep '^VM_DIR=' $ONE_LOCAL_VAR/config | cut -d= -f2`
45
    fix_var_slashes
46
}
47

    
48
function fix_dir_slashes
49
{
50
    dirname "$1/file" | sed -E 's/\/+/\//g'
51
}
52

    
53
function fix_var_slashes
54
{
55
    ONE_LOCAL_VAR=`fix_dir_slashes "$ONE_LOCAL_VAR"`
56
    VMDIR=`fix_dir_slashes "$VMDIR"`
57
}
58

    
59
function fix_paths
60
{
61
    if [ -n "$VMDIR" ]; then
62
        SRC_PATH=`fix_dir_slashes "$SRC_PATH"`
63
        SRC_PATH=${SRC_PATH/$VMDIR/$ONE_LOCAL_VAR}
64
        DST_PATH=`fix_dir_slashes "$DST_PATH"`
65
        DST_PATH=${DST_PATH/$VMDIR/$ONE_LOCAL_VAR}
66
    fi
67
}
68

    
69
function fix_src_path
70
{
71
    if [ -n "$VMDIR" ]; then
72
        SRC_PATH=`fix_dir_slashes "$SRC_PATH"`
73
        SRC_PATH=${SRC_PATH/$VMDIR/$ONE_LOCAL_VAR}
74
    fi
75
}
76

    
77
function fix_dst_path
78
{
79
    if [ -n "$VMDIR" ]; then
80
        DST_PATH=`fix_dir_slashes "$DST_PATH"`
81
        DST_PATH=${DST_PATH/$VMDIR/$ONE_LOCAL_VAR}
82
    fi
83
}
84

    
85
# Used for log messages
86
SCRIPT_NAME=`basename $0`
87

    
88
# Formats date for logs
89
function log_date
90
{
91
    $DATE +"%a %b %d %T %Y"
92
}
93

    
94
# Logs a message
95
function log
96
{
97
    echo "$SCRIPT_NAME: $1"
98
}
99

    
100
# Logs an error message
101
function log_error
102
{
103
    log "ERROR: $1"
104
}
105

    
106
# This function is used to pass error message to the mad
107
function error_message
108
{
109
    (
110
        echo "ERROR MESSAGE --8<------"
111
        echo "$1"
112
        echo "ERROR MESSAGE ------>8--"
113
    ) 1>&2
114
}
115

    
116
# Gets the host from an argument
117
function arg_host
118
{
119
    echo $1 | $SED -e 's/^\([^:]*\):.*$/\1/'
120
}
121

    
122
# Gets the path from an argument
123
function arg_path
124
{
125
    echo $1 | $SED -e 's/^[^:]*:\(.*\)$/\1/'
126
}
127

    
128
# Executes a command, if it fails return error message and exits
129
function exec_and_log
130
{
131
    output=`$1 2>&1 1>/dev/null`
132
    code=$?
133
    if [ "x$code" != "x0" ]; then
134
        log_error "Command \"$1\" failed."
135
        log_error "$output"
136
        error_message "$output"
137
        exit $code
138
    fi
139
    log "Executed \"$1\"."
140
}
141

    
142
# Like exec_and_log but the first argument is the number of seconds
143
# before here is timeout and kills the command
144
#
145
# NOTE: if the command is killed because a timeout the exit code
146
# will be 143 = 128+15 (SIGHUP)
147
function timeout_exec_and_log
148
{
149
    TIMEOUT=$1
150
    shift
151

    
152
    CMD="$1"
153

    
154
    exec_and_log "$CMD" &
155
    CMD_PID=$!
156

    
157
    # timeout process
158
    (
159
        sleep $TIMEOUT
160
        kill $CMD_PID 2>/dev/null
161
        log_error "Timeout executing $CMD"
162
        error_message "Timeout executing $CMD"
163
        exit -1
164
    ) &
165
    TIMEOUT_PID=$!
166

    
167
    # stops the execution until the command finalizes
168
    wait $CMD_PID 2>/dev/null
169
    CMD_CODE=$?
170

    
171
    # if the script reaches here the command finished before it
172
    # consumes timeout seconds so we can kill timeout process
173
    kill $TIMEOUT_PID 2>/dev/null 1>/dev/null
174
    wait $TIMEOUT_PID 2>/dev/null
175

    
176
    # checks the exit code of the command and exits if it is not 0
177
    if [ "x$CMD_CODE" != "x0" ]; then
178
        exit $CMD_CODE
179
    fi
180
}
181