Пример #1
1
    def test_lazy_loading(self, _base_poll, _poll, _job_poll):
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one',
                 'url': 'http://localhost:8080/job_one',
                 'color': 'blue'},
                {'name': 'job_two',
                 'url': 'http://localhost:8080/job_two',
                 'color': 'blue'},
            ]
        }
        _base_poll.return_value = _poll.return_value
        _job_poll.return_value = {}
        J = Jenkins('http://localhost:8080/',
                    username='******', password='******', lazy=True)

        self.assertEquals(J._data, None)

        for idx, (job_name, job) in enumerate(J.get_jobs()):
            self.assertEquals(
                job_name,
                _poll.return_value['jobs'][idx]['name'])
            self.assertTrue(isinstance(job, Job))
            self.assertEquals(
                job.name,
                _poll.return_value['jobs'][idx]['name'])
            self.assertEquals(
                job.baseurl,
                _poll.return_value['jobs'][idx]['url'])
Пример #2
0
def jenkins_build (job_name):
    """Builds jenkins job. If job has not started (queued) it will check every 30 seconds
    once it runs it will wait 10 minutes to build and go on to EDIMapper
    """
    jenkins_url = 'http://hq-build3:8080'
    logging_message('attempting to build %s' % job_name)
    server = Jenkins(jenkins_url, username='******', password=decrypt_key('jenkins'))
    if  job_name in server.keys():
        job = server.get_job(job_name)
        if not is_debug:
            server.build_job(job_name)
        else:
            print("would have built Jenkins ", job_name)
        print("is job queued? ", job.is_queued())
        print("this is the job: ", job)
        while(job.is_queued_or_running()):
            print("job is queued")
            print("job in queued: ", job)
            time.sleep(60)
        time.sleep(10)
        try:
            if not job.is_queued_or_running():
                if job.get_last_failed_buildnumber() == job.get_last_buildnumber():
                    print ('build failed')
                    logging_message('build failed')
                    sys.exit()
        except:
            pass

        logging_message('started build %s' % job_name)
    else:
        logging_message('unable to find build %s' % job_name)
        print("should not have displayed this line 148")
        sys.exit()
Пример #3
0
def get_jenkins_result(git_repo):
    jenkinsurl = git_repo.jenkins_url
    jobname = git_repo.jenkins_job_name
    job = Jenkins(jenkinsurl)[jobname]
    last_build_number = job.get_last_buildnumber()
    results = range(last_build_number)
    prev = True  # The previous build status, used to determined fixer
    for i in range(last_build_number):
        build = job[i+1]
        #print build.get_revision() #this is actually commit id
        #print build.get_revision_branch() #contain in about branch
        commit = git_repo.commit_set.all().filter(commit_id=build.get_revision())[0]  # commit_id should be unique
        author = commit.author
        if not build.is_good():
            # Find a commit that matches the id of broken builds
            commit.break_build_status = 1
            commit.save()
            author.num_break_build += 1
            author.save()
        elif not prev:
            author.num_fix_build += 1
            author.save()
        prev = build.is_good()
        author.num_build += 1
        author.save()
    return results
Пример #4
0
    def test_get_jenkins_obj_from_url(self, _base_poll, _poll, _job_poll):
        _job_poll.return_value = {}
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one',
                 'url': 'http://localhost:8080/job_one',
                 'color': 'blue'},
                {'name': 'job_one',
                 'url': 'http://localhost:8080/job_one',
                 'color': 'blue'},
            ]
        }
        _base_poll.return_value = _poll.return_value

        mock_requester = Requester(username='******', password='******')
        mock_requester.post_xml_and_confirm_status = mock.MagicMock(
            return_value='')

        J = Jenkins('http://localhost:8080/',
                    username='******', password='******',
                    requester=mock_requester)

        new_jenkins = J.get_jenkins_obj_from_url('http://localhost:8080/')
        self.assertEquals(new_jenkins, J)

        new_jenkins = J.get_jenkins_obj_from_url('http://localhost:8080/foo')
        self.assertNotEquals(new_jenkins, J)
Пример #5
0
def test_has_plugin(monkeypatch):
    def fake_poll(cls, tree=None):  # pylint: disable=unused-argument
        return {}

    monkeypatch.setattr(Jenkins, '_poll', fake_poll)

    def fake_plugin_poll(cls, tree=None):  # pylint: disable=unused-argument
        return {
            'plugins': [
                {
                    'deleted': False, 'hasUpdate': True, 'downgradable': False,
                    'dependencies': [{}, {}, {}, {}],
                    'longName': 'Jenkins Subversion Plug-in', 'active': True,
                    'shortName': 'subversion', 'backupVersion': None,
                    'url': 'http://wiki.jenkins-ci.org/'
                           'display/JENKINS/Subversion+Plugin',
                    'enabled': True, 'pinned': False, 'version': '1.45',
                    'supportsDynamicLoad': 'MAYBE', 'bundled': True
                }
            ]
        }

    monkeypatch.setattr(Plugins, '_poll', fake_plugin_poll)

    jenkins = Jenkins('http://localhost:8080/',
                      username='******', password='******')
    assert jenkins.has_plugin('subversion') is True
Пример #6
0
 def test_slave_alive(self):
     try:
         J = Jenkins('http://builds.apache.org/', )
         test_slave = J.get_node('H0')
         self.assertIsNotNone(test_slave.is_online())
     except:
         print('HTTPError from http://builds.apache.org. skipping test')
Пример #7
0
def tmp_job_name(request, tmpdir):
    config = file(os.path.join(os.path.dirname(__file__), 'test_config.xml')).read()
    
    jenkins = Jenkins(JENKINS_URL)
    
    # create job using base config
    hasher = hashlib.sha1(str(time.time()))
    job_name = '%s%s' % (JOB_TEST_PREFIX, hasher.hexdigest())
    
    job = jenkins.create_job(job_name, config)
    job.update_config(config) 
    
    # configure local cit information using this job as base
    cit_config = {
        'jobs' : [{
            'source-job' : job_name,
            'feature-branch-job' : job_name + '-$name',
        }],
    }
    cit_config_file = str(tmpdir.join('.cit.yaml'))
    yaml.dump(cit_config, file(cit_config_file, 'w'))
    
    
    def delete_test_jobs():
        '''
        finalizer for this fixture that removes left-over test jobs from the live jenkins server.
        '''
        jenkins = Jenkins(JENKINS_URL)
        for name, job in jenkins.get_jobs():
            if name.startswith(JOB_TEST_PREFIX):
                jenkins.delete_job(name)
                
    request.addfinalizer(delete_test_jobs)
    return job_name
Пример #8
0
def main():
    parser = argparse.ArgumentParser(description='ciapiparams - get build parameters from build number')
    parser.add_argument('build_number', type=int, help="Build number, i.e. 28945")
    args = parser.parse_args()

    print "build_number: {}".format(args.build_number)

    j = Jenkins('http://ci.suse.de', ssl_verify=False)
    job = j.get_job("openstack-mkcloud")
    build = job.get_build(args.build_number)
    parameters = build.get_actions()['parameters']

    for parameter in parameters:
        if isinstance(parameter['value'], bool):
            if parameter['value']:
                value="1"
            else:
                value="0"
        else:
            value=parameter['value']

        # Set default clusterconfig if not defined
        if parameter['name'] == 'clusterconfig':
            if parameter['value']:
                print "export clusterconfig=" + parameter['value']
            else:
                print "export clusterconfig=data+services+network=2"

        else:
            print "export " + parameter['name'] + "=\"" + value + "\""
Пример #9
0
    def test_create_new_job_fail(self, _base_poll, _poll, _job_poll):
        _job_poll.return_value = {}
        _poll.return_value = {
            'jobs': [
                {'name': 'job_one',
                 'url': 'http://localhost:8080/job_one',
                 'color': 'blue'},
                {'name': 'job_one',
                 'url': 'http://localhost:8080/job_one',
                 'color': 'blue'},
            ]
        }
        _base_poll.return_value = _poll.return_value

        mock_requester = Requester(username='******', password='******')
        mock_requester.post_xml_and_confirm_status = mock.MagicMock(
            return_value='')

        J = Jenkins('http://localhost:8080/',
                    username='******', password='******',
                    requester=mock_requester)

        with self.assertRaises(JenkinsAPIException) as ar:
            J.create_job('job_new', None)

        self.assertEquals(str(ar.exception), 'Cannot create job job_new')
Пример #10
0
    def get_context_data(self, **kwargs):
        context = super(HostDetail, self).get_context_data(**kwargs)

        host = self.get_object()
        context['pk'] = self.kwargs['pk']
        jenkins_server = Jenkins('http://'+host.name,host.jenkins_username,host.jenkins_password)

        ## Get Current Available Plugins
        html_plugins = 'http://updates.jenkins-ci.org/download/plugins/'
        html_doc = requests.get(html_plugins)
        soup = BeautifulSoup(html_doc.text, 'html.parser')
        asfind = soup.find_all('a')
        asfind = asfind[5:]
        plugins = [x.string.strip("/") for x in asfind]
        installed_plugins = jenkins_server.get_plugins().values()
        plugin_list = [{'name':x.shortName,'version':x.version,'url':self.get_url(x.shortName,plugins)} for x in installed_plugins]
        sorted_plugin_list = sorted(plugin_list, key=lambda k: k['name'])
        context['plugin'] = sorted_plugin_list
        table = HostPluginsInstalledTable(sorted_plugin_list,
                                          host_id=self.kwargs['pk'])

        RequestConfig(self.request, paginate={"per_page": getattr(
            settings, "NUM_RESULTS_PER_PAGE", None)}).configure(
            table)
        context['table'] = table
        return context
Пример #11
0
 def test_reload(self, _poll):
     mock_requester = Requester(username='******', password='******')
     mock_requester.get_url = mock.MagicMock(return_value='')
     J = Jenkins('http://localhost:8080/',
                 username='******', password='******',
                 requester=mock_requester)
     J.poll()
Пример #12
0
    def test_create_existing_view(self):
        """
        Assert that attempting to create a view which
        already exists simply returns the same view.
        """
        def mockGetData(JJ, url):
            DATA = {}
            DATA['http://localhost:8080/api/python/'] = \
                {'views':[dict(name='NewView', url='http://xxxxx/yyyy')]}
            DATA['http://xxxxx/yyyy/api/python/'] = \
                {}

            try:
                result = DATA[url]

                return result
            except KeyError:
                raise TestDataMissing(url)

        with mock.patch.object(JenkinsBase, 'get_data', mockGetData):

            J = Jenkins('http://localhost:8080',
                    username='******',
                    password='******')

            new_view = J.views().create(str_view_name='NewView')

            self.assertIsInstance(new_view, View)
Пример #13
0
def test_get_jenkins_obj_from_url(mocker, monkeypatch):
    def fake_jenkins_poll(cls, tree=None):  # pylint: disable=unused-argument
        return TWO_JOBS_DATA

    def fake_job_poll(cls, tree=None):  # pylint: disable=unused-argument
        return {}

    monkeypatch.setattr(JenkinsBase, '_poll', fake_jenkins_poll)
    monkeypatch.setattr(Jenkins, '_poll', fake_jenkins_poll)
    monkeypatch.setattr(Job, '_poll', fake_job_poll)

    mock_requester = Requester(username='******', password='******')
    mock_requester.post_xml_and_confirm_status = mocker.MagicMock(
        return_value=''
    )

    jenkins = Jenkins('http://localhost:8080/',
                      username='******', password='******',
                      requester=mock_requester)

    new_jenkins = jenkins.get_jenkins_obj_from_url('http://localhost:8080/')
    assert new_jenkins == jenkins

    new_jenkins = jenkins.get_jenkins_obj_from_url('http://localhost:8080/foo')
    assert new_jenkins != jenkins
Пример #14
0
 def testWithSlash(self, _poll):
     _poll.return_value = {}
     J = Jenkins('http://localhost:8080/',
                 username='******', password='******')
     self.assertEquals(
         J.get_create_url(),
         'http://localhost:8080/createItem')
Пример #15
0
 def do_build(self,socket_id,data):
     projects = data.get("projects")
     put_console_log1  = data.get("put_console_log")
     deploy = data.get("deploy")
     startMsg = "start build project <span style='color:green;'> {project_name} </span>"
     endMsg = "build jenkins project <span style='color:green;'> {project_name} </span> {status}"
     server  = Jenkins(jenkins_url, username, password)
     for project_name in projects:
         ChatSocketHandler.client_map[socket_id].try_write_message(startMsg.format(project_name=project_name))
         server.build_job(project_name)
         job = server[project_name]
         while not job.is_running():
             ChatSocketHandler.client_map[socket_id].try_write_message("........")
             time.sleep(1)
         
         ChatSocketHandler.client_map[socket_id].try_write_message("waiting jenkins start building")
         if put_console_log1:
             self.put_console_log(socket_id,data)
         else:
             ChatSocketHandler.client_map[socket_id].try_write_message("wait build finish not put logs")
             while job.is_running():
                 time.sleep(1)
         last_build = job.get_last_build()
         build_status = last_build.get_status()
         ChatSocketHandler.client_map[socket_id].try_write_message(endMsg.format(project_name=project_name,status=build_status))
         if build_status == "SUCCESS" and deploy:
             self.do_deploy(self.socket_id,project_name)
Пример #16
0
    def getBuildStatusForRevision(self, revision):
        self.debug('Connect to Jenkins at: %s' % self.url)
        jenkins = Jenkins(self.url)

        self.debug('Get job: %s' % self.jobName)
        job = jenkins.get_job(self.jobName)

        self.debug('Get build number for revision: %s' % revision)
        buildNumbers = job.get_buildnumber_for_revision(revision)

        if buildNumbers:

            for buildNumber in buildNumbers:

                self.debug('Get build info for build number: %s' % buildNumber)
                build = job.get_build(buildNumber)

                if build:
                    if build.is_running():
                        raise BuildRunning(revision, buildNumber)
                    else:
                        return (build.get_status(), buildNumber)
                else:
                    raise Exception('No build info for buildNumber=%s revision=%s' % (buildNumber, revision))

        else:
            raise NoSuchBuild(revision)
Пример #17
0
class BaseSystemTest(unittest.TestCase):

    def setUp(self):
        port = jenkinsapi_tests.systests.state['launcher'].http_port
        self.jenkins = Jenkins('http://localhost:%d' % port)
        self._delete_all_jobs()
        self._delete_all_views()

    def tearDown(self):
        pass

    def _delete_all_jobs(self):
        self.jenkins.poll()
        for name in self.jenkins.keys():
            del self.jenkins[name]

    def _delete_all_views(self):
        all_view_names = self.jenkins.views.keys()[1:]
        for name in all_view_names:
            del self.jenkins.views[name]

    def _create_job(self, name='whatever', config=EMPTY_JOB):
        job = self.jenkins.create_job(name, config)
        self.jenkins.poll()
        return job

    def assertJobIsPresent(self, name):
        self.jenkins.poll()
        self.assertTrue(name in self.jenkins,
                        'Job %r is absent in jenkins.' % name)

    def assertJobIsAbsent(self, name):
        self.jenkins.poll()
        self.assertTrue(name not in self.jenkins,
                        'Job %r is present in jenkins.' % name)
Пример #18
0
def deploy_ui_cre(servers, version_release, jenkins_job=False):
    """Deploys ui to servers, if job is still running it waits for it. Reloads config on server.
    """
    print ('Deploying UI. Must wait for Jenkins Job to finish to do Reload Config.')
    if jenkins_job:
        jenkins_url = 'http://*****:*****@%s /home/jboss/reload_config.sh' % server).split()
        logging_message('Deploying SMART - UI on %s...' % server)
        svn_cmd_switch = ['ssh', 'jboss@%s' % server, '/opt/CollabNet_Subversion/bin/svn switch http://svn-prod-ro.wernerds.net/svn/werner/tms-coldfusion/smart/wernervas/tags/%s /srv/deploy/tmsWebApp/tmsWebApp.war/wernervas' % version_release ]
        if not is_debug:
            Popen(svn_cmd_switch).wait()
            Popen(svn_reload_config).wait()
            logging_message('Deployed SMART - UI on %s' % server)
        else:
            print("Would have ran ", svn_cmd_switch)
            print("Would have ran ", svn_reload_config)
Пример #19
0
 def delete_test_jobs():
     '''
     finalizer for this fixture that removes left-over test jobs from the live jenkins server.
     '''
     jenkins = Jenkins(JENKINS_URL)
     for name, job in jenkins.get_jobs():
         if name.startswith(JOB_TEST_PREFIX):
             jenkins.delete_job(name)
Пример #20
0
 def __init__(self, config):
   self.url = config['url']
   self.jobName = config['jobName']
   self.user = config['user']
   self.password = config['password']
   Jenkins.__init__(self, self.url, config['user'], config['password'])
   self.job = self.get_job(self.jobName)
   self.number = 0
Пример #21
0
def main():
    username = os.environ['username']
    password = os.environ['password']
    J = Jenkins('http://115.28.134.83:8000',username,password)
    #get all jenkins jobs
    for key,job in J.iteritems():
        build_time(job)
    return(0)
Пример #22
0
 def delete_test_jobs():
     '''
     finalizer for this fixture that removes left-over test jobs from the live jenkins server.
     '''
     jenkins = Jenkins(jenkins_url, jenkins_user, jenkins_pass)
     for job_name in jenkins.iterkeys():
         if job_name.startswith(JOB_TEST_PREFIX):
             jenkins.delete_job(job_name)
Пример #23
0
def get_last_buildno(job_name):
    #j = Jenkins(JENKINS_URL, requester=Requester(username, password, baseurl=JENKINS_URL, ssl_verify=False))
    j = Jenkins(JENKINS_URL, ssl_verify=False)
    try:
        return j.get_job(job_name).get_last_good_build().buildno
    except jenkinsapi.custom_exceptions.NoBuildData:
        return j.get_job(job_name).get_last_build().buildno
    except jenkinsapi.custom_exceptions.UnknownJob:
        raise
 def MessageStatus(self, Message, Status):
       """ Event handler for Skype chats """
       print "status is "+Status
       if Status == Skype4Py.cmsReceived:
           print "Message '%s' received from user %s", Message.Body, Message.FromHandle
           if Message.Body=='build':
               j = Jenkins('http://10.42.0.123:8080/jenkins/')
               j.build_job('Build')
               print 'command jenkins received'
Пример #25
0
def get_nested_view_from_url(url, username=None, password=None):
    """
    Returns View based on provided URL. Convenient for nested views.
    """
    matched = constants.RE_SPLIT_VIEW_URL.search(url)
    if not matched:
        raise BadURL("Cannot parse URL %s" % url)
    jenkinsci = Jenkins(matched.group(0), username=username, password=password)
    return jenkinsci.get_view_by_url(url)
Пример #26
0
 def test_fb_add(self, tmp_job_name, global_config_file, branch):
     '''
     test "fb.add" command
     
     :param branch: 
         parametrized to test adding passing a branch name in the command line and without
         (which means "use current branch as branch name") 
     '''
     with mock.patch('cit.get_git_branch', autospec=True) as mock_get_git_branch:
         mock_get_git_branch.return_value = 'new-feature'
         
         with mock.patch('cit.get_git_user', autospec=True) as mock_get_git_user:
             mock_get_git_user.return_value = ('anonymous', '*****@*****.**')
             
             with mock.patch('cit.get_global_config_file', autospec=True) as mock_get_global_config_file:
                 mock_get_global_config_file.return_value = str(global_config_file)
                 argv = ['fb.add']
                 if branch:
                     argv.append(branch)
                 assert cit.app.main(argv) is None
     
     branch = 'new-feature'
     
     jenkins = Jenkins(JENKINS_URL)
     new_job_name = tmp_job_name + '-' + branch
     assert jenkins.has_job(new_job_name), "no job %s found. available: %s" % (new_job_name, jenkins.get_jobs_list())
     
     config_xml = jenkins.get_job(new_job_name).get_config()
     
     # ensure we configured branch correctly
     config = ET.fromstring(config_xml)
     branches_elements = list(config.findall('.//branches'))
     assert len(branches_elements) == 1
     branches_elem = branches_elements[0]
     name_elem = branches_elem.findall('hudson.plugins.git.BranchSpec/name')[0]    
     assert name_elem.text == branch
     
     # ensure we have updated the display name with the feature branch's name
     display_name_elements = list(config.findall('.//displayName'))
     assert len(display_name_elements) == 1
     display_name_element = display_name_elements[0]
     assert display_name_element.text == '%s SS win32' % branch
     
     # ensure we have set the user email recipient
     recipient_elements = list(config.findall('.//hudson.tasks.Mailer/recipients'))
     assert len(recipient_elements) == 1
     recipient_element = recipient_elements[0]
     assert recipient_element.text == '*****@*****.**'
     
     # ensure we don't have build parameters anymore
     params = list(config.findall('.//hudson.model.ParametersDefinitionProperty'))
     assert len(params) == 0
 
     # ensure no build is triggered after the job
     build_triggers = list(config.findall('.//hudson.tasks.BuildTrigger'))
     assert len(build_triggers) == 0
Пример #27
0
    def __init__(self,buildURL = None):

        if buildURL:
            self.__JenkinsURL = buildURL       

        j = Jenkins(self.__JenkinsURL)
        if self.__Job not in j.keys():
            raise xenrt.XRTError('No Jenkins job found with id %s' % self.__Job)
   
        self.__JenkinsCommand = JenkinsCommand(j[self.__Job])
Пример #28
0
def get_view_from_url(url):
    """
    Factory method
    """
    matched = constants.RE_SPLIT_VIEW_URL.search(url)
    if not matched:
        raise BadURL("Cannot parse URL %s" % url)
    jenkinsurl, view_name = matched.groups()
    jenkinsci = Jenkins(jenkinsurl)
    return jenkinsci.get_view(view_name)
Пример #29
0
def build_job(name):
    api=Jenkins('http://192.168.2.39:8080','','test123!')
#    job=api.get_job('deploy_dh_client_resource_to_test')
    job=api.get_job(name)
    try:
        result = job.invoke(build_params=None, cause=None)

        print result
    except Exception,e:
        print colored("catch the exption:%s" % e,'red')
Пример #30
0
 def test_get_master_data(self, _base_poll):
     base_url = 'http://localhost:808'
     _base_poll.return_value = {
         "busyExecutors": 59,
         "totalExecutors": 75
     }
     j = Jenkins(base_url,
                 username='******', password='******')
     data = j.get_master_data()
     self.assertEquals(data['busyExecutors'], 59)
     self.assertEquals(data['totalExecutors'], 75)
Пример #31
0
    content = 'to view build result, check:\r\n' + target_url + '\r\nYou can view the failed pr at:\r\n' + html_url
    send_mail(sub, title, content)


payload_str = os.environ['payload']
payload_str = payload_str.decode('utf-8', 'ignore')
#parse to json obj
payload = json.loads(payload_str)
#pr = payload['pull_request']
url = payload['html_url']
print "build pr:" + url
pr_num = payload['number']
#get statuses url
statuses_url = payload['statuses_url']

J = Jenkins(os.environ['JENKINS_URL'])
target_url = os.environ['BUILD_URL']
build_number = int(os.environ['BUILD_NUMBER'])
data = {
    "state": "pending",
    "target_url": target_url,
    "context": "Jenkins CI",
    "description": "Build finished!"
}
access_token = os.environ['GITHUB_ACCESS_TOKEN']
Headers = {"Authorization": "token " + access_token}

result = J[os.environ['JOB_NAME']].get_build(build_number).get_status()

if (result == STATUS_SUCCESS):
    data['state'] = "success"
Пример #32
0
  else:
    build_result['error'] = [{'description': '', 'count': 0}]

  return build_result 


# parse command line
argp = argparse.ArgumentParser(description='Get build statistics.')
argp.add_argument('-u', '--username', default='jenkins')
argp.add_argument('-b', '--builds', 
                  choices=['all'] + sorted(_BUILDS.keys()),
                  nargs='+',
                  default=['all'])
args = argp.parse_args()

J = Jenkins('https://grpc-testing.appspot.com', args.username, 'apiToken')
bq = big_query_utils.create_big_query()

for build_name in _BUILDS.keys() if 'all' in args.builds else args.builds:
  print('====> Build: %s' % build_name)
  # Since get_last_completed_build() always fails due to malformatted string
  # error, we use get_build_metadata() instead.
  job = None
  try:
    job = J[build_name]
  except Exception as e:
    print('====> Failed to get build %s: %s.' % (build_name, str(e)))
    continue
  last_processed_build_number = _get_last_processed_buildnumber(build_name)
  last_complete_build_number = job.get_last_completed_buildnumber()
  # To avoid processing all builds for a project never looked at. In this case,
Пример #33
0
from flask import Flask, request
from flask_cors import CORS
from flask_restful import Resource, Api
from flask_sqlalchemy import SQLAlchemy
from jenkinsapi.jenkins import Jenkins

app = Flask(__name__)
CORS(app)
api = Api(app)
app.config[
    'SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://lagou5:[email protected]:23306/lagou5db?charset=utf8mb4'
db = SQLAlchemy(app)

jenkins = Jenkins(
    'http://stuq.ceshiren.com:8020/',
    username='******',
    password='******'
)


# testcases = []

class TestCase(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=False, nullable=False)
    steps = db.Column(db.String(1000), unique=False, nullable=True)

    def __repr__(self):
        return '<TestCase %r>' % self.name

    def as_dict(self):
Пример #34
0
    3.1 Jenkins创建Webhook:http://jenkins.babamama.cn/jenkins/view/devops/job/nginxtest/buildWithParameters?token=nginxtest&action=deploy
    3.2 提取工单中对应上线项目信息——项目名称
    3.3 拼接 Webhook
    3.4 拼接action 
"""
# 钉钉
Appkey = "dingkv*****kdcpx27t"
Appsecret = "B_H3332GU0sIozvtI*****V4kM1difVpfr_Wb4_hZwulBMZoBR1L7xxSEVu"

process_code = 'PROC-89492106-253F-4EAC-AB55-9D3C2E1C763D'  # 查看 process_code 接口 /process/listbyuserid

# jenkins
username = '******'
password = '******'
jenkins_url = 'http://*****:8080/jenkins/'
server = Jenkins(jenkins_url, username=username, password=password)


def get_timestamp(mins):
    """
    获取n分钟前时间并转换为毫秒级时间戳(钉钉查询必须使用毫秒级时间戳)
    :return: timestamp
    """
    # 获取n分钟前的时间
    mins = int(mins)
    start_time = (datetime.datetime.now() + datetime.timedelta(minutes=-mins)).strftime('%Y-%m-%d %H:%M:%S')
    # 转换为毫秒级时间戳
    start_time_array = time.strptime(start_time, "%Y-%m-%d %H:%M:%S")
    timestamp = int(round(time.mktime(start_time_array) * 1000))

    return timestamp
Пример #35
0
#!/usr/bin/env python
"""
Utility for pretty printing Build objects into a file.
Helpful for learning the jenkins api.
"""

import json
import pprint
import sys
from jenkinsapi.jenkins import Jenkins

if len(sys.argv) < 2:
    print "Usage: dump job [build]"
    sys.exit(0)

with open('credentials/jenkins.json') as data_file:
    jen_cred = json.load(data_file)

jenkins = Jenkins('http://jenkins2.datastax.lan:8080/',
                  username=jen_cred["username"],
                  password=jen_cred["password"])

JOB = sys.argv[1]
j = jenkins.get_job(JOB)

BUILD = int(sys.argv[2]) if len(sys.argv) == 3 else j.get_last_buildnumber()
b = j.get_build(BUILD)

with open("examples/{0}_{1}.txt".format(b.job.name, b.buildno), "w") as f:
    pprint.pprint(b._data, stream=f)
Пример #36
0
from __future__ import print_function
from pkg_resources import resource_string
from jenkinsapi.jenkins import Jenkins
from jenkinsapi.utils.crumb_requester import CrumbRequester

url = "jenkinsURL"
username = "******"
password = "******"
crumb_requester = CrumbRequester(baseurl=url, username=username, password=password)
j = Jenkins(url, username=username, password=password, requester=crumb_requester)
job_name = 'jaydeepCreatedByPythonTest-4'
xml = resource_string(__name__, 'job.xml')

print(xml)

job = j.create_job(jobname=job_name, xml=xml)

# Get job from Jenkins by job name
my_job = j[job_name]
print(my_job)
Пример #37
0
#!/usr/bin/python
import datetime, json, urllib, time, sys, pycurl, os, paramiko, pexpect, subprocess
from jenkinsapi.jenkins import Jenkins
from key_gen import ssh_key
KGB_Full = "https://fem135-eiffel004.lmera.ericsson.se:8443/jenkins/view/KGB+N_Full/"
J = Jenkins(KGB_Full)
jobs = J.keys()
jobs_list = []
for j in jobs:
    if "_Full_vApp_KGB+N" in j:
        jobs_list.append(j)
num_of_hours = 2
current_date = datetime.datetime.today()
lastHourDateTime = current_date - datetime.timedelta(hours=2)
base_past_date = lastHourDateTime.strftime('%Y-%m-%d %H:%M:%S')
print "current_date: ", current_date
print "base_past_date: ", base_past_date
print "================"


def get_ip(url):
    class ContentCallback:
        def __init__(self):
            self.contents = ''

        def content_callback(self, buf):
            self.contents = self.contents + buf

    t = ContentCallback()
    curlObj = pycurl.Curl()
    curlObj.setopt(curlObj.URL, url)
Пример #38
0
from __future__ import print_function
from jenkinsapi.jenkins import Jenkins

J = Jenkins('http://localhost:8080')
print(J.items())
j = J.get_job("foo")
b = j.get_last_build()
print(b)
mjn = b.get_master_job_name()
print(mjn)

EMPTY_JOB_CONFIG = '''\
<?xml version='1.0' encoding='UTF-8'?>
<project>
  <actions/>
  <description></description>
  <keepDependencies>false</keepDependencies>
  <properties/>
  <scm class="hudson.scm.NullSCM"/>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers class="vector"/>
  <concurrentBuild>false</concurrentBuild>
  <builders/>
  <publishers/>
  <buildWrappers/>
</project>
'''
Пример #39
0
from flask_sqlalchemy import SQLAlchemy
from jenkinsapi.jenkins import Jenkins

with open('../conf/config.yml', encoding='utf-8') as f:
    config = yaml.safe_load(f)
    db_conf = config['DB']
    jenkins_conf = config['Jenkins']

app = Flask(__name__)
# 允许跨域访问
CORS(app)
# 配置json转ASCII编码已解决返回json中中文的UTF-8编码问题
app.config['JSON_AS_ASCII'] = False
api = Api(app)
jenkins = Jenkins(f'http://{jenkins_conf["Host"]}:{jenkins_conf["Port"]}',
                  username=jenkins_conf['username'],
                  password=jenkins_conf['password'])

# 配置数据库
'''
Python Console中输入如下命令创建表

from core.backend import db
db.create_all()
'''
app.config[
    'SQLALCHEMY_DATABASE_URI'] = f'mysql+pymysql://{db_conf["username"]}:{db_conf["password"]}@' \
                                 f'{db_conf["Host"]}:{db_conf["Port"]}/{db_conf["DBname"]}'
db = SQLAlchemy(app)
# token管理
app.config['JWT_SECRET_KEY'] = 'TestPlatform'
Пример #40
0
class TestPlugins(unittest.TestCase):
    DATA = {
        'plugins': [{
            'deleted': False,
            'hasUpdate': True,
            'downgradable': False,
            'dependencies': [{}, {}, {}, {}],
            'longName': 'Jenkins Subversion Plug-in',
            'active': True,
            'shortName': 'subversion',
            'backupVersion': None,
            'url': 'http://wiki.jenkins-ci.org/display/'
            'JENKINS/Subversion+Plugin',
            'enabled': True,
            'pinned': False,
            'version': '1.45',
            'supportsDynamicLoad': 'MAYBE',
            'bundled': True
        }, {
            'deleted': False,
            'hasUpdate': True,
            'downgradable': False,
            'dependencies': [{}, {}],
            'longName': 'Maven Integration plugin',
            'active': True,
            'shortName': 'maven-plugin',
            'backupVersion': None,
            'url': 'http://wiki.jenkins-ci.org/display/JENKINS/'
            'Maven+Project+Plugin',
            'enabled': True,
            'pinned': False,
            'version': '1.521',
            'supportsDynamicLoad': 'MAYBE',
            'bundled': True
        }]
    }

    @mock.patch.object(Jenkins, '_poll')
    def setUp(self, _poll_jenkins):
        _poll_jenkins.return_value = {}

        self.J = Jenkins('http://*****:*****@mock.patch.object(Plugins, '_poll')
    def test_get_plugins(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        # Can we produce a repr string for this object
        self.assertIsInstance(self.J.get_plugins(), Plugins)

    @mock.patch.object(Plugins, '_poll')
    def test_no_plugins_str(self, _poll_plugins):
        _poll_plugins.return_value = {}

        plugins = self.J.get_plugins()
        self.assertEquals(str(plugins), "[]")

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_str(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        plugins = self.J.get_plugins()
        self.assertEquals(str(plugins), "['maven-plugin', 'subversion']")

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_len(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        plugins = self.J.get_plugins()
        self.assertEquals(len(plugins), 2)

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_contains(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        plugins = self.J.get_plugins()
        self.assertIn('subversion', plugins)
        self.assertIn('maven-plugin', plugins)

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_values(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        p = Plugin({
            'deleted': False,
            'hasUpdate': True,
            'downgradable': False,
            'dependencies': [{}, {}, {}, {}],
            'longName': 'Jenkins Subversion Plug-in',
            'active': True,
            'shortName': 'subversion',
            'backupVersion': None,
            'url': 'http://wiki.jenkins-ci.org/display/JENKINS/'
            'Subversion+Plugin',
            'enabled': True,
            'pinned': False,
            'version': '1.45',
            'supportsDynamicLoad': 'MAYBE',
            'bundled': True
        })

        plugins = self.J.get_plugins().values()
        self.assertIn(p, plugins)

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_keys(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        plugins = self.J.get_plugins().keys()
        self.assertIn('subversion', plugins)
        self.assertIn('maven-plugin', plugins)

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_empty(self, _poll_plugins):
        _poll_plugins.return_value = {}

        # list() is required here for python 3.x compatibility
        plugins = list(self.J.get_plugins().keys())
        self.assertEquals([], plugins)

    @mock.patch.object(Plugins, '_poll')
    def test_plugin_get_by_name(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        p = Plugin({
            'deleted': False,
            'hasUpdate': True,
            'downgradable': False,
            'dependencies': [{}, {}, {}, {}],
            'longName': 'Jenkins Subversion Plug-in',
            'active': True,
            'shortName': 'subversion',
            'backupVersion': None,
            'url': 'http://wiki.jenkins-ci.org/display/JENKINS/'
            'Subversion+Plugin',
            'enabled': True,
            'pinned': False,
            'version': '1.45',
            'supportsDynamicLoad': 'MAYBE',
            'bundled': True
        })

        plugin = self.J.get_plugins()['subversion']
        self.assertEquals(p, plugin)

    @mock.patch.object(Plugins, '_poll')
    def test_get_plugin_details(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA
        plugin = self.J.get_plugins()['subversion']
        self.assertEquals('1.45', plugin.version)
        self.assertEquals('subversion', plugin.shortName)
        self.assertEquals('Jenkins Subversion Plug-in', plugin.longName)
        self.assertEquals(
            'http://wiki.jenkins-ci.org/display/JENKINS/'
            'Subversion+Plugin', plugin.url)

    @mock.patch.object(Requester, 'post_xml_and_confirm_status')
    def test_install_plugin_bad_input(self, _post):
        with self.assertRaises(ValueError):
            self.J.install_plugin('test')

    @mock.patch.object(Plugins, '_poll')
    @mock.patch.object(Plugins, 'plugin_version_already_installed')
    @mock.patch.object(Plugins, '_wait_until_plugin_installed')
    @mock.patch.object(Requester, 'post_xml_and_confirm_status')
    def test_install_plugin_good_input(self, _post, _wait, already_installed,
                                       _poll_plugins):
        _poll_plugins.return_value = self.DATA
        already_installed.return_value = False
        self.J.install_plugin('test@latest')
        expected_data = '<jenkins> <install plugin="test@latest" /> </jenkins>'
        _post.assert_called_with('/'.join(
            [self.J.baseurl, 'pluginManager', 'installNecessaryPlugins']),
                                 data=expected_data)

    @mock.patch.object(Plugins, '_poll')
    @mock.patch.object(Plugins, 'plugin_version_already_installed')
    @mock.patch.object(Plugins, '_wait_until_plugin_installed')
    @mock.patch.object(Requester, 'post_xml_and_confirm_status')
    @mock.patch.object(Jenkins, 'safe_restart')
    def test_install_plugins_good_input_no_restart(self, _restart, _post,
                                                   _wait, already_installed,
                                                   _poll_plugins):
        _poll_plugins.return_value = self.DATA
        already_installed.return_value = False
        self.J.install_plugins(['test@latest', 'test@latest'])
        self.assertEqual(_post.call_count, 2)
        self.assertEqual(_restart.call_count, 0)

    @mock.patch.object(Plugins, '_poll')
    @mock.patch.object(Plugins, 'plugin_version_already_installed')
    @mock.patch.object(Plugins,
                       'restart_required',
                       new_callable=mock.mock.PropertyMock)
    @mock.patch.object(Plugins, '_wait_until_plugin_installed')
    @mock.patch.object(Requester, 'post_xml_and_confirm_status')
    @mock.patch.object(Jenkins, 'safe_restart')
    def test_install_plugins_good_input_with_restart(self, _restart, _post,
                                                     _wait, restart_required,
                                                     already_installed,
                                                     _poll_plugins):
        _poll_plugins.return_value = self.DATA
        restart_required.return_value = True,
        already_installed.return_value = False
        self.J.install_plugins(['test@latest', 'test@latest'], restart=True)
        self.assertEqual(_post.call_count, 2)
        self.assertEqual(_restart.call_count, 1)

    @mock.patch.object(Plugins, '_poll')
    def test_get_plugin_dependencies(self, _poll_plugins):
        manifest = (
            'Manifest-Version: 1.0\n'
            'bla: somestuff\n'
            'Plugin-Dependencies: aws-java-sdk:1.10.45,aws-credentials:1.15')
        downloaded_plugin = StringIO()
        zipfile.ZipFile(downloaded_plugin,
                        mode='w').writestr('META-INF/MANIFEST.MF', manifest)
        _poll_plugins.return_value = self.DATA
        dependencies = self.J.plugins._get_plugin_dependencies(
            downloaded_plugin)
        self.assertEquals(len(dependencies), 2)
        for dep in dependencies:
            self.assertIsInstance(dep, Plugin)

    @mock.patch.object(Plugins, '_poll')
    def test_plugin_version_already_installed(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA
        already_installed = Plugin({
            'shortName': 'subversion',
            'version': '1.45'
        })
        self.assertTrue(
            self.J.plugins.plugin_version_already_installed(already_installed))
        not_installed = Plugin({'shortName': 'subversion', 'version': '1.46'})
        self.assertFalse(
            self.J.plugins.plugin_version_already_installed(not_installed))
        latest = Plugin({'shortName': 'subversion', 'version': 'latest'})
        self.assertFalse(
            self.J.plugins.plugin_version_already_installed(latest))

    @mock.patch.object(Plugins, '_poll')
    @mock.patch.object(Plugins,
                       'update_center_install_status',
                       new_callable=mock.mock.PropertyMock)
    def test_restart_required_after_plugin_installation(
            self, status, _poll_plugins):
        _poll_plugins.return_value = self.DATA
        status.return_value = {
            'data': {
                'jobs': [{
                    'installStatus': 'SuccessButRequiresRestart',
                    'name': 'credentials',
                    'requiresRestart': 'true',
                    'title': None,
                    'version': '0'
                }],
                'state':
                'RUNNING'
            },
            'status': 'ok'
        }
        self.assertTrue(self.J.plugins.restart_required)

    @mock.patch.object(Plugins, '_poll')
    @mock.patch.object(Plugins,
                       'update_center_install_status',
                       new_callable=mock.mock.PropertyMock)
    def test_restart_not_required_after_plugin_installation(
            self, status, _poll_plugins):
        _poll_plugins.return_value = self.DATA
        status.return_value = {
            'data': {
                'jobs': [],
                'state': 'RUNNING'
            },
            'status': 'ok'
        }
        self.assertFalse(self.J.plugins.restart_required)

    def test_plugin_repr(self):
        p = Plugin({
            'shortName': 'subversion',
        })
        self.assertEquals(repr(p), '<jenkinsapi.plugin.Plugin subversion>')
Пример #41
0
    def setUp(self, _poll_jenkins):
        _poll_jenkins.return_value = {}

        self.J = Jenkins('http://localhost:8080')
Пример #42
0
class TestPlugins(unittest.TestCase):
    DATA = {
        'plugins': [{
            'deleted': False,
            'hasUpdate': True,
            'downgradable': False,
            'dependencies': [{}, {}, {}, {}],
            'longName': 'Jenkins Subversion Plug-in',
            'active': True,
            'shortName': 'subversion',
            'backupVersion': None,
            'url':
            'http://wiki.jenkins-ci.org/display/JENKINS/Subversion+Plugin',
            'enabled': True,
            'pinned': False,
            'version': '1.45',
            'supportsDynamicLoad': 'MAYBE',
            'bundled': True
        }, {
            'deleted': False,
            'hasUpdate': True,
            'downgradable': False,
            'dependencies': [{}, {}],
            'longName': 'Maven Integration plugin',
            'active': True,
            'shortName': 'maven-plugin',
            'backupVersion': None,
            'url':
            'http://wiki.jenkins-ci.org/display/JENKINS/Maven+Project+Plugin',
            'enabled': True,
            'pinned': False,
            'version': '1.521',
            'supportsDynamicLoad': 'MAYBE',
            'bundled': True
        }]
    }

    @mock.patch.object(Jenkins, '_poll')
    def setUp(self, _poll_jenkins):
        _poll_jenkins.return_value = {}

        self.J = Jenkins('http://*****:*****@mock.patch.object(Plugins, '_poll')
    def test_get_plugins(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        # Can we produce a repr string for this object
        self.assertIsInstance(self.J.get_plugins(), Plugins)

    @mock.patch.object(Plugins, '_poll')
    def test_no_plugins_str(self, _poll_plugins):
        _poll_plugins.return_value = {}

        plugins = self.J.get_plugins()
        self.assertEquals(str(plugins), "[]")

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_str(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        plugins = self.J.get_plugins()
        self.assertEquals(str(plugins), "['maven-plugin', 'subversion']")

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_len(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        plugins = self.J.get_plugins()
        self.assertEquals(len(plugins), 2)

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_contains(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        plugins = self.J.get_plugins()
        self.assertIn('subversion', plugins)
        self.assertIn('maven-plugin', plugins)

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_values(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        p = Plugin({
            'deleted': False,
            'hasUpdate': True,
            'downgradable': False,
            'dependencies': [{}, {}, {}, {}],
            'longName': 'Jenkins Subversion Plug-in',
            'active': True,
            'shortName': 'subversion',
            'backupVersion': None,
            'url':
            'http://wiki.jenkins-ci.org/display/JENKINS/Subversion+Plugin',
            'enabled': True,
            'pinned': False,
            'version': '1.45',
            'supportsDynamicLoad': 'MAYBE',
            'bundled': True
        })

        plugins = self.J.get_plugins().values()
        self.assertIn(p, plugins)

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_keys(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        plugins = self.J.get_plugins().keys()
        self.assertIn('subversion', plugins)
        self.assertIn('maven-plugin', plugins)

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_empty(self, _poll_plugins):
        _poll_plugins.return_value = {}

        # list() is required here for python 3.x compatibility
        plugins = list(self.J.get_plugins().keys())
        self.assertEquals([], plugins)

    @mock.patch.object(Plugins, '_poll')
    def test_plugin_get_by_name(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        p = Plugin({
            'deleted': False,
            'hasUpdate': True,
            'downgradable': False,
            'dependencies': [{}, {}, {}, {}],
            'longName': 'Jenkins Subversion Plug-in',
            'active': True,
            'shortName': 'subversion',
            'backupVersion': None,
            'url':
            'http://wiki.jenkins-ci.org/display/JENKINS/Subversion+Plugin',
            'enabled': True,
            'pinned': False,
            'version': '1.45',
            'supportsDynamicLoad': 'MAYBE',
            'bundled': True
        })

        plugin = self.J.get_plugins()['subversion']
        self.assertEquals(p, plugin)

    @mock.patch.object(Plugins, '_poll')
    def test_get_plugin_details(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA
        plugin = self.J.get_plugins()['subversion']
        self.assertEquals('1.45', plugin.version)
        self.assertEquals('subversion', plugin.shortName)
        self.assertEquals('Jenkins Subversion Plug-in', plugin.longName)
        self.assertEquals(
            'http://wiki.jenkins-ci.org/display/JENKINS/Subversion+Plugin',
            plugin.url)

    def test_plugin_repr(self):
        p = Plugin({
            'shortName': 'subversion',
        })
        self.assertEquals(repr(p), '<jenkinsapi.plugin.Plugin subversion>')
Пример #43
0
#jenkins登录

import jenkinsapi
from jenkinsapi.jenkins import Jenkins

username = '******'
password = '******'
api_token = 'cce529148db2e7d98735ba2ebd0d9cae'

J = Jenkins('http://10.10.10.10:8080', username, password)
print(J.version)

#[Jenkins]如何自动停止超时任务?
for key, job in J.iteritems():
    last_build = job.get_last_buildnumber() // 获得最后一个构建的编号
    running = build.is_running() // 任务是否在运行
    start_time = last_build.get_timestamp() // 获得构建的开始时间
    last_build.stop() // 停止构建
Пример #44
0
import jenkinsapi
from jenkinsapi.jenkins import Jenkins
from jenkinsapi.build import Build

url = 'http://localhost:8080/'
username = '******'
password = '******'
server = Jenkins(url, username, password, useCrumb=True)

job = server.get_job('JobReport')
print(job.get_last_build().get_status())
Пример #45
0
def run_jenkins_job(job_name):
    job = Jenkins('http://172.30.205.179:9090')
    #job = Jenkins('http://10.2.162.80:9090')
    job.build_job(job_name)
Пример #46
0
class JenkinsBuildBot(BaseBot):
    def __init__(self, bot_slack, user_slack, jenkins_url):
        self.favorites = []
        self.jenkins = Jenkins(jenkins_url, ssl_verify=False)
        BaseBot.__init__(self, bot_slack, user_slack)

    def _load_from_file(self):
        pass

    def _write_to_file(self):
        pass

    def 명령어(self):
        return "\n".join(["명령어 목록"] + list(self.help_dict.values()))

    def 작업목록(self):
        return self.작업검색()

    def 작업검색(self, 검색어=''):
        jobs = []
        for job_name in self.jenkins.get_jobs_list():
            if not 검색어 or 검색어.lower() in job_name.lower(
            ) and "OLD" not in job_name:
                jobs.append(job_name.replace(' ', '_'))

        return "\n".join(jobs)

    def 빌드상태(self, 작업이름):
        if not self.jenkins.has_job(작업이름):
            return "해당 작업이 존재하지 않습니다."

        job = self.jenkins.get_job(작업이름)

        if not job.is_queued_or_running():
            return "실행중인 빌드가 없습니다."

        response = self.jenkins.requester.get_and_confirm_status(
            f"{job.baseurl}/{job.get_last_buildnumber()}/wfapi/describe")

        json = response.json()
        return f"빌드 실행중입니다. 현재 {json['stages'][-1]['name']} 단계 빌드중입니다."

    async def 빌드시작(self, 작업이름):
        if not self.jenkins.has_job(작업이름):
            return "해당 작업이 존재하지 않습니다."

        job = self.jenkins.get_job(작업이름)

        if job.is_queued_or_running():
            return "이미 실행중인 빌드가 있습니다."

        build_parameter_dict = await self._start_conversation_and_return_parameter_dict(
            job)

        if build_parameter_dict is None:
            return "빌드시작을 취소했습니다."

        job.invoke(build_params=build_parameter_dict)

        build_num = job.get_last_buildnumber()

        return f"{작업이름} #{build_num}을 시작하였습니다."

    async def _start_conversation_and_return_parameter_dict(self, job):
        channel = self._last_message_channel
        user = self._last_message_user

        parameter_dict = dict()
        params = job.get_params()

        self._send_slack_message("빌드 파라미터를 입력해주세요.\n"
                                 "d 입력시 기본값, ad 입력시 입력하지 않은 값을 기본값으로 빌드합니다. "
                                 "cancel 또는 취소 입력시 빌드 시작을 취소합니다.")
        for param in params:
            param_name = param['name']
            default_value = param.get('defaultParameterValue',
                                      {}).get('value', '')
            self._send_slack_message(f"빌드 파라미터 {param_name} 의 값을 입력해주세요. "
                                     f"기본값: {default_value}")

            while True:
                input_parameter = await self._get_conversation_input(
                    channel, user)

                input_parameter = input_parameter.strip().replace(
                    ' ', '').replace('\n', '').replace('\t', '')

                if self._validate_input_parameter(param, input_parameter):
                    break

                self._send_slack_message('잘못된 입력입니다. 다시 입력해주세요.')

            if input_parameter == 'ad':
                break
            elif input_parameter == 'd':
                pass
            elif input_parameter == '취소' or input_parameter == 'cancel':
                return None
            else:
                if input_parameter in ['False', 'True']:
                    input_parameter = input_parameter.lower()

                parameter_dict[param_name] = input_parameter

        return parameter_dict

    @staticmethod
    def _validate_input_parameter(param, input_parameter):
        is_bool_parameter = 'bool' in param['type'].lower()

        if is_bool_parameter and input_parameter not in [
                'true', 'True', 'false', 'False', 'ad', 'd', '취소', 'cancel'
        ]:
            return False

        return True

    async def _get_conversation_input(self, channel, user):
        while True:
            received_message = await self._receive_user_message()

            # FIXME: 지금은 대화메세지가 아니면 다시 일반 명령어로 처리해주지만, 구조적 개선이 필요하다.
            # FIXME: RTM 메세지 처리 클래스, 대화 클래스, 봇 클래스가 분리되는 편이 좋다.
            if self._last_message_user != user or self._last_message_channel != channel:
                await self._treat_received_message(message=received_message)

            else:
                break
            await asyncio.sleep(0.1)

        return received_message

    async def 빌드취소(self, 작업이름):
        if not self.jenkins.has_job(작업이름):
            return "해당 작업이 존재하지 않습니다."

        job = self.jenkins.get_job(작업이름)

        if not job.is_queued_or_running():
            return "실행중인 빌드가 없습니다."

        job.get_last_build().stop()

        build_num = job.get_last_buildnumber()

        return f"{작업이름} #{build_num}을 취소하였습니다."
Пример #47
0
def get_job_status(job_name):
    job = Jenkins('http://172.30.205.179:9090')
    #job = Jenkins('http://10.2.162.80:9090')
    return job.get_job(job_name).get_last_build().get_status()
Пример #48
0
 def __init__(self, jenkins=None):
     self._jenkins = jenkins or Jenkins()
Пример #49
0
        pswd = getpass.getpass()
    return username, pswd


#************************************************8
username, password = login(machine)
LOG_DIR = clean_sandbox("sandbox")


def dump_json(j):
    return json.dumps(j, sort_keys=True, indent=2)


#************************************************8

J = Jenkins(jenkins_url, username, password)

print "\nCurrent jobs available at %s" % jenkins_url
print J.keys()
print "\nChecking this job:", J[jobname]
job = J[jobname]
print "\nGetting %s job config" % jobname
print job.get_config
print "\nlast good build:"
lgb = job.get_last_good_build()
print "\nlast good build revision:"
print lgb.get_revision()

from jenkinsapi.api import get_latest_complete_build
from jenkinsapi.api import get_latest_test_results
Пример #50
0
def get_server_instance():
    jenkins_url = 'http://jenkins.ci.snsshop.net'
    server = Jenkins(jenkins_url, username='******', password='******')
    return server
Пример #51
0
from jenkinsapi.jenkins import Jenkins

JENKINS_RA_FOLDER_URL = "http://192.168.17.139:8080/"
jenkins_server_obj = Jenkins(JENKINS_RA_FOLDER_URL,
                             username='******',
                             password='******',
                             lazy=True,
                             ssl_verify=False)
JENKINS_INSTALLER_JOB_NAME = "selenium"

#start a job
jenkins_server_obj.build_job(JENKINS_INSTALLER_JOB_NAME)
jenkins_job_instance = jenkins_server_obj.get_job(JENKINS_INSTALLER_JOB_NAME)

#get job list
print(jenkins_server_obj.get_jobs_list())
#get job selenium
print(jenkins_job_instance)
#global message
jenkins_server_obj.pprint()
#job config.xml
xml = jenkins_server_obj['selenium'].get_config()
print(xml)
'''
#CURB JOB
jenkins_server_obj.create_job('RF1',xml)
jenkins_server_obj.delete_job('RF1')
jenkins_server_obj.copy_job('selenium','selenium3')
jenkins_server_obj.rename_job('selenium1','selenium4')
'''
#job list
Пример #52
0
def get_server_instance():
    server = Jenkins(JenkinsHelper.jenkins_base_url,
                     username=JenkinsHelper.jenkins_user,
                     password=JenkinsHelper.jenkins_password)
    return server
from jenkinsapi.jenkins import Jenkins
from jenkinsapi.credential import UsernamePasswordCredential, SSHKeyCredential
import socket

api = Jenkins('http://jenkins:8080')
# Get a list of all global credentials
credentials = api.credentials
for key in credentials.credentials.keys():
    creds_dict = credentials.credentials[key].get_attributes()
    if creds_dict['json']['credentials'][
            'description'] == 'jenkinsslave1 credentials':
        credentialsId = creds_dict['json']['credentials']['id']
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(('jenkins', 9001))
        s.send('/var/jenkins_home/config.xml JENKINSSLAVE1_CREDENTIALSID ' +
               credentialsId)
Пример #54
0
def get_server_instance():
    server = Jenkins(jenkins_url, username, password)
    return server
Пример #55
0
 def __init__(self, bot_slack, user_slack, jenkins_url):
     self.favorites = []
     self.jenkins = Jenkins(jenkins_url, ssl_verify=False)
     BaseBot.__init__(self, bot_slack, user_slack)
Пример #56
0
class TestPlugins(unittest.TestCase):
    DATA = {
        'plugins': [
            {
                'deleted': False,
                'hasUpdate': True,
                'downgradable': False,
                'dependencies': [{}, {}, {}, {}],
                'longName': 'Jenkins Subversion Plug-in',
                'active': True,
                'shortName': 'subversion',
                'backupVersion': None,
                'url': 'http://wiki.jenkins-ci.org/display/JENKINS/Subversion+Plugin',
                'enabled': True,
                'pinned': False,
                'version': '1.45',
                'supportsDynamicLoad': 'MAYBE',
                'bundled': True
            },
            {
                'deleted': False,
                'hasUpdate': True,
                'downgradable': False,
                'dependencies': [{}, {}],
                'longName': 'Maven Integration plugin',
                'active': True,
                'shortName': 'maven-plugin',
                'backupVersion': None,
                'url': 'http://wiki.jenkins-ci.org/display/JENKINS/Maven+Project+Plugin',
                'enabled': True,
                'pinned': False,
                'version': '1.521',
                'supportsDynamicLoad': 'MAYBE',
                'bundled': True
            }
        ]
    }

    @mock.patch.object(Jenkins, '_poll')
    def setUp(self, _poll_jenkins):
        _poll_jenkins.return_value = {}

        self.J = Jenkins('http://*****:*****@mock.patch.object(Plugins, '_poll')
    def test_get_plugins(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        # Can we produce a repr string for this object
        self.assertIsInstance(self.J.get_plugins(), Plugins)

    @mock.patch.object(Plugins, '_poll')
    def test_no_plugins_str(self, _poll_plugins):
        _poll_plugins.return_value = {}

        plugins = self.J.get_plugins()
        self.assertEquals(str(plugins), "[]")

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_str(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        plugins = self.J.get_plugins()
        self.assertEquals(str(plugins), "['maven-plugin', 'subversion']")

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_len(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        plugins = self.J.get_plugins()
        self.assertEquals(len(plugins), 2)

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_contains(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        plugins = self.J.get_plugins()
        self.assertIn('subversion', plugins)
        self.assertIn('maven-plugin', plugins)

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_values(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        p = Plugin(
            {
                'deleted': False,
                'hasUpdate': True,
                'downgradable': False,
                'dependencies': [{}, {}, {}, {}],
                'longName': 'Jenkins Subversion Plug-in',
                'active': True,
                'shortName': 'subversion',
                'backupVersion': None,
                'url': 'http://wiki.jenkins-ci.org/display/JENKINS/Subversion+Plugin',
                'enabled': True,
                'pinned': False,
                'version': '1.45',
                'supportsDynamicLoad': 'MAYBE',
                'bundled': True
            }
        )

        plugins = self.J.get_plugins().values()
        self.assertIn(p, plugins)

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_keys(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        plugins = self.J.get_plugins().keys()
        self.assertIn('subversion', plugins)
        self.assertIn('maven-plugin', plugins)

    @mock.patch.object(Plugins, '_poll')
    def test_plugins_empty(self, _poll_plugins):
        _poll_plugins.return_value = {}

        # list() is required here for python 3.x compatibility
        plugins = list(self.J.get_plugins().keys())
        self.assertEquals([], plugins)

    @mock.patch.object(Plugins, '_poll')
    def test_plugin_get_by_name(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA

        p = Plugin(
            {
                'deleted': False,
                'hasUpdate': True,
                'downgradable': False,
                'dependencies': [{}, {}, {}, {}],
                'longName': 'Jenkins Subversion Plug-in',
                'active': True,
                'shortName': 'subversion',
                'backupVersion': None,
                'url': 'http://wiki.jenkins-ci.org/display/JENKINS/Subversion+Plugin',
                'enabled': True,
                'pinned': False,
                'version': '1.45',
                'supportsDynamicLoad': 'MAYBE',
                'bundled': True
            }
        )

        plugin = self.J.get_plugins()['subversion']
        self.assertEquals(p, plugin)

    @mock.patch.object(Plugins, '_poll')
    def test_get_plugin_details(self, _poll_plugins):
        _poll_plugins.return_value = self.DATA
        plugin = self.J.get_plugins()['subversion']
        self.assertEquals('1.45', plugin.version)
        self.assertEquals('subversion', plugin.shortName)
        self.assertEquals('Jenkins Subversion Plug-in', plugin.longName)
        self.assertEquals('http://wiki.jenkins-ci.org/display/JENKINS/Subversion+Plugin',
                          plugin.url)

    @mock.patch.object(Requester, 'post_xml_and_confirm_status')
    def test_install_plugin_bad_input(self, _post):
        with self.assertRaises(ValueError):
            self.J.install_plugin('test')

    @mock.patch.object(Requester, 'post_xml_and_confirm_status')
    def test_install_plugin_good_input(self, _post):
        self.J.install_plugin('[email protected]')
        expected_data = '<jenkins> <install plugin="[email protected]" /> </jenkins>'
        _post.assert_called_with(
            '/'.join([self.J.baseurl,
                      'pluginManager',
                      'installNecessaryPlugins']),
            data=expected_data)

    @mock.patch.object(Requester, 'post_xml_and_confirm_status')
    @mock.patch.object(Jenkins, 'safe_restart')
    def test_install_plugins_good_input_no_restart(self, _restart, _post):
        self.J.install_plugins(['[email protected]', '[email protected]'])
        self.assertEqual(_post.call_count, 2)
        self.assertEqual(_restart.call_count, 0)

    @mock.patch.object(Requester, 'post_xml_and_confirm_status')
    @mock.patch.object(Jenkins, 'safe_restart')
    def test_install_plugins_good_input_with_restart(self, _restart, _post):
        self.J.install_plugins(['[email protected]', '[email protected]'], restart=True)
        self.assertEqual(_post.call_count, 2)
        self.assertEqual(_restart.call_count, 1)

    def test_plugin_repr(self):
        p = Plugin(
            {
                'shortName': 'subversion',
            }
        )
        self.assertEquals(repr(p), '<jenkinsapi.plugin.Plugin subversion>')
Пример #57
0
def querynodes(jenkinsurl):
    J = Jenkins(jenkinsurl)
    return J.get_nodes().keys()
Пример #58
0
def trigger_build(base_url,
                  user_name,
                  user_token,
                  job_name,
                  job_token,
                  job_cause=None,
                  job_params=None,
                  timeout=60 * 30):
    u"""
    Trigger a jenkins job/project (note that jenkins uses these terms interchangeably)

    Args:
        base_url (str): The base URL for the jenkins server, e.g. https://test-jenkins.testeng.edx.org
        user_name (str): The jenkins username
        user_token (str): API token for the user. Available at {base_url}/user/{user_name)/configure
        job_name (str): The Jenkins job name, e.g. test-project
        job_token (str): Jobs must be configured with the option "Trigger builds remotely" selected.
            Under this option, you must provide an authorization token (configured in the job)
            in the form of a string so that only those who know it would be able to remotely
            trigger this project's builds.
        job_cause (str): Text that will be included in the recorded build cause
        job_params (set of tuples): Parameter names and their values to pass to the job
        timeout (int): The maximum number of seconds to wait for the jenkins build to complete (measured
            from when the job is triggered.)

    Returns:
        A the status of the build that was triggered

    Raises:
        BackendError: if the Jenkins job could not be triggered successfully
    """
    @backoff.on_predicate(
        backoff.constant,
        interval=60,
        max_tries=timeout / 60 + 1,
        on_giveup=_poll_giveup,
        # We aren't worried about concurrent access, so turn off jitter
        jitter=None,
    )
    def poll_build_for_result(build):
        u"""
        Poll for the build running, with exponential backoff, capped to ``timeout`` seconds.
        The on_predicate decorator is used to retry when the return value
        of the target function is True.
        """
        return not build.is_running()

    # Create a dict with key/value pairs from the job_params
    # that were passed in like this:  --param FOO bar --param BAZ biz
    # These will get passed to the job as string parameters like this:
    # {u'FOO': u'bar', u'BAX': u'biz'}
    request_params = {}
    for param in job_params:
        request_params[param[0]] = param[1]

    # Contact jenkins, log in, and get the base data on the system.
    try:
        jenkins = Jenkins(base_url, username=user_name, password=user_token)
    except (JenkinsAPIException, HTTPError) as err:
        raise BackendError(str(err))

    if not jenkins.has_job(job_name):
        msg = u'Job not found: {}.'.format(job_name)
        msg += u' Verify that you have permissions for the job and double check the spelling of its name.'
        raise BackendError(msg)

    # This will start the job and will return a QueueItem object which can be used to get build results
    job = jenkins[job_name]
    queue_item = job.invoke(securitytoken=job_token,
                            build_params=request_params,
                            cause=job_cause)
    LOG.info(u'Added item to jenkins. Server: {} Job: {} '.format(
        jenkins.base_server_url(), queue_item))

    # Block this script until we are through the queue and the job has begun to build.
    queue_item.block_until_building()
    build = queue_item.get_build()
    LOG.info(u'Created build {}'.format(build))
    LOG.info(u'See {}'.format(build.baseurl))

    # Now block until you get a result back from the build.
    poll_build_for_result(build)

    # Update the build's internal state, so that the final status is available
    build.poll()

    status = build.get_status()
    LOG.info(u'Build status: {status}'.format(status=status))
    return status
Пример #59
0
                       action="store_true",
                       help="Statistics per country")
    group.add_argument("--global-stats",
                       dest="stats_global",
                       action="store_true",
                       help="Global statistics")

    args = parser.parse_args()

    if not args.num_builds:
        if args.stats_global:
            args.num_builds = 0
        else:
            args.num_builds = 5

    J = Jenkins('http://jenkins.osmose.openstreetmap.fr')

    all_country = []
    list_country = set()
    for country in args.country:
        found = False
        if country in J:
            list_country.add(country)
            found = True
        elif country.endswith("*"):
            if not all_country:
                all_country = J.keys()
                all_country.remove("osmose-frontend")
                all_country.remove("osmose-backend")
            for c in all_country:
                if c.startswith(country[:-1]):
Пример #60
0
 def __init__(self,url,username,password):
     self.server=Jenkins(url, username=username, password=password)