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").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",
  },
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/convox_installer/config.rb', line 44

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

Instance Method Details

#config_keysObject



57
58
59
# File 'lib/convox_installer/config.rb', line 57

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

#prompt_for_configObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/convox_installer/config.rb', line 61

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."

    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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/convox_installer/config.rb', line 90

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