Config files and Reltool - erlang

I'm building a release with Reltool. The app needs config files to start. It reads a config file using the following function:
read_config(Filename) ->
{ok, [Config]} = file:consult(filename:join(
[filename:dirname(code:which(?MODULE)),
"..", "config", Filename])),
Config.
What's a good way to use config files so that Reltool builds a working release?

In case you need more specialized config files rebar allows you to copy files into your release, eg. into a etc folder under your app (rebar creates etc by default) using the overlay option in your reltool.config file (overlay is not a standard reltool config option):
%% reltool.config
{overlay, [{copy, "../path/foo.config", "etc/foo.config"}, ...
You can pass the config file as argument to the vm using the vm.args file:
%% vm.args
-config etc/foo.config
Your start script should pass the vm.args file as argument to the vm (again rebar generates a script that does that automatically).
The function init:get_argument allows you to read more specialized arguments to the vm, eg:
%% vm.args
-very_special_config etc/foo.config
and
case init:get_argument(very_special_config) of
{ok, Arg} -> Arg;
_ -> fail
end

You do not need to have your own config file, unless it's for very special purpose.
If your config file is different from version to version, you can have those different config to your <application>/ebin/<application>.app.
You can setup your default config variables to your <application>/ebin/<application>.app.
For more details about this, please refer to http://www.erlang.org/doc/man/app.html
Then, you are ready to use the config variables by using
application:get_env(<application_name>, <key>, <default_value>).
If not defined, you can also set with application:set_env/3.
For more, please look at this http://www.erlang.org/doc/man/application.html
Then you can also override those application variables by defining <any_name_or_system_name>.config, then use that one when you start with erl command with --config <file_name>.config. You can take a look at this for starting command options, http://www.erlang.org/doc/man/erl.html
When you start a command, you can also override the config variables by using -<application> <key> <value>.
You may also take look at this for config file syntax for your application.
http://www.erlang.org/doc/man/config.html
Once you have succesfully built an OTP application, it will seem very easy to you.

Related

Why isn't telegraf reading environmental variables?

My goal is to put my telegraf config into source control. To do so, I have a repo in my user's home directory with the appropriate config file which has already been tested and proven working.
I have added the path to the new config file in the "default" environment variables file:
/etc/default/telegraf
like this:
TELEGRAF_CONFIG_PATH="/home/ubuntu/some_repo/telegraf.conf"
... as well as other required variables such as passwords.
However, when I attempt to run
telegraf --test
It says No config file specified, and could not find one in $TELEGRAF_CONFIG_PATH etc.
Further, if I force it by
telegraf --test --config /home/ubuntu/some_repo/telegraf.conf
Then the process fails because it is missing the other required variables.
Questions:
What am I doing wrong?
Is there not also a way of specifying a config directory too (I would like to break my file down into separate input files)?
Perhaps as an alternative to all of this... is there not a way of specifying additional configuration files to be included from within the default /etc/telegraf/telegraf.conf file? (I've been unable to find any mention of this in documentation).
What am I doing wrong?
See what user:group owns /etc/default/telegraf. This file is better used when running telegraf as a service via systemd. Additionally, if you run env do you see the TELEGRAF_CONFIG_PATH variable? What about your other variables? If not, then you probably need to source the file first.
Is there not also a way of specifying a config directory too (I would like to break my file down into separate input files)?
Yes! Take a look at all the options of telegraf with telegraf --help and you will find:
--config-directory <directory> directory containing additional *.conf files
Perhaps as an alternative to all of this... is there not a way of specifying additional configuration files to be included from within the default /etc/telegraf/telegraf.conf file? (I've been unable to find any mention of this in documentation).
That is not the method I would suggest going down. Check out the config directory option above I mentioned.
Ok, after a LOT of trial and error, I figured everything out. For those facing similar issues, here is your shortcut to the answer:
Firstly, remember that when adding variables to the /etc/default/telegraf file, it must effectively be reloaded. So for example using ubuntu systemctl, that requires a restart.
You can verify that the variables have been loaded successfully using this:
$ sudo strings /proc/<pid>/environ
where <pid> is the "Main PID" from the telegraf status output
Secondly, when testing (eg telegraf --test) then (this is the part that is not necessarily intuitive and isn't documented) you will have to ALSO load the same environmental variables into the current user (eg: SET var=value) such that running
$ env
shows the same results as the previous command.
Hint: This is a good method for loading the current env file directly rather than doing it manually.

What are different among various dot nix files?

I am new to nixos, this is my understanding about configurations
Configuration files created by installer
/etc/nixos/configuration.nix :: The central point of system description used by nixos-rebuild
/etc/nixos/hardware-configuration.nix :: to be include in above configuration.nix
Configuration files for packages
<package>.nix on nixpkgs github :: configuration for each module (options are searchable on nixos package page)
These are what I do not fully understand
defatult.nix (any where in filesystem) :: for nix-shell like .bashrc
~/.nixpkgs/config.nix :: nix-env overrided configuration for each users
~/.config/<various>.nix :: ?? no idea
Am I understand it right?
Where can I find more information on these configuration files?
You don't call all of these files configuration files. E.g. the <package>.nix files are rather called derivations. What all these files share is the language in which they are written.
/etc/nixos/configuration.nix is indeed where you configure your system and ~/.nixpkgs/config.nix where you configure nix-env.
default.nix doesn't mean anything in particular except that this is the default file that it selected by the commands nix-build and nix-shell when you give them a directory as argument instead of a specific file. Note e.g. that the nixpkgs collection (on GitHub like you noticed already) contains a lot of such default.nix files.
To understand all of this better I advise you to read Nix-pills (that's a long series but it's worth it) and of course the NixOS, Nix and nixpkgs manuals.

How do you set application variables for a rebar dependency?

I created a project using rebar called "mything". I added lager as a dependency. Now how do I configure lager? I tried adding a "lager" section to "env" in mything.app.src but it doesn't seem to get those settings. I guess I don't know where env settings for dependencies are supposed to go.
You need to create config_name.config file which contains list of configurations for each appliaction you want to configure:
[{mything, [...]},
{lager, [...]}].
Then you can pass it to erl using option -config config_name. It's called system configuration and it overrides default environment properties from .app files. In releases it's usually named sys.config.
Resources for you: Configuring an Application and config.

How to specify directory for mnesia in cowboy application?

Is there ways to specify path to schema in cowboy app? Maybe it's possible to set in my_app.app.src or any config file?
The path to the mnesia directory has to be provided to erlang VM before mnesia application is started through application configuration parameters. In Mnesia tutorial, this is done with the -Application par val VM arguments syntax.
What you call a cowboy application is probably an Erlang OTP release (built by relx as per cowboy tutorial). The solutions, quickly described in Cowboy issue #595, are as follows.
The choice between solutions really depends on style as well as some constraints. Any sufficiently complex release would use a configuration file, so it would be a good choice. vm.args seems easier to deal with. Eventually, you might need to alter the start script (for example to run several nodes from a single deployment), and include some logic to define the mnesia directory.
Provide relx with a configuration file (sys_config option)
To do so, add the following term to relx.config as documented.
{sys_config, "./config/sys.config"}.
sys.config actually is a standard Erlang configuration file, also documented. Specifying mnesia dir is done by adding a section for mnesia application. If no other configuration is required, the file would be:
[{mnesia, [{dir, "/path/to/dir"}]}].
Get relx to pass arguments to the vm (vm_args option)
The vm.args file is actually passed to the VM through -args_file option. This is a simple text file with arguments.
You would add the following term to relx.config as documented.
{vm_args, "./config/vm.args"}.
And put the following content in the vm.args file:
-mnesia dir foo
Write your own start script
relx actually creates a start script, which passes -config sys.config and args_file vm.args to the VM as required. You could modify this script or roll your own to actually pass the -mnesia dir argument to the VM.
in case of vm.args, planit text -mnesia dir foo is invalid, please use format:
-mnesia {dir,foo}

Compiled Application with Ruby config files (yml)

I compiled a program from source on mac os x which tells me to use the supplied example config files, but how can I run a specific config file?
Readme shows what to edit in the config but not how to load it.
More specifically this program (http://github.com/carsonmcdonald/HTTP-Live-Video-Stream-Segmenter-and-Distributor)
Looks like the following should work:
./http_streamer.rb example-configs/config-simple.yml
basically the name of the config file goes as the first argument to the script.

Resources