class EC2Bootstrap::Instance
Constants
- REQUIRED_KNIFE_EC2_FLAGS
Attributes
knife_ec2_flags[RW]
name[RW]
Public Class Methods
new(instance_name:, knife_ec2_flags:, logger:, dryrun:, domain: nil, json_attributes_file:nil, image: nil, cloud_config: nil)
click to toggle source
# File lib/ec2_bootstrap/instance.rb, line 12 def initialize(instance_name:, knife_ec2_flags:, logger:, dryrun:, domain: nil, json_attributes_file:nil, image: nil, cloud_config: nil) @name = instance_name @logger = logger @logger.debug("Instance name: #{@name}") @dryrun = dryrun @json_attributes_file = json_attributes_file @image = image @domain = domain $AWS_TAGS.append("Name=#{@name}") @knife_ec2_flags = build_knife_ec2_flags_hash(knife_ec2_flags, cloud_config) end
Public Instance Methods
build_knife_ec2_flags_hash(knife_ec2_flags, cloud_config)
click to toggle source
# File lib/ec2_bootstrap/instance.rb, line 28 def build_knife_ec2_flags_hash(knife_ec2_flags, cloud_config) knife_ec2_flags['json-attributes'] = "'#{self.load_json_attributes(@json_attributes_file)}'" if @json_attributes_file and not knife_ec2_flags['json-attributes'] knife_ec2_flags['user-data'] = self.generate_cloud_init(cloud_config) if cloud_config and not knife_ec2_flags['user-data'] knife_ec2_flags['image'] = @image unless knife_ec2_flags['image'] $AWS_TAGS = $AWS_TAGS + knife_ec2_flags['tags'] if knife_ec2_flags['tags'] knife_ec2_flags.delete('tags') if knife_ec2_flags['tags'] additional_knife_flags = { 'node-name' => @name, 'no-host-key-verify' => "" } knife_flags_hash = knife_ec2_flags.merge(additional_knife_flags) self.validate_knife_flags(knife_flags_hash) return knife_flags_hash end
format_knife_shell_command()
click to toggle source
# File lib/ec2_bootstrap/instance.rb, line 64 def format_knife_shell_command prefix = 'knife ec2 server create ' aws_tags_string = $AWS_TAGS.map {|key | ['--aws-tag ' + key]}.flatten.compact knife_flag_array = @knife_ec2_flags.map {|key, value| ['--' + key, value]}.flatten.compact @logger.info(prefix + knife_flag_array.join(' ') + aws_tags_string.join(' ')) return prefix + knife_flag_array.join(' ') + aws_tags_string.join(' ') end
generate_cloud_init(cloud_config)
click to toggle source
# File lib/ec2_bootstrap/instance.rb, line 72 def generate_cloud_init(cloud_config) 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.info(msg) else self.write_cloud_config_to_file(cloud_config_path, formatted_cloud_config) @logger.info("Wrote cloud config to #{cloud_config_path}.") end return cloud_config_path end
load_json_attributes(file_path)
click to toggle source
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.
# File lib/ec2_bootstrap/instance.rb, line 53 def load_json_attributes(file_path) return JSON.dump(JSON.load(File.read(file_path))) end
validate_knife_flags(given_knife_flags)
click to toggle source
Ensure that all REQUIRED_EC2_FLAGS have values other than nil.
# File lib/ec2_bootstrap/instance.rb, line 58 def validate_knife_flags(given_knife_flags) missing_flags = REQUIRED_KNIFE_EC2_FLAGS.select {|flag| given_knife_flags[flag].nil? } raise KeyError, "Instance #{@name} is missing one or more required flags. Missing flags are: #{missing_flags}." unless missing_flags.empty? return true end
write_cloud_config_to_file(path, contents)
click to toggle source
# File lib/ec2_bootstrap/instance.rb, line 90 def write_cloud_config_to_file(path, contents) File.open(path, 'w') {|f| f.write(contents)} end