Accessing shell environment variables in catalyst web app using Apache/FastCGI - apache

I have a catalyst web application on an Amazon EC2 Linux instance and its content is being served by Apache/FastCGI.
Whenever I start a new instance, I need to get the internal IP of the database server and assign it to an environment variable by running this command at startup:
export MYSQL_HOST=$(dig +short ec2-*-*-*-*.compute-1.amazonaws.com);
I have a perl module that should create the database connection after looking up the $MYSQL_HOST environment variable value.
My problem is fairly well documented in that FastCGI cannot directly access these shell environment variables.
I see that you can use PassEnv from Apache mod_env to access environment variables and that you can assign values to FastCGI environment variables using the Apache directive, FcgidInitialEnv (if I hard-code a value in here, I can retrieve it using my Perl module).
My Apache configuration skills are rather basic so I was wondering if someone could recommend a way to tie these together in order for my perl module to access $MYSQL_HOST.
Thanks!

If you're trying to read environment variables, have a look at $c->engine->env:
use Data::Dumper;
sub debugEnv :Local
{
my ( $self, $c ) = #_;
$c->res->headers->header("Content-type"=> 'text/plain');
my $req = $c->req;
$c->response->body('$c->engine->env is : '.Dumper($c->engine->env)
."c->req is $req\n"
.'c->config is ' .Dumper($c->config)
."\nENV is : ".Dumper(\%ENV))
}

Related

Cypress PORT is returning undefined in integration testing (visual)

How can I access the Cypress test running port in my test file? I am visual testing my application and I need to test different base URLs in a single script command. So I am planning to use cy.visit(<base_ulr>:<port>') directly in test file. But when I tried to access the PORT using env variable, porcess.env.PORT, its getting undefined. Anyone have any idea about this? Please need some help....
If your port is defined as an environment variable, this is how you access Cypress environment variables:
Cypress.env('port') or Cypress.env().port
See doc here
Usually, you will define your baseUrl and port in your configuration (cypress.json), and you can access it using:
Cypress.config('port') or Cypress.config().port

How to set Tomcat.runtime.environment.version as PROD on the web server?

I want to set Tomcat environment variable as PROD. I tried by putting
set "ENVIRONMENT=PROD"
set JAVA_OPTS="-Dtomcat.runtime.environment.version=PROD"
in catalina.bat
and tried to retrieve it with
env = System.getProperty("tomcat.runtime.environment.version");
but every time env is null! Where exactly does the variable have to be declared in catalina.bat and what's the perfect syntax to set the environment variable? Other possible ways to declare variables are also welcome!
Since you are on Windows and in production, I'm going to assume that you are using a Microsoft Windows Service for Tomcat. If that's the case, the .bat files are completely ignored when launching and stopping Tomcat. There is a service binary that reads the configuration from the Windows Registry and no disk-based scripts are used at all.
If you run the program called tomcatXw.exe (where X is your Tomcat major version number), that will run the configuration GUI. From there, you can configure everything stored in the Registry.
Go to the "System Properties" tab and add your system property -Dtomcat.runtime.environment.version=PROD to the list of properties already found in there. Restart your service and you should be able to see the new system property available to your application (actually the whole JVM, of course).

Artifactory not using specified home directory

I'm currently hosting Artifactory on Tomcat8/JDK1.8.
If I check the system info screen, I can see that files are being written in /u01/usr/share/tomcat8/.artifactory/
However, in the servlet configuration in bin/setenv.sh, I specified the Artifactory home to be somewhere else:
-DARTIFACTORY_HOME=/u01/opt/artifactory
Then, I discovered in the docs, it's supposed to be lower case:
-Dartifactory_home=/u01/opt/artifactory
I rebooted Tomcat after the changes and the path targetted is still /u01/usr/share/tomcat8/.artifactory. The folder is owned by the user running Tomcat as well.
Why isn't it using the specified home dir?
This seems a bit old but for others that struggle with it, you need to set an Environment variable ARTIFACTORY_HOME in your Tomcat startup script not a System variable via -D parameter.
Linux
set ARTIFACTORY_HOME=/pathto/your/artifactory
Windows
"set ARTIFACTORY_HOME=C:\path to your/artifactory"
Notice the quotes for Windows
System properties are set on the Java command line using the -Dpropertyname=value syntax. They can also be added at runtime using System.setProperty(String key, String value) or via the various System.getProperties().load() methods.
To get a specific system property you can use System.getProperty(String key) or System.getProperty(String key, String def).
Environment variables are set in the OS, e.g. in Linux export HOME=/Users/myusername or on Windows SET WINDIR=C:\Windows etc, and, unlike properties, may not be set at runtime.
To get a specific environment variable you can use System.getenv(String name).

Where to store globally accessible variables using Dancer

I'd like store variables in a configuration file so that I can just set them in one place and access them whenever needed.
Where is the place to put such variables in a Dancer app, and how do I access them?
The best way is to have one config.yml file with default global settings, like the following:
# appdir/config.yml
logger: 'file'
layout: 'main'
Your Question: How to access?
Ans: A Dancer application can use the 'config' keyword to easily access the settings within its config file, for instance:
get '/appname' => sub {
return "This is " . config->{appname};
};
This makes keeping your application's settings all in one place simple and easy - you shouldn't need to worry about implementing all that yourself.
You may well want to access your webapp's configuration from outside your webapp. Using Dancer, you can use the values from config.yml and some additional default values:
# bin/script1.pl
use Dancer ':script';
print "template:".config->{template}."\n"; #simple
print "log:".config->{log}."\n"; #undef
Note that config->{log} should result undef error on a default scaffold since you did not load the environment and in the default scaffold log is defined in the environment and not in config.yml. Hence undef.
If you want to load an environment you need to tell Dancer where to look for it. One way to do so, is to tell Dancer where the webapp lives. From there Dancer deducts where the config.yml file is (typically $webapp/config.yml).
# bin/script2.pl
use FindBin;
use Cwd qw/realpath/;
use Dancer ':script';
#tell the Dancer where the app lives
my $appdir=realpath( "$FindBin::Bin/..");
Dancer::Config::setting('appdir',$appdir);
Dancer::Config::load();
#getter
print "environment:".config->{environment}."\n"; #development
print "log:".config->{log}."\n"; #value from development environment
By default Dancer loads development environment (typically $webapp/environment/development.yml). If you want to load an environment other than the default, try this:
# bin/script2.pl
use Dancer ':script';
#tell the Dancer where the app lives
Dancer::Config::setting('appdir','/path/to/app/dir');
#which environment to load
config->{environment}='production';
Dancer::Config::load();
#getter
print "log:".config->{log}."\n"; #has value from production environment

weblogic clustering.. setting parameters through startup script for each server

I need to setup separate parameters for each server during the startup. so application inside the server will use these parameters. Application will be deployed as an EAR file.
What is the easiest way to do this? weblogic 10.3.2
Typically what I have done is the reverse of JoseK - have a script called 'startms#.sh' and in that script you can set/export JAVA_OPTIONS and then call startManagedWebLogic.sh. They should be propogated down to the startManagedWebLogic.sh script unless you have changed that script.
Have the startManagedWeblogic.sh command run a separate sh internally.
That should contain your specific params (one example below)
JAVA_OPTIONS="$JAVA_OPTIONS -Dweblogic.configuration.schemaValidationEnabled=false"
export JAVA_OPTIONS
And set these on each server as required