docker_brick 0.0.1

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.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.project +17 -0
  4. data/Gemfile +11 -0
  5. data/LICENSE.txt +24 -0
  6. data/README.md +29 -0
  7. data/Rakefile +2 -0
  8. data/bin/brick +11 -0
  9. data/docker_brick.gemspec +29 -0
  10. data/lib/brick/application.rb +35 -0
  11. data/lib/brick/cli/build.rb +28 -0
  12. data/lib/brick/cli/core/subcommand_loader.rb +39 -0
  13. data/lib/brick/cli/help.rb +16 -0
  14. data/lib/brick/cli/project_new.rb +29 -0
  15. data/lib/brick/cli/run.rb +113 -0
  16. data/lib/brick/cli/service_new.rb +44 -0
  17. data/lib/brick/cli/up.rb +46 -0
  18. data/lib/brick/cli.rb +230 -0
  19. data/lib/brick/cli__validator.rb +28 -0
  20. data/lib/brick/config.rb +9 -0
  21. data/lib/brick/docker/docker_client.rb +160 -0
  22. data/lib/brick/generators/new_project_generator/templates/fig.yml +0 -0
  23. data/lib/brick/generators/new_project_generator.rb +51 -0
  24. data/lib/brick/generators/new_service_generator.rb +24 -0
  25. data/lib/brick/generators.rb +10 -0
  26. data/lib/brick/mixin/aliasing.rb +24 -0
  27. data/lib/brick/mixin/colors.rb +24 -0
  28. data/lib/brick/mixin/convert_to_class_name.rb +52 -0
  29. data/lib/brick/mixin/docker_support.rb +253 -0
  30. data/lib/brick/mixin/yaml_helper.rb +18 -0
  31. data/lib/brick/mixin.rb +8 -0
  32. data/lib/brick/models/project.rb +139 -0
  33. data/lib/brick/models/service.rb +276 -0
  34. data/lib/brick/models.rb +13 -0
  35. data/lib/brick/monkey_patches/cli.rb +24 -0
  36. data/lib/brick/monkey_patches/connection.rb +15 -0
  37. data/lib/brick/monkey_patches/docker_container.rb +28 -0
  38. data/lib/brick/monkey_patches/hash.rb +16 -0
  39. data/lib/brick/monkey_patches.rb +5 -0
  40. data/lib/brick/version.rb +4 -0
  41. data/lib/brick.rb +34 -0
  42. data/spec/brick_update.sh +7 -0
  43. data/spec/integration/brick/models/project_spec.rb +70 -0
  44. data/spec/projects/nc_test/fig.yml +14 -0
  45. data/spec/projects/nc_test/ncserver/Dockerfile +7 -0
  46. data/spec/projects/rails/myapp/Dockerfile +7 -0
  47. data/spec/projects/rails/myapp/Gemfile +2 -0
  48. data/spec/projects/rails/myapp/fig.yml +13 -0
  49. data/spec/shell/brick_update.sh +7 -0
  50. data/spec/spec_helper.rb +19 -0
  51. data/spec/unit/brick/mixin/docker_support.yml +33 -0
  52. data/spec/unit/brick/mixin/docker_support_spec.rb +66 -0
  53. data/spec/unit/brick/models/dockerfile/Dockerfile +7 -0
  54. data/spec/unit/brick/models/fig_build.yml +3 -0
  55. data/spec/unit/brick/models/fig_completed.yml +41 -0
  56. data/spec/unit/brick/models/fig_dependency.yml +19 -0
  57. data/spec/unit/brick/models/fig_single.yml +6 -0
  58. data/spec/unit/brick/models/fig_volumes.yml +10 -0
  59. data/spec/unit/brick/models/fig_volumes_from.yml +19 -0
  60. data/spec/unit/brick/models/project_spec.rb +70 -0
  61. data/spec/unit/brick/models/service_spec.rb +80 -0
  62. metadata +231 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 871b125062a6423b2cc1cb71a460fd37c57a61d8
4
+ data.tar.gz: a8f96cafd7c996a4511a5109178e1edcb4e51d54
5
+ SHA512:
6
+ metadata.gz: e878625bf45f7adb55cd08ad54a96a133280849dae024fc4ef791181b4be871bea9da50e0d7672fceb7ccbed47cdb6055ee0d3ad8af3f05bf6be5373e51faf83
7
+ data.tar.gz: e9e3b8a58c6b966ab1016457dbf62d12fe2966c8e754fcea27a5ea32b474e8a66c685def0c568825a2a7046cd846608c3b31df60a6aba993bda73329352de514
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.project ADDED
@@ -0,0 +1,17 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>docker_brick</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ <buildCommand>
9
+ <name>org.rubypeople.rdt.core.rubybuilder</name>
10
+ <arguments>
11
+ </arguments>
12
+ </buildCommand>
13
+ </buildSpec>
14
+ <natures>
15
+ <nature>org.rubypeople.rdt.core.rubynature</nature>
16
+ </natures>
17
+ </projectDescription>
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in docker_brick.gemspec
4
+ gemspec
5
+
6
+ gem 'docker-api', '~> 1.17.0'
7
+ gem "mixlib-cli", "~> 1.5"
8
+ #gem 'deepstruct', '~> 0.0.7'
9
+ gem 'mixlib-config', '~> 2.1.0'
10
+ gem 'colorize', '~> 0.7'
11
+ gem 'thor', '~> 0.19'
data/LICENSE.txt ADDED
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2014 cheyang
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.
23
+
24
+ Use docker_api, thanks very much.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # DockerBrick
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'docker_brick'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install brick
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/docker_brick/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/brick ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # ./brick
4
+ #
5
+
6
+ require 'rubygems'
7
+ $:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
8
+ require 'brick'
9
+ require 'brick/cli'
10
+
11
+ Brick::CLI.run(ARGV)
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'brick/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "docker_brick"
8
+ spec.version = Brick::VERSION
9
+ spec.authors = ["cheyang"]
10
+ spec.email = ["cheyang@163.com"]
11
+ spec.summary = %q{ build, deploy app environment with docker, and can populate complicated configuration files }
12
+ spec.description = %q{A tool can build and deploy app environment with docker, and can populate complicated configuration files }
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
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 0"
23
+ spec.add_dependency "mixlib-cli", "~> 1.5"
24
+ spec.add_dependency "docker-api", "~> 1.17"
25
+ spec.add_dependency "mixlib-config", ">= 1.1.2", "~> 1.1"
26
+ spec.add_dependency 'colorize', '~> 0.7'
27
+ spec.add_dependency 'thor', '~> 0.19'
28
+ # spec.add_dependency "deepstruct", "~> 0.0.7"
29
+ end
@@ -0,0 +1,35 @@
1
+ require 'fileutils'
2
+ require 'mixlib/cli'
3
+
4
+ module Brick
5
+ class Application
6
+ include ::Mixlib::CLI
7
+
8
+ option :config_file,
9
+ :short => "-f FILE",
10
+ :long => "--file FILE",
11
+ :default => File.join(FileUtils.pwd, 'fig.yml'),
12
+ :description => "The configuration file to deploy the project"
13
+
14
+ option :project,
15
+ :short => "-p PROJECT",
16
+ :long => "--project PROJECT",
17
+ :default => File.basename(FileUtils.pwd),
18
+ :description => "Specify an alternate project name"
19
+
20
+ option :version,
21
+ :short => "-v",
22
+ :long => "--version",
23
+ :description => "Show brick version",
24
+ :boolean => true,
25
+ :proc => lambda {|v| puts "brick: #{::Brick::VERSION}"},
26
+ :exit => 0
27
+
28
+
29
+ def configure_brick
30
+ parse_options
31
+ Brick::Config.merge!(config)
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,28 @@
1
+ require 'brick/cli'
2
+
3
+ class Brick::CLI::Build < Brick::CLI
4
+ banner 'brick build'
5
+
6
+ option :cache,
7
+ :long => "--[no-]cache",
8
+ :default => true,
9
+ :boolean => true,
10
+ :description => "Do not use cache when building the image."
11
+
12
+ def run
13
+ #::Brick::CLI::logger.info "hello, #{banner}, option[:no_cache]=#{::Brick::Config.configuration}"
14
+ project_name = ::Brick::Config[:project]
15
+
16
+ config_file = ::Brick::Config[:config_file]
17
+
18
+ project_dir = File.dirname(config_file)
19
+
20
+ no_cache = !::Brick::Config[:cache]
21
+
22
+ project = ::Brick::Models::Project.new(project_name,{:config_file => config_file})
23
+
24
+ project.build_services(no_cache, project_dir)
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,39 @@
1
+
2
+
3
+ require 'brick/version'
4
+ class Brick::CLI::SubcommandLoader
5
+
6
+
7
+ attr_reader :env
8
+
9
+ def initialize( env=ENV)
10
+ @env = env
11
+ end
12
+
13
+ # Load all the sub-commands
14
+ def load_commands
15
+ subcommand_files.each { |subcommand| Kernel.load subcommand }
16
+ true
17
+ end
18
+
19
+
20
+
21
+ def subcommand_files
22
+ @subcommand_files ||= (find_subcommands_via_dirglob.values).flatten.uniq
23
+ end
24
+
25
+ def find_subcommands_via_dirglob
26
+ # The "require paths" of the core knife subcommands bundled with chef
27
+ files = Dir[File.expand_path('../../../cli/*.rb', __FILE__)]
28
+ subcommand_files = {}
29
+ files.each do |cli_file|
30
+ rel_path = cli_file[/#{::Brick::BRICK_ROOT}#{Regexp.escape(File::SEPARATOR)}(.*)\.rb/,1]
31
+ subcommand_files[rel_path] = cli_file
32
+ end
33
+ subcommand_files
34
+ end
35
+
36
+
37
+
38
+
39
+ end
@@ -0,0 +1,16 @@
1
+ require 'brick/cli'
2
+ require 'brick/cli__validator'
3
+
4
+ class Brick::CLI
5
+
6
+ class Help < Brick::CLI
7
+ banner 'brick help'
8
+
9
+ def run
10
+ ::Brick::CLI.common_optparser.opt_parser.banner=self.banner
11
+ ::Brick::CLI_Validator::print_help_and_exit(0)
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,29 @@
1
+ require 'brick/cli'
2
+ require 'brick/generators'
3
+
4
+ class Brick::CLI
5
+
6
+ class ProjectNew < Brick::CLI
7
+ banner 'brick project new PROJECT(options)'
8
+
9
+ def run
10
+
11
+ if @cmd_args.size > 0
12
+ project_name = @cmd_args[0]
13
+ else
14
+ puts "You must specify the project name to create"
15
+ puts banner
16
+ exit 1
17
+ end
18
+
19
+
20
+ working_dir = Dir.pwd
21
+
22
+ Brick::Generators::NewProjectGenerator.start([working_dir, project_name])
23
+
24
+ #puts "value=#{values}"
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,113 @@
1
+ require 'brick/cli'
2
+
3
+ class Brick::CLI::Run < Brick::CLI
4
+ banner 'brick run'
5
+
6
+ option :detach_mode,
7
+ :long => "--detach",
8
+ :short => "-d",
9
+ :default => false,
10
+ :boolean => true,
11
+ :description => "Detached mode: run the service in the background"
12
+
13
+ option :entrypoint,
14
+ :long => "--entrypoint CMD",
15
+ :default => nil,
16
+ :description => "Override the entrypoint of the image."
17
+
18
+ option :deps,
19
+ :long => "--[no-]deps",
20
+ :default => true,
21
+ :boolean => true,
22
+ :description => "[not] start linked services."
23
+
24
+ option :rm,
25
+ :long => "--rm",
26
+ :default => false,
27
+ :boolean => true,
28
+ :description => "Remove container after run. Ignored in detached mode."
29
+
30
+ option :rebuild,
31
+ :long => "--rebuild",
32
+ :default => false,
33
+ :boolean => true,
34
+ :description => "rebuild the image template."
35
+
36
+
37
+ option :T,
38
+ :long => "--T",
39
+ :default => false,
40
+ :boolean => true,
41
+ :description => "Disable pseudo-tty allocation. By default `brick run` allocates a TTY."
42
+
43
+ #By default, it's running in background
44
+ def run
45
+ #::Brick::CLI::logger.info "hello, #{banner}, option[:no_cache]=#{::Brick::Config.configuration}"
46
+ project_name = ::Brick::Config[:project]
47
+
48
+ config_file = ::Brick::Config[:config_file]
49
+
50
+ project_dir = File.dirname(config_file)
51
+
52
+ detach_mode = ::Brick::Config[:detach_mode]
53
+
54
+ start_link_mode = ::Brick::Config[:deps]
55
+
56
+ project = ::Brick::Models::Project.new(project_name,{:config_file => config_file})
57
+
58
+ entrypoint = ::Brick::Config[:entrypoint]
59
+
60
+ if @cmd_args.size > 0
61
+ service_name = @cmd_args[0]
62
+ else
63
+ puts "You must specify the servcie name to run"
64
+ exit 1
65
+ end
66
+
67
+ cmd_array = @cmd_args - [@cmd_args[0]]
68
+
69
+
70
+ service = project.get_service service_name
71
+
72
+ if cmd_array.size >0
73
+ service.service_config_hash["command"] = cmd_array
74
+ end
75
+
76
+ unless entrypoint.nil?
77
+ service.service_config_hash["entrypoint"] = entrypoint.split(" ")
78
+ end
79
+
80
+ disable_tty=::Brick::Config[:T]
81
+
82
+ if disable_tty
83
+ service.service_config_hash["tty"] = false
84
+ else
85
+ service.service_config_hash["tty"] = true
86
+ end
87
+
88
+
89
+
90
+ puts "running service #{service_name}"
91
+
92
+ if service.running?
93
+ puts "exec #{cmd_array} on running service #{service_name}"
94
+
95
+ service.exec cmd_array, {:detach => detach_mode}
96
+
97
+ else
98
+ puts "start service #{service_name}"
99
+ project.run_services(service_name,start_link_mode)
100
+
101
+ unless service.running?
102
+ raise "#{service_name} is failed to start"
103
+ end
104
+
105
+ unless detach_mode
106
+ service.attach
107
+ end
108
+ end
109
+
110
+ ::Brick::Models::Service.wait_for_deamon
111
+
112
+ end
113
+ end
@@ -0,0 +1,44 @@
1
+ require 'brick/cli'
2
+ #require 'byebug'
3
+ class Brick::CLI::ServiceNew < Brick::CLI
4
+ banner "brick service new PROJECT(options) --build centos --links db,redis or \
5
+ brick service new PROJECT(options) --image centos --links db,redis"
6
+
7
+ option :links,
8
+ :long => "--links service1,service2",
9
+ :default => nil,
10
+ :description => "Link to containers in another service. It's optional"
11
+
12
+ option :build,
13
+ :long => "--build centos",
14
+ :default => nil,
15
+ :description => "the service is from built with Dockerfile from specified linux platform like redhat,centos,ubuntu. not along with image."
16
+
17
+ option :image,
18
+ :long => "--image redis",
19
+ :default => nil,
20
+ :description => "the service is from image. Not laong with build"
21
+
22
+ def run
23
+
24
+ if ::Brick::Config[:build].nil? and ::Brick::Config[:image].nil?
25
+ show_usage
26
+ puts ("You must specify the way of creating service, either from build or image".colorize(:red))
27
+ exit 1
28
+ end
29
+
30
+ if !::Brick::Config[:build].nil? and !::Brick::Config[:image].nil?
31
+ show_usage
32
+ puts ("You must specify the way of creating service, either from build or image. You can't choose both".colorize(:red))
33
+ exit 1
34
+ end
35
+
36
+ puts "hello #{banner}"
37
+
38
+ working_dir = Dir.pwd
39
+
40
+ value=Brick::Generators::NewServiceGenerator.start([working_dir])
41
+ byebug
42
+ puts "value=#{value}"
43
+ end
44
+ end
@@ -0,0 +1,46 @@
1
+ require 'brick/cli'
2
+
3
+ class Brick::CLI::Up < Brick::CLI
4
+ banner 'brick up'
5
+
6
+ option :detach_mode,
7
+ :long => "--detach",
8
+ :short => "-d",
9
+ :default => false,
10
+ :boolean => true,
11
+ :description => "Detached mode: run the service in the background"
12
+
13
+ option :recreate,
14
+ :long => "--[no-]recreate",
15
+ :default => false,
16
+ :boolean => true,
17
+ :description => "If containers already exist but still [not] recreate them."
18
+
19
+ option :deps,
20
+ :long => "--[no-]deps",
21
+ :default => true,
22
+ :boolean => true,
23
+ :description => "[not] start linked services."
24
+
25
+ #By default, it's running in background
26
+ def run
27
+ #::Brick::CLI::logger.info "hello, #{banner}, option[:no_cache]=#{::Brick::Config.configuration}"
28
+ project_name = ::Brick::Config[:project]
29
+
30
+ config_file = ::Brick::Config[:config_file]
31
+
32
+ project_dir = File.dirname(config_file)
33
+
34
+ detach_mode = ::Brick::Config[:detach_mode]
35
+
36
+ recreate_mode = ::Brick::Config[:recreate]
37
+
38
+ start_link_mode = ::Brick::Config[:deps]
39
+
40
+ project = ::Brick::Models::Project.new(project_name,{:config_file => config_file})
41
+
42
+ project.up(detach_mode, start_link_mode, recreate_mode)
43
+
44
+ end
45
+
46
+ end