Class: SOULs::Sql
- Inherits:
-
Thor
- Object
- Thor
- SOULs::Sql
- Defined in:
- lib/souls/cli/gcloud/sql/index.rb
Instance Method Summary collapse
- #assign_ip ⇒ Object
- #assign_network ⇒ Object
- #create_instance ⇒ Object
- #create_ip_range ⇒ Object
- #create_vpc_connector ⇒ Object
- #env(password: "Password") ⇒ Object
- #list ⇒ Object
- #setup_private_ip ⇒ Object
Instance Method Details
#assign_ip ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/souls/cli/gcloud/sql/index.rb', line 126 def assign_ip require(SOULs.get_mother_path.to_s + "/config/souls") project_id = SOULs.configuration.project_id instance_name = SOULs.configuration.instance_name ips = [] ip = if [:ip].blank? `curl inet-ip.info`.strip else [:ip].strip end ips << ip cloud_sql = JSON.parse( `curl -X GET \ -H "Authorization: Bearer "$(gcloud auth print-access-token) \ "https://sqladmin.googleapis.com/v1/projects/#{project_id}/instances/#{instance_name}?fields=settings"` ) begin cloud_sql["settings"]["ipConfiguration"]["authorizedNetworks"].blank? white_ips = cloud_sql["settings"]["ipConfiguration"]["authorizedNetworks"].map do |sql_ips| sql_ips["value"] end ips = (ips + white_ips).uniq rescue StandardError => e puts(e) end ips = ips.join(",") Whirly.start(spinner: "clock", interval: 420, stop: "🎉") do system( " gcloud sql instances patch #{instance_name} \ --project=#{project_id} \ --assign-ip \ --authorized-networks=#{ips} \ --quiet " ) Whirly.status = Paint["Your IP is successfully added!", :green] end true end |
#assign_network ⇒ Object
86 87 88 89 90 91 |
# File 'lib/souls/cli/gcloud/sql/index.rb', line 86 def assign_network app_name = SOULs.configuration.app instance_name = SOULs.configuration.instance_name project_id = SOULs.configuration.project_id system("gcloud beta sql instances patch #{instance_name} --project=#{project_id} --network=#{app_name}") end |
#create_instance ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/souls/cli/gcloud/sql/index.rb', line 6 def create_instance prompt = TTY::Prompt.new password = prompt.mask("Set DB PassWord:") project_id = SOULs.configuration.project_id instance_name = SOULs.configuration.instance_name region = SOULs.configuration.region if [:region].blank? db_type = [: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" ) SOULs::Sql.new.env(password: password) SOULs::Github.new.secret_set Whirly.status = Paint["Cloud SQL #{instance_name} is successfully created! You can push to deploy!", :green] end true end |
#create_ip_range ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/souls/cli/gcloud/sql/index.rb', line 94 def create_ip_range app_name = SOULs.configuration.app project_id = SOULs.configuration.project_id system( " gcloud compute addresses create #{app_name}-ip-range \ --global \ --purpose=VPC_PEERING \ --prefix-length=16 \ --description='peering range for SOULs' \ --network=#{app_name} \ --project=#{project_id}" ) end |
#create_vpc_connector ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/souls/cli/gcloud/sql/index.rb', line 110 def create_vpc_connector app_name = SOULs.configuration.app project_id = SOULs.configuration.project_id system( " gcloud services vpc-peerings connect \ --service=servicenetworking.googleapis.com \ --ranges=#{app_name}-ip-range \ --network=#{app_name} \ --project=#{project_id} " ) end |
#env(password: "Password") ⇒ Object
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 |
# File 'lib/souls/cli/gcloud/sql/index.rb', line 30 def env(password: "Password") require(SOULs.get_mother_path.to_s + "/config/souls") project_id = SOULs.configuration.project_id instance_name = SOULs.configuration.instance_name region = SOULs.configuration.region app_name = SOULs.configuration.app prompt = TTY::Prompt.new db_password = password == "Password" ? prompt.mask("Set DB PassWord:") : password instance_ip = `gcloud sql instances list --project=#{project_id} | 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(<<~TEXT) GOOGLE_AUTH_SUPPRESS_CREDENTIALS_WARNINGS=1 SOULS_DB_HOST=#{instance_ip} SOULS_DB_PW=#{db_password} SOULS_DB_USER=postgres SOULS_GCP_PROJECT_ID=#{project_id} SOULS_SECRET_KEY_BASE='#{SecureRandom.base64(64)}' TZ="#{region_to_timezone(region: region)}" TEXT end SOULs::Painter.create_file(file_path) end Dir.chdir(SOULs.get_mother_path.to_s) do file_path = ".env.production" File.open(file_path, "w") do |line| line.write(<<~TEXT) SOULS_DB_HOST="/cloudsql/#{project_id}:#{region}:#{instance_name}" SOULS_DB_PW=#{db_password} SOULS_DB_USER=postgres SOULS_APP_NAME=#{app_name} SOULS_GCP_PROJECT_ID=#{project_id} SOULS_GCP_REGION=#{region} SOULS_GCLOUDSQL_INSTANCE="#{project_id}:#{region}:#{instance_name}" SOULS_SECRET_KEY_BASE='#{SecureRandom.base64(64)}' TZ="#{region_to_timezone(region: region)}" TEXT end SOULs::Painter.create_file(file_path) end end |
#list ⇒ Object
74 75 76 |
# File 'lib/souls/cli/gcloud/sql/index.rb', line 74 def list system("gcloud sql instances list") end |
#setup_private_ip ⇒ Object
79 80 81 82 83 |
# File 'lib/souls/cli/gcloud/sql/index.rb', line 79 def setup_private_ip create_ip_range create_vpc_connector assign_network end |