Class: VScripts::Commands::Tags2facts
- Inherits:
-
Object
- Object
- VScripts::Commands::Tags2facts
- 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
-
#arguments ⇒ Array
readonly
Command specific arguments.
Instance Method Summary collapse
-
#cli ⇒ Object
Parses command line arguments.
-
#exclude_list ⇒ Array
A list of tags to be excluded.
-
#execute ⇒ Object
Writes the formatted JSON to a file.
-
#initialize(argv = []) ⇒ Tags2facts
constructor
A new instance of Tags2facts.
-
#parser ⇒ Object
Specifies command line options.
-
#tags_json ⇒ JSON
Formatted JSON.
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
Returns a new instance of Tags2facts.
33 34 35 |
# File 'lib/vscripts/commands/tags2facts.rb', line 33 def initialize(argv = []) @arguments ||= argv end |
Instance Attribute Details
#arguments ⇒ Array (readonly)
Returns Command specific arguments.
31 32 33 |
# File 'lib/vscripts/commands/tags2facts.rb', line 31 def arguments @arguments end |
Instance Method Details
#cli ⇒ Object
Parses command line arguments
49 50 51 52 53 |
# File 'lib/vscripts/commands/tags2facts.rb', line 49 def cli @cli ||= Trollop.with_standard_exception_handling parser do parser.parse arguments end end |
#exclude_list ⇒ Array
Returns A list of tags to be excluded.
56 57 58 |
# File 'lib/vscripts/commands/tags2facts.rb', line 56 def exclude_list cli.all ? [] : %w(Name Domain) end |
#execute ⇒ Object
Writes the formatted JSON to a file
71 72 73 74 75 76 |
# File 'lib/vscripts/commands/tags2facts.rb', line 71 def execute file = cli.file puts "Writing tags to \"#{file}\"" ensure_file_content(file, ) puts 'Done.' end |
#parser ⇒ Object
Specifies command line options
38 39 40 41 42 43 44 45 46 |
# File 'lib/vscripts/commands/tags2facts.rb', line 38 def parser Trollop::Parser.new do 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_json ⇒ JSON
Returns Formatted JSON.
61 62 63 64 65 66 67 68 |
# File 'lib/vscripts/commands/tags2facts.rb', line 61 def filtered = (exclude_list) if filtered.empty? abort 'No tags were found!' else JSON.pretty_generate(filtered) end end |