【问题标题】:Chef - Source a Script厨师 - 源脚本
【发布时间】:2021-08-21 18:50:01
【问题描述】:

我在使用 rbenv 和 Chef 安装 Ruby 2.1 时遇到问题。我想我会使用RVM。在安装 RVM 时,如果我们要立即使用它,我们需要获取一个脚本。

source /path/to/rvm/script

你会如何在 Chef 中做到这一点?

我试过但没有成功。我相信这是一个采购问题,因为食谱第一次坏了,但第二次运行良好。这应该是因为 RVM 脚本是在 .bash_profile 中获取的,并且当登录时它获取脚本并且 RVM 变得可用。

bash "Source RVM Script: source /etc/profile.d/rvm.sh" do
  code "source /etc/profile.d/rvm.sh"
end
==> default: * bash[source /etc/profile.d/rvm.sh] action run
==> default: [2021-06-03T15:26:00+00:00] INFO: bash[source /etc/profile.d/rvm.sh] ran successfully
==> default:
==> default:     - execute "bash"
==> default:   * bash[Install Ruby 2.1.10] action run
==> default:     [execute]
==> default: bash: line 1: rvm: command not found
==> default:
==> default:
==> default: ================================================================================
==> default:
==> default: Error executing action `run` on resource 'bash[Install Ruby 2.1.10]'
==> default:
==> default: ================================================================================
==> default:
==> default:
==> default: Mixlib::ShellOut::ShellCommandFailed
==> default:
==> default: ------------------------------------
==> default:
==> default: Expected process to exit with [0], but received '127'
==> default:
==> default: ---- Begin output of "bash"  ----
==> default:
==> default: STDOUT:
==> default:
==> default: STDERR: bash: line 1: rvm: command not found

详情

我写的简单 RVM 配方:

# sudo apt-get install software-properties-common
apt_package "software-properties-common"


# sudo apt-add-repository -y ppa:rael-gc/rvm
# sudo apt-get update
apt_repository 'rvm' do
  uri 'ppa:rael-gc/rvm'
  action :add
end

# sudo apt-get install rvm
apt_package "rvm"

# sudo usermod -a -G rvm $USER
group "rvm" do
  append true
  members "vagrant"
end

bash "source /etc/profile.d/rvm.sh" do
  code "source /etc/profile.d/rvm.sh"
end

bash "Install Ruby 2.1.0" do
  code "rvm install 2.1.0"
end

平台:

  • 主机:macOS Catalina
  • 来宾:Ubuntu 20.04
  • Chef Solo + Vagrant + VirtualBox(本地开发)
# -*- mode: ruby -*-
# vi: set ft=ruby :

cookbook_path = "/Volumes/Dev/Work/GF/Projects/gf-cookbook"

Vagrant.configure("2") do |config|
  config.vm.provider "virtualbox" do |vbox|
    vbox.memory = 1024 * 4
    vbox.cpus = 2
  end

  config.vm.provision "chef_solo" do |chef|
    # chef.install = false
    chef.custom_config_path = "#{cookbook_path}/config.chef"
    chef.cookbooks_path = ["#{cookbook_path}/berks-cookbooks"]
    chef.nodes_path = "#{cookbook_path}/nodes"
    chef.add_recipe "gf"
  end
end

【问题讨论】:

  • chef-client 的运行情况如何? rvm 安装适用于特定用户。
  • @seshadri_c 抱歉,我是 Chef 的新手。我更新了答案。我正在使用 Chef Solo 来配置 Vagrant VM。我应该将它安装在 Vagrant 用户上。它安装得很好。问题是w.r.t。采购脚本;否则,在安装后,rvm 命令立即不可用。目前,如果我第二次运行vagrant provision,它就可以正常运行,可能是因为它的工作方式类似于注销/登录。

标签: chef-infra cookbook


【解决方案1】:

收敛期间的 shell 命令(主厨客户端运行)在子 shell 中运行。所以当你执行以下操作时

bash "source /etc/profile.d/rvm.sh" do
  code "source /etc/profile.d/rvm.sh"
end

它只在当前子shell中生效。这就是为什么前面的bash 资源没有选择在source /etc/profile.d/rvm.sh 期间设置的任何内容。

你可以通过在同一个 shell 上运行这两个命令来解决这个问题

bash "rvm" do
  code "source /etc/profile.d/rvm.sh && rvm install 2.1.0"
end

或者通过利用flag 属性来执行这样的登录

bash "source /etc/profile.d/rvm.sh" do
  code "source /etc/profile.d/rvm.sh"
end

bash "Install Ruby 2.1.0" do
  code "rvm install 2.1.0"
  flags "-l"
end

您提交的内容在第二次运行 chef-client 时对您有效,因为它在第一次(失败)运行中配置后读取用户配置文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    相关资源
    最近更新 更多