Sha256: 314e5f8c11beebe65c57f87b24c61423654421b1b308f717ddf246f8bb0193a5

Contents?: true

Size: 1.82 KB

Versions: 4

Compression:

Stored size: 1.82 KB

Contents

module ZZSharedLib
  class CL
    # run a command line and echo to console
    def self.do_cmd(cmd)
      puts cmd
      Kernel.system(cmd)
    end

    # same as above but returns the result code
    # 0 is success, anything else is an error code
    def self.do_cmd_result(cmd)
      do_cmd(cmd)
      $?.exitstatus
    end
  end

  class Utils
    READY = "ready".freeze
    ERROR = "error".freeze
    START = "deploying".freeze
    MAINT = "maint".freeze
    NEVER = "never".freeze
    RESTARTING = "restarting".freeze
    OK_TO_DEPLOY_STATES = [NEVER, READY, ERROR].freeze

    def initialize(amazon)
      @amazon = amazon
    end

    # mark the deploy state of all unless already marked
    # as errors so we don't overwrite that state
    def mark_deploy_state(instances, state_tag, state, keep_error_state = false)
      to_tag = []
      instances.each do |instance|
        inst_id = instance[:resource_id]
        if keep_error_state
          tags = @amazon.flat_tags_for_resource(inst_id)
          deploy_tag = tags[state_tag]
          if !deploy_tag.nil? && deploy_tag != ERROR
            to_tag << inst_id
          end
        else
          to_tag << inst_id
        end
      end
      @amazon.ec2.create_tags(to_tag, {state_tag => state })
      @amazon.flush_tags
    end

    def check_deploy_state(instances, state_tags)
      instances.each do |instance|
        inst_id = instance[:resource_id]
        tags = @amazon.flat_tags_for_resource(inst_id)
        state_tags.each do |state_tag|
          deploy_tag = tags[state_tag]
          if !deploy_tag.nil? && !OK_TO_DEPLOY_STATES.include?(deploy_tag)
            raise "One or more instances is still marked as deploying for tag key: #{state_tag}, value: #{deploy_tag}, we will not deploy again.  You can use --force to override"
          end
        end
      end
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zzsharedlib-0.0.7 lib/zzsharedlib/utilities.rb
zzsharedlib-0.0.6 lib/zzsharedlib/utilities.rb
zzsharedlib-0.0.5 lib/zzsharedlib/utilities.rb
zzsharedlib-0.0.4 lib/zzsharedlib/utilities.rb