/
fabos.py
187 lines (170 loc) · 6.44 KB
/
fabos.py
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
import os
import ast
import tempfile
from fabfile.config import *
def copy_pkg(tgt_host, pkg_file):
with settings(host_string = tgt_host):
put(pkg_file, use_sudo=True)
#end _copy_pkg
def install_pkg(tgt_host, pkg_file):
with settings(host_string = tgt_host):
sudo("rpm -iv --force %s" %(pkg_file.split('/')[-1]))
#end _install_pkg
def get_linux_distro():
linux_distro = "python -c 'from platform import linux_distribution; print linux_distribution()'"
(dist, version, extra) = ast.literal_eval(sudo(linux_distro))
return (dist, version, extra)
def detect_ostype():
(dist, version, extra) = get_linux_distro()
if extra is not None and 'xen' in extra:
dist = 'xen'
elif 'red hat' in dist.lower():
dist = 'redhat'
elif 'centos linux' in dist.lower():
dist = 'centoslinux'
return dist.lower()
#end detect_ostype
def get_openstack_sku(use_install_repo=False):
dist = detect_ostype()
if dist in ['ubuntu']:
pkg = 'nova-common'
elif dist in ['centos', 'fedora', 'redhat', 'centoslinux']:
pkg = 'openstack-nova-common'
else:
print "Unsupported OS type"
return None
pkg_ver = get_release(pkg=pkg, use_install_repo=use_install_repo)
if pkg_ver is None:
return None
if pkg_ver.find('2013.2') != -1:
openstack_sku = 'havana'
elif pkg_ver.find('2014.1') != -1:
openstack_sku = 'icehouse'
elif pkg_ver.find('2014.2') != -1:
openstack_sku = 'juno'
elif pkg_ver.find('2015.1') != -1:
openstack_sku = 'kilo'
elif pkg_ver.find('12.0') != -1:
openstack_sku = 'liberty'
else:
print "OpenStack distribution unknown.. assuming icehouse.."
openstack_sku = 'icehouse'
return openstack_sku
#end get_openstack_sku
def get_release(pkg='contrail-install-packages', use_install_repo=False):
pkg_ver = None
dist = detect_ostype()
print "Dist is %s" % dist
if dist in ['centos', 'fedora', 'redhat', 'centoslinux']:
if use_install_repo:
cmd = "rpm -qp --queryformat '%%{VERSION}' /opt/contrail/contrail_install_repo/%s*.rpm 2> /dev/null" %pkg
else:
cmd = "rpm -q --queryformat '%%{VERSION}' %s" %pkg
elif dist in ['ubuntu']:
if use_install_repo:
cmd = "dpkg --info /opt/contrail/contrail_install_repo/%s*.deb | grep Version: | cut -d' ' -f3 | cut -d'-' -f1" %pkg
else:
cmd = "dpkg -s %s | grep Version: | cut -d' ' -f2 | cut -d'-' -f1" %pkg
pkg_ver = sudo(cmd)
if 'is not installed' in pkg_ver or 'is not available' in pkg_ver or 'No such file or directory' in pkg_ver:
print "Package %s not installed." % pkg
return None
return pkg_ver
def get_build(pkg='contrail-install-packages'):
pkg_rel = None
dist = detect_ostype()
if dist in ['centos', 'fedora', 'redhat', 'centoslinux']:
cmd = "rpm -q --queryformat '%%{RELEASE}' %s" %pkg
elif dist in ['ubuntu']:
cmd = "dpkg -s %s | grep Version: | cut -d' ' -f2 | cut -d'-' -f2" %pkg
pkg_rel = sudo(cmd)
if 'is not installed' in pkg_rel or 'is not available' in pkg_rel:
print "Package %s not installed." % pkg
return None
return pkg_rel
def get_pkg_version_release(pkg='contrail-install-packages'):
pkg_rel = None
dist = detect_ostype()
if dist in ['centos', 'fedora', 'redhat', 'centoslinux']:
cmd = "rpm -q --queryformat '%%{VERSION}-%%{RELEASE}\\n' %s" % pkg
elif dist in ['ubuntu']:
cmd = "dpkg-query -W -f='${VERSION}\\n' %s" % pkg
else:
raise Exception("ERROR: Unknown dist (%s)" % dist)
pkg_rel = sudo(cmd)
if pkg_rel.failed or 'is not installed' in pkg_rel or 'is not available' in pkg_rel:
print "Package %s not installed." % pkg
return None
return pkg_rel.split('\r\n') if pkg_rel else pkg_rel
def is_package_installed(pkg_name):
ostype = detect_ostype()
if ostype in ['ubuntu']:
cmd = 'dpkg-query -l "%s" | grep -q ^.i'
elif ostype in ['centos','fedora', 'centoslinux', 'redhat']:
cmd = 'rpm -qi %s '
cmd = cmd % (pkg_name)
with settings(warn_only=True):
result = sudo(cmd)
return result.succeeded
#end is_package_installed
def get_as_sudo(src_file, dst_file):
basename = os.path.basename(src_file.rstrip('/'))
if not basename or basename in ['*']:
raise Exception('%s is not a valid path'%src_file)
tempdir = run('(tempdir=$(mktemp -d); echo $tempdir)')
tmp_file = '%s/tmp%s' % (tempdir, basename)
sudo('cp -rf %s %s'%(src_file, tmp_file))
sudo('chmod -R 777 %s'%tmp_file)
try:
status = get(tmp_file, dst_file)
if os.path.isdir(dst_file):
local('mv %s/tmp%s %s/%s' %(dst_file, basename, dst_file, basename))
except:
sudo('rm -rf %s' % tempdir)
raise
sudo('rm -rf %s' % tempdir)
#end get_as_sudo
def verify_command_succeeded(cmd, expected_output, error_str, max_count,
sleep_interval, warn_only):
count = 1
cmd_str = cmd
if warn_only:
with settings(warn_only=True):
output = sudo(cmd)
else:
output = sudo(cmd)
while not output.succeeded or output != expected_output:
count += 1
if count > max_count:
raise RuntimeError("%s: %s %s" % (error_str, output.succeeded,\
output))
sleep(sleep_interval)
if warn_only:
with settings(warn_only=True):
output = sudo(cmd)
else:
output = sudo(cmd)
#end verify_command_succeeded
# Delete file
def remove_file(file_name):
cmd = 'rm -f ' + file_name
sudo(cmd)
#end remove_file
def get_available_packages(os_type, *packages):
'''Retrieve packages version available in the node'''
os_type = detect_ostype()
pkg_dict = {}
for package in packages:
pkg_dict[package] = []
versions = None
with settings(warn_only=True):
if os_type in ['centos', 'redhat', 'centoslinux', 'fedora']:
versions = sudo("yum list %s | grep %s | awk -F ' ' '{print $2}'" %(package, package))
elif os_type in ['ubuntu']:
versions = sudo('apt-cache show %s | grep Version: | grep -Po "Version: \K.*"' % package)
else:
raise RuntimeError('UnSupported OS Type (%s)' % os_type)
if versions:
if versions.succeeded:
pkg_dict[package] = versions.split('\r\n')
return pkg_dict