Class: Pfm::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/iapi-idlc-sdk-pfm/settings.rb

Overview

PFM Settings

Top level settings defined and loaded here. This class loads the user config file located at .pfm/config

AWS_REGION default -> ‘us-east-1’

  • The AWS region to work in. This will eventually become the AWS_REGION environment variable and can be used with other AWS API operations.

RUBOCOP_RULES_FILE default -> ‘.rubocop.yml’

  • Set the path to the global .rubocop.yml rules file. This file will be used for all server builds when running syntax checks.

FOODCRITIC_RULES_FILE default -> ‘.foodcritic’

  • Set the path to the global .foodcritic rules file. This file will be used for all server builds when running semantics checks.

BUILD_BASE_DIR default -> ‘builds’

  • Set the root path to the builds directory. This should be where top level server builds are located. Ex:

    .
    ├── builds
    │   ├── app-axpdb
    │   └── app-axpwa
    

INF_BASE_DIR default -> ‘inf’

  • Set the path to the top level infrastructure directory. Ex:

    .
    ├── inf
    │   ├── env
    │   ├── tasks
    │   └── tf
    

PUBLISH_BUCKET default -> ”

  • The top level bucket where deployment packages are stored in S3.

PUBLISH_PREFIX default -> ”

  • The prefix to the deployment package storage location. Ex: s3://PUBLISH_BUCKET/PUBLISH_PREFIX/package.name

PACKER_VERSION <em>default -> ‘default from packer/binary’

  • The version of Hashicorp Packer to use for builds

TERRAFORM_VERSION <em>default -> ‘default from terraform/binary’

  • The version of Hashicorp Terraform to use for deployments

Note that if you change these settings from the default, be sure you commit the .pfm/config file.

Example Configuration File

An example .pfm/config file might look like this:

---
AWS_REGION: 'us-east-1'
RUBOCOP_RULES_FILE: '.pfm/.rubocop.yml'
FOODCRITIC_RULES_FILE: '.pfm/.foodcritic'
BUILD_BASE_DIR: 'builds'
INF_BASE_DIR: 'inf'
PUBLISH_BUCKET: 'dev-publish'
PUBLISH_PREFIX: 'axp'
PACKER_VERSION: '1.0.4'
TERRAFORM_VERSION: '0.8.7'

Defined Under Namespace

Classes: Setting

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expand = false) ⇒ Settings

Returns a new instance of Settings.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/iapi-idlc-sdk-pfm/settings.rb', line 95

def initialize(expand = false)
  @run_dir = Dir.pwd
  @config_directory = config_dir
  @expand = expand

  @settings = {}

  # Required
  @settings['AWS_REGION'] = Setting.new(ENV['AWS_REGION'], true)
  @settings['RUBOCOP_RULES_FILE'] = Setting.new('.rubocop.yml', true)
  @settings['FOODCRITIC_RULES_FILE'] = Setting.new('.foodcritic', true)
  @settings['BUILD_BASE_DIR'] = Setting.new('builds', true)
  @settings['INF_BASE_DIR'] = Setting.new('inf', true)
  @settings['PACKER_VERSION'] = Setting.new(::Packer::Binary.config.version, true)
  @settings['TERRAFORM_VERSION'] = Setting.new(::Terraform::Binary.config.version, true)

  # Optional Defaults
  @settings['PUBLISH_BUCKET'] = Setting.new('')
  @settings['PUBLISH_PREFIX'] = Setting.new('')

  load_config
end

Instance Attribute Details

#config_directoryObject (readonly)

Returns the value of attribute config_directory.



92
93
94
# File 'lib/iapi-idlc-sdk-pfm/settings.rb', line 92

def config_directory
  @config_directory
end

#run_dirObject (readonly)

Returns the value of attribute run_dir.



93
94
95
# File 'lib/iapi-idlc-sdk-pfm/settings.rb', line 93

def run_dir
  @run_dir
end

#settingsMap (readonly)

Returns the map of Pfm::Settings::Setting objects

Returns:

  • (Map)

    returns the map of Pfm::Settings::Setting objects



91
92
93
# File 'lib/iapi-idlc-sdk-pfm/settings.rb', line 91

def settings
  @settings
end

Instance Method Details

#load_configObject

load the config file and resolve file paths if set



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/iapi-idlc-sdk-pfm/settings.rb', line 119

def load_config
  if config_exists?
    require 'yaml'
    YAML.load_file(config_file).each do |key, value|
      msg("WARNING: unrecognized config key: '#{key}'") unless @settings.key? key
      next unless @settings.key? key

      required = @settings[key].required?
      @settings[key] = Setting.new(value, required)
    end

    # Also load each setting into the environment
    @settings.each { |key, setting| ENV[key] = setting.value }

    # Resolve paths if set
    resolve_paths if @expand
  else
    create_config
    load_config
  end
end

#save_config(settings) ⇒ Object

Saves a new configuration to disk

Parameters:



143
144
145
146
147
# File 'lib/iapi-idlc-sdk-pfm/settings.rb', line 143

def save_config(settings)
  @settings = settings

  write_config
end

#settings_stringsMap<String>

returns a simple map of Pfm::Settings.settings => Pfm::Settings.setting.to_s

Returns:

  • (Map<String>)


151
152
153
154
155
156
157
158
# File 'lib/iapi-idlc-sdk-pfm/settings.rb', line 151

def settings_strings
  map = {}
  @settings.each do |key, setting|
    map[key] = setting.to_s
  end

  map
end