1 # Copyright (C) 2015 <contact@redhat.com>
3 # Author: Alfredo Deza <adeza@redhat.com>
4 # Author: Loic Dachary <loic@dachary.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU Library Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Library Public License for more details.
16 from ceph_detect_init import alpine
17 from ceph_detect_init import arch
18 from ceph_detect_init import centos
19 from ceph_detect_init import debian
20 from ceph_detect_init import exc
21 from ceph_detect_init import fedora
22 from ceph_detect_init import rhel
23 from ceph_detect_init import suse
24 from ceph_detect_init import gentoo
25 from ceph_detect_init import freebsd
26 from ceph_detect_init import docker
27 from ceph_detect_init import oraclevms
33 def get(use_rhceph=False):
34 distro_name, release, codename = platform_information()
35 # Not all distributions have a concept that maps to codenames
36 # (or even releases really)
37 if not codename and not _get_distro(distro_name):
38 raise exc.UnsupportedPlatform(
43 module = _get_distro(distro_name, use_rhceph=use_rhceph)
44 module.name = distro_name
45 module.normalized_name = _normalized_distro_name(distro_name)
46 module.distro = module.normalized_name
47 module.is_el = module.normalized_name in ['redhat', 'centos',
48 'fedora', 'scientific',
50 module.release = release
51 module.codename = codename
52 module.init = module.choose_init()
56 def _get_distro(distro, use_rhceph=False):
60 distro = _normalized_distro_name(distro)
70 'oraclevms': oraclevms,
82 if distro == 'redhat' and use_rhceph:
85 return distributions.get(distro)
88 def _normalized_distro_name(distro):
89 distro = distro.lower()
90 if distro.startswith(('redhat', 'red hat')):
92 elif distro.startswith(('scientific', 'scientific linux')):
94 elif distro.startswith(('suse', 'opensuse')):
96 elif distro.startswith('centos'):
98 elif distro.startswith('oracle linux'):
100 elif distro.startswith('oracle vm'):
102 elif distro.startswith(('gentoo', 'funtoo', 'exherbo')):
104 elif distro.startswith('virtuozzo'):
109 def platform_information():
110 """detect platform information from remote host."""
112 file_name = '/proc/self/cgroup'
113 with open(file_name, 'r') as f:
114 lines = f.readlines()
116 if "docker" in line.split(':')[2]:
117 return ('docker', 'docker', 'docker')
118 except Exception as err:
119 logging.debug("platform_information: ",
120 "Error while opening %s : %s" % (file_name, err))
122 if os.path.isfile('/.dockerenv'):
123 return ('docker', 'docker', 'docker')
125 if platform.system() == 'Linux':
126 linux_distro = platform.linux_distribution(
127 supported_dists=platform._supported_dists + ('alpine', 'arch'))
128 logging.debug('platform_information: linux_distribution = ' +
130 distro, release, codename = linux_distro
131 elif platform.system() == 'FreeBSD':
133 release = platform.release()
134 codename = platform.version().split(' ')[3].split(':')[0]
136 'platform_information: release = {}, version = {}'.format(
137 platform.release(), platform.version()))
139 raise exc.UnsupportedPlatform(platform.system(), '', '')
141 distro_lower = distro.lower()
142 # this could be an empty string in Debian
143 if not codename and 'debian' in distro_lower:
145 # this is an empty string in Oracle
146 elif distro_lower.startswith('oracle linux'):
147 codename = 'OL' + release
148 elif distro_lower.startswith('oracle vm'):
149 codename = 'OVS' + release
150 # this could be an empty string in Virtuozzo linux
151 elif distro_lower.startswith('virtuozzo linux'):
152 codename = 'virtuozzo'
155 str(distro).rstrip(),
156 str(release).rstrip(),
157 str(codename).rstrip()