Skip to content

Commit

Permalink
Add config.d-style directory for blahp configuration. HTCONDOR-521
Browse files Browse the repository at this point in the history
The directory name is the name of the regular configuration file with
'.d' appended (default is /etc/blah.config.d/). Files in the directory
are processed in sorted order after the main configuration file. So
parameters set in config.d files will override those in the main file.
  • Loading branch information
JaimeFrey committed Jul 26, 2021
1 parent 2f024bd commit 8fc9df0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
24 changes: 22 additions & 2 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <dirent.h>

#include "blahpd.h"
#include "config.h"
Expand Down Expand Up @@ -89,8 +90,8 @@ config_setenv(const char *ipath)
config_entry *cur;
int n_added = 0;

envs_before = config_read_cmd(ipath, printenv_command_before, envs_before);
envs_after = config_read_cmd(ipath, printenv_command_after, envs_after);
config_read_cmd(ipath, printenv_command_before, envs_before);
config_read_cmd(ipath, printenv_command_after, envs_after);


/* Set in the local environment all env variables that were exported in */
Expand All @@ -117,9 +118,14 @@ config_read(const char *ipath)
const char *set_command_format = ". %s; set";
char *path;
char *install_location=NULL;
char *config_dir=NULL;
config_handle *rha;
config_entry *bp;
FILE *test;
int file_cnt;
struct dirent **file_list = NULL;
char full_file[PATH_MAX];
int i;

if ((install_location = getenv("BLAHPD_LOCATION")) == NULL)
{
Expand Down Expand Up @@ -163,6 +169,20 @@ config_read(const char *ipath)
return NULL;
}

config_dir = (char *)malloc(strlen(rha->config_path)+3);
sprintf(config_dir, "%s.d", rha->config_path);
file_cnt = scandir(config_dir, &file_list, NULL, alphasort);
for (i = 0; i < file_cnt; i++)
{
if (file_list[i]->d_name[0] != '.')
{
snprintf(full_file, PATH_MAX, "%s/%s", config_dir, file_list[i]->d_name);
config_read_cmd(full_file, set_command_format, rha);
}
free(file_list[i]);
}
free(file_list);

if ((bp = config_get("blah_bin_directory", rha)) != NULL)
{
rha->bin_path = strdup(bp->value);
Expand Down
9 changes: 7 additions & 2 deletions src/scripts/blah.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ def __init__(self, path=None, defaults=None):
path = "/etc/blah.config"
# RawConfigParser requires ini-style [section headers] but since
# blah.config is also used as a shell script we need to fake one
config_dir = path + ".d/"
config_list = sorted(filter(os.path.isfile, glob.glob(config_dir + "*")))
config_list.insert(0, path)
self.header = 'blahp'
with open(path) as f:
config = f.read()
config = "[%s]\n" % (self.header)
for file in config_list:
with open(file) as f:
config += f.read()
vfile = StringIO('[%s]\n%s' % (self.header, config))

super(BlahConfigParser, self).__init__(defaults=defaults)
Expand Down
18 changes: 13 additions & 5 deletions src/scripts/blah_load_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,20 @@ fi

# Let blah_bin_directory be overridden in the config file.

config_file=/etc/blah.config
if [ -r "$BLAHPD_CONFIG_LOCATION" ]; then
. $BLAHPD_CONFIG_LOCATION
config_file=$BLAHPD_CONFIG_LOCATION
elif [ "x$BLAHPD_LOCATION" != "x" -a -r "${BLAHPD_LOCATION}/etc/blah.config" ]; then
config_file=${BLAHPD_LOCATION}/etc/blah.config
elif [ -r "${GLITE_LOCATION:-/opt/glite}/etc/blah.config" ]; then
. ${GLITE_LOCATION:-/opt/glite}/etc/blah.config
elif [ -r "${BLAHPD_LOCATION}/etc/blah.config" ]; then
# This will default to /etc/blah.config if BLAHPD_LOCATION is unset.
. ${BLAHPD_LOCATION}/etc/blah.config
config_file=${GLITE_LOCATION:-/opt/glite}/etc/blah.config
fi
config_dir=${config_file}.d

. $config_file

if [ -d "$config_dir" ] ; then
for file in `ls $config_dir` ; do
. ${config_dir}/${file}
done
fi

0 comments on commit 8fc9df0

Please sign in to comment.