Class: EC2Bootstrap
- Inherits:
-
Object
- Object
- EC2Bootstrap
- Defined in:
- lib/ec2_bootstrap.rb,
lib/ec2_bootstrap/ami.rb,
lib/ec2_bootstrap/instance.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#cloud_config ⇒ Object
Returns the value of attribute cloud_config.
-
#default_ami_config ⇒ Object
Returns the value of attribute default_ami_config.
-
#dryrun ⇒ Object
Returns the value of attribute dryrun.
-
#instances_config ⇒ Object
Returns the value of attribute instances_config.
Class Method Summary collapse
- .from_config(config, *args) ⇒ Object
- .from_config_file(config_path, *args) ⇒ Object
- .validate_config(config) ⇒ Object
Instance Method Summary collapse
- #ami_class ⇒ Object
- #create_instances ⇒ Object
-
#initialize(config, dryrun: true, verbose: false, logger: nil) ⇒ EC2Bootstrap
constructor
A new instance of EC2Bootstrap.
- #instance_class ⇒ Object
- #make_instances(default_image_id) ⇒ Object
- #new_logger(verbose) ⇒ Object
- #shell_out_command(command) ⇒ Object
Constructor Details
#initialize(config, dryrun: true, verbose: false, logger: nil) ⇒ EC2Bootstrap
Returns a new instance of EC2Bootstrap.
16 17 18 19 20 21 22 |
# File 'lib/ec2_bootstrap.rb', line 16 def initialize(config, dryrun: true, verbose: false, logger: nil) @logger = logger || self.new_logger(verbose) @dryrun = dryrun @cloud_config = config['cloud_config'] @instances_config = config['instances'] @default_ami_config = config['default_ami'] end |
Instance Attribute Details
#cloud_config ⇒ Object
Returns the value of attribute cloud_config.
12 13 14 |
# File 'lib/ec2_bootstrap.rb', line 12 def cloud_config @cloud_config end |
#default_ami_config ⇒ Object
Returns the value of attribute default_ami_config.
14 15 16 |
# File 'lib/ec2_bootstrap.rb', line 14 def default_ami_config @default_ami_config end |
#dryrun ⇒ Object
Returns the value of attribute dryrun.
11 12 13 |
# File 'lib/ec2_bootstrap.rb', line 11 def dryrun @dryrun end |
#instances_config ⇒ Object
Returns the value of attribute instances_config.
13 14 15 |
# File 'lib/ec2_bootstrap.rb', line 13 def instances_config @instances_config end |
Class Method Details
.from_config(config, *args) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/ec2_bootstrap.rb', line 24 def self.from_config(config, *args) self.validate_config(config) config['instances'].map! {|i| i.map {|key, value| [key.to_sym, value]}.to_h} return self.new(config, *args) end |
.from_config_file(config_path, *args) ⇒ Object
32 33 34 35 |
# File 'lib/ec2_bootstrap.rb', line 32 def self.from_config_file(config_path, *args) config = YAML.load(File.read(config_path)) return self.from_config(config, *args) end |
.validate_config(config) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ec2_bootstrap.rb', line 37 def self.validate_config(config) instances = config['instances'] raise KeyError, "Config file is missing 'instances' key." unless instances raise TypeError, "'instances' config must be an array of hashes." unless instances.is_a?(Array) && instances.first.is_a?(Hash) if config['default_ami'] raise TypeError, "'default_ami' config must be a hash." unless config['default_ami'].is_a?(Hash) end return true end |
Instance Method Details
#ami_class ⇒ Object
55 56 57 |
# File 'lib/ec2_bootstrap.rb', line 55 def ami_class return AMI end |
#create_instances ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ec2_bootstrap.rb', line 68 def create_instances @logger.info("This was a dry run. No EC2 instances were created.") if @dryrun default_image_id = @default_ami_config ? ami_class.from_config(@default_ami_config, @logger).find_newest_image_id : nil instances = self.make_instances(default_image_id) instances.each do |instance| knife_shell_command = instance.format_knife_shell_command @logger.debug("Knife shell command for #{instance.name}:\n#{knife_shell_command}") unless @dryrun status = self.shell_out_command(knife_shell_command) return status end end end |
#instance_class ⇒ Object
64 65 66 |
# File 'lib/ec2_bootstrap.rb', line 64 def instance_class return Instance end |
#make_instances(default_image_id) ⇒ Object
59 60 61 62 |
# File 'lib/ec2_bootstrap.rb', line 59 def make_instances(default_image_id) generic_args = {logger: @logger, image: default_image_id, dryrun: @dryrun, cloud_config: @cloud_config} return @instances_config.map {|i| self.instance_class.new(i.merge(generic_args))} end |
#new_logger(verbose) ⇒ Object
49 50 51 52 53 |
# File 'lib/ec2_bootstrap.rb', line 49 def new_logger(verbose) logger = Logger.new(STDOUT) verbose ? logger.level = Logger::DEBUG : logger.level = Logger::INFO return logger end |
#shell_out_command(command) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ec2_bootstrap.rb', line 85 def shell_out_command(command) STDOUT.sync = true Open3::popen2e(command) do |stdin, stdout_and_stderr, wait_thr| while (line = stdout_and_stderr.gets) do @logger.info(line.strip) end status = wait_thr.value @logger.info("status: #{status}") return status end end |