Merge lp:~linaro-infrastructure/linaro-ci-dashboard/create_job_jenkins_changes into lp:linaro-ci-dashboard

Proposed by Deepti B. Kalakeri
Status: Merged
Approved by: Stevan Radaković
Approved revision: 16
Merged at revision: 13
Proposed branch: lp:~linaro-infrastructure/linaro-ci-dashboard/create_job_jenkins_changes
Merge into: lp:linaro-ci-dashboard
Diff against target: 181 lines (+111/-3)
6 files modified
config_template/sample-job/config.xml (+22/-0)
dashboard/frontend/fixtures/initial_data.json (+9/-0)
dashboard/frontend/models/loop.py (+2/-1)
dashboard/jenkinsserver/models/jenkins_server.py (+48/-2)
dashboard/jenkinsserver/tests/test_jenkins_server.py (+27/-0)
dashboard/settings.py (+3/-0)
To merge this branch: bzr merge lp:~linaro-infrastructure/linaro-ci-dashboard/create_job_jenkins_changes
Reviewer Review Type Date Requested Status
Stevan Radaković Approve
Данило Шеган Pending
Review via email: mp+115802@code.launchpad.net

Description of the change

Adding changes to create /delete job on jenkins server

To post a comment you must log in.
15. By Deepti B. Kalakeri

Fix to check if the values are Null

Revision history for this message
Stevan Radaković (stevanr) wrote :

Hey Deepti, thanks for the good work..
Comments following:

initial_data.json is missing the jenkinsserver record as we talked. It is in my branch as well so you can leave it as it is, but someone else trying to run the application will be confused.

Also, create job method breaks when the name of the method has 'whitespace' in it. It creates job on jenkins, but only with the first string. I guess some escaping needs to be done here.

Basically we can't consider this done until we have a build actually working on the jenkins when it is scheduled to run. The following problems and possible solutions follow:
1. I also get a 'permission denied(public key)' on the job build when it tries to 'branch' the code from LP.
I've tried it an putting HOME=/tmp/ bzr branch... worked.
2. Also after the code is checked out, the script needs to position itself in the directory which is checkout.
i.e. cd branch_dir
3. We need to clean up the branch_dir either before the build or after (i think it's better before)

Let's get these few problems resolved then we can take a look at the code implementation.

review: Needs Fixing (code)
Revision history for this message
Deepti B. Kalakeri (deeptik) wrote :

> Hey Deepti, thanks for the good work..
> Comments following:
>
> initial_data.json is missing the jenkinsserver record as we talked. It is in
> my branch as well so you can leave it as it is, but someone else trying to run
> the application will be confused.

Yes, I did not wanted to overlap your changes, so did not make it part of the MP.
>
> Also, create job method breaks when the name of the method has 'whitespace' in
> it. It creates job on jenkins, but only with the first string. I guess some
> escaping needs to be done here.

I will fix this.
>
> Basically we can't consider this done until we have a build actually working
> on the jenkins when it is scheduled to run. The following problems and
> possible solutions follow:
> 1. I also get a 'permission denied(public key)' on the job build when it tries
> to 'branch' the code from LP.
I was able to get the jobs started to build once I Clicked on the Build button.
I did not see this error though. How does your input in the loopcreate form looks like?

> I've tried it an putting HOME=/tmp/ bzr branch... worked.

> 2. Also after the code is checked out, the script needs to position itself in
> the directory which is checkout.

Yes, correct, so my form inputs looks like this:
Name: Job_name
branch: linaro-ci-dashboard
precommand: cd linaro-ci-dashboard; make syncdb
command: make test

> i.e. cd branch_dir
> 3. We need to clean up the branch_dir either before the build or after (i
> think it's better before)
We can make it part of the command.
>
> Let's get these few problems resolved then we can take a look at the code
> implementation.

Revision history for this message
Stevan Radaković (stevanr) wrote :

> > Hey Deepti, thanks for the good work..
> > Comments following:
> >
> > initial_data.json is missing the jenkinsserver record as we talked. It is in
> > my branch as well so you can leave it as it is, but someone else trying to
> run
> > the application will be confused.
>
> Yes, I did not wanted to overlap your changes, so did not make it part of the
> MP.
> >
> > Also, create job method breaks when the name of the method has 'whitespace'
> in
> > it. It creates job on jenkins, but only with the first string. I guess some
> > escaping needs to be done here.
>
> I will fix this.
> >
> > Basically we can't consider this done until we have a build actually working
> > on the jenkins when it is scheduled to run. The following problems and
> > possible solutions follow:
> > 1. I also get a 'permission denied(public key)' on the job build when it
> tries
> > to 'branch' the code from LP.
> I was able to get the jobs started to build once I Clicked on the Build
> button.
> I did not see this error though. How does your input in the loopcreate form
> looks like?
>
>
> > I've tried it an putting HOME=/tmp/ bzr branch... worked.
>
> > 2. Also after the code is checked out, the script needs to position itself
> in
> > the directory which is checkout.
>
> Yes, correct, so my form inputs looks like this:
> Name: Job_name
> branch: linaro-ci-dashboard
> precommand: cd linaro-ci-dashboard; make syncdb
> command: make test
>

We don't want user to think about these things. If we can do it easy (and we can), why shouldn't we. Furthermore we can use some different directory name to check out to (it does not to be same as branch name).
We dont want to end up writing scripts in our command or precommand field.
Same goes for 1. and 3.
We need to add the environment prefix in 1. from code, but what I gave is just an example, it should be some /tmp/tmp_dir/ from i.e. gettempdir() in python.

>
> > i.e. cd branch_dir
> > 3. We need to clean up the branch_dir either before the build or after (i
> > think it's better before)
> We can make it part of the command.
> >
> > Let's get these few problems resolved then we can take a look at the code
> > implementation.

16. By Deepti B. Kalakeri

Addressing the review comments

Revision history for this message
Deepti B. Kalakeri (deeptik) wrote :

Changes are already submitted with the changes.

Revision history for this message
Stevan Radaković (stevanr) wrote :

Thanks for addressing the review comments.
Looks good now.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'config_template/sample-job'
2=== added file 'config_template/sample-job/config.xml'
3--- config_template/sample-job/config.xml 1970-01-01 00:00:00 +0000
4+++ config_template/sample-job/config.xml 2012-07-20 15:53:17 +0000
5@@ -0,0 +1,22 @@
6+<?xml version='1.0' encoding='UTF-8'?>
7+<project>
8+ <actions/>
9+ <description></description>
10+ <keepDependencies>false</keepDependencies>
11+ <properties/>
12+ <scm class="hudson.scm.NullSCM"/>
13+ <canRoam>true</canRoam>
14+ <disabled>false</disabled>
15+ <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
16+ <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
17+ <triggers class="vector"/>
18+ <concurrentBuild>false</concurrentBuild>
19+ <builders>
20+ <hudson.tasks.Shell>
21+ <command> # Put your command here
22+ </command>
23+ </hudson.tasks.Shell>
24+ </builders>
25+ <publishers/>
26+ <buildWrappers/>
27+</project>
28
29=== modified file 'dashboard/frontend/fixtures/initial_data.json'
30--- dashboard/frontend/fixtures/initial_data.json 2012-07-16 11:14:43 +0000
31+++ dashboard/frontend/fixtures/initial_data.json 2012-07-20 15:53:17 +0000
32@@ -12,5 +12,14 @@
33 "fields": {
34 "name": "linaro-infrastructure"
35 }
36+ },
37+ {
38+ "model": "jenkinsserver.JenkinsServer",
39+ "pk": 1,
40+ "fields": {
41+ "url": "http://localhost:9090/jenkins",
42+ "username": "admin",
43+ "password": "admin"
44+ }
45 }
46 ]
47\ No newline at end of file
48
49=== modified file 'dashboard/frontend/models/loop.py'
50--- dashboard/frontend/models/loop.py 2012-07-18 17:48:08 +0000
51+++ dashboard/frontend/models/loop.py 2012-07-20 15:53:17 +0000
52@@ -32,7 +32,8 @@
53
54 def save(self, *args, **kwargs):
55 server = JenkinsServer.objects.get(id=1)
56- server.add_integration_loop()
57+ self.server = server
58+ server.add_integration_loop(self)
59 if (True):
60 super(Loop, self).save(*args, **kwargs)
61 else:
62
63=== modified file 'dashboard/jenkinsserver/models/jenkins_server.py'
64--- dashboard/jenkinsserver/models/jenkins_server.py 2012-07-19 08:00:53 +0000
65+++ dashboard/jenkinsserver/models/jenkins_server.py 2012-07-20 15:53:17 +0000
66@@ -17,6 +17,8 @@
67 # along with linaro-ci-dashboard. If not, see <http://www.gnu.org/licenses/>.
68
69 from django.db import models
70+from django.conf import settings
71+from xml.dom.minidom import parse, parseString
72 import jenkins
73
74
75@@ -42,8 +44,9 @@
76 loop.server = self
77 loop.save()
78
79- def add_integration_loop(self):
80- pass
81+ def add_integration_loop(self, loop):
82+ jxml = self.create_job_xml(loop)
83+ self.create_job(loop.name, jxml)
84
85 def sync_jobs(self):
86 """Sync jobs in dashboard DB with jobs in jenkins server.
87@@ -82,3 +85,46 @@
88 def unique_items(self, list_1, list_2):
89 """Return a list of items that are present in list_1 but not list_2."""
90 return [item for item in list_1 if item not in list_2]
91+
92+
93+ def create_job(self, jname, jxml):
94+ """ Creates a new job on jenkins if its not already present """
95+ jname = jname.replace(' ', '%20').strip()
96+ if not self.jenkins.job_exists(jname):
97+ self.jenkins.create_job(jname, jxml)
98+ self.jenkins.enable_job(jname)
99+
100+ def delete_job(self, jname):
101+ """ Deleted a job on jenkins if present """
102+ if self.jenkins.job_exists(jname):
103+ self.jenkins.delete_job(jname)
104+
105+ def create_job_xml(self, loop):
106+ """ Prepares the config.xml to be used for new job"""
107+
108+ dest_dir = 'dest_dir'
109+ if not 'lp:' in loop.branch:
110+ jbranch = 'bzr branch lp:%s %s '% (loop.branch, dest_dir)
111+ else:
112+ jbranch = 'bzr branch %s %s '% (loop.branch, dest_dir)
113+
114+ jcommands =''
115+ rm_old_dest = 'rm -rf %s' % dest_dir
116+ dest_command = 'cur_dir=$WORKSPACE; cd ./%s' % dest_dir
117+ for command in [rm_old_dest, jbranch, dest_command, loop.precommand, \
118+ loop.command]:
119+ if command:
120+ jcommands = '\n'.join([jcommands, command])
121+
122+ dest_command = "cd \"$cur_dir\""
123+ jcommands = '\n'.join([jcommands, dest_command])
124+
125+ file = open(settings.JENKINS_JOB_CONFIG)
126+ jdata = file.read()
127+ file.close()
128+
129+ doc = parseString(jdata)
130+ node = doc.getElementsByTagName('command')
131+ node[0].firstChild.nodeValue = jcommands
132+ jxml = doc.toprettyxml()
133+ return jxml
134
135=== modified file 'dashboard/jenkinsserver/tests/test_jenkins_server.py'
136--- dashboard/jenkinsserver/tests/test_jenkins_server.py 2012-07-19 10:44:27 +0000
137+++ dashboard/jenkinsserver/tests/test_jenkins_server.py 2012-07-20 15:53:17 +0000
138@@ -98,3 +98,30 @@
139 loops = [loop.name.encode("utf8") for loop in
140 self.server.loop_set.all()]
141 self.assertNotIn(self.test_job_name, loops)
142+
143+ def test_create_job_xml(self):
144+ loop = Loop()
145+ loop.name = 'sample-test'
146+ loop.server = self.server
147+ loop.branch = 'linaro-ci-dashboard'
148+ loop.precommand = 'cd %s' % loop.branch
149+ loop.command = 'make syncdb; make test'
150+ jbranch = 'bzr branch lp:%s' % loop.branch
151+ jcommands = '\n'.join([jbranch, loop.precommand, loop.command])
152+ jxml = self.server.create_job_xml(loop)
153+ self.assertIn(jcommands, jxml)
154+
155+ def test_create_new_job_delete_job(self):
156+ loop = Loop()
157+ loop.name = 'sample-test'
158+ loop.server = self.server
159+ loop.branch = 'linaro-ci-dashboard'
160+ loop.precommand = 'cd %s' % loop.branch
161+ loop.command = 'make syncdb; make test'
162+ jbranch = 'bzr branch lp:%s' % loop.branch
163+ jcommands = '\n'.join([jbranch, loop.precommand, loop.command])
164+ jxml = self.server.create_job_xml(loop)
165+ self.server.create_job(loop.name, jxml)
166+ self.assertTrue(self.test_jenkins.job_exists(loop.name) == True)
167+ self.server.delete_job(loop.name)
168+ self.assertTrue(self.test_jenkins.job_exists(loop.name) == False)
169
170=== modified file 'dashboard/settings.py'
171--- dashboard/settings.py 2012-07-19 10:44:27 +0000
172+++ dashboard/settings.py 2012-07-20 15:53:17 +0000
173@@ -188,5 +188,8 @@
174 JENKINS_USER_CONFIG = os.path.join(ROOT_PATH,
175 '../config_template/users/',
176 'config.xml')
177+JENKINS_JOB_CONFIG = os.path.join(ROOT_PATH,
178+ '../config_template/sample-job/',
179+ 'config.xml')
180 JENKINS_HOME = os.path.join(os.getenv('HOME'), ".jenkins_test")
181 JENKINS_LOG_NAME = 'jenkins.log'

Subscribers

People subscribed via source and target branches

to all changes: