capistrano-container 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1b651e108dd83aaa200aad9f5576445e75cfeccb
4
+ data.tar.gz: 2a036295642ac2ea8a09fbe82549d394fa4c28e3
5
+ SHA512:
6
+ metadata.gz: e4be7aa7fc3703d844cc898ad5775d9a436dfcf061010f81de94519ac41bffdf22946413fe5a973e4f8a0e7e6f15e042fd39dae2cb0c788887728e23aa27bac2
7
+ data.tar.gz: 98ce9c8497aa0bfe6613da4c768cb9c8a605365c345314d52457389380e9d6fe4d8dbff195c2629e1e3c095d8d1c587750aaa1ab94f18e1b8fcf5b75b2a7cd31
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in capistrano-bundler.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 creative-workflow
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,158 @@
1
+ # Capistrano::Container [![Gem Version](https://badge.fury.io/rb/capistrano-container.svg)](http://badge.fury.io/rb/capistrano-container)
2
+
3
+ Helps managing [docker]() container and files inside docker container for Capistrano 3.x.
4
+
5
+ This project is in an early stage but helps me alot dealing with my container deployments and keeps my code clean. It is not only ment for docker, but at the moment there only supports docker, feel free to ditribute =)
6
+
7
+ This gem does not handle Dockerfiles or such things, for that there are enough capistrano modules available.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'capistrano', '>= 3.0.0'
15
+ gem 'capistrano-container'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install capistrano-container
25
+
26
+ Dont forget to require the module in your Capfile:
27
+
28
+ ```ruby
29
+ require 'capistrano/container'
30
+ ```
31
+
32
+ ## Usage
33
+ ### definition
34
+ To define and register a container do the following in you stage config or deploy.rb:
35
+
36
+ ```ruby
37
+ ...
38
+
39
+ server('www.example.com', user: 'root', roles: %w{web})
40
+
41
+ container 'db', roles: %w{db},
42
+ container_id: 'website_company_beta_db',
43
+ server: ['www.example.com']
44
+
45
+ container 'php', roles: %w{php},
46
+ container_id: 'website_company_beta_php',
47
+ server: ['www.example.com']
48
+
49
+ ...
50
+ ```
51
+
52
+ This registers two container (db, php) for the server www.example.com. The roles can later be used to filter container like the way you filter server in capistrano.
53
+
54
+ The container id is optional. If not set the ontainer id is equal to the name you give the container as first argument. The container id will later be used to run docker commands.
55
+
56
+ If you define a container, the given hosts get the role `:container_host` behind the scenes, so you can filter hosts that running container. Also a container specific role will be added, fo container named php `:container_php`.
57
+
58
+
59
+ ### commandline tasks
60
+ There are general tasks you can run, if needed you will be asked on which container_id the command should run.
61
+
62
+ ```ruby
63
+ cap container:all # show all docker containers
64
+ cap container:delete # delete a docker container
65
+ cap container:diff # show FS diffs of docker container
66
+ cap container:events # show events of docker container
67
+ cap container:inspect # show info of docker container
68
+ cap container:logs # show logs of docker container
69
+ cap container:pause # pause a docker container
70
+ cap container:ports # show shows public facing port of docker container
71
+ cap container:ressources # show resource usage statistics of docker container
72
+ cap container:restart # restart a docker container
73
+ cap container:running # show running docker containers
74
+ cap container:stop # stop a docker container
75
+ cap container:top # show running processes of docker container
76
+ cap container:unpause # unpause a docker container
77
+ cap container:update_docker # update docker
78
+ ```
79
+
80
+ For every registered container also specific tasks will be registered. For a container named php the following tasks will be available. (With `cap -T` you will the container specific tasks only see if you register your container in the deploy.rb)
81
+
82
+ ```ruby
83
+ cap container:php:delete # delete a docker container
84
+ cap container:php:diff # show FS diffs of docker container
85
+ cap container:php:events # show events of docker container
86
+ cap container:php:inspect # show info of docker container
87
+ cap container:php:logs # show logs of docker container
88
+ cap container:php:pause # pause a docker container
89
+ cap container:php:ports # show shows public facing port of docker container
90
+ cap container:php:ressources # show resource usage statistics of docker container
91
+ cap container:php:restart # restart a docker container
92
+ cap container:php:stop # stop a docker container
93
+ cap container:php:top # show running processes of docker container
94
+ cap container:php:unpause # unpause a docker container
95
+ ```
96
+
97
+ ### ruby tasks
98
+ You can access the defined container like this:
99
+
100
+ ```ruby
101
+ after :deploy, :updated do
102
+ on roles :web do
103
+ # do your application restarts
104
+ end
105
+
106
+ on_container_roles :php do |container, host|
107
+ container.invoke 'restart'
108
+ end
109
+ end
110
+ ```
111
+ This filter container with the role `:php` and invokes the task `container:#{container.name}:restart`.
112
+
113
+ Or inside other tasks
114
+ ```ruby
115
+ container = container_by_name :db
116
+ container = container_by_roles :php
117
+ container = container_by_id 'website_company_beta_db'
118
+ ```
119
+
120
+ A container instance has the following methods:
121
+ ```ruby
122
+ # tests if the container has a specific role
123
+ def has_role?(role)
124
+
125
+ # the docker container id if configured, the container name instead
126
+ def container_id
127
+
128
+ # the container specific role, for a container named php `:container_php`
129
+ def container_role
130
+
131
+ # uploads a local file to a temp file on host /tmp, then copies the file into the container with docker cp
132
+ def upload!(src, target)
133
+
134
+ # doenloads a container file to host /tmp and then to a local file
135
+ def download!(src, target)
136
+
137
+ # executes a command on the docker container with docker exec
138
+ def execute(command)
139
+
140
+ # invokes a container specific task
141
+ def invoke(task_name)
142
+ ```
143
+
144
+ ## TODO
145
+ * Implement provider pattern for other container manager.
146
+ * Write tests.
147
+
148
+ ## Changes
149
+ ### Version 0.0.2
150
+ * Initial release
151
+
152
+ ## Contributing
153
+
154
+ 1. Fork it
155
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
156
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
157
+ 4. Push to the branch (`git push origin my-new-feature`)
158
+ 5. Create new Pull Request
@@ -0,0 +1,74 @@
1
+ # Capistrano::pm2 [![Gem Version](https://badge.fury.io/rb/capistrano-pm2.svg)](http://badge.fury.io/rb/capistrano-pm2)
2
+
3
+ nodejs [pm2](https://github.com/Unitech/pm2) support for Capistrano 3.x
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'capistrano', '~> 3.1.0'
11
+ gem 'capistrano-pm2'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install capistrano-pm2
21
+
22
+ ## Usage
23
+
24
+ Require in `Capfile` to use the default task:
25
+
26
+ ```ruby
27
+ require 'capistrano/pm2'
28
+ ```
29
+
30
+ The task will run before `deploy:restart` as part of Capistrano's default deploy,
31
+ or can be run in isolation with `cap production pm2:restart`. You can also invoke it in your `deploy.rb`:
32
+ ```ruby
33
+ namespace :deploy do
34
+ desc 'Restart application'
35
+ task :restart do
36
+ # invoke 'npm:install'
37
+ invoke 'pm2:restart'
38
+ end
39
+
40
+ after :publishing, :restart
41
+ end
42
+ ```
43
+
44
+
45
+ Available Tasks
46
+ ```ruby
47
+ cap pm2:delete # Delete pm2 application
48
+ cap pm2:list # Show pm2 application info
49
+ cap pm2:logs # Watch pm2 logs
50
+ cap pm2:restart # Restart app gracefully
51
+ cap pm2:setup # Install pm2 via npm on the remote host
52
+ cap pm2:start # Start pm2 application
53
+ cap pm2:status # List all pm2 applications
54
+ cap pm2:stop # Stop pm2 application
55
+ cap pm2:save # Save pm2 state so it can be loaded after restart
56
+ ```
57
+
58
+ Configurable options:
59
+ ```ruby
60
+ set :pm2_app_command, 'main.js' # default, runs main.js
61
+ set :pm2_app_name, nil # app name for pm2, default cap :application
62
+ set :pm2_target_path, -> { release_path.join('subdir') } # default not set
63
+ set :pm2_roles, :all # default, cap roles to run on
64
+ set :pm2_env_variables, {} # default, env vars for pm2
65
+ set :pm2_start_params, '' # default, pm2 start params see http://pm2.keymetrics.io/docs/usage/quick-start/#cheat-sheet
66
+ ```
67
+
68
+ ## Contributing
69
+
70
+ 1. Fork it
71
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
72
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
73
+ 4. Push to the branch (`git push origin my-new-feature`)
74
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'capistrano-container'
7
+ spec.version = '0.0.2'
8
+ spec.date = '2016-06-08'
9
+ spec.summary = 'Helps managing docker container and files inside docker container for Capistrano 3.x'
10
+ spec.description = spec.summary
11
+ spec.authors = ['Tom Hanoldt']
12
+ spec.email = ['tom@creative-workflow.berlin']
13
+ spec.files = ['lib/hola.rb']
14
+ spec.homepage = 'https://gihub.com/creative-workflow/capistrano-container'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'capistrano', '>= 3.0.0.pre'
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake', '~> 10.1'
25
+ end
@@ -0,0 +1 @@
1
+ require_relative 'capistrano/container.rb'
@@ -0,0 +1,85 @@
1
+ require_relative 'tasks/container.rb'
2
+
3
+ module Capistrano
4
+ module Container
5
+ require_relative 'container/instance.rb'
6
+ require_relative 'container/manager.rb'
7
+ require_relative 'container/mixins.rb'
8
+ end
9
+ end
10
+
11
+ $container_manager = Manager.new
12
+
13
+ module Capistrano
14
+ module DSL
15
+ def container(name, config = {})
16
+ # self gives access to capistrano dsl inside a container instance
17
+ $container_manager.add(name, config)
18
+ end
19
+
20
+ def on_container_roles(container_roles, &block)
21
+ container_by_roles(container_roles).each do |container|
22
+ on roles(container.container_role) do |host|
23
+ container.dsl = self
24
+ block.call container, host
25
+ end
26
+ end
27
+ end
28
+
29
+ def on_container(container, &block)
30
+ on roles(container.container_role) do |host|
31
+ container.dsl = self
32
+ block.call container, host
33
+ end
34
+ end
35
+
36
+ def container_by_roles(roles)
37
+ $container_manager.by_roles(roles).map do |container|
38
+ container.dsl = self
39
+ container
40
+ end
41
+ end
42
+
43
+ def container_by_name(name)
44
+ tmp = $container_manager.by_name(name)
45
+ tmp.dsl = self
46
+ tmp
47
+ end
48
+
49
+ def container_by_id(id)
50
+ tmp = $container_manager.by_id(id)
51
+ tmp.dsl = self
52
+ tmp
53
+ end
54
+
55
+ def run_container_command(command, container = nil)
56
+ if command.include?('{container_id}')
57
+ if container.nil?
58
+ container_id = ask_for_container_id
59
+ container = container_by_id container_id
60
+ end
61
+
62
+ command.gsub!('{container_id}', container.container_id)
63
+
64
+ on roles(container.container_role) do |host|
65
+ container.dsl = self
66
+
67
+ puts capture("docker #{command}")
68
+ end
69
+
70
+ else
71
+ on roles(:container_host) do
72
+ puts capture("docker #{command}")
73
+ end
74
+ end
75
+ end
76
+
77
+ def ask_for_container_id()
78
+ invoke 'container:all'
79
+
80
+ ask(:container_id, "container id?")
81
+
82
+ fetch(:container_id)
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,67 @@
1
+ require 'securerandom'
2
+
3
+ class Instance
4
+ attr_reader :name
5
+ attr_reader :config
6
+ attr_writer :dsl
7
+
8
+ def initialize(name, config)
9
+ @dsl = nil
10
+ @name = name
11
+ @config = {shared_on_host: '/tmp'}.merge(config)
12
+ end
13
+
14
+ def has_role?(role)
15
+ return false unless @config.key? :roles
16
+
17
+ role = role.to_s if role.is_a? Symbol
18
+
19
+ @config[:roles].include? role
20
+ end
21
+
22
+ def container_id
23
+ return @name unless @config.key? :container_id
24
+
25
+ @config[:container_id]
26
+ end
27
+
28
+ def container_role
29
+ "container_#{@name}".to_sym
30
+ end
31
+
32
+ def upload!(src, target)
33
+ tmp = "#{@config[:shared_on_host]}/capistrano-docker.#{SecureRandom.uuid}.tmp"
34
+
35
+ @dsl.upload!(src, tmp)
36
+
37
+ self.cp(tmp, "#{container_id}:#{target}")
38
+
39
+ @dsl.execute("rm -rf #{tmp}")
40
+ end
41
+
42
+ def download!(src, target)
43
+ tmp = "#{@config[:shared_on_host]}/capistrano-docker.#{SecureRandom.uuid}.tmp"
44
+
45
+ self.cp("#{container_id}:#{src}", tmp)
46
+
47
+ @dsl.download!(tmp, target)
48
+
49
+ @dsl.execute("rm -rf #{tmp}")
50
+ end
51
+
52
+ def execute(command)
53
+ command = "docker exec -i #{container_id} bash -c '#{command.gsub("'", "\'")}'"
54
+
55
+ @dsl.execute(command)
56
+ end
57
+
58
+ def cp(src, target)
59
+ command = "docker cp #{src} #{target}"
60
+
61
+ @dsl.execute(command)
62
+ end
63
+
64
+ def invoke(task_name)
65
+ @dsl.invoke "container:#{@name}:#{task_name}"
66
+ end
67
+ end
@@ -0,0 +1,55 @@
1
+ require 'rake/dsl_definition'
2
+
3
+ class Manager
4
+ include Rake::DSL
5
+
6
+ def initialize()
7
+ @container = {}
8
+ end
9
+
10
+ def add(name, config)
11
+ @container[name.to_sym] = container = Instance.new(name, config)
12
+
13
+ config[:server].map!{ |ip| server(ip) }
14
+
15
+ config[:server].each do |server|
16
+ server.add_roles [:container_host, container.container_role]
17
+ end
18
+
19
+ self.create_container_tasks(container)
20
+
21
+ container
22
+ end
23
+
24
+ def by_name(name)
25
+ @container[name.to_sym]
26
+ end
27
+
28
+ def by_id(id)
29
+ @container.each do |name, instance|
30
+ return instance if instance.container_id == id
31
+ end
32
+ end
33
+
34
+ def by_roles(roles)
35
+ roles = Array(roles)
36
+
37
+ return @container.values if roles.include? :all
38
+
39
+ tmp = {}
40
+ roles.each do |role|
41
+ @container.each do |name, instance|
42
+ tmp[name] = instance if instance.has_role? role
43
+ end
44
+ end
45
+ tmp.values
46
+ end
47
+
48
+ def create_container_tasks(container)
49
+ namespace :container do
50
+ namespace container.name do
51
+ Mixins.define_tasks(container)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,67 @@
1
+ require 'rake/dsl_definition'
2
+
3
+ module Mixins
4
+ extend Rake::DSL
5
+
6
+ def self.define_tasks(container = nil)
7
+ desc "pause a docker container"
8
+ task :pause do
9
+ run_container_command("pause {container_id}", container)
10
+ end
11
+
12
+ desc "unpause a docker container"
13
+ task :unpause do
14
+ run_container_command("unpause {container_id}", container)
15
+ end
16
+
17
+ desc "restart a docker container"
18
+ task :restart do
19
+ run_container_command("restart {container_id}", container)
20
+ end
21
+
22
+ desc "delete a docker container"
23
+ task :delete do
24
+ run_container_command("rm {container_id}", container)
25
+ end
26
+
27
+ desc "stop a docker container"
28
+ task :stop do
29
+ run_container_command("stop {container_id}", container)
30
+ end
31
+
32
+ desc "show info of docker container"
33
+ task :inspect do
34
+ run_container_command("inspect {container_id}", container)
35
+ end
36
+
37
+ desc "show logs of docker container"
38
+ task :logs do
39
+ run_container_command("logs {container_id}", container)
40
+ end
41
+
42
+ desc "show FS diffs of docker container"
43
+ task :diff do
44
+ run_container_command("diff {container_id}", container)
45
+ end
46
+
47
+ desc "show resource usage statistics of docker container"
48
+ task :ressources do
49
+ run_container_command("statt {container_id}", container)
50
+ end
51
+
52
+ desc "show running processes of docker container"
53
+ task :top do
54
+ run_container_command("top {container_id}", container)
55
+ end
56
+
57
+ desc "show events of docker container"
58
+ task :events do
59
+ run_container_command("events {container_id}", container)
60
+ end
61
+
62
+ desc "show shows public facing port of docker container"
63
+ task :ports do
64
+ run_container_command("port {container_id}", container)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,20 @@
1
+ require_relative '../container/mixins.rb'
2
+
3
+ namespace :container do
4
+ desc "update docker"
5
+ task :update_docker do
6
+ run_container_command("wget -qO- https://get.docker.com/ | sh")
7
+ end
8
+
9
+ desc "show all docker containers"
10
+ task :all do
11
+ run_container_command('ps -a')
12
+ end
13
+
14
+ desc "show running docker containers"
15
+ task :running do
16
+ run_container_command('ps')
17
+ end
18
+
19
+ Mixins.define_tasks
20
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-container
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Tom Hanoldt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capistrano
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0.pre
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0.pre
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.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
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.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.1'
55
+ description: Helps managing docker container and files inside docker container for
56
+ Capistrano 3.x
57
+ email:
58
+ - tom@creative-workflow.berlin
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - README0.md
68
+ - Rakefile
69
+ - capistrano-container.gemspec
70
+ - lib/capistrano-container.rb
71
+ - lib/capistrano/container.rb
72
+ - lib/capistrano/container/instance.rb
73
+ - lib/capistrano/container/manager.rb
74
+ - lib/capistrano/container/mixins.rb
75
+ - lib/capistrano/tasks/container.rb
76
+ homepage: https://gihub.com/creative-workflow/capistrano-container
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.6
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Helps managing docker container and files inside docker container for Capistrano
100
+ 3.x
101
+ test_files: []