summaryrefslogtreecommitdiffstats
path: root/compass-tasks-base/actions/health_check/check_os_installer.py
blob: 6ef98180c668831c781eaf3024036b13407903a3 (plain)
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
# Copyright 2014 Huawei Technologies Co. Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Compass Health Check module for OS Installer."""

import os
import xmlrpclib

from compass.actions.health_check import base


class OsInstallerCheck(base.BaseCheck):
    """os installer health check."""
    NAME = "OS Installer Check"

    def run(self):
        """do health check."""
        method_name = 'self.' + self.os_installer['name'] + '_check()'
        return eval(method_name)

    def cobbler_check(self):
        """Runs cobbler check from xmlrpc client."""
        try:
            remote = xmlrpclib.Server(
                self.os_installer['cobbler_url'],
                allow_none=True)
            credentials = self.os_installer['credentials']
            token = remote.login(
                credentials['username'], credentials['password'])
        except Exception:
            self.code = 0
            self.messages.append(
                "[%s]Error: Cannot login to Cobbler with "
                "the tokens provided in the config file"
                % self.NAME)
            self.messages.append(
                "[%s]Error: Failed to connect to Cobbler "
                "API, please check if /etc/cobbler/setting "
                "is properly configured" % self.NAME)
            return (self.code, self.messages)

        check_result = remote.check(token)

        for index, message in enumerate(check_result):
            if "SELinux" in message:
                check_result.pop(index)

        if len(check_result) != 0:
            self.code = 0
            for error_msg in check_result:
                self.messages.append("[%s]Error: " % self.NAME + error_msg)

        if len(remote.get_distros()) == 0:
            self._set_status(0,
                             "[%s]Error: No Cobbler distros found" % self.NAME)

        if len(remote.get_profiles()) == 0:
            self._set_status(0,
                             "[%s]Error: No Cobbler profiles found"
                             % self.NAME)

        found_ppa = False
        if len(remote.get_repos()) != 0:
            for repo in remote.get_repos():
                if 'ppa_repo' in repo['mirror']:
                    found_ppa = True
                    break

        if found_ppa is False:
            self._set_status(0,
                             "[%s]Error: No repository ppa_repo found"
                             % self.NAME)

        path_map = {
            'match_kickstart': (
                '/var/lib/cobbler/kickstarts/',
                ['default.ks', 'default.seed']
            ),
            'match_snippets': (
                '/var/lib/cobbler/snippets/',
                [
                    'kickstart_done',
                    'kickstart_start',
                    'kickstart_pre_partition_disks',
                    'kickstart_partition_disks',
                    'kickstart_pre_anamon',
                    'kickstart_post_anamon',
                    'kickstart_pre_install_network_config',
                    'kickstart_network_config',
                    'kickstart_post_install_network_config',
                    'kickstart_chef',
                    'kickstart_ntp',
                    'kickstart_yum_repo_config',
                    'preseed_pre_partition_disks',
                    'preseed_partition_disks',
                    'preseed_pre_anamon',
                    'preseed_post_anamon',
                    'preseed_pre_install_network_config',
                    'preseed_network_config',
                    'preseed_post_install_network_config',
                    'preseed_chef',
                    'preseed_ntp',
                    'preseed_apt_repo_config',
                ]
            ),
            'match_ks_mirror': (
                '/var/www/cobbler/',
                ['ks_mirror']
            ),
            'match_repo_mirror': (
                '/var/www/cobbler/',
                ['repo_mirror']
            ),
            'match_iso': (
                '/var/lib/cobbler/',
                ['iso']
            ),
        }
        not_exists = []
        for key in path_map.keys():
            for path in path_map[key][1]:
                if not os.path.exists(path_map[key][0] + path):
                    not_exists.append(path_map[key][0] + path)

        if len(not_exists) != 0:
            self._set_status(
                0,
                "[%s]Error: These locations do not exist: "
                "%s" % (
                    self.NAME,
                    ', '.join(item for item in not_exists)
                )
            )

        if self.code == 1:
            self.messages.append(
                "[%s]Info: OS Installer health check has completed."
                " No problems found, all systems go." % self.NAME)

        return (self.code, self.messages)