Skip to content

Commit

Permalink
MT#55942 Add multi-site config loading support via sites.yml
Browse files Browse the repository at this point in the history
We check whether we have both a sites.yml and a sites/ directory.
If so, and multi-sites support is enabled, then we proceed to load
all the multi-sites configurations, by placing each of them under
«sites.<site-name>.<config>». Adding a «sites.current» link to the
current site, and filling the root tree config with the current site
as well for backwards compatibility.

Change-Id: Ia810f57e8e976eef6a7582bddda46c1701456ed6
  • Loading branch information
guillemj committed Aug 12, 2023
1 parent 264e36b commit ea15166
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 7 deletions.
4 changes: 4 additions & 0 deletions etc/ngcp-ngcpcfg/ngcpcfg.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ TEMPLATE_POOL_BASE="${NGCPCTL_MAIN}/templates"
# location of service definitions
SERVICES_POOL_BASE="${NGCPCTL_MAIN}/templates"

# location of sites info for templates
SITES_DIR="${NGCPCTL_MAIN}/sites"
SITES_CONFIG="${NGCPCTL_MAIN}/sites.yml"

# location of instances info for templates
TEMPLATE_INSTANCES="${NGCPCTL_MAIN}/instances.yml"

Expand Down
2 changes: 2 additions & 0 deletions functions/init
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ export LC_ALL=C
export CONFIG_POOL
export HOST_CONFIG
export LOCAL_CONFIG
export SITES_CONFIG
export SITES_DIR
export NGCPCTL_CONFIG
export CONSTANTS_CONFIG
export MAINTENANCE_CONFIG
Expand Down
58 changes: 56 additions & 2 deletions helper/tt2-process
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ $TIME_FORMAT =~ s/^\+//;

my $NGCPCTL_MAIN = $ENV{NGCPCTL_MAIN};
my $TEMPLATE_POOL_BASE = $ENV{TEMPLATE_POOL_BASE};
my $SITES_DIR = $ENV{SITES_DIR} // "$NGCPCTL_MAIN/sites";
my $SITES_CONFIG = $ENV{SITES_CONFIG} // "$NGCPCTL_MAIN/sites.yml";
my $CONFIG_POOL = $ENV{CONFIG_POOL} // '';

my %options = (
Expand All @@ -35,6 +37,8 @@ my %options = (
chomp $options{jobs};

error("NGCPCTL_MAIN is not defined") unless $NGCPCTL_MAIN;
error("SITES_DIR is not defined") unless $SITES_DIR;
error("SITES_CONFIG is not defined") unless $SITES_CONFIG;
error("TEMPLATE_POOL_BASE is not defined") unless $TEMPLATE_POOL_BASE;

GetOptions(\%options,
Expand Down Expand Up @@ -436,14 +440,64 @@ sub merge_yml {
sub process {
my %options = @_;
my $config = {};
my $sites = {};

my $visible_jobs = $options{jobs} || 'unlimited';
info("Building configurations with $visible_jobs concurrent jobs");

foreach my $file (@{$options{config}}) {
$config = merge_yml($config, $file);
if (-f $SITES_CONFIG && -d $SITES_DIR && -l "$SITES_DIR/current") {
$sites = LoadFile($SITES_CONFIG);
}

$sites->{sites_enable} //= 'no';

if ($sites->{sites_enable} eq 'yes') {
info("Loading in multi-site mode");

my $current_site = readlink "$SITES_DIR/current";
if (not defined $current_site) {
error("Cannot read symlink target for $SITES_DIR/current: $!");
}
info("Current site is '$current_site'");

if (not exists $sites->{sites}{$current_site}) {
error("Cannot find current site $current_site in sites definitions.");
}

foreach my $site (sort keys %{$sites->{sites}}) {
foreach my $file (@{$options{config}}) {
my $site_file = "$SITES_DIR/$site/" . basename($file);

$sites->{sites}{$site} = merge_yml($sites->{sites}{$site}, $site_file);
}
}

# First fill the root config with the current site.
$config = $sites->{sites}{$current_site};

# Then fill the sites sub-trees.
foreach my $site (sort keys %{$sites->{sites}}) {
$config->{sites}{$site} = $sites->{sites}{$site};
}

# Create the 'current' reference.
$config->{sites}{current} = $config->{sites}{$current_site};
} else {
info("Loading in single-site mode");

foreach my $file (@{$options{config}}) {
$sites = merge_yml($sites, $file);
}

# First fill the root config with the current site.
$config = $sites;

# Create the 'current' site reference for compatibility.
$config->{sites}{current} = $sites;
}

$config->{sites_enable} = $sites->{sites_enable};

my $tt = NGCP::Template->new();
my $rc;

Expand Down
7 changes: 5 additions & 2 deletions scripts/cat
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fi
# shellcheck disable=SC1090
. "${FUNCTIONS}"/main

CONFIG_TYPES=(config maintenance network constants)
CONFIG_TYPES=(config maintenance sites network constants)

# Get the list of config types to load.
if [ "${#:-}" -eq 0 ]; then
Expand All @@ -49,7 +49,7 @@ for t in "${CONFIG_TYPES[@]}"; do
done
while [ -n "${1:-}" ]; do
case "$1" in
constants|config|network|maintenance)
constants|config|network|maintenance|sites)
config_types[$1]=true
shift
;;
Expand All @@ -72,6 +72,9 @@ if "${config_types[config]}"; then
configs_try+=("${HOST_CONFIG}")
configs_try+=("${LOCAL_CONFIG}")
fi
if "${config_types[sites]}"; then
configs_try+=("${SITES_CONFIG}")
fi
if "${config_types[maintenance]}"; then
configs_try+=("${MAINTENANCE_CONFIG}")
fi
Expand Down
18 changes: 15 additions & 3 deletions scripts/edit
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,25 @@ if [ "${#:-}" -ge 1 ] ; then
if [ ! -r "${file}" ]; then
case ${file,,} in
1|config|config.yml)
file="${NGCPCTL_MAIN}/config.yml"
if [ -e "${SITES_DIR}/current/config.yml" ]; then
file="${SITES_DIR}/current/config.yml"
else
file="${NGCPCTL_MAIN}/config.yml"
fi
;;
2|network|network.yml)
file="${NGCPCTL_MAIN}/network.yml"
if [ -e "${SITES_DIR}/current/network.yml" ]; then
file="${SITES_DIR}/current/network.yml"
else
file="${NGCPCTL_MAIN}/network.yml"
fi
;;
3|constants|constants.yml)
file="${NGCPCTL_MAIN}/constants.yml"
if [ -e "${SITES_DIR}/current/constants.yml" ]; then
file="${SITES_DIR}/current/constants.yml"
else
file="${NGCPCTL_MAIN}/constants.yml"
fi
;;
*)
echo "Error: Unknown choice '${file}'. Aborting." >&2
Expand Down
4 changes: 4 additions & 0 deletions t/fixtures/ngcpcfg.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
# location of service definitions
[ -n "${SERVICES_POOL_BASE:-}" ] || SERVICES_POOL_BASE="${NGCPCTL_MAIN}/templates"

# location of sites info for templates
[ -n "${SITES_DIR:-}" ] || SITES_DIR="${NGCPCTL_MAIN}/sites"
[ -n "${SITES_CONFIG:-}" ] || SITES_CONFIG="${NGCPCTL_MAIN}/sites.yml"

# timestamp format for console output
[ -n "${TIME_FORMAT:-}" ] || TIME_FORMAT="+%F %T"

Expand Down
4 changes: 4 additions & 0 deletions t/fixtures/ngcpcfg_carrier.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
# location of service definitions
[ -n "${SERVICES_POOL_BASE:-}" ] || SERVICES_POOL_BASE="${NGCPCTL_MAIN}/templates"

# location of sites info for templates
[ -n "${SITES_DIR:-}" ] || SITES_DIR="${NGCPCTL_MAIN}/sites"
[ -n "${SITES_CONFIG:-}" ] || SITES_CONFIG="${NGCPCTL_MAIN}/sites.yml"

# timestamp format for console output
[ -n "${TIME_FORMAT:-}" ] || TIME_FORMAT="+%F %T"

Expand Down
4 changes: 4 additions & 0 deletions t/fixtures/ngcpcfg_carrier_instances.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
# location of service definitions
[ -n "${SERVICES_POOL_BASE:-}" ] || SERVICES_POOL_BASE="${NGCPCTL_MAIN}/templates"

# location of sites info for templates
[ -n "${SITES_DIR:-}" ] || SITES_DIR="${NGCPCTL_MAIN}/sites"
[ -n "${SITES_CONFIG:-}" ] || SITES_CONFIG="${NGCPCTL_MAIN}/sites.yml"

# location of instances info for templates
[ -n "${TEMPLATE_INSTANCES:-}" ] || TEMPLATE_INSTANCES="${NGCPCTL_MAIN}/instances.yml"

Expand Down
4 changes: 4 additions & 0 deletions t/fixtures/ngcpcfg_network_interfaces.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
# location of service definitions
[ -n "${SERVICES_POOL_BASE:-}" ] || SERVICES_POOL_BASE="${NGCPCTL_MAIN}/templates"

# location of sites info for templates
[ -n "${SITES_DIR:-}" ] || SITES_DIR="${NGCPCTL_MAIN}/sites"
[ -n "${SITES_CONFIG:-}" ] || SITES_CONFIG="${NGCPCTL_MAIN}/sites.yml"

# timestamp format for console output
[ -n "${TIME_FORMAT:-}" ] || TIME_FORMAT="+%F %T"

Expand Down
4 changes: 4 additions & 0 deletions t/fixtures/ngcpcfg_pro.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
# location of service definitions
[ -n "${SERVICES_POOL_BASE:-}" ] || SERVICES_POOL_BASE="${NGCPCTL_MAIN}/templates"

# location of sites info for templates
[ -n "${SITES_DIR:-}" ] || SITES_DIR="${NGCPCTL_MAIN}/sites"
[ -n "${SITES_CONFIG:-}" ] || SITES_CONFIG="${NGCPCTL_MAIN}/sites.yml"

# timestamp format for console output
[ -n "${TIME_FORMAT:-}" ] || TIME_FORMAT="+%F %T"

Expand Down
4 changes: 4 additions & 0 deletions t/fixtures/ngcpcfg_pro_instances.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
# location of service definitions
[ -n "${SERVICES_POOL_BASE:-}" ] || SERVICES_POOL_BASE="${NGCPCTL_MAIN}/templates"

# location of sites info for templates
[ -n "${SITES_DIR:-}" ] || SITES_DIR="${NGCPCTL_MAIN}/sites"
[ -n "${SITES_CONFIG:-}" ] || SITES_CONFIG="${NGCPCTL_MAIN}/sites.yml"

# timestamp format for console output
[ -n "${TIME_FORMAT:-}" ] || TIME_FORMAT="+%F %T"

Expand Down
2 changes: 2 additions & 0 deletions t/fixtures/programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"PAIR_CONFIG",
"HOST_CONFIG",
"LOCAL_CONFIG",
"SITES_DIR",
"SITES_CONFIG",
"CONSTANTS_CONFIG",
"MAINTENANCE_CONFIG",
"NETWORK_CONFIG",
Expand Down
4 changes: 4 additions & 0 deletions t/fixtures/read_cfg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ echo "TEMPLATE_POOL_BASE=${TEMPLATE_POOL_BASE:-}"
# location of service definitions
echo "SERVICES_POOL_BASE=${SERVICES_POOL_BASE:-}"

# location of sites info for templates
echo "SITES_DIR=${SITES_DIR:-}"
echo "SITES_CONFIG=${SITES_CONFIG:-}"

# location of instances info for templates
echo "TEMPLATE_INSTANCES=${TEMPLATE_INSTANCES:-}"

Expand Down

0 comments on commit ea15166

Please sign in to comment.