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
|
# File 'lib/souls/cli/gcloud/sql/index.rb', line 6
def create_instance
prompt = TTY::Prompt.new
password = prompt.mask("Set DB PassWord:")
app_name = Souls.configuration.app
project_id = Souls.configuration.project_id
instance_name = Souls.configuration.instance_name
region = Souls.configuration.region if options[:region].blank?
db_type = options[:mysql] ? "MYSQL_8_0" : "POSTGRES_13"
zone = "#{region}-b"
system("gcloud config set project #{project_id} >/dev/null 2>&1")
Whirly.start(spinner: "clock", interval: 420, stop: "🎉") do
system(
"gcloud sql instances create #{instance_name} \
--database-version=#{db_type} --cpu=1 --memory=4096MB --zone=#{zone} \
--root-password='#{password}' --database-flags cloudsql.iam_authentication=on"
)
instance_ip = `gcloud sql instances list | grep #{instance_name} | awk '{print $5}'`.strip
Dir.chdir(Souls.get_api_path.to_s) do
file_path = ".env"
File.open(file_path, "w") do |line|
line.write(" GOOGLE_AUTH_SUPPRESS_CREDENTIALS_WARNINGS=1\n SOULS_DB_HOST=\#{instance_ip}\n SOULS_DB_PW=\#{password}\n SOULS_DB_USER=postgres\n SOULS_GCP_PROJECT_ID=\#{project_id}\n SOULS_SECRET_KEY_BASE='\#{SecureRandom.base64(64)}'\n TZ=\"\#{region_to_timezone(region: region)}\"\n TEXT\n end\n end\n Dir.chdir(Souls.get_mother_path.to_s) do\n file_path = \".env.production\"\n File.open(file_path, \"w\") do |line|\n line.write(<<~TEXT)\n SOULS_DB_HOST=\"/cloudsql/\#{project_id}:\#{region}:\#{instance_name}\"\n SOULS_DB_PW=\#{password}\n SOULS_DB_USER=postgres\n SOULS_APP_NAME=\#{app_name}\n SOULS_GCP_PROJECT_ID=\#{project_id}\n SOULS_GCP_REGION=\#{region}\n SOULS_GCLOUDSQL_INSTANCE=\"\#{project_id}:\#{region}:\#{instance_name}\"\n SOULS_SECRET_KEY_BASE='\#{SecureRandom.base64(64)}'\n TZ=\"\#{region_to_timezone(region: region)}\"\n TEXT\n end\n end\n Souls::Github.new.secret_set\n Whirly.status = Paint[\"Cloud SQL \#{instance_name} is successfully created! You can push to deploy!\", :green]\n end\n true\nend\n")
|