Chef自定义资源操作属性无效

时间:2017-08-20 04:01:09

标签: ruby chef chef-recipe

我在我正在制作的新食谱中创建的自定义资源存在问题。当我在以下设置中收集厨房时,我收到以下错误:

error: NoMethodError: undefined method `repository_url' for PoiseApplicationGit::Resource

关于我的开发机器的信息:

  • Chef Development Kit版本:2.1.11
  • chef-client version:13.2.20
  • 交付版本:master(73ebb72a6c42b3d2ff5370c476be800fee7e5427)
  • berks version:6.3.0
  • 厨房版:1.17.0
  • inspec版本:1.33.1

这是我在resources / deploy.rb下的自定义资源:

resource_name :opsworks_deploy

property :app_path, String, name_property: true
property :app_name, String, required: true
property :repository_url, String, required: true
property :repository_key, String, required: true
property :short_name, String, required: true
property :app_type, String, required: true
property :app, Object, required: true
property :permission, String, required: true

action :deploy do    
  apt_update 'update'

  # Install NGinx
  package 'nginx' do
    not_if { ::File.exist?("/etc/init.d/nginx") }
  end

  # Setup the app directory
  directory my_app_path do
    owner 'www-data'
    group 'www-data'
    mode '0755'
    recursive true
  end

  slack_notify "notify_deployment_end" do
    message "App #{app_name} deployed successfully"
    action :nothing
  end

  slack_notify "notify_nginx_reload" do
    message "Nginx has reloaded"
    action :nothing
  end

  slack_notify "notify_nginx_config" do
    message "Nginx site config has been updated for #{app_name}"
    action :nothing
  end

  slack_notify "notify_git_deploy" do
    message "App #{app_name} has been checkout out from git"
    action :nothing
  end

  slack_notify "notify_file_permissions" do
    message "App #{app_name} has been given proper file permissions"
    action :nothing
  end

  # Deploy git repo from opsworks app
  application app_path do
    owner 'www-data'
    group 'www-data'

    git do
      user 'root'
      group 'root'
      repository my_repo_url
      deploy_key my_repo_key
      notifies :notify, "slack_notify[notify_git_deploy]", :immediately
    end

    execute "chown-data-www" do
      command "chown -R www-data:www-data #{my_app_path}"
      user "root"
      action :run
      notifies :notify, "slack_notify[notify_file_permissions]", :immediately
    end

    # Setup the nginx config file for the site
    template "/etc/nginx/sites-enabled/#{my_short_name}" do
      source "#{my_app_type}.erb"
      owner "root"
      group "root"
      mode 0644
      variables( :app => my_app )
      notifies :notify, "slack_notify[notify_nginx_config]", :immediately
    end

    # Reload nginx
    service "nginx" do
      supports :status => true, :restart => true, :reload => true, :stop => true, :start => true
      action :reload
      notifies :notify, "slack_notify[notify_nginx_reload]", :immediately
    end
  end
end

这是我在食谱/ default.rb下的配方(所有变量都设置正确并且设置正确,我正在使用测试数据包并正确传递):

command = search(:aws_opsworks_command).first
deploy_app = command[:args][:app_ids].first
app = search(:aws_opsworks_app, "app_id:#{deploy_app}").first
app_path = "/var/www/" + app[:shortname]

opsworks_deploy app_path do
  app_name app[:name]
  app_type app[:environment][:APP_TYPE]
  repository_url app[:app_source][:url]
  repository_key app[:app_source][:ssh_key]
  short_name app[:shortname]
  app app
  permission '0755'
end

我能让它工作的唯一方法是,如果我在动作声明的顶部添加它并更新其中的属性以匹配:

action :deploy do
    my_repo_url = repository_url.dup
    my_repo_key = repository_key.dup
    my_app_path = app_path.dup
    my_app_type = app_type.dup
    my_short_name = short_name.dup
    my_app = app.dup
    ...

出现这种情况的原因是什么?我是否应该像在行动中那样重新宣布他们?

1 个答案:

答案 0 :(得分:0)

使用new_resource.repository_url等。魔术别名在很多情况下都无法正常工作,我们从13.1开始正式弃用它(虽然暂时不推荐)。