Development Override Compose File
[laas.git] / src / jenkins / tasks.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 from celery import shared_task
12
13 from dashboard.models import Resource
14 from jenkins.models import JenkinsSlave, JenkinsStatistic
15 from .adapter import *
16
17
18 @shared_task
19 def sync_jenkins():
20     update_jenkins_slaves()
21
22
23 def update_jenkins_slaves():
24     JenkinsSlave.objects.all().update(active=False)
25
26     jenkins_slaves = get_all_slaves()
27     for slave in jenkins_slaves:
28         jenkins_slave, created = JenkinsSlave.objects.get_or_create(name=slave['displayName'],
29                                                                     url=get_slave_url(slave))
30         jenkins_slave.active = True
31         jenkins_slave.ci_slave = is_ci_slave(slave['displayName'])
32         jenkins_slave.dev_pod = is_dev_pod(slave['displayName'])
33         jenkins_slave.status = get_slave_status(slave)
34
35         # if this is a new slave and a pod, check if there is a resource for it, create one if not
36         if  created and 'pod' in slave['displayName']:
37             # parse resource name from slave name
38             # naming example: orange-pod1, resource name: Orange POD 1
39             tokens = slave['displayName'].split('-')
40             name = tokens[0].capitalize() + ' POD '# company name
41             name += tokens[1][3:] # remove 'pod'
42             resource, created = Resource.objects.get_or_create(name=name)
43             resource.slave = jenkins_slave
44             resource.save()
45
46         last_job = get_jenkins_job(jenkins_slave.name)
47         if last_job is not None:
48             last_job = parse_job(last_job)
49             jenkins_slave.last_job_name = last_job['name']
50             jenkins_slave.last_job_url = last_job['url']
51             jenkins_slave.last_job_scenario = last_job['scenario']
52             jenkins_slave.last_job_branch = last_job['branch']
53             jenkins_slave.last_job_installer = last_job['installer']
54             jenkins_slave.last_job_result = last_job['result']
55         jenkins_slave.save()
56
57         jenkins_statistic = JenkinsStatistic(slave=jenkins_slave)
58         if jenkins_slave.status == 'online' or jenkins_slave.status == 'building':
59             jenkins_statistic.online = True
60         if jenkins_slave.status == 'offline':
61             jenkins_statistic.offline = True
62         if jenkins_slave.status == 'online / idle':
63             jenkins_statistic.idle = True
64         jenkins_statistic.save()