def main(build_url, sleeptime=15, pushover=False, user=None, password=None):
    if pushover and not have_pushover:
        raise SystemExit(
            "ERROR: to use pushover notifications, please `pip install python-pushover` and configure it."
        )
    job_name, build_no = get_job_name_and_build_number(build_url)
    logger.info("Job: %s", job_name)
    logger.info("Build Number: %s", build_no)
    jenkins_url = get_jenkins_base_url(build_url)
    logger.debug("Connecting to Jenkins...")

    if user is not None:
        logger.debug("Connecting to Jenkins as user %s ...", user)
        j = Jenkins(jenkins_url, user, password)
    else:
        logger.debug("Connecting to Jenkins anonymously...")
        j = Jenkins(jenkins_url)
    logger.debug("Connected.")

    if build_no is None:
        jobinfo = j.get_job_info(job_name)
        build_no = jobinfo['nextBuildNumber'] - 1
        logger.info("Using latest build, #{b}".format(b=build_no))
    build_url = get_formal_build_url(jenkins_url, job_name, build_no)
    print("Watching job {j} #{b} until completion <{u}>...".format(
        j=job_name, b=build_no, u=build_url))
    while True:
        logger.debug("Getting build info...")
        buildinfo = j.get_build_info(job_name, build_no)
        if not buildinfo['building']:
            # job is not still building
            duration = datetime.timedelta(seconds=(buildinfo['duration'] /
                                                   1000))
            if pushover:
                notify_pushover(buildinfo['result'], job_name, build_no,
                                duration, build_url)
            if buildinfo['result'] == "SUCCESS":
                print("SUCCESS for {j} #{b} in {d} <{bu}>".format(j=job_name,
                                                                  b=build_no,
                                                                  bu=build_url,
                                                                  d=duration))
                raise SystemExit(0)
            print("{r}: {j} #{b} failed in {d}".format(j=job_name,
                                                       b=build_no,
                                                       r=buildinfo['result'],
                                                       d=duration))
            raise SystemExit(1)
        else:
            duration = datetime.datetime.now(
            ) - datetime.datetime.fromtimestamp(buildinfo['timestamp'] / 1000)
            print("still running ({d})...".format(d=duration))
        time.sleep(sleeptime)
Beispiel #2
0
def get_plugins_dict(jenkins_url, puppet=False, user=None, password=None):
    if user is not None:
        logger.debug("Connecting to Jenkins (<%s>) as user %s ...",
                     jenkins_url, user)
        j = Jenkins(jenkins_url, user, password)
    else:
        logger.debug("Connecting to Jenkins (<%s>) anonymously...",
                     jenkins_url)
        j = Jenkins(jenkins_url)
    logger.debug("Connected.")
    p = j.get_plugins()
    plugins = {}
    for k, v in p.items():
        plugins[k[0]] = v['version']
    return plugins
Beispiel #3
0
    def connect_to_jenkins_server(self):

        # Because if there is a problem in reading server.conf file
        # both IP address and Port number becomes none
        # so we just check server address to be none or not


        fh=open("C:\\Users\{0}\JC\controls".format(getuser()))
        for line in fh:
            if line.startswith('address'):
                self.JenkinsServerAddress=(line.split("=")[1]).strip()
            elif line.startswith('port'):
                self.JenkinsServerPort=(line.split("=")[1]).strip()
        fh.close()

        if self.JenkinsServerAddress !='none':
            print("Connecting {0}:{1}".format(self.JenkinsServerAddress, self.JenkinsServerPort))
            UserName = input("Username : "******"Password : "******"Connected successfully as {2}".format(self.JenkinsServerAddress, self.JenkinsServerPort, UserName))
            except:
                self.ServerHandler = None
                print("Connection to server Failed")

        else:
            print("-Server configuration parameters are not defined properly")
            print("-Check server.conf for parameters and then use jcr.exe to set them")
Beispiel #4
0
    def __init__(self, config: RootConfig):
        self.config = config
        self._create_dirs()

        if config.db_type == Postgres.DB_TYPE:
            self.db = Postgres(config.db)

        elif config.db_type == Mssql.DB_TYPE:
            self.db = Mssql(config.db)

        elif config.db_type == Pgdocker.DB_TYPE:
            self.db = Pgdocker(config.db)

        else:
            raise RuntimeError('Unsupported database type')
        self._write_hibernate_properties()

        self.jenkins = Jenkins(config.jenkins)

        os.environ['CATALINA_OPTS'] = config.catalina_opts
        self.tomcat = None
        self.last_error = None
        self.active_task = None
        self.last_task = None

        log.info('Test tools started')
Beispiel #5
0
 def create_docker_sync_job(self, obj):
     # s = {r'/': r'...'}
     # jn = utils.multiple_replace(jobname, s)
     s = utils.randomstr(8)
     jn = '__docker_sync_job_' + s
     cid = obj['sync_cloud_id']
     jsonUtil = JsonUtil()
     c = jsonUtil.parseJsonString(config.CLOUD_CONFIG)
     j = Jenkins(c[cid]['jenkins_url'],
                 username=c[cid]['jenkins_name'],
                 password=c[cid]['jenkins_token'])
     re = postimagesync()
     re.time = datetime.now()
     re.sync_cloud_id = obj['sync_cloud_id']
     re.image_name = obj['image_name']
     re.post_callback_url = obj['post_callback_url']
     re.tag = obj['tag']
     re.status = 'started'
     try:
         if j.job_exists(jn):
             jn = jn + utils.randomstr(4)
             j.create_job(jn, self.edit_docker_sync_job_config(obj))
             yield gen.sleep(0.5)
             j.build_job(jn)
         j.create_job(jn, self.edit_docker_sync_job_config(obj))
         yield gen.sleep(0.5)
         j.build_job(jn)
     except Exception as e:
         print e.message
         re.status = 'error'
     raise gen.Return(re)
Beispiel #6
0
 def __init__(self, jenkinsURL):
     self.j = Jenkins(jenkinsURL)
     self.jobName = ""
     #with open("config.xml") as file:
     with open("./builds/config/job/config.xml") as file:
         self.templateConfig = file.read()
     self.template = Template(unicode(self.templateConfig))
Beispiel #7
0
 def __init__(self, url, username, password):
     self.url = url
     self.username = username
     self.password = password
     self.jenkins = Jenkins(url=self.url,
                            username=self.username,
                            password=self.password)
Beispiel #8
0
def main(build_url, sleeptime=15, pushover=False):
    if pushover and not have_pushover:
        raise SystemExit("ERROR: to use pushover notifications, please `pip install python-pushover` and configure it.")
    job_name, build_no = get_job_name_and_build_number(build_url)
    jenkins_url = get_jenkins_base_url(build_url)
    logger.debug("Connecting to Jenkins...")
    """
    NOTE: this is using unauthenticated / anonymous access.
    If that doesn't work for you, change this to something like:
    j = Jenkins(jenkins_url, 'username', 'password')
    """
    j = Jenkins(jenkins_url)
    logger.debug("Connected.")
    if build_no is None:
        jobinfo = j.get_job_info(job_name)
        build_no = jobinfo['nextBuildNumber'] - 1
        print("Using latest build, #{b}".format(b=build_no))
    build_url = get_formal_build_url(jenkins_url, job_name, build_no)
    print("Watching job {j} #{b} until completion <{u}>...".format(j=job_name, b=build_no, u=build_url))
    while True:
        buildinfo = j.get_build_info(job_name, build_no)
        if not buildinfo['building']:
            # job is not still building
            duration = datetime.timedelta(seconds=(buildinfo['duration'] / 1000))
            if pushover:
                notify_pushover(buildinfo['result'], job_name, build_no, duration, build_url)
            if buildinfo['result'] == "SUCCESS":
                print("SUCCESS for {j} #{b} in {d} <{bu}>".format(j=job_name, b=build_no, bu=build_url, d=duration))
                raise SystemExit(0)
            print("{r}: {j} #{b} failed in {d}".format(j=job_name, b=build_no, r=buildinfo['result'], d=duration))
            raise SystemExit(1)
        else:
            duration = datetime.datetime.now() - datetime.datetime.fromtimestamp(buildinfo['timestamp'] / 1000)
            print("still running ({d})...".format(d=duration))
        time.sleep(sleeptime)
Beispiel #9
0
 def connect(self):
     """
     连接jenkins(实例化jenkins)
     :return:
     """
     server = Jenkins(self.url, username=self.username, password=self.password)
     return server
Beispiel #10
0
def slave_create(node_name, working_dir, executors, labels):
    j = Jenkins(os.environ['JENKINS_URL'], jenkins_user, jenkins_password)
    j.node_create(node_name,
                  working_dir,
                  num_executors=int(executors),
                  labels=labels,
                  launcher=NodeLaunchMethod.JNLP)
Beispiel #11
0
 def __init__(self, token):
     """
     Create the JenkinsTrigger instance.
     """
     self.token = token
     self.repo_name = get_repo_name()
     self.jenkins_instance = Jenkins(JENKINS_URL)
Beispiel #12
0
    def post_docker_sync_hook(self, obj, cloudid):
        jn = obj['name']
        # bid = str(obj['build']['number'])
        jsonUtil = JsonUtil()
        c = jsonUtil.parseJsonString(config.CLOUD_CONFIG)
        j = Jenkins(c[cloudid]['jenkins_url'],
                    username=c[cloudid]['jenkins_name'],
                    password=c[cloudid]['jenkins_token'])

        re = postimagesync()
        try:

            if j.job_exists(jn):
                ss = xmltodict.parse(j.get_job_config(jn))
                jsonpickle.set_preferred_backend('json')

                if isinstance(jsonpickle.decode(ss['project']['description']),
                              callback):
                    desobj = jsonpickle.decode(ss['project']['description'])
                    re.image_name = desobj.image_name
                    re.status = self.getstatus(obj['build']['status'])
                    re.sync_cloud_id = desobj.repo_name
                    re.tag = desobj.tag
                    re.time = datetime.now()
                    re.post_callback_url = desobj.callback_url
                    if re.status != 'error' and config.JENKINS_IMAGEOPTJOB_DELETE == 'true':
                        j.delete_job(jn)

        except Exception as e:
            print e.message
            re = None
        raise gen.Return(re)
Beispiel #13
0
def main():
    parser = ArgumentParser()
    parser.add_argument('jobname', type=str, default=None)
    parser.add_argument('--buildno', type=int)

    args = parser.parse_args()

    japi = Jenkins(jenkins_host, username=jenkins_user, password=jenkins_pass)

    # Input: jobname (without -source, etc)
    # 1. Get info for -source job (and check 'result' == 'SUCCESS')
    # 2. Get matching -binaries job (and check 'result' == 'SUCCESS')
    # 3. Get runs for -binaries job (and check 'result' == 'SUCCESS')
    # 4. For each -binaries run, find repos jobs (we will find all repos jobs for all runs, but we deal with that)
    # 5. With each repos job, check 'result' == 'SUCCESS'.

    binaries_name, binaries_buildno = wait_for_source_job(
        japi, args.jobname, args.buildno)
    print('binaries_name, binaries_buildno:', binaries_name, binaries_buildno)
    print('Source OK')

    repos_name, repos_buildnos = wait_for_binaries_job(japi, binaries_name,
                                                       binaries_buildno)
    assert len(repos_buildnos) > 0
    print('repos_buildnos:', repos_buildnos)
    print('Binaries OK')

    for buildno in repos_buildnos:
        wait_for_repos_job(japi, repos_name, buildno)
    print('Repos OK')
Beispiel #14
0
 def getjenkinsviews(self, msg, args):
     server = Jenkins(JENKINS_URL,
                      username=JENKINS_USERNAME,
                      password=JENKINS_PASSWORD)
     #server = Jenkins(JENKINS_URL, username=JENKINS_USERNAME, password=JENKINS_PASSWORD)
     views = server.get_views()
     return views.json()
Beispiel #15
0
def start_job(server_label, job_name):
    job_data = models.Servers.query.filter_by(label=server_label).first()
    job_settings = configure_build(job_data.address, job_data.branch,
                                   job_data.env)
    server = Jenkins(jenkins_server, jenkins_user, jenkins_pass)
    server.build_job(job_name, job_settings)
    return "task planned"
Beispiel #16
0
def init_jenkins():
    jenkins_url = f'http://{jenkins_domain}'
    username = jenkins_user
    password = jenkins_pass

    server = Jenkins(jenkins_url, username=username, password=password)
    return server
Beispiel #17
0
 def __init__(self):
     self.url = config.JENKINS_URL
     self.username = config.JENKINS_NAME
     self.token = config.JENKINS_TOKEN
     self.j = Jenkins(config.JENKINS_URL,
                      username=config.JENKINS_NAME,
                      password=config.JENKINS_TOKEN)
Beispiel #18
0
 def connect_to_jenkins(self):
     """Connect to a Jenkins instance using configuration."""
     self.log.debug('Connecting to Jenkins ({0})'.format(
         self.config['URL']))
     self.jenkins = Jenkins(url=self.config['URL'],
                            username=self.config['USERNAME'],
                            password=self.config['PASSWORD'])
     return
def build_jobs(credentials, test_plans, args):
    jenkins = Jenkins('http://juju-ci.vapour.ws:8080', *credentials)
    for test_plan in test_plans:
        test_id = generate_test_id()
        for controller in args.controllers:
            job_name = get_job_name(controller)
            parameter = make_parameters(test_plan, args, controller, test_id)
            jenkins.build_job(job_name, parameter, token=args.cwr_test_token)
Beispiel #20
0
 def getjobs(self, msg, args):
     #server = self.get_server_instance()
     server = Jenkins(JENKINS_URL,
                      username=JENKINS_USERNAME,
                      password=JENKINS_PASSWORD)
     for j in server.get_jobs():
         jobs = server.get_job(j[0])
         return jobs.name
Beispiel #21
0
def slave_create(node_name, working_dir, executors, labels):
    print(node_name, working_dir, executors, labels)
    j = Jenkins(os.environ['JENKINS_URL'], os.environ['JENKINS_USER'],
                os.environ['JENKINS_PASS'])
    j.node_create(node_name,
                  working_dir,
                  num_executors=int(executors),
                  labels=labels,
                  launcher=NodeLaunchMethod.JNLP)
def main(jenkins_url, user=None, password=None):
    if user is not None:
        logger.debug("Connecting to Jenkins as user %s ...", user)
        j = Jenkins(jenkins_url, user, password)
    else:
        logger.debug("Connecting to Jenkins anonymously...")
        j = Jenkins(jenkins_url)
    logger.debug("Connected.")
    p = j.get_plugins()
    plugins = {}
    namelen = 0
    for k, v in p.items():
        plugins[k[0]] = v['version']
        if len(k[0]) > namelen:
            namelen = len(k[0])
    # format
    for name, ver in sorted(plugins.items()):
        print("  jenkins::plugin {'%s': version => '%s'}" % (name, ver))
def build_job(credentials, root, job_name, candidates, suite):
    parameters = {'suite': ','.join(suite), 'attempts': '10'}
    jenkins = Jenkins('http://juju-ci.vapour.ws:8080', credentials.user,
                      credentials.password)
    for candidate, revision_build in candidates:
        call_parameters = {
            'revision_build': '{:d}'.format(revision_build),
        }
        call_parameters.update(parameters)
        jenkins.build_job(job_name, call_parameters)
Beispiel #24
0
 def __init__(self, username, password, can_use_colour):
     """
     Create the JenkinsTrigger instance.
     """
     self.jobs = {}
     self.can_use_colour = can_use_colour and not os.name.startswith('nt')
     self.repo_name = get_repo_name()
     self.server = Jenkins(JENKINS_URL,
                           username=username,
                           password=password)
Beispiel #25
0
 def __init__(self, jenkins_url, login_id, password):
     """
     Initialize the jenkins connection object
     :param jenkins_url:
     :param login_id:
     :param password:
     """
     self._jenkins_url = jenkins_url
     self._login_id = login_id
     self._password = password
     self.server_obj = Jenkins(jenkins_url, username=self._login_id, password=self._password)
Beispiel #26
0
def jenkin_server_instance(url, username, password):
    print("Connecting to jenkin instance \n")
    try:
        server = Jenkins(url, username, password)
        user = server.get_whoami()
        version = server.get_version()
        print("Hello %s from Jenkins %s \n" % (user["fullName"], version))
        return server
    except JenkinsException:
        print("Authentication Error: Invalid Password/Token for user \n")
    return
Beispiel #27
0
    def run(self):
        # Run the egg_info step to find our VCS url.
        self.run_command('egg_info')
        if not self.distribution.metadata.url:
            log.warn("This package does not appear to be in any repository, "
                     "aborting.")
            sys.exit(1)

        # Pull down Jenkins package
        base.fetch_build_eggs(['python-jenkins'], dist=self.distribution)
        from jenkins import Jenkins
        server = CONFIG.jenkins_url
        log.info("Connecting to Jenkins at %s" % server)

        jenkins = Jenkins(server, self.username, self.password)
        name = self.distribution.metadata.name

        if (self.matrix):
            log.info("Matrix job")
            if CONFIG.jenkins_matrix_job_xml:
                path, fname = CONFIG.jenkins_matrix_job.split(':')
            else:
                path, fname = None, 'jenkins_job_matrix.xml'
        else:
            log.info(
                "Non-matrix job - use \'--matrix\' option for matrix builds")
            if CONFIG.jenkins_job_xml:
                path, fname = CONFIG.jenkins_job.split(':')
            else:
                path, fname = None, 'jenkins_job.xml'

        with open(base.get_resource_file(fname, path)) as f:
            jenkins_config_xml = Template(f.read())

        cfg_xml = jenkins_config_xml.safe_substitute(
            name=cgi.escape(name),
            hyphen_escaped_name=cgi.escape(name).replace("-", "?").replace(
                "_", "?"),
            description=cgi.escape(self.distribution.metadata.description),
            repository=self.distribution.metadata.url,
            email=self.distribution.metadata.author_email,
            python_string_xml=self._construct_string_values(
                self._get_active_python_versions()),
            virtualenv=CONFIG.virtualenv_executable,
            username=self.username)

        if jenkins.job_exists(name):
            log.error(
                "Job found at %s/job/%s Please delete this before creating a new one."
                % (server, name))
        else:
            if (not self.dry_run):
                log.info("Creating job at %s/job/%s" % (server, name))
                jenkins.create_job(name, cfg_xml)
Beispiel #28
0
    def getConfig(self,job,jenkins_username,jenkins_password):
        # server = Jenkins("https://jenkins.cn-x-cloud-pg.com.cn",username="******",password="******",timeout=3600)

        server = Jenkins("https://jenkins.cn-x-cloud-pg.com.cn",username=jenkins_username,password=jenkins_password,timeout=3600)
        jobConfig = server.get_job_config(job)
        root = ET.fromstring(jobConfig)
        for i in root.iter('url'):
            gitlabProject = i.text.split('/')[-1].split('.')[0]
        for j in root.iter('scriptPath'):
            scriptPath = j.text
        return (i.text,gitlabProject,scriptPath)
Beispiel #29
0
 def _contact_jenkins_server(self) -> str:
     try:
         server: Jenkins = Jenkins(self.server_url)
         last_build_number: int = (server.get_job_info(
             self.job_name)['lastBuild']['number'])
         return (server.get_build_info(self.job_name,
                                       last_build_number)['result'])
     except JenkinsException:
         raise StateNotFoundError(self.name)
     except (requests_exceptions.ConnectionError,
             url_exceptions.NewConnectionError):
         raise NoInternetError(self.name)
def jenkins():
    print('Looking for a running Jenkins instance')
    webapi = Jenkins('http://127.0.0.1:60888')

    print('Removing all jobs ...')
    for job in webapi.jobs:
        webapi.job_delete(job.name)

    print('Creating test jobs ...')
    configxml = 'master-job-svn-config.xml'
    configxml = open(configxml).read().encode('utf8')
    webapi.job_create('master-job-svn', configxml)
    return webapi