~doanac/ubuntu-ci-services-itself/lander-integration-bugs

« back to all changes in this revision

Viewing changes to cli/ubuntu-ci

[r=Chris Johnston, PS Jenkins bot] Better error messages for the user in case ticket system server is down or unreachable or data store fails somehow  from Ursula Junque

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
import logging
19
19
import os
20
20
import sys
 
21
from urllib2 import URLError
21
22
 
22
23
from ci_libs import status, utils
23
24
from ci_libs.file_handler import (ChangesFileNotFound, FileToUploadNotFound,
24
25
                                  UploadDirNotFound)
25
26
from ci_libs.ticket import new_ticket
 
27
from ci_utils.data_store import DataStoreException
26
28
 
27
29
DEF_CFG = os.path.join(os.environ["HOME"], '.ubuntu-ci')
28
30
 
66
68
    return parser.parse_args(args)
67
69
 
68
70
 
 
71
def set_loglevel(logger, loglevel):
 
72
    if loglevel == logging.DEBUG:
 
73
        formatting = "%(asctime)s %(module)-14s %(levelname)-8s %(message)s"
 
74
    else:
 
75
        formatting = "%(module)-14s %(levelname)-8s %(message)s"
 
76
    logging.basicConfig(format=formatting, datefmt='%Y-%m-%d %H:%M:%S')
 
77
    logger.setLevel(loglevel)
 
78
 
 
79
 
69
80
def main(args=None, log=None):
70
81
    if log is None:
71
 
        logging.basicConfig()
72
82
        log = logging.getLogger()
73
83
        log.name = "ubuntu-ci"
74
84
 
75
85
    try:
76
86
        args = parse_arguments(args)
 
87
        if args.verbosity == 2:
 
88
            set_loglevel(log, logging.INFO)
 
89
        elif args.verbosity > 2:
 
90
            set_loglevel(log, logging.DEBUG)
 
91
        else:
 
92
            set_loglevel(log, logging.ERROR)
77
93
 
78
94
        # Validating sources paths so ticket is created only after all files
79
95
        # found.
85
101
            # We're not in create_ticket context, moving on.
86
102
            pass
87
103
 
88
 
        if args.verbosity == 2:
89
 
            log.setLevel(logging.INFO)
90
 
        elif args.verbosity > 2:
91
 
            log.setLevel(logging.DEBUG)
92
 
        else:
93
 
            log.setLevel(logging.ERROR)
94
 
 
95
104
        utils.load_config(DEF_CFG)
96
 
 
97
105
        args.func(args)
98
106
        return 0
99
 
 
100
107
    except ChangesFileNotFound, exc:
101
108
        log.error("Changes file not found: %s" % exc)
102
109
    except UploadDirNotFound, exc:
104
111
    except FileToUploadNotFound, exc:
105
112
        log.error("File to upload not found: %s. Maybe a wrong or missing -f?"
106
113
                  % exc)
 
114
    except URLError, exc:
 
115
        try:
 
116
            reason = exc.reason.msg
 
117
        except AttributeError:
 
118
            reason = exc.reason
 
119
        log.error("Cannot reach the server at %s. Please, check your "
 
120
                  "configuration file (%s): is 'ci_url' correctly set? "
 
121
                  "Is the server accessible from this machine? "
 
122
                  "(%s)." % (utils.CI_URL, DEF_CFG, reason))
 
123
    except DataStoreException, exc:
 
124
        log.error("Data Store Error: %s" % exc)
107
125
    except Exception, exc:
108
126
        log.exception(exc)
109
127
    return 1