Skip to content

Commit

Permalink
Remove the istio-specific logic in the migrate process
Browse files Browse the repository at this point in the history
Wait for the database to be available instead, so that the migrations
can run in deployments without istio (e.g. with contour).

[#175367511]

Authored-by: Matt Royal <[email protected]>
  • Loading branch information
matt-royal committed Nov 24, 2020
1 parent 6d81c00 commit d7a5fe3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ local-worker: bundle exec rake jobs:local
api-worker: bundle exec rake jobs:generic
clock: bundle exec rake clock:start
deployment-updater: bundle exec rake deployment_updater:start
migrate: /bin/bash -c 'bundle exec rake db:wait_for_istio && bundle exec rake db:setup_database && bundle exec rake db:terminate_istio'
migrate: /bin/bash -c 'bundle exec rake db:connect && bundle exec rake db:setup_database'

54 changes: 22 additions & 32 deletions lib/tasks/db.rake
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'httpclient'
require 'retryable'

namespace :db do
desc 'Create a Sequel migration in ./db/migrate'
Expand Down Expand Up @@ -38,6 +39,13 @@ namespace :db do
migrate
end

desc 'Make up to 5 attempts to connect to the database. Succeed it one is successful, and fail otherwise.'
task :connect do
RakeConfig.context = :migrate

connect
end

desc 'Rollback migrations to the database (one migration by default)'
task :rollback, [:number_to_rollback] do |_, args|
RakeConfig.context = :migrate
Expand Down Expand Up @@ -171,24 +179,6 @@ namespace :db do
end
end

desc 'Wait for Istio sidecar to startup'
task :wait_for_istio do
puts 'Waiting indefinitely for Istio sidecar to be healthy'

sleep(5) until istio_sidecar_is_healthy?

puts 'Istio sidecar is now healthy'
end

desc 'Terminate Istio sidecar for migration job'
task :terminate_istio do
puts 'Terminating Istio sidecar'

terminate_istio_sidecar

puts 'Istio sidecar is now terminated'
end

namespace :dev do
desc 'Migrate the database set in spec/support/bootstrap/db_config'
task :migrate do
Expand Down Expand Up @@ -237,6 +227,20 @@ namespace :db do
task recreate: %w[parallel:drop parallel:create]
end

def connect
Steno.init(Steno::Config.new(sinks: [Steno::Sink::IO.new($stdout)]))
logger = Steno.logger('cc.db.connect')
log_method = lambda do |retries, exception|
logger.info("[Attempt ##{retries}] Retrying because [#{exception.class} - #{exception.message}]: #{exception.backtrace.first(5).join(' | ')}")
end

Retryable.retryable(sleep: 1, tries: 5, log_method: log_method) do
VCAP::CloudController::DB.connect(RakeConfig.config.get(:db), logger)
end

logger.info("Successfully connected to database")
end

def migrate
Steno.init(Steno::Config.new(sinks: [Steno::Sink::IO.new($stdout)]))
db_logger = Steno.logger('cc.db.migrations')
Expand Down Expand Up @@ -298,18 +302,4 @@ namespace :db do
end
end
end

def istio_sidecar_is_healthy?
client = HTTPClient.new
response = client.request(:get, 'http://localhost:15020/healthz/ready')

response.code == 200
rescue StandardError
end

def terminate_istio_sidecar
client = HTTPClient.new
response = client.request(:post, 'http://localhost:15000/quitquitquit')
raise StandardError.new("Failed to terminate Istio sidecar. Received response code: #{response.code}") unless response.code == 200
end
end

0 comments on commit d7a5fe3

Please sign in to comment.