vcenter_lib_mongodb 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rubocop.yml +106 -0
- data/.travis.yml +22 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +40 -0
- data/Rakefile +18 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/vcenter_lib_mongodb/logging.rb +43 -0
- data/lib/vcenter_lib_mongodb/mongodb.rb +196 -0
- data/lib/vcenter_lib_mongodb/updater.rb +57 -0
- data/lib/vcenter_lib_mongodb/version.rb +3 -0
- data/lib/vcenter_lib_mongodb.rb +18 -0
- data/scripts/deploy_gem.sh +87 -0
- data/spec/classes/spec_helper.rb +4 -0
- data/vcenter_lib_mongodb.gemspec +30 -0
- metadata +174 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 386919cf7d912b9eb85e3b056799b512418cbafb
|
4
|
+
data.tar.gz: 2bdc9675b1b30feac1ec82f39d04b130b61b50c5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 969d6a12f23afc097a6321dedd021f1e6d6864354b1be264fed575f33ed0ae4667cb9aae4df83c2e0da5e5b1ceaa17b47ba9184cb25bd69561502f36cfafb6cc
|
7
|
+
data.tar.gz: fb5abc6000dc230394000c1955509b75e5b9edc1f7cb36d9d33f7847250225cf5b1221b4bae6ba378b7d214b1c8fc0b2823a8c7dc1574dab065e130db5817b40
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# This yaml file describes which files are excluded in rubocop run.
|
2
|
+
# It can be in the project home directory or in the $HOME folder.
|
3
|
+
|
4
|
+
# Common configuration.
|
5
|
+
AllCops:
|
6
|
+
Include:
|
7
|
+
- 'lib/**/'
|
8
|
+
- 'exe/*'
|
9
|
+
|
10
|
+
Exclude:
|
11
|
+
- 'scripts/**/*'
|
12
|
+
- 'vendor/**/*'
|
13
|
+
- 'bin/**/*'
|
14
|
+
- 'bundle/**/*'
|
15
|
+
- 'local-gems/**/*'
|
16
|
+
- '**/*.sh'
|
17
|
+
- 'tester.rb'
|
18
|
+
- 'test/**/*'
|
19
|
+
- 'toaster.rb'
|
20
|
+
|
21
|
+
# --- XXXLength-Section --------------------------------------------------------------------
|
22
|
+
# too long lines, methods and classes are annoying,
|
23
|
+
LineLength:
|
24
|
+
Enabled: true
|
25
|
+
Max: 100
|
26
|
+
|
27
|
+
MethodLength:
|
28
|
+
Enabled: true
|
29
|
+
Max: 35
|
30
|
+
|
31
|
+
Metrics/AbcSize:
|
32
|
+
Max: 40
|
33
|
+
|
34
|
+
ClassLength:
|
35
|
+
Enabled: true
|
36
|
+
Max: 140
|
37
|
+
|
38
|
+
# --- Style Cops - Section -----------------------------------------------------------------
|
39
|
+
# Don't be so dogmatic about Hash-Style! Both are fine for us
|
40
|
+
HashSyntax:
|
41
|
+
Enabled: true
|
42
|
+
|
43
|
+
# From Ruby 2.x on there is no need for this anymore, so why bothering now?
|
44
|
+
Encoding:
|
45
|
+
Enabled: false
|
46
|
+
|
47
|
+
# Ensable following message: Documentation: Missing top-level class documentation comment.
|
48
|
+
Documentation:
|
49
|
+
Enabled: true
|
50
|
+
|
51
|
+
# check filename conventions
|
52
|
+
FileName:
|
53
|
+
Enabled: false
|
54
|
+
|
55
|
+
# this 3-digit thing for portnumbers? oh, come on!
|
56
|
+
NumericLiterals:
|
57
|
+
Enabled: false
|
58
|
+
|
59
|
+
# ok, one should avoid global vars, but from time to time we need them
|
60
|
+
Style/GlobalVars:
|
61
|
+
Enabled: true
|
62
|
+
|
63
|
+
Style/RegexpLiteral:
|
64
|
+
Enabled: true
|
65
|
+
|
66
|
+
Layout/AlignParameters:
|
67
|
+
EnforcedStyle: "with_fixed_indentation"
|
68
|
+
|
69
|
+
Style/BracesAroundHashParameters:
|
70
|
+
EnforcedStyle: "context_dependent"
|
71
|
+
|
72
|
+
Style/EachWithObject:
|
73
|
+
Enabled: false
|
74
|
+
|
75
|
+
# we don't want be forced to use %i
|
76
|
+
Style/SymbolArray:
|
77
|
+
Enabled: false
|
78
|
+
|
79
|
+
# we know the special global variables by heart
|
80
|
+
Style/SpecialGlobalVars:
|
81
|
+
Enabled: false
|
82
|
+
|
83
|
+
# we don't care about quoting style
|
84
|
+
Style/StringLiterals:
|
85
|
+
Enabled: false
|
86
|
+
|
87
|
+
# for easier line moving
|
88
|
+
Style/TrailingCommaInLiteral:
|
89
|
+
Enabled: false
|
90
|
+
|
91
|
+
|
92
|
+
# --- Complexity - Section -----------------------------------------------------------------
|
93
|
+
# as old McCabe says:
|
94
|
+
#
|
95
|
+
# Cyclomatic Complexity Risk Evaluation...
|
96
|
+
# 1-10 A simple module without much risk
|
97
|
+
# 11-20 A more complex module with moderate risk
|
98
|
+
# 21-50 A complex module of high risk
|
99
|
+
# 51 and greater An untestable program of very high risk
|
100
|
+
CyclomaticComplexity:
|
101
|
+
Max: 10
|
102
|
+
|
103
|
+
# Lint-Section -----------------------------------------------------------------------------
|
104
|
+
# what is soooo bad about blablubb.match /..../ compared to blablubb.match(/..../)?
|
105
|
+
Lint/AmbiguousRegexpLiteral:
|
106
|
+
Enabled: true
|
data/.travis.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
cache: bundler
|
4
|
+
before_install: gem install bundler
|
5
|
+
install: bundle install --jobs=3 --retry=3
|
6
|
+
rvm:
|
7
|
+
- 1.9.3
|
8
|
+
- 2.0.0
|
9
|
+
- 2.1.10
|
10
|
+
- 2.2.6
|
11
|
+
- 2.3.3
|
12
|
+
- 2.4.0
|
13
|
+
- ruby-head
|
14
|
+
- jruby-9.1.5.0
|
15
|
+
- jruby-19mode
|
16
|
+
- jruby-head
|
17
|
+
matrix:
|
18
|
+
fast_finish: true
|
19
|
+
allow_failures:
|
20
|
+
- rvm: jruby-head
|
21
|
+
- rvm: ruby-head
|
22
|
+
|
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
# only for local testing but not needed for spec tests
|
6
|
+
group :test do
|
7
|
+
gem 'rake'
|
8
|
+
gem 'rubocop' if RUBY_VERSION !~ /^1\./
|
9
|
+
# rubocop:disable Bundler/DuplicatedGem
|
10
|
+
gem 'rubocop', '=0.39.0' if RUBY_VERSION =~ /^1\./
|
11
|
+
# rubocop:enable Bundler/DuplicatedGem
|
12
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Michael Meyling
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# vcenter library mongodb backend
|
2
|
+
|
3
|
+
Export vcenter data into a mongodb and ask the mongodb for properties
|
4
|
+
of any VM.
|
5
|
+
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'vcenter_lib_mongodb'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install vcenter_lib_mongodb
|
22
|
+
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
## Development
|
27
|
+
|
28
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
29
|
+
|
30
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/m-31/vcenter_lib_mongodb.
|
35
|
+
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
40
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require "rubocop/rake_task"
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
desc "Run RuboCop on the lib directory"
|
7
|
+
RuboCop::RakeTask.new(:rubocop) do |task|
|
8
|
+
task.formatters = ["fuubar"]
|
9
|
+
task.options = ["-D"]
|
10
|
+
task.options = task.options + ["--fail-level", "E"] if RUBY_VERSION =~ /^1\./
|
11
|
+
task.fail_on_error = true
|
12
|
+
end
|
13
|
+
|
14
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
15
|
+
t.pattern = 'spec/**/*_spec.rb'
|
16
|
+
end
|
17
|
+
|
18
|
+
task default: %w[spec rubocop]
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "vcenter_lib.gemspec"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'vcenter_lib/logging'
|
3
|
+
|
4
|
+
module VcenterLibMongodb
|
5
|
+
# for logger access just include this module
|
6
|
+
module Logging
|
7
|
+
class << self
|
8
|
+
attr_writer :logger
|
9
|
+
|
10
|
+
def logger
|
11
|
+
unless @logger
|
12
|
+
@logger = VcenterLib::Logging.logger
|
13
|
+
end
|
14
|
+
@logger
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# addition
|
19
|
+
def self.included(base)
|
20
|
+
class << base
|
21
|
+
def logger
|
22
|
+
# :nocov:
|
23
|
+
Logging.logger
|
24
|
+
# :nocov:
|
25
|
+
end
|
26
|
+
|
27
|
+
def logger=(logger)
|
28
|
+
# :nocov:
|
29
|
+
Logging.logger = logger
|
30
|
+
# :nocov:
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def logger
|
36
|
+
Logging.logger
|
37
|
+
end
|
38
|
+
|
39
|
+
def logger=(logger)
|
40
|
+
Logging.logger = logger
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,196 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
require_relative "logging"
|
4
|
+
|
5
|
+
module VcenterLibMongodb
|
6
|
+
# access vms and their facts from mongo database
|
7
|
+
class Mongodb
|
8
|
+
include Logging
|
9
|
+
attr_reader :connection
|
10
|
+
attr_reader :vms_collection
|
11
|
+
attr_reader :meta_collection
|
12
|
+
|
13
|
+
# initialize access to mongodb
|
14
|
+
#
|
15
|
+
# You might want to adjust the logging level, for example:
|
16
|
+
# ::Mongo::Logger.logger.level = logger.level
|
17
|
+
#
|
18
|
+
# @param connection mongodb connection, should already be switched to correct database
|
19
|
+
# @param vms symbol for collection that contains vms with their facts
|
20
|
+
# @param meta symbol for collection with update metadata
|
21
|
+
def initialize(connection, vms = :vms, meta = :meta)
|
22
|
+
@connection = connection
|
23
|
+
@vms_collection = vms
|
24
|
+
@meta_collection = meta
|
25
|
+
end
|
26
|
+
|
27
|
+
# get all vm names
|
28
|
+
def all_vms
|
29
|
+
collection = connection[vms_collection]
|
30
|
+
collection.find.batch_size(999).projection(_id: 1).map { |k| k[:_id] }
|
31
|
+
end
|
32
|
+
|
33
|
+
# get vm names that fulfill given mongodb query
|
34
|
+
#
|
35
|
+
# @param query mongodb query
|
36
|
+
def query_vms(query)
|
37
|
+
collection = connection[vms_collection]
|
38
|
+
collection.find(query).batch_size(999).projection(_id: 1).map { |k| k[:_id] }
|
39
|
+
end
|
40
|
+
|
41
|
+
# get vms and their facts that fulfill given mongodb query
|
42
|
+
#
|
43
|
+
# @param query mongodb query
|
44
|
+
# @param facts [Array<String>] get these facts in the result, eg ['boot_time'], empty for all
|
45
|
+
def query_facts(query, facts = [])
|
46
|
+
fields = Hash[facts.collect { |fact| [fact.to_sym, 1] }]
|
47
|
+
collection = connection[vms_collection]
|
48
|
+
result = {}
|
49
|
+
collection.find(query).batch_size(999).projection(fields).each do |values|
|
50
|
+
id = values.delete('_id')
|
51
|
+
result[id] = values
|
52
|
+
end
|
53
|
+
result
|
54
|
+
end
|
55
|
+
|
56
|
+
# get vms and their facts that fulfill given mongodb query and have at least one
|
57
|
+
# value for one the given fact names
|
58
|
+
#
|
59
|
+
# @param query mongodb query
|
60
|
+
# @param facts [Array<String>] get these facts in the result, eg ['fqdn'], empty for all
|
61
|
+
def query_facts_exist(query, facts = [])
|
62
|
+
result = query_facts(query, facts)
|
63
|
+
unless facts.empty?
|
64
|
+
result.keep_if do |_k, v|
|
65
|
+
facts.any? { |f| !v[f].nil? }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
result
|
69
|
+
end
|
70
|
+
|
71
|
+
# get vms and their facts for a pattern
|
72
|
+
#
|
73
|
+
# @param query mongodb query
|
74
|
+
# @param pattern [RegExp] search for
|
75
|
+
# @param facts [Array<String>] get these facts in the result, eg ['fqdn'], empty for all
|
76
|
+
# @param facts_found [Array<String>] fact names are added to this array
|
77
|
+
# @param check_names [Boolean] also search fact names
|
78
|
+
def search_facts(query, pattern, facts = [], facts_found = [], check_names = false)
|
79
|
+
collection = connection[vms_collection]
|
80
|
+
result = {}
|
81
|
+
collection.find(query).batch_size(999).each do |values|
|
82
|
+
id = values.delete('_id')
|
83
|
+
found = {}
|
84
|
+
values.each do |k, v|
|
85
|
+
if v =~ pattern
|
86
|
+
found[k] = v
|
87
|
+
elsif check_names && k =~ pattern
|
88
|
+
found[k] = v
|
89
|
+
end
|
90
|
+
end
|
91
|
+
next if found.empty?
|
92
|
+
facts_found.concat(found.keys).uniq!
|
93
|
+
facts.each do |f|
|
94
|
+
found[f] = values[f]
|
95
|
+
end
|
96
|
+
result[id] = found
|
97
|
+
end
|
98
|
+
result
|
99
|
+
end
|
100
|
+
|
101
|
+
# get facts for given vm name
|
102
|
+
#
|
103
|
+
# @param vm [String] vm name
|
104
|
+
# @param facts [Array<String>] get these facts in the result, eg ['fqdn'], empty for all
|
105
|
+
def single_vm_facts(vm, facts)
|
106
|
+
fields = Hash[facts.collect { |fact| [fact.to_sym, 1] }]
|
107
|
+
collection = connection[vms_collection]
|
108
|
+
result = collection.find(_id: vm).limit(1).batch_size(1).projection(fields).to_a.first
|
109
|
+
result.delete("_id") if result
|
110
|
+
result
|
111
|
+
end
|
112
|
+
|
113
|
+
# get all vms and their facts
|
114
|
+
#
|
115
|
+
# @param facts [Array<String>] get these facts in the result, eg ['fqdn'], empty for all
|
116
|
+
def facts(facts = [])
|
117
|
+
fields = Hash[facts.collect { |fact| [fact.to_sym, 1] }]
|
118
|
+
collection = connection[vms_collection]
|
119
|
+
result = {}
|
120
|
+
collection.find.batch_size(999).projection(fields).each do |values|
|
121
|
+
id = values.delete('_id')
|
122
|
+
result[id] = values
|
123
|
+
end
|
124
|
+
result
|
125
|
+
end
|
126
|
+
|
127
|
+
# get meta informations about updates
|
128
|
+
def meta
|
129
|
+
collection = connection[meta_collection]
|
130
|
+
result = collection.find.first
|
131
|
+
result.delete(:_id)
|
132
|
+
result
|
133
|
+
end
|
134
|
+
|
135
|
+
# update or insert facts for given vm name
|
136
|
+
#
|
137
|
+
# @param vm [String] vm name
|
138
|
+
# @param facts [Hash] facts for the vm
|
139
|
+
def vm_update(vm, facts)
|
140
|
+
logger.debug " updating #{vm}"
|
141
|
+
connection[vms_collection].find(_id: vm).replace_one(facts,
|
142
|
+
upsert: true,
|
143
|
+
bypass_document_validation: true,
|
144
|
+
check_keys: false,
|
145
|
+
validating_keys: false)
|
146
|
+
rescue ::Mongo::Error::OperationFailure => e
|
147
|
+
logger.warn " updating #{vm} failed with: #{e.message}"
|
148
|
+
# mongodb doesn't support keys with a dot
|
149
|
+
# see https://docs.mongodb.com/manual/reference/limits/#Restrictions-on-Field-Names
|
150
|
+
# as a dirty workaround we delete the document and insert it ;-)
|
151
|
+
# The dotted field .. in .. is not valid for storage. (57)
|
152
|
+
# .. is an illegal key in MongoDB. Keys may not start with '$' or contain a '.'.
|
153
|
+
# (BSON::String::IllegalKey)
|
154
|
+
raise e unless e.message =~ /The dotted field / || e.message =~ /is an illegal key/
|
155
|
+
logger.warn " we transform the dots into underline characters"
|
156
|
+
begin
|
157
|
+
facts = Hash[facts.map { |k, v| [k.tr('.', '_'), v] }]
|
158
|
+
connection[vms_collection].find(_id: vm).replace_one(facts,
|
159
|
+
upsert: true,
|
160
|
+
bypass_document_validation: true,
|
161
|
+
check_keys: false,
|
162
|
+
validating_keys: false)
|
163
|
+
rescue
|
164
|
+
logger.error " inserting #{vm} failed again with: #{e.message}"
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
# delete vm data for given vm name
|
169
|
+
#
|
170
|
+
# @param vm [String] vm name
|
171
|
+
def vm_delete(vm)
|
172
|
+
connection[vms_collection].find(_id: vm).delete_one
|
173
|
+
end
|
174
|
+
|
175
|
+
# update or insert timestamps for given fact update method
|
176
|
+
def meta_fact_update(method, ts_begin, ts_end)
|
177
|
+
connection[meta_collection].find_one_and_update(
|
178
|
+
{},
|
179
|
+
{
|
180
|
+
'$set' => {
|
181
|
+
last_fact_update: {
|
182
|
+
ts_begin: ts_begin.iso8601,
|
183
|
+
ts_end: ts_end.iso8601,
|
184
|
+
method: method
|
185
|
+
},
|
186
|
+
method => {
|
187
|
+
ts_begin: ts_begin.iso8601,
|
188
|
+
ts_end: ts_end.iso8601
|
189
|
+
}
|
190
|
+
}
|
191
|
+
},
|
192
|
+
{ upsert: true }
|
193
|
+
)
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require_relative "logging"
|
2
|
+
|
3
|
+
module VcenterLibMongodb
|
4
|
+
# update vms data from source to destination
|
5
|
+
class Updater
|
6
|
+
include Logging
|
7
|
+
|
8
|
+
attr_reader :source
|
9
|
+
attr_reader :destination
|
10
|
+
|
11
|
+
def initialize(source, destination)
|
12
|
+
@source = source
|
13
|
+
@destination = destination
|
14
|
+
end
|
15
|
+
|
16
|
+
# update by deleting missing vms and get a complete map of vms with facts
|
17
|
+
# and update or insert facts for each one
|
18
|
+
#
|
19
|
+
# for example: 1633 vms in 60.70 seconds
|
20
|
+
def update
|
21
|
+
logger.info "update started (full update)"
|
22
|
+
tsb = Time.now
|
23
|
+
complete = source.facts
|
24
|
+
source_vms = complete.keys
|
25
|
+
destination_vms = destination.all_vms
|
26
|
+
delete_missing(destination_vms, source_vms)
|
27
|
+
errors = false
|
28
|
+
|
29
|
+
complete.each do |vm, facts|
|
30
|
+
begin
|
31
|
+
destination.vm_update(vm, facts)
|
32
|
+
rescue
|
33
|
+
errors = true
|
34
|
+
logger.error $!
|
35
|
+
pp facts
|
36
|
+
end
|
37
|
+
end
|
38
|
+
tse = Time.now
|
39
|
+
logger.info "update updated #{source_vms.size} vms in #{tse - tsb}"
|
40
|
+
if errors
|
41
|
+
logger.error "we don't update metadata information due to update errors"
|
42
|
+
else
|
43
|
+
destination.meta_fact_update("update", tsb, tse)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def delete_missing(destination_vms, source_vms)
|
50
|
+
missing = destination_vms - source_vms
|
51
|
+
missing.each do |vm|
|
52
|
+
destination.vm_delete(vm)
|
53
|
+
end
|
54
|
+
logger.info " deleted #{missing.size} vms"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
# library for easy acces to vcenter informations
|
4
|
+
module VcenterLibMongodb
|
5
|
+
require_relative 'vcenter_lib_mongodb/logging'
|
6
|
+
require_relative 'vcenter_lib_mongodb/version'
|
7
|
+
require_relative 'vcenter_lib_mongodb/mongodb'
|
8
|
+
require_relative 'vcenter_lib_mongodb/updater'
|
9
|
+
|
10
|
+
def logger
|
11
|
+
unless @logger
|
12
|
+
@logger = Logger.new(STDOUT)
|
13
|
+
@logger.level = Logger::INFO
|
14
|
+
end
|
15
|
+
|
16
|
+
@logger
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
|
3
|
+
# release gem to rubygems
|
4
|
+
|
5
|
+
# exit on any error
|
6
|
+
set -e
|
7
|
+
|
8
|
+
#############################################################################
|
9
|
+
## defaults
|
10
|
+
|
11
|
+
readonly progname=$(basename $0)
|
12
|
+
readonly script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
|
13
|
+
readonly main_dir=$( cd "${script_dir}" && cd .. && pwd )
|
14
|
+
readonly application=$(basename ${main_dir})
|
15
|
+
readonly module_version=$( cd "${main_dir}" && grep spec.version *.gemspec | grep -o '[=].*[^ ]' | tr -d "= ")
|
16
|
+
|
17
|
+
#############################################################################
|
18
|
+
## functions
|
19
|
+
|
20
|
+
## write green message with time stamp to stdout
|
21
|
+
function puts () {
|
22
|
+
echo -e "\033[0;32m"$(date +"%Y-%m-%d %T") $*"\033[0m"
|
23
|
+
}
|
24
|
+
|
25
|
+
## bump version, must be in main git directory
|
26
|
+
function bump_version () {
|
27
|
+
puts "bump ${application} gem version"
|
28
|
+
|
29
|
+
old_version=$( ruby -I lib/${application} -e "require 'version'; puts Gem::Version.new(${module_version})" )
|
30
|
+
puts "gem version currently:" ${old_version}
|
31
|
+
|
32
|
+
new_version=$( ruby -I lib/${application} -e "require 'version'; puts Gem::Version.new(${module_version} + '.1').bump" )
|
33
|
+
puts "we will change it into:" ${new_version}
|
34
|
+
|
35
|
+
cat lib/${application}/version.rb | sed "s/$old_version/$new_version/" > lib/${application}/version.rb.new
|
36
|
+
mv lib/${application}/version.rb.new lib/${application}/version.rb
|
37
|
+
}
|
38
|
+
|
39
|
+
## commit and push version, must be in main git directory
|
40
|
+
function commit_and_push_version () {
|
41
|
+
puts "commit and push ${application} gem version"
|
42
|
+
git add lib/${application}/version.rb
|
43
|
+
git commit -m "m31's version bumper"
|
44
|
+
git push
|
45
|
+
}
|
46
|
+
|
47
|
+
#############################################################################
|
48
|
+
## main processsing
|
49
|
+
|
50
|
+
cd ${main_dir}
|
51
|
+
|
52
|
+
puts "trying to release ${application} gem to rubygems"
|
53
|
+
puts
|
54
|
+
|
55
|
+
puts "updating git repository"
|
56
|
+
git pull -r
|
57
|
+
|
58
|
+
puts "build new Gemfile.lock with bundle install"
|
59
|
+
rm Gemfile.lock
|
60
|
+
bundle install
|
61
|
+
|
62
|
+
puts "check and test"
|
63
|
+
bundle exec rake spec
|
64
|
+
|
65
|
+
bump_version
|
66
|
+
commit_and_push_version
|
67
|
+
|
68
|
+
puts "build gem"
|
69
|
+
result=$( gem build ${application}.gemspec )
|
70
|
+
puts " ${result}"
|
71
|
+
|
72
|
+
# get gem file name
|
73
|
+
set +e
|
74
|
+
gem_file=$( echo $result | grep -o "${application}-[.0-9]*\.gem$" )
|
75
|
+
set -e
|
76
|
+
puts " gem file:" $gem_file
|
77
|
+
|
78
|
+
## check if group was specified
|
79
|
+
if [ -z "${gem_file}" ]; then
|
80
|
+
echo "generated gem file not found" >&2
|
81
|
+
exit 1
|
82
|
+
fi
|
83
|
+
|
84
|
+
puts "push gem to rubygems"
|
85
|
+
gem push ${gem_file} --host https://rubygems.org
|
86
|
+
|
87
|
+
echo -e "\033[0;34mThe lioness has rejoined her cub, and all is right in the jungle...\033[0m"
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require File.expand_path('../lib/vcenter_lib_mongodb/version', __FILE__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'vcenter_lib_mongodb'
|
7
|
+
spec.version = VcenterLibMongodb::VERSION
|
8
|
+
spec.authors = ['Michael Meyling']
|
9
|
+
spec.email = ['search@meyling.com']
|
10
|
+
spec.summary = 'save and synchronize vcenter information in a mongodb'
|
11
|
+
spec.description = 'We will see what we can do.'
|
12
|
+
spec.homepage = 'https://github.com/m-31/vcenter_lib_mongodb'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.bindir = 'exe'
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'vcenter_lib'
|
22
|
+
spec.add_dependency 'mongo'
|
23
|
+
spec.add_dependency 'vault'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler'
|
26
|
+
spec.add_development_dependency 'rspec'
|
27
|
+
spec.add_development_dependency 'pry'
|
28
|
+
spec.add_development_dependency 'webmock'
|
29
|
+
spec.add_development_dependency 'simplecov', '~> 0.9.0'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vcenter_lib_mongodb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Meyling
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: vcenter_lib
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mongo
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: vault
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: webmock
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: simplecov
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.9.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.9.0
|
125
|
+
description: We will see what we can do.
|
126
|
+
email:
|
127
|
+
- search@meyling.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rubocop.yml"
|
134
|
+
- ".travis.yml"
|
135
|
+
- Gemfile
|
136
|
+
- LICENSE.txt
|
137
|
+
- README.md
|
138
|
+
- Rakefile
|
139
|
+
- bin/console
|
140
|
+
- bin/setup
|
141
|
+
- lib/vcenter_lib_mongodb.rb
|
142
|
+
- lib/vcenter_lib_mongodb/logging.rb
|
143
|
+
- lib/vcenter_lib_mongodb/mongodb.rb
|
144
|
+
- lib/vcenter_lib_mongodb/updater.rb
|
145
|
+
- lib/vcenter_lib_mongodb/version.rb
|
146
|
+
- scripts/deploy_gem.sh
|
147
|
+
- spec/classes/spec_helper.rb
|
148
|
+
- vcenter_lib_mongodb.gemspec
|
149
|
+
homepage: https://github.com/m-31/vcenter_lib_mongodb
|
150
|
+
licenses:
|
151
|
+
- MIT
|
152
|
+
metadata: {}
|
153
|
+
post_install_message:
|
154
|
+
rdoc_options: []
|
155
|
+
require_paths:
|
156
|
+
- lib
|
157
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
158
|
+
requirements:
|
159
|
+
- - ">="
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
version: '0'
|
162
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
requirements: []
|
168
|
+
rubyforge_project:
|
169
|
+
rubygems_version: 2.6.8
|
170
|
+
signing_key:
|
171
|
+
specification_version: 4
|
172
|
+
summary: save and synchronize vcenter information in a mongodb
|
173
|
+
test_files:
|
174
|
+
- spec/classes/spec_helper.rb
|