[master] 8f8d26c0c Add VARNISH_DEFAULT_N environment variable as -n argument default

Nils Goroll nils.goroll at uplex.de
Fri May 24 10:54:08 UTC 2024


commit 8f8d26c0c1fec91aec9a7f79380c93eb4d266b7e
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Fri May 24 12:51:17 2024 +0200

    Add VARNISH_DEFAULT_N environment variable as -n argument default

diff --git a/bin/varnishadm/varnishadm.c b/bin/varnishadm/varnishadm.c
index 3f84afcc2..f855d2124 100644
--- a/bin/varnishadm/varnishadm.c
+++ b/bin/varnishadm/varnishadm.c
@@ -467,6 +467,7 @@ main(int argc, char * const *argv)
 		printf(OPTARG "\n");
 		exit(0);
 	}
+	n_arg = getenv("VARNISH_DEFAULT_N");
 	/*
 	 * By default linux::getopt(3) mangles the argv order, such that
 	 *	varnishadm -n bla param.set foo -bar
diff --git a/bin/varnishd/mgt/mgt_main.c b/bin/varnishd/mgt/mgt_main.c
index 6df16b285..0db5c6559 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -614,7 +614,7 @@ main(int argc, char * const *argv)
 	const char *i_arg = NULL;
 	const char *j_arg = NULL;
 	const char *h_arg = "critbit";
-	const char *n_arg = NULL;
+	const char *n_arg = getenv("VARNISH_DEFAULT_N");
 	const char *S_arg = NULL;
 	const char *s_arg = "default,100m";
 	const char *W_arg = NULL;
diff --git a/bin/varnishtest/tests/b00041.vtc b/bin/varnishtest/tests/b00041.vtc
index b829efc17..8f3107424 100644
--- a/bin/varnishtest/tests/b00041.vtc
+++ b/bin/varnishtest/tests/b00041.vtc
@@ -93,3 +93,5 @@ shell -expect {Tested} \
 	{varnishadm -T ${s1_sock} -S ${v1_name}/_.secret test}
 
 shell "varnishadm -n ${v1_name} help > /dev/null"
+
+shell {VARNISH_DEFAULT_N="${v1_name}" varnishadm help > /dev/null}
diff --git a/bin/varnishtest/tests/b00045.vtc b/bin/varnishtest/tests/b00045.vtc
index c687f2d49..ec8c5cb51 100644
--- a/bin/varnishtest/tests/b00045.vtc
+++ b/bin/varnishtest/tests/b00045.vtc
@@ -25,3 +25,7 @@ shell -err -expect {Error: Varnishd is already running} {
 shell -err -expect {Error: Varnishd is already running} {
 	varnishd -b None -a:0 -n ${tmpdir}/v1 -F
 }
+
+shell -err -expect {Error: Varnishd is already running} {
+	VARNISH_DEFAULT_N="${tmpdir}/v1" varnishd -b None -a:0 -F
+}
diff --git a/bin/varnishtest/tests/u00003.vtc b/bin/varnishtest/tests/u00003.vtc
index 2f6cc7569..343c77e0f 100644
--- a/bin/varnishtest/tests/u00003.vtc
+++ b/bin/varnishtest/tests/u00003.vtc
@@ -118,6 +118,10 @@ shell -err -match "Usage: .*varnishncsa <options>" \
 shell -match {^${localhost} 100
 ${localhost} 0$} \
 	{varnishncsa -n ${v1_name} -b -d -F "%{Host}i %b"}
+# -b with VARNISH_DEFAULT_N
+shell -match {^${localhost} 100
+${localhost} 0$} \
+	{VARNISH_DEFAULT_N="${v1_name}" varnishncsa -b -d -F "%{Host}i %b"}
 # -c
 shell -match {^${localhost} 100
 ${localhost} \d+
diff --git a/bin/varnishtest/tests/u00004.vtc b/bin/varnishtest/tests/u00004.vtc
index a861ae57a..eed2cfa6b 100644
--- a/bin/varnishtest/tests/u00004.vtc
+++ b/bin/varnishtest/tests/u00004.vtc
@@ -13,6 +13,7 @@ client c1 {
 } -run
 
 shell -expect "fetch" "varnishtop -n ${v1_name} -1"
+shell -expect "fetch" {VARNISH_DEFAULT_N="${v1_name}" varnishtop  -1}
 
 process p1 "varnishtop -n ${v1_name} -d" -start
 delay 1
diff --git a/bin/varnishtest/tests/u00005.vtc b/bin/varnishtest/tests/u00005.vtc
index 7e22b22e3..6f85abb90 100644
--- a/bin/varnishtest/tests/u00005.vtc
+++ b/bin/varnishtest/tests/u00005.vtc
@@ -51,8 +51,12 @@ shell -err -expect "-t: Invalid argument: foo" \
 	"varnishstat -t foo"
 shell -err -expect "Could not get hold of varnishd" \
 	"varnishstat -n /nonexistent -t 1"
+shell -err -expect "Could not get hold of varnishd" \
+	{VARNISH_DEFAULT_N="/nonexistent" varnishstat -t 1}
 shell -expect "MAIN.uptime" \
 	"varnishstat -n ${v1_name} -1"
+shell -expect "MAIN.uptime" \
+	{VARNISH_DEFAULT_N="${v1_name}" varnishstat -1}
 shell -expect "<name>MAIN.uptime</name>" \
 	"varnishstat -n ${v1_name} -x"
 shell -match {"MAIN.uptime":} \
diff --git a/bin/varnishtest/tests/u00006.vtc b/bin/varnishtest/tests/u00006.vtc
index 7c4fcf706..3282ebabb 100644
--- a/bin/varnishtest/tests/u00006.vtc
+++ b/bin/varnishtest/tests/u00006.vtc
@@ -131,6 +131,9 @@ process p1 -stop
 shell -match "0 CLI[ ]+- Wr 200 [0-9]+ PONG" \
 	{varnishlog -n ${v1_name} -d -g raw -X "Wr 200 [0-9]+ [^P]"}
 
+shell -match "0 CLI[ ]+- Wr 200 [0-9]+ PONG" \
+	{VARNISH_DEFAULT_N="${v1_name}" varnishlog -d -g raw -X "Wr 200 [0-9]+ [^P]"}
+
 client c1 {
 	txreq -url /foo
 	rxresp
diff --git a/bin/varnishtest/tests/u00009.vtc b/bin/varnishtest/tests/u00009.vtc
index 05c14b046..3abdcdc28 100644
--- a/bin/varnishtest/tests/u00009.vtc
+++ b/bin/varnishtest/tests/u00009.vtc
@@ -9,7 +9,7 @@ varnish v1 -vcl+backend {} -start
 
 process p1 -dump {varnishhist -n ${v1_name}} -start
 process p2 -dump {varnishhist -n ${v1_name} -P b:BereqAcct::5:-1:1} -start
-process p3 -dump {varnishhist -n ${v1_name} -P BerespBodytime -B 2} -start
+process p3 -dump {VARNISH_DEFAULT_N="${v1_name}" varnishhist -P BerespBodytime -B 2} -start
 
 process p1 -expect-text 24 0 {1e2}
 process p2 -expect-text 24 0 {1e-1}
diff --git a/doc/changes.rst b/doc/changes.rst
index e41815083..dbe31e3ab 100644
--- a/doc/changes.rst
+++ b/doc/changes.rst
@@ -41,6 +41,11 @@ Varnish Cache NEXT (2024-09-15)
 .. PLEASE keep this roughly in commit order as shown by git-log / tig
    (new to old)
 
+* The environment variable ``VARNISH_DEFAULT_N`` now provides the
+  default "varnish name" / "workdir" as otherwise specified by he
+  ``-n`` argument to ``varnishd`` and ``varnish*`` utilities except
+  ``varnishtest``.
+
 ================================
 Varnish Cache 7.5.0 (2024-03-18)
 ================================
diff --git a/lib/libvarnish/vin.c b/lib/libvarnish/vin.c
index 9e1028f34..37fe8a568 100644
--- a/lib/libvarnish/vin.c
+++ b/lib/libvarnish/vin.c
@@ -71,9 +71,12 @@ VIN_n_Arg(const char *n_arg)
 void
 VIN_DumpDefaults(void)
 {
+	printf("  The default is taken from the ``VARNISH_DEFAULT_N`` "
+	       "environment variable.\n\n");
 	printf("  Relative paths will be appended to ``%s``.\n\n",
 	       VARNISH_STATE_DIR);
-	printf("  The default value is ``%s``.\n\n",
+	printf("  If neither ``VARNISH_DEFAULT_N`` nor ``-n`` are "
+	       "present, the value is ``%s``.\n\n",
 	       VARNISH_STATE_DIR "/" VARNISH_DEFAULT_REL_NAME);
 	printf("  Note: These defaults may be distribution specific.\n\n");
 }
diff --git a/lib/libvarnishapi/vsm.c b/lib/libvarnishapi/vsm.c
index 6ada5dfc0..7da5aa171 100644
--- a/lib/libvarnishapi/vsm.c
+++ b/lib/libvarnishapi/vsm.c
@@ -346,6 +346,8 @@ VSM_New(void)
 	ALLOC_OBJ(vd, VSM_MAGIC);
 	AN(vd);
 
+	REPLACE(vd->wdname, getenv("VARNISH_DEFAULT_N"));
+
 	vd->mgt = vsm_newset(VSM_MGT_DIRNAME);
 	vd->mgt->flag_running = VSM_MGT_RUNNING;
 	vd->mgt->flag_changed = VSM_MGT_CHANGED;
diff --git a/lib/libvarnishapi/vut.c b/lib/libvarnishapi/vut.c
index 42839da0c..315aee7a5 100644
--- a/lib/libvarnishapi/vut.c
+++ b/lib/libvarnishapi/vut.c
@@ -263,6 +263,7 @@ VUT_Init(const char *progname, int argc, char * const *argv,
 	AZ(vut->vsl);
 	vut->vsl = VSL_New();
 	AN(vut->vsl);
+	REPLACE(vut->n_arg, getenv("VARNISH_DEFAULT_N"));
 	return (vut);
 }
 


More information about the varnish-commit mailing list