nib-integrate 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e73989921e020f7c4c6d067db840a0995bd39c5a00498c2c02a45d6093b0094
4
- data.tar.gz: c59a466ca7da71a0f8b295ea74992d0a21a75516461c318359830dbbe7cdfc9a
3
+ metadata.gz: f1386d7ff19edc5f38506ff83e7fcfbaa96ed25bd7b1a5b30e920d6b4f727cda
4
+ data.tar.gz: 306428b15ebf5d930b8a2b40ba7ac53d9d2155a9aa7864bff58fe882766475aa
5
5
  SHA512:
6
- metadata.gz: 3ebc92e7470cae10c6077a4b4cf387c21552bcade4467041daecd8758be5b5515716eb025d80c96abd8bd557806ac7fc72b0903e6b85205a6161fc9ab88608c2
7
- data.tar.gz: 74cb44ad303b7e64d47430739005b5a8d0a0b60289c031a552bebcfcd9aa5edc44060b5b485d0a763723595e80d3d6881497135ab7c93b9a9daa76c0dbacf684
6
+ metadata.gz: 2e267c1ab97254e3f9e906379c8a3ebdcc76b7e3492828a2a9742a6cf6421ca4adb49b725c5a54418fd946c2c12b7aed2bda1642ac571c0a2765b6f6df1539b3
7
+ data.tar.gz: dc2dade4cbbd84d37a60c3d5bfb46a1e9b30716cecf2b66f9b08eaa58fa7e5c0a216b7731739c51743cb65841ad71b559736222354622da5f25aea1fd07b1c1d
@@ -6,7 +6,7 @@ module Nib
6
6
  # reads and writes the config file
7
7
  class ConfigFile
8
8
  PATH = "#{ENV['HOME']}/.nib-integrate-config".freeze
9
- DEFAULT_CONFIG = { 'apps' => [] }.freeze
9
+ DEFAULT_CONFIG = { 'apps' => [], 'initial_port' => 10_000 }.freeze
10
10
  class << self
11
11
  def write(config, path = PATH)
12
12
  # this will write the config file.
@@ -1,93 +1,54 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Nib
2
4
  module Integrate
3
5
  # dynamically generated network config file
4
6
  class IntegrationFile
5
7
  class << self
6
- def write(app_name)
7
- new(app_name).write
8
+ def write(app_name, port = 10_000)
9
+ new(app_name, port).write
8
10
  end
9
- end
10
11
 
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)
12
+ def write_empty_config(app_name, port = 10_000)
13
+ new(app_name, port).write_empty_config
20
14
  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
15
  end
33
16
 
34
- def other_apps
35
- apps.reject { |a| a['name'] == app_name }
36
- end
17
+ attr_reader :app_name, :current_port
37
18
 
38
- def path
39
- "#{ENV['HOME']}/.nib-integrate-network-config-#{app['name']}"
19
+ def initialize(app_name, port = 10_000)
20
+ @current_port = port
21
+ @app_name = app_name
40
22
  end
41
23
 
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
- }
24
+ def write
25
+ config.tap do |cfg|
26
+ File.open(cfg.path, 'w') do |f|
27
+ f.write(cfg.config.to_yaml)
28
+ end
48
29
  end
49
30
  end
50
31
 
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)}"
32
+ def write_empty_config
33
+ empty_config.tap do |cfg|
34
+ File.open(cfg.path, 'w') do |f|
35
+ f.write(cfg.config.to_yaml)
36
+ end
58
37
  end
59
38
  end
60
39
 
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
40
+ private
80
41
 
81
- def app_compose_contents
82
- YAML.load_file("#{app['path']}/#{app['compose_file']}")
42
+ def config
43
+ integration_file_config.config
83
44
  end
84
45
 
85
- def app_services
86
- app_docker_compose['services'].keys
46
+ def empty_config
47
+ integration_file_config.empty_config
87
48
  end
88
49
 
89
- def global_config
90
- ConfigFile.read
50
+ def integration_file_config
51
+ IntegrationFileConfig.new(app_name, current_port)
91
52
  end
92
53
  end
93
54
  end
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nib
4
+ module Integrate
5
+ # IntegrationFileConfig creates a proper configuration yaml string
6
+ # and keeps track of port assignments
7
+ class IntegrationFileConfig
8
+ attr_reader :app_name, :current_port
9
+ def initialize(app_name, current_port)
10
+ @app_name = app_name
11
+ @current_port = current_port
12
+ end
13
+
14
+ def config
15
+ OpenStruct.new(
16
+ config: config_hash,
17
+ path: path,
18
+ port: current_port
19
+ )
20
+ end
21
+
22
+ def empty_config
23
+ OpenStruct.new(
24
+ config: { 'version' => app_compose_contents['version'] },
25
+ path: empty_path
26
+ )
27
+ end
28
+
29
+ private
30
+
31
+ def config_hash
32
+ app_services.each_with_object(merged_config) do |elem, acc|
33
+ service_definition = {
34
+ 'external_links' => external_links,
35
+ 'networks' => %w[default nib]
36
+ }
37
+ service_definition['ports'] = ports(elem) if ports(elem)
38
+
39
+ acc['services'][elem].merge!(service_definition)
40
+ end
41
+ end
42
+
43
+ def apps
44
+ @apps ||= global_config['apps']
45
+ end
46
+
47
+ def app
48
+ apps.find { |a| a['name'] == app_name }
49
+ end
50
+
51
+ def other_apps
52
+ apps.reject { |a| a['name'] == app_name }
53
+ end
54
+
55
+ def path
56
+ "#{app['path']}/.nib-integrate-network-config"
57
+ end
58
+
59
+ def empty_path
60
+ "#{app['path']}/.nib-integrate-empty-config-file"
61
+ end
62
+
63
+ def merged_config
64
+ app_compose_contents.merge(network_config)
65
+ end
66
+
67
+ def ports(service_definition)
68
+ ports = app_docker_compose['services'][service_definition]['ports']
69
+ return unless ports && !ports.empty?
70
+ ports.map do |port|
71
+ existing_ports = port.split(':')
72
+ [next_port, existing_ports[1]].join(':')
73
+ end
74
+ end
75
+
76
+ def next_port
77
+ @current_port += 1
78
+ end
79
+
80
+ def external_links
81
+ other_apps.map(&external_link)
82
+ end
83
+
84
+ def external_link
85
+ lambda do |registration|
86
+ "#{container_name(registration)}_1:#{container_name(registration)}"
87
+ end
88
+ end
89
+
90
+ def container_name(registration)
91
+ project_name = registration['path'].split('/').last.gsub(/[ _]/, '')
92
+ service_name = registration['service']
93
+ "#{project_name}_#{service_name}"
94
+ end
95
+
96
+ def network_config
97
+ {
98
+ 'networks' => {
99
+ 'nib' => { 'external' => { 'name' => 'nib-integrate-network' } }
100
+ }
101
+ }
102
+ end
103
+
104
+ def app_docker_compose
105
+ @app_docker_compose ||= app_compose_contents
106
+ end
107
+
108
+ def app_compose_contents
109
+ YAML.load_file("#{app['path']}/#{app['compose_file']}")
110
+ end
111
+
112
+ def app_services
113
+ app_docker_compose['services'].keys
114
+ end
115
+
116
+ def global_config
117
+ ConfigFile.read
118
+ end
119
+ end
120
+ end
121
+ end
@@ -12,16 +12,18 @@ module Nib
12
12
  end
13
13
  end
14
14
 
15
- attr_reader :args, :current_arg, :config_path
15
+ attr_reader :args, :current_arg, :config_path, :current_port
16
16
 
17
17
  def initialize(args, config_path)
18
18
  @args = args
19
19
  @config_path = config_path
20
+ @current_port = initial_port
20
21
  end
21
22
 
22
23
  def up
23
24
  args.map do |arg|
24
25
  @current_arg = arg
26
+ @current_port = current_integration_object.port + 1
25
27
  command
26
28
  end
27
29
  end
@@ -40,11 +42,20 @@ module Nib
40
42
  end
41
43
 
42
44
  def apps
43
- @apps ||= config_file.read(config_path)['apps']
45
+ @apps ||= config['apps']
46
+ end
47
+
48
+ def initial_port
49
+ @initial_port ||= config['initial_port']
44
50
  end
45
51
 
46
52
  def command
47
- [cd_command, docker_compose_command].join(' && ')
53
+ integration_file.write_empty_config(app['name'])
54
+ [
55
+ cd_command,
56
+ docker_compose_command,
57
+ clean_files_command
58
+ ].join(' && ')
48
59
  end
49
60
 
50
61
  def down_command
@@ -62,8 +73,12 @@ module Nib
62
73
  [run_command, integration_file_flag, up_command].compact.join(' ')
63
74
  end
64
75
 
76
+ def clean_files_command
77
+ 'rm .nib-integrate*'
78
+ end
79
+
65
80
  def run_command
66
- "docker-compose -f #{app['compose_file']}"
81
+ 'docker-compose -f .nib-integrate-empty-config-file'
67
82
  end
68
83
 
69
84
  def up_command
@@ -71,11 +86,15 @@ module Nib
71
86
  end
72
87
 
73
88
  def integration_file_flag
74
- "-f #{integration_file_path}"
89
+ "-f #{current_integration_object.path}"
90
+ end
91
+
92
+ def current_integration_object
93
+ integration_file.write(app['name'], current_port)
75
94
  end
76
95
 
77
- def integration_file_path
78
- integration_file.write(app['name'])
96
+ def config
97
+ @config ||= config_file.read(config_path)
79
98
  end
80
99
 
81
100
  def config_file
@@ -1,5 +1,5 @@
1
1
  module Nib
2
2
  module Integrate
3
- VERSION = '0.1.0'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
5
5
  end
@@ -1,5 +1,7 @@
1
+ require 'ostruct'
1
2
  require 'nib/integrate/config_file'
2
3
  require 'nib/integrate/integration_file'
4
+ require 'nib/integrate/integration_file_config'
3
5
  require 'nib/integrate/integrator'
4
6
  require 'nib/integrate/network_creator'
5
7
  require 'nib/integrate/lister'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nib-integrate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Helm
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-07 00:00:00.000000000 Z
11
+ date: 2018-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nib
@@ -91,6 +91,7 @@ files:
91
91
  - bin/nib-integrate
92
92
  - lib/nib/integrate/config_file.rb
93
93
  - lib/nib/integrate/integration_file.rb
94
+ - lib/nib/integrate/integration_file_config.rb
94
95
  - lib/nib/integrate/integrator.rb
95
96
  - lib/nib/integrate/lister.rb
96
97
  - lib/nib/integrate/network_creator.rb