lib: add support for scripts directory

Specify default via --with-scriptdir at compile time, override default
with --scriptdir at runtime. If unspecified, it's {sysconfdir}/scripts
(usually /etc/frr/scripts)

Signed-off-by: Quentin Young <qlyoung@nvidia.com>
This commit is contained in:
Quentin Young 2020-11-30 17:37:18 -05:00
parent fa22080d22
commit e4e0229aba
5 changed files with 35 additions and 6 deletions

View file

@ -138,6 +138,12 @@ AC_ARG_WITH([moduledir], [AS_HELP_STRING([--with-moduledir=DIR], [module directo
])
AC_SUBST([moduledir], [$moduledir])
AC_ARG_WITH([scriptdir], [AS_HELP_STRING([--with-scriptdir=DIR], [script directory (${libdir}/frr/scripts)])], [
scriptdir="$withval"
], [
scriptdir="\${sysconfdir}/scripts"
])
AC_SUBST([scriptdir], [$scriptdir])
AC_ARG_WITH([yangmodelsdir], [AS_HELP_STRING([--with-yangmodelsdir=DIR], [yang models directory (${datarootdir}/yang)])], [
yangmodelsdir="$withval"
@ -2423,19 +2429,23 @@ CFG_SBIN="$sbindir"
CFG_STATE="$frr_statedir"
CFG_MODULE="$moduledir"
CFG_YANGMODELS="$yangmodelsdir"
CFG_SCRIPT="$scriptdir"
for I in 1 2 3 4 5 6 7 8 9 10; do
eval CFG_SYSCONF="\"$CFG_SYSCONF\""
eval CFG_SBIN="\"$CFG_SBIN\""
eval CFG_STATE="\"$CFG_STATE\""
eval CFG_MODULE="\"$CFG_MODULE\""
eval CFG_YANGMODELS="\"$CFG_YANGMODELS\""
eval CFG_SCRIPT="\"$CFG_SCRIPT\""
done
AC_SUBST([CFG_SYSCONF])
AC_SUBST([CFG_SBIN])
AC_SUBST([CFG_STATE])
AC_SUBST([CFG_MODULE])
AC_SUBST([CFG_SCRIPT])
AC_SUBST([CFG_YANGMODELS])
AC_DEFINE_UNQUOTED([MODULE_PATH], ["$CFG_MODULE"], [path to modules])
AC_DEFINE_UNQUOTED([SCRIPT_PATH], ["$CFG_SCRIPT"], [path to scripts])
AC_DEFINE_UNQUOTED([YANG_MODELS_PATH], ["$CFG_YANGMODELS"], [path to YANG data models])
AC_DEFINE_UNQUOTED([WATCHFRR_SH_PATH], ["${CFG_SBIN%/}/watchfrr.sh"], [path to watchfrr.sh])
@ -2546,6 +2556,7 @@ state file directory : ${frr_statedir}
config file directory : `eval echo \`echo ${sysconfdir}\``
example directory : `eval echo \`echo ${exampledir}\``
module directory : ${CFG_MODULE}
script directory : ${CFG_SCRIPT}
user to run as : ${enable_user}
group to run as : ${enable_group}
group for vty sockets : ${enable_vty_group}

View file

@ -64,6 +64,7 @@ struct frrscript_codec frrscript_codecs_lib[] = {
/* Type codecs */
struct hash *codec_hash;
char scriptdir[MAXPATHLEN];
static unsigned int codec_hash_key(const void *data)
{
@ -213,7 +214,7 @@ struct frrscript *frrscript_load(const char *name,
frrlua_export_logging(fs->L);
char fname[MAXPATHLEN];
snprintf(fname, sizeof(fname), FRRSCRIPT_PATH "/%s.lua", fs->name);
snprintf(fname, sizeof(fname), "%s/%s.lua", scriptdir, fs->name);
int ret = luaL_loadfile(fs->L, fname);
@ -262,11 +263,13 @@ void frrscript_unload(struct frrscript *fs)
XFREE(MTYPE_SCRIPT, fs);
}
void frrscript_init()
void frrscript_init(const char *sd)
{
codec_hash = hash_create(codec_hash_key, codec_hash_cmp,
"Lua type encoders");
strlcpy(scriptdir, sd, sizeof(scriptdir));
/* Register core library types */
frrscript_register_type_codecs(frrscript_codecs_lib);
}

View file

@ -30,8 +30,6 @@
extern "C" {
#endif
#define FRRSCRIPT_PATH "/etc/frr/scripts"
typedef void (*encoder_func)(lua_State *, const void *);
typedef void *(*decoder_func)(lua_State *, int);
@ -92,8 +90,11 @@ void frrscript_register_type_codecs(struct frrscript_codec *codecs);
/*
* Initialize scripting subsystem. Call this before anything else.
*
* scriptdir
* Directory in which to look for scripts
*/
void frrscript_init(void);
void frrscript_init(const char *scriptdir);
/*

View file

@ -56,6 +56,7 @@ char frr_vtydir[256];
const char frr_dbdir[] = DAEMON_DB_DIR;
#endif
const char frr_moduledir[] = MODULE_PATH;
const char frr_scriptdir[] = SCRIPT_PATH;
char frr_protoname[256] = "NONE";
char frr_protonameinst[256] = "NONE";
@ -101,6 +102,7 @@ static void opt_extend(const struct optspec *os)
#define OPTION_DB_FILE 1006
#define OPTION_LOGGING 1007
#define OPTION_LIMIT_FDS 1008
#define OPTION_SCRIPTDIR 1009
static const struct option lo_always[] = {
{"help", no_argument, NULL, 'h'},
@ -111,6 +113,7 @@ static const struct option lo_always[] = {
{"pathspace", required_argument, NULL, 'N'},
{"vty_socket", required_argument, NULL, OPTION_VTYSOCK},
{"moduledir", required_argument, NULL, OPTION_MODULEDIR},
{"scriptdir", required_argument, NULL, OPTION_SCRIPTDIR},
{"log", required_argument, NULL, OPTION_LOG},
{"log-level", required_argument, NULL, OPTION_LOGLEVEL},
{"tcli", no_argument, NULL, OPTION_TCLI},
@ -127,6 +130,7 @@ static const struct optspec os_always = {
" -N, --pathspace Insert prefix into config & socket paths\n"
" --vty_socket Override vty socket path\n"
" --moduledir Override modules directory\n"
" --scriptdir Override scripts directory\n"
" --log Set Logging to stdout, syslog, or file:<name>\n"
" --log-level Set Logging Level to use, debug, info, warn, etc\n"
" --tcli Use transaction-based CLI\n"
@ -534,6 +538,14 @@ static int frr_opt(int opt)
}
di->module_path = optarg;
break;
case OPTION_SCRIPTDIR:
if (di->script_path) {
fprintf(stderr, "--scriptdir option specified more than once!\n");
errors++;
break;
}
di->script_path = optarg;
break;
case OPTION_TCLI:
di->cli_mode = FRR_CLI_TRANSACTIONAL;
break;
@ -719,7 +731,7 @@ struct thread_master *frr_init(void)
frr_pthread_init();
#ifdef HAVE_SCRIPTING
frrscript_init();
frrscript_init(di->script_path ? di->script_path : frr_scriptdir);
#endif
log_ref_init();

View file

@ -81,6 +81,7 @@ struct frr_daemon_info {
#endif
const char *vty_path;
const char *module_path;
const char *script_path;
const char *pathspace;
bool zpathspace;
@ -162,6 +163,7 @@ extern char frr_zclientpath[256];
extern const char frr_sysconfdir[];
extern char frr_vtydir[256];
extern const char frr_moduledir[];
extern const char frr_scriptdir[];
extern char frr_protoname[];
extern char frr_protonameinst[];