-
Notifications
You must be signed in to change notification settings - Fork 275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Environment variables for config options #217
Comments
This is not directly supported. However, if you create your own WAR overlay (highly recommended) you can use this trick to read configuration from a properties file outside the webapp. |
Thanks for the link @kris-sigur, that was a good read. But I am trying to run Wayback in containers (and I have done successful attempts) and to make it easier, I was looking for a way to pass some environment variables at the time of spinning containers instead of having different config files for every possible combination of cases I might want to run it. The trick described in that WAR overlay trick just allows me to have overrides in yet another file. I was looking for ways to do so and I found about Spring Expression Language that is supported in Spring 3 (and from the schema used in wayback.xml file, it looks like it is using Spring 3). Don't you think it should be possible to it in the XML file itself? I usually don't work on Java projects so I don't have an idea, how will it all work, but I do feel that it is possible. |
Do you think something like this will work? <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<value>
wayback.basedir=#{systemEnvironment['BASE_DIR']?:'/tmp/openwayback'}
...
</value>
</property>
</bean> The intention is to capture the value of environment variable |
Above approach seems to be working in my tests. :) |
I'm happy to have set you on the right path :) |
@kris-sigur, I understand that it will cause confusion when editing <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<value>
<!-- Customize these basic placeholders. -->
wayback.basedir=/tmp/openwayback
wayback.url.scheme=http
wayback.url.host=localhost
wayback.url.port=8080
<!-- Environment variable overrides (only if present). No need to customize these. -->
wayback.basedir=#{systemEnvironment['WAYBACK_BASEDIR']?:${wayback.basedir}}
wayback.url.scheme=#{systemEnvironment['WAYBACK_URL_SCHEME']?:${wayback.url.scheme}}
wayback.url.host=#{systemEnvironment['WAYBACK_URL_HOST']?:${wayback.url.host}}
wayback.url.port=#{systemEnvironment['WAYBACK_URL_PORT']?:${wayback.url.port}}
<!-- Derived placeholders. -->
wayback.archivedir.1=${wayback.basedir}/files1/
wayback.archivedir.2=${wayback.basedir}/files2/
wayback.url.prefix=${wayback.url.scheme}://${wayback.url.host}:${wayback.url.port}
</value>
</property>
</bean> |
This approach of setting OpenWayback paths via environment variables (Pull request #220) is extremely relevant to my interests in regard to https://github.com/machawk1/wail , which relies on these paths for all of the bundled tools to communicate and have a common data source. |
@ibnesayeed I like the revised approach. |
@kris-sigur, the sample code in the later approach has some syntax problems. The Spring expression interpreter is possibly not happy with the $ sign there or nested { } are an issue. If one can point out the proper way of doing it, I will make the changes in the relevant PR. |
@ibnesayeed, I am unfamiliar with the Spring config syntax but shouldn't the ternary operator be
If this form applies to Spring, no value is set if the condition (e.g., systemEnvironment['WAYBACK_URL_SCHEME']) is true. |
@machawk1 this is called displayName = fullName ?: userName It is equivalent to: displayName = fullName ? fullName : userName In this example display_name = full_name || user_name This is good way for setting fallback or default value. |
I was able to fix the syntax issue. Property <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<value>
<!-- Customize these basic placeholders. -->
wayback.basedir=/tmp/openwayback
wayback.url.scheme=http
wayback.url.host=localhost
wayback.url.port=8080
<!-- Environment variable overrides (only if present). No need to customize these. -->
wayback.basedir=#{ systemEnvironment['WAYBACK_BASEDIR'] ?: '${wayback.basedir}' }
wayback.url.scheme=#{ systemEnvironment['WAYBACK_URL_SCHEME'] ?: '${wayback.url.scheme}' }
wayback.url.host=#{ systemEnvironment['WAYBACK_URL_HOST'] ?: '${wayback.url.host}' }
wayback.url.port=#{ systemEnvironment['WAYBACK_URL_PORT'] ?: '${wayback.url.port}' }
<!-- Derived placeholders. -->
wayback.archivedir.1=${wayback.basedir}/files1/
wayback.archivedir.2=${wayback.basedir}/files2/
wayback.url.prefix=${wayback.url.scheme}://${wayback.url.host}:${wayback.url.port}
</value>
</property>
</bean> But this configuration throws an error:
It turns out that <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<value>
<!-- Customize these basic placeholders. -->
config.wayback.basedir=/tmp/openwayback
config.wayback.url.scheme=http
config.wayback.url.host=localhost
config.wayback.url.port=8080
<!-- Environment variable overrides (only if present). No need to customize these. -->
wayback.basedir=#{ systemEnvironment['WAYBACK_BASEDIR'] ?: '${config.wayback.basedir}' }
wayback.url.scheme=#{ systemEnvironment['WAYBACK_URL_SCHEME'] ?: '${config.wayback.url.scheme}' }
wayback.url.host=#{ systemEnvironment['WAYBACK_URL_HOST'] ?: '${config.wayback.url.host}' }
wayback.url.port=#{ systemEnvironment['WAYBACK_URL_PORT'] ?: '${config.wayback.url.port}' }
<!-- Derived placeholders. -->
wayback.archivedir.1=${wayback.basedir}/files1/
wayback.archivedir.2=${wayback.basedir}/files2/
wayback.url.prefix=${wayback.url.scheme}://${wayback.url.host}:${wayback.url.port}
</value>
</property>
</bean> Here I am changing <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<value>
<!-- Customize these basic placeholders. -->
WAYBACK_BASEDIR=/tmp/openwayback
WAYBACK_URL_SCHEME=http
WAYBACK_URL_HOST=localhost
WAYBACK_URL_PORT=8080
<!-- Environment variable overrides (only if present). No need to customize these. -->
wayback.basedir=#{ systemEnvironment['WAYBACK_BASEDIR'] ?: '${WAYBACK_BASEDIR}' }
wayback.url.scheme=#{ systemEnvironment['WAYBACK_URL_SCHEME'] ?: '${WAYBACK_URL_SCHEME}' }
wayback.url.host=#{ systemEnvironment['WAYBACK_URL_HOST'] ?: '${WAYBACK_URL_HOST}' }
wayback.url.port=#{ systemEnvironment['WAYBACK_URL_PORT'] ?: '${WAYBACK_URL_PORT}' }
<!-- Derived placeholders. -->
wayback.archivedir.1=${wayback.basedir}/files1/
wayback.archivedir.2=${wayback.basedir}/files2/
wayback.url.prefix=${wayback.url.scheme}://${wayback.url.host}:${wayback.url.port}
</value>
</property>
</bean> I personally like this last approach because it now has very clear "call for action" at the same time it uses the same names for config in the wayback.xml file as the corresponding environment variable names. Before I make changes in the related PR, I would like to know what others have to say on this? Also, we will have to make changes in the documentation to reflect these changes. |
I'd probably prefer |
👍 I, too, prefer |
I have pushed some changes 2c6ea46 to the related PR #220 to incorporate |
@ibnesayeed One issue remains with the PR, please include an update to the release notes as specified here: https://github.com/iipc/openwayback/wiki/How-to-contribute#code Other than that, I'm inclined to accept the PR. We do need to remember to update the how to configure wiki page to reflect this change. |
@kris-sigur, I have made the changes to the release notes file. I realized that Git is not recognizing |
Fixed in PR #240 |
@kris-sigur PR #220 is more complete than PR #240 and the earlier should be merged because it also takes care of customizing prefix independent of the host and port customization which is needed for load balancing scenario. Also, the #220 has naming convention more aligned with rest of the stuff as agreed by @anjackson and @machawk1 above. |
@ibnesayeed Agreed this needs another look, will do so tomorrow |
@ibnesayeed I've merged your changes by way of PR #249 |
Looks good to me @kris-sigur |
We need to remember to change "How to configure" wiki page to reflect these changes near the release. I am not changing it yet, because it is not valid for the current version. |
Is there a way to set certain config option using environment variables? There are cases when it can be handy to pass some environment variables instead of making changes to the config files such as:
.war
file, there is no way to overwrite the defaults unless the config files are modified and the and the application is reloaded by restarting Tomcat.Here is an example of some configs from the
wayback.xml
file:I understand that XML files cannot load environment variables, but when these config options are loaded in Java files, at that time respective environment variables can take precedence if present otherwise fallback to the values set in the config files. Here is a pseudo example to illustrate the usage:
I was deploying Wayback suing Docker Container and I wished this functionality was there.
The text was updated successfully, but these errors were encountered: