【问题标题】:Are there any good chef/ruby aws-sdk examples for ELB manipulation是否有任何用于 ELB 操作的优秀厨师/ruby aws-sdk 示例
【发布时间】:2014-11-19 03:59:34
【问题描述】:

我正在编写一本厨师食谱,我需要将实例附加和分离到现有的 ELB。我查看了“aws”食谱,但它使用了 right_aws gem,我们所有的其他 LWRP 都是针对 aws-sdk 编写的。我可以找到大量用于 EC2 和 S3 的 aws-sdk gem 示例,但我找不到任何现有的 ELB 配方。

有人知道吗,或者您能否提供一些关于通过 Chef 在 VPC 中使用 ELB 的指导?

【问题讨论】:

    标签: ruby amazon-web-services chef-infra amazon-elb aws-sdk


    【解决方案1】:

    在下载并浏览了 aws-sdk-ruby source,重新了解 ruby​​ 并找到 this example 后,我能够在 coobkook 的库文件夹中构建以下 ruby​​ 类:

    elb.rb:

    class Chef
        class Resource
            class AmazonElb < Chef::Resource::Amazon
                def initialize(name, run_context=nil)
                    super(name, run_context)
                    @resource_name      = :amazon_elb
                    @provider           = Chef::Provider::AmazonElb
                    @action             = :attach
                    @allowed_actions    = [:attach, :detach]
                end
            end
    
            def instance_id (arg=nil)
                set_or_return(
                    :instance_id,
                    arg,
                    :kind_of            => [String, NilClass]
                )
            end
    
            def elb_name (arg=nil)
                set_or_return(
                    :elb_name,
                    arg,
                    :kind_of            => [String, NilClass]
                )
            end
        end
    end
    

    elb_provider.rb:

    class Chef
        class Provider
            class AmazonElb < Chef::Provider::Amazon
                def initialize(new_resource, run_context)
                    #super(new_resource, runcontext)
                    super
                    @ec2 = AWS::EC2.new
                    @elb = AWS::ELB.new
                end
    
                def load_current_resource
                    @current_resource = @new_resource
                    @current_resource
                end
    
                def define_resource_requirements
                    requirements.assert(:attach, :detach) do |a|
                        a.assertion {
                            @new_resource.instance_id != nil &&
                            @new_resource.elb_name != nil
                        }
                        a.failure_message(Chef::Exceptions::ELBException, "The attributes instance_id and elb_name must be specified.")
                    end
                end
    
                def action_attach
                    new_resource = @new_resource
                    instance = @ec2.instances[new_resource.instance_id]
                    @elb.load_balancers["#{new_resource.elb_name}"].instances.register(instance)
                end
    
                def action_detach
                    new_resource = @new_resource
                    instance = @ec2.instances[new_resource.instance_id]
                    @elb.load_balancers["#{new_resource.elb_name}"].instances.deregister(instance)
                end
    
            end
        end
    end
    

    我现在可以这样称呼:

    amazon_elb "something" do
        instance_id "i-xxx"
        elb_name "name of elb"
        aws_access_key xxx
        aws_secret_key xxx
    end
    

    注意:这依赖于创建的另一组类,以便将 aws-sdk 公开给说明书、检索访问密钥,并依赖于正在安装的 aws-sdk gem。以上内容不会按原样工作,但应该提供一个很好的起点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-19
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 2017-11-18
      • 1970-01-01
      • 2016-01-02
      相关资源
      最近更新 更多