Class: EC2Bootstrap::Instance
- Inherits:
-
Object
- Object
- EC2Bootstrap::Instance
- Defined in:
- lib/ec2_bootstrap/instance.rb
Constant Summary collapse
- REQUIRED_KNIFE_EC2_FLAGS =
['image', 'private-ip-address']
Instance Attribute Summary collapse
-
#knife_ec2_flags ⇒ Object
Returns the value of attribute knife_ec2_flags.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #build_knife_ec2_flags_hash(knife_ec2_flags, cloud_config) ⇒ Object
- #format_knife_shell_command ⇒ Object
- #generate_cloud_init(cloud_config) ⇒ Object
-
#initialize(instance_name:, knife_ec2_flags:, logger:, dryrun:, domain: nil, json_attributes_file: nil, image: nil, cloud_config: nil) ⇒ Instance
constructor
A new instance of Instance.
-
#load_json_attributes(file_path) ⇒ Object
Load the JSON and then dump it back out to ensure it’s valid JSON.
-
#validate_knife_flags(given_knife_flags) ⇒ Object
Ensure that all REQUIRED_EC2_FLAGS have values other than nil.
- #write_cloud_config_to_file(path, contents) ⇒ Object
Constructor Details
#initialize(instance_name:, knife_ec2_flags:, logger:, dryrun:, domain: nil, json_attributes_file: nil, image: nil, cloud_config: nil) ⇒ Instance
Returns a new instance of Instance.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# 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 |
Instance Attribute Details
#knife_ec2_flags ⇒ Object
Returns the value of attribute knife_ec2_flags.
9 10 11 |
# File 'lib/ec2_bootstrap/instance.rb', line 9 def knife_ec2_flags @knife_ec2_flags end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/ec2_bootstrap/instance.rb', line 8 def name @name end |
Instance Method Details
#build_knife_ec2_flags_hash(knife_ec2_flags, cloud_config) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# 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 ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/ec2_bootstrap/instance.rb', line 64 def format_knife_shell_command prefix = 'knife ec2 server create ' = $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(' ') + .join(' ')) return prefix + knife_flag_array.join(' ') + .join(' ') end |
#generate_cloud_init(cloud_config) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# 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) ⇒ 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.
53 54 55 |
# 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) ⇒ Object
Ensure that all REQUIRED_EC2_FLAGS have values other than nil.
58 59 60 61 62 |
# 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) ⇒ Object
90 91 92 |
# 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 |