Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / ceph-detect-init / ceph_detect_init / __init__.py
1 # Copyright (C) 2015 <contact@redhat.com>
2 #
3 # Author: Alfredo Deza <adeza@redhat.com>
4 # Author: Loic Dachary <loic@dachary.org>
5 #
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)
9 # any later version.
10 #
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.
15 #
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
28 import os
29 import logging
30 import platform
31
32
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(
39             distro=distro_name,
40             codename=codename,
41             release=release)
42
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',
49                                               'oraclel']
50     module.release = release
51     module.codename = codename
52     module.init = module.choose_init()
53     return module
54
55
56 def _get_distro(distro, use_rhceph=False):
57     if not distro:
58         return
59
60     distro = _normalized_distro_name(distro)
61     distributions = {
62         'alpine': alpine,
63         'arch': arch,
64         'debian': debian,
65         'ubuntu': debian,
66         'linuxmint': debian,
67         'centos': centos,
68         'scientific': centos,
69         'oraclel': centos,
70         'oraclevms': oraclevms,
71         'redhat': centos,
72         'fedora': fedora,
73         'suse': suse,
74         'gentoo': gentoo,
75         'funtoo': gentoo,
76         'exherbo': gentoo,
77         'freebsd': freebsd,
78         'docker': docker,
79         'virtuozzo': centos,
80     }
81
82     if distro == 'redhat' and use_rhceph:
83         return rhel
84     else:
85         return distributions.get(distro)
86
87
88 def _normalized_distro_name(distro):
89     distro = distro.lower()
90     if distro.startswith(('redhat', 'red hat')):
91         return 'redhat'
92     elif distro.startswith(('scientific', 'scientific linux')):
93         return 'scientific'
94     elif distro.startswith(('suse', 'opensuse')):
95         return 'suse'
96     elif distro.startswith('centos'):
97         return 'centos'
98     elif distro.startswith('oracle linux'):
99         return 'oraclel'
100     elif distro.startswith('oracle vm'):
101         return 'oraclevms'
102     elif distro.startswith(('gentoo', 'funtoo', 'exherbo')):
103         return 'gentoo'
104     elif distro.startswith('virtuozzo'):
105         return 'virtuozzo'
106     return distro
107
108
109 def platform_information():
110     """detect platform information from remote host."""
111     try:
112         file_name = '/proc/self/cgroup'
113         with open(file_name, 'r') as f:
114             lines = f.readlines()
115             for line in lines:
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))
121
122     if os.path.isfile('/.dockerenv'):
123         return ('docker', 'docker', 'docker')
124
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 = ' +
129                       str(linux_distro))
130         distro, release, codename = linux_distro
131     elif platform.system() == 'FreeBSD':
132         distro = 'freebsd'
133         release = platform.release()
134         codename = platform.version().split(' ')[3].split(':')[0]
135         logging.debug(
136             'platform_information: release = {}, version = {}'.format(
137                 platform.release(), platform.version()))
138     else:
139         raise exc.UnsupportedPlatform(platform.system(), '', '')
140
141     distro_lower = distro.lower()
142     # this could be an empty string in Debian
143     if not codename and 'debian' in distro_lower:
144         pass
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'
153
154     return (
155         str(distro).rstrip(),
156         str(release).rstrip(),
157         str(codename).rstrip()
158     )