vagrant-docker-nsenter 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
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
+ *.swp
19
+ *.swo
20
+ example/files/*
21
+ example/vagrant/.vagrant
@@ -0,0 +1,17 @@
1
+ ## 0.0.2 (June 28, 2014)
2
+
3
+ BUG FIXES:
4
+
5
+ Fix a bug that I introduced right before pushing 0.0.1.
6
+
7
+ ## 0.0.1 (June 28, 2014)
8
+
9
+ Initial release.
10
+
11
+ FEATURES:
12
+
13
+ - Add the `vagrant docker-nsenter` command.
14
+
15
+ ## Backlog
16
+
17
+ - Test on Linux hosts without a proxy VM.
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vagrant-docker-nsenter.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem "vagrant", :git => "https://github.com/mitchellh/vagrant.git", :tag => 'v1.6.3'
8
+ end
9
+
10
+ group :plugins do
11
+ gem "vagrant-docker-nsenter", path: "."
12
+ end
13
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Steven Merrill
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.
@@ -0,0 +1,70 @@
1
+ # vagrant-docker-nsenter
2
+
3
+ This plugin allows you to use the `nsenter` command from your host (or from
4
+ your proxy VM if you are on Windows or Mac OS X) to run commands in your
5
+ Vagrant-provisioned Docker containers.
6
+
7
+ You can run non-interactive commands on all your containers or an interactive
8
+ command on a single container. The command defaults to an interactive
9
+ invocation of `/bin/bash` so that you get a shell in the container.
10
+
11
+ ## Considerations
12
+
13
+ The default boot2docker proxy VM does not come with nsenter. There
14
+ [an issue open for its inclusion in the boot2docker project](https://github.com/boot2docker/boot2docker/issues/374).
15
+
16
+ Therefore, at this time, this plugin may only be useful when running a custom
17
+ proxy VM using [the vagrant_vagrantfile or vagrant_machine options](https://docs.vagrantup.com/v2/docker/configuration.html)
18
+ to the Docker provider.
19
+
20
+ ## Getting started
21
+
22
+ To get started, you need to have Vagrant 1.6+ installed on your host machine.
23
+ To install the plugin, use the following command.
24
+
25
+ ```bash
26
+ vagrant plugin install vagrant-docker-nsenter
27
+ ```
28
+
29
+ ## Working with this plugin
30
+
31
+ The syntax for this command is similar to `vagrant docker-run` in that you
32
+ must put the commands you wish to run after an `--`.
33
+
34
+ The command will default to running interactively, but you may also specify
35
+ `--no-interactive` to run commands non-interactively.
36
+
37
+ Output of non-interactive commands defaults to being prefixed with the
38
+ container name. This can be disabled with the `--no-prefix` flag.
39
+
40
+ Given three containers called `web`, `php`, and `mysql` you can run a single
41
+ non-interactive command on all hosts.
42
+
43
+ ```bash
44
+ $ vagrant docker-nsenter --no-interactive -- hostname
45
+ ==> mysql: 6223741bc113
46
+ ==> php: 71dca3d53a0f
47
+ ==> web: fe1d05768d5b
48
+ ```
49
+
50
+ Or if you did not want prefixed output, add `--no-prefix`.
51
+
52
+ ```bash
53
+ $ vagrant docker-nsenter --no-interactive --no-prefix -- hostname
54
+ 6223741bc113
55
+ 71dca3d53a0f
56
+ fe1d05768d5b
57
+ ```
58
+
59
+ Finally, if you wanted to get an interactive shell, you can just invoke
60
+ `vagrant docker-nsenter` with the machine name. This will default to running
61
+ `/bin/bash`.
62
+
63
+ ```bash
64
+ $ vagrant docker-nsenter mysql
65
+ root@6223741bc113:/#
66
+ ```
67
+
68
+ ## Author
69
+
70
+ Steven Merrill (@stevenmerrill) wrote this for use at @phase2.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ # This file is required because Vagrant's plugin system expects
2
+ # an eponymous ruby file matching the rubygem.
3
+ #
4
+ # So this gem is called 'vagrant-docker-nsenter' and thus vagrant tries
5
+ # to require "vagrant-docker-nsenter"
6
+
7
+ require "vagrant-docker-nsenter/plugin"
8
+
9
+ require "pathname"
10
+
11
+ module VagrantPlugins
12
+ module DockerNSEnter
13
+ lib_path = Pathname.new(File.expand_path("../vagrant-docker-nsenter", __FILE__))
14
+ autoload :Errors, lib_path.join("errors")
15
+
16
+ def self.source_root
17
+ @source_root ||= Pathname.new(File.expand_path("../../", __FILE__))
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,95 @@
1
+ module VagrantPlugins
2
+ module DockerNSEnter
3
+ module Command
4
+ class NSEnter < Vagrant.plugin("2", :command)
5
+ def self.synopsis
6
+ "Use nsenter to start a command in a running container."
7
+ end
8
+
9
+ def execute
10
+ options = {}
11
+ options[:interactive] = true
12
+ options[:prefix] = true
13
+
14
+ opts = OptionParser.new do |o|
15
+ o.banner = "Usage: vagrant docker-nsenter [command...]"
16
+ o.separator ""
17
+ o.separator "Options:"
18
+ o.separator ""
19
+
20
+ o.on("--[no-]interactive", "Run the command interactively") do |i|
21
+ options[:interactive] = i
22
+ end
23
+
24
+ o.on("--[no-]prefix", "Prefix the output with the machine name") do |p|
25
+ options[:prefix] = p
26
+ end
27
+
28
+ end
29
+
30
+ # Parse out the extra args to send to nsenter, which is everything
31
+ # after the "--"
32
+ command = nil
33
+ split_index = @argv.index("--")
34
+ if split_index
35
+ command = @argv.drop(split_index + 1)
36
+ @argv = @argv.take(split_index)
37
+ end
38
+
39
+ # Parse the options
40
+ argv = parse_options(opts)
41
+ return if !argv
42
+
43
+ # Assume /bin/bash if no command is provided.
44
+ if !split_index
45
+ command = ["/bin/bash"]
46
+ end
47
+
48
+ target_opts = { provider: :docker }
49
+ target_opts[:single_target] = options[:pty]
50
+
51
+ with_target_vms(argv, target_opts) do |machine|
52
+ # Run it!
53
+ execute_single(machine, options, command)
54
+ end
55
+ 0
56
+ end
57
+
58
+ protected
59
+
60
+ # Gets the PID of the container and then runs the command in it via
61
+ # "sudo nsenter." Note that boot2docker does not currently ship with
62
+ # nsenter, so this will only work with other proxy VMs.
63
+ def execute_single(machine, options, command)
64
+ # @TODO: Check to see if the proxy VM has `nsenter` installed.
65
+ pid_command = ["docker", "inspect", "--format", "{{.State.Pid}}", machine.id]
66
+
67
+ pid = 0
68
+ pid_data = ""
69
+ machine.provider.driver.execute(*pid_command, options) do |type, data|
70
+ pid_data += data
71
+ end
72
+ # @TODO: Error handling.
73
+ pid = pid_data.to_i.to_s
74
+
75
+ nsenter_command = ["sudo", "nsenter", "-m", "-u", "-n", "-i", "-p", "-t", pid, "--"].concat(command)
76
+
77
+ # Run this interactively if asked.
78
+ nsenter_options = options
79
+ nsenter_options[:stdin] = true if options[:interactive]
80
+
81
+ nsenter_data = ""
82
+ machine.provider.driver.execute(*nsenter_command, nsenter_options) do |type, data|
83
+ nsenter_data += data
84
+ end
85
+
86
+ output_options = {}
87
+ output_options[:prefix] = false if !options[:prefix]
88
+ machine.ui.output(nsenter_data.chomp!, **output_options) if !nsenter_data.empty?
89
+
90
+ exit 0 if options[:interactive]
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,16 @@
1
+ require "vagrant"
2
+
3
+ module VagrantPlugins
4
+ module DockerNSEnter
5
+ module Errors
6
+ class VagrantDockerNSEnterError < Vagrant::Errors::VagrantError
7
+ error_namespace("vagrant_docker_nsenter.errors")
8
+ end
9
+
10
+ class VagrantTooOld < VagrantDockerNSEnterError
11
+ error_key(:vagrant_16_required)
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,39 @@
1
+ begin
2
+ require "vagrant"
3
+ rescue LoadError
4
+ raise "The Vagrant docker-nsenter plugin must be run within Vagrant."
5
+ end
6
+
7
+ # This is a sanity check to make sure no one is attempting to install
8
+ # this into an early Vagrant version.
9
+ if Vagrant::VERSION < "1.6.0"
10
+ raise Errors::VagrantTooOld
11
+ end
12
+
13
+ module VagrantPlugins
14
+ module DockerNSEnter
15
+ class Plugin < Vagrant.plugin("2")
16
+ name "docker-nsenter"
17
+ description <<-EOF
18
+ The vagrant-docker-nsenter plugin lets you run commands in containers
19
+ that are not running SSH.
20
+ EOF
21
+
22
+ command "docker-nsenter" do
23
+ require_relative "command/nsenter"
24
+ #init!
25
+ Command::NSEnter
26
+ end
27
+
28
+ #protected
29
+
30
+ #def self.init!
31
+ #return if defined?(@_init)
32
+ #I18n.load_path << File.expand_path(
33
+ #"templates/locales/providers_docker.yml", Vagrant.source_root)
34
+ #I18n.reload!
35
+ #@_init = true
36
+ #end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ module VagrantPlugins
2
+ module DockerNSEnter
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ en:
2
+ vagrant_docker_nsenter:
3
+ errors:
4
+ vagrant_16_required: |-
5
+ This plugin requires Vagrant 1.6.0 or newer to function.
data/test.rb ADDED
@@ -0,0 +1 @@
1
+ puts @argv.join(" ")
@@ -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 'vagrant-docker-nsenter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vagrant-docker-nsenter"
8
+ spec.version = VagrantPlugins::DockerNSEnter::VERSION
9
+ spec.authors = ["Steven Merrill"]
10
+ spec.email = ["steven.merrill@gmail.com"]
11
+ spec.summary = %q{A way to attach to Vagrant-managed Docker containers.}
12
+ spec.description = %q{The docker-nsenter plugin allows you to use the nsenter binary on your host or the Vagrant proxy VM to run commands or get a shell in Vagrant-managed Docker containers.}
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.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "pry"
24
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-docker-nsenter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Steven Merrill
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-06-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.5'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.5'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: pry
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: The docker-nsenter plugin allows you to use the nsenter binary on your
63
+ host or the Vagrant proxy VM to run commands or get a shell in Vagrant-managed Docker
64
+ containers.
65
+ email:
66
+ - steven.merrill@gmail.com
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - .gitignore
72
+ - CHANGELOG.md
73
+ - Gemfile
74
+ - LICENSE
75
+ - README.md
76
+ - Rakefile
77
+ - lib/vagrant-docker-nsenter.rb
78
+ - lib/vagrant-docker-nsenter/command/nsenter.rb
79
+ - lib/vagrant-docker-nsenter/errors.rb
80
+ - lib/vagrant-docker-nsenter/plugin.rb
81
+ - lib/vagrant-docker-nsenter/version.rb
82
+ - locales/en.yml
83
+ - test.rb
84
+ - vagrant-docker-nsenter.gemspec
85
+ homepage: ''
86
+ licenses:
87
+ - MIT
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ segments:
99
+ - 0
100
+ hash: 388879333041889696
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ segments:
108
+ - 0
109
+ hash: 388879333041889696
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.8.23
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: A way to attach to Vagrant-managed Docker containers.
116
+ test_files: []