Class: Infrastructure

Inherits:
Object
  • Object
show all
Includes:
Cluster::Logging
Defined in:
lib/cluster/infrastructure.rb

Direct Known Subclasses

Amazon

Constant Summary collapse

@@subsystem =
nil
@@cluster_name =
'cluster'
@@in_cluster =
false
@@machine_sizes =
%w(minimum basic average power super)
@@dns =
nil

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cluster::Logging

#logger

Constructor Details

#initialize(arguments = nil) ⇒ Infrastructure

Returns a new instance of Infrastructure.

[View source]

11
12
13
# File 'lib/cluster/infrastructure.rb', line 11

def initialize(arguments = nil)
  @arguments = arguments
end

Class Method Details

.cluster_nameObject

[View source]

110
111
112
# File 'lib/cluster/infrastructure.rb', line 110

def cluster_name
  @@cluster_name
end

.connect(arguments = nil) ⇒ Object

[View source]

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cluster/infrastructure.rb', line 114

def connect(arguments = nil)
  name = nil
  # removes the infrastructure argument, if given, and processes it
  new_args = arguments.map {|arg|
    case arg
    when /^-i([^=]+)$/, /^--infr[^=]+=(.+)$/
      name = $1
      nil
    when /^-n([^=]+)$/, /^--name[^=]+=(.+)$/
      @@cluster_name = $1
    else
      arg
    end
  }.compact

  name ||= 'amazon'

  begin
    require File.join('cluster', 'infrastructures', name)
  rescue LoadError => err
    STDERR.puts "Subsystem of #{name} either is not included or not available.\n\t#{err.message}\n#{err.backtrace.join("\n\t")}"
    exit 1
  end

  begin
    sub = Object.const_get name.capitalize
  rescue NameError
    STDERR.puts "Subsystem of #{name.capitalize} cannot be initialized."
    exit 1
  end

  sub.new new_args
end

.currentObject

[View source]

106
107
108
# File 'lib/cluster/infrastructure.rb', line 106

def current
  @@subsystem
end

.dnsObject

[View source]

148
149
150
151
152
153
154
# File 'lib/cluster/infrastructure.rb', line 148

def dns
  return @@dns unless @@dns.nil?
  ns = ''
  File.open('/etc/resolv.conf').each do |line| ns = $1 if line =~ /nameserver\s+([\d\.]+)/; end

  @@dns = Resolv::DNS.new(:nameserver => ns)
end

.in_cluster?Boolean

Returns:

  • (Boolean)
[View source]

156
157
158
# File 'lib/cluster/infrastructure.rb', line 156

def in_cluster?
  @@in_cluster
end

.sizesObject Also known as: machine_sizes

[View source]

101
102
103
# File 'lib/cluster/infrastructure.rb', line 101

def sizes
  @@machine_sizes
end

Instance Method Details

#configureObject

[View source]

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cluster/infrastructure.rb', line 15

def configure
  @options = OpenStruct.new
  @options.current_instance_id = nil
  @options.original_services = []

  if credentials?
    cluster = credentials['cluster']
    if cluster
      @options.current_instance_id = cluster['id']
      @options.original_services = cluster['services'].split(',')
    end
  end

  @@subsystem ||= self
end

#credentialsObject

[View source]

50
51
52
# File 'lib/cluster/infrastructure.rb', line 50

def credentials
  @credentials
end

#credentials?Boolean

Returns:

  • (Boolean)
[View source]

42
43
44
45
46
47
48
# File 'lib/cluster/infrastructure.rb', line 42

def credentials?
  return true if @credentials

  if Cluster::Configuration.credentials?
    @credentials = Cluster::Configuration.credentials
  end
end

#current_instanceObject

[View source]

35
36
37
38
39
# File 'lib/cluster/infrastructure.rb', line 35

def current_instance
  return nil unless @options.current_instance_id

  instances.detect {|ins| ins.id.eql?(@options.current_instance_id) }
end

#in_cluster?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)
[View source]

54
55
56
# File 'lib/cluster/infrastructure.rb', line 54

def in_cluster?
  raise NotImplementedError
end

#instancesObject

Raises:

  • (NotImplementedError)
[View source]

31
32
33
# File 'lib/cluster/infrastructure.rb', line 31

def instances
  raise NotImplementedError
end

#machines(*filter_groups) ⇒ Object

[View source]

66
67
68
69
70
71
72
# File 'lib/cluster/infrastructure.rb', line 66

def machines(*filter_groups)
  if filter_groups.empty?
    self.instances
  else
    self.instances.select {|i| i.groups.any? {|g| filter_groups.include? g }}
  end
end

#names(*roles) ⇒ Object

Raises:

  • (NotImplementedError)
[View source]

58
59
60
# File 'lib/cluster/infrastructure.rb', line 58

def names(*roles)
  raise NotImplementedError
end

#release_classObject

Raises:

  • (NotImplementedError)
[View source]

62
63
64
# File 'lib/cluster/infrastructure.rb', line 62

def release_class
  raise NotImplementedError
end

#services(filter_services) ⇒ Object

[View source]

74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cluster/infrastructure.rb', line 74

def services(filter_services)
  filter_services = Array(filter_services) unless filter_services.is_a? Array

  if filter_services.empty?
    puts "No services provided to return a list for #{filter_services.join(' ')}"
    exit 1
  else
    self.instances.select {|i|
      i.services.any? {|s| filter_services.include?(s) and !i.disabled_services.include?(s)}
    }
  end
end

#to_credentialsObject

[View source]

87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cluster/infrastructure.rb', line 87

def to_credentials
  opts = @options.marshal_dump.keys.inject({}) {|m, k| 
    if k.eql? :role
      m
    else
      m.merge k.to_s => @options.send(k)
    end
  }
  {'cluster_name' => self.class.cluster_name,
    self.class.to_s.downcase => opts }
end