Class: VScripts::Commands::Tags2facts

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

Overview

Tags2Facts Class

Constant Summary collapse

USAGE =

HELP

<<-EOS

  This command can only be run on an AWS EC2 instance. It looks for all tags
  associated with it and dumps them in a JSON file. By default this file is
  `/etc/facter/facts.d/ec2_tags.json`. It can be overridden with the
  ***`--file`*** argument.

  The `Name` and `Domain` tags are excluded by default because this command is
  intended to add Facter facts and these 2 already exist in Facter. This
  behaviour can be overridden by adding `[-a|--all]` command line option.

  Usage:
 $ vscripts tags2facts [options]

  Options:
EOS

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 = []) ⇒ Tags2facts

Returns a new instance of Tags2facts.



37
38
39
# File 'lib/vscripts/commands/tags2facts.rb', line 37

def initialize(argv = [])
  @arguments ||= argv
end

Instance Attribute Details

#argumentsArray (readonly)

Returns Command specific arguments.

Returns:

  • (Array)

    Command specific arguments



33
34
35
# File 'lib/vscripts/commands/tags2facts.rb', line 33

def arguments
  @arguments
end

#ec2Object (readonly)

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



35
36
37
# File 'lib/vscripts/commands/tags2facts.rb', line 35

def ec2
  @ec2
end

Instance Method Details

#cliObject

Parses command line arguments



59
60
61
62
63
# File 'lib/vscripts/commands/tags2facts.rb', line 59

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

#exclude_listArray

Returns A list of tags to be excluded.

Returns:

  • (Array)

    A list of tags to be excluded



66
67
68
# File 'lib/vscripts/commands/tags2facts.rb', line 66

def exclude_list
  cli.all ? [] : %w(Name Domain)
end

#executeObject

Writes the formatted JSON to a file



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

def execute
  file = cli.file
  puts "Writing tags to \"#{file}\""
  ensure_file_content(file, tags_json)
  puts 'Done.'
end

#filtered_tagsHash

Returns Filtered tags.

Returns:

  • (Hash)

    Filtered tags



71
72
73
74
75
76
# File 'lib/vscripts/commands/tags2facts.rb', line 71

def filtered_tags
  ec2.tags_without(exclude_list).each_with_object({}) do |tag, hash|
    hash[tag[0]] = tag[1]
    hash
  end
end

#parserObject

Specifies command line options



42
43
44
45
46
47
48
49
50
# File 'lib/vscripts/commands/tags2facts.rb', line 42

def parser
  Trollop::Parser.new do
    banner USAGE
    opt :file, 'The file that will store the tags',
        type: :string, default: '/etc/facter/facts.d/ec2_tags.json'
    opt :all, 'Collect all tags'
    stop_on_unknown
  end
end

#tags_jsonJSON

Returns Formatted JSON.

Returns:

  • (JSON)

    Formatted JSON



79
80
81
82
83
84
85
86
# File 'lib/vscripts/commands/tags2facts.rb', line 79

def tags_json
  tags_hash = filtered_tags
  if tags_hash.empty?
    abort 'No tags were found!'
  else
    JSON.pretty_generate(tags_hash)
  end
end