barnacle 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0c8dbe0e035531f309a0b26e950d85a2d4862732
4
+ data.tar.gz: 4c25e3cb78e1a8448b7ea4d9bccdc118c2eca1be
5
+ SHA512:
6
+ metadata.gz: cc6589d60772aade2b54dd631473be42693a2f37297b69f6e16485c421e898418f0742e6ac07654e767e1ffc3495c063bc9beb2ef3a74719b54802de031e39e2
7
+ data.tar.gz: 28fe6c9065bf0db1087fcb7bbd49834f4dd895641a4a6cd8cd41068c42c59ec897a724fb2b4692b042ebf9ec07c087a3e31616fc92ac98e77ea132676b55f873
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in barnacle.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 kswope
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,151 @@
1
+ # Barnacle
2
+
3
+
4
+ All I want from Docker is convenience and the ability to run isolate services.
5
+ I don't want to push to registries, tag images, manage containers, etc. I
6
+ created this simple gem to hide these complexities.
7
+
8
+ There are only four ways to run barnacle, and you can probably guess what they do.
9
+
10
+ ```
11
+ barnacle up
12
+ ```
13
+
14
+ ```
15
+ barnacle down
16
+ ```
17
+
18
+ ```
19
+ barnacle status
20
+ ```
21
+
22
+ ```
23
+ barnacle enter
24
+ ```
25
+
26
+ ## Requires
27
+
28
+ docker version >= 1.5.0
29
+
30
+ some reasonable version of ruby
31
+
32
+ ## Install
33
+
34
+ This is a ruby script packaged in a ruby gem.
35
+
36
+ ```
37
+ shell> gem install barnacle
38
+ ```
39
+
40
+ or Gemfile:
41
+
42
+ ```
43
+ gem 'barnacle'
44
+ ```
45
+
46
+ ## Setup
47
+
48
+ Place your Dockerfile and an optional file named config.yml in your services
49
+ root directory. barnacle will look for these files in a directory named
50
+ barnacle. The Dockerfile will probably contain an ADD statement to include
51
+ some or all of the directory tree - but that's up to you. Your Dockerfile will
52
+ definitely need a CMD because there's no way to pass arguments to barnacle - a
53
+ design decision for simplicy, the objective of barnacle.
54
+
55
+
56
+ my_service/
57
+ ├── app
58
+ ├── barnacle
59
+ │   ├── config.yml
60
+ │   └── Dockerfile
61
+ ├── bin
62
+ └── lib
63
+
64
+ ## Execution and Defaults
65
+
66
+ ##### Defaults:
67
+
68
+ * detach = true
69
+ * [name] = current directory name
70
+ * [hostname] = current directory name
71
+
72
+ If you want to override any defaults put them in the barnacle/config.yml file described in the next section.
73
+
74
+ ##### barnacle up (runs)
75
+
76
+ ```
77
+ docker build -f barnacle/Dockerfile -t [name]
78
+ docker rm -f [name]
79
+ docker run --detach=true --name=[name] --hostname=[hostname] [config.yml switches] [name]
80
+ ```
81
+
82
+ ##### barnacle down (runs)
83
+
84
+ ```
85
+ docker stop NAME
86
+ ```
87
+
88
+
89
+ ##### barnacle status (just highlights line with [name] in the name column)
90
+
91
+ ```
92
+ docker ps -a
93
+ ```
94
+
95
+
96
+ ##### docker enter (executes)
97
+
98
+ ```
99
+ docker exec -ti NAME bash
100
+
101
+ ```
102
+
103
+ ## config.yml
104
+
105
+ This file is made up only of command line switches passed to docker commands.
106
+
107
+ Example config.yml:
108
+
109
+ docker:
110
+ privileged: true
111
+ v:
112
+ - /sys/fs/cgroup:/sys/fs/cgroup:ro
113
+ - /var/log/relay:/var/log
114
+
115
+ The above config.yml results in these __additional__ command line switches
116
+ for $>docker up
117
+
118
+ --priviledged=true -v /sys/fs/cgroup:/sys/fs/cgroup:ro -v /var/log/relay:/var/log
119
+
120
+
121
+ ## Workflow
122
+
123
+ Just a suggestion for production deployment.
124
+
125
+ 1. Get changes up to production server (git push/pull, rsync, floppy, whatever)
126
+
127
+ 2. Log into remote host... cd to service's directory
128
+
129
+ 4. barnacle up
130
+
131
+ If you are more sophisticated you might combine all these steps in a single
132
+ command - like a rake task utilizing ssh -c for the remote commands, or even
133
+ something like Ansible.
134
+
135
+ No image repository is needed except for the base image on the first run. If
136
+ you keep your images up on the production server, and are smart about your
137
+ Dockerfile, this should be a very quick deployment.
138
+
139
+ Note: if you are deploying rails don't forget to put 'bundle install', compile
140
+ assets, and tell the web server of the changes in your Dockerfile. (research
141
+ into caching as much 'bundle install' as possible)
142
+
143
+
144
+
145
+ ## Contributing
146
+
147
+ 1. Fork it ( https://github.com/[my-github-username]/barnacle/fork )
148
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
149
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
150
+ 4. Push to the branch (`git push origin my-new-feature`)
151
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/barnacle.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'barnacle/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "barnacle"
8
+ spec.version = Barnacle::VERSION
9
+ spec.authors = ["kswope"]
10
+ spec.email = ["github-kevdev@snkmail.com"]
11
+
12
+ if spec.respond_to?(:metadata)
13
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
14
+ end
15
+
16
+ spec.summary = %q{Manage your Dockerized service with four simple commands}
17
+ spec.description = %q{Manage your Dockerized service with four simple commands}
18
+ spec.homepage = "https://github.com/kswope/barnacle"
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.8"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_runtime_dependency 'rainbow'
29
+
30
+
31
+ end
data/exe/barnacle ADDED
@@ -0,0 +1,214 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rainbow'
4
+ require 'yaml'
5
+
6
+ yml_filename = 'barnacle/config.yml'
7
+
8
+
9
+ #-------------------------------------------------------------------------------
10
+ # requires >1.5 (because of "build -f") (delete this -f is in distros)
11
+ #-------------------------------------------------------------------------------
12
+ version = `docker -v`[/[\d\.]+/]
13
+ raise "Requires docker version >= 1.5.0" unless version >= "1.5.0"
14
+
15
+
16
+ #-------------------------------------------------------------------------------
17
+ # colorized puts
18
+ #-------------------------------------------------------------------------------
19
+ def cputs(str)
20
+ puts Rainbow(str).yellow
21
+ end
22
+
23
+
24
+ #-------------------------------------------------------------------------------
25
+ # Default options
26
+ #-------------------------------------------------------------------------------
27
+ options = { # default options
28
+ 'name' => File.basename(Dir.pwd),
29
+ 'hostname' => File.basename(Dir.pwd),
30
+ 'detach' => true
31
+ }
32
+
33
+
34
+ #-------------------------------------------------------------------------------
35
+ # Process command line arg
36
+ #-------------------------------------------------------------------------------
37
+ switch = ARGV[0]
38
+ unless (ARGV.size == 1) && ["up", "down", "status", "enter"].include?(switch)
39
+ puts "usage: #{File.basename($0)} up|down|status|enter"
40
+ exit(1)
41
+ end
42
+
43
+
44
+ #-------------------------------------------------------------------------------
45
+ # merge in options from optional barnacle/switches.yml
46
+ #-------------------------------------------------------------------------------
47
+ if File.exists?(yml_filename)
48
+ if docker_yml = YAML.load_file(yml_filename)['docker']
49
+ p docker_yml
50
+ options.merge!(docker_yml) if docker_yml
51
+ else
52
+ cputs "Warning: #{yml_filename} found but didn't contain 'docker' node, not using"
53
+ end
54
+ else
55
+ cputs "Warning: no config file found, using only defaults"
56
+ end
57
+
58
+
59
+ #-------------------------------------------------------------------------------
60
+ # method that tried to run commands or blows up with error
61
+ #-------------------------------------------------------------------------------
62
+ def run_command(command)
63
+ cputs "Kernel#system> #{command}"
64
+ unless system(command)
65
+ cputs "*** ERROR: Failure running command"
66
+ cputs "*** #{$?}"
67
+ exit(1)
68
+ end
69
+ end
70
+
71
+
72
+ #-------------------------------------------------------------------------------
73
+ # down
74
+ #-------------------------------------------------------------------------------
75
+ if switch == 'down'
76
+ run_command("docker stop #{options['name']}")
77
+ exit(0)
78
+ end
79
+
80
+
81
+ #-------------------------------------------------------------------------------
82
+ # up
83
+ #-------------------------------------------------------------------------------
84
+ if switch == 'up'
85
+
86
+ # Build the docker image
87
+ run_command("docker build -f barnacle/Dockerfile -t #{options['name']} .")
88
+
89
+ # Delete current container, use -f in case its already running. ";true" kludge
90
+ # is because running "docker rm" on a non-existant container causes "docker rm"
91
+ # to return a false status, rather than just quietly fail and there is
92
+ # currently no "fail silently" option like many unix commands.
93
+ run_command("docker rm -f #{options['name']}; true")
94
+
95
+ # Build up a docker run command with arguments supplied by our default options
96
+ # and options overriding those from the yaml config file.
97
+ command = "docker run"
98
+ options.each do |option|
99
+
100
+ switch = option[0]
101
+ args = [option[1]].flatten
102
+
103
+ if switch.length > 1 # --key=value
104
+ args.each do |arg|
105
+ command << " --#{switch}=#{arg}"
106
+ end
107
+ else # -k value
108
+ args.each do |arg|
109
+ command << " -#{switch} #{arg}"
110
+ end
111
+ end
112
+
113
+ end
114
+
115
+ command << " #{options['name']}"
116
+
117
+ # execute our docker run command
118
+ run_command(command)
119
+
120
+ end # if up
121
+
122
+
123
+ #-------------------------------------------------------------------------------
124
+ # enter
125
+ #-------------------------------------------------------------------------------
126
+ if switch == 'enter'
127
+
128
+ cmd = "docker exec -ti #{options['name']} bash"
129
+ cputs "Kernel#exec> #{cmd}"
130
+ exec cmd
131
+
132
+ end
133
+
134
+
135
+ #-------------------------------------------------------------------------------
136
+ # Clean up the docker ps formatting mess - calculate the max size needed for
137
+ # each column with the power of ruby.
138
+ #-------------------------------------------------------------------------------
139
+ def calc_column_max_sizes(lines)
140
+
141
+ max_widths = Hash.new(0)
142
+ lines.each do |line|
143
+
144
+ line.each.with_index do |column, i|
145
+
146
+ # reassign max_widths[i] if new value is greater
147
+ max_widths[i] = (max_widths[i] < column.size) ? column.size : max_widths[i]
148
+
149
+ end
150
+
151
+ end
152
+
153
+ max_widths
154
+
155
+ end
156
+
157
+
158
+ #-------------------------------------------------------------------------------
159
+ # figure out header column positions
160
+ #-------------------------------------------------------------------------------
161
+ def header_indexes(header)
162
+
163
+ ['CONTAINER ID','IMAGE','COMMAND','CREATED','STATUS','PORTS','NAMES'].inject([]) do |a,t|
164
+ a << header.index(t)
165
+ end
166
+
167
+ end
168
+
169
+
170
+ #-------------------------------------------------------------------------------
171
+ # status - basically colorizes docker ps output laboriously; best we can do
172
+ # with the current docker cli (most of the effort is just to clean up the ugly
173
+ # fixed column widths that docker ps puts out, it made barnacle look bad.
174
+ #-------------------------------------------------------------------------------
175
+ if switch == 'status'
176
+
177
+ cputs %{Searching for container named "#{options["name"]}"}
178
+
179
+ lines = `docker ps -a`.split("\n")
180
+
181
+ column_indexes = header_indexes( lines[0] )
182
+
183
+ # convert lines to [line][column] array (scary code.. look away)
184
+ columns = lines.inject([]) do |columns, line|
185
+ line = column_indexes.reverse.each.with_index.inject([]) do |columns, (position, i)|
186
+ columns[i] = line[position..-1].strip # pluck out last column in this row
187
+ line = line[0..position - 1] # truncate part of line we just plucked
188
+ columns
189
+ end
190
+ columns << line.reverse # reverse because we gobbled up the line from the end
191
+ end
192
+
193
+ max_widths = calc_column_max_sizes(columns)
194
+
195
+ columns.each do |line|
196
+
197
+ # build output string with columns
198
+ str = line.each_with_index.inject('') do |accum, (part,i)|
199
+ accum << part.ljust(max_widths[i]+ 2) # pad column data to max width plus a little extra
200
+ accum
201
+ end
202
+
203
+ # what all this was about, coloring line
204
+ if line[6].eql?(options['name'])
205
+ cputs str #<--- cputs
206
+ else
207
+ puts str #<--- puts
208
+ end
209
+
210
+ end
211
+
212
+ end
213
+
214
+
@@ -0,0 +1,3 @@
1
+ module Barnacle
2
+ VERSION = "0.1.1"
3
+ end
data/lib/barnacle.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "barnacle/version"
2
+
3
+ module Barnacle
4
+ # Your code goes here...
5
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: barnacle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - kswope
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-05 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.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
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
+ - !ruby/object:Gem::Dependency
42
+ name: rainbow
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Manage your Dockerized service with four simple commands
56
+ email:
57
+ - github-kevdev@snkmail.com
58
+ executables:
59
+ - barnacle
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - barnacle.gemspec
69
+ - exe/barnacle
70
+ - lib/barnacle.rb
71
+ - lib/barnacle/version.rb
72
+ homepage: https://github.com/kswope/barnacle
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.0.14
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Manage your Dockerized service with four simple commands
96
+ test_files: []