vscripts 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjM5YzJkNTdlYjQ5MmRmYTkzNGNkYTk4YzM0MTgwMTM2YmVjOWNiZg==
4
+ ZTNmYzIzYjVhMTVmMjhmMjI0MGRiYTRlMDRiNzFjYmYxNzMwMzEyYw==
5
5
  data.tar.gz: !binary |-
6
- ZDYzNzVkMjVmZWE0Njc4MzIxMmE4Nzk4OTY1MWQ4OWY5YzI4YWNjNA==
6
+ MzQ2YmZmOWQ2ZTk1YzkzMmE1OTcyNDVjZGJjYTk4ZGZmNWViZjYyMw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MzkzNDZmZTFmYjljZDg0NmRmODE3YTNkYTQ5Y2JkYWM2NmRlY2NkYzM1YjU2
10
- M2RiYzExMjg3YWQ3MjIzMWY0ZGY4ZjdlNDZmMThmZDNjZDdmYmYwNmEzNzc2
11
- YTU5OGFmZWFlZTE4NDI3YTMwMzk2NjAzZTA2NDhhNjRmOTVkM2Y=
9
+ ZjBlMzBkYWJjYzMyZjM0Y2I1ZTE4NTYwMDZkY2MzNzBlMzYwNjIwNGUzZTdk
10
+ MWU5ZDRhZWJjMTFiZDhlZTdmMWE4MzI3ZTUzNzUyMjQ2ZGM2NTU0NTVkMzgy
11
+ OTczMTEzNzU2OTZhMmI3NmM5ZTRiZjYwMTI0MTNjMDcxYzZhMjA=
12
12
  data.tar.gz: !binary |-
13
- ZGM0MTNhMmQxOTYwZTc1ZTgxNWJkYzZjNGI0NmExN2RkNzc0OGIyMGY2OTU0
14
- YzI0MTRmNmVlN2ZiYTJlM2U1NjhmYzhhZTJjZTliZmMxY2I0ZjIyNjQ4NmJk
15
- MGViMThkOGUyZTgyMTlhNjEwNDJmNmYxMjFjZDI2MGYxMWZjMWY=
13
+ MmYwMDhkODRlMjQyMDhhMjMxMDUxMzBhZTU3ZmI1YWE5YWZiOTFmMjdhMTMz
14
+ ZjQxZmMwYjIxMGM3MzJmODZiYTMzMzgyOTYzNmQ0ZDJkYzNiOTYxNzYyOTEw
15
+ NmRlYTU3NGQxZTRkODBkMzk1M2M1NzMxODk3MmZiM2QwYTYyMmI=
@@ -1,6 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
+ - 2.0.0
4
5
  - 2.1.2
5
6
  deploy:
6
7
  provider: rubygems
@@ -1,3 +1,23 @@
1
+ Version 0.1.5
2
+ ---
3
+ * Add configuration file ([Vlad - f928415](https://github.com/vghn/vscripts/commit/f928415a19c4f300566721fcffd14877497de6f1))
4
+ * Add testing for Ruby 2.0.0 ([Vlad - ba19d81](https://github.com/vghn/vscripts/commit/ba19d817194fe1395ce5d2b3884552004e5e07ef))
5
+ * Fix command line options ([Vlad - cc6e8ad](https://github.com/vghn/vscripts/commit/cc6e8ad6ee77218cfe4cb8ae80af57a291d36ddb))
6
+ * Fix test for Ruby 2.0 ([Vlad - 2d32474](https://github.com/vghn/vscripts/commit/2d32474afcd2f1122d0d9deebc4e5c0bae700ff5))
7
+ * Fix smells ([Vlad - 23283a9](https://github.com/vghn/vscripts/commit/23283a941d356e185364d83832d88b15919c2bf7))
8
+ * Minor code improvements ([Vlad - b9091e5](https://github.com/vghn/vscripts/commit/b9091e5fbd6a50679c214d4d7db9539965d727e6))
9
+
10
+ Version 0.1.4
11
+ ---
12
+ * Switch back to previous branch on deployment ([Vlad - 244ac9a](https://github.com/vghn/vscripts/commit/244ac9a6d810abd63a625c88bc0a2efac19a9993))
13
+ * Respect RSpec convention for class methods ([Vlad - 35130ef](https://github.com/vghn/vscripts/commit/35130efd5b21cc18335efb1d620e3412e1d57963))
14
+ * Add LICENSE and CHANGELOG to the README ([Vlad - 4616bf9](https://github.com/vghn/vscripts/commit/4616bf984d4b77718505595651fa62532ecf8a67))
15
+ * Improve versioning ([Vlad - 0f52286](https://github.com/vghn/vscripts/commit/0f52286d344fa2d46e41e96f693c989b4367f822))
16
+ * Minor changes ([Vlad - a32dbcc](https://github.com/vghn/vscripts/commit/a32dbcc70be3043e12624a83d5e25aa1a0ffd405))
17
+ * Minor code changes ([Vlad - 8821acd](https://github.com/vghn/vscripts/commit/8821acd570fb9058feefe8eacf9212ef9d5c4a39))
18
+ * Add integration testing ([Vlad - a875320](https://github.com/vghn/vscripts/commit/a875320b65c94cb84304136a0b2967e744098207))
19
+ * Metadata only since rubygems 2.0 ([Vlad - ab5264b](https://github.com/vghn/vscripts/commit/ab5264b8a3bd93c325f99eae6473a89ebdfa71e9))
20
+
1
21
  Version 0.1.3
2
22
  ---
3
23
  * Upgrade documentation ([Vlad - fd6fb30](https://github.com/vghn/vscripts/commit/fd6fb30af09d4f4050f0b35d41cc3bfb7e534e4a))
data/README.md CHANGED
@@ -16,6 +16,12 @@ Automation daemon.
16
16
  ### Gem installation
17
17
  `gem install vscripts`
18
18
 
19
+ ## Configuration
20
+
21
+ VScripts looks for a configuration file in the following locations(in order):
22
+ - a file specified in the `--config` command line option
23
+ - `.vscripts.yml` in the $HOME directory
24
+ - `/etc/vscripts/config.yml`
19
25
 
20
26
  ## Usage
21
27
 
@@ -26,8 +32,9 @@ vscripts GLOBAL-OPTIONS COMMAND OPTIONS
26
32
 
27
33
  ### Global Options
28
34
  ```
29
- -h|--help: Displays VScripts help.
30
- -v|--version: Displays the version number.
35
+ --config, -c <s>: Specify configuration file
36
+ --help, -h : Displays VScripts help
37
+ --version, -v : Displays the version number
31
38
  ```
32
39
 
33
40
 
@@ -106,3 +113,11 @@ associated with the current instance.
106
113
  3. Commit your changes (`git commit -am 'Add some feature'`)
107
114
  4. Push to the branch (`git push origin my-new-feature`)
108
115
  5. Create new Pull Request
116
+
117
+
118
+ ## License
119
+ See [LICENSE](LICENSE)
120
+
121
+
122
+ ## Changelog
123
+ See [CHANGELOG](CHANGELOG.md)
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'vscripts'
4
- VScripts.run(ARGV)
4
+ VScripts.run
@@ -1,12 +1,21 @@
1
1
  require 'vscripts/commands'
2
2
  require 'vscripts/command_line'
3
+ require 'vscripts/config'
3
4
 
4
5
  # Main VScripts module
5
6
  module VScripts
7
+ # Reads the command line arguments
8
+ def self.cli
9
+ @cli ||= CommandLine.new # Parses command line
10
+ end
11
+
12
+ # Reads the configuration files
13
+ def self.config
14
+ @config ||= VScripts::Config.new(cli.global.config) # Parses configuration
15
+ end
16
+
6
17
  # Reads the arguments and runs the given command
7
- def self.run(argv)
8
- cli = CommandLine.new(argv)
9
- command = VScripts::Commands.const_get(cli.command).new(cli.arguments)
10
- command.execute
18
+ def self.run
19
+ VScripts::Commands.const_get(cli.command).new(cli.command_options).execute
11
20
  end
12
21
  end # module VScripts
@@ -38,15 +38,24 @@ module VScripts
38
38
  ec2.tags.create(resource, key, value)
39
39
  end
40
40
 
41
+ # @return [Hash] a correct Hash object with all tags and keys
42
+ def all_tags_hash
43
+ hash = {}
44
+ all_tags.each do |key, value|
45
+ hash = hash.merge(Hash[key, value])
46
+ end
47
+ hash
48
+ end
49
+
41
50
  # Exclude tags
42
51
  # @param list [Array] the list of tag keys to be excluded
43
52
  # @return [Hash] the filtered tags
44
53
  def tags_without(list = [])
45
- all_tags.each_with_object({}) do |tag, hash|
46
- key, value = tag[0], tag[1]
47
- hash[key] = value unless list.include? key
48
- hash
54
+ hash = all_tags_hash
55
+ list.each do |key|
56
+ hash.delete(key)
49
57
  end
58
+ hash
50
59
  end
51
60
 
52
61
  # Looks for the value of the 'Name' tag for the given instance
@@ -72,7 +81,7 @@ module VScripts
72
81
  # a Name tag
73
82
  def similar_instances
74
83
  check_instance
75
- functional_instances.map do |functional_instance|
84
+ functional_instances.compact.map do |functional_instance|
76
85
  next if functional_instance.id == instance_id
77
86
  functional_instance.tags['Name']
78
87
  end
@@ -1,21 +1,21 @@
1
1
  require 'trollop'
2
+ require 'vscripts/version'
2
3
  require 'vscripts/commands'
3
4
 
4
5
  module VScripts
5
6
  # Parses the command line arguments
6
7
  class CommandLine
7
- # @return [Array] all command line arguments
8
- attr_accessor :arguments
9
8
  # @return [Hash] the global command line arguments.
10
9
  attr_reader :global
11
10
  # @return [String] the command name
12
11
  attr_reader :command
12
+ # @return [Array] the command specific arguments.
13
+ attr_reader :command_options
13
14
 
14
- # Builds command line arguments
15
- def initialize(argv = [])
16
- @arguments ||= argv
17
- @global ||= parse_cli_options
18
- @command ||= verify_command
15
+ # @param args [Array] the command line arguments
16
+ # @return [Hash] the command line options
17
+ def initialize(args = ARGV)
18
+ parse_global(args)
19
19
  end
20
20
 
21
21
  # Specifies command line options
@@ -23,44 +23,49 @@ module VScripts
23
23
  # This method smells of :reek:TooManyStatements but ignores them
24
24
  def parser # rubocop:disable MethodLength
25
25
  available = Commands.list.map { |cmd| cmd.to_s.downcase }
26
- @parser ||= Trollop::Parser.new do
27
- version VScripts::VERSION_C
26
+ Trollop::Parser.new do
27
+ version VScripts::VERSION::COPYRIGHT
28
28
  banner <<-EOS
29
29
  VScripts automation daemon.
30
30
 
31
31
  Available commands:
32
32
  #{available}
33
33
 
34
- Usage:
34
+ USAGE:
35
35
  vscripts GLOBAL-OPTIONS COMMAND OPTIONS
36
36
 
37
37
  For help on an individual command:
38
38
  vscripts COMMAND --help
39
39
 
40
- Global Options:
40
+ GLOBAL OPTIONS:
41
41
  EOS
42
+ opt :config, 'Specify configuration file',
43
+ type: :string, short: '-c'
42
44
  stop_on available
45
+ stop_on_unknown
43
46
  end
44
47
  end
45
48
 
46
- # @return [Hash] the command line arguments
47
- def parse_cli_options
49
+ # @param args [Array] the command line arguments
50
+ # @return [Hash] the command line options
51
+ def parse_global(args)
48
52
  Trollop.with_standard_exception_handling parser do
49
- fail Trollop::HelpNeeded if arguments.empty?
50
- parser.parse arguments
53
+ @global = parser.parse args
54
+ fail Trollop::HelpNeeded if args.empty? || !parse_command(args)
51
55
  end
56
+ @global
52
57
  end
53
58
 
54
59
  # Ensures command is available
55
60
  # @return [String] the command name
56
- def verify_command
57
- command_cli = arguments.shift
58
- command_cls = command_cli.capitalize.to_sym
59
- if Commands.list.include?(command_cls)
60
- return command_cls
61
- else
62
- abort "Error: Unknown subcommand '#{command_cli}'\nTry --help."
63
- end
61
+ # @return [Array] the command specific arguments
62
+ def parse_command(args)
63
+ command = args.shift
64
+ return unless command
65
+ command_class = command.capitalize.to_sym
66
+ abort "Error: unknown subcommand '#{command}'\nTry --help." \
67
+ unless Commands.list.include?(command_class)
68
+ @command, @command_options = [command_class, args]
64
69
  end
65
70
  end # class CommandLine
66
71
  end # module VScripts
@@ -11,6 +11,7 @@ module VScripts
11
11
 
12
12
  # Shows help
13
13
  USAGE = <<-EOS
14
+
14
15
  This command creates a themed host name and fully qualified domain name for
15
16
  the server, using AWS EC2 tags. The default theme is `Group-Role-#` which means
16
17
  that the command collects the value of the `Group` and the `Role` AWS EC2 tags
@@ -24,20 +25,20 @@ Once a new host name is composed, both `/etc/hostname` and `/etc/hosts` are
24
25
  modified on the local instance and a new `Name` EC2 tag is created and
25
26
  associated with the current instance.
26
27
 
27
- If a ***--host*** argument is provided it will override the default theme.
28
- *DOMAIN* is still looked up.
29
- If a ***--domain*** argument is provided it will override the default
30
- domain.
28
+ If a ***--host*** argument is provided it will override the default theme.
29
+ *DOMAIN* is still looked up.
30
+ If a ***--domain*** argument is provided it will override the default
31
+ domain.
31
32
 
32
- EXAMPLES:
33
- $ vscripts identify
34
- MyGroup-MyRole-1.Example.tld
35
- $ vscripts identify --ec2-tag-theme NAME-#
36
- MyName-1.Example.tld`
37
- $ vscripts identify --host myhost --domain example.com
38
- myhost.example.com`
33
+ USAGE:
34
+ $ vscripts identify
35
+ MyGroup-MyRole-1.Example.tld
36
+ $ vscripts identify --ec2-tag-theme NAME-#
37
+ MyName-1.Example.tld`
38
+ $ vscripts identify --host myhost --domain example.com
39
+ myhost.example.com`
39
40
 
40
- Options:
41
+ OPTIONS:
41
42
  EOS
42
43
 
43
44
  # @return [String] the theme
@@ -137,6 +138,7 @@ associated with the current instance.
137
138
 
138
139
  # Modifies the host name
139
140
  def set_hostname
141
+ return unless File.exist?(hostname_path)
140
142
  return if File.read(hostname_path).strip == new_hostname
141
143
  puts "Setting local hostname (#{new_hostname})..."
142
144
  write_file(hostname_path, new_hostname)
@@ -145,10 +147,11 @@ associated with the current instance.
145
147
 
146
148
  # Modifies the hosts file
147
149
  def update_hosts
150
+ return unless File.exist?(hosts_path)
148
151
  return if File.readlines(hosts_path)
149
152
  .grep(/#{new_fqdn} #{new_hostname}/)
150
153
  .any?
151
- hosts_body = hosts_file.gsub(
154
+ hosts_body = File.read(hosts_path).gsub(
152
155
  /^127\.0\.0\.1.*/,
153
156
  "127\.0\.0\.1 #{new_fqdn} #{new_hostname} localhost"
154
157
  )
@@ -158,9 +161,9 @@ associated with the current instance.
158
161
 
159
162
  # Executes the command
160
163
  def execute
161
- set_name_tag
162
- set_hostname
163
- update_hosts
164
+ puts 'EC2 tag not changed!' unless set_name_tag
165
+ puts 'Hostname not changed!' unless set_hostname
166
+ puts 'Hosts file not changed!' unless update_hosts
164
167
  puts 'Done.'
165
168
  end
166
169
  end # class Identify
@@ -12,19 +12,19 @@ module VScripts
12
12
  # Shows help
13
13
  USAGE = <<-EOS
14
14
 
15
- This command can only be run on an AWS EC2 instance. It looks for all tags
16
- associated with it and dumps them in a JSON file. By default this file is
17
- `/etc/facter/facts.d/ec2_tags.json`. It can be overridden with the
18
- ***`--file`*** argument.
15
+ This command can only be run on an AWS EC2 instance. It looks for all tags
16
+ associated with it and dumps them in a JSON file. By default this file is
17
+ `/etc/facter/facts.d/ec2_tags.json`. It can be overridden with the
18
+ ***`--file`*** argument.
19
19
 
20
- The `Name` and `Domain` tags are excluded by default because this command is
21
- intended to add Facter facts and these 2 already exist in Facter. This
22
- behaviour can be overridden by adding `[-a|--all]` command line option.
20
+ The `Name` and `Domain` tags are excluded by default because this command is
21
+ intended to add Facter facts and these 2 already exist in Facter. This
22
+ behaviour can be overridden by adding `[-a|--all]` command line option.
23
23
 
24
- Usage:
25
- $ vscripts tags2facts [options]
24
+ USAGE:
25
+ vscripts tags2facts [options]
26
26
 
27
- Options:
27
+ OPTIONS:
28
28
  EOS
29
29
 
30
30
  # @return [Array] the command specific arguments
@@ -0,0 +1,52 @@
1
+ require 'yaml'
2
+
3
+ module VScripts
4
+ # VScripts Configuration
5
+ class Config
6
+ # User's configuration file
7
+ DOT_FILE = "#{File.expand_path('~')}/.vscripts.yml"
8
+ # Global configuration file
9
+ SYSTEM_CONFIG_FILE = '/etc/vscripts/config.yml'
10
+ # Global defaults
11
+ GLOBAL_DEFAULTS = {}
12
+
13
+ # @return [Hash] all configuration options
14
+ attr_reader :get
15
+
16
+ # Loads class
17
+ # @param cfg_file [String] the path to the configuration file
18
+ # @return [Hash] the configuration options
19
+ def initialize(config_file = nil)
20
+ @file = config_file
21
+ @get = GLOBAL_DEFAULTS.merge(options)
22
+ end
23
+
24
+ # Parses the configuration files in order
25
+ # @return [Hash] the first configuration hash found
26
+ def options
27
+ parse(@file) ||
28
+ parse(DOT_FILE) ||
29
+ parse(SYSTEM_CONFIG_FILE) ||
30
+ {}
31
+ end
32
+
33
+ # Parses the configuration
34
+ # @param file [String] the path to the configuration file
35
+ # @return [Hash] the configuration hash
36
+ def parse(file)
37
+ YAML.load(read(file)) if check_config(file)
38
+ end
39
+
40
+ # @param (see #parse)
41
+ # @return [String] the contents of the file
42
+ def read(file)
43
+ File.read(file)
44
+ end
45
+
46
+ # @param (see #parse)
47
+ # @return [Boolean] true if the file exists
48
+ def check_config(file)
49
+ file && File.exist?(file)
50
+ end
51
+ end # class Config
52
+ end # module VScripts
@@ -7,11 +7,6 @@ module VScripts
7
7
  '/etc/hosts'
8
8
  end
9
9
 
10
- # @return [String] the contents of the hosts file
11
- def hosts_file
12
- File.read(hosts_path)
13
- end
14
-
15
10
  # @return [String] the hostname file path
16
11
  def hostname_path
17
12
  '/etc/hostname'
@@ -1,8 +1,13 @@
1
1
  module VScripts
2
- # @return [String] the version number
3
- VERSION = File.read(File.expand_path('../../../VERSION', __FILE__)).strip
4
- # @return [String] the semantic version number
5
- VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH = VERSION.split('.')
6
- # @return [String] the copyrighted version number
7
- VERSION_C = "VScripts #{VERSION} (c) #{Time.new.year} Vlad Ghinea"
2
+ # This module holds the version information.
3
+ module VERSION
4
+ # @return [String] the version number
5
+ STRING = '0.1.5'
6
+
7
+ # @return [String] the semantic version number
8
+ MAJOR, MINOR, PATCH = STRING.split('.').map(&:to_i)
9
+
10
+ # @return [String] the copyrighted version number
11
+ COPYRIGHT = "VScripts #{STRING} (c) #{Time.new.year} Vlad Ghinea"
12
+ end
8
13
  end