~dbart/charms/trusty/mariadb/trunk

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
#!/bin/sh

##
# Copyright 2011 Marco Ceppi <marco@ceppi.net>
#
# This file is part of Charm Helpers.
#
# Charm Helpers is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Charm Helpers is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Charm Helpers.  If not, see <http://www.gnu.org/licenses/>.
##

##
# Globally overridable settings. Make sure to set them before sourcing
# this file.
CH_WGET_ARGS=${CH_WGET_ARGS:-"-q --content-disposition"}

##
# Get File
# Retrives a file and compares the file to a hash
#
# param FILE URL or Path to file
# param HASH URL, Path, or HASH string for file's hash
#
# return filepath|null
##
ch_get_file()
{
	local FILE=${1:-""}
	local HASH=${2:-""}
	local CH_DOWNLOAD_DIR=${CH_DOWNLOAD_DIR:-"`mktemp -d /tmp/ch-downloads.XXXXXX`"}

	if [ `ch_is_url "$FILE"` ]; then
		wget $CH_WGET_ARGS --directory-prefix="$CH_DOWNLOAD_DIR/" "$FILE"
		FILE=$CH_DOWNLOAD_DIR/$(ls -tr $CH_DOWNLOAD_DIR|head -n 1)
	fi

	if [ ! -f "$FILE" ]; then
		return 2
	fi

	if [ -z "$HASH" ];then
		#echo "Warning, no has specified. The file will be downloaded but not cryptographically checked!" > 2
		echo "$FILE"
		return 0
	elif [ `ch_is_url "$HASH"` ]; then
		local HASHNAME=$(basename $HASH)
		wget $CH_WGET_ARGS "$HASH" -O /tmp/$HASHNAME
		HASH=$(cat /tmp/$HASHNAME | awk '{ print $1 }')
	elif [ -f "$HASH" ]; then
		HASH=$(cat "$HASH" | awk '{ print $1 }')
	fi

	local HASH_TYPE=$(ch_type_hash $HASH)

	if [ -z "$HASH_TYPE" ]; then
		return 3
	else
		local FILE_HASH=$(${HASH_TYPE}sum $FILE | awk '{ print $1 }')

		if [ "$FILE_HASH" != "$HASH" ]; then
			return 4
		fi
	fi

	echo "$FILE"
	return 0
}

##
# Hash Type
# Determine, using best approximation, if the hash is valid and what type
# of hashing algorithm was used
#
# param HASH
#
# return type|false
##
ch_type_hash()
{
	local DIRTY="$1"

	case $DIRTY in
		*[![:xdigit:]]* | "" )
			echo ""
			return 1
		;;
		* )
			case ${#DIRTY} in
				32 )
					echo md5
				;;
				40 )
					echo sha1
				;;
				64 )
					echo sha256
				;;
			esac
		;;
	esac
}

##
# Is URL?
# Checks if the string passed is a valid URL (http(s), ftp)
#
# param URL The URL to be checked
#
# return boolean
##
ch_is_url()
{
	local DIRTY="$1"

	case "$DIRTY" in
		"http://"* | "https://"* | "ftp://"*)
			echo true
			return 0
		;;
		*)
			return 1
		;;
	esac
}

##
# Is IP?
# Checks if the string passed is an IP address
#
# param IP The IP addressed to be checked
#
# return boolean
##
ch_is_ip()
{
	local DIRTY="$1"
	local IP=$(echo $DIRTY | awk -F. '$1 <=255 && $2 <= 255 && $3 <= 255 && $4 <= 255 ')

	if [ -z "$IP" ]; then
		return 1
	else
		echo true
		return 0
	fi
}

##
# Get IP
# Returns the first IP match to the hostname provided
#
# param HOSTNAME Host for which to retrieve an IP
#
# return IP|false
##
ch_get_ip()
{
	local HOST="$1"
	#So, if there's multiple IP addresses, just grab the first
	# for now.
	if [ `ch_is_ip "$HOST"` ]; then
		echo "$HOST"
		return 0
	fi

	local CHECK_IP=$(host -t A $HOST | awk 'NR==1{ print $4 }')

	if [ ! `ch_is_ip "$CHECK_IP"` ]; then
		# Try a dig, why not?
		CHECK_IP=$(dig +short $HOST | awk 'NR==1{ print $1 }')

		if [ ! `ch_is_ip "$CHECK_IP"` ]; then
			return 1
		fi
	fi

	echo $CHECK_IP
	return 0
}