Statistics
| Branch: | Tag: | Revision:

one / src / tm_mad / tm_common.sh @ fd8b1db0

History | View | Annotate | Download (4.53 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
# Takes out uneeded slashes. Repeated and final directory slashes:
49
# /some//path///somewhere/ -> /some/path/somewhere
50
function fix_dir_slashes
51
{
52
    dirname "$1/file" | sed -E 's/\/+/\//g'
53
}
54

    
55
function fix_var_slashes
56
{
57
    ONE_LOCAL_VAR=`fix_dir_slashes "$ONE_LOCAL_VAR"`
58
    VMDIR=`fix_dir_slashes "$VMDIR"`
59
}
60

    
61
function fix_paths
62
{
63
    if [ "x$ONE_LOCAL_VAR" != "x$VMDIR" ]; then
64
        SRC_PATH=`fix_dir_slashes "$SRC_PATH"`
65
        SRC_PATH=${SRC_PATH/$VMDIR/$ONE_LOCAL_VAR}
66
        DST_PATH=`fix_dir_slashes "$DST_PATH"`
67
        DST_PATH=${DST_PATH/$VMDIR/$ONE_LOCAL_VAR}
68
    fi
69
}
70

    
71
function fix_src_path
72
{
73
    if [ "x$ONE_LOCAL_VAR" != "x$VMDIR" ]; then
74
        SRC_PATH=`fix_dir_slashes "$SRC_PATH"`
75
        SRC_PATH=${SRC_PATH/$VMDIR/$ONE_LOCAL_VAR}
76
    fi
77
}
78

    
79
function fix_dst_path
80
{
81
    if [ "x$ONE_LOCAL_VAR" != "x$VMDIR" ]; then
82
        DST_PATH=`fix_dir_slashes "$DST_PATH"`
83
        DST_PATH=${DST_PATH/$VMDIR/$ONE_LOCAL_VAR}
84
    fi
85
}
86

    
87
# Used for log messages
88
SCRIPT_NAME=`basename $0`
89

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

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

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

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

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

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

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

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

    
154
    CMD="$1"
155

    
156
    exec_and_log "$CMD" &
157
    CMD_PID=$!
158

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

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

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

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