Class: EC2Bootstrap::Instance

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance_name:, knife_ec2_flags:, logger:, domain: nil, json_attributes_file: nil) ⇒ Instance

Returns a new instance of Instance.



9
10
11
12
13
14
15
# File 'lib/ec2_bootstrap/instance.rb', line 9

def initialize(instance_name:, knife_ec2_flags:, logger:, domain: nil, json_attributes_file:nil)
	@name = instance_name
	@json_attributes_file = json_attributes_file
	@knife_ec2_flags = build_knife_ec2_flags_hash(knife_ec2_flags)
	@logger = logger
	@domain = domain
end

Instance Attribute Details

#knife_ec2_flagsObject

Returns the value of attribute knife_ec2_flags.



7
8
9
# File 'lib/ec2_bootstrap/instance.rb', line 7

def knife_ec2_flags
  @knife_ec2_flags
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/ec2_bootstrap/instance.rb', line 6

def name
  @name
end

Instance Method Details

#build_knife_ec2_flags_hash(knife_ec2_flags) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/ec2_bootstrap/instance.rb', line 17

def build_knife_ec2_flags_hash(knife_ec2_flags)
	knife_ec2_flags['json-attributes'] = "'#{self.load_json_attributes(@json_attributes_file)}'" if @json_attributes_file

	additional_knife_flags = {
		'node-name' => @name,
		'tags' => "Name=#{@name}"
	}

	return knife_ec2_flags.merge(additional_knife_flags)
end

#format_knife_shell_commandObject



35
36
37
38
39
# File 'lib/ec2_bootstrap/instance.rb', line 35

def format_knife_shell_command
	prefix = 'knife ec2 server create '
	knife_flag_array = @knife_ec2_flags.map {|key, value| ['--' + key, value]}.flatten.compact
	return prefix + knife_flag_array.join(' ')
end

#generate_cloud_config(cloud_config, dryrun) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ec2_bootstrap/instance.rb', line 41

def generate_cloud_config(cloud_config, dryrun)
	cloud_config['hostname'] = @name
	cloud_config['fqdn'] = "#{@name}.#{@domain}" if @domain

	formatted_cloud_config = cloud_config.to_yaml.gsub('---', '#cloud-config')
	cloud_config_path = "cloud_config_#{@name}.txt"

	if dryrun
		msg = "If this weren't a dry run, I would write the following contents to #{cloud_config_path}:\n#{formatted_cloud_config}"
		@logger.debug(msg)
	else
		self.write_cloud_config_to_file(cloud_config_path, formatted_cloud_config)
		@logger.debug("Wrote cloud config to #{cloud_config_path}.")
	end

	@knife_ec2_flags['user-data'] = cloud_config_path
end

#load_json_attributes(file_path) ⇒ Object

Load the JSON and then dump it back out to ensure it’s valid JSON. Also makes the JSON easier to read when printing out the command in verbose mode by removing all newlines.



31
32
33
# File 'lib/ec2_bootstrap/instance.rb', line 31

def load_json_attributes(file_path)
	return JSON.dump(JSON.load(File.read(file_path)))
end

#write_cloud_config_to_file(path, contents) ⇒ Object



59
60
61
# File 'lib/ec2_bootstrap/instance.rb', line 59

def write_cloud_config_to_file(path, contents)
	File.open(path, 'w') {|f| f.write(contents)}
end