navyrb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ errors.err
2
+ .cadre
3
+ .bundle
4
+ *.swp
5
+ coverage
6
+ pkg
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ require 'cadre/simplecov'
2
+
3
+ #SimpleCov.start 'rails' do #if, you know: Rails.
4
+ SimpleCov.start do
5
+ formatter SimpleCov::Formatter::MultiFormatter[
6
+ SimpleCov::Formatter::HTMLFormatter,
7
+ Cadre::SimpleCov::VimFormatter
8
+ ]
9
+ end
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rspec'
4
+ gem 'guard-rspec'
5
+
6
+ gem 'simplecov'
7
+ gem 'cadre'
8
+
9
+ # Specify your gem's dependencies in navyrb.gemspec
10
+ gemspec
@@ -0,0 +1,87 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ navyrb (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.3.6)
10
+ cadre (0.3.1)
11
+ thor (>= 0.18.1, < 1.0)
12
+ tilt (~> 1.0)
13
+ valise (~> 1.0)
14
+ celluloid (0.16.0)
15
+ timers (~> 4.0.0)
16
+ coderay (1.1.0)
17
+ crack (0.4.1)
18
+ safe_yaml (~> 0.9.0)
19
+ diff-lcs (1.2.5)
20
+ docile (1.1.5)
21
+ ffi (1.9.3)
22
+ formatador (0.2.5)
23
+ guard (2.6.1)
24
+ formatador (>= 0.2.4)
25
+ listen (~> 2.7)
26
+ lumberjack (~> 1.0)
27
+ pry (>= 0.9.12)
28
+ thor (>= 0.18.1)
29
+ guard-rspec (4.3.1)
30
+ guard (~> 2.1)
31
+ rspec (>= 2.14, < 4.0)
32
+ hitimes (1.2.2)
33
+ listen (2.7.9)
34
+ celluloid (>= 0.15.2)
35
+ rb-fsevent (>= 0.9.3)
36
+ rb-inotify (>= 0.9)
37
+ lumberjack (1.0.9)
38
+ method_source (0.8.2)
39
+ multi_json (1.10.1)
40
+ pry (0.10.1)
41
+ coderay (~> 1.1.0)
42
+ method_source (~> 0.8.1)
43
+ slop (~> 3.4)
44
+ rake (10.3.2)
45
+ rb-fsevent (0.9.4)
46
+ rb-inotify (0.9.5)
47
+ ffi (>= 0.5.0)
48
+ rspec (3.1.0)
49
+ rspec-core (~> 3.1.0)
50
+ rspec-expectations (~> 3.1.0)
51
+ rspec-mocks (~> 3.1.0)
52
+ rspec-core (3.1.3)
53
+ rspec-support (~> 3.1.0)
54
+ rspec-expectations (3.1.1)
55
+ diff-lcs (>= 1.2.0, < 2.0)
56
+ rspec-support (~> 3.1.0)
57
+ rspec-mocks (3.1.0)
58
+ rspec-support (~> 3.1.0)
59
+ rspec-support (3.1.0)
60
+ safe_yaml (0.9.7)
61
+ simplecov (0.9.0)
62
+ docile (~> 1.1.0)
63
+ multi_json
64
+ simplecov-html (~> 0.8.0)
65
+ simplecov-html (0.8.0)
66
+ slop (3.6.0)
67
+ thor (0.19.1)
68
+ tilt (1.4.1)
69
+ timers (4.0.1)
70
+ hitimes
71
+ valise (1.1.1)
72
+ webmock (1.17.1)
73
+ addressable (>= 2.2.7)
74
+ crack (>= 0.3.2)
75
+
76
+ PLATFORMS
77
+ ruby
78
+
79
+ DEPENDENCIES
80
+ bundler (~> 1.6)
81
+ cadre
82
+ guard-rspec
83
+ navyrb!
84
+ rake (~> 10.0)
85
+ rspec
86
+ simplecov
87
+ webmock
@@ -0,0 +1,17 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ # Note: The cmd option is now required due to the increasing number of ways
5
+ # rspec may be run, below are examples of the most common uses.
6
+ # * bundler: 'bundle exec rspec'
7
+ # * bundler binstubs: 'bin/rspec'
8
+ # * spring: 'bin/rsspec' (This will use spring if running and you have
9
+ # installed the spring binstubs per the docs)
10
+ # * zeus: 'zeus rspec' (requires the server to be started separetly)
11
+ # * 'just' rspec: 'rspec'
12
+ guard :rspec, cmd: 'bundle exec rspec' do
13
+ watch(%r{^spec/.+_spec\.rb$})
14
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
15
+ watch('spec/spec_helper.rb') { "spec" }
16
+ end
17
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
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
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ # Navyrb
2
+
3
+ Navy library to talk to navy services from ruby
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'navyrb'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install navyrb
21
+
22
+ ## Contributing
23
+
24
+ 1. Fork it ( https://github.com/[my-github-username]/navyrb/fork )
25
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
26
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
27
+ 4. Push to the branch (`git push origin my-new-feature`)
28
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,15 @@
1
+ require "navy/version"
2
+
3
+ module Navy
4
+ autoload :Etcd, 'navy/etcd'
5
+ autoload :Logger, 'navy/logger'
6
+ autoload :Configuration, 'navy/configuration'
7
+ autoload :Application, 'navy/application'
8
+ autoload :CommandBuilder, 'navy/command_builder'
9
+ autoload :Router, 'navy/router'
10
+ autoload :Runner, 'navy/runner'
11
+ autoload :Container, 'navy/container'
12
+ autoload :ContainerBuilding, 'navy/container_building'
13
+ autoload :AppContainerBuilder, 'navy/app_container_builder'
14
+ autoload :TaskContainerBuilder, 'navy/task_container_builder'
15
+ end
@@ -0,0 +1,72 @@
1
+ class Navy::AppContainerBuilder
2
+ include Navy::ContainerBuilding
3
+
4
+ attr_reader :specification, :dependencies, :options
5
+
6
+ def initialize(app, config, options)
7
+ @options = options.dup
8
+ @options[:app] = app
9
+ @options[:config] = config
10
+ end
11
+
12
+ def build
13
+ @specification = {
14
+ :type => "application",
15
+ :env => {},
16
+ :links => [],
17
+ :volumes_from => []
18
+ }
19
+ @dependencies = []
20
+ app = options.delete :app
21
+ config = options.delete :config
22
+ @options = options
23
+ build_specification(app, config)
24
+ build_dependencies(app, config)
25
+ build_container
26
+ end
27
+
28
+ private
29
+
30
+ def build_dependencies(app, config)
31
+ build_app_dependencies(app, config)
32
+ build_task_dependencies(app, config)
33
+ end
34
+
35
+
36
+ def build_task_dependencies(app, config)
37
+ tasks = config.pre_tasks(app.name)
38
+ unless tasks.empty?
39
+ flags = {:mode => :pretasks, :convoy => options[:convoy]}
40
+ dependencies << config.container_name(app.name, flags)
41
+ end
42
+ end
43
+
44
+ def build_specification(app, config)
45
+ @convoy_id = options[:convoy]
46
+ @cluster = options[:cluster]
47
+ specification[:name] = app.name
48
+ specification[:container_name] = config.container_name(app.name, options)
49
+ specification[:image] = app.image
50
+ mode = options[:mode]
51
+ specification[:mode] = mode
52
+ if app.modes
53
+ specification[:cmd] = app.modes[mode]
54
+ end
55
+ build_app_links(app, config)
56
+ build_dep_links(app, config)
57
+ build_env_flags(app, config)
58
+ build_proxy_flags(app, config)
59
+ build_volumes_flags(app, config)
60
+ build_additional_args(app, config)
61
+ end
62
+
63
+ def build_proxy_flags(app, config)
64
+ mode = options[:mode]
65
+ if app.proxy_to?(mode)
66
+ fqdn = app_fqdn(app.name, config)
67
+ port = app.proxy_port(mode)
68
+ specification[:env]['VIRTUAL_HOST'] = fqdn
69
+ specification[:env]['VIRTUAL_PORT'] = port
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,63 @@
1
+ module Navy
2
+ class Application
3
+ attr_reader :name
4
+
5
+ def initialize(name, settings)
6
+ @name = name
7
+ @settings = settings
8
+ end
9
+
10
+ def image
11
+ @settings['image']
12
+ end
13
+
14
+ def modes
15
+ @settings['modes']
16
+ end
17
+
18
+ def volumes_from
19
+ @settings['volumes_from'] || []
20
+ end
21
+
22
+ def links
23
+ @settings['links'] || []
24
+ end
25
+
26
+ def dependencies(config)
27
+ apps = config.applications
28
+ links - apps
29
+ end
30
+
31
+ def linked_apps(config)
32
+ apps = config.applications
33
+ links = @settings['links'] || []
34
+ links & apps
35
+ end
36
+
37
+ def env_var?
38
+ !!@settings['env_var']
39
+ end
40
+
41
+ def env_var
42
+ @settings['env_var']
43
+ end
44
+
45
+ def proxy_to?(mode=nil)
46
+ if mode
47
+ proxy = @settings['proxy_to'] || {}
48
+ proxy.keys.include? mode
49
+ else
50
+ @settings['proxy_to']
51
+ end
52
+ end
53
+
54
+ def proxy_port(mode=nil)
55
+ if mode
56
+ proxy = @settings['proxy_to'] || {}
57
+ proxy[mode]
58
+ else
59
+ @settings['proxy_to']
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,73 @@
1
+ module Navy
2
+ class CommandBuilder
3
+
4
+ attr_reader :container, :spec
5
+
6
+ def initialize(container)
7
+ @container = container
8
+ @spec = container.specification
9
+ end
10
+
11
+ def build(options = {})
12
+ cmd = []
13
+ docker_run(cmd)
14
+ docker_name(cmd)
15
+ docker_links(cmd)
16
+ docker_env(cmd)
17
+ docker_volumes_from(cmd)
18
+ docker_misc_args(cmd)
19
+
20
+ docker_image(cmd)
21
+ execute_command(cmd, options)
22
+ cmd.compact
23
+ end
24
+
25
+ private
26
+
27
+ def docker_run(cmd)
28
+ if container.daemon?
29
+ cmd << "docker run -d"
30
+ else
31
+ cmd << "docker run --rm"
32
+ end
33
+ end
34
+
35
+ def docker_image(cmd)
36
+ cmd << spec[:image]
37
+ end
38
+
39
+ def docker_name(cmd)
40
+ cmd << "--name #{spec[:container_name]}"
41
+ end
42
+
43
+ def docker_links(cmd)
44
+ links = spec[:links] || []
45
+ links.each do |link|
46
+ from, to = link
47
+ cmd << "--link=#{from}:#{to}"
48
+ end
49
+ end
50
+
51
+ def docker_env(cmd)
52
+ envvars = spec[:env] || {}
53
+ envvars.each do |key, value|
54
+ cmd << "-e=\"#{key}=#{value}\""
55
+ end
56
+ end
57
+
58
+ def docker_volumes_from(cmd)
59
+ vols = spec[:volumes_from] || []
60
+ vols.each do |vol|
61
+ cmd << "--volumes-from=#{vol}"
62
+ end
63
+ end
64
+
65
+ def docker_misc_args(cmd)
66
+ cmd << spec[:docker_args]
67
+ end
68
+
69
+ def execute_command(cmd, options)
70
+ cmd << (options[:command] || spec[:cmd])
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,102 @@
1
+ require 'yaml'
2
+
3
+ module Navy
4
+ class Configuration
5
+ attr_reader :definition, :environment
6
+
7
+ def initialize(definition)
8
+ @definition = definition
9
+ @apps = parse_apps
10
+ end
11
+
12
+ def self.from_string(yaml_content)
13
+ cfg = YAML.load(yaml_content)
14
+ new(cfg)
15
+ end
16
+
17
+ def self.from_file(config_file)
18
+ cfg = YAML.load_file(config_file)
19
+ new(cfg)
20
+ end
21
+
22
+ def apps
23
+ @apps.each do |app|
24
+ yield app if block_given?
25
+ end
26
+ end
27
+
28
+ def set_env(env)
29
+ @environment = env
30
+ @env = environments[environment] || {}
31
+ @deps = parse_deps
32
+ @pre = parse_pre_tasks
33
+ @post = parse_post_tasks
34
+ @docker_args = parse_docker_args
35
+ end
36
+
37
+ def container_name(app, options={})
38
+ mode = options[:mode]
39
+ convoy = options[:convoy]
40
+ scale = options[:scale]
41
+ [convoy, app, mode, scale].compact.join '_'
42
+ end
43
+
44
+ def applications
45
+ @apps.map &:name
46
+ end
47
+
48
+ def find_app(name)
49
+ @apps.detect {|a| a.name == name }
50
+ end
51
+
52
+ def dependencies
53
+ @deps.each do |app|
54
+ yield app if block_given?
55
+ end
56
+ end
57
+
58
+ def pre_tasks(app)
59
+ @pre[app] || []
60
+ end
61
+
62
+ def post_tasks(app)
63
+ @post[app] || []
64
+ end
65
+
66
+ def docker_args(app)
67
+ @docker_args[app]
68
+ end
69
+
70
+ private
71
+
72
+ def environments
73
+ definition['environments'] || {}
74
+ end
75
+
76
+ def parse_apps
77
+ definition['apps'].map do |name, settings|
78
+ Application.new(name, settings)
79
+ end
80
+ end
81
+
82
+ def parse_deps
83
+ deps = @env['dependencies'] || {}
84
+ deps.map do |name, settings|
85
+ Application.new(name, settings)
86
+ end
87
+ end
88
+
89
+ def parse_pre_tasks
90
+ @env['pre'] || {}
91
+ end
92
+
93
+ def parse_post_tasks
94
+ @env['post'] || {}
95
+ end
96
+
97
+ def parse_docker_args
98
+ @env['docker'] || {}
99
+ end
100
+
101
+ end
102
+ end