-
Notifications
You must be signed in to change notification settings - Fork 21
/
Rakefile
85 lines (77 loc) · 2.8 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# frozen_string_literal: true
# Copyright 2023 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
require_relative 'config/environment'
require 'sinatra/activerecord/rake'
require "testcontainers"
Bundler.require
desc 'Runs a simple ActiveRecord tutorial for Google Cloud Spanner PostgreSQL on the Spanner emulator.'
task :run do
puts "Starting PGAdapter..."
container = create_pgadapter_and_emulator_container
# Set the port where PGAdapter is running.
ENV["PGPORT"] = container.mapped_port(5432).to_s
begin
sh 'rake db:migrate'
sh 'rake db:seed'
sh 'ruby application.rb'
ensure
container.stop!
container.remove
end
end
desc 'Runs a simple ActiveRecord tutorial for Google Cloud Spanner PostgreSQL.'
task :run_prod do
credentials = ENV["GOOGLE_APPLICATION_CREDENTIALS"]
fail "You must set the environment variable GOOGLE_APPLICATION_CREDENTIALS before running the sample." if credentials.nil?
container = create_pgadapter_container(credentials)
# Set the port where PGAdapter is running.
ENV["PGPORT"] = container.mapped_port(5432).to_s
begin
sh 'rake db:migrate'
sh 'rake db:seed'
sh 'ruby application.rb'
ensure
container.stop!
container.remove
end
end
def create_pgadapter_container(credentials)
puts "Creating PGAdapter container..."
container = Testcontainers::DockerContainer
.new(image="gcr.io/cloud-spanner-pg-adapter/pgadapter",
image_create_options: {
pull: :always,
})
.with_exposed_ports(5432)
.with_filesystem_binds("#{credentials}:/credentials.json:ro")
.with_command("-c /credentials.json -x")
puts "Pulling PGAdapter container image and starting container..."
container.start
puts "Waiting for PGAdapter to be ready..."
container.wait_for_tcp_port(5432)
puts "PGAdapter running on port #{container.mapped_port(5432)}"
# Sleep 2 seconds to be absolutely sure that PGAdapter has started.
sleep(2)
container
end
def create_pgadapter_and_emulator_container
puts "Creating PGAdapter+Emulator container..."
container = Testcontainers::DockerContainer
.new(image="gcr.io/cloud-spanner-pg-adapter/pgadapter-emulator",
image_create_options: {
pull: :always,
})
.with_exposed_ports(5432)
puts "Pulling PGAdapter+Emulator container image and starting container..."
container.start
puts "Waiting for PGAdapter to be ready..."
container.wait_for_tcp_port(5432)
puts "PGAdapter running on port #{container.mapped_port(5432)}"
# Sleep 2 seconds to be absolutely sure that PGAdapter has started.
sleep(2)
container
end