summaryrefslogtreecommitdiff
path: root/src/automationstatus/crons.py
blob: da1d59d71d1507f49efda59e5d861ba40b435e2e (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
from .models import DeploymentWorkflow, Summary
from .models import OrchestrationWorkflow
from datetime import datetime
from django.apps import apps
from django.db import connection
from django.core import serializers
from django.utils import timezone


def count():
  queryset = DeploymentWorkflow.objects.all()

  in_progress_clusters = 0
  failed_clusters = 0
  completed_clusters = 0
  ingested = 0
  online = 0
  bmc = 0
  firmware_scheduled = 0
  firmware_upgraded = 0
  wr_scheduled = 0
  wr_installed = 0

  for workflow in queryset:
    if workflow.wr_installed:
      completed_clusters = completed_clusters + 1
    else:
      in_progress_clusters = in_progress_clusters + 1
    # summary.failed = failed_clusters

    if workflow.ingested:
      ingested = ingested + 1

    if workflow.online:
      online = online + 1

    if workflow.bmc:
      bmc = bmc + 1

    if workflow.firmware_scheduled and not workflow.firmware_upgraded:
      firmware_scheduled = firmware_scheduled + 1

    if workflow.firmware_upgraded:
      firmware_upgraded = firmware_upgraded + 1

    if workflow.wr_scheduled and not workflow.wr_installed:
      wr_scheduled = wr_scheduled + 1

    if workflow.wr_installed:
      wr_installed = wr_installed + 1


  summary = Summary()
  summary.clusters = len(queryset)
  summary.in_progress = in_progress_clusters
  summary.completed = completed_clusters
  summary.ingested = ingested
  summary.online = online
  summary.bmc = bmc
  summary.firmware_scheduled = firmware_scheduled
  summary.firmware_upgraded = firmware_upgraded
  summary.wr_scheduled = wr_scheduled
  summary.wr_installed = wr_installed
  summary.created_at = timezone.now()
  print(summary)
  try:
    summary.save()
  except Exception as e:
    print(e)


def summary():
  print("**** running cron: summary **** : {}".format(datetime.now()))
  servers = apps.get_model('caas', 'Server')
  query_set = servers.objects.select_related('cluster').select_related('blade')
  for server in query_set:
      # Set server id and date
      status = DeploymentWorkflow()
      status.cluster = server.cluster
      status.created_at = timezone.now()
      # CIQ ingestion
      status.ingested = True
      # Server online check
      status.online = True if server.blade_id else False
      #BMC done check
      if server.pxe_mac_address and server.intel_nic_firmware_version:
          status.bmc = True
      # Check Firmware schedule and installation status
      # Get server list from
      Server = apps.get_model('caas', 'Server')
      server = Server.objects.get(cluster=server.cluster)
      FirmwareUpgradeSchedule = apps.get_model('caas', 'FirmwareUpgradeSchedule')
      try:
          firmware_schedule = FirmwareUpgradeSchedule.objects.get(server=server)
          # Firmware installation Scheduled and completiong check
          status.firmware_scheduled = True if firmware_schedule.date_scheduled else False
          status.firmware_upgraded = True if firmware_schedule.date_completed else False
      except FirmwareUpgradeSchedule.DoesNotExist:
          status.firmware_schedule = False
          status.firmware_upgraded = False
      except Exception as e:
        print(e)
      
      # Check WindRiver schedule and installation status
      WrInstallSchedule = apps.get_model('caas', 'WrInstallSchedule')
      try:
          wr_schedule = WrInstallSchedule.objects.select_related('cluster').get(cluster=server.cluster)
          # WindRiver installation Scheduled and completiong check
          status.wr_scheduled = True if wr_schedule.date_scheduled else False
          status.wr_installed = True if wr_schedule.date_completed else False
      except WrInstallSchedule.DoesNotExist:
          status.wr_scheduled = False
          status.wr_installed = False
      except Exception as e:
        print(e)
      # Save status object
      status.save()
  count()

def orchestration_summary():
  print("**** running cron: orchestration_summary **** : {}".format(datetime.now()))
  regions = apps.get_model('orchestration', 'remoteregionsetup')
  status = OrchestrationWorkflow()

  # Namespace count and Timestamp
  status.namespaces = regions.objects.count()
  status.created_at = timezone.now()

  # Service accounts
  status.orch_service_account = regions.objects.filter(serviceaccount__in=['orchestration-sa', 'SVC-FE-Atlas']).count()
  status.edge_eng_service_account = regions.objects.filter(serviceaccount='SVC-Edge-Eng').count() + regions.objects.filter(serviceaccount__contains='application-sa-').count()
  status.samsung_service_account = regions.objects.filter(serviceaccount='samsung-sa').count()

  # Orchestration Kubeconfigs
  status.orch_kubeconfig = regions.objects.filter(serviceaccount__in=['orchestration-sa', 'SVC-FE-Atlas']).exclude(kubeconfig='').count()

  # Edge engineering Kubeconfigs
  status.edge_eng_kubeconfig = regions.objects.filter(serviceaccount='SVC-Edge-Eng').exclude(kubeconfig='').count() 
  status.edge_eng_kubeconfig += regions.objects.filter(serviceaccount__contains='application-sa-').exclude(kubeconfig='').count()

  # Samsung Kubeconfigs
  status.samsung_kubeconfig= regions.objects.filter(serviceaccount__in=['orchestration-sa', 'SVC-FE-Atlas']).exclude(kubeconfig='').count()

  # Uncomment and use once Dev completes tracking on his side
  # Setting to 0 for now
  status.data_network_setup = 0

  status.save()