-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
__init__.py
158 lines (143 loc) · 5.16 KB
/
__init__.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
# Copyright (C) 2015 <contact@redhat.com>
#
# Author: Alfredo Deza <adeza@redhat.com>
# Author: Loic Dachary <loic@dachary.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program 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 Library Public License for more details.
#
from ceph_detect_init import alpine
from ceph_detect_init import arch
from ceph_detect_init import centos
from ceph_detect_init import debian
from ceph_detect_init import exc
from ceph_detect_init import fedora
from ceph_detect_init import rhel
from ceph_detect_init import suse
from ceph_detect_init import gentoo
from ceph_detect_init import freebsd
from ceph_detect_init import docker
from ceph_detect_init import oraclevms
import os
import logging
import platform
def get(use_rhceph=False):
distro_name, release, codename = platform_information()
# Not all distributions have a concept that maps to codenames
# (or even releases really)
if not codename and not _get_distro(distro_name):
raise exc.UnsupportedPlatform(
distro=distro_name,
codename=codename,
release=release)
module = _get_distro(distro_name, use_rhceph=use_rhceph)
module.name = distro_name
module.normalized_name = _normalized_distro_name(distro_name)
module.distro = module.normalized_name
module.is_el = module.normalized_name in ['redhat', 'centos',
'fedora', 'scientific',
'oraclel']
module.release = release
module.codename = codename
module.init = module.choose_init()
return module
def _get_distro(distro, use_rhceph=False):
if not distro:
return
distro = _normalized_distro_name(distro)
distributions = {
'alpine': alpine,
'arch': arch,
'debian': debian,
'ubuntu': debian,
'linuxmint': debian,
'centos': centos,
'scientific': centos,
'oraclel': centos,
'oraclevms': oraclevms,
'redhat': centos,
'fedora': fedora,
'suse': suse,
'gentoo': gentoo,
'funtoo': gentoo,
'exherbo': gentoo,
'freebsd': freebsd,
'docker': docker,
'virtuozzo': centos,
}
if distro == 'redhat' and use_rhceph:
return rhel
else:
return distributions.get(distro)
def _normalized_distro_name(distro):
distro = distro.lower()
if distro.startswith(('redhat', 'red hat')):
return 'redhat'
elif distro.startswith(('scientific', 'scientific linux')):
return 'scientific'
elif distro.startswith(('suse', 'opensuse')):
return 'suse'
elif distro.startswith('centos'):
return 'centos'
elif distro.startswith('oracle linux'):
return 'oraclel'
elif distro.startswith('oracle vm'):
return 'oraclevms'
elif distro.startswith(('gentoo', 'funtoo', 'exherbo')):
return 'gentoo'
elif distro.startswith('virtuozzo'):
return 'virtuozzo'
return distro
def platform_information():
"""detect platform information from remote host."""
try:
file_name = '/proc/self/cgroup'
with open(file_name, 'r') as f:
lines = f.readlines()
for line in lines:
if "docker" in line.split(':')[2]:
return ('docker', 'docker', 'docker')
except Exception as err:
logging.debug("platform_information: ",
"Error while opening %s : %s" % (file_name, err))
if os.path.isfile('/.dockerenv'):
return ('docker', 'docker', 'docker')
if platform.system() == 'Linux':
linux_distro = platform.linux_distribution(
supported_dists=platform._supported_dists + ('alpine', 'arch'))
logging.debug('platform_information: linux_distribution = ' +
str(linux_distro))
distro, release, codename = linux_distro
elif platform.system() == 'FreeBSD':
distro = 'freebsd'
release = platform.release()
codename = platform.version().split(' ')[3].split(':')[0]
logging.debug(
'platform_information: release = {}, version = {}'.format(
platform.release(), platform.version()))
else:
raise exc.UnsupportedPlatform(platform.system(), '', '')
distro_lower = distro.lower()
# this could be an empty string in Debian
if not codename and 'debian' in distro_lower:
pass
# this is an empty string in Oracle
elif distro_lower.startswith('oracle linux'):
codename = 'OL' + release
elif distro_lower.startswith('oracle vm'):
codename = 'OVS' + release
# this could be an empty string in Virtuozzo linux
elif distro_lower.startswith('virtuozzo linux'):
codename = 'virtuozzo'
return (
str(distro).rstrip(),
str(release).rstrip(),
str(codename).rstrip()
)