Class: Hula::CloudFoundry

Inherits:
Object
  • Object
show all
Defined in:
lib/hula/cloud_foundry.rb,
lib/hula/cloud_foundry/service_broker.rb

Defined Under Namespace

Classes: ServiceBroker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CloudFoundry

Returns a new instance of CloudFoundry.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hula/cloud_foundry.rb', line 23

def initialize(args)
  @domain = args.fetch(:domain)
  @api_url = args.fetch(:api_url)
  @logger = args.fetch(:logger, default_logger)
  @command_runner = args.fetch(:command_runner, default_command_runner)

   = args.fetch(:target_and_login, true)
  if 
    target(api_url)
    (args.fetch(:username), args.fetch(:password))
  end
end

Instance Attribute Details

#api_urlObject (readonly)

Returns the value of attribute api_url.



21
22
23
# File 'lib/hula/cloud_foundry.rb', line 21

def api_url
  @api_url
end

#current_organizationObject (readonly)

Returns the value of attribute current_organization.



21
22
23
# File 'lib/hula/cloud_foundry.rb', line 21

def current_organization
  @current_organization
end

#current_spaceObject (readonly)

Returns the value of attribute current_space.



21
22
23
# File 'lib/hula/cloud_foundry.rb', line 21

def current_space
  @current_space
end

#domainObject (readonly)

Returns the value of attribute domain.



21
22
23
# File 'lib/hula/cloud_foundry.rb', line 21

def domain
  @domain
end

Instance Method Details

#add_public_service_broker(service_name, _service_label, url, username, password) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/hula/cloud_foundry.rb', line 138

def add_public_service_broker(service_name, _service_label, url, username, password)
  cf("create-service-broker #{service_name} #{username} #{password} #{url}")

  service_plans = JSON.parse(cf('curl /v2/service_plans -X GET'))
  guids = service_plans['resources'].map do |resource|
    resource['metadata']['guid']
  end

  guids.each do |guid|
    cf(%(curl /v2/service_plans/#{guid} -X PUT -d '{"public":true}'))
  end
end

#app_env(app_name) ⇒ Object



257
258
259
# File 'lib/hula/cloud_foundry.rb', line 257

def app_env(app_name)
  cf("env #{app_name}")
end

#app_vcap_services(app_name) ⇒ Object



48
49
50
# File 'lib/hula/cloud_foundry.rb', line 48

def app_vcap_services(app_name)
  app_environment(app_name)["VCAP_SERVICES"]
end

#assert_broker_is_in_marketplace(type) ⇒ Object



156
157
158
159
160
161
# File 'lib/hula/cloud_foundry.rb', line 156

def assert_broker_is_in_marketplace(type)
  output = marketplace
  unless output.include?(type)
    fail "Broker #{type} not found in marketplace"
  end
end

#assert_instance_is_in_services_list(service_name) ⇒ Object



189
190
191
192
193
194
# File 'lib/hula/cloud_foundry.rb', line 189

def assert_instance_is_in_services_list(service_name)
  output = cf('services')
  unless output.include?(service_name)
    fail "Instance #{service_name} not found in services list"
  end
end

#auth_tokenObject



36
37
38
# File 'lib/hula/cloud_foundry.rb', line 36

def auth_token
  cf("oauth-token").lines.last.strip!
end

#bind_app_to_service(app_name, service_name) ⇒ Object



214
215
216
# File 'lib/hula/cloud_foundry.rb', line 214

def bind_app_to_service(app_name, service_name)
  cf("bind-service #{app_name} #{service_name}")
end

#cf(command, options = {}) ⇒ Object



287
288
289
290
291
292
293
294
# File 'lib/hula/cloud_foundry.rb', line 287

def cf(command, options = {})
  allow_failure = options.fetch(:allow_failure, false)
  cf_command = "cf #{command}"

  logger.info(cf_command)

  command_runner.run(cf_command, allow_failure: allow_failure)
end

#create_and_target_org(name) ⇒ Object



69
70
71
72
73
# File 'lib/hula/cloud_foundry.rb', line 69

def create_and_target_org(name)
  create_org(name)
  sleep 1
  target_org(name)
end

#create_and_target_space(name) ⇒ Object



84
85
86
87
# File 'lib/hula/cloud_foundry.rb', line 84

def create_and_target_space(name)
  create_space(name)
  target_space(name)
end

#create_org(name) ⇒ Object



75
76
77
# File 'lib/hula/cloud_foundry.rb', line 75

def create_org(name)
  cf("create-org #{name}")
end

#create_service_instance(type, name, plan) ⇒ Object



167
168
169
# File 'lib/hula/cloud_foundry.rb', line 167

def create_service_instance(type, name, plan)
  cf("create-service #{type} #{plan} #{name}")
end

#create_service_key(service_instance_name, key_name) ⇒ Object



226
227
228
# File 'lib/hula/cloud_foundry.rb', line 226

def create_service_key(service_instance_name, key_name)
  cf("create-service-key #{service_instance_name} #{key_name}")
end

#create_space(name) ⇒ Object



89
90
91
# File 'lib/hula/cloud_foundry.rb', line 89

def create_space(name)
  cf("create-space #{name}")
end

#create_user(username, password) ⇒ Object



261
262
263
# File 'lib/hula/cloud_foundry.rb', line 261

def create_user(username, password)
  cf("create-user #{username} #{password}")
end

#delete_app(name, options = {}) ⇒ Object



209
210
211
212
# File 'lib/hula/cloud_foundry.rb', line 209

def delete_app(name, options = {})
  allow_failure = options.fetch(:allow_failure, true)
  cf("delete #{name} -f", allow_failure: allow_failure)
end

#delete_org(name, options = {}) ⇒ Object Also known as: reset!



131
132
133
134
# File 'lib/hula/cloud_foundry.rb', line 131

def delete_org(name, options = {})
  allow_failure = options.fetch(:allow_failure, true)
  cf("delete-org #{name} -f", allow_failure: allow_failure)
end

#delete_service_instance_and_unbind(name, options = {}) ⇒ Object



171
172
173
174
# File 'lib/hula/cloud_foundry.rb', line 171

def delete_service_instance_and_unbind(name, options = {})
  allow_failure = options.fetch(:allow_failure, true)
  cf("delete-service -f #{name}", allow_failure: allow_failure)
end

#delete_service_key(service_instance_name, key_name) ⇒ Object



230
231
232
# File 'lib/hula/cloud_foundry.rb', line 230

def delete_service_key(service_instance_name, key_name)
  cf("delete-service-key #{service_instance_name} #{key_name} -f")
end

#delete_space(name, options = {}) ⇒ Object



126
127
128
129
# File 'lib/hula/cloud_foundry.rb', line 126

def delete_space(name, options = {})
  allow_failure = options.fetch(:allow_failure, true)
  cf("delete-space #{name} -f", allow_failure: allow_failure)
end

#delete_user(username) ⇒ Object



265
266
267
# File 'lib/hula/cloud_foundry.rb', line 265

def delete_user(username)
  cf("delete-user -f #{username}")
end

#enable_diego_for_app(name) ⇒ Object



205
206
207
# File 'lib/hula/cloud_foundry.rb', line 205

def enable_diego_for_app(name)
  cf("enable-diego #{name}")
end

#get_service_status(service_name, options = {}) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/hula/cloud_foundry.rb', line 176

def get_service_status(service_name, options = {})
  allow_failure = options.fetch(:allow_failure, false)

  output = cf("service #{service_name}", allow_failure: allow_failure)

  missing_instance_message = "Service instance #{service_name} not found"
  if output.include? missing_instance_message
    missing_instance_message
  else
    output[/^Status: .*$/i].gsub(/Status:\s+/i, '')
  end
end

#infoObject



282
283
284
285
# File 'lib/hula/cloud_foundry.rb', line 282

def info
  output = cf("curl /v2/info")
  JSON.parse(output)
end

#list_service_keys(service_instance_name) ⇒ Object



222
223
224
# File 'lib/hula/cloud_foundry.rb', line 222

def list_service_keys(service_instance_name)
  cf("service-keys #{service_instance_name}")
end

#login(username, password, allow_failure = true) ⇒ Object



52
53
54
# File 'lib/hula/cloud_foundry.rb', line 52

def (username, password, allow_failure = true)
  cf("auth #{username} #{password}", allow_failure: allow_failure)
end

#marketplaceObject



163
164
165
# File 'lib/hula/cloud_foundry.rb', line 163

def marketplace
  cf('marketplace')
end

#org_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
# File 'lib/hula/cloud_foundry.rb', line 103

def org_exists?(name)
  orgs = cf('orgs').lines[3..-1]
  orgs.map(&:strip).include?(name)
end

#push_app(app_path, name) ⇒ Object



201
202
203
# File 'lib/hula/cloud_foundry.rb', line 201

def push_app(app_path, name)
  cf("push #{name} -p #{app_path} -n #{name} -d #{domain} --no-start")
end

#push_app_and_start(app_path, name) ⇒ Object



196
197
198
199
# File 'lib/hula/cloud_foundry.rb', line 196

def push_app_and_start(app_path, name)
  push_app(app_path, name)
  start_app(name)
end

#remove_service_broker(service_name, options = {}) ⇒ Object



151
152
153
154
# File 'lib/hula/cloud_foundry.rb', line 151

def remove_service_broker(service_name, options = {})
  allow_failure = options.fetch(:allow_failure, true)
  cf("delete-service-broker #{service_name} -f", allow_failure: allow_failure)
end

#restart_app(name) ⇒ Object



238
239
240
241
# File 'lib/hula/cloud_foundry.rb', line 238

def restart_app(name)
  stop_app(name)
  start_app(name)
end

#service_brokersObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hula/cloud_foundry.rb', line 56

def service_brokers
  output = cf('service-brokers')

  if output.include?('No service brokers found')
    []
  else
    output.split("\n").drop(3).map do |row|
      name, url = row.split(/\s+/)
      ServiceBroker.new(name: name, url: url)
    end
  end
end

#service_key(service_instance_name, key_name) ⇒ Object



234
235
236
# File 'lib/hula/cloud_foundry.rb', line 234

def service_key(service_instance_name, key_name)
  cf("service-key #{service_instance_name} #{key_name}")
end

#set_org_role(username, org, role) ⇒ Object



274
275
276
# File 'lib/hula/cloud_foundry.rb', line 274

def set_org_role(username, org, role)
  cf("set-org-role #{username} #{org} #{role}")
end

#setup_permissive_security_group(org, space) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/hula/cloud_foundry.rb', line 108

def setup_permissive_security_group(org, space)
  rules = [{
    'destination' => '0.0.0.0-255.255.255.255',
    'protocol' => 'all'
  }]

  rule_file = Tempfile.new('default_security_group.json')
  rule_file.write(rules.to_json)
  rule_file.close

  cf("create-security-group prof-test #{rule_file.path}")
  cf("bind-security-group prof-test #{org} #{space}")
  cf('bind-staging-security-group prof-test')
  cf('bind-running-security-group prof-test')

  rule_file.unlink
end

#space_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
# File 'lib/hula/cloud_foundry.rb', line 98

def space_exists?(name)
  spaces = cf('spaces').lines[3..-1]
  spaces.map(&:strip).include?(name)
end

#start_app(name) ⇒ Object



243
244
245
246
247
248
249
250
251
# File 'lib/hula/cloud_foundry.rb', line 243

def start_app(name)
  cf("start #{name}")
rescue => start_exception
  begin
    cf("logs --recent #{name}")
  ensure
    raise start_exception
  end
end

#stop_app(name) ⇒ Object



253
254
255
# File 'lib/hula/cloud_foundry.rb', line 253

def stop_app(name)
  cf("stop #{name}")
end

#target(cloud_controller_url) ⇒ Object



44
45
46
# File 'lib/hula/cloud_foundry.rb', line 44

def target(cloud_controller_url)
  cf("api #{cloud_controller_url} --skip-ssl-validation")
end

#target_org(name) ⇒ Object



79
80
81
82
# File 'lib/hula/cloud_foundry.rb', line 79

def target_org(name)
  cf("target -o #{name}")
  @current_organization = name
end

#target_space(name) ⇒ Object



93
94
95
96
# File 'lib/hula/cloud_foundry.rb', line 93

def target_space(name)
  cf("target -s #{name}")
  @current_space = name
end

#unbind_app_from_service(app_name, service_name) ⇒ Object



218
219
220
# File 'lib/hula/cloud_foundry.rb', line 218

def unbind_app_from_service(app_name, service_name)
  cf("unbind-service #{app_name} #{service_name}")
end

#url_for_app(app_name) ⇒ Object



40
41
42
# File 'lib/hula/cloud_foundry.rb', line 40

def url_for_app(app_name)
  "https://#{app_name}.#{domain}"
end

#user_exists?(username, org) ⇒ Boolean

Returns:

  • (Boolean)


269
270
271
272
# File 'lib/hula/cloud_foundry.rb', line 269

def user_exists?(username, org)
  output = cf("org-users #{org}")
  output.lines.select { |l| l.start_with? '  ' }.map(&:strip).uniq.include?(username)
end

#versionObject



278
279
280
# File 'lib/hula/cloud_foundry.rb', line 278

def version
  cf('-v')
end