Example #1
0
 def _upsert_jenkins_job(server: Jenkins, full_job_name: str, xml_job_config: str):
     if server.job_exists(full_job_name):
         job = server.get_job_info(full_job_name)
     else:
         print(f'Jenkins job {full_job_name} doesn''t exist.')
         job = None
     config_xml_string = canonicalize_jenkins_xml(xml_job_config)
     if job is None and writeback:
         print(f'Creating new job {full_job_name}')
         try:
             server.create_job(full_job_name, config_xml_string)
         except JenkinsException as e:
             print(f'Failed creating job:\n{e}')
     elif job is not None:
         current_xml_string = canonicalize_jenkins_xml(server.get_job_config(full_job_name))
         diffs = diff_files(BytesIO(bytearray(current_xml_string, encoding='utf-8')),
                            BytesIO(bytearray(config_xml_string, encoding='utf-8')))
         if len(diffs) > 0:
             print(f'Jenkins job {full_job_name} needs update')
             if writeback:
                 print(json.dumps(diffs, indent=4))
                 if input(f'Are you sure you want to update configuration for {full_job_name} (y/n) ? ') == 'y':
                     print(f'Updating job configuration for {full_job_name}')
                     try:
                         server.reconfig_job(full_job_name, config_xml_string)
                     except JenkinsException as e:
                         print(f'Failed updating job:\n{e}')
Example #2
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:
            print "This package does not appear to be in any repository, aborting."
            sys.exit(1)

        # Pull down jenkins package
        self.fetch_build_eggs(['python_jenkins'])

        from jenkins import Jenkins

        print "Connecting to Jenkins at %s" % self.server

        # Prompt for password or use command-line
        if self.no_prompt:
            if self.password is None:
                print "Must specify password if no-prompt is set."
                sys.exit(1)
            password = self.password
        else:
            password = getpass.getpass("Please enter your Jenkins password: "******"Job already exists, update configuration? [y/n]")
                    if not user_input in ('y', 'n'):
                        continue
                    break
                if user_input == 'n':
                    return

            print "Reconfiguring job at %s/job/%s" % (self.server, name)
            jenkins.reconfig_job(name, cfg_xml)
        else:
            print "Creating job at %s/job/%s" % (self.server, name)
            jenkins.create_job(name, cfg_xml)
Example #3
0
for i in jobs:
    # print(i['fullname'])
    tag = "Jenkinsfile-bak"
    full_name = i['fullname']
    if full_name in names:
        config = server.get_job_config(full_name)
        # if full_name == "check-account":
        #     print(config)
        result = re.search(r'<scriptId>(.*?)</scriptId>', config, re.M)
        if result:
            find = result.groups()[0]
            if find == "Jenkinsfile-Java-Business-Docker" or find == "Jenkinsfile-Java-Business":
                print(full_name, find)
                # if find == 'Jenkinsfile-bak':
                cc = config.replace(find, tag)
                server.reconfig_job(full_name, cc)
            # print(cc)
# for item in names:
#     tag = 'Jenkinsfile-Java-Business'
#     resp = requests.get("http://172.16.101.96:8080/job/{}/config.xml".format(item), auth=auth)
#     if resp.status_code == 200:
#         content = resp.content.decode("utf-8")
#         # print(content)
#         regex = re.compile('<scriptId>(.*?)</scriptId>')
#         result = re.search(r'<scriptId>(.*?)</scriptId>', content, re.M)
#         if result:
#             find = result.groups()[0]
#             if find == 'Jenkinsfile-bak':
#                 cc = content.replace(find, tag)
#                 with open('config.xml', 'wb') as f:
#                     f.write(cc.encode("utf-8"))
Example #4
0
class JC:
    def __init__(self):
        self.JCDirectoryLoc = ''
        self.JCBuiltFileLoc = ''
        self.JCServerConfFileLoc = ''
        self.JenkinsServerAddress = 'none'
        self.JenkinsServerPort = 'none'
        self.ServerHandler = None
        self.IpRegex = '''^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 
            25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 
            25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 
            25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)'''

        self.JCSelectedJobCommands = {
            '(enable)': ' - Enable the specified job on Jenkins Server',
            '(disable)': ' - Disable the specified job on Jenkins Server',
            '(build)': ' - Build the specified job on Jenkins Server',
            '(stat [*])': ' - Displays status and general information about the specified job',
            '(?)': ' - Displays list of available commands',
            '(??)': ' - Displays list of available commands verbosely',
            '(quit/exit)': ' - Quit Job configuration mode'

        }

        self.JigGlobalCommands = {
            '(show local jobs [*])': ' - Displays all jobs saved and waiting in your local machine to be created on server',
            '(show server jobs)': ' - Displays all jobs currently on your jenkins server',
            '(show build queue)': ' - Displays all jobs in build queue',
            '(delete local job)': ' - Deletes jobs on your local machine',
            '(delete server job)': ' - Deletes jobs on your jenkins server',
            '(reconfig server job)': ' - Reconfigures an existing job',
            '(select server job)': ' - Selects job and enter job configuration mode',
            '(connect)': ' - Connects to jenkins server',
            '(disconnect)': ' - Disconnects from jenkins server',
            '(create server job)': ' - Creates a new job on server ',
            '(?)': ' - Displays list of available commands',
            '(??)': ' - Displays list of available commands verbosely',
            '(exit / quit)': ' - Quit JC program'

        }

    def initialize(self):

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


    def display_introduction(self):
        '''
        Display an introduction message for the program containing the version and status of
        development
        '''

        print("     Welcome to JC 1.4")
        print("JC is a project which creates an interface to manage your Jenkins Server for your projects")
        print("This project is still under development ")

    def display_list_of_global_mode_commands(self, verbos=False, search_key=None):
        '''
        Display list of available commands in global mode

        :param verbos:  display description of command(s) if true
        :param search_key: display all commands starts with the 'key' param . default is None meaning all available commands

        '''

        print("---------------Commands---------------")
        if search_key is None:
            for command, description in self.JigGlobalCommands.items():
                print(command, description if verbos else '')
        else:
            for command, description in self.JigGlobalCommands.items():

                if command.startswith('({0}'.format(search_key)):
                    print(command, description if verbos else '')
        print("---------------------------------------")

    def display_list_of_selected_job_mode_commands(self, verbos=False, search_key=None):

        '''
        Display list of available commands in selected job mode

        :param verbos: display description of command(s) if true
        :param search_key: display all commands starts with the 'key' param . default is None meaning all available commands

        '''

        print("---------------Commands---------------")
        if search_key is None:
            for command, description in self.JCSelectedJobCommands.items():
                print(command, description if verbos else '')
        else:
            for command, description in self.JCSelectedJobCommands.items():
                if command.startswith('({0}'.format(search_key)):
                    print(command, description if verbos else '')
        print("---------------------------------------")

    def show_local_jobs(self, Detail=False):
        '''
        Display list of local jobs
        :param Detail: If True , Displays more detail about local jobs
        :return:
        '''

        # TODO display format
        if len(listdir(self.JCDirectoryLoc)) == 0:
            print("No local job")
        else:
            if Detail:
                print("Status".ljust(8), "CRL".ljust(23), "CRS".ljust(23), "RCFG".ljust(23), "Name")
                print("-----".ljust(8), "--------".ljust(23), "--------".ljust(23), "--------".ljust(23), "-----")

            else:
                print("Status".ljust(8),"Name".ljust(20))
                print("-----".ljust(8), "-----".ljust(33))

            fhandle = open(self.JCBuiltFileLoc)
            for line in fhandle:
                line = line.strip()
                if line.startswith("#"):
                    pass
                else:
                    if Detail:
                        Status=(line.split("@")[0]).ljust(8)
                        CRL = ((line.split("@")[1]).split(".")[0]).ljust(23)
                        CRS = ((line.split("@")[2]).split(".")[0] if line.split("@")[2]!='None' else 'None').ljust(23)
                        RCFG = ((line.split("@")[3]).split(".")[0] if line.split("@")[3]!='None' else 'None').ljust(23)
                        Name= (line.split("@")[4])
                        print(Status,CRL,CRS,RCFG,Name)
                    else:
                        Status = (line.split("@")[0]).ljust(8)
                        Name= (line.split("@")[4])
                        print(Status,Name)

            fhandle.close()



    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")


    def is_connected(self):
        '''
        Check whether we are connected to server or not
        '''
        return True if self.ServerHandler else False

    def disconnect_from_jenkins_server(self, place=None):
        '''
        Disconnect from the server
        :param place: Defines the place we are disconnecting . Displayed message can be different based on this parameter
        '''
        # TODO Close the TCP PORT

        if self.ServerHandler == None:
            # Disconnect and Stay in the program
            if place is None:
                print("You are already disconnected")
            return
        self.ServerHandler = None
        # Disconnect while exiting the program
        print("You are disconnected successfully")

    def create_server_job(self, JobName):
        '''
        Create a server job based on a config.xml file existing on the local machine
        :param JobName: job's name to be created
        '''

        if self.ServerHandler is None:
            print("Your are not connected to server")
            print("First connect by 'connect' command")
            return
        if not path.exists('{0}{1}{2}'.format(self.JCDirectoryLoc, JobName, '.config.xml')):
            print("-Job '{0}' config.xml file does not exist on your local machine".format(JobName))
            print("First create a config.xml file for job {0} by jigjr command".format(JobName))
        else:
            ListServerJobs = [server_job["name"] for server_job in self.ServerHandler.get_jobs()]
            if JobName in ListServerJobs:
                print("-Job '{0}' already exists on the server".format(JobName))
                AskingResult = self.asking_user_decision("Do you want to recreate job {0}?[y/n] : ".format(JobName))

                if AskingResult:
                    self.ServerHandler.delete_job(JobName)
                    print("-Deleting job '{0}' from server ..".format(JobName))

                    fhandle = open(self.JCDirectoryLoc + JobName + ".config.xml")
                    SelectedJobXml = fhandle.read()
                    fhandle.close()
                    self.ServerHandler.create_job(JobName, SelectedJobXml)
                    print("-Job '{0}' recreated successfully".format(JobName))

                    # Recreate a job is similar to reconfigure that job
                    update_built_file(self.JCBuiltFileLoc, 'update-reconfigure-date', JobName,
                                      reconfig_date=datetime.now())

            else:
                AskingResult = self.asking_user_decision("Sure creating job {0}?[y/n] : ".format(JobName))
                if AskingResult:
                    fhandle = open(self.JCDirectoryLoc + JobName + ".config.xml")
                    SelectedJobXml = fhandle.read()
                    fhandle.close()
                    self.ServerHandler.create_job(JobName, SelectedJobXml)
                    print("-Job '{0}' created successfully".format(JobName))
                    update_built_file(self.JCBuiltFileLoc, 'update-server-create-date', JobName,
                                      server_creation_date=datetime.now())

    def reconfigure_server_job(self, JobName, LocalConfigJobName=None):
        '''
        Reconfigure a server job
        :param JobName: Server job to be configured
        :param LocalConfigJobName:  If None , JobName.config.xml file is searched in local machine , else, LocalConfigJobName.config.xml is searched
        '''
        if self.ServerHandler is None:
            print("Your are not connected to server")
            print("First connect by 'connect' command")
            return
        ListServerJobs = [server_job["name"] for server_job in self.ServerHandler.get_jobs()]
        if JobName not in ListServerJobs:
            print("-Job '{0}' does not exist on the server".format(JobName))

        else:
            ListLocalJobs = [job.split(".")[0] for job in listdir(self.JCDirectoryLoc)]

            # If the LocalConfigJobName is not specified use the JobName as defualt
            LocalConfigJobName = JobName if LocalConfigJobName is None else LocalConfigJobName

            if LocalConfigJobName not in ListLocalJobs:
                print("-Job {0} config.xml file does not exist in your local machine".format(LocalConfigJobName))

            else:
                print("{0}.config.xml found".format(LocalConfigJobName))
                AskingResult = self.asking_user_decision("Sure reconfiguring job {0}?[y/n] : ".format(JobName))
                if AskingResult:
                    fhandle = open(self.JCDirectoryLoc + LocalConfigJobName + ".config.xml")
                    SelectedJobXml = fhandle.read()
                    fhandle.close()
                    self.ServerHandler.reconfig_job(LocalConfigJobName, SelectedJobXml)
                    print("-Job '{0}' reconfigured successfully".format(LocalConfigJobName))
                    update_built_file(self.JCBuiltFileLoc, 'update-reconfigure-date', LocalConfigJobName,
                                      reconfig_date=datetime.now())

    def show_build_queue(self):
        '''
        Display list of jobs which are currently under building process
        '''
        if self.ServerHandler is None:
            print("Your are not connected to server")
            print("First connect by 'connect' command")
            return
        else:
            BuildQueue = self.ServerHandler.get_queue_info()
            if len(BuildQueue) == 0:
                print("No job in build queue")
                return
            else:
                BuildQueueCounter = 1
                for job in BuildQueue:
                    jobInfo = self.ServerHandler.get_job_info(job['task']['name'])
                    print("{0}: {1}\tBuilding No: {2}".format(BuildQueueCounter, job["task"]["name"],
                                                              jobInfo['nextBuildNumber']))
                    BuildQueueCounter += 1

    def show_server_jobs(self):
        '''
        Display list of jobs currently configured on the server
        '''
        if self.ServerHandler is None:
            print("Your are not connected to server")
            print("First connect by 'connect' command")
            return
        else:
            if len(self.ServerHandler.get_jobs()) == 0:
                print("No job currently on the server")
            else:
                ListServerJobs = [server_job["name"] for server_job in self.ServerHandler.get_jobs()]
                # This list is to reduce the latency of display of all jobs
                # First collect information of all jobs then display
                ReadyList = []
                for job in ListServerJobs:
                    JobInfo = self.ServerHandler.get_job_info(job)
                    JobStatus = 'Disabled' if JobInfo['disabled'] else 'Enabled'
                    JobTotalBuilds = "0" if JobInfo['lastBuild'] is None else JobInfo['lastBuild']['number']
                    JobLastBuild = "None" if JobInfo['lastBuild'] is None else datetime.fromtimestamp(
                        self.ServerHandler.get_build_info(job, JobInfo['lastBuild']['number'])['timestamp'] / 1000)
                    ReadyList.append([JobStatus, JobTotalBuilds, JobLastBuild, job])

                print("Status".ljust(8), "Build".ljust(7), "Timestamp".ljust(22), "Name".ljust(20))
                print("-----".ljust(8), "-----".ljust(7), "------".ljust(22), "----".ljust(20))

                for j in ReadyList:
                    print(j[0].ljust(9), str(j[1]).ljust(6),('None' if str(j[2]) == 'None' else str(j[2]).split(".")[0]).ljust(22), j[3].ljust(20))



    def delete_local_job(self, JobName):
        '''
        Delete local jobs
        '''

        LocalJobs = [job.split(".")[0] for job in listdir(self.JCDirectoryLoc)]

        if JobName not in LocalJobs:
            print("-Job '{0}' does not exist in your local machine".format(JobName))
            return
        else:
            AskingResult = self.asking_user_decision("Sure deleting job '{0}'?[y/n] : ".format(JobName))
            if AskingResult:
                remove(self.JCDirectoryLoc + JobName + ".config.xml")
                print("-Job '{0}' removed successfully from your local machine".format(JobName))

                update_built_file(self.JCBuiltFileLoc, 'delete', JobName)

    def delete_server_job(self, JobName):
        '''
        Delete Specified server job
        '''
        if self.ServerHandler is None:
            print("Your are not connected to server")
            print("First connect by 'connect' command")
            return
        else:
            ListServerJobs = [server_job["name"] for server_job in self.ServerHandler.get_jobs()]
            if JobName not in ListServerJobs:
                print("-Job '{0}' does not exist in Jenkins Server".format(JobName))
                return
            else:
                AskingResult = self.asking_user_decision("Sure deleting job '{0}'?[y/n]: ".format(JobName))
                if AskingResult:
                    self.ServerHandler.delete_job(JobName)
                    print("-Job '{0}' removed successfully from server".format(JobName))
                    update_built_file(self.JCBuiltFileLoc, 'update-deploy-status-ND', JobName)

    def disable_server_job(self, JobName):
        '''
        Disable specified server job
        '''

        JobState = self.ServerHandler.get_job_info(JobName)['disabled']
        if JobState:
            print("-Job '{0}' is already disabled".format(JobName))
            return
        self.ServerHandler.disable_job(JobName)
        print("-Job '{0}' successfully disabled".format(JobName))

    def enable_server_job(self, JobName):
        '''
        Enable specified server job
        '''
        JobState = self.ServerHandler.get_job_info(JobName)['disabled']
        if not JobState:
            print("-Job '{0}' is already enabled".format(JobName))
            return
        self.ServerHandler.enable_job(JobName)
        print("-Job '{0}' successfully enabled".format(JobName))

    def build_server_job(self, JobName):
        '''
        Build specified server job
        '''
        JobState = self.ServerHandler.get_job_info(JobName)['disabled']
        if JobState:
            print("-Job '{0}' is disabled and cannot built".format(JobName))
        else:
            self.ServerHandler.build_job(JobName)
            print("-Job '{0}' successfully built".format(JobName))

    def stat_server_job(self, JobName, Detail=False):
        '''
        Display status of the selected job
        :param Detail: If * specified , more detail about job is displayed
        '''

        # Because building a job can take long time we do not display the status
        # until the building process gets done
        BuildQueue = self.ServerHandler.get_queue_info()

        for job in BuildQueue:
            if JobName == job["task"]["name"]:
                print("-Job '{0}' is under building process".format(JobName))
                return

        JobInfo = self.ServerHandler.get_job_info(JobName)
        JobName_Sta = "Name: {0}".format(JobName)
        JobStatus_Sta = "Status: {0}".format('Disabled' if JobInfo['disabled'] else 'Enabled')
        JobTotalBuilds_Sta = "TotalBuilds: {0}".format(
            "0" if JobInfo['lastBuild'] is None else JobInfo['lastBuild']['number'])
        JobLastBuild_Sta = "LastBuild: {0}".format("None" if JobInfo['lastBuild'] is None else datetime.fromtimestamp(
            self.ServerHandler.get_build_info(JobName, JobInfo['lastBuild']['number'])['timestamp'] / 1000))
        JobFirstBuild_Sta = "FirstBuild: {0}".format("None" if JobInfo['lastBuild'] is None else datetime.fromtimestamp(
            self.ServerHandler.get_build_info(JobName, JobInfo['lastBuild']['number'])['timestamp'] / 1000))

        JobLastCompletedBuild_Sta = "LastCompletedBuild: {0}".format(
            "None" if JobInfo['lastCompletedBuild'] is None else str(datetime.fromtimestamp(
                self.ServerHandler.get_build_info(JobName, JobInfo['lastCompletedBuild']['number'])[
                    'timestamp'] / 1000)).split(".")[0])
        JobLastFailedBuild_Sta = "LastFailedBuild: {0}".format(
            "None" if JobInfo['lastFailedBuild'] is None else str(datetime.fromtimestamp(
                self.ServerHandler.get_build_info(JobName, JobInfo['lastFailedBuild']['number'])['timestamp'] / 1000)).split(".")[0])
        JobLastStableBuild_Sta = "LastStableBuild: {0}".format(
            "None" if JobInfo['lastStableBuild'] is None else str(datetime.fromtimestamp(
                self.ServerHandler.get_build_info(JobName, JobInfo['lastStableBuild']['number'])['timestamp'] / 1000)).split(".")[0])
        JobLastSuccessfulBuild_Sta = "LastSuccessfulBuild: {0}".format(
            "None" if JobInfo['lastSuccessfulBuild'] is None else str(datetime.fromtimestamp(
                self.ServerHandler.get_build_info(JobName, JobInfo['lastSuccessfulBuild']['number'])[
                    'timestamp'] / 1000)).split(".")[0])
        JobLastUnstableBuild_Sta = "LastUnstableBuild: {0}".format(
            "None" if JobInfo['lastUnstableBuild'] is None else str(datetime.fromtimestamp(
                self.ServerHandler.get_build_info(JobName, JobInfo['lastUnstableBuild']['number'])['timestamp'] / 1000)).split(".")[0])
        JobLastUnsuccessfulBuild_Sta = "LastUnsuccessfulBuild: {0}".format(
            "None" if JobInfo['lastUnsuccessfulBuild'] is None else str(datetime.fromtimestamp(
                self.ServerHandler.get_build_info(JobName, JobInfo['lastUnsuccessfulBuild']['number'])[
                    'timestamp'] / 1000)).split(".")[0])

        print(JobName_Sta)
        print(JobStatus_Sta)
        print(JobTotalBuilds_Sta)
        print(JobLastBuild_Sta)
        print(JobLastStableBuild_Sta)
        print(JobLastCompletedBuild_Sta)

        if Detail:
            print(JobFirstBuild_Sta)
            print(JobLastFailedBuild_Sta)
            print(JobLastSuccessfulBuild_Sta)
            print(JobLastUnstableBuild_Sta)
            print(JobLastUnsuccessfulBuild_Sta)

    def select_server_job(self, JobName):
        '''
        Select a server job and enter selected job mode
        '''
        if self.ServerHandler is None:
            print("Your are not connected to server")
            print("First connect by 'connect' command")
            return
        else:

            ListServerJobs = [server_job["name"] for server_job in self.ServerHandler.get_jobs()]
            if JobName not in ListServerJobs:
                print("-Job '{0}'  not exist in Jenkins Server".format(JobName))
                return
            else:
                SelectedJobSession = PromptSession(
                    lexer=PygmentsLexer(SqlLexer), completer=selected_job_command_completer)
                while True:

                    try:
                        UserCommand = SelectedJobSession.prompt("(" + JobName + ')## ')
                        UserCommand = (sub(' +', ' ', UserCommand)).strip()

                        if UserCommand.startswith("??"):
                            keyValue = (UserCommand.split("??")[1].strip())
                            if keyValue == '':
                                jc.display_list_of_selected_job_mode_commands(verbos=True)
                            else:
                                jc.display_list_of_selected_job_mode_commands(search_key=keyValue, verbos=True)

                        elif UserCommand.startswith("?"):
                            keyValue = (UserCommand.split("?")[1].strip())

                            if keyValue == '':
                                jc.display_list_of_selected_job_mode_commands()
                            else:
                                jc.display_list_of_selected_job_mode_commands(search_key=keyValue)


                        elif UserCommand == "quit" or UserCommand == "exit":
                            return

                        elif UserCommand == "enable":
                            self.enable_server_job(JobName)

                        elif UserCommand == "disable":
                            self.disable_server_job(JobName)

                        elif UserCommand == "build":
                            self.build_server_job(JobName)

                        elif UserCommand == 'stat *':
                            self.stat_server_job(JobName, Detail=True)

                        elif UserCommand == 'stat':
                            self.stat_server_job(JobName)

                        elif UserCommand == "":
                            pass
                        else:
                            print("%Invalid Command")

                    except KeyboardInterrupt:
                        break
                    except EOFError:
                        break
                    except:
                        print("There was a problem in program")
                        return

    def get_whoami(self):
        '''
        Display who is connected to server
        '''
        if self.ServerHandler is None:
            print("Your are not connected to server")
            print("First connect by 'connect' command")
            return
        else:
            UserInfo = self.ServerHandler.get_whoami()
            print("Full Name : ", UserInfo["fullName"])
            print("Id : ", UserInfo["id"])

    def asking_user_decision(self, displayMessage):
        '''
        This process is repeated many times in the code asking the user for yes/no answer
        so this process is turned into a function for simplicity

        :param displayMessage:  Message to be displayed for asking user his/her decision
        '''

        while True:
            UserDecision = input(displayMessage)
            if UserDecision.lower() in "y yes ye":
                return True
            elif UserDecision.lower() in "n no":
                return False

    def get_job_directory(self):
        return self.JCDirectoryLoc
Example #5
0
    def execOnJenkins(self,env,testSpecifierString,mailto,execOnOneZone=True):
        try:
              testMgr=testManager(testSpecifierString,env['virtenvPath'])
              jobModifier=modifyJOb()
              modifiedjob=""
              j=Jenkins('http://jenkins-ccp.citrix.com','bharatk','BharatK')
              tests=testMgr.getTests()
              if(tests==None):
                raise Exception("found no tests to run")
              while(not tests is None):
                  #trigger a jenkins job.
                  os.chdir(env['virtenvPath'])
                  self.logger.info("launching jenkins TestExecutor Job")
                  #createing testexecutorjobs for each zone.
                  cscfg=configGenerator.getSetupConfig(env['config_file'])
                  jobIdentifierList=[]
                  for zone in cscfg.zones:
                      for pod in zone.pods:
                         for cluster in pod.clusters:
                             modifiedjob=jobModifier.addTests(env['build_number'],tests)
                             file=open("/root/cloud-autodeploy2/newcode/"+modifiedjob,'r')
                             config=file.read()
                             file.close()
                             bash("rm -f /root/cloud-autodeploy2/newcode/%s"%modifiedjob)
                             if(not j.job_exists(modifiedjob)):
                                  j.create_job(modifiedjob,config)
                             else:
                                  j.reconfig_job(modifiedjob,config)
                             j.build_job(modifiedjob, {'BASEDIR':env['virtenvPath'], 'MGMT_SVR' : env['hostip'],'buildNumber':env['build_number'],'zoneName':zone.name,'hypervisor':cluster.hypervisor.lower(),'zoneType':zone.networktype,'configFileName':env['config_file'],'token':'bharat'})
                             jobIdentifierList.append(zone.name)
                             break
                         break
                      if (execOnOneZone):
                        break
                  self.waitForJobComplete(env['virtenvPath'],jobIdentifierList)
                  tests=testMgr.getTests()  

              j.delete_job(modifiedjob) 
              jobIdentifierList=[]
              bugLoggerData=[]
              time.sleep(30)
              for zone in cscfg.zones:
                 self.logger.info(zone.name)
                 for pod in zone.pods:
                     for cluster in pod.clusters:
                         self.logger.info("creating a jeknins job to generate results and email notfication for hypervisor %s and zone %s"%(cluster.hypervisor, zone.name))
                         modifiedjob=jobModifier.modifyReportGenerator(env['build_number']+"_"+zone.name+"_"+cluster.hypervisor, mailto)
                         jobname=modifiedjob
                         file=open("/root/cloud-autodeploy2/newcode/"+modifiedjob,'r')
                         config=file.read()
                         file.close()
                         j.create_job(modifiedjob,config)
                         j.build_job(modifiedjob, {'buildNumber':env['build_number'],'BuildNo':env['build_number'], 'MGMT_SVR' : env['hostip'], 'BASEDIR':env['virtenvPath'], 'version':env['version'], 'BranchInfo':env['version'],\
                         'GitRepoUrl':env['repo_url'],'GitCommitId':env['commit_id'], 'CIRunStartDateTime':env['startTime'],'CIRunEndDateTime':time.strftime("%c"), 'WikiLinks':'https://cwiki.apache.org/confluence/display/CLOUDSTACK/Infrastructure%2CCI%2CSimulator%2CAutomation+Changes','hypervisor':cluster.hypervisor.lower(), 'HyperVisorInfo':cluster.hypervisor.lower(), 'zoneName':zone.name, 'BuildReport':"http://jenkins-ccp.citrix.com/job/"+jobname+"/1/testReport/",'token':'bharat'})
                         jobIdentifierList.append("report_"+zone.name)
                         jobDetails={"job_name":modifiedjob,"related_data_path":env['virtenvPath']}
                         self.resourceMgr.addJobDetails(jobDetails)
                         bugLoggerData.append({'hypervisor':cluster.hypervisor.lower(), 'branch':env['version'],'logname':cluster.hypervisor.lower()+'__Log_'+env['build_number'], 'type':'BVT'})
                         self.logger.info("bug logger data in zone looop %s"%bugLoggerData)
                         break
                     break
                 if (execOnOneZone):
                    #env['hypervisor':cluster.hypervisor.lower()]
                    break  
              self.logger.info("job identifier list", jobIdentifierList)       
              self.waitForJobComplete(env['virtenvPath'],jobIdentifierList)
              #self.logger.info("deleting the reporter job on jenkins job_name=%s",jobname)
              #j.delete_job(jobname)
              self.logger.info("cleaning up the workspace")
              bash("rm -f /root/cloud-autodeploy2/newcode/%s"%modifiedjob)
              self.logger.info("running bug logger")
              #self.runBugLogger(bugLoggerData)
              #os.system("rm -rf %s"%(self.jenkinsWorkspace+"/"+jobname))
        except Exception, e:
               self.logger.error(e) 
Example #6
0
class jenkinscls(object):
    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)

    def getjobnames(self, strval=''):
        rs = {r'...': r'/'}
        s = utils.multiple_replace(str(strval), rs).split('/')
        return s[0], "/".join(s[1:])

    def getlenstr(self, strval, n):
        return str(strval)[0:n]

    def getstatus(self, strval):
        if str(strval) == 'FAILURE':
            return 'error'
        elif str(strval) == 'ABORTED':
            return 'aborted'
        elif str(strval) == 'SUCCESS':
            return 'success'
        else:
            return 'started'

    def edit_userjob_config(self, jn, obj):
        n, r = self.getjobnames(jn)
        try:
            desobj = callback()
            desobj.des = obj['description']
            desobj.callback_url = ""
            desobj.build_id = ''
            desobj.duration = ''
            desobj.namespace = n
            desobj.image_name = obj['image_name']
            desobj.repo_name = r
            desobj.status = ''
            desobj.tag = obj['build_config']['tag_configs']['docker_repo_tag']
            desobj.time = ''

            ss = xmltodict.parse(self.getbasejob_config())
            jsonpickle.set_preferred_backend('json')
            ss['project']['description'] = jsonpickle.encode(desobj)
            ss['project']['properties']['com.tikal.hudson.plugins.notification.HudsonNotificationProperty']['endpoints'] \
                ['com.tikal.hudson.plugins.notification.Endpoint']['url'] = config.JOBHOOKURL

            ss['project']['scm']['userRemoteConfigs']['hudson.plugins.git.UserRemoteConfig'] \
                ['url'] = obj['build_config']['code_repo_clone_url']
            ss['project']['scm']['branches']['hudson.plugins.git.BranchSpec'] \
                ['name'] = '*/' + obj['build_config']['tag_configs']['code_repo_type_value']
            ss['project']['builders']['hudson.tasks.Shell'][
                'command'] = config.JOBCOMMON1
            ss['project']['builders']['com.cloudbees.dockerpublish.DockerBuilder']['registry'] \
                ['url'] = config.REGISTRYURL
            b = str(obj['build_config']['tag_configs']['build_cache_enabled'])
            ss['project']['builders']['com.cloudbees.dockerpublish.DockerBuilder']['noCache'] \
                = ('true' if b == 'false' else 'false')
            ss['project']['builders']['com.cloudbees.dockerpublish.DockerBuilder']['dockerfilePath'] \
                = obj['build_config']['tag_configs']['dockerfile_location']
            ss['project']['builders']['com.cloudbees.dockerpublish.DockerBuilder']['repoTag'] \
                = obj['build_config']['tag_configs']['docker_repo_tag']
            ss['project']['builders']['com.cloudbees.dockerpublish.DockerBuilder']['repoName'] \
                = obj['image_name']

            return xmltodict.unparse(ss)
        except Exception as e:
            print e.message

    def edit_docker_load_job_config(self, obj):

        try:
            # {docker_login} && docker import {httpfilename} {imagename} && docker push {imagename}
            ss = xmltodict.parse(self.getdocker_load_config())
            desobj = callback()
            desobj.des = obj['export_file_url']
            desobj.callback_url = obj['post_callback_url']
            desobj.build_id = obj['build_id']
            desobj.duration = ''
            desobj.namespace = ""
            desobj.repo_name = ""
            desobj.image_name = obj['image_name']
            desobj.status = ''
            desobj.tag = obj['tag']
            desobj.time = ''
            jsonpickle.set_preferred_backend('json')
            ss['project']['description'] = jsonpickle.encode(desobj)
            ss['project']['properties']['com.tikal.hudson.plugins.notification.HudsonNotificationProperty']['endpoints'] \
                ['com.tikal.hudson.plugins.notification.Endpoint']['url'] = config.JOBHOOKURL
            tempstr = str(
                ss['project']['builders']['hudson.tasks.Shell']['command'])
            s = {
                r'{docker_login}':
                config.JOBCOMMON1,
                r'{httpfilename}':
                obj['export_file_url'],
                r'{imagename}':
                config.REGISTRYNAME + '/' + obj['image_name'] + ':' +
                obj['tag']
            }
            ss['project']['builders']['hudson.tasks.Shell'][
                'command'] = utils.multiple_replace(tempstr, s)

            return xmltodict.unparse(ss)
        except Exception as e:
            print e.message

    def edit_docker_sync_job_config(self, obj):

        try:
            # {docker_login} && docker pull {oldimage} && docker tag {oldimage} {newimage} && docker push {newimage}
            ss = xmltodict.parse(self.getdocker_sync_config())
            jsonUtil = JsonUtil()
            c = jsonUtil.parseJsonString(config.CLOUD_CONFIG)
            cid = obj['sync_cloud_id']
            desobj = callback()
            desobj.des = ""
            desobj.callback_url = obj['post_callback_url']
            desobj.build_id = ''
            desobj.duration = ''
            desobj.namespace = ""
            desobj.repo_name = obj['sync_cloud_id']  # 把cloudid 临时存在 这
            desobj.image_name = obj['image_name']
            desobj.status = ''
            desobj.tag = obj['tag']
            desobj.time = ''
            jsonpickle.set_preferred_backend('json')
            ss['project']['description'] = jsonpickle.encode(desobj)
            ss['project']['properties']['com.tikal.hudson.plugins.notification.HudsonNotificationProperty']['endpoints'] \
                ['com.tikal.hudson.plugins.notification.Endpoint']['url'] = config.JOBHOOKURL+'?cloudid='+obj['sync_cloud_id']
            tempstr = str(
                ss['project']['builders']['hudson.tasks.Shell']['command'])
            s = {
                r'{docker_login}':
                c[cid]['login_common'],
                r'{oldimage}':
                config.REGISTRYNAME + '/' + obj['image_name'] + ':' +
                obj['tag'],
                r'{newimage}':
                c[cid]['registry_name'] + '/' + obj['image_name'] + ':' +
                obj['tag']
            }
            ss['project']['builders']['hudson.tasks.Shell'][
                'command'] = utils.multiple_replace(tempstr, s)

            return xmltodict.unparse(ss)
        except Exception as e:
            print e.message

    def updateconfig_buildid(self, jn, imagename, build_id, callback_url):
        try:
            ss = xmltodict.parse(self.j.get_job_config(jn))
            jsonpickle.set_preferred_backend('json')
            desobj = jsonpickle.decode(ss['project']['description'])
            if str(desobj.build_id) == str(build_id):
                return True
            desobj.build_id = build_id
            desobj.callback_url = callback_url
            desobj.image_name = imagename
            ss['project']['description'] = jsonpickle.encode(desobj)

            self.j.reconfig_job(jn, xmltodict.unparse(ss))

            return True
        except Exception as e:
            print e.message
            return False

    @gen.coroutine
    def posthook(self, obj):
        # s = {r'/': r'...'}
        jn = obj['name']
        bid = str(obj['build']['number'])
        # n, r = self.getjobnames(jn)
        re = hook()
        try:
            info = self.j.get_build_info(jn, int(bid))
            if self.j.job_exists(jn):
                ss = xmltodict.parse(self.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.namespace = desobj.namespace
                    re.repo_name = desobj.repo_name
                    re.build_id = str(obj['build']['number'])
                    re.status = self.getstatus(obj['build']['status'])
                    re.duration = info['duration']
                    re.tag = desobj.tag
                    re.time = datetime.now()
                    re.callurl = desobj.callback_url
        except Exception as e:
            print e.message
            re = None
        raise gen.Return(re)

    @gen.coroutine
    def post_docker_load_hook(self, obj):

        jn = obj['name']
        bid = str(obj['build']['number'])

        re = postimage()
        try:
            # info = self.j.get_build_info(jn, int(bid))
            if self.j.job_exists(jn):
                ss = xmltodict.parse(self.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.tag = desobj.tag
                    re.export_file_url = desobj.des
                    re.time = datetime.now()
                    re.build_id = desobj.build_id
                    re.post_callback_url = desobj.callback_url
                    if re.status != 'error' and config.JENKINS_IMAGEOPTJOB_DELETE == 'true':
                        self.j.delete_job(jn)

        except Exception as e:
            print e.message
            re = None
        raise gen.Return(re)

    @gen.coroutine
    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)

    @gen.coroutine
    def createjob(self, jobname, obj):
        s = {r'/': r'...'}
        jn = utils.multiple_replace(jobname, s)
        n, r = self.getjobnames(jn)
        try:
            if self.j.job_exists(jn):
                re = createrespo(n, r, '工程已存在', 'error', datetime.now())
            self.j.create_job(jn, self.edit_userjob_config(jn, obj))
            re = createrespo(n, r, '', 'success', datetime.now())
        except Exception as e:
            print e.message
            re = createrespo(n, r, '', 'error', datetime.now())
        raise gen.Return(re)

    @gen.coroutine
    def create_docker_load_job(self, obj):
        # s = {r'/': r'...'}
        # jn = utils.multiple_replace(jobname, s)
        s = utils.randomstr(8)
        jn = '__docker_load_job_' + s
        re = postimage()
        re.created_at = datetime.now()
        re.image_name = obj['image_name']
        re.build_id = str(obj['build_id'])
        re.post_callback_url = obj['post_callback_url']
        re.tag = obj['tag']
        re.status = 'started'
        try:
            if self.j.job_exists(jn):
                jn = jn + utils.randomstr(4)
                x = self.edit_docker_load_job_config(obj)
                self.j.create_job(jn, x)
                yield gen.sleep(0.5)
                self.j.build_job(jn)
            x = self.edit_docker_load_job_config(obj)
            self.j.create_job(jn, x)
            yield gen.sleep(0.5)
            self.j.build_job(jn)

        except Exception as e:
            print e.message
            re.status = 'error'
        raise gen.Return(re)

    @gen.coroutine
    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)

    @gen.coroutine
    def editjob(self, jobname, obj):
        s = {r'/': r'...'}
        jn = utils.multiple_replace(jobname, s)
        n, r = self.getjobnames(jn)
        try:
            if self.j.job_exists(jn):
                self.j.reconfig_job(jn, self.edit_userjob_config(jn, obj))
                re = createrespo(n, r, '', 'success', datetime.now())
            else:
                re = createrespo(n, r, 'repo is not find', 'error',
                                 datetime.now())
        except Exception as e:
            print e.message
            re = createrespo(n, r, '', 'error', datetime.now())
        raise gen.Return(re)

    @gen.coroutine
    def getjobinfo(self, jobname):
        s = {r'/': r'...'}
        jn = utils.multiple_replace(jobname, s)
        n, r = self.getjobnames(jn)
        re = jobinfo()
        try:
            if self.j.job_exists(jn):
                re.namespace = n
                re.repo_name = r
                re.info = self.j.get_job_info(jn)
        except Exception as e:
            print e.message
            re.namespace = n
            re.repo_name = r
            re.info = ""
        raise gen.Return(re)

    @gen.coroutine
    def deljob(self, jobname):
        s = {r'/': r'...'}
        jn = utils.multiple_replace(jobname, s)
        n, r = self.getjobnames(jn)
        try:
            if self.j.job_exists(jn):
                self.j.delete_job(jn)
            re = delrespo(n, r, 'success')

        except Exception as e:
            print e.message
            re = delrespo(n, r, 'error')
        raise gen.Return(re)

    @gen.coroutine
    def stopbuild(self, jobname, build_id):
        s = {r'/': r'...'}
        jn = utils.multiple_replace(jobname, s)
        n, r = self.getjobnames(jn)
        try:
            if self.j.job_exists(jn) and self.j.get_build_info(
                    jn, int(build_id)):
                self.j.stop_build(jn, int(build_id))
                re = delbuild(n, r, build_id, 'aborted')
            else:
                re = delbuild(n, r, build_id, 'error')

        except Exception as e:
            print e.message
            re = delbuild(n, r, build_id, 'error')
        raise gen.Return(re)

    @gen.coroutine
    def postbuild(self, jobname, imagename, tag, callback_url):
        s = {r'/': r'...'}
        jn = utils.multiple_replace(jobname, s)
        n, r = self.getjobnames(jn)
        try:

            if self.j.job_exists(jn):

                j = self.j.get_job_info(jn)

                build_id = j['nextBuildNumber']

                if self.j.get_queue_info() != []:
                    re = postbuild(n, r, imagename, build_id, tag,
                                   datetime.now(), 'queue')
                elif j['queueItem'] != None:
                    re = postbuild(n, r, imagename, build_id, tag,
                                   datetime.now(), 'queue')
                else:
                    self.updateconfig_buildid(jn, imagename, build_id,
                                              callback_url)
                    self.j.build_job(jn)
                    re = postbuild(n, r, imagename, build_id, tag,
                                   datetime.now(), 'started')
            else:
                re = postbuild(n, r, '', '', datetime.now(), 'error')

        except Exception as e:
            print e.message
            re = postbuild(n, r, '', tag, datetime.now(), 'error')
        raise gen.Return(re)

    @gen.coroutine
    def getbuild(self, jobname, build_id):
        s = {r'/': r'...'}
        jn = utils.multiple_replace(jobname, s)
        n, r = self.getjobnames(jn)
        try:
            b = self.j.get_build_info(jn, int(build_id))
            building = b['building']
            duration = b['duration']
            dt = self.getlenstr(b['timestamp'], 10)
            started_at = utils.timestamp_datetime(int(dt))
            status = self.getstatus(b['result'])
            stdout = self.j.get_build_console_output(jn, int(build_id))
            bd = build_detail(n, r, build_id, building, started_at, duration,
                              status, stdout)
        except Exception as e:
            print e.message
            bd = build_detail(n, r, build_id, '', '', '', 'error', '')
        raise gen.Return(bd)

    def getdocker_sync_config(self):
        s = '''<?xml version='1.0' encoding='UTF-8'?>
<project>
  <actions/>
  <description></description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <com.tikal.hudson.plugins.notification.HudsonNotificationProperty plugin="[email protected]">
      <endpoints>
        <com.tikal.hudson.plugins.notification.Endpoint>
          <protocol>HTTP</protocol>
          <format>JSON</format>
          <url>http://10.1.39.60:8080/v1/hook</url>
          <event>completed</event>
          <timeout>30000</timeout>
          <loglines>0</loglines>
        </com.tikal.hudson.plugins.notification.Endpoint>
      </endpoints>
    </com.tikal.hudson.plugins.notification.HudsonNotificationProperty>
    <com.synopsys.arc.jenkins.plugins.ownership.jobs.JobOwnerJobProperty plugin="[email protected]"/>
    <hudson.plugins.heavy__job.HeavyJobProperty plugin="[email protected]">
      <weight>1</weight>
    </hudson.plugins.heavy__job.HeavyJobProperty>
    <jenkins.model.BuildDiscarderProperty>
      <strategy class="hudson.tasks.LogRotator">
        <daysToKeep>30</daysToKeep>
        <numToKeep>50</numToKeep>
        <artifactDaysToKeep>-1</artifactDaysToKeep>
        <artifactNumToKeep>-1</artifactNumToKeep>
      </strategy>
    </jenkins.model.BuildDiscarderProperty>
    <job-metadata plugin="[email protected]">
      <values class="linked-list">
        <metadata-tree>
          <name>job-info</name>
          <parent class="job-metadata" reference="../../.."/>
          <generated>true</generated>
          <exposedToEnvironment>false</exposedToEnvironment>
          <children class="linked-list">
            <metadata-tree>
              <name>last-saved</name>
              <description></description>
              <parent class="metadata-tree" reference="../../.."/>
              <generated>true</generated>
              <exposedToEnvironment>false</exposedToEnvironment>
              <children class="linked-list">
                <metadata-date>
                  <name>time</name>
                  <description></description>
                  <parent class="metadata-tree" reference="../../.."/>
                  <generated>true</generated>
                  <exposedToEnvironment>false</exposedToEnvironment>
                  <value>
                    <time>1458098001639</time>
                    <timezone>Asia/Shanghai</timezone>
                  </value>
                  <checked>false</checked>
                </metadata-date>
                <metadata-tree>
                  <name>user</name>
                  <parent class="metadata-tree" reference="../../.."/>
                  <generated>true</generated>
                  <exposedToEnvironment>false</exposedToEnvironment>
                  <children class="linked-list">
                    <metadata-string>
                      <name>display-name</name>
                      <description></description>
                      <parent class="metadata-tree" reference="../../.."/>
                      <generated>true</generated>
                      <exposedToEnvironment>false</exposedToEnvironment>
                      <value>admin</value>
                    </metadata-string>
                    <metadata-string>
                      <name>full-name</name>
                      <description></description>
                      <parent class="metadata-tree" reference="../../.."/>
                      <generated>true</generated>
                      <exposedToEnvironment>false</exposedToEnvironment>
                      <value>admin</value>
                    </metadata-string>
                  </children>
                </metadata-tree>
              </children>
            </metadata-tree>
          </children>
        </metadata-tree>
      </values>
    </job-metadata>
  </properties>
  <scm class="hudson.scm.NullSCM"/>
  <scmCheckoutRetryCount>3</scmCheckoutRetryCount>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <builders>
    <hudson.tasks.Shell>
      <command>{docker_login} &amp;&amp; docker pull {oldimage} &amp;&amp; docker tag --force=true {oldimage} {newimage} &amp;&amp; docker push {newimage}
</command>
    </hudson.tasks.Shell>
  </builders>
  <publishers/>
  <buildWrappers>
    <hudson.plugins.ansicolor.AnsiColorBuildWrapper plugin="[email protected]">
      <colorMapName>xterm</colorMapName>
    </hudson.plugins.ansicolor.AnsiColorBuildWrapper>
  </buildWrappers>
</project>'''
        return s

    def getdocker_load_config(self):
        s = '''<?xml version='1.0' encoding='UTF-8'?>
<project>
  <actions/>
  <description></description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <com.tikal.hudson.plugins.notification.HudsonNotificationProperty plugin="[email protected]">
      <endpoints>
        <com.tikal.hudson.plugins.notification.Endpoint>
          <protocol>HTTP</protocol>
          <format>JSON</format>
          <url>http://10.1.39.60:8080/v1/hook</url>
          <event>completed</event>
          <timeout>30000</timeout>
          <loglines>0</loglines>
        </com.tikal.hudson.plugins.notification.Endpoint>
      </endpoints>
    </com.tikal.hudson.plugins.notification.HudsonNotificationProperty>
    <com.synopsys.arc.jenkins.plugins.ownership.jobs.JobOwnerJobProperty plugin="[email protected]"/>
    <hudson.plugins.heavy__job.HeavyJobProperty plugin="[email protected]">
      <weight>1</weight>
    </hudson.plugins.heavy__job.HeavyJobProperty>
    <jenkins.model.BuildDiscarderProperty>
      <strategy class="hudson.tasks.LogRotator">
        <daysToKeep>30</daysToKeep>
        <numToKeep>50</numToKeep>
        <artifactDaysToKeep>-1</artifactDaysToKeep>
        <artifactNumToKeep>-1</artifactNumToKeep>
      </strategy>
    </jenkins.model.BuildDiscarderProperty>
    <job-metadata plugin="[email protected]">
      <values class="linked-list">
        <metadata-tree>
          <name>job-info</name>
          <parent class="job-metadata" reference="../../.."/>
          <generated>true</generated>
          <exposedToEnvironment>false</exposedToEnvironment>
          <children class="linked-list">
            <metadata-tree>
              <name>last-saved</name>
              <description></description>
              <parent class="metadata-tree" reference="../../.."/>
              <generated>true</generated>
              <exposedToEnvironment>false</exposedToEnvironment>
              <children class="linked-list">
                <metadata-date>
                  <name>time</name>
                  <description></description>
                  <parent class="metadata-tree" reference="../../.."/>
                  <generated>true</generated>
                  <exposedToEnvironment>false</exposedToEnvironment>
                  <value>
                    <time>1458097635464</time>
                    <timezone>Asia/Shanghai</timezone>
                  </value>
                  <checked>false</checked>
                </metadata-date>
                <metadata-tree>
                  <name>user</name>
                  <parent class="metadata-tree" reference="../../.."/>
                  <generated>true</generated>
                  <exposedToEnvironment>false</exposedToEnvironment>
                  <children class="linked-list">
                    <metadata-string>
                      <name>display-name</name>
                      <description></description>
                      <parent class="metadata-tree" reference="../../.."/>
                      <generated>true</generated>
                      <exposedToEnvironment>false</exposedToEnvironment>
                      <value>admin</value>
                    </metadata-string>
                    <metadata-string>
                      <name>full-name</name>
                      <description></description>
                      <parent class="metadata-tree" reference="../../.."/>
                      <generated>true</generated>
                      <exposedToEnvironment>false</exposedToEnvironment>
                      <value>admin</value>
                    </metadata-string>
                  </children>
                </metadata-tree>
              </children>
            </metadata-tree>
          </children>
        </metadata-tree>
      </values>
    </job-metadata>
  </properties>
  <scm class="hudson.scm.NullSCM"/>
  <scmCheckoutRetryCount>3</scmCheckoutRetryCount>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <builders>
    <hudson.tasks.Shell>
      <command>{docker_login} &amp;&amp; docker import {httpfilename} {imagename} &amp;&amp; docker push {imagename}
</command>
    </hudson.tasks.Shell>
  </builders>
  <publishers/>
  <buildWrappers>
    <hudson.plugins.ansicolor.AnsiColorBuildWrapper plugin="[email protected]">
      <colorMapName>xterm</colorMapName>
    </hudson.plugins.ansicolor.AnsiColorBuildWrapper>
  </buildWrappers>
</project>'''
        return s

    def getbasejob_config(self):
        s = '''<?xml version='1.0' encoding='UTF-8'?>
<project>
  <actions/>
  <description></description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <com.tikal.hudson.plugins.notification.HudsonNotificationProperty plugin="[email protected]">
      <endpoints>
        <com.tikal.hudson.plugins.notification.Endpoint>
          <protocol>HTTP</protocol>
          <format>JSON</format>
          <url>http://10.1.39.60:8080/v1/hook</url>
          <event>completed</event>
          <timeout>30000</timeout>
          <loglines>0</loglines>
        </com.tikal.hudson.plugins.notification.Endpoint>
      </endpoints>
    </com.tikal.hudson.plugins.notification.HudsonNotificationProperty>
    <com.synopsys.arc.jenkins.plugins.ownership.jobs.JobOwnerJobProperty plugin="[email protected]"/>
    <hudson.plugins.heavy__job.HeavyJobProperty plugin="[email protected]">
      <weight>1</weight>
    </hudson.plugins.heavy__job.HeavyJobProperty>
    <jenkins.model.BuildDiscarderProperty>
      <strategy class="hudson.tasks.LogRotator">
        <daysToKeep>30</daysToKeep>
        <numToKeep>50</numToKeep>
        <artifactDaysToKeep>-1</artifactDaysToKeep>
        <artifactNumToKeep>-1</artifactNumToKeep>
      </strategy>
    </jenkins.model.BuildDiscarderProperty>
    <job-metadata plugin="[email protected]">
      <values class="linked-list">
        <metadata-tree>
          <name>job-info</name>
          <parent class="job-metadata" reference="../../.."/>
          <generated>true</generated>
          <exposedToEnvironment>false</exposedToEnvironment>
          <children class="linked-list">
            <metadata-tree>
              <name>last-saved</name>
              <description></description>
              <parent class="metadata-tree" reference="../../.."/>
              <generated>true</generated>
              <exposedToEnvironment>false</exposedToEnvironment>
              <children class="linked-list">
                <metadata-date>
                  <name>time</name>
                  <description></description>
                  <parent class="metadata-tree" reference="../../.."/>
                  <generated>true</generated>
                  <exposedToEnvironment>false</exposedToEnvironment>
                  <value>
                    <time>1457958794480</time>
                    <timezone>Asia/Shanghai</timezone>
                  </value>
                  <checked>false</checked>
                </metadata-date>
                <metadata-tree>
                  <name>user</name>
                  <parent class="metadata-tree" reference="../../.."/>
                  <generated>true</generated>
                  <exposedToEnvironment>false</exposedToEnvironment>
                  <children class="linked-list">
                    <metadata-string>
                      <name>display-name</name>
                      <description></description>
                      <parent class="metadata-tree" reference="../../.."/>
                      <generated>true</generated>
                      <exposedToEnvironment>false</exposedToEnvironment>
                      <value>admin</value>
                    </metadata-string>
                    <metadata-string>
                      <name>full-name</name>
                      <description></description>
                      <parent class="metadata-tree" reference="../../.."/>
                      <generated>true</generated>
                      <exposedToEnvironment>false</exposedToEnvironment>
                      <value>admin</value>
                    </metadata-string>
                  </children>
                </metadata-tree>
              </children>
            </metadata-tree>
          </children>
        </metadata-tree>
      </values>
    </job-metadata>
  </properties>
  <scm class="hudson.plugins.git.GitSCM" plugin="[email protected]">
    <configVersion>2</configVersion>
    <userRemoteConfigs>
      <hudson.plugins.git.UserRemoteConfig>
        <url>https://github.com/zhwenh/dockerfile-jdk-tomcat.git</url>
      </hudson.plugins.git.UserRemoteConfig>
    </userRemoteConfigs>
    <branches>
      <hudson.plugins.git.BranchSpec>
        <name>*/master</name>
      </hudson.plugins.git.BranchSpec>
    </branches>
    <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
    <browser class="hudson.plugins.git.browser.GitLab">
      <url></url>
      <version>7.11</version>
    </browser>
    <submoduleCfg class="list"/>
    <extensions/>
  </scm>
  <scmCheckoutRetryCount>3</scmCheckoutRetryCount>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <builders>
    <hudson.tasks.Shell>
      <command>docker login -u admin -p admin123 -e [email protected] registry.test.com</command>
    </hudson.tasks.Shell>
    <com.cloudbees.dockerpublish.DockerBuilder plugin="[email protected]">
      <server plugin="[email protected]">
        <uri>unix:///var/run/docker.sock</uri>
      </server>
      <registry plugin="[email protected]">
        <url>http://registry.test.com/v2</url>
      </registry>
      <repoName>zhwenh/tomcat</repoName>
      <noCache>false</noCache>
      <forcePull>true</forcePull>
      <dockerfilePath>./Dockerfile</dockerfilePath>
      <skipBuild>false</skipBuild>
      <skipDecorate>false</skipDecorate>
      <repoTag>2.3.1</repoTag>
      <skipPush>false</skipPush>
      <createFingerprint>true</createFingerprint>
      <skipTagLatest>false</skipTagLatest>
      <buildAdditionalArgs></buildAdditionalArgs>
      <forceTag>true</forceTag>
    </com.cloudbees.dockerpublish.DockerBuilder>
  </builders>
  <publishers>
    <hudson.plugins.emailext.ExtendedEmailPublisher plugin="[email protected]">
      <recipientList>$DEFAULT_RECIPIENTS</recipientList>
      <configuredTriggers>
        <hudson.plugins.emailext.plugins.trigger.FailureTrigger>
          <email>
            <recipientList>$DEFAULT_RECIPIENTS</recipientList>
            <subject>$PROJECT_DEFAULT_SUBJECT</subject>
            <body>$PROJECT_DEFAULT_CONTENT</body>
            <recipientProviders>
              <hudson.plugins.emailext.plugins.recipients.DevelopersRecipientProvider/>
            </recipientProviders>
            <attachmentsPattern></attachmentsPattern>
            <attachBuildLog>false</attachBuildLog>
            <compressBuildLog>false</compressBuildLog>
            <replyTo>$PROJECT_DEFAULT_REPLYTO</replyTo>
            <contentType>project</contentType>
          </email>
        </hudson.plugins.emailext.plugins.trigger.FailureTrigger>
        <hudson.plugins.emailext.plugins.trigger.SuccessTrigger>
          <email>
            <recipientList>$DEFAULT_RECIPIENTS</recipientList>
            <subject>$PROJECT_DEFAULT_SUBJECT</subject>
            <body>$PROJECT_DEFAULT_CONTENT</body>
            <recipientProviders>
              <hudson.plugins.emailext.plugins.recipients.DevelopersRecipientProvider/>
            </recipientProviders>
            <attachmentsPattern></attachmentsPattern>
            <attachBuildLog>false</attachBuildLog>
            <compressBuildLog>false</compressBuildLog>
            <replyTo>$PROJECT_DEFAULT_REPLYTO</replyTo>
            <contentType>project</contentType>
          </email>
        </hudson.plugins.emailext.plugins.trigger.SuccessTrigger>
      </configuredTriggers>
      <contentType>default</contentType>
      <defaultSubject>$DEFAULT_SUBJECT</defaultSubject>
      <defaultContent>$DEFAULT_CONTENT</defaultContent>
      <attachmentsPattern></attachmentsPattern>
      <presendScript>$DEFAULT_PRESEND_SCRIPT</presendScript>
      <attachBuildLog>false</attachBuildLog>
      <compressBuildLog>false</compressBuildLog>
      <replyTo></replyTo>
      <saveOutput>false</saveOutput>
      <disabled>false</disabled>
    </hudson.plugins.emailext.ExtendedEmailPublisher>
  </publishers>
  <buildWrappers>
    <hudson.plugins.ansicolor.AnsiColorBuildWrapper plugin="[email protected]">
      <colorMapName>xterm</colorMapName>
    </hudson.plugins.ansicolor.AnsiColorBuildWrapper>
  </buildWrappers>
</project>'''

        return s

    @gen.coroutine
    def createbasejob(self):

        s = self.getbasejob_config()

        try:
            self.j.create_job(config.JENKINS_BASEJOB, s)
        except Exception as e:
            print e.message
            raise gen.Return(False)
        raise gen.Return(True)
Example #7
0
 def addJob():
     startingUp = True
     JS_URL = app.config["JENKINS_URL"]
     while startingUp:
         try:
             app.logger.debug('Connecting to Jenkins %s as %s', JS_URL,
                              app.config["UID"])
             js = JenkinsServer(JS_URL,
                                username=app.config["UID"],
                                password=app.config["PWD"])
             if (js.wait_for_normal_op(30)):
                 app.logger.debug('Connected')
                 app.logger.info('Hello from Jenkins %s', js.get_version())
                 startingUp = False
         except:
             app.logger.debug('%s caught during Jenkins connect',
                              sys.exc_info()[0])
             app.logger.debug('Waiting for startup, sleeping')
             time.sleep(5)
     try:
         app.logger.debug('Creating folder %s', app.config["FOLDER_NAME"])
         js.create_job(app.config["FOLDER_NAME"], jenkins.EMPTY_FOLDER_XML)
     except:
         app.logger.debug('%s caught during folder create.',
                          sys.exc_info()[0])
         pass
     if app.config["GIT_UID"] != "":
         cj = credentialXML()
         try:
             app.logger.debug('Credential check.')
             try:
                 app.logger.debug('Prophylactic delete of credential.')
                 js.delete_credential(app.config["GIT_UID"],
                                      app.config["FOLDER_NAME"])
             except:
                 pass
             app.logger.debug('Creating credential.')
             js.create_credential(app.config["FOLDER_NAME"], cj)
         except:
             app.logger.debug('%s caught during config', sys.exc_info()[0])
     else:
         app.logger.debug("Anonymous GIT access")
     app.logger.debug('Generating Job XML.')
     nj = jobXML()
     app.logger.debug('Creating job.')
     try:
         app.logger.debug('Does job exist?.')
         if js.job_exists(app.config["FOLDER_NAME"] + '/' +
                          app.config["PIPELINE_NAME"]):
             exists = True
             app.logger.debug('Yep!')
             #app.logger.debug('Reconfiguring job %s using [%s]', app.config["PIPELINE_NAME"], nj)
             js.reconfig_job(
                 app.config["FOLDER_NAME"] + '/' +
                 app.config["PIPELINE_NAME"], nj)
             exists = True
         else:
             app.logger.debug('Nope!')
             #app.logger.debug('Trying to create job %s using [%s].', app.config["PIPELINE_NAME"], nj)
             js.create_job(
                 app.config["FOLDER_NAME"] + '/' +
                 app.config["PIPELINE_NAME"], nj)
             app.logger.debug(
                 'Attempting initial build to allow Jenkinsfile based configuration.'
             )
             rid = js.build_job(app.config["FOLDER_NAME"] + '/' +
                                app.config["PIPELINE_NAME"])
             app.logger.debug('Started %d', rid)
         app.logger.debug('Initial build to set parameters (jobid=%d)',
                          JenkinsBuild('', ''))
     except:
         app.logger.debug('%s caught during job config', sys.exc_info()[0])