-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRakefile
235 lines (197 loc) · 5.64 KB
/
Rakefile
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
KYTOON_PROJECT = "#{File.dirname(__FILE__)}" unless defined?(KYTOON_PROJECT)
SSH_OPTS="-o StrictHostKeyChecking=no"
require 'rubygems'
#version_file=(File.join(KYTOON_PROJECT, 'config', 'TOOLKIT_VERSION'))
#toolkit_version=nil
#if ENV['KYTOON_VERSION'] then
#toolkit_version=ENV['KYTOON_VERSION']
#elsif File.exists?(version_file)
#toolkit_version=IO.read(version_file)
#end
#gem 'kytoon', "~>#{toolkit_version}" if toolkit_version
require 'kytoon'
include Kytoon
require 'tempfile'
require 'fileutils'
def mktempdir(prefix="firestack")
tmp_file=Tempfile.new(prefix)
path=tmp_file.path
tmp_file.close(true)
FileUtils.mkdir_p path
return path
end
def shh(script)
out=%x{#{script}}
retval=$?
if block_given? then
yield retval.success?, out
else
return [retval.success?, out]
end
end
def remote_exec(script_text)
sg=ServerGroup.get
gw_ip=sg.gateway_ip
out=%x{
ssh #{SSH_OPTS} root@#{gw_ip} bash <<-"REMOTE_EXEC_EOF"
#{BASH_COMMON}
#{script_text}
REMOTE_EXEC_EOF
}
retval=$?
if block_given? then
yield retval.success?, out
else
return [retval.success?, out]
end
end
def remote_multi_exec(hosts, script_text)
sg=ServerGroup.get
gw_ip=sg.gateway_ip
results = {}
threads = []
hosts.each do |host|
t = Thread.new do
out=%x{
ssh #{SSH_OPTS} root@#{gw_ip} bash <<-"REMOTE_EXEC_EOF"
ssh #{host} bash <<-"EOF_HOST"
#{BASH_COMMON}
#{script_text}
EOF_HOST
REMOTE_EXEC_EOF
}
retval=$?
results.store host, [retval.success?, out]
end
threads << t
end
threads.each {|t| t.join}
return results
end
def scp(src_dir, dest)
gw_ip = ServerGroup.get.gateway_ip
shh %{
scp -r #{SSH_OPTS} #{src_dir} root@#{gw_ip}:#{dest}
} do |ok, out|
fail "Failed to scp #{src_dir}! \n #{out}" unless ok
end
end
def get_revision(source_dir)
%x{
cd #{source_dir}
if [ -d ".git" ]; then
git log --oneline | wc -l
else
bzr revno --tree
fi
}.strip
end
Dir[File.join("#{Kytoon::Version::KYTOON_ROOT}/rake", '*.rake')].each do |rakefile|
import(rakefile)
end
if File.exist?(File.join(KYTOON_PROJECT, 'tasks')) then
Dir[File.join(File.dirname("__FILE__"), 'tasks', '*.rake')].each do |rakefile|
import(rakefile)
end
end
#functions to help install packages
BASH_COMMON_PKG=%{
function is_package_installed {
local PKG=$1
if [ -f /etc/fedora-release -o /etc/SuSE-release ]; then
rpm -q ${PKG} &> /dev/null
return $?
elif [ -f /usr/bin/dpkg ]; then
dpkg -l ${PKG} &> /dev/null
return $?
else
return 1
fi
}
function install_package {
local PKGS=
for PKG in $*; do
is_package_installed "${PKG}" || PKGS="${PKGS} ${PKG}"
done
if [ -n "${PKGS}" ]; then
if [ -f /etc/fedora-release ]; then
yum -y -q install ${PKGS}
elif [ -f /etc/redhat-release ]; then
yum -y -q install ${PKGS}
elif [ -f /etc/SuSE-release ]; then
zypper -q --non-interactive install ${PKGS}
elif [ -f /usr/bin/dpkg ]; then
apt-get -y -q install ${PKGS} &> /dev/null
fi
fi
}
function install_git {
if [ -f /etc/fedora-release ]; then
install_package git
else
install_package git-core
fi
}
}
#git clone w/ retry
firestack_debug=ENV.fetch("FIRESTACK_DEBUG", "")
BASH_COMMON=%{
if [ -n "#{firestack_debug}" ]; then
set -x
fi
#{BASH_COMMON_PKG}
function fail {
local MSG=$1
echo "FAILURE_MSG=$MSG"
exit 1
}
GIT_CACHE_DIR=/root/.git_repo_cache
function git_clone_with_retry {
local URL=${1:?"Please specify a URL."}
local DIR=${2:?"Please specify a DIR."}
local URLSHA=$(echo \"$URL\" | sha1sum | cut -f 1 -d ' ')
local SHORT_REPO_NAME=${URL/#*\\//}
local CACHE_DIR="${GIT_CACHE_DIR}/${SHORT_REPO_NAME}-${URLSHA}"
install_git
[ -d "$GIT_CACHE_DIR" ] || mkdir -p "$GIT_CACHE_DIR"
if [ -d "$CACHE_DIR" ]; then
echo "Using git repository cache..."
pushd "$CACHE_DIR" > /dev/null
git pull &> /dev/null
popd > /dev/null
cp -a "$CACHE_DIR" "$DIR"
else
local COUNT=1
echo "Git cloning: $URL"
until GIT_ASKPASS=echo git clone "$URL" "$DIR"; do
[ "$COUNT" -eq "4" ] && fail "Failed to clone: $URL"
sleep $(( $COUNT * 5 ))
COUNT=$(( $COUNT + 1 ))
done
cp -a "$DIR" "$CACHE_DIR"
fi
}
function configure_noauth {
cat > ~/novarc <<-EOF_CAT
NOVARC=$(readlink -f "${BASH_SOURCE:-${0}}" 2>/dev/null) ||
NOVARC=$(python -c 'import os,sys; print os.path.abspath(os.path.realpath(sys.argv[1]))' "${BASH_SOURCE:-${0}}")
NOVA_KEY_DIR=${NOVARC%/*}
export EC2_ACCESS_KEY="admin:admin"
export EC2_SECRET_KEY="91f4dacb-1aea-4428-97e1-f0ed631801f0"
export EC2_URL="http://127.0.0.1:8773/services/Cloud"
export S3_URL="http://127.0.0.1:3333"
export EC2_USER_ID=42 # nova does not use user id, but bundling requires it
#export EC2_PRIVATE_KEY=${NOVA_KEY_DIR}/pk.pem
#export EC2_CERT=${NOVA_KEY_DIR}/cert.pem
#export NOVA_CERT=${NOVA_KEY_DIR}/cacert.pem
export EUCALYPTUS_CERT=${NOVA_CERT} # euca-bundle-image seems to require this set
#alias ec2-bundle-image="ec2-bundle-image --cert ${EC2_CERT} --privatekey ${EC2_PRIVATE_KEY} --user 42 --ec2cert ${NOVA_CERT}"
#alias ec2-upload-bundle="ec2-upload-bundle -a ${EC2_ACCESS_KEY} -s ${EC2_SECRET_KEY} --url ${S3_URL} --ec2cert ${NOVA_CERT}"
export NOVA_API_KEY="admin"
export NOVA_USERNAME="admin"
export NOVA_PROJECT_ID="admin"
export NOVA_URL="http://127.0.0.1:8774/v1.1/"
export NOVA_VERSION="1.1"
EOF_CAT
}
}