Class: VScripts::Commands::Identify

Inherits:
Object
  • Object
show all
Includes:
AWS, Util
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

Instance Method Summary collapse

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

Parameters:

  • argv (Array) (defaults to: [])

    the command specific arguments



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

#argumentsArray (readonly)

Returns the command specific arguments.

Returns:

  • (Array)

    the command specific arguments



51
52
53
# File 'lib/vscripts/commands/identify.rb', line 51

def arguments
  @arguments
end

#domainString (readonly)

Returns the domain name.

Returns:

  • (String)

    the domain name



49
50
51
# File 'lib/vscripts/commands/identify.rb', line 49

def domain
  @domain
end

#hostString (readonly)

Returns the host name.

Returns:

  • (String)

    the host name



47
48
49
# File 'lib/vscripts/commands/identify.rb', line 47

def host
  @host
end

#themeString (readonly)

Returns the theme.

Returns:

  • (String)

    the theme



45
46
47
# File 'lib/vscripts/commands/identify.rb', line 45

def theme
  @theme
end

Instance Method Details

#cliHash

Returns the command line arguments.

Returns:

  • (Hash)

    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

#executeObject

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_hostnameString

Increments the new hostname if it finds similar ones

Returns:

  • (String)

    the incremented host name



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

#map2tagsArray

Lists values corresponding to each tag specified in the theme

Returns:

  • (Array)

    the values list



90
91
92
93
94
# File 'lib/vscripts/commands/identify.rb', line 90

def map2tags
  theme_elements.map do |element|
    element == '#' ? element : tag(element)
  end
end

#new_domainString

The value of the command line ‘–domain` argument, or the value of the ’Domain’ EC2 tag or the local domain name.

Returns:

  • (String)

    the new domain name



123
124
125
# File 'lib/vscripts/commands/identify.rb', line 123

def new_domain
  domain || tag('Domain') || local_domain_name
end

#new_fqdnString

Returns the fully qualified domain name.

Returns:

  • (String)

    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_hostnameString

The command line ‘–host` argument, the themed hostname or the existing local hostname

Returns:

  • (String)

    the new hostname



116
117
118
# File 'lib/vscripts/commands/identify.rb', line 116

def new_hostname
  host || incremented_hostname || local_host_name
end

#parserObject

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
    banner 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_hostnameObject

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_tagObject

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_elementsArray

Splits theme into elements

Returns:

  • (Array)

    the theme elements



84
85
86
# File 'lib/vscripts/commands/identify.rb', line 84

def theme_elements
  theme.split('-')
end

#themed_host_nameString

Composes the host name based on found tags

Returns:

  • (String)

    the new host name



98
99
100
# File 'lib/vscripts/commands/identify.rb', line 98

def themed_host_name
  map2tags.compact.join('-')
end

#update_hostsObject

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