Class: VScripts::Commands::Identify
- Inherits:
-
Object
- Object
- VScripts::Commands::Identify
- Defined in:
- lib/vscripts/commands/identify.rb
Overview
Identify Class
Constant Summary collapse
- USAGE =
Shows help
<<-EOS This command creates a themed host name and fully qualified domain name for the server, using AWS EC2 tags. The default theme is `Group-Role-#` which means that the command collects the value of the `Group` and the `Role` AWS EC2 tags (if they are associated with the instance). Additionally, the value of the `Domain` tag is also collected so the resulting new host name will be `MYGROUP-MYROLE-#.MYDOMAIN`. These tags can be any existing EC2 tags. `#` is used as a placeholder for a number. This number starts at 1, and, in case other similarly named instances exist in the current AWS account, it will be incremented accordingly. Once a new host name is composed, both `/etc/hostname` and `/etc/hosts` are modified on the local instance and a new `Name` EC2 tag is created and associated with the current instance. If a ***--host*** argument is provided it will override the default theme. *DOMAIN* is still looked up. If a ***--domain*** argument is provided it will override the default domain. USAGE: $ vscripts identify MyGroup-MyRole-1.Example.tld $ vscripts identify --ec2-tag-theme NAME-# MyName-1.Example.tld` $ vscripts identify --host myhost --domain example.com myhost.example.com` OPTIONS: EOS
Instance Attribute Summary collapse
-
#arguments ⇒ Array
readonly
The command specific arguments.
-
#domain ⇒ String
readonly
The domain name.
-
#host ⇒ String
readonly
The host name.
-
#theme ⇒ String
readonly
The theme.
Instance Method Summary collapse
-
#cli ⇒ Hash
The command line arguments.
-
#execute ⇒ Object
Executes the command.
-
#incremented_hostname ⇒ String
Increments the new hostname if it finds similar ones.
-
#initialize(argv = []) ⇒ Identify
constructor
Loads the Identify command.
-
#map2tags ⇒ Array
Lists values corresponding to each tag specified in the theme.
-
#new_domain ⇒ String
The value of the command line ‘–domain` argument, or the value of the ’Domain’ EC2 tag or the local domain name.
-
#new_fqdn ⇒ String
The fully qualified domain name.
-
#new_hostname ⇒ String
The command line ‘–host` argument, the themed hostname or the existing local hostname.
-
#parser ⇒ Object
Specifies command line options This method smells of :reek:TooManyStatements but ignores them.
-
#set_hostname ⇒ Object
Modifies the host name.
-
#set_name_tag ⇒ Object
Modifies the ‘Name’ tag.
-
#theme_elements ⇒ Array
Splits theme into elements.
-
#themed_host_name ⇒ String
Composes the host name based on found tags.
-
#update_hosts ⇒ Object
Modifies the hosts file.
Methods included from Util::LocalSystem
#ensure_file_content, #ensure_file_dir, #external_dns, #hostname_path, #hosts_path, #local_domain_name, #local_fqdn, #local_host_name, #write_file
Methods included from AWS::Metadata
#check_instance, #ec2_instance?, #instance_id, #metadata_url, #public_hostname, #region, #zone
Methods included from AWS::EC2
#all_tags, #all_tags_hash, #create_tag, #ec2, #functional_instances, #instance, #name, #named_instances, #similar_instances, #tag, #tags_without
Constructor Details
#initialize(argv = []) ⇒ Identify
Loads the Identify command
55 56 57 58 59 60 |
# File 'lib/vscripts/commands/identify.rb', line 55 def initialize(argv = []) @arguments ||= argv @theme ||= cli.ec2_tag_theme @host ||= cli.host @domain ||= cli.domain end |
Instance Attribute Details
#arguments ⇒ Array (readonly)
Returns the command specific arguments.
51 52 53 |
# File 'lib/vscripts/commands/identify.rb', line 51 def arguments @arguments end |
#domain ⇒ String (readonly)
Returns the domain name.
49 50 51 |
# File 'lib/vscripts/commands/identify.rb', line 49 def domain @domain end |
#host ⇒ String (readonly)
Returns the host name.
47 48 49 |
# File 'lib/vscripts/commands/identify.rb', line 47 def host @host end |
#theme ⇒ String (readonly)
Returns the theme.
45 46 47 |
# File 'lib/vscripts/commands/identify.rb', line 45 def theme @theme end |
Instance Method Details
#cli ⇒ Hash
Returns the command line arguments.
76 77 78 79 80 |
# File 'lib/vscripts/commands/identify.rb', line 76 def cli @cli ||= Trollop.with_standard_exception_handling parser do parser.parse arguments end end |
#execute ⇒ Object
Executes the command
163 164 165 166 167 168 |
# File 'lib/vscripts/commands/identify.rb', line 163 def execute puts 'EC2 tag not changed!' unless set_name_tag puts 'Hostname not changed!' unless set_hostname puts 'Hosts file not changed!' unless update_hosts puts 'Done.' end |
#incremented_hostname ⇒ String
Increments the new hostname if it finds similar ones
104 105 106 107 108 109 110 111 |
# File 'lib/vscripts/commands/identify.rb', line 104 def incremented_hostname number = 1 while similar_instances.include? "#{themed_host_name}.#{domain}" .sub(/#/, "#{number}") number += 1 end "#{themed_host_name}".sub(/#/, "#{number}") end |
#map2tags ⇒ Array
Lists values corresponding to each tag specified in the theme
90 91 92 93 94 |
# File 'lib/vscripts/commands/identify.rb', line 90 def theme_elements.map do |element| element == '#' ? element : tag(element) end end |
#new_domain ⇒ String
The value of the command line ‘–domain` argument, or the value of the ’Domain’ EC2 tag or the local domain name.
123 124 125 |
# File 'lib/vscripts/commands/identify.rb', line 123 def new_domain domain || tag('Domain') || local_domain_name end |
#new_fqdn ⇒ String
Returns the fully qualified domain name.
128 129 130 |
# File 'lib/vscripts/commands/identify.rb', line 128 def new_fqdn "#{new_hostname}.#{new_domain}" end |
#new_hostname ⇒ String
The command line ‘–host` argument, the themed hostname or the existing local hostname
116 117 118 |
# File 'lib/vscripts/commands/identify.rb', line 116 def new_hostname host || incremented_hostname || local_host_name end |
#parser ⇒ Object
Specifies command line options This method smells of :reek:TooManyStatements but ignores them
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/vscripts/commands/identify.rb', line 64 def parser Trollop::Parser.new do USAGE opt :ec2_tag_theme, 'Theme (default: Group-Role-#)', type: :string, default: 'Group-Role-#', short: '-t' opt :host, 'Host name', type: :string, short: '-n' opt :domain, 'Domain name', type: :string, short: '-d' stop_on_unknown end end |
#set_hostname ⇒ Object
Modifies the host name
140 141 142 143 144 145 146 |
# File 'lib/vscripts/commands/identify.rb', line 140 def set_hostname return unless File.exist?(hostname_path) return if File.read(hostname_path).strip == new_hostname puts "Setting local hostname (#{new_hostname})..." write_file(hostname_path, new_hostname) `hostname -F /etc/hostname` end |
#set_name_tag ⇒ Object
Modifies the ‘Name’ tag
133 134 135 136 137 |
# File 'lib/vscripts/commands/identify.rb', line 133 def set_name_tag return if tag('Name') == new_fqdn puts "Setting name tag to \"#{new_fqdn}\"..." create_tag(instance, 'Name', value: new_fqdn) end |
#theme_elements ⇒ Array
Splits theme into elements
84 85 86 |
# File 'lib/vscripts/commands/identify.rb', line 84 def theme_elements theme.split('-') end |
#themed_host_name ⇒ String
Composes the host name based on found tags
98 99 100 |
# File 'lib/vscripts/commands/identify.rb', line 98 def themed_host_name .compact.join('-') end |
#update_hosts ⇒ Object
Modifies the hosts file
149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/vscripts/commands/identify.rb', line 149 def update_hosts return unless File.exist?(hosts_path) return if File.readlines(hosts_path) .grep(/#{new_fqdn} #{new_hostname}/) .any? hosts_body = File.read(hosts_path).gsub( /^127\.0\.0\.1.*/, "127\.0\.0\.1 #{new_fqdn} #{new_hostname} localhost" ) puts "Adding \"#{new_fqdn}\" to #{hosts_path}..." write_file(hosts_path, hosts_body) end |