Class: EC2Bootstrap

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2_bootstrap.rb,
lib/ec2_bootstrap/instance.rb

Defined Under Namespace

Classes: Instance

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, dryrun = true, verbose = false) ⇒ EC2Bootstrap

Returns a new instance of EC2Bootstrap.



14
15
16
17
18
19
20
21
# File 'lib/ec2_bootstrap.rb', line 14

def initialize(config, dryrun=true, verbose=false)
	@logger = Logger.new(STDOUT)
	verbose ? @logger.level = Logger::DEBUG : @logger.level = Logger::INFO

	@cloud_config = config['cloud_config']
	@instances = self.make_instances(config['instances'])
	@dryrun = dryrun
end

Instance Attribute Details

#cloud_configObject

Returns the value of attribute cloud_config.



10
11
12
# File 'lib/ec2_bootstrap.rb', line 10

def cloud_config
  @cloud_config
end

#dryrunObject

Returns the value of attribute dryrun.



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

def dryrun
  @dryrun
end

#instancesObject

Returns the value of attribute instances.



11
12
13
# File 'lib/ec2_bootstrap.rb', line 11

def instances
  @instances
end

Class Method Details

.from_config(config, *args) ⇒ Object

Raises:

  • (KeyError)


23
24
25
26
27
28
29
30
# File 'lib/ec2_bootstrap.rb', line 23

def self.from_config(config, *args)
	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)
	config['instances'] = 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
36
# File 'lib/ec2_bootstrap.rb', line 32

def self.from_config_file(config_path, *args)
	config = YAML.load(File.read(config_path))

	self.from_config(config, *args)
end

Instance Method Details

#create_instancesObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ec2_bootstrap.rb', line 46

def create_instances
	@logger.debug("This was a dry run. No EC2 instances were created.") if @dryrun

	@instances.each do |instance|
		@logger.debug("Instance name: #{instance.name}")

		instance.generate_cloud_config(@cloud_config, @dryrun) if @cloud_config

		knife_shell_command = instance.format_knife_shell_command
		@logger.debug("Knife shell command:\n#{knife_shell_command}")
		
		unless @dryrun
			status = self.shell_out_command(knife_shell_command)
			return status
		end
	end
end

#instance_classObject



42
43
44
# File 'lib/ec2_bootstrap.rb', line 42

def instance_class
	return Instance
end

#make_instances(instances_config) ⇒ Object



38
39
40
# File 'lib/ec2_bootstrap.rb', line 38

def make_instances(instances_config)
	return instances_config.map {|i| self.instance_class.new(i.merge(logger: @logger))}
end

#shell_out_command(command) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ec2_bootstrap.rb', line 64

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