class Google::Cloud::Gemserver::CLI::CloudSQL
# CloudSQL
CloudSQL
manages the creation of a Cloud
SQL
instance as well as the necessary database and user creation.
Constants
Attributes
The name of the database used to store gemserver data. @return [String]
The project ID of the project the gemserver will be deployed to. @return [String]
The password of the default user created to access the database. @return [String]
The name of the default user created to access the database. @return [String]
Public Class Methods
Creates a CloudSQL
object and loads the necessary configuration settings.
@param inst [String] Name of the instance to be used. Optional.
# File lib/google/cloud/gemserver/cli/cloud_sql.rb, line 76 def initialize inst = nil auth = Google::Auth.get_application_default SCOPES Google::Apis::RequestOptions.default.authorization = auth @config = Configuration.new @service = SQL::SQLAdminService.new @inst = inst || "instance-#{SecureRandom.uuid}".freeze @custom = inst ? true : false load_config end
Public Instance Methods
Prepares a Cloud
SQL
instance with a database and user. Also saves the database settings in the appropriate configuration file.
# File lib/google/cloud/gemserver/cli/cloud_sql.rb, line 89 def run create_instance do |instance_op| run_sql_task instance_op if instance_op.class == SQL::Operation update_root_user create_db do |db_op| run_sql_task db_op create_user end end update_config end
Private Instance Methods
@private Creates a Cloud
SQL
instance.
# File lib/google/cloud/gemserver/cli/cloud_sql.rb, line 105 def create_instance &block if @custom puts "Using existing Cloud SQL instance: #{@inst}" yield return instance end puts "Creating Cloud SQL instance #{@inst} (this takes a few "\ "minutes to complete)" settings = SQL::Settings.new(tier: "db-f1-micro") payload = SQL::DatabaseInstance.new( name: @inst, project: @proj_id, settings: settings ) @service.insert_instance(@proj_id, payload, &block) end
Sets various Cloud
SQL
settings used to create a Cloud
SQL
instance.
# File lib/google/cloud/gemserver/cli/cloud_sql.rb, line 166 def load_config @db = @config[:db_connection_options][:database] @user = @config[:db_connection_options][:username] @pwd = @config[:db_connection_options][:password] @proj_id = @config[:proj_id] end
Saves the Cloud
SQL
configuration in the appropriate configuration file and app configuration file.
# File lib/google/cloud/gemserver/cli/cloud_sql.rb, line 176 def update_config puts "Updating configurations: app.yaml and config.yml " conn_name = instance.connection_name @config.update_config "/cloudsql/#{conn_name}", :db_connection_options, :socket @config.update_app conn_name, "beta_settings", "cloud_sql_instances" end
@private Updates the password of the root user if a new Cloud
SQL
instance was created.
# File lib/google/cloud/gemserver/cli/cloud_sql.rb, line 141 def update_root_user return if @custom cmd = "gcloud sql users set-password root % --password #{@pwd} "\ "-i #{@inst} --project #{@proj_id}" `#{cmd}` end