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

SCOPES

Permits SQL admin operations with the Cloud SQL API.

SQL

An alias for the SqladminV1beta4 module.

Attributes

db[R]

The name of the database used to store gemserver data. @return [String]

inst[R]

The name of the Cloud SQL instance. @return [String]

proj_id[R]

The project ID of the project the gemserver will be deployed to. @return [String]

pwd[R]

The password of the default user created to access the database. @return [String]

service[R]

The Cloud SQL API used to manage Cloud SQL instances. @return [Google::Apis::SqladminV1beta4::SQLAdminService]

user[R]

The name of the default user created to access the database. @return [String]

Public Class Methods

new(inst = nil) click to toggle source

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

run() click to toggle source

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

create_db(&block) click to toggle source

@private Creates a database for a Cloud SQL instance.

# File lib/google/cloud/gemserver/cli/cloud_sql.rb, line 124
def create_db &block
  puts "Creating database #{@db}"
  db = SQL::Database.new name: @db
  @service.insert_database(@proj_id, @inst, db, &block)
end
create_instance() { || ... } click to toggle source

@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
create_user() click to toggle source

@private Creates a user for a Cloud SQL instance.

# File lib/google/cloud/gemserver/cli/cloud_sql.rb, line 132
def create_user
  puts "Creating user #{@user}"
  user = SQL::User.new(name: @user, password: @pwd)
  run_sql_task @service.insert_user(@proj_id, @inst, user)
end
del_instance() click to toggle source

Deletes the Cloud SQL instance for a gemserver.

# File lib/google/cloud/gemserver/cli/cloud_sql.rb, line 158
def del_instance
  puts "Deleting instance: #{@inst}"
  @service.delete_instance @proj_id, @inst
end
instance() click to toggle source

@private Fetches a Cloud SQL instance.

@return [Google::Apis::SqladminV1beta4::DatabaseInstance

# File lib/google/cloud/gemserver/cli/cloud_sql.rb, line 152
def instance
  @service.get_instance @proj_id, @inst
end
load_config() click to toggle source

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
run_sql_task(op) click to toggle source

Runs a Cloud SQL task and polls for its completion.

# File lib/google/cloud/gemserver/cli/cloud_sql.rb, line 187
def run_sql_task op
  while @service.get_operation(@proj_id, op.name).status != "DONE"
    sleep 2
  end
end
update_config() click to toggle source

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
update_root_user() click to toggle source

@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