Class: ConvoxInstaller::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/convox_installer/config.rb

Constant Summary collapse

CONFIG_FILE =
File.expand_path('./.installer_config.json').freeze
DEFAULT_PROMPTS =
[
  {
    key: :stack_name,
    title: 'Convox Stack Name',
    prompt: 'Please enter a name for your Convox installation',
    default: 'convox'
  },
  {
    key: :aws_region,
    title: 'AWS Region',
    default: 'us-east-1'
  },
  {
    key: :instance_type,
    title: 'EC2 Instance Type',
    default: 't3.medium'
  },
  {
    section: 'Admin AWS Credentials'
  },
  {
    key: :aws_access_key_id,
    title: 'AWS Access Key ID'
  },
  {
    key: :aws_secret_access_key,
    title: 'AWS Secret Access Key'
  },
  # Short random ID used to ensure that resources are always unique
  {
    key: :random_id,
    value: -> { SecureRandom.hex(4) },
    hidden: true
  }
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/convox_installer/config.rb', line 50

def initialize(options = {})
  @logger = Logger.new($stdout)
  logger.level = options[:log_level] || Logger::INFO

  self.prompts = options[:prompts] || DEFAULT_PROMPTS
  self.config = {}
  load_config_from_file
  load_config_from_env
  self.config = config.merge((options[:config] || {}).symbolize_keys)

  self.highline = options[:highline] || HighLine.new
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



12
13
14
# File 'lib/convox_installer/config.rb', line 12

def config
  @config
end

#highlineObject

Returns the value of attribute highline.



12
13
14
# File 'lib/convox_installer/config.rb', line 12

def highline
  @highline
end

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/convox_installer/config.rb', line 12

def logger
  @logger
end

#promptsObject

Returns the value of attribute prompts.



12
13
14
# File 'lib/convox_installer/config.rb', line 12

def prompts
  @prompts
end

Class Method Details

.config_file_exists?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/convox_installer/config.rb', line 122

def self.config_file_exists?
  File.exist?(CONFIG_FILE)
end

.read_config_fileObject



126
127
128
# File 'lib/convox_installer/config.rb', line 126

def self.read_config_file
  File.read(CONFIG_FILE)
end

Instance Method Details

#config_keysObject



63
64
65
# File 'lib/convox_installer/config.rb', line 63

def config_keys
  prompts.map { |prompt| prompt[:key] }.compact.map(&:to_sym)
end

#prompt_for_configObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/convox_installer/config.rb', line 67

def prompt_for_config
  loop do
    prompts.each do |prompt|
      if prompt[:section]
        highline.say "\n#{prompt[:section]}"
        highline.say "============================================\n\n"
      end
      next unless prompt[:key]

      ask_prompt(prompt)
    end

    show_config_summary

    @completed_prompt = true

    highline.say 'Please double check all of these configuration details.'

    break if ENV['AUTOSTART_CONVOX_INSTALLATION']

    agree = highline.agree(
      'Would you like to start the Convox installation?' \
      " (press 'n' to correct any settings)"
    )
    break if agree

    highline.say "\n"
  end

  config
end

#show_config_summaryObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/convox_installer/config.rb', line 99

def show_config_summary
  highline.say "\n============================================"
  highline.say '                 SUMMARY'
  highline.say "============================================\n\n"

  config_titles = prompts.map do |prompt|
    prompt[:title] || prompt[:key]
  end.compact
  max = config_titles.map(&:length).max

  prompts.each do |prompt|
    next if !prompt[:key] || prompt[:hidden]

    value = config[prompt[:key]]
    title = prompt[:title] || prompt[:key]
    padded_key = "#{title}:".ljust(max + 3)
    highline.say "    #{padded_key} #{value}"
  end
  highline.say "\nWe've saved your configuration to: #{CONFIG_FILE}"
  highline.say 'If anything goes wrong during the installation, ' \
               "you can restart the script to reload the config and continue.\n\n"
end