【问题标题】:Chef crashes when installing gem安装 gem 时 Chef 崩溃
【发布时间】:2017-10-26 16:08:08
【问题描述】:

我使用 chef 和 vagrant 作为供应商。

当我尝试按如下方式安装 gems 时,应用程序崩溃:

元数据.rb

gem 'faraday'
gem 'json'

default.rb

require 'faraday'
require 'json'

conn = Faraday.new(:url => 'http://127.0.0.1:8200')

引发此错误:

==> default: Running handlers:
==> default: [2017-10-25T09:42:43+00:00] ERROR: Running exception handlers
==> default: Running handlers complete
==> default: [2017-10-25T09:42:43+00:00] ERROR: Exception handlers complete
==> default: Chef Client failed. 0 resources updated in 19 seconds
==> default: [2017-10-25T09:42:43+00:00] INFO: Sending resource update report (run-id: 7f4593f5-1c86-409c-95fe-988f09501740)
==> default: [2017-10-25T09:42:43+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
==> default: [2017-10-25T09:42:43+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
==> default: [2017-10-25T09:42:43+00:00] ERROR: Expected process to exit with [0], but received '5'
==> default: ---- Begin output of bundle install ----
==> default: STDOUT: Don't run Bundler as root. Bundler can ask for sudo if it is needed, and
==> default: installing your bundle as root will break this application for all non-root
==> default: users on this machine.
==> default: Fetching gem metadata from https://rubygems.org/
==> default: Fetching version metadata from https://rubygems.org/
==> default: Resolving dependencies...
==> default: Installing multipart-post 2.0.0
==> default: Installing json 2.1.0 with native extensions
==> default:
==> default: Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
==> default:
==> default:     current directory: /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/json-2.1.0/ext/json/ext/generator
==> default: /opt/chef/embedded/bin/ruby -r ./siteconf20171025-3434-1qh8whd.rb extconf.rb
==> default: creating Makefile
==> default:
==> default: current directory: /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/json-2.1.0/ext/json/ext/generator
==> default: make "DESTDIR=" clean
==> default:
==> default: current directory: /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/json-2.1.0/ext/json/ext/generator
==> default: make "DESTDIR="
==> default: compiling generator.c
==> default: make: gcc: Command not found
==> default: make: *** [generator.o] Error 127
==> default:
==> default: make failed, exit code 2
==> default:
==> default: Gem files will remain installed in /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/json-2.1.0 for inspection.
==> default: Results logged to /opt/chef/embedded/lib/ruby/gems/2.3.0/extensions/x86_64-linux/2.3.0/json-2.1.0/gem_make.out
==> default: Using bundler 1.12.5
==> default: Installing faraday 0.13.1
==> default: An error occurred while installing json (2.1.0), and Bundler cannot continue.
==> default: Make sure that `gem install json -v '2.1.0'` succeeds before bundling.
==> default: STDERR:
==> default: ---- End output of bundle install ----
==> default: Ran bundle install returned 5
==> default: [2017-10-25T09:42:43+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
Chef never successfully completed! Any errors should be visible in the
output above. Please fix your recipes so that they properly complete.

gcc 似乎丢失了,但我无法安装它,因为它首先尝试安装 gems,然后执行配方,所以在我做任何事情之前它就崩溃了。

-- 编辑--

我已经添加了这些行并且正在工作:

metadata.rb: (从这里移除 gem 依赖)

depends 'hashicorp-vault', '~> 2.5.0'
depends 'build-essential', '~> 8.0.3'

default.rb:

include_recipe 'hashicorp-vault::default'
chef_gem 'faraday'
chef_gem 'json'
require 'faraday'
require 'json'

【问题讨论】:

  • make: gcc: Command not found 这意味着你没有安装 gcc。你需要安装它,你的操作系统是什么?
  • 我知道 gcc 丢失了,问题是我如何解决这种依赖关系。 Vagrant 正在配置 CentOS 7
  • 尝试运行sudo yum groupinstall 'Development Tools',这将在您的机器上安装常用的构建工具,包括gcc。不确定gcc 如何依赖于安装 Ruby gems,但很确定不是这样。
  • 我知道如何手动解决。问题是 vagrant 使用 chef 来配置机器。当厨师试图编译食谱时,它会因为未安装 gcc 而崩溃。
  • 你能编辑Vagrantfile吗?如果是这样,为什么不在 Chef 安装之前添加该命令?

标签: ruby vagrant chef-infra


【解决方案1】:

安装gcc 的Chef-y 方法是使用build-essential 食谱。

【讨论】:

    【解决方案2】:

    在主厨配置之前添加一个外壳配置器?

    例如在你的 Vagrantfile 中:

    Vagrant.configure("2") do |config|
        config.vm.provision :shell, inline: 'yum install -y gcc'
        config.vm.provision :chef, inline: '...'
    end
    

    【讨论】:

    • 这行得通,但是如果我将我的 CentOS 换成 ubuntu 呢?那是行不通的。
    • 然后您将编写一些逻辑来检测您正在运行的操作系统并执行适当的命令。这种事情很常见。
    【解决方案3】:

    Recipes 完全能够运行任意命令,因此,如果您知道如何手动执行此操作,您可以在紧要关头使用 Chef 执行此操作。话虽如此,您应该保护任何任意命令,这样它们就不会在每次运行配方时都运行,只有在需要时才运行。

    execute 'sudo yum groupinstall \'Development Tools\'' unless <your logic for 
    detecting gcc goes here>
    

    Chef 还拥有 Ohai,它甚至可以在运行开始之前收集有关运行的元数据,包括您正在运行的操作系统,因此您可以利用它使您的食谱跨平台。

    https://docs.chef.io/dsl_recipe.html

    include_recipe 'mycookbook::debian' if node['platform_family'] == 'debian'
    include_recipe 'mycookbook::rhel' if node['platform_family'] == 'rhel'
    

    或者,如果您计划拥有多种平台类型,则为您提供更大的灵活性

    possible_platforms = ['debian', 'rhel']
    if possible_platforms.contains? node['platform_family']
       include_recipe "mycookbook::node['platform_family']"
    else
        puts 'This platform isn't valid'
    end
    

    【讨论】:

      猜你喜欢
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 2015-01-31
      相关资源
      最近更新 更多