Sha256: f5d22e9d495eba78325cac05f9d75fe656be4b0f90fd0533f82cd7676301d3e4

Contents?: true

Size: 1.94 KB

Versions: 5

Compression:

Stored size: 1.94 KB

Contents

require 'gli'
require "opsicle/user_profile"
require "opsicle/manageable_layer"
require "opsicle/manageable_instance"
require "opsicle/manageable_stack"

module Opsicle
  class AddTags

    def initialize(environment)
      @client = Client.new(environment)
      @opsworks = @client.opsworks
      @ec2 = @client.ec2
      stack_id = @client.config.opsworks_config[:stack_id]
      @stack = ManageableStack.new(@client.config.opsworks_config[:stack_id], @opsworks)
      @cli = HighLine.new
    end

    def execute(options={})
      puts "Stack ID = #{@stack.id}"
      layer = select_layer
      all_instances = layer.get_cloneable_instances
      instances_to_add_tags = select_instances(all_instances)
      add_tags_to_instances(instances_to_add_tags)
    end

    def add_tags_to_instances(instances)
      instances.each { |instance| instance.add_tags({add_tags_mode: true}) }
    end

    def select_layer
      puts "\nLayers:\n"
      ops_layers = @opsworks.describe_layers({ :stack_id => @stack.id }).layers

      layers = []
      ops_layers.each do |layer|
        layers << ManageableLayer.new(layer.name, layer.layer_id, @stack, @opsworks, @ec2, @cli)
      end

      layers.each_with_index { |layer, index| puts "#{index.to_i + 1}) #{layer.name}" }
      layer_index = @cli.ask("Layer?\n", Integer) { |q| q.in = 1..layers.length.to_i } - 1
      layers[layer_index]
    end

    def select_instances(instances)
      puts "\nInstances:\n"
      instances.each_with_index { |instance, index| puts "#{index.to_i + 1}) #{instance.status} - #{instance.hostname}" }
      instance_indices_string = @cli.ask("Instances? (enter as a comma separated list)\n", String)
      instance_indices_list = instance_indices_string.split(/,\s*/)
      instance_indices_list.map! { |instance_index| instance_index.to_i - 1 }

      return_array = []
      instance_indices_list.each do |index|
        return_array << instances[index]
      end
      return_array
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
opsicle-2.11.3 lib/opsicle/commands/add_tags.rb
opsicle-2.11.1 lib/opsicle/commands/add_tags.rb
opsicle-2.11.0 lib/opsicle/commands/add_tags.rb
opsicle-2.10.1 lib/opsicle/commands/add_tags.rb
opsicle-2.10.0 lib/opsicle/commands/add_tags.rb