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 =

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

Returns a new instance of Identify.



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

def initialize(argv = [])
  @arguments ||= argv
  @theme     ||= cli.ec2_tag_theme
  @host      ||= cli.host
  @domain    ||= cli.domain
end

Instance Attribute Details

#argumentsArray (readonly)

Returns Command specific arguments.

Returns:

  • (Array)

    Command specific arguments



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

def arguments
  @arguments
end

#domainString (readonly)

Returns Domain name.

Returns:

  • (String)

    Domain name



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

def domain
  @domain
end

#hostString (readonly)

Returns Host name.

Returns:

  • (String)

    Host name



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

def host
  @host
end

#themeString (readonly)

Returns Theme string.

Returns:

  • (String)

    Theme string



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

def theme
  @theme
end

Instance Method Details

#cliObject

Parses command line arguments



73
74
75
76
77
# File 'lib/vscripts/commands/identify.rb', line 73

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

#executeObject

Executes the command



151
152
153
154
155
156
# File 'lib/vscripts/commands/identify.rb', line 151

def execute
  set_name_tag
  set_hostname
  update_hosts
  puts 'Done.'
end

#incremented_hostnameString

Returns The incremented host name.

Returns:

  • (String)

    The incremented host name



97
98
99
100
101
102
103
104
# File 'lib/vscripts/commands/identify.rb', line 97

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

Returns An array of values for each tag specified in the theme.

Returns:

  • (Array)

    An array of values for each tag specified in the theme



85
86
87
88
89
# File 'lib/vscripts/commands/identify.rb', line 85

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

#new_domainString

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

Returns:

  • (String)

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



113
114
115
# File 'lib/vscripts/commands/identify.rb', line 113

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



118
119
120
# File 'lib/vscripts/commands/identify.rb', line 118

def new_fqdn
  "#{new_hostname}.#{new_domain}"
end

#new_hostnameString

Returns The command line –host argument or the themed hostname.

Returns:

  • (String)

    The command line –host argument or the themed hostname



107
108
109
# File 'lib/vscripts/commands/identify.rb', line 107

def new_hostname
  host || incremented_hostname || local_host_name
end

#parserObject

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



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

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

Modify the host name



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

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

Modify the ‘Name’ tag



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

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

Returns Splits theme into elements.

Returns:

  • (Array)

    Splits theme into elements



80
81
82
# File 'lib/vscripts/commands/identify.rb', line 80

def theme_elements
  theme.split('-')
end

#themed_host_nameString

Returns Compose host name based on found tags.

Returns:

  • (String)

    Compose host name based on found tags



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

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

#update_hostsObject

Modify the hosts file



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/vscripts/commands/identify.rb', line 138

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