def run_bootstrap(self):
     """ run a bootstrap job remotely
     @param remote_action: supply an action
     """
     print "\n[run_bootstrap]"
     rest = Restful(self.url)
     output = rest.send('POST', 'buildByToken/build?job=bootstrap&token=bootstrap',strict=False)
示例#2
0
 def upload(self,file_path,plugin_shortName,version):
     """ Upload a plugin from client system
     @param plugin_shortName: Name of the plugin in Jenkins
     @param version: Version of the given plugin 
     @note: needs further testing
     """
     rest = Restful(self.url)
     data_plugin = ('<jenkins>  <install plugin="%s@%s" /></jenkins>') % (plugin_shortName,version)
     output = rest.post_multipart(self,'/pluginManager/uploadPlugin',data=data_plugin,files=[file_path],strict=True)
     return output
示例#3
0
 def __init__(self,url,user,token):
     """
     @param url:jenkins http path
     @param user: jenkins username 
     @param token: Jenkins token authentication
     @todo: Need to inherit, general web requests object
     """
     self.url = url
     self.user = user 
     self.token = token 
     self.rest = Restful(self.url)
示例#4
0
class System_jenkins(object):
    """ Represents all actions which are system specific to a Jenkins instance
    """
    def __init__(self,url,user,token):
        """
        @param url:jenkins http path
        @param user: jenkins username 
        @param token: Jenkins token authentication
        @todo: Need to inherit, general web requests object
        """
        self.url = url
        self.user = user 
        self.token = token
        self.rest = Restful(self.url)
    
    def restart(self):
        """ Restart Jenkins
        """
        output = self.rest.send('POST','restart')
        status = self._wait()
        return status
        
    def _wait(self,timeout=600):
        """ Wait for jenkins to become available
        @param timeout: int, second to wait before failing 
        """
        server_up = False
        cnt = 0
        time.sleep(3)
        while (server_up==False or cnt >= timeout):
            print "[restart poll] sec: %s  status: %s" % (cnt,server_up)
            time.sleep(1)
            try:
                status = self.rest.send('GET','api/json?tree=jobs[name]').get("status")
                print status
            except Exception as e:
                print e
            if status == 200:
                server_up = True
            cnt+=1
        return server_up
示例#5
0
class Actions(object):
    ''' Using the the jenkins restful api handles all actions related to jobs
    @note: http://<jenkins_url>/api
    '''
    def __init__(self,url,user,token):
        """
        @param url:jenkins http path
        @param user: jenkins username 
        @param token: Jenkins token authentication
        @todo: Need to inherit, general web requests object
        """
        self.url = url
        self.user = user 
        self.token = token 
        self.rest = Restful(self.url)
    
    def _read_file(self,file_path):
        """ private reads files from path
        @param file_path: path to file you wish to read 
        """
        data = None
        try:
            with open(file_path, 'r') as file:
                data = file.read()
            return data
        except Exception as e:
            print "[error] %s" % file_path
            print e
            sys.exit(1)
    
    def _is_building(self,job_name):
        """ Private Is the job currently building
        @param job_name: String of job name 
        """
        ext_url = ("job/%s/lastBuild/api/json?tree=building") % (job_name)
        output = self.rest.send('GET',ext_url)
        
        return output
    
    def is_building(self,job_name):
        """ Is the job currently building
        @param job_name: String of job name 
        """
        status = 404
        building = False
        time.sleep(5)
        while not status == 200:
            response = self._is_building(job_name)
            status = response.get('status')
            building = response.get('building')
        if building == True:
            return True
        return False

    def list(self):
        """ list of jobs in a jenkins instance
        @return: dictionary
        """
        jobs = []
        ext_url = ("api/json?tree=jobs[name]") % (self.url)
        response = self.rest.send('GET',ext_url)
        response = response.json()
        response = response.get('jobs')

        for job in response:
            jobs.append(job.get('name'))
        return jobs
    
    def exists(self,job_name):
        """ determine if a job exists
        @param job_name: 
        """
        jobs = self.list()
        for job in jobs:
            if job == job_name:
                return True
        else:
            return False
    
    def create(self,job_name,data):
        """ Creates a jenkins job using raw xml string
        @param job_name: String job_name
        @param data: xml string  
        """
        print "\n[create]"
        ext_url = ("createItem?name=%s") % (job_name)
        response = self.rest.send('POST',ext_url,data=data,Content_Type='application/xml')
        return response
    
    def create_from(self,file_path,job_name,force=True):
        """ Creates a jenkins job from config.xml file
        @param file_path: file path to config.xml
        @param job_name: String name of the job
        @param force: Boolean will delete an existing job of the same name and create a new one   
        """
        data = self._read_file(file_path)
        ext_url = ("createItem?name=%s") % (job_name)
        response = self.create(job_name, data)
        if response.get('status')==400:
            self.delete(job_name)
            response = self.create(job_name, data)
        if response.get('status')==200:
            return True
        return False

    def delete(self,job_name):
        """ Deletes a jenkins job
        @param job_name: String, job name 
        """
        while self.is_building(job_name)==True:
            print "[waiting] build in progress"
            time.sleep(3)
        ext_url = ("job/%s/doDelete") % (job_name)
        response = self.rest.send('POST',ext_url,Content_Type='application/xml')
        if response.get('status')==200:
            return True
        return False
    
    def start(self,job_name,parameters={},timeout=60):
        """ Starts a Jenkins job
        @param job_name: String, name of the job
        @param parameters: Dict, pass parameters to the job before starting
        @param timeout: will set a timer for when this should fail   
        """
        print "\n[start]"
        ext_url_has_params = ("job/%s/buildWithParameters") % (job_name)
        ext_url = ("job/%s/build") % (job_name)
        if len(parameters) > 0:
            ext_url = ext_url_has_params
            for key,value in parameters.iteritems():
                append= ("&%s=%s") % (key,value)
                put_url = put_url+append
        response = self.rest.send('POST',ext_url,Content_Type='application/xml')
        if response.get('status')==201:
            return True
        return False
    
    def build_latest_result(self,job_name):
        """ Get the latest know build result
        @param job_name: String, name of the job 
        """
        print "\n[build_latest_result]"
        while self.is_building(job_name)==True:
            print "[waiting] build in progress"
            time.sleep(3)
        time.sleep(3)
        ext_url = ("job/%s/lastBuild/api/json?tree=result") % (job_name)
        output = self.rest.send('GET',ext_url)
        return output.get('result')
    
    def build_latest_number(self,job_name):
        """ latest build number 
        @param job_name:string, name of the job
        """
        ext_url = ("job/%s/lastBuild/api/json?tree=number") % (job_name)
        response = self.rest.send('GET',ext_url)
        return response