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

« back to all changes in this revision

Viewing changes to cupstream2distro/cupstream2distro/silomanager.py

  • 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
# -*- coding: utf-8 -*-
 
2
# Copyright (C) 2014 Canonical
 
3
#
 
4
# Authors:
 
5
#  Didier Roche
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify it under
 
8
# the terms of the GNU General Public License as published by the Free Software
 
9
# Foundation; version 3.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but WITHOUT
 
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
13
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
14
# details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along with
 
17
# this program; if not, write to the Free Software Foundation, Inc.,
 
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
19
 
 
20
import json
 
21
import logging
 
22
import os
 
23
import shutil
 
24
 
 
25
from cupstream2distro.settings import SILO_CONFIG_FILENAME, SILO_NAME_LIST, SILO_PREPROD_NAME_LIST, SILO_STATUS_RSYNCDIR
 
26
from cupstream2distro.utils import ignored
 
27
 
 
28
 
 
29
def get_silo_root_path():
 
30
    '''Recursiverly look for a config file as a silo path name from cur dir and return it'''
 
31
    silo_root_path = os.path.abspath('.')
 
32
    while not os.path.isfile(os.path.join(silo_root_path, SILO_CONFIG_FILENAME)):
 
33
        if silo_root_path == os.path.abspath(os.path.dirname(silo_root_path)):
 
34
            raise Exception("We are not in a silo config path")
 
35
        silo_root_path = os.path.abspath(os.path.dirname(silo_root_path))
 
36
    return silo_root_path
 
37
 
 
38
def save_config(config, uri=''):
 
39
    """Save config in uri and copy to outdir"""
 
40
    if not uri:
 
41
        uri = get_silo_root_path()
 
42
 
 
43
    silo_config_path = os.path.abspath(os.path.join(uri, SILO_CONFIG_FILENAME))
 
44
    with ignored(OSError):
 
45
        os.makedirs(uri)
 
46
    try:
 
47
        new_file = "{}.new".format(silo_config_path)
 
48
        json.dump(config, open(new_file, 'w'))
 
49
        os.rename(new_file, silo_config_path)
 
50
    except TypeError as e:
 
51
        logging.error("Can't save configuration: " + e.message)
 
52
        os.remove(silo_config_path)
 
53
        return False
 
54
    # copy to outdir
 
55
    with ignored(OSError):
 
56
        os.makedirs(SILO_STATUS_RSYNCDIR)
 
57
    silo_name = os.path.dirname(silo_config_path).split(os.path.sep)[-1]
 
58
    dest = os.path.join(SILO_STATUS_RSYNCDIR, silo_name)
 
59
    logging.debug("Copying configuration from {} to {}".format(silo_config_path, dest))
 
60
    shutil.copy2(silo_config_path, os.path.join(SILO_STATUS_RSYNCDIR, silo_name))
 
61
    return True
 
62
 
 
63
def load_config(uri=None):
 
64
    """return a loaded config
 
65
 
 
66
    If no uri, load in the current directory"""
 
67
    if not uri:
 
68
        uri = os.path.abspath('.')
 
69
    logging.debug("Reading configuration in {}".format(uri))
 
70
    try:
 
71
        return json.load(open(os.path.join(uri, SILO_CONFIG_FILENAME)))
 
72
    # if silo isn't configured
 
73
    except IOError:
 
74
        pass
 
75
    except ValueError as e:
 
76
        logging.warning("Can't load configuration: " + e.message)
 
77
    return None
 
78
 
 
79
def remove_status_file(silo_name):
 
80
    """Remove status file"""
 
81
    os.remove(os.path.join(SILO_STATUS_RSYNCDIR, silo_name))
 
82
 
 
83
 
 
84
def is_project_not_in_any_configs(project_name, series, dest, base_silo_uri, ignore_silo, dont_error_but_warn=False):
 
85
    """Return true if the project for that serie in that dest is not in any configuration"""
 
86
    logging.info("Checking if {} is already configured for {} ({}) in another silo".format(project_name, dest.name, series.name))
 
87
    for silo_name in SILO_NAME_LIST:
 
88
        # we are reconfiguring current silo, ignoring it
 
89
        if ignore_silo == silo_name:
 
90
            continue
 
91
        config = load_config(os.path.join(base_silo_uri, silo_name))
 
92
        if config:
 
93
            if (config["global"]["dest"] == dest.self_link and config["global"]["series"] == series.self_link and
 
94
                (project_name in config["mps"] or project_name in config["sources"])):
 
95
                message = "{} is already prepared for the same serie and destination in {}".format(project_name, silo_name)
 
96
                if dont_error_but_warn:
 
97
                    logging.warning(message)
 
98
                else:
 
99
                    logging.error(message)
 
100
                return False
 
101
    return True
 
102
 
 
103
 
 
104
def find_silo_config_for_request_id(request_id, base_silo_uri):
 
105
    """Find a config silo matching the request id, return None otherwise"""
 
106
    logging.info("Checking if {} is in any assigned silo".format(request_id))
 
107
    for silo_name in SILO_NAME_LIST:
 
108
        config = load_config(os.path.join(base_silo_uri, silo_name))
 
109
        if config:
 
110
            if config["requestid"] == request_id:
 
111
                return config
 
112
    return None
 
113
 
 
114
 
 
115
def return_first_available_silo(base_silo_uri, preprod=False):
 
116
    """Check which silos are free and return the first available one. We separate preprod and production silos"""
 
117
    silo_list = [silo for silo in SILO_NAME_LIST if silo not in SILO_PREPROD_NAME_LIST]
 
118
    if preprod:
 
119
        silo_list = SILO_PREPROD_NAME_LIST
 
120
 
 
121
    for silo_name in silo_list:
 
122
        if not os.path.isfile(os.path.join(base_silo_uri, silo_name, SILO_CONFIG_FILENAME)):
 
123
            return silo_name
 
124
    return None
 
125
 
 
126
 
 
127
def get_config_step(config):
 
128
    """Get configuration step"""
 
129
    return config["global"]["step"]
 
130
 
 
131
def set_config_step(config, new_step, uri=''):
 
132
    """Set configuration step to new_step"""
 
133
    config["global"]["step"] = new_step
 
134
    return save_config(config, uri)
 
135
 
 
136
def set_config_status(config, status, uri='', add_url=True, ping=True):
 
137
    """Change status to reflect latest status"""
 
138
    build_url = os.getenv('BUILD_URL')
 
139
    url = ""
 
140
    if add_url and build_url:
 
141
        url = "{}console".format(build_url)
 
142
    config["global"]["status"] = {
 
143
        "message": status,
 
144
        "ping":  ping,
 
145
        "url": url
 
146
    }
 
147
    return save_config(config, uri)
 
148
 
 
149
def set_config_pkgversionlist(config, pkglist, uri=''):
 
150
    config["global"]["pkgversionlist"] = pkglist
 
151
    return save_config(config, uri)
 
152
 
 
153
def get_all_projects(config):
 
154
    """Get a list of all projets"""
 
155
    projects = []
 
156
    projects.extend(config["mps"])
 
157
    projects.extend(config["sources"])
 
158
    return projects