~dbart/charms/trusty/mariadb/trunk

« back to all changes in this revision

Viewing changes to lib/net.sh

  • Committer: Daniel Bartholomew
  • Date: 2014-09-25 20:40:27 UTC
  • Revision ID: dbart@mariadb.org-20140925204027-45xyiwh0qijlibhi
Added the rest of the files from the old MariaDB Charm.

The following was copied with some small changes:
  - copyright

The following were copied without any changes:
  - hooks/ceph-relation-changed@
  - hooks/ceph-relation-joined@
  - hooks/cluster-relation-changed@
  - hooks/common.py
  - hooks/db-admin-relation-joined@
  - hooks/db-relation-broken
  - hooks/db-relation-joined
  - hooks/ha-relation-changed@
  - hooks/ha-relation-joined@
  - hooks/ha_relations.py
  - hooks/lib/__init__.py
  - hooks/lib/ceph_utils.py
  - hooks/lib/cluster_utils.py
  - hooks/lib/utils.py
  - hooks/local-monitors-relation-joined@
  - hooks/master-relation-broken@
  - hooks/master-relation-changed
  - hooks/master-relation-departed@
  - hooks/monitors-relation-broken
  - hooks/monitors-relation-departed
  - hooks/monitors-relation-joined
  - hooks/monitors.common.bash
  - hooks/munin-relation-changed
  - hooks/munin-relation-joined
  - hooks/shared-db-relation-changed@
  - hooks/shared-db-relation-joined@
  - hooks/shared_db_relations.py
  - hooks/slave-relation-broken
  - hooks/slave-relation-changed
  - hooks/slave-relation-departed@
  - hooks/slave-relation-joined
  - keys/repo.percona.com
  - lib/net.sh
  - scripts/
  - scripts/add_to_cluster
  - scripts/remove_from_cluster
  - monitors.yaml

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
 
 
3
##
 
4
# Copyright 2011 Marco Ceppi <marco@ceppi.net>
 
5
#
 
6
# This file is part of Charm Helpers.
 
7
#
 
8
# Charm Helpers is free software: you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation, either version 3 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# Charm Helpers is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License
 
19
# along with Charm Helpers.  If not, see <http://www.gnu.org/licenses/>.
 
20
##
 
21
 
 
22
##
 
23
# Globally overridable settings. Make sure to set them before sourcing
 
24
# this file.
 
25
CH_WGET_ARGS=${CH_WGET_ARGS:-"-q --content-disposition"}
 
26
 
 
27
##
 
28
# Get File
 
29
# Retrives a file and compares the file to a hash
 
30
#
 
31
# param FILE URL or Path to file
 
32
# param HASH URL, Path, or HASH string for file's hash
 
33
#
 
34
# return filepath|null
 
35
##
 
36
ch_get_file()
 
37
{
 
38
        local FILE=${1:-""}
 
39
        local HASH=${2:-""}
 
40
        local CH_DOWNLOAD_DIR=${CH_DOWNLOAD_DIR:-"`mktemp -d /tmp/ch-downloads.XXXXXX`"}
 
41
 
 
42
        if [ `ch_is_url "$FILE"` ]; then
 
43
                wget $CH_WGET_ARGS --directory-prefix="$CH_DOWNLOAD_DIR/" "$FILE"
 
44
                FILE=$CH_DOWNLOAD_DIR/$(ls -tr $CH_DOWNLOAD_DIR|head -n 1)
 
45
        fi
 
46
 
 
47
        if [ ! -f "$FILE" ]; then
 
48
                return 2
 
49
        fi
 
50
 
 
51
        if [ -z "$HASH" ];then
 
52
                #echo "Warning, no has specified. The file will be downloaded but not cryptographically checked!" > 2
 
53
                echo "$FILE"
 
54
                return 0
 
55
        elif [ `ch_is_url "$HASH"` ]; then
 
56
                local HASHNAME=$(basename $HASH)
 
57
                wget $CH_WGET_ARGS "$HASH" -O /tmp/$HASHNAME
 
58
                HASH=$(cat /tmp/$HASHNAME | awk '{ print $1 }')
 
59
        elif [ -f "$HASH" ]; then
 
60
                HASH=$(cat "$HASH" | awk '{ print $1 }')
 
61
        fi
 
62
 
 
63
        local HASH_TYPE=$(ch_type_hash $HASH)
 
64
 
 
65
        if [ -z "$HASH_TYPE" ]; then
 
66
                return 3
 
67
        else
 
68
                local FILE_HASH=$(${HASH_TYPE}sum $FILE | awk '{ print $1 }')
 
69
 
 
70
                if [ "$FILE_HASH" != "$HASH" ]; then
 
71
                        return 4
 
72
                fi
 
73
        fi
 
74
 
 
75
        echo "$FILE"
 
76
        return 0
 
77
}
 
78
 
 
79
##
 
80
# Hash Type
 
81
# Determine, using best approximation, if the hash is valid and what type
 
82
# of hashing algorithm was used
 
83
#
 
84
# param HASH
 
85
#
 
86
# return type|false
 
87
##
 
88
ch_type_hash()
 
89
{
 
90
        local DIRTY="$1"
 
91
 
 
92
        case $DIRTY in
 
93
                *[![:xdigit:]]* | "" )
 
94
                        echo ""
 
95
                        return 1
 
96
                ;;
 
97
                * )
 
98
                        case ${#DIRTY} in
 
99
                                32 )
 
100
                                        echo md5
 
101
                                ;;
 
102
                                40 )
 
103
                                        echo sha1
 
104
                                ;;
 
105
                                64 )
 
106
                                        echo sha256
 
107
                                ;;
 
108
                        esac
 
109
                ;;
 
110
        esac
 
111
}
 
112
 
 
113
##
 
114
# Is URL?
 
115
# Checks if the string passed is a valid URL (http(s), ftp)
 
116
#
 
117
# param URL The URL to be checked
 
118
#
 
119
# return boolean
 
120
##
 
121
ch_is_url()
 
122
{
 
123
        local DIRTY="$1"
 
124
 
 
125
        case "$DIRTY" in
 
126
                "http://"* | "https://"* | "ftp://"*)
 
127
                        echo true
 
128
                        return 0
 
129
                ;;
 
130
                *)
 
131
                        return 1
 
132
                ;;
 
133
        esac
 
134
}
 
135
 
 
136
##
 
137
# Is IP?
 
138
# Checks if the string passed is an IP address
 
139
#
 
140
# param IP The IP addressed to be checked
 
141
#
 
142
# return boolean
 
143
##
 
144
ch_is_ip()
 
145
{
 
146
        local DIRTY="$1"
 
147
        local IP=$(echo $DIRTY | awk -F. '$1 <=255 && $2 <= 255 && $3 <= 255 && $4 <= 255 ')
 
148
 
 
149
        if [ -z "$IP" ]; then
 
150
                return 1
 
151
        else
 
152
                echo true
 
153
                return 0
 
154
        fi
 
155
}
 
156
 
 
157
##
 
158
# Get IP
 
159
# Returns the first IP match to the hostname provided
 
160
#
 
161
# param HOSTNAME Host for which to retrieve an IP
 
162
#
 
163
# return IP|false
 
164
##
 
165
ch_get_ip()
 
166
{
 
167
        local HOST="$1"
 
168
        #So, if there's multiple IP addresses, just grab the first
 
169
        # for now.
 
170
        if [ `ch_is_ip "$HOST"` ]; then
 
171
                echo "$HOST"
 
172
                return 0
 
173
        fi
 
174
 
 
175
        local CHECK_IP=$(host -t A $HOST | awk 'NR==1{ print $4 }')
 
176
 
 
177
        if [ ! `ch_is_ip "$CHECK_IP"` ]; then
 
178
                # Try a dig, why not?
 
179
                CHECK_IP=$(dig +short $HOST | awk 'NR==1{ print $1 }')
 
180
 
 
181
                if [ ! `ch_is_ip "$CHECK_IP"` ]; then
 
182
                        return 1
 
183
                fi
 
184
        fi
 
185
 
 
186
        echo $CHECK_IP
 
187
        return 0
 
188
}