summaryrefslogtreecommitdiff
path: root/lib/ohai/plugins/cloud.rb
blob: 68852d0431f756cd37911eaf0075c3563dfe5585 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#
# Author:: Cary Penniman (<cary@rightscale.com>)
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

provides "cloud"

require_plugin "ec2"
require_plugin "gce"
require_plugin "rackspace"
require_plugin "eucalyptus"
require_plugin "linode"
require_plugin "openstack"

# Make top-level cloud hashes
#
def create_objects
  cloud Mash.new
  cloud[:public_ips] = Array.new
  cloud[:private_ips] = Array.new
end
#---------------------------------------
# Google Compute Engine (gce)
#--------------------------------------

def on_gce?
  gce != nil
end
def get_gce_values
  cloud[:public_ipv4] = []
  cloud[:local_ipv4] = []

  public_ips = gce['network']["networkInterface"].collect do |interface|
    if interface.has_key?('accessConfiguration')
      interface['accessConfiguration'].collect{|ac| ac['externalIp']}
    end
  end.flatten.compact

  private_ips = gce['network']["networkInterface"].collect do |interface|
    interface['ip']
  end.compact
  
  cloud[:public_ips] += public_ips
  cloud[:private_ips] += private_ips
  cloud[:public_ipv4] +=  public_ips
  cloud[:public_hostname] = nil
  cloud[:local_ipv4] += private_ips
  cloud[:local_hostname] = gce['hostname']
  cloud[:provider] = "gce"
end

# setup gce cloud
if on_gce?
  create_objects
  get_gce_values
end

# ----------------------------------------
# ec2
# ----------------------------------------

# Is current cloud ec2?
#
# === Return
# true:: If ec2 Hash is defined
# false:: Otherwise
def on_ec2?
  ec2 != nil
end

# Fill cloud hash with ec2 values
def get_ec2_values
  cloud[:public_ips] << ec2['public_ipv4']
  cloud[:private_ips] << ec2['local_ipv4']
  cloud[:public_ipv4] = ec2['public_ipv4']
  cloud[:public_hostname] = ec2['public_hostname']
  cloud[:local_ipv4] = ec2['local_ipv4']
  cloud[:local_hostname] = ec2['local_hostname']
  cloud[:provider] = "ec2"
end

# setup ec2 cloud
if on_ec2?
  create_objects
  get_ec2_values
end

# ----------------------------------------
# rackspace
# ----------------------------------------

# Is current cloud rackspace?
#
# === Return
# true:: If rackspace Hash is defined
# false:: Otherwise
def on_rackspace?
  rackspace != nil
end

# Fill cloud hash with rackspace values
def get_rackspace_values 
  cloud[:public_ips] << rackspace['public_ipv4'] if rackspace['public_ipv4']
  cloud[:private_ips] << rackspace['local_ipv4'] if rackspace['local_ipv4']
  cloud[:public_ipv4] = rackspace['public_ipv4']
  cloud[:public_ipv6] = rackspace['public_ipv6']
  cloud[:public_hostname] = rackspace['public_hostname']
  cloud[:local_ipv4] = rackspace['local_ipv4']
  cloud[:local_ipv6] = rackspace['local_ipv6']
  cloud[:local_hostname] = rackspace['local_hostname']
  cloud[:provider] = "rackspace"
end

# setup rackspace cloud
if on_rackspace?
  create_objects
  get_rackspace_values
end

# ----------------------------------------
# linode
# ----------------------------------------

# Is current cloud linode?
#
# === Return
# true:: If linode Hash is defined
# false:: Otherwise
def on_linode?
  linode != nil
end

# Fill cloud hash with linode values
def get_linode_values
  cloud[:public_ips] << linode['public_ip']
  cloud[:private_ips] << linode['private_ip']
  cloud[:public_ipv4] = linode['public_ipv4']
  cloud[:public_hostname] = linode['public_hostname']
  cloud[:local_ipv4] = linode['local_ipv4']
  cloud[:local_hostname] = linode['local_hostname']
  cloud[:provider] = "linode"
end

# setup linode cloud data
if on_linode?
  create_objects
  get_linode_values
end

# ----------------------------------------
# eucalyptus
# ----------------------------------------

# Is current cloud eucalyptus?
#
# === Return
# true:: If eucalyptus Hash is defined
# false:: Otherwise
def on_eucalyptus?
  eucalyptus != nil
end

def get_eucalyptus_values
  cloud[:public_ips] << eucalyptus['public_ipv4']
  cloud[:private_ips] << eucalyptus['local_ipv4']
  cloud[:public_ipv4] = eucalyptus['public_ipv4']
  cloud[:public_hostname] = eucalyptus['public_hostname']
  cloud[:local_ipv4] = eucalyptus['local_ipv4']
  cloud[:local_hostname] = eucalyptus['local_hostname']
  cloud[:provider] = "eucalyptus"
end

if on_eucalyptus?
  create_objects
  get_eucalyptus_values
end

# ----------------------------------------
# openstack
# ----------------------------------------

# Is current cloud openstack-based?
#
# === Return
# true:: If openstack Hash is defined
# false:: Otherwise
def on_openstack?
  openstack != nil
end

# Fill cloud hash with openstack values
def get_openstack_values
  cloud[:public_ips] << openstack['public_ipv4']
  cloud[:private_ips] << openstack['local_ipv4']
  cloud[:public_ipv4] = openstack['public_ipv4']
  cloud[:public_hostname] = openstack['public_hostname']
  cloud[:local_ipv4] = openstack['local_ipv4']
  cloud[:local_hostname] = openstack['local_hostname']
  cloud[:provider] = openstack['provider']
end

# setup openstack cloud
if on_openstack?
  create_objects
  get_openstack_values
end