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



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

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



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

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

#canonical_domain_informationObject



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

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



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

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

#canonical_uriObject



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

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

#canonical_uri?Boolean

Returns:

  • (Boolean)


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

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

#configure_routesObject



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

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



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

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)


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

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)


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

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

#defined_and_internal_routesObject



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

def defined_and_internal_routes
  defined_routes.merge flynn_internal_routes
end

#defined_routesObject



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

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



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

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



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

def env_var_crt_prefix
  "CRT_#{ current_branch }_"
end

#env_var_domain_prefixObject



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

def env_var_domain_prefix
  "DDEV_#{ current_branch }_"
end

#env_var_key_prefixObject



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

def env_var_key_prefix
  "KEY_#{ current_branch }_"
end

#fc_routeObject



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

def fc_route
  run_command("flynn -a #{ project_name } route", quiet: true).chomp
end

#fc_route_add(domain) ⇒ Object



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

def fc_route_add(domain)
  run_command("flynn route add http #{ domain } --sticky").chomp
end

#fc_route_remove(route_id) ⇒ Object



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

def fc_route_remove(route_id)
  run_command "flynn route remove #{ route_id }"
end

#fc_route_update(route_id) ⇒ Object



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

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



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

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



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

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

#key_for_ddev_id(ddev_id) ⇒ Object



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

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

#only_whitespaceObject



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

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

#remove_undefined_routesObject



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

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



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

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



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

def routes
  routes_with_legend.drop(1)
end

#routes_all_of(field) ⇒ Object



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

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

#routes_legendObject



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

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

#routes_to_removeObject



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

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



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

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

#service_for_route_id(route_id) ⇒ Object



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

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

#setup_routesObject



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

def setup_routes
  configure_routes
  remove_undefined_routes
  display_routing_information
end

#urlified_routesObject



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

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

#urlified_routes_for_displayObject



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

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

#urlify_route(route) ⇒ Object



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

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

#urlify_route_for_display(route) ⇒ Object



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

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

#use_lets_encrypt?(ddev_id) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


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

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)


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

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