Sha256: e7da58ffb53f61f8d360a8207f044a8d477f8027193c4d41130263b72ae60b0e

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

module Elb
  class Deploy
    include Helper

    attr_reader :instance_id
    def initialize(options={})
      @options = options
      @instance_id = options[:instance_id] || %x[curl -s http://instance-data.ec2.internal/latest/meta-data/instance-id].strip
      setup_aws
    end

    def run
      UI.say("Restarting server started")
      deregister
      drop_inbound
      restart
      warm
      add_inbound
      register
      UI.say("Restarting server completed")
    end

    def asg
      return @asg if @asg
      name = ec2.instances[instance_id].tags["aws:autoscaling:groupName"]
      @asg = as.groups[name]
    end

    def elb
      return @elb if @elb
      @elb = asg.load_balancers.first
    end

    def deregister
      UI.say("Deregistering server from ELB")
      return true if @options[:noop]
      elb.client.deregister_instances_from_load_balancer(
        :load_balancer_name => elb.name,
        :instances => [{:instance_id => @instance_id}]
      )
      wait(@options[:wait]) # takes a while for the elb to deregister
    end

    def drop_inbound
      %x{sudo /sbin/iptables -A INPUT -j DROP -p tcp --destination-port 80 -i eth0}
    end

    def add_inbound
      %x{sudo /sbin/iptables -D INPUT -j DROP -p tcp --destination-port 80 -i eth0}
    end


    def restart
      UI.say("Restarting server")
      system(@options[:restart_command])
    end

    def warm
      UI.say("Warming up server")
      system(@options[:warm_command])
    end

    def register
      UI.say("Registering server with ELB")
      return true if @options[:noop]
      elb.client.register_instances_with_load_balancer(
        :load_balancer_name => elb.name,
        :instances => [{:instance_id => @instance_id}]
      )
    end

    def wait(n)
      sleep n
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
elb-0.0.7 lib/elb/deploy.rb
elb-0.0.6 lib/elb/deploy.rb