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.

    EXAMPLES:
    $ 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_file, #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, #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



54
55
56
57
58
59
# File 'lib/vscripts/commands/identify.rb', line 54

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



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

def arguments
  @arguments
end

#domainString (readonly)

Returns the domain name.

Returns:

  • (String)

    the domain name



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

def domain
  @domain
end

#hostString (readonly)

Returns the host name.

Returns:

  • (String)

    the host name



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

def host
  @host
end

#themeString (readonly)

Returns the theme.

Returns:

  • (String)

    the theme



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

def theme
  @theme
end

Instance Method Details

#cliHash

Returns the command line arguments.

Returns:

  • (Hash)

    the command line arguments



75
76
77
78
79
# File 'lib/vscripts/commands/identify.rb', line 75

def cli
  @cli ||= Trollop.with_standard_exception_handling parser do
    parser.parse arguments
  end
end

#executeObject

Executes the command



160
161
162
163
164
165
# File 'lib/vscripts/commands/identify.rb', line 160

def execute
  set_name_tag
  set_hostname
  update_hosts
  puts 'Done.'
end

#incremented_hostnameString

Increments the new hostname if it finds similar ones

Returns:

  • (String)

    the incremented host name



103
104
105
106
107
108
109
110
# File 'lib/vscripts/commands/identify.rb', line 103

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



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

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



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

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



127
128
129
# File 'lib/vscripts/commands/identify.rb', line 127

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



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

def new_hostname
  host || incremented_hostname || local_host_name
end

#parserObject

Specifies command line options This method smells of :reek:TooManyStatements but ignores them



63
64
65
66
67
68
69
70
71
72
# File 'lib/vscripts/commands/identify.rb', line 63

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



139
140
141
142
143
144
# File 'lib/vscripts/commands/identify.rb', line 139

def set_hostname
  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



132
133
134
135
136
# File 'lib/vscripts/commands/identify.rb', line 132

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



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

def theme_elements
  theme.split('-')
end

#themed_host_nameString

Composes the host name based on found tags

Returns:

  • (String)

    the new host name



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

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

#update_hostsObject

Modifies the hosts file



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/vscripts/commands/identify.rb', line 147

def update_hosts
  return if File.readlines(hosts_path)
    .grep(/#{new_fqdn} #{new_hostname}/)
    .any?
  hosts_body = hosts_file.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