使用`magic_shell` Cookbook更新$ PATH

时间:2014-03-24 19:35:18

标签: chef

我创建了2本食谱:java_kevin和maven_kevin:

食谱/ java_kevin /食谱/ 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 

食谱/ maven_kmm /食谱/ 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 

配置完成后,我只看到Maven路径已正确添加到$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:....

为什么JAVA_HOME不属于最终的$PATH项?

1 个答案:

答案 0 :(得分:3)

我相信magic_shell_environment会为每个资源名称创建一个唯一的文件(此处为' PATH' - 检查/etc/profile.d/PATH.sh),并且每次都会重新创建文件。这两个配方都试图创建相同的文件。并且很可能由第一个配方创建的PATH.sh文件被第二个配方覆盖。

我不确定如何使用magic_shell解决这个问题,但我相信这是你问题的答案"为什么"

请参阅https://github.com/customink-webops/magic_shell/blob/master/providers/environment.rb