File: r4dcfg

package info (click to toggle)
r4d 1.7-3
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 268 kB
  • sloc: python: 954; sh: 4; makefile: 2
file content (249 lines) | stat: -rwxr-xr-x 8,505 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python3
#
# r4dcfg - config tool for r4d database rack infrastructure control
#
# Copyright (C) 2016  Linutronix GmbH
# Author: Benedikt Spranger <b.spranger@linutronix.de>
#
# This file is part of r4d.
#
# r4d 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 3 of the License, or
# (at your option) any later version.
#
# r4d 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 r4d.  If not, see <http://www.gnu.org/licenses/>.

from r4d.config import R4DdConfig
from r4d.db import R4DDataBase
from r4d.tools import register_modules
from r4d.version import r4d_version

from r4d.powerservice import PowerService
from r4d.serialservice import SerialService
from r4d.boardservice import BoardService
from r4d.rackservice import RackService

import logging
import time
import pysimplesoap

import r4d.power
import r4d.serial

from argparse import ArgumentParser

log = logging.getLogger ("r4dcfg")

if __name__ == "__main__":

    logging.basicConfig ()
    parser = ArgumentParser (description = 'r4d dbconfig and test tool v' + r4d_version)
    parser.add_argument ('-c', '--config',
                         help = 'use configuration CONFIG',
                         default = '/etc/r4d.cfg')

    parser.add_argument ('-v', '--verbose',
                         nargs = '?',
                         help = 'enable verbose messages',
                         const = "INFO")

    group = parser.add_mutually_exclusive_group()

    group.add_argument ('--show-db',
                        action = "store_true",
                        help = 'show DB')

    group.add_argument ('--add-power',
                        action = "store_true",
                        help = 'add a new power switch')

    group.add_argument ('--poweron',
                        action = "store_true",
                        help = 'power board')

    group.add_argument ('--poweroff',
                        action = "store_true",
                        help = 'poweroff board')

    group.add_argument ('--powercycle',
                        action = "store_true",
                        help = 'powercycle board')

    group.add_argument ('--list-power',
                        action = "store_true",
                        help = 'lists configured power switches')

    group.add_argument ('--add-serial',
                         action="store_true",
                         help = 'add a new serial console server')

    group.add_argument ('--list-serial',
                         action="store_true",
                         help = 'lists configured serial console servers')

    group.add_argument ('--add-board',
                        action = "store_true",
                        help = 'add a new board')

    group.add_argument ('--move-board',
                        action = "store_true",
                        help = 'move a board to another rack/slot')

    group.add_argument ('--delete-board',
                        action = "store_true",
                        help = 'delete board from DB')

    group.add_argument ('--list-boards',
                        action = "store_true",
                        help = 'list configured boards')

    group.add_argument ('--add-rack',
                        action = "store_true",
                        help = 'add a new rack')

    group.add_argument ('--list-racks',
                        action = "store_true",
                        help = 'list configured racks')


    args, extra = parser.parse_known_args ()

    loglevel = None
    if args.verbose:
        loglevel = getattr (logging, args.verbose.upper (), None)

    config = R4DdConfig (configname = args.config)
    db = R4DDataBase (config)
    power = PowerService (db, None)
    serial = SerialService (db, None)
    board = BoardService (db, None)
    rack = RackService (db, None)

    if args.list_power:
        r = power.list_power ()
        if (r['result'] == -1):
            log.error(" failed to get list of power controls")
            exit (-1)
        for p in r['list']:
            print ('ID: %d\tHost: %s\tModel: %s\tPorts: %d' % (
                               p['id'], p['host'], p['model'], len(p['ports'])))
        exit (0)

    if args.list_serial:
        r = serial.list_serial ()
        if (r['result'] == -1):
            log.error(" failed to get list of power controls")
            exit (-1)
        for s in r['list']:
            print ('ID: %d\tHost: %s\tModel: %s\tPorts: %d' % (
                               s['id'], s['host'], s['model'], len(s['ports'])))
        exit (0)

    if args.list_boards:
        r = board.list_boards ()
        if (r['result'] == -1):
            log.error(" failed to get list of boards")
            exit (-1)
        for s in r['list']:
            try:
                console = serial.get_serial (s['name'])
                powerstate = power.status(s['name'])
            except pysimplesoap.server.SoapFault as e:
                # rack without serial and/or power server
                console = { 'host': 'unknown', 'port': -1 }
                powerstate = -1
            print ('ID[%d]: %s\tName: %s\tRack: %s\tPort: %d\tserial: %s:%d' % (
                       powerstate, s['uuid'], s['name'],
                       s['rack'], s['port'], console['host'], console['port']))
        exit (0)

    if args.list_racks:
        rl = rack.list_racks ()
        for r in rl:
            print ('ID: %s\tName: %s\tLocation: %s' % (
                r['id'], r['name'], r['location']))
        exit (0)

    if args.poweron:
        parser.add_argument ("name", type = str, help = "board name")
        eargs = parser.parse_args (extra)
        ret = power.change (eargs.name, 1)
        if not ret['state']:
            log.error(" powerctl failed")
            exit (-1)
        exit (0)

    if args.poweroff:
        parser.add_argument ("name", type = str, help = "board name")
        eargs = parser.parse_args (extra)
        ret = power.change (eargs.name, 0)
        if not ret['state']:
            log.error(" powerctl failed")
            exit (-1)
        exit (0)

    if args.powercycle:
        parser.add_argument ("name", type = str, help = "board name")
        eargs = parser.parse_args (extra)
        ret = power.change (eargs.name, 0)
        if not ret['state']:
            log.error(" powerctl failed")
            exit (-1)
        time.sleep (5)
        ret = power.change (eargs.name, 1)
        if not ret['state']:
            log.error(" powerctl failed")
            exit (-1)
        exit (0)

    if args.show_db:
        db.print_config ()
        exit (0)

    if args.add_rack:
        parser.add_argument ("name", type = str, help = "rack name")
        parser.add_argument ("location", type = str, help = "rack location")
        eargs = parser.parse_args (extra)
        rack.add_rack (eargs.name, eargs.location)
        exit (0)

    if args.delete_board:
        parser.add_argument ("name", type = str, help = "board name")
        eargs = parser.parse_args (extra)
        board.delete_board (eargs.name)
        exit (0)

    parser.add_argument ("rack", type = str, help = "rack name")

    if args.add_board:
        parser.add_argument ("slot", type = int, help = "slot position in rack")
        parser.add_argument ("name", type = str, help = "board name")
        eargs = parser.parse_args (extra)
        board.add_board (eargs.name, eargs.rack, eargs.slot)
        exit (0)

    if args.move_board:
        parser.add_argument ("slot", type = int, help = "destination slot in rack")
        parser.add_argument ("name", type = str, help = "board name")
        eargs = parser.parse_args (extra)
        board.move_board (eargs.name, eargs.rack, eargs.slot)
        exit (0)

    parser.add_argument ("model", type = str, help = "model name")
    parser.add_argument ("host", type = str, help = "hostname or IP")
    eargs = parser.parse_args (extra)

    if args.add_power:
        power.add_power (eargs.model, eargs.host, eargs.rack)

    if args.add_serial:
        serial.add_serial (eargs.model, eargs.host, eargs.rack)

    exit (0)