Aman,
Like Morgan, I’d encourage using the community java cookbook to handle Java’s installation, and then wrap that cookbook to set up the .profile & /etc/profile using the attributes supplied by the java cookbook (namely, node[‘java’]['java_home’]).
Bear in mind that the EnvVars you’re setting in that ruby_block will not persist beyond the Chef run, so I’m not entirely sure why you’re setting them at all.
You might consider this gist: https://gist.github.com/jeffbyrnes/adc37c4e69d0f68b0214
–
Jeff Byrnes
@berkleebassist
Lead DevOps Engineer
EverTrue
704.516.4628
On December 17, 2014 at 2:52:28 AM, aman singh (amanscollection@gmail.com) wrote:
Hi All
I am not able to set JAVA_HOME and PATH variable through my recipe. Please refer below my recipe code for java installation.
Ruby block is accepting path /opt/app/workload/installed_apps/jdk1.7.0_71 and not including /opt/app/workload/installed_apps/jdk1.7.0_71’/bin in $PATH variable.
I have tried execute block, script resource also to run .sh file but nothing works in my case.
CODE-:
Attributes-:
default[‘java_manual’][‘JAVA_HOME’] = ‘/opt/app/workload/installed_apps/jdk1.7.0_71’
default[‘java_manual’][‘JRE_HOME’] = ‘/opt/app/workload/installed_apps/jdk1.7.0_71/jre’
Cookbook Name:: java_manual
Recipe:: default
Copyright 2014, YOUR_COMPANY_NAME
All rights reserved - Do Not Redistribute
%w[ /app /app/workload /app/workload/installed_apps ].each do |path|
directory “/opt/#{path}” do
owner 'root’
action :create
end
end
cookbook_file “jdk7.tar.gz” do
path "/opt/app/workload/installed_apps/jdk7.tar.gz"
action :create_if_missing
end
bash “untar java” do
cwd "/opt/app/workload/installed_apps"
user "root"
code <<-EOH
su -sH
tar xzf jdk7.tar.gz
rm jdk7.tar.gz
EOH
end
#execute “slapadd” do
environment ({‘JAVA_HOME’ => “#{node[‘java_manual’][‘JAVA_HOME’]}”})
command ‘export JAVA_HOME="/opt/app/workload/installed_apps/jdk1.7.0_71"’
action :run
#end
ruby_block “mydate” do
block do
ENV[“JAVA_HOME”] = "#{node[‘java_manual’][‘JAVA_HOME’]}"
print "#{node[‘java_manual’][‘JAVA_HOME’]}/bin:#{node[‘java_manual’][‘JRE_HOME’]}/bin:#{ENV[‘PATH’]}"
ENV[“PATH”] = "#{ENV[‘PATH’]}:#{node[‘java_manual’][‘JAVA_HOME’]}/bin:#{node[‘java_manual’][‘JRE_HOME’]}/bin"
end
end
Thanks
Amandeep Singh
On Tue, Dec 16, 2014 at 6:45 PM, Ranjib Dey dey.ranjib@gmail.com wrote:
If you want to run any java stuff, you can use the execute resource, which allows passing environment variables as hash (JAVA_HOME and PATH) in your case.
You can use the same resource or bash resource to read profile.sh and then execute the command as a single step using the && bash hack, but that will be ugly
On Dec 16, 2014 2:52 AM, “Amandeep” amanscollection@gmail.com wrote:
Thanks Morgan. I am writing my own cookbook currently where i am setting java home and path variable in .profile as well as /etc/profile file so that when these files are executed then automatically env variables will be set.
Regards
Amandeep Singh
Sent from my iPhone
On 16-Dec-2014, at 3:40 pm, Morgan Blackthorne stormerider@gmail.com wrote:
You don’t execute .profile. If you’re in a shell and need to reload the file you can do “, ~/.profile” but that is a shell built-in, it’s not a command. Typically commands without a TTY won’t load the .profile/etc files, unless the command is a shell script or you explicitly call the shell to run the command.
If you’re having trouble running scripts via Chef, you’ll need to paste at least the error that occurs for us to help.
Also, why not just use the existing community java cookbook (https://github.com/agileorbit-cookbooks/java)? ISTR that has settings to configure JAVA_HOME via an /etc/profile.d file.
–
~~ StormeRider ~~
“Every world needs its heroes […] They inspire us to be better than we are. And they protect from the darkness that’s just around the corner.”
(from Smallville Season 6x1: “Zod”)
On why I hate the phrase “that’s so lame”… http://bit.ly/Ps3uSS
On Tue, Dec 16, 2014 at 1:55 AM, Amandeep amanscollection@gmail.com wrote:
Hi team,
I have created a cookbook for java installation and adding java home into path environment variable and exporting the variables by executing the .profile file.
I am not able to execute .profile file as well as any sh file.
Please help in this. If my recipe code is required then i will paste it here.
Regards
Amandeep Singh