gv-common 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 21725caba627d162a68c133d8421979e61948db9
4
+ data.tar.gz: 93e5c2ce9e8d978c9dafe2f2338927f5769e4b8d
5
+ SHA512:
6
+ metadata.gz: 90b93866023cb56d08c0eee1f5869cf9365d08394e3eb590b3b9a9c9e0625e40a54c5d17d591f789c0021af2f0071ec7c35d42007f319572612f7f82c35e0df0
7
+ data.tar.gz: d5929518290d73ee6d2ce9053eae33c0a86418e226b277e2bb033e685a9a7660e2d354e526991a4a98ef9f4fabcc5c7e197a8963bd7094c20fb8f20fa003c1e4
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gv-common.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org>
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # GV::Common
2
+
3
+ Green Valley Shared Library
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'gv-common'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install gv-common
18
+
19
+ ## Usage
20
+
21
+ include 'gv/common/$libname'
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/gv-common/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/**/test*.rb']
7
+ end
8
+
9
+ task :default => :test
data/gv-common.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gv/common/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gv-common"
8
+ spec.version = GV::Common::VERSION
9
+ spec.authors = ["Onur Uyar"]
10
+ spec.email = ["me@onuruyar.com"]
11
+ spec.summary = %q{GreenValley Shared Library}
12
+ spec.homepage = "https://github.com/green-valley/gv-common"
13
+ spec.license = "Unlicense"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.5"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "minitest"
23
+ spec.add_dependency "activesupport"
24
+ spec.add_dependency "sticks-pipe"
25
+ spec.add_dependency "commander"
26
+ end
@@ -0,0 +1,75 @@
1
+ $stdout.sync = true
2
+ $stderr.sync = true
3
+
4
+ module GV
5
+ module Common
6
+ module CLI
7
+ module Server
8
+
9
+ module_function
10
+
11
+ def gen lib, services
12
+
13
+ require "gv/#{lib}"
14
+ require "commander/import"
15
+ require "active_support/inflector"
16
+
17
+ program :name, "Green Valley #{lib.capitalize}"
18
+ program :version, '0.0.1'
19
+ program :description, 'Green Valley #{lib.capitalize} CLI'
20
+
21
+ command :provide do |c|
22
+ c.syntax = "gv-#{lib} provide SERVICE"
23
+ c.description = 'Provides a service'
24
+ c.action do |args, options|
25
+ service = args.shift
26
+ require service
27
+ service_class = service.classify.gsub("Gv","GV").constantize
28
+ service_class.provide
29
+ end
30
+ end
31
+
32
+ command :'service' do |c|
33
+ c.syntax = "gv-#{lib} service SERVICE METHOD <ARGS>"
34
+ c.description = 'Runs method on remote service'
35
+ c.action do |args, options|
36
+
37
+ service = args.shift
38
+ method = args.shift
39
+ require service
40
+ service_class = service.classify.gsub("Gv","GV").constantize
41
+
42
+ DRb.start_service
43
+ service = service_class.service
44
+ result = service.public_send(method,*args)
45
+ puts result
46
+ DRb.stop_service
47
+
48
+ end
49
+ end
50
+
51
+ command :run do |c|
52
+ c.syntax = "gv-#{lib} run"
53
+ c.description = 'Runs everything'
54
+ c.action do |args, options|
55
+
56
+ pids = []
57
+ services.each do |service|
58
+ pids << spawn("gv-#{lib} provide #{service}")
59
+ Process.detach(pids.last)
60
+ sleep 2
61
+ end
62
+
63
+ at_exit { pids.each{|pid| Process.kill "TERM", pid} }
64
+
65
+ loop do; end
66
+
67
+ end
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,53 @@
1
+ # Colorizes the output of the standard library logger, depending on the logger level:
2
+ # To adjust the colors, look at Logger::Colors::SCHEMA and Logger::Colors::constants
3
+
4
+ require 'logger'
5
+
6
+ class Logger
7
+ module Colors
8
+ VERSION = '1.0.0'
9
+
10
+ NOTHING = '0;0'
11
+ BLACK = '0;30'
12
+ RED = '0;31'
13
+ GREEN = '0;32'
14
+ BROWN = '0;33'
15
+ BLUE = '0;34'
16
+ PURPLE = '0;35'
17
+ CYAN = '0;36'
18
+ LIGHT_GRAY = '0;37'
19
+ DARK_GRAY = '1;30'
20
+ LIGHT_RED = '1;31'
21
+ LIGHT_GREEN = '1;32'
22
+ YELLOW = '1;33'
23
+ LIGHT_BLUE = '1;34'
24
+ LIGHT_PURPLE = '1;35'
25
+ LIGHT_CYAN = '1;36'
26
+ WHITE = '1;37'
27
+
28
+ SCHEMA = {
29
+ STDOUT => %w[nothing green brown red purple cyan],
30
+ STDERR => %w[nothing green yellow light_red light_purple light_cyan],
31
+ }
32
+ end
33
+ end
34
+
35
+ class Logger
36
+ alias format_message_colorless format_message
37
+
38
+ def format_message(level, *args)
39
+ if Logger::Colors::SCHEMA[@logdev.dev]
40
+ color = begin
41
+ Logger::Colors.const_get \
42
+ Logger::Colors::SCHEMA[@logdev.dev][Logger.const_get(level.sub "ANY","UNKNOWN")].to_s.upcase
43
+ rescue NameError
44
+ "0;0"
45
+ end
46
+ "\e[#{ color }m#{ format_message_colorless(level, *args) }\e[0;0m"
47
+ else
48
+ format_message_colorless(level, *args)
49
+ end
50
+ end
51
+ end
52
+
53
+ # J-_-L
@@ -0,0 +1,73 @@
1
+ require 'gv/common/pipe_helper'
2
+ require 'json'
3
+ require 'timeout'
4
+
5
+ module GV
6
+ module Common
7
+ module DockerHelper
8
+
9
+ include GV::Common::PipeHelper
10
+
11
+ SPAWN_TIMEOUT = 30
12
+
13
+ module_function
14
+
15
+ def info container_id
16
+ JSON.load(pipe("docker inspect #{container_id}")).first rescue nil
17
+ end
18
+
19
+ def ps name
20
+ pipe("docker ps | grep #{name}")
21
+ end
22
+
23
+ def ps? name
24
+ pipe("docker ps | grep #{name}") =~ /#{name}/
25
+ end
26
+
27
+ def container_id name
28
+ pipe "docker ps | grep #{name} | awk '{ print $1 }'"
29
+ end
30
+
31
+ def container_port name, ip, port
32
+ pipe "docker port #{container_id(name)} #{port} | sed 's/#{ip}://'"
33
+ end
34
+
35
+ def pull_image_if_does_not_exists name
36
+ pipe("docker pull #{name}", &Blocks.stream) unless pipe("docker images") =~ /#{name}/
37
+ end
38
+
39
+ def cleanup
40
+ batch "Exit", "rm", false
41
+ end
42
+
43
+ def batch pattern, method, wait = false
44
+ sh "docker ps -a | grep #{pattern} | awk '{ print $1 }' | xargs docker #{method}", {err: "/dev/null"}, wait
45
+ end
46
+
47
+ def sh cmd, options = {}, wait = true
48
+ begin
49
+ ::Timeout::timeout(SPAWN_TIMEOUT) {
50
+ debug "SH #{cmd}"
51
+ pid = spawn cmd, options
52
+ Process.public_send(wait ? :wait : :detach, pid)
53
+ }
54
+ rescue ::Timeout::Error,::Timeout::TimeoutError,::Object::TimeoutError
55
+ restart_docker!
56
+ sh cmd, options, wait
57
+ end
58
+ end
59
+
60
+ def restart_docker! container_id = nil, sec = 2
61
+ pid, status = Process.wait2(spawn("service docker restart"))
62
+ unless status.success?
63
+ fatal "docker service cannot restarted, terminating..."
64
+ raise container_id if container_id
65
+ else
66
+ sleep sec
67
+ end
68
+ end
69
+
70
+
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,28 @@
1
+ require 'sticks/pipe'
2
+ require 'open3'
3
+
4
+ module GV
5
+ module Common
6
+ module HostHelper
7
+
8
+ include Sticks::Pipe
9
+
10
+ ##
11
+ # Several configuration helpers
12
+ #
13
+
14
+ def external_ip interface = "eth0"
15
+ @external_ip ||= pipe("/sbin/ifconfig #{interface} | grep 'inet addr' | awk -F: '{print $2}' | awk '{print $1}'")
16
+ end
17
+
18
+ def hostname
19
+ @hostname ||= pipe("hostname -f")
20
+ end
21
+
22
+ def port
23
+ @port ||= ENV['PORT']
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,21 @@
1
+ require 'gv/common/core_ext/logger'
2
+
3
+ module GV
4
+ module Common
5
+ module Logging
6
+
7
+ module_function
8
+
9
+ def logger
10
+ $logger ||= logger = Logger.new($stderr)
11
+ end
12
+
13
+ %w(debug info warn error fatal).each do |met|
14
+ define_method(met){ |msg| logger.public_send(met,msg) }
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+
21
+ Object.send :include, GV::Common::Logging
@@ -0,0 +1,30 @@
1
+ require 'gv/bedrock/server'
2
+
3
+ class Minitest::Test
4
+
5
+ def provide service_class
6
+ pid = fork {
7
+ service_class.provide
8
+ }
9
+ Process.detach pid
10
+ sleep 2
11
+ pid
12
+ end
13
+
14
+ def kll pid
15
+ Process.kill "TERM", pid rescue nil
16
+ end
17
+
18
+ def start_server
19
+ @serverpid = fork {
20
+ server = GV::Bedrock::Server.new
21
+ server.start
22
+ }
23
+ sleep 2
24
+ end
25
+
26
+ def stop_server
27
+ kll @serverpid
28
+ end
29
+
30
+ end
@@ -0,0 +1,21 @@
1
+ require 'sticks/pipe'
2
+
3
+ module GV
4
+ module Common
5
+ module PipeHelper
6
+
7
+ include Sticks::Pipe
8
+
9
+ module_function
10
+
11
+ def indicate string
12
+ say %(-----> #{string}), &@block
13
+ end
14
+
15
+ def say string
16
+ pipe %(echo '\e[1G#{string}'), &@block
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ module GV
2
+ module Common
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/lib/gv/common.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "gv/common/version"
2
+
3
+ module GV
4
+ module Common
5
+ end
6
+ end
@@ -0,0 +1,16 @@
1
+ require 'minitest_helper'
2
+ require 'gv/common/logging'
3
+
4
+ module GV
5
+ module Common
6
+ class TestLogging < Minitest::Test
7
+
8
+ def test_methods
9
+ %w(debug info warn error fatal).each do |met|
10
+ public_send(met,"test")
11
+ end
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'gv/common'
3
+
4
+ require 'minitest/autorun'
5
+ require 'minitest/pride'
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gv-common
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Onur Uyar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: sticks-pipe
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
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: commander
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - me@onuruyar.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .travis.yml
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - gv-common.gemspec
111
+ - lib/gv/common.rb
112
+ - lib/gv/common/cli/server.rb
113
+ - lib/gv/common/core_ext/logger.rb
114
+ - lib/gv/common/docker_helper.rb
115
+ - lib/gv/common/host_helper.rb
116
+ - lib/gv/common/logging.rb
117
+ - lib/gv/common/minitest_helper.rb
118
+ - lib/gv/common/pipe_helper.rb
119
+ - lib/gv/common/version.rb
120
+ - test/gv/test_logging.rb
121
+ - test/minitest_helper.rb
122
+ homepage: https://github.com/green-valley/gv-common
123
+ licenses:
124
+ - Unlicense
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.2.2
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: GreenValley Shared Library
146
+ test_files:
147
+ - test/gv/test_logging.rb
148
+ - test/minitest_helper.rb