~milo/linaro-ci-dashboard/xml-to-dict

« back to all changes in this revision

Viewing changes to dashboard/frontend/management/commands/runjenkins.py

  • Committer: Deepti B. Kalakeri
  • Date: 2012-07-17 15:56:44 UTC
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: deepti.kalakeri@linaro.org-20120717155644-tj3ecfzor0t7ldhz
Adding runjenkins and stop jenkins command changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2012 Linaro
 
2
#
 
3
# This file is part of linaro-ci-dashboard.
 
4
#
 
5
# linaro-ci-dashboard is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU Affero General Public License as published by
 
7
# the Free Software Foundation, either version 3 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# linaro-ci-dashboard is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU Affero General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU Affero General Public License
 
16
# along with linaro-ci-dashboard.  If not, see <http://www.gnu.org/licenses/>.
 
17
# Django settings for dashboard project.
 
18
 
 
19
import os
 
20
import sys
 
21
import shutil
 
22
import urllib2
 
23
import urlparse
 
24
import shlex
 
25
from django.conf import settings 
 
26
from django.core import management
 
27
from subprocess import check_output, CalledProcessError, Popen, STDOUT
 
28
from django.core.management.base import NoArgsCommand, CommandError
 
29
 
 
30
class Command(NoArgsCommand):
 
31
    help = 'Run the jenkins service for development/testing'
 
32
  
 
33
    def __init__(self):
 
34
        self.jenkins_war = None
 
35
        self.jenkins_pid = None
 
36
        self.port = settings.JENKINS_PORT
 
37
        self.jenkins_user = settings.JENKINS_ADMIN_USER
 
38
        self.jenkins_passwd = settings.JENKINS_ADMIN_PASSWD
 
39
        self.jenkins_home = settings.JENKINS_HOME
 
40
        self.jenkins_log = settings.JENKINS_LOG_NAME
 
41
        self.jenkins_war_url = settings.JENKKINS_WAR_URL
 
42
 
 
43
    def handle_noargs(self, **options):
 
44
        self.install_package()
 
45
        jenkins_url='http://127.0.0.1:%s/jenkins' % self.port
 
46
        msg = "Successfully configured jenkins with pid %s. \n"\
 
47
              "Use '%s' in your browser \n" % (self.jenkins_pid, jenkins_url)
 
48
        self.stdout.write(msg)
 
49
 
 
50
    def install_package(self):
 
51
        try:
 
52
            # Download openid plugin
 
53
            cmd = 'mkdir -p %s' % self.jenkins_home
 
54
            self.run(cmd)
 
55
 
 
56
            # Stop any jenkins which is already running on the same port
 
57
            cmd = "lsof -w -n -i tcp:%s | awk -F ' ' '{print $2}' | tail -n 1" \
 
58
              % self.port
 
59
            pid = self.run(cmd)
 
60
            if pid:
 
61
                management.call_command('stopjenkins', pid, interactive=False)
 
62
 
 
63
            # start jenkins service  
 
64
            self.jenkins_start()
 
65
  
 
66
            cmd = 'cp %s %s' % (settings.JENKINS_CONFIG, self.jenkins_home)
 
67
            self.run(cmd)
 
68
 
 
69
        except CalledProcessError, e:
 
70
            msg = "Cmd %s failed \n" % e.cmd
 
71
            self.stdout.write(msg) 
 
72
            sys.exit(1)
 
73
        return
 
74
 
 
75
    def download(self, url, filename):
 
76
        try:
 
77
            response = urllib2.urlopen(urllib2.quote(url, safe=":/"))
 
78
            fd = open(filename, 'wb')
 
79
            shutil.copyfileobj(response, fd)
 
80
            fd.close()
 
81
 
 
82
            actual_size = int(response.headers['Content-Length'])
 
83
            downloaded_size = int(os.stat(filename).st_size)
 
84
            response.close()
 
85
            if actual_size != downloaded_size:
 
86
                raise
 
87
        except:
 
88
            raise CommandError("Could not retrieve %s" % url)
 
89
            sys.exit(1)
 
90
        return 0
 
91
 
 
92
    def jenkins_start(self):
 
93
        # Download Jenkins war file
 
94
        self.jenkins_war = os.path.join(self.jenkins_home, 
 
95
                           os.path.basename(self.jenkins_war_url))
 
96
 
 
97
        self.download(self.jenkins_war_url, self.jenkins_war)
 
98
 
 
99
        # Start the jenkins service on port 9090
 
100
        cmd = '/usr/bin/java -jar %s --httpPort=%s --prefix=/jenkins \
 
101
              --argumentsRealm.passwd.%s=%s \
 
102
              --argumentsRealm.roles.%s=admin' \
 
103
              % (self.jenkins_war, self.port, 
 
104
                 self.jenkins_user, self.jenkins_passwd,
 
105
                 self.jenkins_user)
 
106
 
 
107
        cmd = shlex.split(cmd)
 
108
        log_fn = os.path.join(self.jenkins_home, self.jenkins_log)
 
109
        log_fd = open(log_fn, 'a')
 
110
        ret = Popen(cmd, stdout=log_fd, stderr=STDOUT)
 
111
 
 
112
        if ret.returncode:  
 
113
            raise CalledProcessError(1, cmd, 'cmd Failed')
 
114
        log_fd.close()
 
115
        
 
116
        self.jenkins_pid = ret.pid
 
117
        return self.jenkins_pid
 
118
 
 
119
    def run(self, cmd):
 
120
        try:
 
121
            ret = check_output(cmd, shell=True)
 
122
            return ret
 
123
        except CalledProcessError:
 
124
            raise CalledProcessError(1, cmd, "Command Error")
 
125
            sys.exit(1)