1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2014 Canonical
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.
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
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
25
from cupstream2distro.settings import SILO_CONFIG_FILENAME, SILO_NAME_LIST, SILO_PREPROD_NAME_LIST, SILO_STATUS_RSYNCDIR
26
from cupstream2distro.utils import ignored
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))
38
def save_config(config, uri=''):
39
"""Save config in uri and copy to outdir"""
41
uri = get_silo_root_path()
43
silo_config_path = os.path.abspath(os.path.join(uri, SILO_CONFIG_FILENAME))
44
with ignored(OSError):
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)
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))
63
def load_config(uri=None):
64
"""return a loaded config
66
If no uri, load in the current directory"""
68
uri = os.path.abspath('.')
69
logging.debug("Reading configuration in {}".format(uri))
71
return json.load(open(os.path.join(uri, SILO_CONFIG_FILENAME)))
72
# if silo isn't configured
75
except ValueError as e:
76
logging.warning("Can't load configuration: " + e.message)
79
def remove_status_file(silo_name):
80
"""Remove status file"""
81
os.remove(os.path.join(SILO_STATUS_RSYNCDIR, silo_name))
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:
91
config = load_config(os.path.join(base_silo_uri, silo_name))
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)
99
logging.error(message)
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))
110
if config["requestid"] == request_id:
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]
119
silo_list = SILO_PREPROD_NAME_LIST
121
for silo_name in silo_list:
122
if not os.path.isfile(os.path.join(base_silo_uri, silo_name, SILO_CONFIG_FILENAME)):
127
def get_config_step(config):
128
"""Get configuration step"""
129
return config["global"]["step"]
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)
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')
140
if add_url and build_url:
141
url = "{}console".format(build_url)
142
config["global"]["status"] = {
147
return save_config(config, uri)
149
def set_config_pkgversionlist(config, pkglist, uri=''):
150
config["global"]["pkgversionlist"] = pkglist
151
return save_config(config, uri)
153
def get_all_projects(config):
154
"""Get a list of all projets"""
156
projects.extend(config["mps"])
157
projects.extend(config["sources"])