Class: VScripts::Commands::Identify

Inherits:
Object
  • Object
show all
Includes:
Util::LocalSystem
Defined in:
lib/vscripts/commands/identify.rb

Overview

Identify Class

Constant Summary collapse

USAGE =

HELP

"This command creates a themed host name and fully qualified domain name for\nthe server, using AWS EC2 tags. The default theme is `Group-Role-#` which means\nthat the command collects the value of the `Group` and the `Role` AWS EC2 tags\n(if they are associated with the instance). Additionally, the value of the\n`Domain` tag is also collected so the resulting new host name will be\n`MYGROUP-MYROLE-#.MYDOMAIN`.\nThese tags can be any existing EC2 tags. `#` is used as a placeholder for a\nnumber. This number starts at 1, and, in case other similarly named instances\nexist in the current AWS account, it will be incremented accordingly.\nOnce a new host name is composed, both `/etc/hostname` and `/etc/hosts` are\nmodified on the local instance and a new `Name` EC2 tag is created and\nassociated with the current instance.\n\n    If a ***--host*** argument is provided it will override the default theme.\n    *DOMAIN* is still looked up.\n    If a ***--domain*** argument is provided it will override the default\n    domain.\n\n    EXAMPLES:\n    $ vscripts identify\n    MyGroup-MyRole-1.Example.tld\n    $ vscripts identify --ec2-tag-theme NAME-#\n    MyName-1.Example.tld`\n    $ vscripts identify --host myhost --domain example.com\n    myhost.example.com`\n\n    Options:\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::LocalSystem

#checks, #ensure_file_content, #ensure_file_dir, #external_dns, #hostname_path, #hosts_file, #hosts_path, #local_domain_name, #local_fqdn, #local_host_name, #process_checks, #status_codes, #write_file

Constructor Details

#initialize(argv = []) ⇒ Identify

Returns a new instance of Identify.



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

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



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

def arguments
  @arguments
end

#domainString (readonly)

Returns Domain name.

Returns:

  • (String)

    Domain name



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

def domain
  @domain
end

#ec2Object (readonly)

Loads AWS::EC2 This method smells of :reek:UncommunicativeMethodName but ignores it



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

def ec2
  @ec2
end

#hostString (readonly)

Returns Host name.

Returns:

  • (String)

    Host name



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

def host
  @host
end

#themeString (readonly)

Returns Theme string.

Returns:

  • (String)

    Theme string



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

def theme
  @theme
end

Instance Method Details

#cliObject

Parses command line arguments



77
78
79
80
81
# File 'lib/vscripts/commands/identify.rb', line 77

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

#executeObject

Executes the command



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

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



107
108
109
110
111
112
113
114
# File 'lib/vscripts/commands/identify.rb', line 107

def incremented_hostname
  number = 1
  while ec2.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



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

def map2tags
  theme_elements.map do |element|
    element == '#' ? element : ec2.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.



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

def new_domain
  domain || ec2.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

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

Returns:

  • (String)

    The command line –host argument or the themed hostname



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

def new_hostname
  host || incremented_hostname || local_host_name
end

#parserObject

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



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

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



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

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



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

def set_name_tag
  return if ec2.tag('Name') == new_fqdn
  puts "Setting name tag to \"#{new_fqdn}\"..."
  ec2.create_tag(ec2.instance, 'Name', value: new_fqdn)
end

#theme_elementsArray

Returns Splits theme into elements.

Returns:

  • (Array)

    Splits theme into elements



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

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



102
103
104
# File 'lib/vscripts/commands/identify.rb', line 102

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

#update_hostsObject

Modify the hosts file



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

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