nib-integrate 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8e73989921e020f7c4c6d067db840a0995bd39c5a00498c2c02a45d6093b0094
4
+ data.tar.gz: c59a466ca7da71a0f8b295ea74992d0a21a75516461c318359830dbbe7cdfc9a
5
+ SHA512:
6
+ metadata.gz: 3ebc92e7470cae10c6077a4b4cf387c21552bcade4467041daecd8758be5b5515716eb025d80c96abd8bd557806ac7fc72b0903e6b85205a6161fc9ab88608c2
7
+ data.tar.gz: 74cb44ad303b7e64d47430739005b5a8d0a0b60289c031a552bebcfcd9aa5edc44060b5b485d0a763723595e80d3d6881497135ab7c93b9a9daa76c0dbacf684
data/bin/nib-integrate ADDED
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'nib_integrate_plugin'
4
+
5
+ desc 'integrate two or more docker-compose applications'
6
+ # rubocop:disable Metrics/BlockLength
7
+ command :integrate do |c|
8
+ c.desc 'intialize gem and create config file'
9
+ c.command :init do |ic|
10
+ ic.action do |_global_options, _options, _args|
11
+ next if File.exist?(Nib::Integrate::ConfigFile::PATH)
12
+ Nib::Integrate::ConfigFile
13
+ .write(Nib::Integrate::ConfigFile::DEFAULT_CONFIG)
14
+ Nib::Integrate::NetworkCreator.call
15
+ end
16
+ end
17
+
18
+ c.desc 'register an application to integrate'
19
+ c.command :register do |rc|
20
+ rc.flag %i[a app], description: 'the app to register', required: true
21
+ rc.flag [:p, :app_path, 'app-path'],
22
+ description: 'the application path', required: true
23
+ rc.flag [:s, :service_name, 'service-name'],
24
+ description: 'the service name to use', required: true
25
+ rc.flag [:d, :docker_compose_file, 'docker-compose-file'],
26
+ description: 'docker_compose yml file',
27
+ required: false,
28
+ default_value: 'docker-compose.yml'
29
+ rc.action do |_global_options, options, _args|
30
+ Nib::Integrate::Registrar.call(options)
31
+ end
32
+ end
33
+
34
+ c.desc 'bring up one or more applications'
35
+ c.arg :apps, %i[multiple]
36
+ c.command :up do |uc|
37
+ uc.action do |_global_options, _options, args|
38
+ Nib::Integrate::Integrator.up(args).each { |command| system command }
39
+ end
40
+ end
41
+
42
+ c.desc 'bring down one or more applications'
43
+ c.arg :apps, %i[multiple]
44
+ c.command :down do |uc|
45
+ uc.action do |_global_options, _options, args|
46
+ Nib::Integrate::Integrator.down(args).each { |command| system command }
47
+ end
48
+ end
49
+
50
+ c.desc 'list registered applications'
51
+ c.command :list do |ls|
52
+ ls.action do |_global_options, _options, _args|
53
+ Nib::Integrate::Lister.call.each do |name|
54
+ puts name
55
+ end
56
+ end
57
+ end
58
+ end
59
+ # rubocop:enable Metrics/BlockLength
@@ -0,0 +1,24 @@
1
+ require 'yaml'
2
+
3
+ module Nib
4
+ # plugin module
5
+ module Integrate
6
+ # reads and writes the config file
7
+ class ConfigFile
8
+ PATH = "#{ENV['HOME']}/.nib-integrate-config".freeze
9
+ DEFAULT_CONFIG = { 'apps' => [] }.freeze
10
+ class << self
11
+ def write(config, path = PATH)
12
+ # this will write the config file.
13
+ File.open(path, 'w') do |f|
14
+ f.write(config.to_yaml)
15
+ end
16
+ end
17
+
18
+ def read(path = PATH)
19
+ YAML.load_file(path)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,94 @@
1
+ module Nib
2
+ module Integrate
3
+ # dynamically generated network config file
4
+ class IntegrationFile
5
+ class << self
6
+ def write(app_name)
7
+ new(app_name).write
8
+ end
9
+ end
10
+
11
+ attr_reader :app_name
12
+
13
+ def initialize(app_name)
14
+ @app_name = app_name
15
+ end
16
+
17
+ def write
18
+ File.open(path, 'w') do |f|
19
+ f.write(config.to_yaml)
20
+ end
21
+ path
22
+ end
23
+
24
+ private
25
+
26
+ def apps
27
+ @apps ||= global_config['apps']
28
+ end
29
+
30
+ def app
31
+ apps.find { |a| a['name'] == app_name }
32
+ end
33
+
34
+ def other_apps
35
+ apps.reject { |a| a['name'] == app_name }
36
+ end
37
+
38
+ def path
39
+ "#{ENV['HOME']}/.nib-integrate-network-config-#{app['name']}"
40
+ end
41
+
42
+ def config
43
+ app_services.each_with_object(network_config) do |elem, acc|
44
+ acc['services'][elem] = {
45
+ 'external_links' => external_links,
46
+ 'networks' => %w[default nib]
47
+ }
48
+ end
49
+ end
50
+
51
+ def external_links
52
+ other_apps.map(&external_link)
53
+ end
54
+
55
+ def external_link
56
+ lambda do |registration|
57
+ "#{container_name(registration)}_1:#{container_name(registration)}"
58
+ end
59
+ end
60
+
61
+ def container_name(registration)
62
+ project_name = registration['path'].split('/').last.gsub(/[ _]/, '')
63
+ service_name = registration['service']
64
+ "#{project_name}_#{service_name}"
65
+ end
66
+
67
+ def network_config
68
+ {
69
+ 'version' => '2',
70
+ 'services' => {},
71
+ 'networks' => {
72
+ 'nib' => { 'external' => { 'name' => 'nib-integrate-network' } }
73
+ }
74
+ }
75
+ end
76
+
77
+ def app_docker_compose
78
+ @app_docker_compose ||= app_compose_contents
79
+ end
80
+
81
+ def app_compose_contents
82
+ YAML.load_file("#{app['path']}/#{app['compose_file']}")
83
+ end
84
+
85
+ def app_services
86
+ app_docker_compose['services'].keys
87
+ end
88
+
89
+ def global_config
90
+ ConfigFile.read
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,90 @@
1
+ module Nib
2
+ module Integrate
3
+ # performs docker-compose integration
4
+ class Integrator
5
+ class << self
6
+ def up(args, config_path = Nib::Integrate::ConfigFile::PATH)
7
+ new(args, config_path).up
8
+ end
9
+
10
+ def down(args, config_path = Nib::Integrate::ConfigFile::PATH)
11
+ new(args, config_path).down
12
+ end
13
+ end
14
+
15
+ attr_reader :args, :current_arg, :config_path
16
+
17
+ def initialize(args, config_path)
18
+ @args = args
19
+ @config_path = config_path
20
+ end
21
+
22
+ def up
23
+ args.map do |arg|
24
+ @current_arg = arg
25
+ command
26
+ end
27
+ end
28
+
29
+ def down
30
+ args.map do |arg|
31
+ @current_arg = arg
32
+ down_command
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def app
39
+ apps.find { |a| a['name'] == current_arg }
40
+ end
41
+
42
+ def apps
43
+ @apps ||= config_file.read(config_path)['apps']
44
+ end
45
+
46
+ def command
47
+ [cd_command, docker_compose_command].join(' && ')
48
+ end
49
+
50
+ def down_command
51
+ [
52
+ cd_command,
53
+ "docker-compose -f #{app['compose_file']} stop"
54
+ ].join(' && ')
55
+ end
56
+
57
+ def cd_command
58
+ "cd #{app['path']}"
59
+ end
60
+
61
+ def docker_compose_command
62
+ [run_command, integration_file_flag, up_command].compact.join(' ')
63
+ end
64
+
65
+ def run_command
66
+ "docker-compose -f #{app['compose_file']}"
67
+ end
68
+
69
+ def up_command
70
+ "up -d #{app['service']}"
71
+ end
72
+
73
+ def integration_file_flag
74
+ "-f #{integration_file_path}"
75
+ end
76
+
77
+ def integration_file_path
78
+ integration_file.write(app['name'])
79
+ end
80
+
81
+ def config_file
82
+ ConfigFile
83
+ end
84
+
85
+ def integration_file
86
+ IntegrationFile
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,20 @@
1
+ module Nib
2
+ module Integrate
3
+ # lists registered applications
4
+ class Lister
5
+ class << self
6
+ def call(config_path = ConfigFile::PATH)
7
+ apps(config_path).map { |app| app['name'] }
8
+ end
9
+
10
+ def config_file
11
+ ConfigFile
12
+ end
13
+
14
+ def apps(config_path)
15
+ config_file.read(config_path)['apps']
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ module Nib
2
+ module Integrate
3
+ # NetworkCreator creates the nib-integrate-network
4
+ class NetworkCreator
5
+ class << self
6
+ def call
7
+ create_network unless network_exists?
8
+ end
9
+
10
+ private
11
+
12
+ def create_network
13
+ system 'docker network create nib-integrate-network'
14
+ end
15
+
16
+ def network_exists?
17
+ `docker network ls` =~ /nib-integrate-network/
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,69 @@
1
+ module Nib
2
+ module Integrate
3
+ # registers applications for integration
4
+ class Registrar
5
+ class << self
6
+ def call(options, config_path = ConfigFile::PATH)
7
+ new(options, config_path).call
8
+ end
9
+ end
10
+
11
+ attr_reader :options, :config_path
12
+
13
+ def initialize(options, config_path)
14
+ @options = options
15
+ @config_path = config_path
16
+ end
17
+
18
+ def call
19
+ return unless valid?
20
+ return if app_config_exists?
21
+ register_app
22
+ true
23
+ end
24
+
25
+ private
26
+
27
+ def valid?
28
+ raise 'No app name given' unless options[:a]
29
+ raise 'No path given' unless options[:p]
30
+ raise 'No service name given' unless options[:s]
31
+ true
32
+ end
33
+
34
+ def app_config_exists?
35
+ existing_config['apps']
36
+ .select { |app| app['name'] == app_name }
37
+ .size
38
+ .positive?
39
+ end
40
+
41
+ def register_app
42
+ existing_config['apps'] << new_app
43
+ config_file.write(existing_config, config_path)
44
+ end
45
+
46
+ def new_app
47
+ {
48
+ 'name' => options[:a],
49
+ 'path' => options[:p],
50
+ 'service' => options[:s],
51
+ 'compose_file' => options[:d],
52
+ 'integration_compose_file' => options[:i]
53
+ }.compact
54
+ end
55
+
56
+ def existing_config
57
+ @existing_config ||= config_file.read(config_path)
58
+ end
59
+
60
+ def app_name
61
+ options[:a]
62
+ end
63
+
64
+ def config_file
65
+ ConfigFile
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,5 @@
1
+ module Nib
2
+ module Integrate
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ require 'nib/integrate/config_file'
2
+ require 'nib/integrate/integration_file'
3
+ require 'nib/integrate/integrator'
4
+ require 'nib/integrate/network_creator'
5
+ require 'nib/integrate/lister'
6
+ require 'nib/integrate/registrar'
7
+ require 'nib/integrate/version'
8
+
9
+ module Nib
10
+ # integrates multiple docker containers
11
+ module Integrate
12
+ def self.applies?
13
+ true
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nib-integrate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Scott Helm
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nib
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ description: integreates specified docker-compose applications
84
+ email:
85
+ - scott.helm@technekes.com
86
+ executables:
87
+ - nib-integrate
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - bin/nib-integrate
92
+ - lib/nib/integrate/config_file.rb
93
+ - lib/nib/integrate/integration_file.rb
94
+ - lib/nib/integrate/integrator.rb
95
+ - lib/nib/integrate/lister.rb
96
+ - lib/nib/integrate/network_creator.rb
97
+ - lib/nib/integrate/registrar.rb
98
+ - lib/nib/integrate/version.rb
99
+ - lib/nib_integrate_plugin.rb
100
+ homepage: https://github.com/technekes/nib-integrate
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.7.4
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: integrates specified docker-compose applications
124
+ test_files: []