Module: Satorix::CI::Deploy::Flynn::Routes

Included in:
Satorix::CI::Deploy::Flynn
Defined in:
lib/satorix/CI/deploy/flynn/routes.rb

Instance Method Summary collapse

Instance Method Details

#add_tls_to_route(route_id:, ddev_id:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 32

def add_tls_to_route(route_id:, ddev_id:)
  domain = defined_and_internal_routes[ddev_id]
  if use_tls?(ddev_id)
    if use_lets_encrypt?(ddev_id)
      log "Using Let's Encrypt for #{ domain }."
      log_error_and_abort "Let's Encrypt support is not implemented, yet!"
    else
      log "Using #{ user_defined_tls?(ddev_id) ? 'custom' : 'default' } certificate details for #{ domain } (#{ service_for_route_id(route_id) } service)."
      File.open('crt', 'w') { |f| f.write(crt_for_ddev_id(ddev_id)) }
      File.open('key', 'w') { |f| f.write(key_for_ddev_id(ddev_id)) }
      fc_route_update(route_id)
    end
  else
    log "Skipping TLS configuration for #{ domain }."
    log "Environment variables #{ env_var_crt_prefix }#{ ddev_id } and #{ env_var_key_prefix }#{ ddev_id } have not been specified."
    log 'For more information, please refer to https://www.satorix.com/docs/user/projects#certificates'
  end
  log ''
end

#canonical_domainObject



53
54
55
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 53

def canonical_domain
  ENV["AEEV_#{ current_branch }_SATORIX_CANONICAL_URI_HOST"]
end

#canonical_domain_informationObject



58
59
60
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 58

def canonical_domain_information
  "\nThe above routes will all be redirected to the default URL:\n\n\t#{ canonical_uri }\n\n" if canonical_uri?
end

#canonical_domain_protocolObject



63
64
65
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 63

def canonical_domain_protocol
  ENV["AEEV_#{ current_branch }_SATORIX_CANONICAL_URI_PROTOCOL"]
end

#canonical_uriObject



68
69
70
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 68

def canonical_uri
  "#{ canonical_domain_protocol }://#{ canonical_domain }" if canonical_uri?
end

#canonical_uri?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 73

def canonical_uri?
  [canonical_domain, canonical_domain_protocol].all? { |x| x.to_s !~ only_whitespace }
end

#configure_routesObject



22
23
24
25
26
27
28
29
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 22

def configure_routes
  defined_and_internal_routes.each do |ddev_id, domain|
    route_ids = find_or_create_route(domain)
    route_ids.each do |route_id|
      add_tls_to_route(route_id: route_id, ddev_id: ddev_id)
    end
  end
end

#crt_for_ddev_id(ddev_id) ⇒ Object



78
79
80
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 78

def crt_for_ddev_id(ddev_id)
  ENV["#{ env_var_crt_prefix }#{ user_defined_tls?(ddev_id) ? ddev_id : 'DEFAULT' }"]
end

#custom_crt_for_ddev_id?(ddev_id) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 83

def custom_crt_for_ddev_id?(ddev_id)
  ENV["#{ env_var_crt_prefix }#{ ddev_id }"].to_s !~ only_whitespace
end

#custom_key_for_ddev_id?(ddev_id) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 88

def custom_key_for_ddev_id?(ddev_id)
  ENV["#{ env_var_key_prefix }#{ ddev_id }"].to_s !~ only_whitespace
end

#defined_and_internal_routesObject



93
94
95
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 93

def defined_and_internal_routes
  defined_routes.merge flynn_internal_routes
end

#defined_routesObject



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 98

def defined_routes
  {}.tap do |routes|
    invalid_keys = flynn_internal_routes.keys
    ENV.each do |key, value|
      next unless key.start_with?(env_var_domain_prefix)
      renamed_key = key.sub(env_var_domain_prefix, '')
      message = "The user-defined route #{ key } conflicts with an internal route."
      log_error_and_abort message if invalid_keys.include?(renamed_key)
      routes[renamed_key] = value
    end
  end
end

#display_routing_informationObject



112
113
114
115
116
117
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 112

def display_routing_information
  log 'All application routes...'
  log ''
  log urlified_routes_for_display.map { |r| "\t#{ r }" }
  log canonical_domain_information if canonical_uri?
end

#env_var_crt_prefixObject



120
121
122
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 120

def env_var_crt_prefix
  "CRT_#{ current_branch }_"
end

#env_var_domain_prefixObject



125
126
127
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 125

def env_var_domain_prefix
  "DDEV_#{ current_branch }_"
end

#env_var_key_prefixObject



130
131
132
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 130

def env_var_key_prefix
  "KEY_#{ current_branch }_"
end

#fc_routeObject



135
136
137
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 135

def fc_route
  run_command(['flynn', '-a', project_name, 'route'], quiet: true).chomp
end

#fc_route_add(domain) ⇒ Object



140
141
142
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 140

def fc_route_add(domain)
  run_command(['flynn', 'route', 'add', 'http', domain, '--sticky']).chomp
end

#fc_route_remove(route_id) ⇒ Object



145
146
147
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 145

def fc_route_remove(route_id)
  run_command(['flynn', 'route', 'remove', route_id])
end

#fc_route_update(route_id) ⇒ Object



150
151
152
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 150

def fc_route_update(route_id)
  run_command(['flynn', 'route', 'update', route_id, '--tls-cert=crt', '--tls-key=key', '--sticky'], quiet: true)
end

#find_or_create_route(domain) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 7

def find_or_create_route(domain)
  route_ids = route_ids(domain)
  if route_ids.empty?
    log "Adding route for #{ domain }..."
    route_ids << fc_route_add(domain)
    log "Route for #{ domain } added with the ID of #{ route_ids.first }."
  else
    multiple = route_ids.length > 1
    log "Route#{ 's' if multiple } already exist#{ 's' unless multiple } for #{ domain }."
  end

  route_ids
end

#flynn_internal_routesObject



155
156
157
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 155

def flynn_internal_routes
  { 'FLYNN_INTERNAL' => "#{ project_name }.#{ domain_for_web_host }" }
end

#key_for_ddev_id(ddev_id) ⇒ Object



160
161
162
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 160

def key_for_ddev_id(ddev_id)
  ENV["#{ env_var_key_prefix }#{ user_defined_tls?(ddev_id) ? ddev_id : 'DEFAULT' }"]
end

#only_whitespaceObject



165
166
167
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 165

def only_whitespace
  /\A\s*\z/m
end

#remove_undefined_routesObject



170
171
172
173
174
175
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 170

def remove_undefined_routes
  routes_to_remove.each do |route|
    log_header "Removing undefined route #{ route[routes_legend['ROUTE']] }..."
    fc_route_remove route[routes_legend['ID']]
  end
end

#route_ids(route) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 178

def route_ids(route)
  [].tap do |route_ids|
    routes_all_of('ROUTE').map { |r| r.sub(/https??:/i, '') }.each_with_index do |r, i|
      route_ids << routes_all_of('ID')[i] if r == route
    end
  end
end

#routesObject



187
188
189
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 187

def routes
  routes_with_legend.drop(1)
end

#routes_all_of(field) ⇒ Object



192
193
194
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 192

def routes_all_of(field)
  routes.map { |a| a[routes_legend[field]] }
end

#routes_legendObject



197
198
199
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 197

def routes_legend
  {}.tap { |h| routes_with_legend.first.each_with_index { |route, index| h[route] = index } }
end

#routes_to_removeObject



202
203
204
205
206
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 202

def routes_to_remove
  routes.reject do |route|
    defined_and_internal_routes.values.include? route[routes_legend['ROUTE']].partition(':').last
  end
end

#routes_with_legendObject



209
210
211
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 209

def routes_with_legend
  fc_route.split("\n").map(&:split)
end

#service_for_route_id(route_id) ⇒ Object



214
215
216
217
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 214

def service_for_route_id(route_id)
  index = routes_all_of('ID').index(route_id)
  routes_all_of('SERVICE')[index]
end

#setup_routesObject



220
221
222
223
224
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 220

def setup_routes
  configure_routes
  remove_undefined_routes
  display_routing_information
end

#urlified_routesObject



227
228
229
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 227

def urlified_routes
  routes_all_of('ROUTE').map { |r| urlify_route r }
end

#urlified_routes_for_displayObject



232
233
234
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 232

def urlified_routes_for_display
  urlified_routes.map { |route| urlify_route_for_display route }.sort
end

#urlify_route(route) ⇒ Object



237
238
239
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 237

def urlify_route(route)
  route.sub(':', '://')
end

#urlify_route_for_display(route) ⇒ Object



242
243
244
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 242

def urlify_route_for_display(route)
  route =~ /^http:/ ? route : "#{ route.sub 'https', 'http' }, #{ route }"
end

#use_lets_encrypt?(ddev_id) ⇒ Boolean

Returns:

  • (Boolean)


247
248
249
250
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 247

def use_lets_encrypt?(ddev_id)
  crt_for_ddev_id(ddev_id).to_s.gsub(/\W+/, '').casecmp('letsencrypt').zero? &&
    key_for_ddev_id(ddev_id).to_s.gsub(/\W+/, '').casecmp('letsencrypt').zero?
end

#use_tls?(ddev_id) ⇒ Boolean

Returns:

  • (Boolean)


253
254
255
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 253

def use_tls?(ddev_id)
  [crt_for_ddev_id(ddev_id), key_for_ddev_id(ddev_id)].all? { |x| x.to_s !~ only_whitespace }
end

#user_defined_tls?(ddev_id) ⇒ Boolean

Returns:

  • (Boolean)


258
259
260
# File 'lib/satorix/CI/deploy/flynn/routes.rb', line 258

def user_defined_tls?(ddev_id)
  custom_crt_for_ddev_id?(ddev_id) && custom_key_for_ddev_id?(ddev_id)
end