Class: VScripts::Commands::Tags2facts

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

Overview

Tags2Facts Class

Constant Summary collapse

USAGE =

Shows help

"\n  This command can only be run on an AWS EC2 instance. It looks for all tags\n  associated with it and dumps them in a JSON file. By default this file is\n  `/etc/facter/facts.d/ec2_tags.json`. It can be overridden with the\n  ***`--file`*** argument.\n\n  The `Name` and `Domain` tags are excluded by default because this command is\n  intended to add Facter facts and these 2 already exist in Facter. This\n  behaviour can be overridden by adding `[-a|--all]` command line option.\n\n  Usage:\n $ vscripts tags2facts [options]\n\n  Options:\n"

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

Loads the Tags2Facts class



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

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

Instance Attribute Details

#argumentsArray (readonly)



31
32
33
# File 'lib/vscripts/commands/tags2facts.rb', line 31

def arguments
  @arguments
end

Instance Method Details

#cliHash



51
52
53
54
55
# File 'lib/vscripts/commands/tags2facts.rb', line 51

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

#exclude_listArray



58
59
60
# File 'lib/vscripts/commands/tags2facts.rb', line 58

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

#executeObject

Writes the formatted JSON to the file



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

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

#parserObject

Specifies command line options



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

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



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

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