Class: Sambot::Rackspace::Instances

Inherits:
Object
  • Object
show all
Defined in:
lib/sambot/rackspace/instances.rb

Instance Method Summary collapse

Instance Method Details

#all(extended = false) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sambot/rackspace/instances.rb', line 7

def all(extended = false)
  result = []
  RACKSPACE_ACCOUNTS.each do ||
    api = Client.new([:api_key], [:id])
    api.servers.all.map do |server|
      name = server.name
      ip = server.addresses['private'].last['addr']
      if extended
        result << { name: name, value: ip, id: server.id }
      else
        result << { name: name, value: ip }
      end
    end
  end
  result.flatten
end

#as_rundeck_nodesObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sambot/rackspace/instances.rb', line 47

def as_rundeck_nodes
  result = []
  images = Images.all
  flavors = Flavors.all
  RACKSPACE_ACCOUNTS.each do ||
    api = Rackspace.connect([:api_key], [:id])
    api.servers.all.map do |server|
      flavor = flavors.detect { |x| x[:value] == server.flavor_id }
      image = images.detect { |x| x[:value] == server.image_id }
      image_name = image ? image[:name] : 'unknown'
      team = Teams.find_from_instance_name(server.name)
      result << {
        name: server.name,
        team: team ? Teams.all.find { |x| x[:value] == team }[:name] : 'Night Watch',
        os: find_os(server.name),
        hostname: server.addresses['private'].last['addr'],
        created: server.created,
        state: server.state,
        role: 'none',
        flavor: flavor ? flavor[:name] : 'unknown',
        image: image_name
      }
    end
  end
  result.flatten
end

#belonging_to(team) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sambot/rackspace/instances.rb', line 24

def belonging_to(team)
  results = self.all
  team_initials = team.downcase
  results.select do |instance|
    instance_name = instance[:name].downcase
    if team_initials == 'nw'
      instance_name.start_with?(team_initials) || instance_name.start_with?('dev')
    else
      instance_name.start_with?(team_initials)
    end
  end
end

#by_name(name) ⇒ Object



74
75
76
77
78
# File 'lib/sambot/rackspace/instances.rb', line 74

def by_name(name)
  instances = all(true)
  x = instances.select { |y| y[:name].upcase == name.upcase }
  x.size == 1 ? x[0][:id] : nil
end

#find_next_index(team, role) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/sambot/rackspace/instances.rb', line 37

def find_next_index(team, role)
  raise 'No team name was provided. It should be the short form of the team i.e. NW or AVG.' unless team
  raise 'No cookbook name was provided. It needs to be a role cookbook.' unless role
  team_instances = self.all.find_all { |x| x[:name] =~ /#{team}-#{role}/i }
  return '01' if team_instances.size < 1
  similar_instances = team_instances.map { |x| x[:name].upcase.gsub("#{team}-#{role}".upcase, '').gsub(/^\-/, '') }
  current_index = similar_instances.map { |x| x.match(/(.+)-(.+)/)[1].to_i }.sort[-1] + 1
  current_index.to_s.rjust(2, '0')
end

#find_os(name) ⇒ Object



80
81
82
# File 'lib/sambot/rackspace/instances.rb', line 80

def find_os(name)
  name =~ /\-L\d+$/ ? 'Linux' : 'Windows'
end