是否有任何好的厨师/红宝石aws-sdk ELB操作的例子

时间:2014-11-12 21:42:46

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

我正在写一本厨师食谱,我需要将实例附加到现有的ELB上。我已经查看过" aws" cookbook,但它使用right_aws gem,所有其他LWRP都是针对aws-sdk编写的。我可以找到很多关于EC2和S3使用的aws-sdk gem的例子,但是我找不到任何ELB的现有配方。

有没有人知道,或者您能否通过厨师提供有关在VPC中使用ELB的指导?

1 个答案:

答案 0 :(得分:0)

下载并浏览aws-sdk-ruby source后,刷新ruby本身,然后找到this example,我能够在coobkook'中构建以下ruby类。 s库文件夹:

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暴露给cookbook,检索访问密钥,并依赖于正在安装的aws-sdk gem。以上不会起作用,但应该给出一个良好的起点。