def __init__(self, jenkins_settings):
        # set some class vars
        self.jenkins_settings = jenkins_settings
        self.jenkins_wrapper = JenkinsWrapper(self.jenkins_settings)

        self.openedconsole = False
        self.quit_confirm = False
        self.pager_term = None
        self.search_bar = None
        self.searchmode = False
        self.current_jobdict = None
        self.search_result = []
        self.ragequit = False
        self.main_loop = None
        self.old_search_term = ""
        self.current_search_term = ""

        # ctrl+c support
        def signal_handler(signal, frame):
            self.ragequit = True
            raise urwid.ExitMainLoop()
        signal.signal(signal.SIGINT, signal_handler)

        # create the layout
        self.view = urwid.Frame(None, header=None, footer=None)
        self.__refresh_header_footer()
        self.__refresh_jenkins(init=True)
Example #2
0
 def __init__(self, jenkins_token, jenkins_server, jenkins_user, git_token, git_org,
              git_project, slack_token, ci_job_name, watchdog_job_name):
     self._config_path = os.path.join(_WATCHDOG_DIR, '{}/.{}_ci_watchdog.json'.format(_WATCHDOG_DIR, git_project))
     # Jenkins Wrapper object for CI job
     self._jenkins = JenkinsWrapper(jenkins_token,
                                    jenkins_user=jenkins_user,
                                    jenkins_server=jenkins_server)
     # Load GitHub token and log in, retrieve pull requests
     self._git = GitWrapper(git_token, repository=git_org, project=git_project)
     # Create Slack api object
     self._slack_app = SlackCommunicator(slack_token=slack_token)
     self._ci_job_name = ci_job_name
     self._watchdog_job_name = watchdog_job_name
     # Read config file
     self._config = self._read_config_file()
     # Time at Watchdog initiation
     self._now_time = datetime.datetime.now()
     self._current_prs = {}
Example #3
0
class Watchdog:
    """Class describing nGraph-ONNX-CI Watchdog.

    Watchdog connects to GitHub and retrieves the list of current pull requests (PRs) in
    NervanaSystems/ngraph-onnx repository. Then it connects to specified Jenkins server to
    check CI jobs associated with every PR. Watchdog verifies time durations for Jenkins
    initial response, job queue and execution against time treshold constants. Every fail
    is logged and reported through Slack and MS Teams communicators.

    :param jenkins_token:       Token used for Jenkins
    :param jenkins_server:      Jenkins server address
    :param jenkins_user:        Username used to connect to Jenkins
    :param git_token:           Token used to connect to GitHub
    :param slack_token:         Token used to connect to Slack App
    :param msteams_url:         URL used to connect to MS Teams channel
    :param ci_job_name:         nGraph-ONNX CI job name used in Jenkins
    :param watchdog_job_name:   Watchdog job name used in Jenkins
    :param slack_enabled:       Enable watchdog on Slack
    :param ms_teams_enabled:    Enable watchdog on MS Teams
    :type jenkins_token:        String
    :type jenkins_server:       String
    :type jenkins_user:         String
    :type git_token:            String
    :type slack_token:          String
    :type msteams_url:          String
    :type ci_job_name:          String
    :type watchdog_job_name:    String
    :type slack_enabled:        Integer
    :type ms_teams_enabled:     Integer

    .. note::
        Watchdog and nGraph-ONNX CI job must be placed on the same Jenkins server.
    """

    def __init__(self, jenkins_token, jenkins_server, jenkins_user, git_token, git_org,
                 git_project, slack_token, msteams_url, ci_job_name, watchdog_job_name,
                 slack_enabled, ms_teams_enabled):
        self._config_path = os.path.join(_WATCHDOG_DIR, '{}/.{}_ci_watchdog.json'.format(_WATCHDOG_DIR, git_project))
        # Jenkins Wrapper object for CI job
        self._jenkins = JenkinsWrapper(jenkins_token,
                                       jenkins_user=jenkins_user,
                                       jenkins_server=jenkins_server)
        # Load GitHub token and log in, retrieve pull requests
        self._git = GitWrapper(git_token, repository=git_org, project=git_project)
        # Create Slack api object
        self._slack_app = SlackCommunicator(slack_token=slack_token)
        # Create MS Teams api object
        self._msteams_hook = MSTeamsCommunicator(msteams_url)
        self._ci_job_name = ci_job_name
        self._watchdog_job_name = watchdog_job_name
        # Read config file
        self._config = self._read_config_file()
        # Time at Watchdog initiation
        self._now_time = datetime.datetime.now()
        self._current_prs = {}
        self._slack_enabled = slack_enabled
        self._ms_teams_enabled = ms_teams_enabled

    def run(self, quiet=False):
        """Run main watchdog logic.

        Retrieve list of pull requests and pass it to the method responsible for checking them.

        :param quiet:   Flag for disabling sending report through communicator
        :type quiet:    Boolean
        """
        try:
            pull_requests = self._git.get_pull_requests()
        except GitWrapperError:
            message = 'Failed to retrieve Pull Requests!'
            log.exception(message)
            self._queue_message(message, message_severity='internal')
        # Check all pull requests
        for pr in pull_requests:
            try:
                self._check_pr(pr)
            except Exception as e:
                log.exception(str(e))
                self._queue_message(str(e), message_severity='internal')
        self._update_config()
        self._send_message(quiet=quiet)

    def _read_config_file(self):
        """Read Watchdog config file stored on the system.

        The file stores every fail already reported along with timestamp. This
        mechanism is used to prevent Watchdog from reporting same failure
        multiple times. In case there's no config under the expected path,
        appropriate data structure is created and returned.

        :return:            Returns dict of dicts with reported fails with their timestamps
        :rtype:             dict of dicts
        """
        if os.path.isfile(self._config_path):
            log.info('Reading config file in: {}'.format(self._config_path))
            file = open(self._config_path, 'r')
            data = json.load(file)
        else:
            log.info('No config file found in: {}'.format(self._config_path))
            data = {_PR_REPORTS_CONFIG_KEY: {}}
        return data

    def _check_pr(self, pr):
        """Check pull request (if there's no reason to skip).

        Retrieve list of statuses for every PR's last commit and interpret them. Filters out statuses
        unrelated to nGraph-ONNX Jenkins CI and passes relevant statuses to method that interprets them.
        If no commit statuses related to Jenkins are available after time defined by
        **_AWAITING_JENKINS_THRESHOLD** calls appropriate method to check for builds waiting in queue.

        :param pr:       GitHub Pull Requests
        :type pr:        github.PullRequest.PullRequest
        """
        log.info('===============================================')
        log.info('Checking PR#{}'.format(pr.number))
        # Get last Jenkins status
        last_status = self._get_last_status(pr)
        # Append PR checked in current run for Watchdog config
        self._current_prs[str(pr.number)] = self._get_pr_timestamps(pr, last_status)
        if self._should_ignore(pr) or self._updated_since_last_run(pr):
            log.info('Ignoring PR#{}'.format(pr.number))
            return

        # Calculate time passed since PR update (any commit, merge or comment)
        pr_time_delta = self._now_time - pr.updated_at
        if last_status:
            # Interpret found CI statuses
            log.info('Last status: {} at {}'.format(last_status.description, last_status.updated_at))
            self._interpret_status(last_status, pr)
        elif pr_time_delta > _CI_START_THRESHOLD:
            # If there's no status after assumed time - check if build is waiting in queue
            log.info('CI for PR {}: NO JENKINS STATUS YET'.format(pr.number))
            self._check_missing_status(pr)

    @staticmethod
    def _get_pr_timestamps(pr, last_status):
        """Get dict containing PR timestamp and last status timestamp.

        :param pr:          Single PR being currently checked
        :type pr:           github.PullRequest.PullRequest

        :return:            Dictionary with PR and last status update timestamps
        :rtype:             dict
        """
        pr_timestamp = time.mktime(pr.updated_at.timetuple())
        if last_status:
            status_timestamp = time.mktime(last_status.updated_at.timetuple())
        else:
            status_timestamp = None
        pr_dict = {'pr_timestamp': pr_timestamp,
                   'status_timestamp': status_timestamp}
        return pr_dict

    @staticmethod
    def _get_last_status(pr):
        """Get last commit status posted from Jenkins.

        :param pr:          Single PR being currently checked
        :type pr:           github.PullRequest.PullRequest

        :return:            Either last PR status posted from Jenkins or None
        :rtype:             github.CommitStatus.CommitStatus
        """
        # Find last commit in PR
        last_commit = pr.get_commits().reversed[0]
        # Get statuses and filter them to contain only those related to Jenkins CI
        # and check if CI in Jenkins started
        statuses = last_commit.get_statuses()
        jenk_statuses = [stat for stat in statuses if
                         'nGraph-ONNX Jenkins CI (IGK)' in stat.context]
        try:
            last_status = jenk_statuses[0]
        except IndexError:
            last_status = None
        return last_status

    @staticmethod
    def _should_ignore(pr):
        """Determine if PR should be ignored.

        :param pr:          Single PR being currently checked
        :type pr:           github.PullRequest.PullRequest

        :return:            Returns True if PR should be ignored
        :rtype:             Bool
        """
        # Ignore PR if it's external contribution
        if pr.head.repo.fork:
            log.info('PR#{} should be ignored. External contribution.'.format(pr.number))
            return True

        # Ignore PR if it has WIP label or WIP in title
        if 'WIP' in pr.title:
            log.info('PR#{} should be ignored. WIP tag in title.'.format(pr.number))
            return True

        label_names = [label.name for label in pr.labels]
        if 'WIP' in label_names:
            log.info('PR#{} should be ignored. WIP label present.'.format(pr.number))
            return True

        # Ignore PR if base ref is not master
        if 'master' not in pr.base.ref:
            log.info('PR#{} should be ignored. Base ref is not master'.format(pr.number))
            return True

        # Ignore PR if mergeable state is 'dirty' or 'behind'.
        # Practically this ignores PR in case of merge conflicts
        ignored_mergeable_states = ['behind', 'dirty', 'draft']
        if pr.mergeable_state in ignored_mergeable_states:
            log.info('PR#{} should be ignored. Mergeable state is {}. '.format(pr.number, pr.mergeable_state))
            return True

        # If no criteria for ignoring PR are met - return false
        return False

    def _updated_since_last_run(self, pr):
        # Ignore if PR was already checked and there was no update in meantime
        pr_number = str(pr.number)
        current_pr_timestamps = self._current_prs.get(pr_number)
        last_pr_timestamps = self._config[_PR_REPORTS_CONFIG_KEY].get(pr_number)
        if current_pr_timestamps == last_pr_timestamps:
            log.info('PR#{} - No update since last check'.format(pr.number))
            return True
        else:
            return False

    def _check_missing_status(self, pr):
        """Verify if missing status is expected.

        This method checks if CI build for last was scheduled and still waits in queue for
        executor.

        :param pr:                  Single PR being currently checked
        :type pr:                   github.PullRequest.PullRequest
        """
        pr_time_delta = self._now_time - pr.updated_at
        build_number = self._build_scheduled(pr)
        if self._build_in_queue(pr, build_number):
            message = ('PR# {}: build waiting in queue after {} minutes.'
                       .format(pr.number, pr_time_delta.seconds / 60))
            severity = 'warning'
        else:
            message = ('PR# {}: missing status on GitHub after {} minutes.'
                       .format(pr.number, pr_time_delta.seconds / 60))
            severity = 'error'
        self._queue_message(message, message_severity=severity, pr=pr)

    def _build_scheduled(self, pr):
        """Check if Jenkins build corresponding to PR was scheduled.

        This method takes last Jenkins build for given PR and compares hash from Jenkins console output
        and sha from PR object to determine if CI build for appropriate commit was scheduled.

        :param pr:                  Single PR being currently checked
        :type pr:                   github.PullRequest.PullRequest

        :return:            Returns build number or -1 if no build found
        :rtype:             int
        """
        pr_number = str(pr.number)
        project_name_full = self._ci_job_name + '/PR-' + pr_number

        try:
            # Retrieve console output from last Jenkins build for job corresponding to this PR
            last_build_number = self._jenkins.get_job_info(project_name_full)['lastBuild']['number']
            console_output = self._jenkins.get_build_console_output(project_name_full, last_build_number)
            # Check if CI build was scheduled - commit hash on GH must match hash in last Jenkins build console output
            # Retrieve hash from Jenkins output
            match_string = '(?:Obtained .ci/[a-zA-Z/]+Jenkinsfile from ([a-z0-9]{40}))'
            retrieved_sha = re.search(match_string, console_output).group(1)
            if retrieved_sha == pr.get_commits().reversed[0].sha:
                return last_build_number
            else:
                return -1
        except (NotFoundException, AttributeError):
            message = ('PR #{}: Jenkins build corresponding to commit {} not found!'
                       .format(pr_number, pr.get_commits().reversed[0].sha))
            self._queue_message(message, message_severity='error', pr=pr)
            return -1

    def _build_in_queue(self, pr, build_number):
        """Check if Jenkins build waits in queue.

        This method verifies if CI build is waiting in queue based on console output.

        :param pr:                  Single PR being currently checked
        :param build_number:        Jenkins build number to retrieve console output from
        :type pr:                   github.PullRequest.PullRequest
        :type build_number:         int

        :return:            Returns True if CI build is waiting in queue
        :rtype:             Bool
        """
        pr_number = str(pr.number)
        project_name_full = self._ci_job_name + '/PR-' + pr_number
        # Retrieve console output
        try:
            console_output = self._jenkins.get_build_console_output(project_name_full, build_number)
        except NotFoundException:
            return False
        # Check if build is waiting in queue (and not already running on an executor)
        if 'Waiting for next available executor on' in console_output \
                and 'Running on' not in console_output:
            log.info('CI for PR %s: WAITING IN QUEUE', pr_number)
            return True
        else:
            return False

    def _interpret_status(self, status, pr):
        """
        Verify GitHub status passed to the method.

        This method verifies last commit status for given PR, calling appropriate methods
        to further validate the status.

        :param status:              GitHub commit status
        :param pr:                  Single PR being currently checked
        :type status:               github.CommitStatus.CommitStatus
        :type pr:                   github.PullRequest.PullRequest
        """
        try:
            # Retrieve build number for Jenkins build related to this PR
            build_number = self._retrieve_build_number(status.target_url)
            # CI build finished - verify if expected output is present
            finished_statuses = ['Build finished', 'This commit cannot be built', 'This commit looks good']
            pending_statuses = ['This commit is being built', 'Testing in progress']
            if any(phrase in status.description for phrase in finished_statuses):
                self._check_finished(pr, build_number)
            # CI build in progress - verify timeouts for build queue and duration
            elif any(phrase in status.description for phrase in pending_statuses):
                self._check_in_progress(pr, build_number)
            else:
                message = 'ONNX CI job for PR# {}: unrecognized status: {}'.format(pr.number, status.description)
                self._queue_message(message, message_severity='error', pr=pr)
        except Exception:
            # Log Watchdog internal error in case any status can't be properly verified
            message = 'Failed to verify status "{}" for PR# {}'.format(status.description, pr.number)
            log.exception(message)
            self._queue_message(message, message_severity='internal', pr=pr)

    def _retrieve_build_number(self, url):
        """Retrieve Jenkins CI job build number from URL address coming from GitHub commit status.

        :param url:         URL address from GitHub commit status
        :type url:          String

        :return:            Returns build number
        :rtype:             int
        """
        # Retrieve the build number from url string
        match_obj = re.search('(?:/PR-[0-9]+/)([0-9]+)', url)
        try:
            number = int(match_obj.group(1))
            return number
        except Exception:
            log.exception('Failed to retrieve build number from url link: %s', url)
            raise

    def _queue_message(self, message, message_severity='info', pr=None):
        """Add a message to message queue in communicator object.

        The queued message is constructed based on message string passed as
        a method argument and message header. Message header is mapped to message severity
        also passed as an argument.

        :param message:                 Message content
        :param message_severity:        Message severity level
        :type message:                  String
        :type message_severity:         int
        """
        log.info(message)
        internal = False
        if 'internal' in message_severity:
            message_header = '!!! --- !!! INTERNAL WATCHDOG ERROR !!! --- !!!'
            internal = True
        elif 'error' in message_severity:
            message_header = '!!! nGraph-ONNX CI Error !!!'
        elif 'warning' in message_severity:
            message_header = 'nGraph-ONNX CI WARNING'
        else:
            message_header = 'nGraph-ONNX CI INFO'
        # If message is related to PR attatch url
        if pr:
            message = message + '\n' + pr.html_url

        send = message_header + '\n' + message
        if self._slack_enabled:
            self._slack_app.queue_message(send, internal_error=internal)
        if self._ms_teams_enabled:
            self._msteams_hook.queue_message(send)

    def _check_finished(self, pr, build_number):
        """Verify if finished build output contains expected string for either fail or success.

        :param pr:                  Single PR being currently checked
        :param build_number:        Jenkins CI job build number
        :type pr:                   github.PullRequest.PullRequest
        :type build_number:         int
        """
        pr_number = str(pr.number)
        log.info('CI for PR %s: FINISHED', pr_number)
        # Check if FINISH was valid FAIL / SUCCESS
        project_name_full = self._ci_job_name + '/PR-' + pr_number
        build_output = self._jenkins.get_build_console_output(project_name_full, build_number)
        if _CI_BUILD_FAIL_MESSAGE not in build_output \
                and _CI_BUILD_SUCCESS_MESSAGE not in build_output:
            message = ('ONNX CI job for PR #{}: finished but no tests success or fail '
                       'confirmation is present in console output!'.format(pr_number))
            self._queue_message(message, message_severity='error', pr=pr)

    def _send_message(self, quiet=False):
        """Send messages queued in Slack and MS Teams objects to designated channel.

        Queued messages are being sent as a single communication.

        :param quiet:   Flag for disabling sending report through communicator
        :type quiet:    Boolean
        """
        if any(messages for messages in self._slack_app.messages) or \
                any(messages for messages in self._msteams_hook.messages):
            try:
                watchdog_build = self._jenkins.get_job_info(self._watchdog_job_name)['lastBuild']
                watchdog_build_number = watchdog_build['number']
                watchdog_build_link = watchdog_build['url']
            except Exception:
                watchdog_build_number = 'UNKNOWN'
                watchdog_build_link = self._jenkins.jenkins_server
            send = self._watchdog_job_name + '- build ' + str(
                watchdog_build_number) + ' - ' + watchdog_build_link

            if self._slack_enabled:
                self._slack_app.send_message(send, quiet=quiet)
            if self._ms_teams_enabled:
                self._msteams_hook.send_message(send, quiet=quiet)
        else:
            log.info('Nothing to report.')

    def _check_in_progress(self, pr, build_number):
        """Check if CI build succesfully started.

        Checks if build started within designated time threshold, and job is
        currently running - it didn't cross the time threshold.

        :param pr:                  Single PR being currently checked
        :param build_number:        Jenkins CI job build number
        :type pr:                   github.PullRequest.PullRequest
        :type build_number:         int
        """
        pr_number = str(pr.number)
        log.info('CI for PR %s: TESTING IN PROGRESS', pr_number)
        project_name_full = self._ci_job_name + '/PR-' + pr_number
        build_info = self._jenkins.get_build_info(project_name_full, build_number)
        build_datetime = datetime.datetime.fromtimestamp(build_info['timestamp'] / 1000.0)
        build_delta = self._now_time - build_datetime
        log.info('Build %s: IN PROGRESS, started: %s minutes ago', str(build_number),
                 str(build_delta))
        # If build still waiting in queue
        if build_delta > _CI_START_THRESHOLD and self._build_in_queue(pr, build_number):
            message = ('ONNX CI job build #{}, for PR #{} waiting in queue after {} '
                       'minutes'.format(build_number, pr_number, str(build_delta.seconds / 60)))
            self._queue_message(message, message_severity='warning', pr=pr)
        elif build_delta > _BUILD_DURATION_THRESHOLD:
            # CI job take too long, possibly froze - communicate failure
            message = ('ONNX CI job build #{}, for PR #{} started,'
                       'but did not finish in designated time of {} '
                       'minutes!'.format(build_number, pr_number,
                                         str(_BUILD_DURATION_THRESHOLD.seconds / 60)))
            self._queue_message(message, message_severity='error', pr=pr)

    def _update_config(self):
        """Update Watchdog config file with PRs checked in current Watchdog run, remove old entries.

        :param current_prs:        List of PR numbers checked during current Watchdog run
        :type current_prs:         list of ints
        """
        # Cleanup config of old reports
        log.info('Writing to config file at: {}'.format(self._config_path))
        new_config = {_PR_REPORTS_CONFIG_KEY: self._current_prs}
        file = open(self._config_path, 'w+')
        json.dump(new_config, file)
class JenkinsJobBrowser():

    OPTION_LABEL_JOB_INFO = "Job Info"
    OPTION_LABEL_BUILD = "Build"
    OPTION_LABEL_LAST_BUILD_LOG = "Last Build Log"

    COLOR_MAPPING = {
        'blue': 'SUCCESS',
        'green': 'SUCCESS',
        'red': 'FAILED',
        'yellow': 'UNSTABLE',
        'aborted': 'ABORTED',
        'disabled': 'DISABLED',
        'grey': 'NOTBUILT',
        'notbuilt': 'NOTBUILT',
        'building': 'BUILDIONG',
    }

    palette = [
        ('body', urwid.LIGHT_GRAY, urwid.BLACK),
        ('table_heading', urwid.LIGHT_GRAY + ",bold", urwid.BLACK),
        ('focus', urwid.LIGHT_GRAY + ",standout", urwid.BLACK),
        ('selected', urwid.DARK_MAGENTA, urwid.DARK_CYAN),

        #         ('title', urwid.WHITE, urwid.DARK_BLUE),
        #         ('head_foot', urwid.WHITE, urwid.DARK_BLUE, ),
        ('title', urwid.LIGHT_GRAY + ",standout", urwid.BLACK),
        ('head_foot', urwid.LIGHT_GRAY + ",standout", urwid.BLACK),
        ('key', urwid.BLACK, urwid.DARK_GREEN,),

        ('console', urwid.WHITE, urwid.DARK_BLUE),

        ('searchbar', urwid.WHITE, urwid.DARK_BLUE),
        ('search_result', urwid.DARK_GREEN + ",standout", urwid.WHITE),
        ('searchterm', urwid.DARK_GREEN, urwid.WHITE),

        ('SUCCESS', urwid.DARK_GREEN, urwid.DEFAULT),
        ('FAILED', urwid.DARK_RED, urwid.DEFAULT),
        ('UNSTABLE', urwid.DARK_CYAN, urwid.DEFAULT),
        ('ABORTED', urwid.DARK_MAGENTA, urwid.DEFAULT),
        ('DISABLED', urwid.LIGHT_GRAY, urwid.DEFAULT),
        ('NOTBUILT', urwid.DARK_BLUE, urwid.DEFAULT),
        ('BUILDIONG', urwid.BROWN, urwid.DEFAULT),

    ]

    table_head_text = [("table_heading", "ID"),
                       ("table_heading", " Available Jobs"),
                       ("table_heading", "Last Successful"),
                       ("table_heading", "Last Fail"),
                       ("table_heading", "Last Duration")]

    footer_text = [
        "   -",
        ('title', "Jenkins Job Browser"), "-    ",
        ('key', "(PAGE) UP/DOWN"), ",",
        ('key', "LEFT/RIGHT"), ",",
        ('key', "HOME/END"),
        " | ",
        ('key', "Enter"), ",",
        ('key', "+"), ",",
        ('key', "-"),
        " | ",
        ('key', "Q"),
    ]

    def __init__(self, jenkins_settings):
        # set some class vars
        self.jenkins_settings = jenkins_settings
        self.jenkins_wrapper = JenkinsWrapper(self.jenkins_settings)

        self.openedconsole = False
        self.quit_confirm = False
        self.pager_term = None
        self.search_bar = None
        self.searchmode = False
        self.current_jobdict = None
        self.search_result = []
        self.ragequit = False
        self.main_loop = None
        self.old_search_term = ""
        self.current_search_term = ""

        # ctrl+c support
        def signal_handler(signal, frame):
            self.ragequit = True
            raise urwid.ExitMainLoop()
        signal.signal(signal.SIGINT, signal_handler)

        # create the layout
        self.view = urwid.Frame(None, header=None, footer=None)
        self.__refresh_header_footer()
        self.__refresh_jenkins(init=True)

    def main(self):
        """Run the program."""

        self.main_loop = urwid.MainLoop(self.view, self.palette, unhandled_input=self.unhandled_input)
#         self.reset_view()

        def initial_update(y, z):
            self.__refresh_jenkins()
        self.main_loop.set_alarm_in(0.01, initial_update)
        self.main_loop.run()

        return self.ragequit

    def reset_view(self):
        self.__refresh_jenkins(init=True)
        self.__refresh_header_footer()
        self.__refresh_jenkins()

    def __refresh_header_footer(self):

        # headder
        self.head_connection = urwid.AttrWrap(urwid.Text(self.get_header(self.jenkins_wrapper.settings.host, user=self.jenkins_wrapper.settings.auth[0] if self.jenkins_wrapper.settings.auth else "Anonymous")), 'head_foot')
        self.head_table = URWIDElements.job_lines_to_column(self.table_head_text)
        self.header = urwid.Pile([self.head_connection, self.head_table], focus_item=0)
        self.view.header = self.header

        # footer
        self.user_input = urwid.Text("Status: ")
        self.status_bar = urwid.AttrWrap(urwid.Text(self.footer_text), 'head_foot')
        self.footer = urwid.Pile([self.status_bar, self.user_input], focus_item=0)
        self.view.footer = self.footer

    def __refresh_jenkins(self, init=False):
        if not init:
            # Actually load data from jenkins ...
            self.print_status("Refreshing Jobs - please wait...")
            self.main_loop.draw_screen()
            self.current_jobdict = self.jenkins_wrapper.get_detailed_joblist(self.status_function)
        else:
            self.current_jobdict = {"Loading...": {"color": "blue"}}
        self.topnode = JenkinsInstanceNode(self.jobdict2urwiddict(self.current_jobdict))
        self.listboxcontent = urwid.TreeWalker(self.topnode)

        self.listbox = VimBindingTreeListBox(self.listboxcontent, self.print_status)
        self.listbox.offset_rows = 1
        self.view.body = self.listbox

    def jobdict2urwiddict(self, detailed_jobdict):
        retval = {"name": [""], "children": []}

        for i, (job_name, info) in enumerate(detailed_jobdict.iteritems()):
            color = info['color']

            if '_anime' in color:
                color = color.split('_')[0]
                color = 'building'

            last_success = None
            last_fail = None
            last_dur = None

            try:
                last_success = info['lastSuccessfulBuild']['number'] if info['lastSuccessfulBuild'] else "N/A"
                last_fail = info['lastFailedBuild']['number'] if info['lastFailedBuild'] else "N/A"
                last_dur = info['lastSuccessfulBuild']['number'] if info['lastSuccessfulBuild'] else "N/A"

                for a_build in info['builds']:

                    if last_success and a_build['number'] == last_success:
                        last_success = datetime.datetime.fromtimestamp(a_build['info']['timestamp'] / 1000).strftime('%H:%M:%S %d.%m.%y')

                    if last_fail and a_build['number'] == last_fail:
                        last_fail = datetime.datetime.fromtimestamp(a_build['info']['timestamp'] / 1000).strftime('%H:%M:%S %d.%m.%y')

                    if last_dur and a_build['number'] == last_dur:
                        ms = a_build['info']['duration']

                        last_dur = "{:02}".format(int((ms / (1000.0 * 60.0 * 60.0)) % 24.0)) + ":" + \
                            "{:02}".format(int((ms / (1000.0 * 60.0)) % 60.0)) + ":" + \
                            "{:02}".format(int((ms / 1000.0) % 60.0))
            except Exception as e:
                pass

            retval['children'].append({"name": [str(i + 1), (self.COLOR_MAPPING[color], job_name), str(last_success), str(last_fail), str(last_dur)], "realname": str(job_name), "job_number": i + 1})
            retval['children'][i]['children'] = []
            retval['children'][i]['children'].append({"name": self.OPTION_LABEL_JOB_INFO})
            retval['children'][i]['children'].append({"name": self.OPTION_LABEL_BUILD})
            retval['children'][i]['children'].append({"name": self.OPTION_LABEL_LAST_BUILD_LOG})

        return retval

    def get_header(self, host, user="******"):
        return [('title', "Jenkins runnung at "), "  ", ('key', str(host)),
                "  |  ",
                ('title', 'Login: '******'key', str(user))]

    def update_search_info(self, amount, extra=[]):
        status = ["Found {} search results for '".format(amount), ('searchterm', str(self.current_search_term)), "'  --  ", ('key', 'ESC'), " exit search mode"]
        status = status + extra
        self.print_status(status)

    def search_function(self, search_term):
        search_result = []
        for a_job, _ in self.current_jobdict.iteritems():
            if search_term.lower() in a_job.lower():
                search_result.append(a_job)

        self.update_search_info(len(search_result), extra=[", ", ('key', 'Enter'), " submit search"])

        # always select the first hit
        self.search_result_selection = -1
        self.select_next_searchresult()

        return search_result

    def execute_search(self, x, y):
        self.current_search_term = y
        self.search_result = self.search_function(self.current_search_term)
        self.search_result_selection = -1
        self.old_search_term = self.search_bar.get_search_term()
        self.highlight_search_results()

    def show_search_bar(self):
        self.search_bar = SearchBar()
        self.view.footer = urwid.Pile([self.user_input, self.search_bar], focus_item=1)
        self.view.focus_position = "footer"
        self.print_status("Press 'ESC' to abort search...")
        urwid.connect_signal(self.search_bar, 'change', self.execute_search)
        self.listbox.disable_keys()

    def hide_search_bar(self):
        if self.search_bar:
            self.listbox.enable_keys()
            self.topnode.reset_highlights()
            self.__refresh_header_footer()
            self.search_bar = None
            self.print_status("")
            self.current_search_term = ""
            self.searchmode = False
            self.view.focus_position = "body"

    def show_pager_term(self, text):
        self.pager_term = ConsoleOutputPager(self.hide_pager_term, text, self.main_loop)
        self.view.body = self.pager_term
        self.print_status("Press 'q' to quit console view...")

    def hide_pager_term(self):
        if self.pager_term:
            self.pager_term.change_focus(False)
            self.view.body = self.listbox
            self.pager_term = None
            self.print_status("")

    def status_function(self, message):
        self.print_status(message)
        self.main_loop.draw_screen()

    def print_status(self, text):
        self.user_input.set_text(text)

    def print_append_status(self, text):
        self.user_input.set_text(str(self.user_input.get_text()) + text)

    def append_status(self, text):
        self.user_input.set_text(self.user_input.get_text()[0] + text)

    def unhandled_input(self, k):
        if self.quit_confirm:
            if k in ('q', 'Q'):
                raise urwid.ExitMainLoop()

            self.quit_confirm = False
            self.print_status("")
            return

        if self.searchmode:
            self.highlight_search_results()
            if self.view.focus_position == "body":
                if k is 'enter':
                    self.hide_search_bar()
                    self._keypress_enter()
                if k is '/':
                    self.view.focus_position = "footer"
                elif k in ('f3', "n", 'j', 'down'):
                    self.select_next_searchresult()
                elif k in ('f4', "N", 'k', 'up'):
                    self.select_next_searchresult(revese=True)
                elif k in ('ctrl l', 'f8'):
                    self.search_bar.set_edit_text("")
                elif k in ('esc', 'q'):
                    self.hide_search_bar()
                elif k in ('h', 'l', 'left', 'right'):
                    pass
            else:
                if k is 'enter':
                    self.update_search_info(len(self.search_result), extra=[", ", ('key', 'n'), "/", ('key', 'N'), " or ", ('key', 'F3'), "/", ('key', 'F4'), " jump to next/previous search result"])
                    # Note: this is a hack as results off screen would not be
                    # highlighted. no idea why ...

                    self.view.focus_position = "body"

            return False

        if k in ('q', 'Q'):
            if not self.quit_confirm:
                self.quit_confirm = True
                self.print_status("Press 'q' again to quit...")

        elif k is '/':
            self.show_search_bar()
            self.searchmode = True

#        elif k in ('ctrl d',):
#            self.print_status("wooop")

        elif k is 'f5':
            self.reset_view()
            self.print_status("Refresh done.")

        elif k is 'enter':
            self._keypress_enter()
        return True

    def _keypress_enter(self):
        selected_node = self.listbox.get_focus()[1]

        # Execute the selected option
        if isinstance(selected_node, JenkinsOptionNode):

            job_name = selected_node.get_job_name()
            chosen_option = selected_node.get_display_text()

            if chosen_option is self.OPTION_LABEL_JOB_INFO:
                import pprint

                self.show_pager_term(pprint.pformat(self.current_jobdict[job_name], indent=4))

                #self.show_pager_term(pprint.pformat(self.jenkins_wrapper.get_jobs_details(job_name), indent=4))

            elif chosen_option is self.OPTION_LABEL_BUILD:
                self.print_status("Triggering build of '{}'...".format(job_name))
                try:
                    result_code = self.jenkins_wrapper.jenkins_build(job_name)
                    if result_code < 400:
                        self.print_status("Build was successfully triggered, Code: " + str(result_code))
                except Exception as error:
                    self.print_status("Error when building {}: {}".format(job_name, str(error)))

            elif chosen_option is self.OPTION_LABEL_LAST_BUILD_LOG:
                try:
                    log = self.jenkins_wrapper.get_last_build_log(job_name)
                    self.show_pager_term(log)
                except Exception as error:
                    self.print_status("Error when querying for log of {}: {}".format(job_name, str(error)))

        # Toggle folding using enter
        elif isinstance(selected_node, JenkinsJobNode) or \
                isinstance(selected_node, JenkinsInstanceNode):
            widget = self.listbox.get_focus()[0]
            widget.expanded = not widget.expanded
            widget.update_expanded_icon()

        # Should never happen ...
        else:
            self.user_input.set_text("wut?!")

    def highlight_search_results(self):
        self.topnode.reset_highlights()
        # make sure we have the instance root element
        self.topnode.visually_highlight_jobs(self.search_result)

    def select_next_searchresult(self, revese=False):
        # reset the view to draw the new results
        self.topnode.reset_highlights()

        if len(self.search_result) > 0:
            # increase the counter
            if revese:
                self.search_result_selection = (self.search_result_selection + -1) if self.search_result_selection > 0 else len(self.search_result) - 1
            else:
                self.search_result_selection = (self.search_result_selection + 1) if self.search_result_selection < len(self.search_result) - 1 else 0

            # obtain name of the job to highlight
            job_name = self.search_result[self.search_result_selection]

            self.view.body.set_focus(self.topnode.job_name2node(job_name))
            self.main_loop.draw_screen()
            self.highlight_search_results()

            selected_node = self.topnode.visually_highlight_jobs(job_name, style="focused")