Class: CloudfoundryBlueGreenDeploy::BlueGreenDeploy

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudfoundry_blue_green_deploy/blue_green_deploy.rb

Class Method Summary collapse

Class Method Details

.cfObject



11
12
13
# File 'lib/cloudfoundry_blue_green_deploy/blue_green_deploy.rb', line 11

def self.cf
  Cloudfoundry
end

.determine_target_color(hot_app_name) ⇒ Object



114
115
116
117
# File 'lib/cloudfoundry_blue_green_deploy/blue_green_deploy.rb', line 114

def self.determine_target_color(hot_app_name)
  target_color = get_color_stem(hot_app_name)
  BlueGreenDeployConfig.toggle_color(target_color)
end

.ensure_hot_instance_is_not_target(deploy_config, hot_app_name) ⇒ Object



83
84
85
86
87
88
# File 'lib/cloudfoundry_blue_green_deploy/blue_green_deploy.rb', line 83

def self.ensure_hot_instance_is_not_target(deploy_config, hot_app_name)
  if deploy_config.is_in_target?(hot_app_name)
    raise InvalidRouteStateError.new(
      "The app \"#{hot_app_name}\" is already hot (target color is #{deploy_config.target_color}).")
  end
end

.ensure_hot_workers_are_not_target(deploy_config) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/cloudfoundry_blue_green_deploy/blue_green_deploy.rb', line 90

def self.ensure_hot_workers_are_not_target(deploy_config)
  apps = cf.apps

  deploy_config.target_worker_app_names.each do |hot_worker|
    if deploy_config.is_in_target?(hot_worker) && invalid_worker?(hot_worker, apps)
      raise InvalidWorkerStateError.new(
        "Worker #{hot_worker} is already hot (target color is #{deploy_config.target_color}).")
    end
  end
end

.ensure_there_is_a_hot_instance(deploy_config, hot_app_name) ⇒ Object



76
77
78
79
80
81
# File 'lib/cloudfoundry_blue_green_deploy/blue_green_deploy.rb', line 76

def self.ensure_there_is_a_hot_instance(deploy_config, hot_app_name)
  if hot_app_name.nil?
    raise InvalidRouteStateError.new(
      "There is no route mapped from #{deploy_config.hot_url} to an app.")
  end
end

.first_deploy?(hot_app_name, both_invalid_and_valid_hot_worker_names) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/cloudfoundry_blue_green_deploy/blue_green_deploy.rb', line 72

def self.first_deploy?(hot_app_name, both_invalid_and_valid_hot_worker_names)
  hot_app_name.nil? && both_invalid_and_valid_hot_worker_names.empty?
end

.get_color_stem(hot_app_name) ⇒ Object



110
111
112
# File 'lib/cloudfoundry_blue_green_deploy/blue_green_deploy.rb', line 110

def self.get_color_stem(hot_app_name)
  hot_app_name.slice((hot_app_name.rindex('-') + 1)..(hot_app_name.length))
end

.get_hot_web_app(hot_url) ⇒ Object



129
130
131
132
133
# File 'lib/cloudfoundry_blue_green_deploy/blue_green_deploy.rb', line 129

def self.get_hot_web_app(hot_url)
  cf_routes = cf.routes
  hot_route = cf_routes.find { |route| route.host == hot_url }
  hot_route.nil? ? nil : hot_route.app
end

.get_hot_worker_namesObject



135
136
137
138
139
140
141
142
# File 'lib/cloudfoundry_blue_green_deploy/blue_green_deploy.rb', line 135

def self.get_hot_worker_names
  cf_apps = cf.apps
  hot_names = []
  cf_apps.each do |app|
    hot_names << app.name if app.state == 'started'
  end
  hot_names
end

.invalid_worker?(hot_worker, apps) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
# File 'lib/cloudfoundry_blue_green_deploy/blue_green_deploy.rb', line 101

def self.invalid_worker?(hot_worker, apps)
  apps.each do |app|
    if app.name == hot_worker && app.state == 'started'
      return true
    end
  end
  return false
end

.make_hot(app_name, deploy_config) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/cloudfoundry_blue_green_deploy/blue_green_deploy.rb', line 119

def self.make_hot(app_name, deploy_config)
  hot_url = deploy_config.hot_url
  hot_app = get_hot_web_app(hot_url)
  cold_app = deploy_config.target_web_app_name
  domain = deploy_config.domain

  cf.map_route(cold_app, domain, hot_url)
  cf.unmap_route(hot_app, domain, hot_url) if hot_app
end

.make_it_so(app_name, worker_apps, deploy_config) ⇒ Object



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
# File 'lib/cloudfoundry_blue_green_deploy/blue_green_deploy.rb', line 15

def self.make_it_so(app_name, worker_apps, deploy_config)
  hot_app_name = get_hot_web_app(deploy_config.hot_url)
  both_invalid_and_valid_hot_worker_names = get_hot_worker_names

  is_first_deploy = first_deploy?(hot_app_name, both_invalid_and_valid_hot_worker_names)

  deploy_config.target_color = set_target_color(is_first_deploy, hot_app_name)

  ready_for_takeoff(hot_app_name, both_invalid_and_valid_hot_worker_names, deploy_config)

  with_shutter = deploy_config.with_shutter
  hot_url = deploy_config.hot_url
  cold_app = deploy_config.target_web_app_name
  domain = deploy_config.domain
  shutter_app_name = deploy_config.shutter_app_name

  if with_shutter
    cf.push(shutter_app_name)
    cf.map_route(shutter_app_name, domain, hot_url)
    cf.unmap_route(hot_app_name, domain, hot_url) if hot_app_name
  end

  cf.push(cold_app)

  deploy_config.target_worker_app_names.each do |worker_app_name|
    cf.push(worker_app_name)
    unless is_first_deploy
      to_be_cold_worker = BlueGreenDeployConfig.toggle_app_color(worker_app_name)
      cf.stop(to_be_cold_worker)
    end
  end

  if with_shutter
    cf.map_route(cold_app, domain, hot_url)
    cf.unmap_route(shutter_app_name, domain, hot_url)
  else
    make_hot(app_name, deploy_config)
  end
end

.ready_for_takeoff(hot_app_name, both_invalid_and_valid_hot_worker_names, deploy_config) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/cloudfoundry_blue_green_deploy/blue_green_deploy.rb', line 64

def self.ready_for_takeoff(hot_app_name, both_invalid_and_valid_hot_worker_names, deploy_config)
  unless first_deploy?(hot_app_name, both_invalid_and_valid_hot_worker_names)
    ensure_there_is_a_hot_instance(deploy_config, hot_app_name)
    ensure_hot_instance_is_not_target(deploy_config, hot_app_name)
    ensure_hot_workers_are_not_target(deploy_config)
  end
end

.set_target_color(is_first_deploy, hot_app_name) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/cloudfoundry_blue_green_deploy/blue_green_deploy.rb', line 55

def self.set_target_color(is_first_deploy, hot_app_name)
  if is_first_deploy
    'blue'
  else
    determine_target_color(hot_app_name) unless hot_app_name.nil?
  end
end