set["tomcat"]["java_options"] = "$JAVA_OPTS -DJENKINS_HOME=#{node[:jenkins][:home]}"
On Saturday, August 20, 2011 at 10:44 AM, Jason J. W. Williams wrote:
> It's got to go into the init.d script you install/generate to start your program.
>
> -J
>
> Sent via iPhone
>
> Is your email Premiere?
>
This comes up frequently enough that it's worth going over in a bit of detail:> On Aug 20, 2011, at 11:09, Brian Jakovich < "> (mailto: "> )> wrote:
>
> > How do I go about setting environment variables in chef? I've tried several methods such as adding them to a template and then sourcing it. Adding them to attributes. I'm out of ideas...
> >
> > ## Problem
> >
> > > The JENKINS_HOME environment variable doesn't get set. It should however be set using the template "default_jenkins.erb"
> > > whenever I go into the instance and echo $JENKINS_HOME it does not return anything
> >
> > ## Current status
> >
> > > Currently it successfully takes the jenkins war from the remote location and places it into the webapps directory of Tomcat. When I go to the http://PublicDNS:8080/jenkins Jenkins is running. (It does however complain about not being able to create .jenkins directory, AKA Jenkins Home, and suggests that there is a problem with its permissions)
> >
> > ## default recipe
> >
> > > # Setting local variables
> > > jenkinsWar = node[:jenkins][:war]
> > > webapp_dir = node["tomcat"]["webapp_dir"]
> > > tomcat_user = node["tomcat"]["user"]
> > > tomcat_group = node["tomcat"]["group"]
> >
> > > # Getting the Jenkins.war and putting it into the tomcat webapps directory
> > > remote_file "/#{webapp_dir}/#{jenkinsWar}" do
> > > source "#{node[:jenkins][:url]}"
> > > mode "0644"
> > > owner "root"
> > > group "root"
> > > end
> >
> > > # Setting Jenkins configuration
> > > template "/etc/default/jenkins" do
> > > source "default_jenkins.erb"
> > > owner "root"
> > > group "root"
> > > mode "0644"
> > > notifies :restart, resources(:service => "tomcat")
> > > end
> >
> >
> > ## default_jenkins.erb
> >
> > > # Set Jenkins home variable. This will tell Jenkins where to find it's jobs/plugins/etc
> > > JENKINS_HOME=<%= node[:jenkins][:home] %>
> >
> > ## default attribute
> >
> > > default[:jenkins][:war] = "jenkins.war"
> > > default[:jenkins][:home] = "/var/lib/jenkins"
> > > default[:jenkins][:url] = "https://s3.amazonaws.com/cookbook-resources/#{node[:jenkins][:war]}"
In UNIX, new processes are created by fork/exec, and environment variables are inherited from the program that you forked from. Processes can modify their environments; in Ruby, you do this:
ENV["JAVA_HOME"] = '/path/to/java_home'
If you use code like the above in a cookbook, this environment variable will be available to any processes that Chef creates (eg, any "shell" command that it executes). However, this environment variable won't be set when you log in via ssh; that's what your shell's initialization routines (in particular, loading your profile and rc scripts) are for.
As Jason says, your best bet in this use case is to set the environment in your init script for jenkins. As long as you always start/stop/restart jenkins via the init script, it will have the correct environment.
--
Dan DeLeo
Archive powered by MHonArc 2.6.16.