Updating $PATH with `magic_shell` Cookbook

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 19:52:51

问题


I created 2 cookbooks: java_kevin and maven_kevin:

cookbooks/java_kevin/recipes/default.rb

node.default['user'] = 'vagrant' 
node.default['user_home'] = '/home/' + node['user'] 
node.default['my_dir'] = node['user_home'] + '/my'

node.default['vagrant'] = '/vagrant'

node.default['java_home'] = node['my_dir'] + '/jdk1.7.0_51'

# Install Java 7 RPM
execute "install_oracle_java7" do
    command "rpm -ivh --prefix=" + node['my_dir'] + " jdk-7u51-linux-x64.rpm"
    cwd node['vagrant']
    creates node['java_home']
end

magic_shell_environment 'JAVA_HOME' do 
    value node['java_home']
end 

magic_shell_environment 'PATH' do 
    value "$PATH:" + node['java_home'] + '/bin'
end 

cookbooks/maven_kmm/recipes/default.rb

node.default['user'] = 'vagrant' 
node.default['user_home'] = '/home/' + node['user'] 
node.default['my_dir'] = node['user_home'] + '/my'

node.default['maven_download_dir'] = node.default['user_home'] + '/apache-maven-3.2.1-bin.tar.gz'

# Download Maven 3.2.1
remote_file node['maven_download_dir'] do
    source "http://www.bizdirusa.com/mirrors/apache/maven/maven-3/3.2.1/binaries/apache-maven-3.2.1-bin.tar.gz"
    checksum "aaef971206104e04e21a3b580d9634fe"
end

execute "unpack_and_uncompress_maven" do
    command "tar -zxvf " + node['maven_download_dir']
    cwd node['user_home']
    creates node['user_home'] + '/apache-maven-3.2.1'
end

execute "move_maven_to_my_dir" do
    command "mv " + node['user_home'] + '/apache-maven-3.2.1' + " " + node['my_dir']
    cwd node['user_home']
    creates node['my_dir'] + '/apache-maven-3.2.1'
end

node.default['maven_home'] = node['my_dir'] + '/apache-maven-3.2.1'

magic_shell_environment 'M2_HOME' do 
    value node['maven_home']
end 

magic_shell_environment 'PATH' do 
    value "$PATH:" + node['maven_home'] + '/bin'
end 

After provisioning, I only see that the Maven path was properly added to the $PATH:

[vagrant@vagrant-centos65 ~]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/vagrant/my/apache-maven-3.2.1/bin:/home/vagrant/bin

[vagrant@vagrant-centos65 ~]$ which mvn
~/rm/apache-maven-3.2.1/bin/mvn

[vagrant@vagrant-centos65 ~]$ which java
/usr/bin/which: no java in (/usr/local/bin:/bin:/usr/bin:....

Why was JAVA_HOME not part of the final $PATH item?


回答1:


I believe that magic_shell_environment creates a unique file for each resource name (here, 'PATH' -- Check /etc/profile.d/PATH.sh) and that each time the file will be recreated. Here both of your recipes are attempting to create the same file. And most likely the PATH.sh file created by the first recipe is getting overwritten by the second recipe.

I'm not sure how to fix this using magic_shell, but I believe that is the answer to your question "why" .

see https://github.com/customink-webops/magic_shell/blob/master/providers/environment.rb



来源:https://stackoverflow.com/questions/22618960/updating-path-with-magic-shell-cookbook

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!