summaryrefslogtreecommitdiffstats
path: root/ogcp/views.py
blob: a0d3e440826afc35ea7109c7c31e6f07198cbe98 (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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
# 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.

from flask import (
    g, render_template, url_for, flash, redirect, request, jsonify, make_response
)
from ogcp.forms.action_forms import (
    WOLForm, SetupForm, ClientDetailsForm, ImageDetailsForm, HardwareForm,
    SessionForm, ImageRestoreForm, ImageCreateForm, SoftwareForm, BootModeForm,
    RoomForm, DeleteRoomForm, CenterForm, DeleteCenterForm, OgliveForm,
    GenericForm, SelectClientForm, ImageUpdateForm, ImportClientsForm
)
from flask_login import (
    current_user, LoginManager,
    login_user, logout_user,
    login_required
)

from pathlib import Path

from ogcp.models import User
from ogcp.forms.auth import LoginForm
from ogcp.og_server import OGServer
from flask_babel import lazy_gettext as _l
from flask_babel import _
from ogcp import app
import requests
import datetime
import re

FS_CODES = {
    0: 'DISK',
    1: 'EMPTY',
    2: 'CACHE',
    6: 'EXT4',
    9: 'FAT32',
    13: 'NTFS',
    18: 'EXFAT',
    19: 'LINUX-SWAP'
}

PART_TYPE_CODES = {
    0: 'EMPTY',
    1: 'DISK',
    5: 'EXTENDED',
    7: 'NTFS',
    11: 'FAT32',
    23: 'HNTFS',
    27: 'HFAT32',
    39: 'HNTFS-WINRE',
    130: 'LINUX-SWAP',
    131: 'LINUX',
    142: 'LINUX-LVM',
    202: 'CACHE',
    218: 'DATA',
    253: 'LINUX-RAID',
    1792: 'NTFS',
    9984: 'WIN-RECOV',
    33280: 'LINUX-SWAP',
    33536: 'LINUX',
    36352: 'LINUX-LVM',
    51712: 'CACHE',
    61184: 'EFI',
    64768: 'LINUX-RAID',
    65535: 'UNKNOWN'
}

PART_SCHEME_CODES = {
    0: 'EMPTY',
    1: 'MSDOS',
    2: 'GPT'
}

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'

def validate_elements(elements, min_len=1, max_len=float('inf')):
    valid = True
    if len(elements) < min_len:
        flash(_('Please, select at least {} element(s)').format(min_len),
              category='error')
        valid = not valid
    elif len(elements) > max_len:
        flash(_('No more than {} element(s) can be selected for the given action').format(max_len),
              category='error')
        valid = not valid
    return valid

def parse_elements(checkboxes_dict):
    elements = set()
    for key, elements_list in checkboxes_dict.items():
        if key != 'csrf_token':
            elements.update(elements_list.split(' '))
    return elements

def get_client_setup(ip):
    payload = {'client': [ip]}
    r = g.server.get('/client/setup', payload)
    db_partitions = r.json()['partitions']
    for partition in db_partitions:
        if partition['partition'] == 0:
            partition['code'] = PART_SCHEME_CODES.get(partition['code'], 'MSDOS')
        else:
            partition['code'] = PART_TYPE_CODES.get(partition['code'], 'EMPTY')

        partition['filesystem'] = FS_CODES.get(partition['filesystem'], 'EMPTY')

    return db_partitions

def get_clients(state_filter=None):
    r = g.server.get('/clients')
    clients = r.json()
    if state_filter:
        return filter(clients.items(), lambda c: c.state == state_filter)
    return clients

def parse_scopes_from_tree(tree, scope_type):
    scopes = []
    for scope in tree['scope']:
        if scope['type'] == scope_type:
            if 'name' in tree:
                scope['parent'] = tree['name']
            scopes.append(scope)
        else:
            scopes += parse_scopes_from_tree(scope, scope_type)
    return scopes

def add_state_and_ips(scope, clients, ips):
    scope['selected'] = False
    if 'ip' in scope:
        filtered_client = filter(lambda x: x['addr']==scope['ip'], clients)
        client = next(filtered_client, False)
        if client:
            scope['state'] = client['state']
        else:
            scope['state'] = 'off'
        scope['ip'] = [scope['ip']]
        scope['selected'] = set(scope['ip']).issubset(ips)
    else:
        scope['ip'] = []
        for child in scope['scope']:
            scope['ip'] += add_state_and_ips(child, clients, ips)
            scope['selected'] = set(scope['ip']).issubset(ips)
    return scope['ip']

def get_allowed_scopes(scopes, allowed_scopes):
    for scope in scopes.get('scope'):
        if scope.get('name') in current_user.scopes:
            allowed_scopes.append(scope)
        else:
            get_allowed_scopes(scope, allowed_scopes)

def get_scopes(ips=set()):
    r = g.server.get('/scopes')
    scopes = r.json()
    if current_user.scopes:
        allowed_scopes = []
        get_allowed_scopes(scopes, allowed_scopes)
        scopes = {'scope': allowed_scopes}
    r = g.server.get('/clients')
    clients = r.json()
    add_state_and_ips(scopes, clients['clients'], ips)

    return scopes, clients

def authenticate_user(username, pwd):
    for user in app.config['USERS']:
        if user.get("USER") == username:
            if user.get("PASS") == pwd:
                return user
            else:
                flash(_('Incorrect password'))
                return None
    flash(_('Incorrect user name'))
    return None

def get_user(username):
    for user in app.config['USERS']:
        if user.get("USER") == username:
            return user
    return None


intervals = (
    (_l('days'), 86400),    # 60 * 60 * 24
    (_l('hours'), 3600),    # 60 * 60
    (_l('minutes'), 60),
)


def display_time(seconds):
    result = []

    for name, count in intervals:
        value = seconds // count
        if value:
            seconds -= value * count
            result.append("{} {}".format(value, name))
    return ', '.join(result)


@login_manager.user_loader
def load_user(username):
    user_dict = get_user(username)
    if not user_dict:
        return None

    user = User(username, user_dict.get('SCOPES'))
    return user

@app.before_request
def load_config():
    g.server = OGServer()

@app.errorhandler(404)
def page_not_found(error):
    return render_template('error.html', message=error), 404

@app.errorhandler(500)
def server_error(error):
    return render_template('error.html', message=error), 500

def image_modified_date_from_str(image):
    return datetime.datetime.strptime(image['modified'], '%a %b %d %H:%M:%S %Y')

@app.route('/')
def index():
    if not current_user.is_authenticated:
        return redirect(url_for('login'))

    try:
        clients = get_clients()
    except requests.exceptions.RequestException as err:
        flash(_('ogServer connection failed: {}.').format(err),
              category='error')
        logout_user()
        return redirect(url_for('index'))
    images_response = g.server.get('/images')
    images = images_response.json()['images']
    images.sort(key=image_modified_date_from_str, reverse=True)
    disk = images_response.json()['disk']
    oglive_list = g.server.get('/oglive/list').json()
    stats = g.server.get('/stats').json()
    timestamp = datetime.datetime.fromtimestamp(stats.get('time').get('now'))
    now = timestamp.strftime('%Y-%m-%d  %H:%M:%S')
    boot = display_time(stats.get('time').get('boot'))
    start = display_time(stats.get('time').get('start'))
    time_dict = {'now': now, 'boot': boot, 'start': start}
    return render_template('dashboard.html', clients=clients,
                           images=images, disk=disk, colsize="6",
                           oglive_list=oglive_list, stats=stats,
                           time_dict=time_dict)

@app.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm(request.form)
    if request.method == 'POST' and form.validate():
        form_user = request.form['user']
        pwd = request.form['pwd']
        user_dict = authenticate_user(form_user, pwd)
        if not user_dict:
            return render_template('auth/login.html', form=form)
        user = User(form_user, user_dict.get('SCOPES'))
        login_user(user)
        return redirect(url_for('index'))
    return render_template('auth/login.html', form=LoginForm())

@app.route("/logout")
@login_required
def logout():
    logout_user()
    return redirect(url_for('index'))

@app.route('/scopes/status')
@login_required
def scopes_status():
    scopes, _clients = get_scopes()
    return jsonify(scopes)

@app.route('/scopes/')
@login_required
def scopes():
    scopes, clients = get_scopes()
    return render_template('scopes.html', scopes=scopes, clients=clients)

@app.route('/action/poweroff', methods=['GET', 'POST'])
@login_required
def action_poweroff():
    form = GenericForm(request.form)
    if request.method == 'POST':
        ips = form.ips.data.split(' ')
        if not validate_elements(ips):
            return redirect(url_for('commands'))

        payload = {'clients': ips}
        r = g.server.post('/poweroff', payload)
        if r.status_code != requests.codes.ok:
            flash(_('ogServer: error powering off client'),
                  category='error')
        else:
            flash(_('Client powered off successfully'),
                  category='info')
        return redirect(url_for('commands'))
    else:
        ips = parse_elements(request.args.to_dict())
        form.ips.data = " ".join(ips)
        if validate_elements(ips):
            scopes, clients = get_scopes(set(ips))
            selected_clients = list(get_selected_clients(scopes['scope']).items())
            return render_template('actions/poweroff.html', form=form,
                                   selected_clients=selected_clients,
                                   scopes=scopes)
        else:
            return redirect(url_for('commands'))


@app.route('/action/wol', methods=['GET', 'POST'])
@login_required
def action_wol():
    form = WOLForm(request.form)
    if request.method == 'POST' and form.validate():
        wol_type = form.wol_type.data
        ips = form.ips.data.split(' ')
        payload = {'type': wol_type, 'clients': ips}
        g.server.post('/wol', payload)
        flash(_('Wake On Lan request sent successfully'), category='info')
        return redirect(url_for('commands'))
    else:
        ips = parse_elements(request.args.to_dict())
        form.ips.data = " ".join(ips)
        if validate_elements(ips, min_len=1):
            scopes, clients = get_scopes(set(ips))
            selected_clients = list(get_selected_clients(scopes['scope']).items())
            return render_template('actions/wol.html', form=form,
                                   selected_clients=selected_clients,
                                   scopes=scopes)
        else:
            return redirect(url_for('commands'))


@app.route('/action/setup/select', methods=['GET'])
@login_required
def action_setup_select():
    args = request.args.copy()

    ips = parse_elements(args.to_dict())
    if not validate_elements(ips):
        return redirect(url_for('commands'))

    if len(ips) == 1:
        ip = list(ips)[0]
        return redirect(url_for('action_setup_show', ip=ip))

    form = SelectClientForm()
    form.ips.data = " ".join(ips)
    form.selected_client.choices = list(ips)

    scopes, _ = get_scopes(ips)
    selected_clients = list(get_selected_clients(scopes['scope']).items())

    return render_template('actions/select_client.html',
                           selected_clients=selected_clients,
                           form=form, scopes=scopes)


@app.route('/action/setup', methods=['GET'])
@login_required
def action_setup_show():
    args = request.args.copy()

    default_disk = 1
    selected_disk = int(args.pop('disk', default_disk))

    if args.get('ip'):
        ips = {args['ip']}
        ips_str = base_client = args['ip']
    else:
        ips_str = args['ips']
        ips = set(args['ips'].split(' '))
        base_client = args['selected_client']

    db_partitions = get_client_setup(base_client)
    filtered_partitions = [p for p in db_partitions
                           if p.get('disk') == selected_disk]

    disk_partition = 0
    disks = [d.get('disk') for d in db_partitions
             if d.get('partition') == disk_partition]

    form = SetupForm()
    form.ips.data = ips_str
    form.disk.data = selected_disk
    # If partition table is empty, set MSDOS
    form.disk_type.data = filtered_partitions[0]['code'] or 1

    disk_size = filtered_partitions[0]['size'] // 1024

    # Make form.partition length equal to (filtered_partitions - 1) length
    diff = len(filtered_partitions) - 1 - len(form.partitions)
    [form.partitions.append_entry() for _ in range(diff)]

    for partition, db_part in zip(form.partitions, filtered_partitions[1:]):
        partition.partition.data = str(db_part['partition'])
        partition.part_type.data = db_part['code']
        partition.fs.data = db_part['filesystem']
        partition.size.data = db_part['size'] // 1024
    scopes, _clients = get_scopes(ips)
    return render_template('actions/setup.html',
                           selected_disk=selected_disk,
                           disks=disks,
                           form=form,
                           disk_size=disk_size,
                           ips=ips_str,
                           base_client=base_client,
                           scopes=scopes)

@app.route('/action/setup', methods=['POST'])
@login_required
def action_setup_modify():
    form = SetupForm(request.form)
    if form.validate():
        ips = form.ips.data.split(' ')

        payload = {'clients': ips,
                   'disk': str(form.disk.data),
                   'type': str(form.disk_type.data),
                   'cache': str(0),
                   'cache_size': str(0),
                   'partition_setup': []}

        required_partitions = ["1", "2", "3", "4"]
        for partition in form.partitions:
            print(partition)
            partition_setup = {'partition': str(partition.partition.data),
                               'code': str(partition.part_type.data),
                               'filesystem': str(partition.fs.data),
                               'size': str(partition.size.data * 1024),
                               'format': str(int(partition.format_partition.data))}
            payload['partition_setup'].append(partition_setup)
            if partition.partition.data in required_partitions:
                required_partitions.remove(partition.partition.data)
            if partition.part_type.data == 'CACHE':
                payload['cache'] = '1'
                payload['cache_size'] = str(partition.size.data * 1024)

        for partition in required_partitions:
            empty_part = {
                'partition': partition,
                'code': 'EMPTY',
                'filesystem': 'EMPTY',
                'size': '0',
                'format': '0',
            }
            payload['partition_setup'].append(empty_part)

        r = g.server.post('/setup', payload=payload)
        if r.status_code == requests.codes.ok:
            return redirect(url_for('commands'))
    flash(_(f'Invalid setup form'), category='error')
    return redirect(url_for('commands'))

def search_image(images_list, image_id):
    for image in images_list:
        if image['id'] == image_id:
            return image
    return False

@app.route('/action/image/restore', methods=['GET', 'POST'])
@login_required
def action_image_restore():
    form = ImageRestoreForm(request.form)
    if request.method == 'POST':
        ips = form.ips.data.split(' ')
        disk, partition = form.partition.data.split(' ')
        image_id = form.image.data
        r = g.server.get('/images')
        images_list = r.json()['images']
        image = search_image(images_list, int(image_id))
        if not image:
            flash(_(f'Image to restore was not found'), category='error')
            return redirect(url_for('commands'))

        payload = {'disk': disk,
                   'partition': partition,
                   'name': image['name'],
                   'repository': g.server.ip,
                   'clients': ips,
                   'type': form.method.data,
                   'profile': str(image['software_id']),
                   'id': str(image['id'])}
        g.server.post('/image/restore', payload)
        if r.status_code == requests.codes.ok:
            flash(_(f'Image restore command sent sucessfully'), category='info')
        else:
            flash(_(f'There was a problem sending the image restore command'), category='error')
        return redirect(url_for('commands'))
    else:
        ips = parse_elements(request.args.to_dict())
        if not validate_elements(ips):
            return redirect(url_for('commands'))
        form.ips.data = ' '.join(ips)

        part_choices = []

        r = g.server.get('/images')
        for image in r.json()['images']:
            form.image.choices.append((image['id'], image['name']))

        for ip in ips:
            r = g.server.get('/client/setup', payload={'client': [ip]})
            if r.status_code == requests.codes.ok:
                partitions = r.json()['partitions']
                parts = []
                for partition in partitions:
                    disk_id = partition['disk']
                    part_id = partition['partition']
                    if part_id == 0:  # This is the disk data, not a partition.
                        continue

                    choice_value = (disk_id, part_id)
                    parts.append(choice_value)

                if not part_choices:  # Use first computer as reference part setup conf
                    part_choices = [part for part in parts]
                elif part_choices != parts:
                    flash(_(f'Computers have different partition setup'), category='error')
                    return redirect(url_for('commands'))

            else:
                flash(_('ogServer was unable to obtain setup of selected computer {}').format(ip), category='error')
                return redirect(url_for('commands'))

        form.partition.choices = [ (f'{disk_id} {part_id}', _('Disk: {} | Part: {}').format(disk_id, part_id))
                                    for disk_id, part_id in part_choices ]

        scopes, clients = get_scopes(set(ips))
        selected_clients = list(get_selected_clients(scopes['scope']).items())

        return render_template('actions/image_restore.html', form=form,
                               selected_clients=selected_clients,
                               scopes=scopes)

@app.route('/action/hardware', methods=['GET', 'POST'])
@login_required
def action_hardware():
    form = HardwareForm(request.form)
    if request.method == 'POST':
        ips = form.ips.data.split(' ')
        r = g.server.post('/hardware', payload={'clients': ips})
        if r.status_code == requests.codes.ok:
            flash(_(f'Hardware inventory command has been sent'), category='info')
        else:
            flash(_(f'There was a problem sending the hardware inventory command'), category='error')
        return redirect(url_for('commands'))
    else:
        ips = parse_elements(request.args.to_dict())
        scopes, _clients = get_scopes(ips)
        if not validate_elements(ips, max_len=1):
            return redirect(url_for('commands'))

        form.ips.data = ' '.join(ips)
        r = g.server.get('/hardware', payload={'client': list(ips)})
        hardware = r.json()['hardware']
        return render_template('actions/hardware.html', form=form,
                               hardware=hardware, scopes=scopes)

@app.route('/action/software', methods=['GET', 'POST'])
@login_required
def action_software():
    form = SoftwareForm(request.form)
    if request.method == 'POST':
        ips = form.ips.data.split(' ')
        disk, partition = form.os.data.split(' ')
        if form.view.data:
            r = g.server.get('/software', payload={'client': ips,
                                               'disk': int(disk),
                                               'partition': int(partition)})
            if r.status_code == requests.codes.ok:
                software = r.json()['software']
                scopes, clients = get_scopes(set(ips))
                return render_template('actions/software_list.html',
                                       software=software, form=form, scopes=scopes)
        elif form.update.data:
            r = g.server.post('/software', payload={'clients': ips,
                                                    'disk': disk,
                                                    'partition': partition})
            if r.status_code == requests.codes.ok:
                flash(_('Software profile request sent successfully'), category='info')
            else:
                flash(_('Error processing software profile request: ({})').format(r.status), category='error')
        else:
            flash(_('Error processing software profile form'), category='error')
        return redirect(url_for('commands'))
    else:
        ips = parse_elements(request.args.to_dict())
        scopes, clients = get_scopes(set(ips))
        if not validate_elements(ips, max_len=1):
            return redirect(url_for('commands'))

        form.ips.data = ' '.join(ips)
        r = g.server.get('/client/setup', payload={'client': list(ips)})

        for part in r.json()['partitions'][1:]:
            form.os.choices.append(
                (f"{part.get('disk')} {part.get('partition')}",
                 f"Disco {part.get('disk')} | Partición {part.get('partition')} "
                 f"| {PART_TYPE_CODES[part.get('code')]} "
                 f"{FS_CODES[part.get('filesystem')]}")
            )
    return render_template('actions/software.html', form=form, scopes=scopes)

@app.route('/action/session', methods=['GET', 'POST'])
@login_required
def action_session():
    form = SessionForm(request.form)
    if request.method == 'POST':
        ips = form.ips.data.split(' ')
        disk, partition = form.os.data.split(' ')
        r = g.server.post('/session', payload={'clients': ips,
                                               'disk': str(disk),
                                               'partition': str(partition)})
        if r.status_code == requests.codes.ok:
            return redirect(url_for('commands'))
        return make_response("400 Bad Request", 400)
    else:
        ips = parse_elements(request.args.to_dict())
        if not validate_elements(ips, max_len=1):
            return redirect(url_for('commands'))

        form.ips.data = ' '.join(ips)
        r = g.server.get('/session', payload={'client': list(ips)})
        sessions = r.json()['sessions']
        for os in sessions:
            choice = (f"{os['disk']} {os['partition']}",
                      f"{os['name']} ({os['disk']},{os['partition']})")
            form.os.choices.append(choice)
        scopes, clients = get_scopes(set(ips))
        selected_clients = list(get_selected_clients(scopes['scope']).items())
        return render_template('actions/session.html', form=form,
                               selected_clients=selected_clients,
                               scopes=scopes)

@app.route('/action/client/info', methods=['GET'])
@login_required
def action_client_info():
    form = ClientDetailsForm()
    ips = parse_elements(request.args.to_dict())
    if not validate_elements(ips, max_len=1):
        return redirect(url_for('commands'))

    payload = {'client': list(ips)}
    r = g.server.get('/client/info', payload)
    db_client = r.json()

    form.name.data = db_client['name']
    form.ip.data = db_client['ip']
    form.mac.data = db_client['mac']
    form.serial_number.data = db_client['serial_number']
    form.netmask.data = db_client['netmask']
    form.livedir.data = db_client['livedir']
    form.remote.data = db_client['remote']
    form.maintenance.data = db_client['maintenance']
    form.netiface.data = db_client['netiface']
    form.netdriver.data = db_client['netdriver']
    form.repo.data = db_client['repo_id']
    form.room.data = db_client['room']
    form.boot.data = db_client['boot']

    r = g.server.get('/oglive/list')
    available_oglives = r.json()['oglive']
    for oglive in available_oglives:
        choice = (oglive.get('directory'), oglive.get('directory'))
        form.livedir.choices.append(choice)

    r = g.server.get('/mode')
    available_modes = [(mode, mode) for mode in r.json()['modes']]
    form.boot.choices = list(available_modes)

    r = g.server.get('/scopes')
    rooms = parse_scopes_from_tree(r.json(), 'room')
    rooms = [(room['id'], room['name']) for room in rooms]
    form.room.choices = list(rooms)

    form.create.render_kw = {"style": "visibility:hidden;"}

    r = g.server.get('/images')
    images = r.json()['images']

    ip = list(ips)[0]
    setup = get_client_setup(ip)
    if setup and setup[0].get('code') == 'MSDOS':
        setup[0]['code'] = 'MBR'

    for entry in setup:
        if images and entry['image'] != 0:
            image = next(img for img in images if img['id'] == entry['image'])
            entry['image'] = image['name']
        else:
            entry['image'] = ""

    scopes, clients = get_scopes(set(ips))

    return render_template('actions/client_details.html', form=form,
                           parent="commands.html", scopes=scopes, setup=setup)

@app.route('/action/client/add', methods=['GET', 'POST'])
@login_required
def action_client_add():
    form = ClientDetailsForm(request.form)
    if request.method == 'POST':
        payload = {"boot": form.boot.data,
                   "ip": form.ip.data,
                   "livedir": form.livedir.data,
                   "mac": form.mac.data,
                   "maintenance": form.maintenance.data,
                   "name": form.name.data,
                   "netdriver": form.netdriver.data,
                   "netiface": form.netiface.data,
                   "netmask": form.netmask.data,
                   "remote": form.remote.data,
                   "repo_id": int(form.repo.data),
                   "room": int(form.room.data),
                   "serial_number": form.serial_number.data}

        r = g.server.post('/client/add', payload)
        if r.status_code != requests.codes.ok:
            flash(_('ogServer: error adding client'),
                  category='error')
        else:
            flash(_('Client added successfully'), category='info')
        return redirect(url_for("scopes"))
    else:
        r = g.server.get('/mode')
        available_modes = [(mode, mode) for mode in r.json()['modes']]
        form.boot.choices = list(available_modes)

        r = g.server.get('/scopes')
        rooms = parse_scopes_from_tree(r.json(), 'room')
        rooms = [(room['id'], room['name']) for room in rooms]
        form.room.choices = list(rooms)

        form.create.render_kw = {"formaction": url_for('action_client_add')}
        scopes, clients = get_scopes()
        return render_template('actions/client_details.html', form=form,
                               parent="scopes.html", scopes=scopes)

@app.route('/action/clients/import', methods=['GET'])
@login_required
def action_clients_import_get():
    form = ImportClientsForm()
    r = g.server.get('/scopes')
    rooms = parse_scopes_from_tree(r.json(), 'room')
    rooms = [(room['id'], room['name'] + " (" + room['parent'] + ")")
             for room in rooms]
    form.room.choices = list(rooms)
    scopes, _clients = get_scopes()
    return render_template('actions/import_clients.html', form=form,
                           scopes=scopes)


OG_REGEX_DHCPD_CONF = (r'(?: *host *)'
                       r'([\w.-]*)'
                       r'(?: *{ *hardware *ethernet *)'
                       r'((?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2}))'
                       r'(?: *; *fixed-address *)'
                       r'(\d+\.\d+\.\d+\.\d+)'
                       r'(?: *; *})')
OG_CLIENT_DEFAULT_BOOT = "pxe"
OG_CLIENT_DEFAULT_LIVEDIR = "ogLive"
OG_CLIENT_DEFAULT_MAINTENANCE = False
OG_CLIENT_DEFAULT_NETDRIVER = "generic"
OG_CLIENT_DEFAULT_NETIFACE = "eth0"
OG_CLIENT_DEFAULT_NETMASK = "255.255.255.0"
OG_CLIENT_DEFAULT_REMOTE = False


@app.route('/action/clients/import', methods=['POST'])
@login_required
def action_clients_import_post():
    form = ImportClientsForm(request.form)
    clients = re.findall(OG_REGEX_DHCPD_CONF, form.dhcpd_conf.data)
    if not clients:
        flash(_('No clients found. Check the dhcpd.conf file.'),
              category='error')
        return redirect(url_for('scopes'))
    payload = {'boot': OG_CLIENT_DEFAULT_BOOT,
               'livedir': OG_CLIENT_DEFAULT_LIVEDIR,
               'maintenance': OG_CLIENT_DEFAULT_MAINTENANCE,
               'netdriver': OG_CLIENT_DEFAULT_NETDRIVER,
               'netiface': OG_CLIENT_DEFAULT_NETIFACE,
               'netmask': OG_CLIENT_DEFAULT_NETMASK,
               'remote': OG_CLIENT_DEFAULT_REMOTE,
               'room': int(form.room.data)}
    for client in clients:
        payload['name'] = client[0]
        payload['mac'] = client[1].replace(':', '')
        payload['ip'] = client[2]
        resp = g.server.post('/client/add', payload)
        if resp.status_code != requests.codes.ok:
            flash(_('ogServer: error adding client {}').format(client[0]),
                  category='error')
            return redirect(url_for('scopes'))
    flash(_('Clients imported successfully'), category='info')
    return redirect(url_for('scopes'))


def get_selected_clients(scopes):
    selected_clients = dict()

    for scope in scopes:
        scope_type = scope.get('type')
        selected = scope.get('selected')
        if ((scope_type == 'computer') and selected):
            name_id = scope.get('name') + '_' + str(scope.get('id'))
            selected_clients[name_id] = scope.get('ip')[0]
        else:
            selected_clients.update(get_selected_clients(scope['scope']))

    return selected_clients

@app.route('/action/client/delete', methods=['GET', 'POST'])
@login_required
def action_client_delete():
    form = GenericForm(request.form)
    if request.method == 'POST':
        ips = form.ips.data.split(' ')
        if not validate_elements(ips):
            return redirect(url_for('scopes'))

        payload = {'clients': ips}
        r = g.server.post('/client/delete', payload)
        if r.status_code != requests.codes.ok:
            flash(_('ogServer: error deleting client'),
                  category='error')
        else:
            flash(_('Client deleted successfully'),
                  category='info')
        return redirect(url_for('scopes'))
    else:
        ips = parse_elements(request.args.to_dict())
        form.ips.data = " ".join(ips)
        if validate_elements(ips):
            scopes, clients = get_scopes(set(ips))
            selected_clients = list(get_selected_clients(scopes['scope']).items())
            return render_template('actions/delete_client.html', form=form,
                                   selected_clients=selected_clients,
                                   scopes=scopes)
        else:
            return redirect(url_for('scopes'))

@app.route('/action/mode', methods=['GET', 'POST'])
@login_required
def action_mode():
    form = BootModeForm(request.form)
    if request.method == 'POST':
        ips = form.ips.data.split(' ')
        payload = { 'clients': ips, 'mode': form.boot.data }
        print(payload)
        r = g.server.post('/mode', payload)
        if r.status_code == requests.codes.ok:
            flash(_('Client set boot mode request sent successfully'), category='info')
        else:
            flash(_('Ogserver replied with status code not ok'), category='error')
        return redirect(url_for('commands'))

    else:
        r = g.server.get('/mode')
        available_modes = [(mode, mode) for mode in r.json()['modes']]
        form.boot.choices = list(available_modes)

        ips = parse_elements(request.args.to_dict())
        form.ips.data = " ".join(ips)
        if not validate_elements(ips):
            return redirect(url_for('commands'))

        form.ok.render_kw = { 'formaction': url_for('action_mode') }
        scopes, clients = get_scopes(set(ips))
        selected_clients = list(get_selected_clients(scopes['scope']).items())
        return render_template('actions/mode.html', form=form, scopes=scopes,
                               selected_clients=selected_clients,
                               clients=clients)


@app.route('/action/oglive', methods=['GET', 'POST'])
@login_required
def action_oglive():
    form = OgliveForm(request.form)
    if request.method == 'POST':
        ips = form.ips.data.split(' ')
        payload = {'clients': ips, 'name': form.oglive.data}
        r = g.server.post('/oglive/set', payload)
        if r.status_code == requests.codes.ok:
            flash(_('Client set ogLive request sent successfully'),
                  category='info')
        else:
            flash(_('Ogserver replied with status code not ok'),
                  category='error')
        return redirect(url_for('commands'))

    else:
        r = g.server.get('/oglive/list')
        if r.status_code != requests.codes.ok:
            flash(_('Ogserver replied with status code not ok'),
                  category='error')
            return redirect(url_for('commands'))
        available_oglives = [(oglive.get('directory'), oglive.get('directory'))
                             for oglive in r.json()['oglive']]
        available_oglives.insert(0, ('default', 'default'))
        form.oglive.choices = list(available_oglives)

        ips = parse_elements(request.args.to_dict())
        form.ips.data = " ".join(ips)
        if not validate_elements(ips):
            return redirect(url_for('commands'))

        form.ok.render_kw = {'formaction': url_for('action_oglive')}
        scopes, clients = get_scopes(set(ips))
        selected_clients = list(get_selected_clients(scopes['scope']).items())
        return render_template('actions/oglive.html', form=form, scopes=scopes,
                               selected_clients=selected_clients)


@app.route('/action/image/create', methods=['GET', 'POST'])
@login_required
def action_image_create():
    form = ImageCreateForm(request.form)
    if request.method == 'POST':
        ip = form.ip.data
        r = g.server.get('/client/info', payload={"client": [ip]})
        disk, partition, code = form.os.data.split(' ')
        payload = {"clients": [ip],
                   "disk": disk,
                   "partition": partition,
                   "code": code,
                   "name": form.name.data,
                   "repository": g.server.ip,
                   "id": "0", # This is ignored by the server.
                   "description": form.description.data,
                   "group_id": 0, # Default group.
                   "center_id": r.json()["center"]}
        r = g.server.post('/image/create', payload)
        if r.status_code == requests.codes.ok:
            return redirect(url_for('commands'))
        return make_response("400 Bad Request", 400)
    else:
        ips = parse_elements(request.args.to_dict())
        form.ip.data = " ".join(ips)
        if not validate_elements(ips, max_len=1):
            return redirect(url_for('commands'))

        r = g.server.get('/client/setup', payload={'client': list(ips)})
        for partition in r.json()['partitions']:
            disk_id = partition['disk']
            part_id = partition['partition']
            fs_id = partition['filesystem']
            code = partition['code']

            if part_id == 0:
                # This is the disk data, not a partition.
                continue

            choice_value = f"{disk_id} {part_id} {code}"
            choice_name = (f"{_('Disk')} {disk_id} | "
                           f"{_('Partition')} {part_id} | "
                           f"{_('FS')} {FS_CODES[fs_id]}")
            form.os.choices.append((choice_value, choice_name))
        scopes, clients = get_scopes(set(ips))
        return render_template('actions/image_create.html', form=form,
                               scopes=scopes)


@app.route('/action/image/update', methods=['GET', 'POST'])
@login_required
def action_image_update():
    form = ImageUpdateForm(request.form)
    if request.method == 'POST':
        ip = form.ip.data
        disk, partition, code = form.os.data.split(' ')
        image_id = form.image.data
        r = g.server.get('/images')
        images_list = r.json()['images']
        image = search_image(images_list, int(image_id))
        if not image:
            flash(_('Image to restore was not found'), category='error')
            return redirect(url_for('commands'))
        payload = {'clients': [ip],
                   'disk': disk,
                   'partition': partition,
                   'code': code,
                   'name': image['name'],
                   'repository': g.server.ip,
                   'id': str(image['id']),
                   # Dummy parameters, not used by ogServer on image update.
                   'group_id': 0,
                   'center_id': 0}
        r = g.server.post('/image/create', payload)
        if r.status_code == requests.codes.ok:
            flash(_('Image update command sent sucessfully'), category='info')
        else:
            flash(_('There was a problem sending the image update command'),
                  category='error')
        return redirect(url_for('commands'))

    ips = parse_elements(request.args.to_dict())
    if not validate_elements(ips, max_len=1):
        return redirect(url_for('commands'))
    form.ip.data = ' '.join(ips)

    r = g.server.get('/images')
    for image in r.json()['images']:
        form.image.choices.append((image['id'], image['name']))

    r = g.server.get('/client/setup', payload={'client': list(ips)})
    for partition in r.json()['partitions']:
        disk_id = partition['disk']
        part_id = partition['partition']
        fs_id = partition['filesystem']
        code = partition['code']

        if part_id == 0:
            # This is the disk data, not a partition.
            continue

        choice_value = f'{disk_id} {part_id} {code}'
        choice_name = (f"{_('Disk')} {disk_id} | "
                       f"{_('Partition')} {part_id} | "
                       f"{_('FS')} {FS_CODES[fs_id]}")
        form.os.choices.append((choice_value, choice_name))

    scopes, _clients = get_scopes(set(ips))
    selected_clients = list(get_selected_clients(scopes['scope']).items())

    return render_template('actions/image_update.html', form=form,
                           selected_clients=selected_clients,
                           scopes=scopes)


@app.route('/action/reboot', methods=['GET', 'POST'])
@login_required
def action_reboot():
    form = GenericForm(request.form)
    if request.method == 'POST':
        ips = form.ips.data.split(' ')
        if not validate_elements(ips):
            return redirect(url_for('commands'))

        payload = {'clients': ips}
        r = g.server.post('/reboot', payload)
        if r.status_code != requests.codes.ok:
            flash(_('ogServer: error rebooting client'),
                  category='error')
        else:
            flash(_('Client rebooted successfully'),
                  category='info')
        return redirect(url_for('commands'))
    else:
        ips = parse_elements(request.args.to_dict())
        form.ips.data = " ".join(ips)
        if validate_elements(ips):
            scopes, clients = get_scopes(set(ips))
            selected_clients = list(get_selected_clients(scopes['scope']).items())
            return render_template('actions/reboot.html', form=form,
                                   selected_clients=selected_clients,
                                   scopes=scopes)
        else:
            return redirect(url_for('commands'))


@app.route('/action/refresh', methods=['POST'])
@login_required
def action_refresh():
    ips = parse_elements(request.form.to_dict())
    if not validate_elements(ips):
        return redirect(url_for('commands'))

    payload = {'clients': list(ips)}
    r = g.server.post('/refresh', payload)
    if r.status_code != requests.codes.ok:
        flash(_('OgServer replied with a non ok status code'), category='error')
    else:
        flash(_('Refresh request processed successfully'), category='info')
    return redirect(url_for('commands'))

@app.route('/action/center/add', methods=['GET', 'POST'])
@login_required
def action_center_add():
    form = CenterForm(request.form)
    if request.method == 'POST':
        payload = {"name": form.name.data,
                   "comment": form.comment.data}
        r = g.server.post('/center/add', payload)
        if r.status_code != requests.codes.ok:
            flash(_('Server replied with error code when adding the center'),
                  category='error')
        else:
            flash(_('Center added successfully'), category='info')
        return redirect(url_for("scopes"))
    else:
        scopes, clients = get_scopes()
        return render_template('actions/add_center.html', form=form,
                               scopes=scopes)

@app.route('/action/center/delete', methods=['GET', 'POST'])
@login_required
def action_center_delete():
    form = DeleteCenterForm(request.form)
    if request.method == 'POST':
        payload = {"id": form.center.data}
        r = g.server.post('/center/delete', payload)
        if r.status_code != requests.codes.ok:
            flash(_('Server replied with error code when deleting the center'),
                  category='error')
        else:
            flash(_('Center deleted successfully'), category='info')
        return redirect(url_for("scopes"))
    else:
        r = g.server.get('/scopes')
        centers = parse_scopes_from_tree(r.json(), 'center')
        centers = [(center['id'], center['name']) for center in centers]
        form.center.choices = list(centers)
        scopes, clients = get_scopes()
        return render_template('actions/delete_center.html', form=form,
                               scopes=scopes)

@app.route('/action/room/add', methods=['GET', 'POST'])
@login_required
def action_room_add():
    form = RoomForm(request.form)
    if request.method == 'POST':
        payload = {"center": int(form.center.data),
                   "name": form.name.data,
                   "netmask": form.netmask.data}
        r = g.server.post('/room/add', payload)
        if r.status_code != requests.codes.ok:
            flash(_('Server replied with error code when adding the room'), category='error')
        else:
            flash(_('Room added successfully'), category='info')
        return redirect(url_for("scopes"))
    else:
        r = g.server.get('/scopes')
        centers = parse_scopes_from_tree(r.json(), 'center')
        centers = [(center['id'], center['name']) for center in centers]
        form.center.choices = list(centers)
        scopes, clients = get_scopes()
        return render_template('actions/add_room.html', form=form,
                               scopes=scopes)

@app.route('/action/room/delete', methods=['GET', 'POST'])
@login_required
def action_room_delete():
    form = DeleteRoomForm(request.form)
    if request.method == 'POST':
        payload = {"id": form.room.data}
        r = g.server.post('/room/delete', payload)
        if r.status_code != requests.codes.ok:
            flash(_('Server replied with error code when deleting the room'),
                  category='error')
        else:
            flash(_('Room deleted successfully'), category='info')
        return redirect(url_for("scopes"))
    else:
        r = g.server.get('/scopes')
        rooms = parse_scopes_from_tree(r.json(), 'room')
        rooms = [(room['id'], room['name'] + " (" + room['parent'] + ")")
                 for room in rooms]
        form.room.choices = list(rooms)
        scopes, clients = get_scopes()
        return render_template('actions/delete_room.html', form=form,
                               scopes=scopes)

@app.route('/commands/', methods=['GET'])
@login_required
def commands():
    scopes, clients = get_scopes()
    return render_template('commands.html', scopes=scopes, clients=clients)

@app.route('/images/', methods=['GET'])
@login_required
def images():
    r = g.server.get('/images')
    images = r.json()['images']
    return render_template('images.html', images=images)

@app.route('/action/image/info', methods=['GET'])
@login_required
def action_image_info():
    form = ImageDetailsForm()
    ids = parse_elements(request.args.to_dict())
    if not validate_elements(ids, max_len=1):
        return redirect(url_for('images'))

    id = ids.pop()
    r = g.server.get('/images')
    images = r.json()['images']
    image = next(img for img in images if img['id'] == int(id))

    form.id.data = image['id']
    form.name.data = image['name']
    # Bytes to Gibibytes
    form.size.data = image['size'] / 1024 ** 3
    form.datasize.data = image['datasize'] / 1024 ** 3
    form.modified.data = image['modified']
    form.permissions.data = image['permissions']
    form.software_id.data = image['software_id']

    images = g.server.get('/images').json()['images']

    return render_template('actions/image_details.html', form=form,
                           images=images)

@app.route('/action/image/delete', methods=['GET', 'POST'])
@login_required
def action_image_delete():
    form = GenericForm(request.form)
    if request.method == 'POST':
        ids = form.ids.data.split(' ')
        if not validate_elements(ids, max_len=1):
            return redirect(url_for('images'))
        id = ids.pop()
        payload = {'image': id}
        r = g.server.post('/image/delete', payload)
        if r.status_code != requests.codes.ok:
            flash(_('OgServer replied with a non ok status code'), category='error')
        else:
            flash(_('Image deletion request sent successfully'), category='info')
        return redirect(url_for('images'))
    else:
        images = [(name, imgid) for name, imgid in request.args.to_dict().items() if name != "csrf_token"]
        if not validate_elements(images, max_len=1):
            return redirect(url_for('images'))
        image_name, image_id = images[0]
        r = g.server.get('/images')
        form.ids.data = image_id
        if not validate_elements(images, max_len=1):
            flash(_('Please select one image to delete'), category='error')
            return redirect(url_for('images'))
        return render_template('actions/delete_image.html', form=form,
                               image_name=image_name.split('_', 1)[0], image_id=image_id,
                               images=r.json()['images'])

@app.route('/action/log', methods=['GET'])
@login_required
def action_legacy_log():
    ips = parse_elements(request.args.to_dict())
    if not validate_elements(ips, max_len=1):
        return redirect(url_for('commands'))
    ip = ips.pop()
    log_file = Path("/opt/opengnsys/log/clients/" + str(ip) + ".log")
    log = log_file.read_text()
    if log:
        scopes, clients = get_scopes(set(ips))
        return render_template('actions/legacy/log.html', log=log,
                               scopes=scopes)
    else:
        return redirect(url_for('commands'))

@app.route('/action/rt-log', methods=['GET'])
@login_required
def action_legacy_rt_log():
    ips = parse_elements(request.args.to_dict())
    if not validate_elements(ips, max_len=1):
        return redirect(url_for('commands'))
    ip = ips.pop()
    scheme = "http://"
    rt_log_path = "/cgi-bin/httpd-log.sh"
    rt_log_url = scheme + ip + rt_log_path
    return redirect(rt_log_url)