Class: Bookbinder::CfCommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/bookbinder/cf_command_runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(streams, sheller, cf_credentials, trace_file) ⇒ CfCommandRunner

Returns a new instance of CfCommandRunner.



5
6
7
8
9
10
# File 'lib/bookbinder/cf_command_runner.rb', line 5

def initialize(streams, sheller, cf_credentials, trace_file)
  @streams = streams
  @sheller = sheller
  @creds = cf_credentials
  @trace_file = trace_file
end

Instance Method Details

#cf_routes_outputObject



27
28
29
30
31
# File 'lib/bookbinder/cf_command_runner.rb', line 27

def cf_routes_output
  sheller.get_stdout("CF_COLOR=false #{cf_binary_path} routes").tap do |output|
    raise 'failure executing cf routes' if output == ''
  end
end

#loginObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bookbinder/cf_command_runner.rb', line 12

def 
  username = creds.username
  password = creds.password
  api_endpoint = creds.api_endpoint
  organization = creds.organization
  space = creds.space
  creds_string = (username && password) ? "-u '#{username}' -p '#{password}'" : ''

  result = sheller.run_command(
    "#{cf_binary_path} login #{creds_string} -a '#{api_endpoint}' -o '#{organization}' -s '#{space}'",
    streams
  )
  raise "Could not log in to #{creds.api_endpoint}" unless result.success?
end

#map_routes(app) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bookbinder/cf_command_runner.rb', line 61

def map_routes(app)
  succeeded = []

  creds.flat_routes.each do |domain, name|
    begin
      map_route(app, domain, name)
      succeeded << [app, domain, name]
    rescue RuntimeError
      succeeded.each { |app, domain, host| unmap_route(app, domain, host) }
      raise
    end
  end
end

#new_appObject



33
34
35
# File 'lib/bookbinder/cf_command_runner.rb', line 33

def new_app
  Deploy::BlueGreenApp.new([creds.app_name, 'blue'].join('-'))
end

#push(deploy_target_app) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/bookbinder/cf_command_runner.rb', line 44

def push(deploy_target_app)
  # Currently --no-routes is used to blow away all existing routes from a newly deployed app.
  # The routes will then be recreated from the creds repo.
  result = sheller.run_command(
    environment_variables,
    "#{cf_binary_path} push #{deploy_target_app} -b ruby_buildpack -s cflinuxfs2 --no-route -m 256M -i 3",
    streams
  )
  raise "Could not deploy app to #{deploy_target_app}" unless result.success?
end

#start(deploy_target_app) ⇒ Object



37
38
39
40
41
42
# File 'lib/bookbinder/cf_command_runner.rb', line 37

def start(deploy_target_app)
  # Theoretically we shouldn't need this (and corresponding "stop" below), but we've seen CF pull files from both
  # green and blue when a DNS redirect points to HOST.cfapps.io
  # Also, shutting down the unused app saves $$
  sheller.run_command("#{cf_binary_path} start #{deploy_target_app}", streams)
end

#takedown_old_target_app(app) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/bookbinder/cf_command_runner.rb', line 75

def takedown_old_target_app(app)
  # Routers flush every 10 seconds (but not guaranteed), so wait a bit longer than that.
  streams[:out].puts "waiting 15 seconds for routes to remap...\n\n"
  (1..15).to_a.reverse.each do |seconds|
    streams[:out] << "\r\r#{seconds}...    "
    Kernel.sleep 1
  end
  stop(app)
  unmap_routes(app)
end

#unmap_routes(app) ⇒ Object



55
56
57
58
59
# File 'lib/bookbinder/cf_command_runner.rb', line 55

def unmap_routes(app)
  creds.flat_routes.each do |domain, host|
    unmap_route(app, domain, host)
  end
end