File: status.py

package info (click to toggle)
lava-coordinator 0.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 244 kB
  • ctags: 114
  • sloc: python: 1,237; xml: 144; sh: 76; makefile: 25
file content (166 lines) | stat: -rwxr-xr-x 5,204 bytes parent folder | download | duplicates (2)
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
#! /usr/bin/python

"""
Status check for lava-coordinator
"""

#  Copyright 2015 Linaro Limited
#  Author Neil Williams <neil.williams@linaro.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, 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 General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.


import os
import sys
import socket
import json
import time
import errno
from socket import gethostname

HOST = 'localhost'       # The coordinator hostname default


def read_settings(filename):
    """
    NodeDispatchers need to use the same port and blocksize as the Coordinator,
    so read the same conffile.
    """
    settings = {"port": 3079,
                "coordinator_hostname": "localhost",
                "blocksize": 4 * 1024}
    if not os.path.exists(filename):
        # unknown as there is no usable configuration
        print "No lava-coordinator configuration file found!"
        sys.exit(3)
    with open(filename) as stream:
        jobdata = stream.read()
        json_default = json.loads(jobdata)
    if "port" in json_default:
        settings['port'] = json_default['port']
    if "blocksize" in json_default:
        settings['blocksize'] = json_default["blocksize"]
    if "coordinator_hostname" in json_default:
        settings['coordinator_hostname'] = json_default['coordinator_hostname']
    return settings

# pylint: disable=too-many-branches,too-many-statements,too-many-locals


def lava_poll(port, host, name, request):
    """
    Modified poll equivalent
    """
    errors = []
    warnings = []
    while True:
        sock = None
        count = 0
        while count < 5:
            try:
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
                sock.connect((host, port))
                break
            except socket.error as exc:
                if exc.errno == errno.ECONNRESET:
                    warnings.append("connection reset by peer: bug 1020")
                errors.append("not connected, sleeping for 1 second")
                time.sleep(1)
                sock = None
                count += 1
                warnings.append("retrying port %s on %s" % (port, host))
        if count >= 5:
            break
        msg = {
            "group_name": "group1",
            "group_size": 2,
            "hostname": gethostname(),
            "role": "client",
            "client_name": name,
            "request": request,
            "message": None
        }
        msg_str = json.dumps(msg)
        msg_len = len(msg_str)
        try:
            # send the length as 32bit hexadecimal
            ret_bytes = sock.send("%08X" % msg_len)
            if ret_bytes == 0:
                warnings.append(
                    "zero bytes sent for length - connection closed?")
                continue
            ret_bytes = sock.send(msg_str)
            if ret_bytes == 0:
                warnings.append(
                    "zero bytes sent for message - connection closed?")
                continue
        except socket.error as exc:
            errors.append("socket error '%d' on send" % exc.message)
            sock.close()
            continue
        try:
            data = str(sock.recv(8))  # 32bit limit
            data = sock.recv(1024)
        except socket.error as exc:
            errors.append("Exception on receive: %s" % exc)
            continue
        try:
            json_data = json.loads(data)
        except ValueError:
            warnings.append("data not JSON %s" % data)
            break
        if 'response' not in json_data:
            errors.append("no response field in data")
            break
        if json_data['response'] != 'wait':
            break
        else:
            break
        sock.shutdown(socket.SHUT_RDWR)
        sock.close()
    ret = 0
    if errors:
        ret = 2
    elif warnings:
        ret = 1
    if errors or warnings:
        print "E:%s W:%s" % (errors, warnings)
        return ret
    else:
        return ret


def main():
    """ Run a simple check on the API
    """
    port = 3079  # The same port as used by the server
    host = 'localhost'
    conffile = "/etc/lava-coordinator/lava-coordinator.conf"
    settings = read_settings(conffile)
    port = settings['port']
    host = settings['coordinator_hostname']
    ret1 = lava_poll(port, host, 'status', 'group_data')
    ret2 = lava_poll(port, host, 'status', 'clear_group')
    if not ret1 and not ret2:
        print "status check complete. No errors"
    if ret1 and ret1 >= ret2:
        sys.exit(ret1)
    if ret2:
        sys.exit(ret2)


if __name__ == '__main__':
    main()