summaryrefslogtreecommitdiff
path: root/sbhs/management/commands/update_boards.py
blob: 849f02029e640483b0d0b4b92feb3599a107fba2 (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
'''
   This command creates a moderator group and adds users to the moderator group
   with permissions to add, change and delete
   the objects in the exam app.
'''

# django imports
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import User, Group, Permission

# local imports
from django.conf import settings
from sbhs.models import Board
from sbhs.views import map_sbhs_to_rpi


class Command(BaseCommand):
    help = 'Ping all boards and update status of boards'

    def handle(self, *args, **options):
        app_label = 'sbhs'
        if settings.SBHS_API_IPS:
            try:
                board_check, dead_servers = map_sbhs_to_rpi()
                board = Board()
                all_mac_ids = []
                for machines in board_check:
                    all_mac_ids.extend(machines["mac_ids"])
                board.switch_off_inactive_boards(all_mac_ids)
                self.stdout.write('Updated Board Status')
                if dead_servers:
                    self.stdout.write('Servers {0} are not responding.'\
                                       .format(", ".join(dead_servers))
                                       )
            except Exception as e:
                self.stdout.write('Failed updating Board because {0}'\
                                  .format(e)
                                  )
        else:
            self.stdout.write('No API IP added in settings.py. '
                "   Please try with adding IPs in the SBHS_API_IPS variable "
                )