Class: Maws

Inherits:
Object show all
Defined in:
lib/maws.rb,
lib/maws/maws.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, command) ⇒ Maws

Returns a new instance of Maws.



9
10
11
12
13
14
15
16
17
# File 'lib/maws/maws.rb', line 9

def initialize(config, command)
  @config = config
  @command = command

  @connection = Connection.new(@config)

  @instances = InstanceCollection.new
  @specification = Specification.new(@config, @config.command_line.selection || "")
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



7
8
9
# File 'lib/maws/maws.rb', line 7

def command
  @command
end

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/maws/maws.rb', line 7

def config
  @config
end

#connectionObject (readonly)

Returns the value of attribute connection.



7
8
9
# File 'lib/maws/maws.rb', line 7

def connection
  @connection
end

#descriptionsObject (readonly)

Returns the value of attribute descriptions.



7
8
9
# File 'lib/maws/maws.rb', line 7

def descriptions
  @descriptions
end

#instancesObject (readonly)

Returns the value of attribute instances.



7
8
9
# File 'lib/maws/maws.rb', line 7

def instances
  @instances
end

Class Method Details

.load_and_run!Object



19
20
21
22
23
24
# File 'lib/maws.rb', line 19

def self.load_and_run!
  base_path = Dir.pwd
  cc_path = 'maws.yml'

  Loader.new(base_path, cc_path).load_and_run
end

Instance Method Details

#build_instancesObject



63
64
65
66
67
68
# File 'lib/maws/maws.rb', line 63

def build_instances
  @descriptions = @connection.descriptions(services)

  create_from_descriptions
  create_from_specification
end

#connect(services = nil) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/maws/maws.rb', line 47

def connect(services = nil)
  services ||= @specification.services
  @connection.connect(services)

  @config.available_zones = @connection.available_zones
  @config.specified_zones = @specification.zones
end

#create_from_descriptionsObject



70
71
72
73
74
75
76
# File 'lib/maws/maws.rb', line 70

def create_from_descriptions
  @descriptions.map { |description|
    instance = description.create_instance(self, @config)
    @instances.add(instance)
    instance.groups << "aws"
  }
end

#create_from_specificationObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/maws/maws.rb', line 78

def create_from_specification
  @specification.existing_instances = @instances.matching(:groups => 'aws')

  prefix = @config.prefix
  @specification.role_indexes.each {|role_name, indexes|
    @specification.zones_for_role(role_name).each {|zone|
      indexes.each do |index|
        instance = find_or_create_specified(prefix, zone, role_name, index)
        instance.groups << "specified"
      end
    }
  }
end

#find_or_create_specified(prefix, zone, role_name, index) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/maws/maws.rb', line 92

def find_or_create_specified(prefix, zone, role_name, index)
  found = @instances.matching({:zone => zone, :role => role_name, :index => index, :prefix => prefix})
  if !found.empty?
    found.first
  else
    instance = Instance.create(self, @config, prefix, zone, role_name, index)
    @instances.add(instance)
    instance
  end
end

#resync_instancesObject



55
56
57
58
59
60
61
# File 'lib/maws/maws.rb', line 55

def resync_instances
  @descriptions = @connection.descriptions(services)
  @descriptions.each {|description|
    instance = @instances.matching(:aws_id => description.aws_id).first
    instance.description = description if instance
  }
end

#run!Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/maws/maws.rb', line 31

def run!
  connect
  info "\n"
  info "REGION: #{@config.region}"
  info "AVAILABLE ZONES: #{@connection.available_zones.join(', ')}"
  build_instances

  info "\n"
  info "QUERYING SERVICES: #{instances.services.join(', ')}"
  info "TOTAL #{@config.profile.name.upcase} ON AWS: #{instances.aws.count}  "
  info "TOTAL SELECTED: #{instances.specified.count}"
  info "TOTAL SELECTED ON AWS: #{instances.specified.aws.count}"

  @command.run!
end

#servicesObject



19
20
21
# File 'lib/maws/maws.rb', line 19

def services
  @specification.services
end

#specified_rolesObject



23
24
25
# File 'lib/maws/maws.rb', line 23

def specified_roles
  @specification.roles
end

#specified_zonesObject



27
28
29
# File 'lib/maws/maws.rb', line 27

def specified_zones
  @specification.zones
end