class Google::Cloud::Gemserver::Configuration

# Configuration

Stores configurations for the gemserver and provides methods for altering that configuration.

Constants

APP_DEFAULTS

Default settings for gemserver deployment to Google App Engine via app.yaml.

APP_ENGINE_ENV_VARS

Environment variables used by app.yaml for gemserver deployment.

AUTO_SCALING_DEFAULT

Setting used by Google App Engine to enable auto scaling.

BETA_SETTING_DEFAULTS

Beta setting used by Google App Engine to connect to the Cloud SQL instance.

CONFIG_DB_PREFIX

Prefix for all database configuration setting fields in app.yaml.

CONFIG_DIR

@private Base directory containing configuration files.

CONFIG_PREFIX

Prefix for all general configuration setting fields in app.yaml.

CREDS_PATH

@private The path to the credentials file used by the `gem` command.

DEFAULT_CONFIG

Default configuration settings for the production gemserver.

DEFAULT_DEV_CONFIG

Default configuration settings for the development gemserver.

DEFAULT_KEY_NAME

@private The default name of the gemserver key.

DEFAULT_TEST_CONFIG

Default configuration settings for the test gemserver.

DEV_DB_DEFAULTS

Default configuration settings for the dev database.

GAE_DIR

@private The path to the app folder on a deployed gemserver.

GAE_PATH

@private The path to the configuration file on a deployed gemserver.

GCS_PATH

@private Path to the configuration file on Google Cloud Storage that was last used for a gemserver deploy. This path is checked by the `config` command to display the last deployed gemserver's configuration.

GEMSTASH_DIR

@private The path gem data is stored

GEM_NAME

The name of the gem.

HEALTH_CHECK_DEFAULT

Setting used by Google App Engine to disable health checks for faster deploys.

PROD_DB_DEFAULTS

Default configuration settings for the production database.

SERVER_PATH

@private Temporary directory created to prepare a gemserver deploy to a Google Cloud Platform project.

TEST_DB_DEFAULTS

Default configuration settings for the test database.

Attributes

app[RW]

The configuration used by gcloud to deploy the gemserver to Google App Engine. @return [Hash]

config[RW]

The configuration used by the gemserver. @return [Hash]

Public Class Methods

deployed?() click to toggle source

Checks if the gemserver was deployed by the existence of the config file used to deploy it on a specific path on Google Cloud Storage.

@return [Boolean]

# File lib/google/cloud/gemserver/configuration.rb, line 319
def self.deployed?
  !GCS.get_file(GCS_PATH).nil?
end
display_config() click to toggle source

Displays the configuration used by the current gemserver

# File lib/google/cloud/gemserver/configuration.rb, line 303
def self.display_config
  unless deployed?
    puts "No configuration found. Was the gemserver deployed?"
    return
  end
  prepare GCS.get_file(GCS_PATH)
  puts "Gemserver is running with this configuration:"
  puts YAML.load_file(GCS_PATH).to_yaml
  cleanup
end
new() click to toggle source

Instantiate a new instance of Configuration

# File lib/google/cloud/gemserver/configuration.rb, line 215
def initialize
  @app    = load_app
  @config = load_config
end

Private Class Methods

cleanup() click to toggle source

@private Deletes a temporary directory.

# File lib/google/cloud/gemserver/configuration.rb, line 496
def self.cleanup
  FileUtils.rm_rf SERVER_PATH
end
prepare(file) click to toggle source

@private Creates a temporary directory to download the configuration file used to deploy the gemserver.

# File lib/google/cloud/gemserver/configuration.rb, line 489
def self.prepare file
  FileUtils.mkpath SERVER_PATH
  file.download file.name
end

Public Instance Methods

[](key) click to toggle source

Accesses a key in the Configuration object.

@param [String] key Name of the key accessed.

@return [String]

# File lib/google/cloud/gemserver/configuration.rb, line 267
def [] key
  @config[key]
end
app_path() click to toggle source

Fetches the path to the relevant app configuration file.

@return [String]

# File lib/google/cloud/gemserver/configuration.rb, line 297
def app_path
  "#{config_dir}/app.yaml"
end
config_path() click to toggle source

Fetches the path to the relevant configuration file based on the environment (production, test, development).

@return [String]

# File lib/google/cloud/gemserver/configuration.rb, line 289
def config_path
  "#{config_dir}/#{suffix}"
end
delete_from_cloud() click to toggle source

Deletes the configuration file used for a deployment

# File lib/google/cloud/gemserver/configuration.rb, line 229
def delete_from_cloud
  GCS.delete_file GCS_PATH
end
gen_config() click to toggle source

@private Generates a set of configuration files for the gemserver to run and deploy to Google App Engine.

# File lib/google/cloud/gemserver/configuration.rb, line 274
def gen_config
  return if on_appengine
  FileUtils.mkpath config_dir unless Dir.exist? config_dir

  write_file "#{config_dir}/app.yaml",        app_config, true
  write_file "#{config_dir}/config.yml",      prod_config
  write_file "#{config_dir}/dev_config.yml",  dev_config
  write_file "#{config_dir}/test_config.yml", test_config
end
save_to_cloud() click to toggle source

Saves the configuration file used for a deployment.

# File lib/google/cloud/gemserver/configuration.rb, line 222
def save_to_cloud
  puts "Saving configuration"
  GCS.upload config_path, GCS_PATH
end
update_app(value, key, sub_key = nil) click to toggle source

Updates the app configuration file.

@param [String] value New value of the key. @param [String] key Name of the key that will be updated. @param [String] sub_key Name of the sub key that will be updated.

# File lib/google/cloud/gemserver/configuration.rb, line 252
def update_app value, key, sub_key = nil
  if sub_key
    @app[key][sub_key] = value
  else
    @app[key] = value
  end
  write_app
end
update_config(value, key, sub_key = nil) click to toggle source

Updates the configuration file.

@param [String] value New value of the key. @param [String] key Name of the key that will be updated. @param [String] sub_key Name of the sub key that will be updated.

# File lib/google/cloud/gemserver/configuration.rb, line 239
def update_config value, key, sub_key = nil
  if sub_key
    @config[key][sub_key] = value
  else
    @config[key] = value
  end
  write_config
end

Private Instance Methods

app_config() click to toggle source

@private The default app.yaml configuration formatted in YAML.

@return [String]

# File lib/google/cloud/gemserver/configuration.rb, line 370
def app_config
  APP_DEFAULTS.merge(load_app).to_yaml
end
config_dir() click to toggle source

@private Fetches the directory that contains the configuration files.

@return [String]

# File lib/google/cloud/gemserver/configuration.rb, line 472
def config_dir
  return GAE_DIR if on_appengine
  dir = ENV["GEMSERVER_CONFIG_DIR"]
  dir.nil? == true ? CONFIG_DIR : dir
end
dev_config() click to toggle source

@private The default dev_config.yml configuration formatted in YAML used by the gemserver in the dev environment.

@return [String]

# File lib/google/cloud/gemserver/configuration.rb, line 388
def dev_config
  DEFAULT_DEV_CONFIG.deep_merge(extract_config("dev")).to_yaml
end
env() click to toggle source

@private Fetches the current environment.

@return [String]

# File lib/google/cloud/gemserver/configuration.rb, line 329
def env
  ENV["APP_ENV"].nil? == true ? "production" : ENV["APP_ENV"]
end
extract_config(pre = "production") click to toggle source

@private Extracts the gemserver configuration from the app.yaml environment variables.

@param [String] pre The prefix of the config fields to extract.

@return [Hash]

# File lib/google/cloud/gemserver/configuration.rb, line 408
def extract_config pre = "production"
  adapter = pre + "_" + CONFIG_DB_PREFIX + "_adapter"
  db_config = @app["env_variables"].map do |k, v|
    # db_adapter is a special case b/c it has the 'db' in its name but is
    # not a db_connection_options field
    next unless k.include?(pre) && k != adapter
    [(k[pre.size + CONFIG_DB_PREFIX.size + 2..k.size - 1]).to_sym, v]
  end.compact.to_h
  config = @app["env_variables"].map do |k, v|
    next unless k.include? CONFIG_PREFIX
    [(k[CONFIG_PREFIX.size + 1..k.size - 1]).to_sym, v]
  end.compact.to_h
  {
    :db_connection_options => db_config,
    :db_adapter => @app["env_variables"][adapter]
  }.deep_merge config
end
load_app() click to toggle source

@private Loads the app configuration file.

@return [Hash]

# File lib/google/cloud/gemserver/configuration.rb, line 438
def load_app
  return APP_DEFAULTS unless File.exist? app_path
  YAML.load_file app_path
end
load_config() click to toggle source

@private Loads a configuration file.

@return [Hash]

# File lib/google/cloud/gemserver/configuration.rb, line 430
def load_config
  extract_config env
end
on_appengine() click to toggle source

@private Determines if the gemserver is running on Google App Engine.

@return [boolean]

# File lib/google/cloud/gemserver/configuration.rb, line 482
def on_appengine
  !ENV["GEMSERVER_ON_APPENGINE"].nil?
end
prod_config() click to toggle source

@private The default config.yml configuration formatted in YAML used by the gemserver in the production environment.

@return [String]

# File lib/google/cloud/gemserver/configuration.rb, line 379
def prod_config
  DEFAULT_CONFIG.deep_merge(extract_config("production")).to_yaml
end
suffix() click to toggle source

@private Determines which configuration file to read based on the environment.

@return [String]

# File lib/google/cloud/gemserver/configuration.rb, line 338
def suffix
  if env == "dev"
    "dev_config.yml"
  elsif env == "test"
    "test_config.yml"
  else
    "config.yml"
  end
end
test_config() click to toggle source

@private The default test_config.yml configuration formatted in YAML used by the gemserver in the test environment.

@return [String]

# File lib/google/cloud/gemserver/configuration.rb, line 397
def test_config
  DEFAULT_TEST_CONFIG.deep_merge(extract_config("test")).to_yaml
end
write_app() click to toggle source

@private Writes the current app configuration object in YAML format to the app configuration file.

# File lib/google/cloud/gemserver/configuration.rb, line 464
def write_app
  File.open(app_path, "w") { |f| YAML.dump @app, f }
end
write_config() click to toggle source

@private Writes the current Configuration object in YAML format to the relevant configuration file (based on environment) and updates app.yaml accordingly.

# File lib/google/cloud/gemserver/configuration.rb, line 447
def write_config
  db_key = env + "_" + CONFIG_DB_PREFIX + "_"
  key = CONFIG_PREFIX + "_"
  db = @config[:db_connection_options]
  non_db = @config.reject { |k, v| k == :db_connection_options }
  formatted_db = db.map { |k, v| [db_key + k.to_s, v] }.to_h
  formatted_non_db = non_db.map { |k, v| [key + k.to_s, v] }.to_h
  @app["env_variables"] = @app["env_variables"].merge(
    formatted_db.merge(formatted_non_db)
  )
  File.open(config_path, "w") { |f| YAML.dump @config, f }
  write_app
end
write_file(path, content, check_existence = false) click to toggle source

@private Writes a given file to a given path.

@param [String] path The path to write the file.

@param [String] content The content to be written to the file.

@param [boolean] check_existence If true, the file is not overwritten if it already exists. Optional.

# File lib/google/cloud/gemserver/configuration.rb, line 357
def write_file path, content, check_existence = false
  if check_existence
    return if File.exist? path
  end
  File.open(path, "w") do |f|
    f.write content
  end
end