summaryrefslogtreecommitdiffstats
path: root/cli/objects/client.py
blob: 58f8094715364e0f85961d1da757adcae5d07d97 (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
# Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the
# Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.

import argparse
import requests

from cli.utils import print_json, check_address, check_mac_address

class OgClient():

    @staticmethod
    def list_clients(rest):
        r = rest.get('/clients')
        print_json(r.text)

    @staticmethod
    def list_client_hardware(rest, args):
        parser = argparse.ArgumentParser(prog='ogcli list hardware')
        parser.add_argument('--client-ip',
                            nargs=1,
                            type=str,
                            required=True,
                            help='client IP')
        parsed_args = parser.parse_args(args)

        payload = {'client': parsed_args.client_ip}
        r = rest.get('/hardware', payload=payload)
        print_json(r.text)

    @staticmethod
    def get_client_properties(rest, args):
        parser = argparse.ArgumentParser(prog='ogcli list client')
        parser.add_argument('--client-ip',
                            nargs=1,
                            required=True,
                            help='client IP')
        parsed_args = parser.parse_args(args)

        payload = {'client': parsed_args.client_ip}
        r = rest.get('/client/info', payload=payload)
        print_json(r.text)

    @staticmethod
    def send_refresh(rest, args):
        parser = argparse.ArgumentParser(prog='ogcli send refresh')
        parser.add_argument('--client-ip',
                            action='append',
                            default=[],
                            required=True,
                            help='Client IP')
        parsed_args = parser.parse_args(args)

        payload = {'clients': parsed_args.client_ip}
        rest.post('/refresh', payload=payload)

    @staticmethod
    def add_client(rest, args):
        parser = argparse.ArgumentParser(prog='ogcli add client')
        parser.add_argument('--repo-id',
                    type=int,
                    nargs='?',
                    required=True,
                    help='specify the id of the repo')
        parser.add_argument('--netmask',
                    nargs='?',
                    required=True,
                    help='specify netmask address')
        parser.add_argument('--room-id',
                    nargs='?',
                    type=int,
                    required=True,
                    help='specify the id of the room where the client is')
        parser.add_argument('--hostname',
                    nargs='?',
                    required=True,
                    help='give a name to the client')
        parser.add_argument('--mac',
                    nargs='?',
                    required=True,
                    help='mac address should have this format xx:xx:xx:xx:xx:xx')
        parser.add_argument('--ip',
                    nargs='?',
                    required=True,
                    help='specify the ip address of the client')
        r = rest.get('/mode')
        boot_choices = r.json()['modes']
        parser.add_argument('--boot-mode',
                    nargs='?',
                    required=True,
                    choices=boot_choices,
                    help='set a valid boot mode for the client')
        parsed_args = parser.parse_args(args)

        err = False
        if not check_address(parsed_args.netmask):
            print('netmask address is not valid', file=sys.stderr)
            err = True
        if not check_address(parsed_args.ip):
            print('client\'s ip address is not valid', file=sys.stderr)
            err = True
        if not check_mac_address(parsed_args.mac):
            print('client\'s mac address is not valid', file=sys.stderr)
            err = True
        if err:
            parser.print_help(file=sys.stderr)
            sys.exit(1)
        payload = {
            'name': parsed_args.hostname,
            'netmask': parsed_args.netmask,
            'ip': parsed_args.ip,
            'mac': parsed_args.mac.replace(":", ""),
            'repo_id': parsed_args.repo_id,
            'room': parsed_args.room_id,
            'boot': parsed_args.boot_mode,
            'netiface': 'eth0',
            'netdriver': 'generic',
            'livedir': 'ogLive'
        }
        rest.post('/client/add', payload=payload)

    @staticmethod
    def delete_client(rest, args):
        parser = argparse.ArgumentParser(prog='ogcli delete client')
        parser.add_argument('--ip',
                            action='append',
                            default=[],
                            required=True,
                            help='Client IP')
        parsed_args = parser.parse_args(args)
        payload = {
            'clients': parsed_args.ip,
        }
        rest.post('/client/delete', payload=payload)