-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChallenge1.py
More file actions
67 lines (45 loc) · 1.73 KB
/
Challenge1.py
File metadata and controls
67 lines (45 loc) · 1.73 KB
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
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 28 07:50:25 2016
@author: efeariaroo
"""
import os
import sys
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from jenkinsapi.jenkins import Jenkins
from models import JenkinsJobsStatus
jenkins_instance = Jenkins('https://jenkins.qa.ubuntu.com/api/python/')
MAX_JENKINS_JOBS = 10
dbpath = 'sqlite:///jenkins_jobs_statuses.db'
Session = sessionmaker()
def initialize_table():
engine = create_engine(dbpath)
JenkinsJobsStatus.Base.metadata.create_all(engine)
Session.configure(bind=engine)
if engine.dialect.has_table(engine, JenkinsJobsStatus.JobStatus.__tablename__):
print(JenkinsJobsStatus.JobStatus.__tablename__ + " table EXISTS.\n")
else:
print(JenkinsJobsStatus.JobStatus.__tablename__ + " table does NOT EXIST.\n")
def get_jobs_names ():
jobs_names_list = jenkins_instance.keys()
if len(jobs_names_list) > MAX_JENKINS_JOBS:
jobs_names_list = jobs_names_list[0:MAX_JENKINS_JOBS]
return jobs_names_list
def start():
initialize_table()
try:
jobs_names_list = get_jobs_names()
session = Session()
for job_name in jobs_names_list:
jenkins_job = jenkins_instance.get_job(job_name)
job_last_build = jenkins_job.get_last_build()
print("Job name: " + job_name + ", Build number: " + str(job_last_build.buildno) \
+ ", status: " + job_last_build.get_status())
a_new_job_status = JenkinsJobsStatus.JobStatus(job_name, job_last_build.get_status())
session.add(a_new_job_status)
session.commit()
except:
print("Unexpected error:" + sys.exc_info()[0] + "\n")
if __name__ == '__main__':
start()