flock-sandbox 0.0.2

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: 654f15e9cee9777f429672a7a04c5145ccb703aa
4
+ data.tar.gz: d7f57b2733e68552a460402f455be7cbf0d8625f
5
+ SHA512:
6
+ metadata.gz: 5d792bc6badf6b8e1d9cebd0918e758855b71cce2e3ca3a26303fbabc1a14951176710cc7f5ae2c13ea97dcff23be9134357aa63b367db15e6f3c79ac6520f78
7
+ data.tar.gz: 19be4f7d381cbab80c88533db8cbc8104a117fdba72329a6b10772c651e5c4fa6608b0e5d05a1ba6035ff442bdf5aefbe53fd9a0c304170a8251414ae63cf81c
data/.gitignore ADDED
@@ -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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in flock-sandbox.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Andrew Rudenko
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.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Flock::Sandbox
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'flock-sandbox'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install flock-sandbox
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/flock-sandbox/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/flock-sandbox ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "flock/sandbox"
4
+
5
+ runner = Flock::Sandbox::Runner.new(ARGV)
6
+ Flock::Sandbox.set_runner runner
7
+ runner.run
data/example.rb ADDED
@@ -0,0 +1,28 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+ Flock::Sandbox.define do |c|
4
+ c.config = { consul: "http://consul.dev.flocktory.com:8500",
5
+ insecure_registries: ["docker2.dev.flocktory.com:5000"],
6
+ private_network: "192.168.50.10"}
7
+ c.container "widget_db",
8
+ image: "postgres:9.4.1",
9
+ envs: ["POSTGRES_USER=flocktory", "POSTGERS_PASSWORD=flocktory"],
10
+ ports: ["5432:5432"]
11
+ c.container "widget_redis",
12
+ image: "redis:3.0.1",
13
+ ports: ["6379:6379"]
14
+
15
+ c.script "app init db",
16
+ image: "docker2.dev.flocktory.com:5000/app:#{consul_key('container_versions/app_production')}",
17
+ cmd: ["/scripts/initdb"],
18
+ links: ["widget_db:db", "widget_redis:redis"],
19
+ envs: ["REDIS=#{host('redis')}",
20
+ "SIDEKIQ_NAMESPACE=flocktory",
21
+ "DATABASE_URL=postgres://flocktory:flocktory@#{host('db')}/flocktory"]
22
+ if ENV['TEST_VERSION']
23
+ c.script "tests",
24
+ image: "docker2.dev.flocktory.com:5000/widget:#{ENV['TEST_VERSION']}",
25
+ cmd: ["/scripts/tests"]
26
+
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'flock/sandbox/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "flock-sandbox"
8
+ spec.version = Flock::Sandbox::VERSION
9
+ spec.authors = ["Andrew Rudenko"]
10
+ spec.email = ["ceo@prepor.ru"]
11
+ spec.summary = %q{Write a short summary. Required.}
12
+ spec.description = %q{Write a longer description. Optional.}
13
+ spec.homepage = ""
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
+ spec.add_dependency "docker-api", "~> 1.21"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
@@ -0,0 +1,5 @@
1
+ module Flock
2
+ module Sandbox
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,117 @@
1
+ require "flock/sandbox/version"
2
+ require 'open-uri'
3
+ require 'erb'
4
+ require 'docker'
5
+
6
+ module Flock
7
+ module Sandbox
8
+ def self.define(&blk)
9
+ @@current_runner.define(&blk)
10
+ end
11
+
12
+ def self.set_runner(runner)
13
+ @@current_runner = runner
14
+ end
15
+ class Runner
16
+ attr_accessor :config_path, :target, :definition
17
+ def initialize(args)
18
+ self.config_path, self.target = args
19
+ end
20
+
21
+ def run
22
+ instance_eval(File.open(config_path).read, config_path)
23
+ case target
24
+ when "docker"
25
+ Docker.new(definition).run
26
+ when "vagrant"
27
+ Vagrant.new(definition).run
28
+ end
29
+ end
30
+
31
+ def define(&blk)
32
+ self.definition = Definition.new
33
+ context = Context.new(self, definition)
34
+ context.instance_exec(definition, &blk)
35
+ end
36
+ end
37
+ class Definition
38
+ attr_accessor :config, :containers, :scripts
39
+ def container(name, params = {})
40
+ @containers ||= []
41
+ @containers << params.merge(name: name)
42
+ end
43
+ def script(name, params = {})
44
+ @scripts ||= []
45
+ @scripts << params.merge(name: name)
46
+ end
47
+ end
48
+ class Context
49
+ attr_accessor :runner, :definition
50
+ def initialize(runner, definition)
51
+ self.runner = runner
52
+ self.definition = definition
53
+ end
54
+ def host(container)
55
+ case runner.target
56
+ when "docker"
57
+ container
58
+ when "vagrant"
59
+ definition.config[:private_network]
60
+ end
61
+ end
62
+ def consul_key(key)
63
+ open(definition.config[:consul] + "/v1/kv/" + key + "?raw").read
64
+ end
65
+ end
66
+
67
+ class Docker
68
+ def initialize(definition)
69
+ @definition = definition
70
+ ::Docker.options = {
71
+ }
72
+ end
73
+
74
+ def run
75
+ containers = []
76
+ begin
77
+ @definition.containers.each do |c|
78
+ puts "Start container #{c[:name]} (#{c[:image]})"
79
+ ::Docker::Image.create('fromImage' => c[:image])
80
+ container = ::Docker::Container.create('Image' => c[:image],
81
+ 'name' => c[:name],
82
+ 'Env' => c[:envs],
83
+ 'HostConfig' => {'Links' => c[:links]})
84
+ containers << container
85
+ container.start
86
+ end
87
+ @definition.scripts.each do |c|
88
+ puts "Run script #{c[:name]} (#{c[:image]} #{c[:cmd].inspect})"
89
+ ::Docker::Image.create('fromImage' => c[:image])
90
+ container = ::Docker::Container.create('Cmd' => c[:cmd],
91
+ 'Image' => c[:image],
92
+ 'Env' => c[:envs],
93
+ 'HostConfig' => {'Links' => c[:links]})
94
+ containers << container
95
+ container.start
96
+ end
97
+ ensure
98
+ containers.each do |c|
99
+ c.kill
100
+ c.delete
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ class Vagrant
107
+ def initialize(definition)
108
+ @definition = definition
109
+ end
110
+
111
+ def run
112
+ template = ERB.new(File.read(File.expand_path('../../../resources/vagrant.erb', __FILE__)))
113
+ puts template.result(@definition.instance_eval { binding })
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,25 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+ Vagrant.configure(2) do |config|
4
+ config.vm.define "env" do |app|
5
+ app.vm.network "private_network", ip: "<%= config[:private_network] %>"
6
+ app.vm.box = "ubuntu/trusty64"
7
+ app.vm.provision "docker"
8
+ <% config[:insecure_registries].each do |r| %>
9
+ app.vm.provision "docker registry", type: "shell",
10
+ inline: <<-EOS
11
+ echo 'DOCKER_OPTS="--insecure-registry <%= r %> ${DOCKER_OPTS}"' \
12
+ >> /etc/default/docker
13
+ service docker restart
14
+ EOS
15
+ <% end %>
16
+ app.vm.provision "docker" do |d|
17
+ <% containers.each do |c| %>
18
+ d.run "<%= c[:image] %>", auto_assign_name: false, args: "--name <%= c[:name] %> <% (c[:envs] || []).each do |e| %> -e <%= e %> <% end %> <% (c[:ports] || []).each do |p| %> -p <%= p %> <% end %>"
19
+ <% end %>
20
+ end
21
+ <% scripts.each do |s| %>
22
+ app.vm.provision "<%= s[:name] %>", type: "shell", inline: "docker run <% s[:envs].each do |e| %> -e <%= e %> <% end %> <%= s[:image] %> <%= s[:cmd] * ' ' %>"
23
+ <% end %>
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flock-sandbox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Rudenko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: docker-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.21'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.21'
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.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Write a longer description. Optional.
56
+ email:
57
+ - ceo@prepor.ru
58
+ executables:
59
+ - flock-sandbox
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/flock-sandbox
69
+ - example.rb
70
+ - flock-sandbox.gemspec
71
+ - lib/flock/sandbox.rb
72
+ - lib/flock/sandbox/version.rb
73
+ - resources/vagrant.erb
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.3
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Write a short summary. Required.
98
+ test_files: []
99
+ has_rdoc: