dockercompose-generator 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b9fac07a73f25351f7d58ffc5bd6c141d9209e0c
4
+ data.tar.gz: a568e7e7ffb7751bd0f0ebdfd6c658e1dda9238f
5
+ SHA512:
6
+ metadata.gz: fd20d5ce9e12194061ee61dabfa0c3eb5522e8bb8fc96328d74682a9d92a1639e75a224567224e65d7f20dfa903159bb7f3a07a13936e618d106f97f507c75f6
7
+ data.tar.gz: d7600b74b259797e4158383b65850e8101044f04bc5d392599c4345d65a9ed479c2493259dd1b725539b32467e2cdf71dd86abdb759194c1c85caa44acb58fdb
@@ -0,0 +1,42 @@
1
+ # This is a sample .codeclimate.yml configured for Engine analysis on Code
2
+ # Climate Platform. For an overview of the Code Climate Platform, see here:
3
+ # http://docs.codeclimate.com/article/300-the-codeclimate-platform
4
+
5
+ # Under the engines key, you can configure which engines will analyze your repo.
6
+ # Each key is an engine name. For each value, you need to specify enabled: true
7
+ # to enable the engine as well as any other engines-specific configuration.
8
+
9
+ # For more details, see here:
10
+ # http://docs.codeclimate.com/article/289-configuring-your-repository-via-codeclimate-yml#platform
11
+
12
+ # For a list of all available engines, see here:
13
+ # http://docs.codeclimate.com/article/296-engines-available-engines
14
+
15
+ engines:
16
+ # to turn on an engine, add it here and set enabled to `true`
17
+ # to turn off an engine, set enabled to `false` or remove it
18
+ rubocop:
19
+ enabled: true
20
+ bundler-audit:
21
+ enabled: true
22
+
23
+ # Engines can analyze files and report issues on them, but you can separately
24
+ # decide which files will receive ratings based on those issues. This is
25
+ # specified by path patterns under the ratings key.
26
+
27
+ # For more details see here:
28
+ # http://docs.codeclimate.com/article/289-configuring-your-repository-via-codeclimate-yml#platform
29
+
30
+ # Note: If the ratings key is not specified, this will result in a 0.0 GPA on your dashboard.
31
+
32
+ ratings:
33
+ paths:
34
+ - lib/**/*
35
+
36
+ # You can globally exclude files from being analyzed by any engine using the
37
+ # exclude_paths key.
38
+
39
+ exclude_paths:
40
+ - bin/*
41
+ - spec/**/*
42
+ #- vendor/**/*
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - "2.1.4"
5
+ - "2.2.2"
6
+ notifications:
7
+ email: false
8
+ addons:
9
+ code_climate:
10
+ repo_token: 668bb7c5d7f42d3e8dcb17598688bd650d21f6b600f54094f7da97d74c0b43ab
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dc-generator.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'coveralls', require: false
8
+ gem 'rspec', '~> 3.0.0'
9
+ gem 'simplecov', require: false
10
+ gem 'codeclimate-test-reporter', require: false
11
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jon Whitcraft
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,64 @@
1
+ # DockerCompose::Generator
2
+
3
+ [![Build Status](https://travis-ci.org/jwhitcraft/dockercompose-generator.svg?branch=master)](https://travis-ci.org/jwhitcraft/dockercompose-generator) [![Coverage Status](https://coveralls.io/repos/jwhitcraft/dockercompose-generator/badge.svg?branch=master&service=github)](https://coveralls.io/github/jwhitcraft/dockercompose-generator?branch=master) [![Code Climate](https://codeclimate.com/github/jwhitcraft/dockercompose-generator/badges/gpa.svg)](https://codeclimate.com/github/jwhitcraft/dockercompose-generator)
4
+
5
+ Generate a Docker-Compose Yaml file
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'docker-compose-generator'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install docker-compose-generator
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ generator = DockerCompose::Generator
27
+ mysql = generator.create_service('db', 'mysql:5.6')
28
+ mysql.add_environment('mysql_root_password', 'root')
29
+ mysql.add_environment('mysql_database', 'test')
30
+ mysql.add_environment('mysql_user', 'test')
31
+ mysql.add_environment('mysql_password', 'test')
32
+ elastic = generator.create_service('elastic', 'elasticsearch:1.4')
33
+ web = generator.create_service('web', 'php:5.6-apache')
34
+ web.add_link(elastic)
35
+ web.add_link(mysql, 'mysql')
36
+ puts generator.to_yaml
37
+ ```
38
+
39
+ will output
40
+
41
+ ```yaml
42
+ db:
43
+ image: mysql:5.6
44
+ environment:
45
+ MYSQL_ROOT_PASSWORD: root
46
+ MYSQL_DATABASE: test
47
+ MYSQL_USER: test
48
+ MYSQL_PASSWORD: test
49
+ elastic:
50
+ image: elasticsearch:1.4
51
+ web:
52
+ image: php:5.6-apache
53
+ links:
54
+ - elastic
55
+ - db:mysql
56
+ ```
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it ( https://github.com/jwhitcraft/docker-compose-generator/fork )
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task(default: [:spec])
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "docker-compose/generator"
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
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'docker-compose/generator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'dockercompose-generator'
8
+ spec.version = DockerCompose::Generator::VERSION
9
+ spec.authors = ['Jon Whitcraft']
10
+ spec.email = ['jwhitcraft@h2ik.co']
11
+ spec.summary = 'Programmatically create a yaml file for docker-compose'
12
+ spec.description = ''
13
+ spec.homepage = 'https://github.com/jwhitcraft/dockercompose-generator'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ end
@@ -0,0 +1,36 @@
1
+ require 'yaml'
2
+ require 'docker-compose/generator/version'
3
+ require 'docker-compose/generator/service'
4
+
5
+ module DockerCompose
6
+ # Static Generator Class
7
+ # Used to create and keep track of services
8
+ module Generator
9
+ # Services Cache
10
+ #
11
+ # @param [Hash]
12
+ @services = {}
13
+
14
+ def self.create_service(name, image)
15
+ @services[name] = Service.new(name, image)
16
+ end
17
+
18
+ def self.service?(name)
19
+ (@services[name])
20
+ end
21
+
22
+ def self.get_service(name)
23
+ @services[name] if self.service?(name)
24
+ end
25
+
26
+ def self.to_yaml
27
+ yaml = {}
28
+
29
+ @services.each do |key, obj|
30
+ yaml[key] = obj.attrs
31
+ end
32
+
33
+ yaml.to_yaml
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,92 @@
1
+ module DockerCompose
2
+ module Generator
3
+ # Service Class
4
+ #
5
+ # Used to interact with services
6
+ class Service
7
+ attr_reader :name, :attrs
8
+
9
+ def initialize(name, image)
10
+ @name = name
11
+ @attrs = {
12
+ 'image' => image
13
+ }
14
+ end
15
+
16
+ def add_environment(name, value)
17
+ add_to_object('environment', name.upcase, value)
18
+ end
19
+
20
+ def drop_environment(name)
21
+ drop_from_object('environment', name.upcase)
22
+ end
23
+
24
+ def environment?(name)
25
+ (@attrs['environment'] && @attrs['environment'][name.upcase])
26
+ end
27
+
28
+ def add_link(service, link_name = nil)
29
+ name = service.name
30
+ name = "#{name}:#{link_name}" unless link_name.nil?
31
+
32
+ add_to_array('links', name)
33
+ end
34
+
35
+ def drop_link(service, link_name = nil)
36
+ name = service.name
37
+ name = "#{name}:#{link_name}" unless link_name.nil?
38
+
39
+ drop_from_array('links', name)
40
+ end
41
+
42
+ def link?(service, link_name = nil)
43
+ name = service.name
44
+ name = "#{name}:#{link_name}" unless link_name.nil?
45
+
46
+ links = @attrs['links'] || []
47
+ links.include?(name)
48
+ end
49
+
50
+ def add_port(source, target = nil, type = 'tcp')
51
+ target ||= source
52
+
53
+ add_to_array('ports', "#{source}:#{target}/#{type}")
54
+ end
55
+
56
+ def drop_port(source, target, type = 'tcp')
57
+ target ||= source
58
+
59
+ drop_from_array('ports', "#{source}:#{target}/#{type}")
60
+ end
61
+
62
+ def port?(source, target = nil, type = 'tcp')
63
+ target ||= source
64
+
65
+ ports = @attrs['ports'] || []
66
+ ports.include?("#{source}:#{target}/#{type}")
67
+ end
68
+
69
+ private
70
+
71
+ def add_to_array(array_name, value)
72
+ @attrs[array_name] ||= []
73
+ @attrs[array_name].push(value)
74
+ end
75
+
76
+ def drop_from_array(array_name, value)
77
+ @attrs[array_name] ||= []
78
+ @attrs[array_name].delete(value)
79
+ end
80
+
81
+ def add_to_object(obj_key, key, value)
82
+ @attrs[obj_key] ||= {}
83
+ @attrs[obj_key][key] = value
84
+ end
85
+
86
+ def drop_from_object(obj_key, key)
87
+ @attrs[obj_key] ||= {}
88
+ @attrs[obj_key].delete(key)
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,20 @@
1
+ module DockerCompose
2
+ # Injects the VERSION into the Generator Module
3
+ module Generator
4
+ # Current major release.
5
+ # @return [Integer]
6
+ MAJOR = 0
7
+
8
+ # Current minor release.
9
+ # @return [Integer]
10
+ MINOR = 1
11
+
12
+ # Current patch level.
13
+ # @return [Integer]
14
+ PATCH = 0
15
+
16
+ # Full release version.
17
+ # @return [String]
18
+ VERSION = [MAJOR, MINOR, PATCH].join('.').freeze
19
+ end
20
+ end
@@ -0,0 +1,79 @@
1
+ require 'helper.rb'
2
+
3
+ describe 'DockerCompose::Generator::Service' do
4
+ let(:service) { DockerCompose::Generator::Service.new('rspec_test', 'test_image') }
5
+ context 'environment' do
6
+ it 'sets environment variable' do
7
+ service.add_environment('test_env', 'value')
8
+ expect(service).to be_environment('test_env')
9
+ end
10
+
11
+ it 'does not have environment variable' do
12
+ expect(service).not_to be_environment('test_env')
13
+ end
14
+
15
+ it 'will drop environment variable' do
16
+ expect(service.add_environment('test_env', 'value')).to eq('value')
17
+ expect(service.drop_environment('test_env')).to eq('value')
18
+ expect(service).not_to be_environment('test_env')
19
+ end
20
+ end
21
+
22
+ context 'ports' do
23
+ it 'sets ports variable with no target' do
24
+ service.add_port('80')
25
+ expect(service).to be_port('80')
26
+ end
27
+
28
+ it 'sets ports variable with target' do
29
+ service.add_port('80', '8080')
30
+ expect(service).to be_port('80', '8080')
31
+ end
32
+
33
+ it 'sets ports variable with target and type' do
34
+ service.add_port('80', '8080', 'http')
35
+ expect(service).to be_port('80', '8080', 'http')
36
+ end
37
+
38
+ it 'does not have ports variable' do
39
+ expect(service).not_to be_port('9090')
40
+ end
41
+
42
+ it 'will drop ports variable' do
43
+ service.add_port('80', '8080')
44
+ service.drop_port('80', '8080')
45
+ expect(service).not_to be_port('80', '8080')
46
+ end
47
+ end
48
+
49
+ context 'links' do
50
+ let(:link_service) { DockerCompose::Generator::Service.new('link_service', 'test_link_image') }
51
+ it 'sets link variable without a name' do
52
+ service.add_link(link_service)
53
+ expect(service).to be_link(link_service)
54
+ end
55
+
56
+ it 'sets link variable with a custom link name' do
57
+ service.add_link(link_service, 'my_custom_name')
58
+ expect(service).to be_link(link_service, 'my_custom_name')
59
+ end
60
+
61
+ it 'does not have a link' do
62
+ expect(service).not_to be_link(link_service)
63
+ end
64
+
65
+ it 'will drop link without custom name' do
66
+ service.add_link(link_service)
67
+ expect(service).to be_link(link_service)
68
+ service.drop_link(link_service)
69
+ expect(service).not_to be_link(link_service)
70
+ end
71
+
72
+ it 'will drop link with a custom name' do
73
+ service.add_link(link_service, 'my_custom_name')
74
+ expect(service).to be_link(link_service, 'my_custom_name')
75
+ service.drop_link(link_service, 'my_custom_name')
76
+ expect(service).not_to be_link(link_service, 'my_custom_name')
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,47 @@
1
+ require 'helper'
2
+
3
+ describe 'DockerCompose::Generator' do
4
+ let(:service_name) { 'test_service' }
5
+ let(:service_image) { 'test_image' }
6
+ let(:service) { DockerCompose::Generator.create_service(service_name, service_image) }
7
+ context '#create_service' do
8
+ it 'will return a Service object' do
9
+ expect(service).to be_a(DockerCompose::Generator::Service)
10
+ end
11
+ it 'will have the correct name set' do
12
+ expect(service.name).to eq(service_name)
13
+ end
14
+
15
+ it 'will have the correct image set' do
16
+ expect(service.attrs['image']).to eq(service_image)
17
+ end
18
+ end
19
+
20
+ context '#to_yaml' do
21
+ it 'will generate correct yaml' do
22
+ expect(DockerCompose::Generator.to_yaml).to eq("---\ntest_service:\n image: test_image\n")
23
+ end
24
+ end
25
+
26
+ context '#service?' do
27
+ it 'will be false when no service exists' do
28
+ expect(DockerCompose::Generator.service?('test')).to be_falsey
29
+ end
30
+
31
+ it 'will be trie when a service exists' do
32
+ expect(DockerCompose::Generator.service?(service_name)).to be_truthy
33
+ end
34
+
35
+ context '#get_service' do
36
+ it 'will be nil when on service exists' do
37
+ expect(DockerCompose::Generator.get_service('test')).to be_nil
38
+ end
39
+
40
+ it 'will be a service when one exists' do
41
+ srv = DockerCompose::Generator.create_service('test_spec', service_image)
42
+ expect(DockerCompose::Generator.get_service('test_spec')).to eq(srv)
43
+ end
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,22 @@
1
+ if RUBY_ENGINE == 'ruby'
2
+ require 'simplecov'
3
+ require 'coveralls'
4
+
5
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
6
+ SimpleCov::Formatter::HTMLFormatter,
7
+ Coveralls::SimpleCov::Formatter
8
+ ]
9
+ SimpleCov.start
10
+
11
+ require 'codeclimate-test-reporter'
12
+ CodeClimate::TestReporter.start
13
+ end
14
+
15
+ require 'yaml'
16
+ require 'docker-compose/generator'
17
+ require 'rspec'
18
+
19
+ RSpec.configure do |config|
20
+ config.raise_errors_for_deprecations!
21
+ end
22
+
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dockercompose-generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jon Whitcraft
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-25 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: ''
42
+ email:
43
+ - jwhitcraft@h2ik.co
44
+ executables:
45
+ - console
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".codeclimate.yml"
50
+ - ".gitignore"
51
+ - ".travis.yml"
52
+ - Gemfile
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - docker-compose-generator.gemspec
58
+ - lib/docker-compose/generator.rb
59
+ - lib/docker-compose/generator/service.rb
60
+ - lib/docker-compose/generator/version.rb
61
+ - spec/docker-compose/generator/service_spec.rb
62
+ - spec/docker-compose/generator_spec.rb
63
+ - spec/helper.rb
64
+ homepage: https://github.com/jwhitcraft/dockercompose-generator
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.4.5
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Programmatically create a yaml file for docker-compose
88
+ test_files:
89
+ - spec/docker-compose/generator/service_spec.rb
90
+ - spec/docker-compose/generator_spec.rb
91
+ - spec/helper.rb