1 | # Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu> |
---|
2 | # |
---|
3 | # This program is free software: you can redistribute it and/or modify it under |
---|
4 | # the terms of the GNU Affero General Public License as published by the |
---|
5 | # Free Software Foundation; either version 3 of the License, or |
---|
6 | # (at your option) any later version. |
---|
7 | |
---|
8 | import argparse |
---|
9 | import re |
---|
10 | |
---|
11 | from cli.utils import print_json |
---|
12 | |
---|
13 | |
---|
14 | class OgDisk(): |
---|
15 | |
---|
16 | @staticmethod |
---|
17 | def list_disks(rest, args): |
---|
18 | parser = argparse.ArgumentParser() |
---|
19 | parser.add_argument('--client-ip', |
---|
20 | nargs='?', |
---|
21 | required=True, |
---|
22 | help='Client IP to query') |
---|
23 | parsed_args = parser.parse_args(args) |
---|
24 | payload = {'client': [parsed_args.client_ip]} |
---|
25 | |
---|
26 | r = rest.get('/client/setup', payload=payload) |
---|
27 | print_json(r.text) |
---|
28 | |
---|
29 | @staticmethod |
---|
30 | def setup_disk(rest, args): |
---|
31 | def parse_size(size): |
---|
32 | size = size.upper() |
---|
33 | units = {"M": 10**3, "G": 10**6, "T": 10**9} # Mapped to K |
---|
34 | # size = re.sub(r'(\d+)([MGT])', r'\1 \2', size) |
---|
35 | match = re.match(r'(\d+)([MGT])', size) |
---|
36 | if match: |
---|
37 | if len(match.groups()) == 2: |
---|
38 | number, unit = match.groups() |
---|
39 | return str(int(float(number)*units[unit])) |
---|
40 | print(f'Error parsing size {size}. Aborting.') |
---|
41 | return None |
---|
42 | |
---|
43 | disk_type_map = {'dos': 'MSDOS', 'gpt': 'GPT'} |
---|
44 | part_types = ['LINUX', 'EFI', 'WINDOWS', 'CACHE'] |
---|
45 | fs_types = ['EXT4', 'FAT32', 'NTFS', 'CACHE'] |
---|
46 | |
---|
47 | parser = argparse.ArgumentParser() |
---|
48 | parser.add_argument('--type', |
---|
49 | nargs='?', |
---|
50 | required=True, |
---|
51 | choices=['dos', 'gpt'], |
---|
52 | help='Disk partition scheme') |
---|
53 | parser.add_argument('--num', |
---|
54 | nargs='?', |
---|
55 | default=1, |
---|
56 | help='Disk number (defaults to 1)') |
---|
57 | parser.add_argument('--format', |
---|
58 | nargs='?', |
---|
59 | const=True, |
---|
60 | type=lambda x: x.split(','), |
---|
61 | help='Indicates which partitions to reformat if they are already present. ' |
---|
62 | 'Use --part alone to mean all partitions.') |
---|
63 | parser.add_argument('--part', |
---|
64 | nargs='+', |
---|
65 | action='append', |
---|
66 | type=lambda x: x.split(','), |
---|
67 | required=True, |
---|
68 | help='Partition definition (syntax: "num,part_scheme,fs,size")') |
---|
69 | group = parser.add_argument_group('clients', 'Client selection args') |
---|
70 | group.add_argument('--center-id', |
---|
71 | type=int, |
---|
72 | action='append', |
---|
73 | default=[], |
---|
74 | required=False, |
---|
75 | help='Clients from given center id') |
---|
76 | group.add_argument('--room-id', |
---|
77 | type=int, |
---|
78 | action='append', |
---|
79 | default=[], |
---|
80 | required=False, |
---|
81 | help='Clients from given room id') |
---|
82 | group.add_argument('--client-ip', |
---|
83 | action='append', |
---|
84 | default=[], |
---|
85 | required=False, |
---|
86 | help='Specific client IP') |
---|
87 | |
---|
88 | parsed_args = parser.parse_args(args) |
---|
89 | r = rest.get('/scopes') |
---|
90 | scopes = r.json() |
---|
91 | ips = set() |
---|
92 | |
---|
93 | for center in parsed_args.center_id: |
---|
94 | center_scope = scope_lookup(center, 'center', scopes) |
---|
95 | ips.update(ips_in_scope(center_scope)) |
---|
96 | for room in parsed_args.room_id: |
---|
97 | room_scope = scope_lookup(room, 'room', scopes) |
---|
98 | ips.update(ips_in_scope(room_scope)) |
---|
99 | for l in parsed_args.client_ip: |
---|
100 | ips.add(l) |
---|
101 | if not ips: |
---|
102 | print("No clients found") |
---|
103 | return None |
---|
104 | |
---|
105 | payload = {'clients': parsed_args.client_ip, 'type': disk_type_map[parsed_args.type], 'disk': str(parsed_args.num), |
---|
106 | 'cache': '0', 'cache_size': '0', 'partition_setup': []} |
---|
107 | for i, p in enumerate(parsed_args.part, start=1): |
---|
108 | p = p[0] |
---|
109 | part_num, code, fs, size = p[0], p[1].upper(), p[2].upper(), p[3] |
---|
110 | |
---|
111 | if code not in part_types: |
---|
112 | print( |
---|
113 | f'Specified partition type {code} is not supported. Aborting...') |
---|
114 | return |
---|
115 | if fs not in fs_types: |
---|
116 | print( |
---|
117 | f'Specified filesystem {code} is not supported. Aborting...') |
---|
118 | return |
---|
119 | size = parse_size(size) |
---|
120 | |
---|
121 | for j in range(i, int(part_num)): |
---|
122 | part = {'partition': str(j), 'code': 'EMPTY', |
---|
123 | 'filesystem': 'EMPTY', 'size': '0', |
---|
124 | 'format': '0'} |
---|
125 | payload['partition_setup'].append(part) |
---|
126 | |
---|
127 | if parsed_args.format is True or (type(parsed_args.format) == list and part_num in parsed_args.format): |
---|
128 | do_format = '1' |
---|
129 | else: |
---|
130 | do_format = '0' |
---|
131 | |
---|
132 | if fs == 'CACHE': |
---|
133 | # Assuming flag specifying if there's cache in the setup |
---|
134 | payload['cache'] = '1' |
---|
135 | payload['cache_size'] = size |
---|
136 | part = {'partition': str(p[0]), 'code': code.upper(), |
---|
137 | 'filesystem': fs.upper(), 'size': size, |
---|
138 | 'format': do_format} |
---|
139 | payload['partition_setup'].append(part) |
---|
140 | |
---|
141 | last_partnum = int(parsed_args.part[-1][0][0]) |
---|
142 | # Pad with empty partitions if no 4th part was defined |
---|
143 | for i in range(last_partnum + 1, 5): |
---|
144 | part = {'partition': str(i), 'code': 'EMPTY', |
---|
145 | 'filesystem': 'EMPTY', 'size': '0', |
---|
146 | 'format': '0'} |
---|
147 | payload['partition_setup'].append(part) |
---|
148 | |
---|
149 | rest.post('/setup', payload=payload) |
---|