dockerhelper 0.0.2 → 0.0.3

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
  SHA1:
3
- metadata.gz: 11aee8a2639b071b1046e9ce1350ded31ab29c13
4
- data.tar.gz: fdf0f3155c16c9c6b47fda301187ed1a8e92f093
3
+ metadata.gz: 8bec3a523719acd104ddce4ae0f3b2180eeae91c
4
+ data.tar.gz: c428627da6a159c3f0e6fc6b7129d4ee6264e656
5
5
  SHA512:
6
- metadata.gz: 9a4c542ef90e9cf2071f06b285133231af45d5ca70a4e52c24660d9ab41decfa1a1c81fa2aa55d50181f9c95c8e418599e67d396c6d2d68f5cee3e654e6eda3b
7
- data.tar.gz: bcfd7a3ee7659e0ef54b0a606d64bb208b15e9b1847eed7c567d6cd6bcb1e77fffd19130f2a189281a870faac0432de0693ddf9c33bfb7927951013469d2117f
6
+ metadata.gz: 4660d2a114a52144f8d728e2af23c1d41b6341d5e3ce80cf369fb85cfb3420396a854da0254ca9cb8ff18c8be23fd38c1942e39ff55408b0d5588b1a005d6264
7
+ data.tar.gz: 7c060304e6bb89bca3ef3a7a51ef5b83428ec65cf578a2d96a3296ec6a15efa28db4b1aff11b87089d557f5e4041a8ff00bf050959fab4f4b2e80d8e5c1dab77
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Dockerhelper
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dockerhelper`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Helper classes and rake tasks to build Docker images, push them to DockerHub,
4
+ and deploy updates to Kubernetes. Assumes a rather specific workflow for
5
+ building Docker images and deploying them to Kubernetes.
6
6
 
7
7
  ## Installation
8
8
 
@@ -22,15 +22,39 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
26
-
27
- ## Development
25
+ Example usage in a `Rakefile`:
28
26
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
27
+ ```ruby
28
+ require 'dockerhelper'
29
+ require 'dockerhelper/rake'
30
+
31
+ project_root = File.expand_path('../../', __FILE__)
32
+ config = Dockerhelper.configure do |c|
33
+ c.app_name = 'project'
34
+ c.environment = :production
35
+ c.git_repo_url = 'git@github.com:org/project.git'
36
+ c.git_root = File.join(project_root, 'tmp/repo')
37
+ c.git_branch = ENV['GIT_BRANCH'] || 'master'
38
+ c.dockerfile = 'docker/Dockerfile.production'
39
+ c.docker_image = 'project-image-name'
40
+ c.docker_repo = 'org/project'
41
+ c.docker_repo_tag_prefix = 'prd-'
42
+ c.kube_rc_template = File.expand_path('../project-rc.yml.erb', __FILE__)
43
+ c.kube_rc_dest_dir = File.dirname(__FILE__)
44
+ end
45
+
46
+ Dockerhelper::Tasks.init(config)
47
+ ```
32
48
 
33
- ## Contributing
49
+ The above will define rake tasks, for example:
34
50
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dockerhelper.
51
+ ```
52
+ rake docker:production:build # Build docker image
53
+ rake docker:production:deploy # Git clone, build image, and push image to Docker Hub
54
+ rake docker:production:info # Print config info
55
+ rake docker:production:kube:gen_rc # Generate replication controller for the current build
56
+ rake docker:production:pull # Pull git repo
57
+ rake docker:production:push # Push docker images to Docker Hub
58
+ rake docker:production:repo_tag # Tag docker image
36
59
 
60
+ ```
data/lib/dockerhelper.rb CHANGED
@@ -1,8 +1,8 @@
1
- require "dockerhelper/build"
2
1
  require "dockerhelper/config"
3
2
  require "dockerhelper/command"
4
3
  require "dockerhelper/docker"
5
4
  require "dockerhelper/git"
5
+ require "dockerhelper/kubernetes"
6
6
  require "dockerhelper/version"
7
7
 
8
8
  module Dockerhelper
@@ -1,3 +1,5 @@
1
+ require 'open3'
2
+
1
3
  module Dockerhelper
2
4
  class Command
3
5
  attr_reader :cmd, :label, :chdir
@@ -1,5 +1,6 @@
1
1
  module Dockerhelper
2
2
  class Config
3
+ attr_accessor :app_name
3
4
  attr_accessor :git_root
4
5
  attr_accessor :git_branch
5
6
  attr_accessor :git_repo_url
@@ -9,10 +10,14 @@ module Dockerhelper
9
10
  attr_accessor :rev_length
10
11
  attr_accessor :dockerfile
11
12
  attr_accessor :docker_repo_tag_prefix
13
+ attr_accessor :environment
14
+ attr_accessor :kube_rc_template
15
+ attr_accessor :kube_rc_dest_dir
12
16
 
13
17
  def initialize
14
18
  # defaults
15
19
  @rev_length = 8
20
+ @kube_rc_dest_dir = Dir.pwd
16
21
  end
17
22
 
18
23
  def git
@@ -23,6 +28,10 @@ module Dockerhelper
23
28
  @docker ||= Docker.new(chdir: git_root)
24
29
  end
25
30
 
31
+ def kubernetes
32
+ @kubernetes ||= Kubernetes.new(self)
33
+ end
34
+
26
35
  def docker_repo_tag
27
36
  @docker_tag || "#{docker_repo_tag_prefix}#{git.latest_rev}"
28
37
  end
@@ -0,0 +1,49 @@
1
+ require 'erb'
2
+ require 'yaml'
3
+
4
+ module Dockerhelper
5
+ class Kubernetes
6
+ attr_reader :config
7
+ def initialize(config)
8
+ @config = config
9
+ end
10
+
11
+ def app_name
12
+ config.app_name
13
+ end
14
+
15
+ def app_version
16
+ config.docker_repo_tag
17
+ end
18
+
19
+ def app_full_name
20
+ "#{app_name}-#{app_version}"
21
+ end
22
+
23
+ def current_rc
24
+ out = Command.new("kubectl get rc -l app=#{app_name},environment=#{config.environment}",
25
+ label: 'kubectl-get-rc').capture
26
+
27
+ # assumes rc will be found
28
+ out.lines[1].match(/^\S+/)[0]
29
+ end
30
+
31
+ def rolling_update
32
+ Command.new("kubectl rolling-update #{current_rc} -f #{replication_controller_filename}",
33
+ label: 'kubectl-get-rc').run
34
+ end
35
+
36
+ def replication_controller_yaml
37
+ yaml_in = File.read(config.kube_rc_template)
38
+ ERB.new(yaml_in).result(binding)
39
+ end
40
+
41
+ def write_replication_controller
42
+ File.write(replication_controller_filename, replication_controller_yaml)
43
+ end
44
+
45
+ def replication_controller_filename
46
+ File.join(config.kube_rc_dest_dir, "#{app_full_name}-rc.yml")
47
+ end
48
+ end
49
+ end
@@ -4,9 +4,9 @@ module Dockerhelper
4
4
  module Tasks
5
5
  extend ::Rake::DSL
6
6
 
7
- def self.init(config, environment: :production)
7
+ def self.init(config)
8
8
  namespace :docker do
9
- namespace(environment) do
9
+ namespace(config.environment) do
10
10
 
11
11
  desc 'Print config info'
12
12
  task :info do
@@ -36,6 +36,23 @@ module Dockerhelper
36
36
 
37
37
  desc 'Git clone, build image, and push image to Docker Hub'
38
38
  task :deploy => [:pull, :build, :repo_tag, :push]
39
+
40
+ namespace :kube do
41
+ desc 'Generate replication controller for the current build'
42
+ task :gen_rc do
43
+ config.kubernetes.write_replication_controller
44
+ end
45
+
46
+ desc 'Get current replication controller version'
47
+ task :current_rc do
48
+ puts config.kubernetes.current_rc
49
+ end
50
+
51
+ desc 'Run replication controller rolling-update'
52
+ task :rolling_update do
53
+ config.kubernetes.rolling_update
54
+ end
55
+ end
39
56
  end
40
57
  end
41
58
  end
@@ -1,3 +1,3 @@
1
1
  module Dockerhelper
2
- VERSION = "0.0.2"
2
+ VERSION = '0.0.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dockerhelper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh McDade
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-08-15 00:00:00.000000000 Z
11
+ date: 2015-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,11 +53,11 @@ files:
53
53
  - bin/setup
54
54
  - dockerhelper.gemspec
55
55
  - lib/dockerhelper.rb
56
- - lib/dockerhelper/build.rb
57
56
  - lib/dockerhelper/command.rb
58
57
  - lib/dockerhelper/config.rb
59
58
  - lib/dockerhelper/docker.rb
60
59
  - lib/dockerhelper/git.rb
60
+ - lib/dockerhelper/kubernetes.rb
61
61
  - lib/dockerhelper/rake.rb
62
62
  - lib/dockerhelper/version.rb
63
63
  homepage: https://github.com/joshm1/dockerhelper
@@ -1,15 +0,0 @@
1
- module Dockerhelper
2
- class Build
3
- def initialize(config)
4
- @builder = config
5
- end
6
-
7
- def git
8
- @git ||= Git.new(config.git_repo, rev_length: config.rev_length)
9
- end
10
-
11
- def docker
12
- @docker ||= Docker.new(chdir: config.git_repo)
13
- end
14
- end
15
- end