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 +8 -8
- data/.travis.yml +1 -0
- data/CHANGELOG.md +20 -0
- data/README.md +17 -2
- data/bin/vscripts +1 -1
- data/lib/vscripts.rb +13 -4
- data/lib/vscripts/aws/ec2.rb +14 -5
- data/lib/vscripts/command_line.rb +28 -23
- data/lib/vscripts/commands/identify.rb +19 -16
- data/lib/vscripts/commands/tags2facts.rb +10 -10
- data/lib/vscripts/config.rb +52 -0
- data/lib/vscripts/util/local_system.rb +0 -5
- data/lib/vscripts/version.rb +11 -6
- data/spec/integration/commands/identify_spec.rb +101 -0
- data/spec/integration/commands/tags2facts_spec.rb +98 -0
- data/spec/integration/commands_spec.rb +12 -0
- data/spec/integration/config_spec.rb +56 -0
- data/spec/integration/global_opts_spec.rb +56 -0
- data/spec/spec_helper.rb +127 -3
- data/spec/unit/vscripts/aws/ec2_spec.rb +9 -1
- data/spec/unit/vscripts/aws_spec.rb +0 -1
- data/spec/unit/vscripts/command_line_spec.rb +5 -4
- data/spec/unit/vscripts/commands/identify_spec.rb +16 -12
- data/spec/unit/vscripts/commands/tags2facts_spec.rb +4 -0
- data/spec/unit/vscripts/commands_spec.rb +1 -1
- data/spec/unit/vscripts/config_spec.rb +53 -0
- data/spec/unit/vscripts/util/local_system_spec.rb +4 -12
- data/spec/unit/vscripts/version_spec.rb +13 -1
- data/spec/unit/vscripts_spec.rb +33 -14
- data/tasks/deploy.rake +23 -14
- data/vscripts.gemspec +8 -6
- metadata +15 -6
- data/VERSION +0 -1
- data/spec/aws_spec_helper.rb +0 -10
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZTNmYzIzYjVhMTVmMjhmMjI0MGRiYTRlMDRiNzFjYmYxNzMwMzEyYw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MzQ2YmZmOWQ2ZTk1YzkzMmE1OTcyNDVjZGJjYTk4ZGZmNWViZjYyMw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZjBlMzBkYWJjYzMyZjM0Y2I1ZTE4NTYwMDZkY2MzNzBlMzYwNjIwNGUzZTdk
|
10
|
+
MWU5ZDRhZWJjMTFiZDhlZTdmMWE4MzI3ZTUzNzUyMjQ2ZGM2NTU0NTVkMzgy
|
11
|
+
OTczMTEzNzU2OTZhMmI3NmM5ZTRiZjYwMTI0MTNjMDcxYzZhMjA=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MmYwMDhkODRlMjQyMDhhMjMxMDUxMzBhZTU3ZmI1YWE5YWZiOTFmMjdhMTMz
|
14
|
+
ZjQxZmMwYjIxMGM3MzJmODZiYTMzMzgyOTYzNmQ0ZDJkYzNiOTYxNzYyOTEw
|
15
|
+
NmRlYTU3NGQxZTRkODBkMzk1M2M1NzMxODk3MmZiM2QwYTYyMmI=
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -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
|
-
-
|
30
|
-
-
|
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)
|
data/bin/vscripts
CHANGED
data/lib/vscripts.rb
CHANGED
@@ -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
|
8
|
-
cli
|
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
|
data/lib/vscripts/aws/ec2.rb
CHANGED
@@ -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
|
-
|
46
|
-
|
47
|
-
hash
|
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
|
-
#
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
27
|
-
version VScripts::
|
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
|
-
|
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
|
-
|
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
|
-
# @
|
47
|
-
|
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
|
-
|
50
|
-
|
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
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
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 =
|
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
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
25
|
-
|
24
|
+
USAGE:
|
25
|
+
vscripts tags2facts [options]
|
26
26
|
|
27
|
-
|
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
|
data/lib/vscripts/version.rb
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
module VScripts
|
2
|
-
#
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|