~larry-e-works/uci-engine/amqp-to-kombu

« back to all changes in this revision

Viewing changes to cupstream2distro/manual/cu2d-skip

  • Committer: Francis Ginther
  • Date: 2014-06-10 20:42:46 UTC
  • mto: This revision was merged to the branch mainline in revision 571.
  • Revision ID: francis.ginther@canonical.com-20140610204246-b1bsrik7nlcolqy7
Import lp:cupstream2distro rev 605.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/python
 
2
""" Skip an arch for a project on a stack
 
3
"""
 
4
# Copyright (C) 2012-2013, Canonical Ltd (http://www.canonical.com/)
 
5
#
 
6
# Author: Jean-Baptiste Lallement <jean-baptiste.lallement@canonical.com>
 
7
# Author: Didier Roche <didier.roche@canonical.com>
 
8
#
 
9
# This software is free software: you can redistribute it
 
10
# and/or modify it under the terms of the GNU General Public License
 
11
# as published by the Free Software Foundation, either version 3 of
 
12
# the License, or (at your option) any later version.
 
13
#
 
14
# This software is distributed in the hope that it will
 
15
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 
16
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
# GNU General Public License for more details.
 
18
#
 
19
# You should have received a copy of the GNU General Public License
 
20
# along with this software.  If not, see <http://www.gnu.org/licenses/>.
 
21
 
 
22
import os
 
23
import logging
 
24
import sys
 
25
import yaml
 
26
import jenkins
 
27
import argparse
 
28
 
 
29
BINDIR = os.path.dirname(__file__)
 
30
PREFIX = 'cu2d'
 
31
DEFAULT_CREDENTIALS = os.path.expanduser('~/.cu2d.cred')
 
32
DEFAULT_RELEASE = 'head'
 
33
 
 
34
 
 
35
def load_jenkins_credentials(path):
 
36
    """ Load Credentials from credentials configuration file """
 
37
    if not os.path.exists(path):
 
38
        return False
 
39
 
 
40
    logging.debug('Loading credentials from %s', path)
 
41
    cred = yaml.load(file(path, 'r'))
 
42
 
 
43
    for param in ('username', 'password', 'url', 'token'):
 
44
        if not param in cred['jenkins']:
 
45
            logging.error("Setting missing from jenkins credentials: %s. "
 
46
                          "Aborting!", param)
 
47
            sys.exit(1)
 
48
    return False if not 'jenkins' in cred else cred['jenkins']
 
49
 
 
50
 
 
51
def run(jkcfg, stack, release, project, archs, **kwargs):
 
52
    """ Force publication of a stack/release
 
53
 
 
54
    :param jkcfg: dictionary with the credentials
 
55
    :param stack: Name of the stack to publish
 
56
    :param release: Name of the release the stack belongs tostack to publish
 
57
    :param project: project on which we want to separate one or more archs
 
58
    :param archs: archs we want to skip
 
59
    """
 
60
    logging.debug('Logging to Jenkins')
 
61
    if 'username' in jkcfg:
 
62
        jkh = jenkins.Jenkins(jkcfg['url'],
 
63
                                  username=jkcfg['username'],
 
64
                                  password=jkcfg['password'])
 
65
    else:
 
66
        jkh = jenkins.Jenkins(jkcfg['url'])
 
67
 
 
68
    jobname = "cu2d-skip-project-archs"
 
69
    args = {'stack': stack,
 
70
            'release': release,
 
71
            'project': project,
 
72
            'archs': " ".join(archs)}
 
73
 
 
74
    if not jkh.job_exists(jobname):
 
75
        logging.info("Job '%s' doesn't exists.", jobname)
 
76
        return False
 
77
 
 
78
    logging.info('Triggering build: %s', jobname)
 
79
    jkh.build_job(jobname, args, token=jkcfg['token'])
 
80
 
 
81
    return True
 
82
 
 
83
 
 
84
def set_logging(debugmode=False):
 
85
    """Initialize logging"""
 
86
    logging.basicConfig(
 
87
        level=logging.DEBUG if debugmode else logging.INFO,
 
88
        format="%(asctime)s %(levelname)s %(message)s"
 
89
        )
 
90
    logging.debug('Debug mode enabled')
 
91
 
 
92
 
 
93
def main():
 
94
    ''' Main routine '''
 
95
    parser = argparse.ArgumentParser(
 
96
        description='Tell an existing run to skip one or multiple archs for a build')
 
97
    parser.add_argument('-C', '--credentials', metavar='CREDENTIALFILE',
 
98
                        default=DEFAULT_CREDENTIALS,
 
99
                        help='use Jenkins and load credentials from '
 
100
                        'CREDENTIAL FILE (default: %s)' % DEFAULT_CREDENTIALS)
 
101
    parser.add_argument('-r', '--release',
 
102
                        default=DEFAULT_RELEASE,
 
103
                        help='Release the stack to publish belongs to '
 
104
                        '(default: %s)' % DEFAULT_RELEASE)
 
105
    parser.add_argument('-d', '--debug', action='store_true', default=False,
 
106
                        help='enable debug mode')
 
107
    parser.add_argument('stack', help='Name of the stack to publish')
 
108
    parser.add_argument('project', help='Name of the project (source package) to skip one arch')
 
109
    parser.add_argument('archs', nargs='+', help='Archs (separated by commas) to skip')
 
110
 
 
111
    args = parser.parse_args()
 
112
    set_logging(args.debug)
 
113
 
 
114
    credentials = None
 
115
    if args.credentials:
 
116
        credentialsfile = args.credentials
 
117
        credentials = load_jenkins_credentials(
 
118
            os.path.expanduser(credentialsfile))
 
119
        if not credentials:
 
120
            logging.error('Credentials not found. Aborting!')
 
121
            sys.exit(1)
 
122
 
 
123
        if not run(credentials, args.stack, args.release, args.project, args.archs):
 
124
            logging.error('Failed to run job. Aborting!')
 
125
            sys.exit(2)
 
126
 
 
127
if __name__ == "__main__":
 
128
    main()