dce 0.0.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/bin/dce +13 -132
- data/lib/dce.rb +144 -0
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8e81156ac11f71cada7598d927c38a3e88e8a42725b656ce04da8fee67cb21e2
|
4
|
+
data.tar.gz: c3f9224417205d1af3fb96b84e68062d91dc0c098aafa32c8c5f57df7d9f7b95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5f27077366e80e654cdb4f68837507e010b79185f2f9f8eac246e5bb97ab48102ee5dfddfb2c6f34a31e0cb6089e73452b543fdb6a167d47676c25d2afbc56f
|
7
|
+
data.tar.gz: f8edd513dbd232f81f9005551adaf168409b8c90f60d7da9c193f0e4194c33cd0237836326803856f6c21e67118d7f686548bf5cb19c81483314a39f58e1c318
|
data/bin/dce
CHANGED
@@ -1,145 +1,26 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
# Wishlist:
|
4
|
-
# Option to delete .dce_container.
|
5
|
-
# Option for using run instead of exec?
|
6
|
-
|
7
3
|
require 'yaml'
|
4
|
+
if ENV['COVERAGE']
|
5
|
+
require 'simplecov'
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
parse_args
|
13
|
-
|
14
|
-
@conf_file = File.join(File.dirname(docker_compose_file), '.dce_container')
|
15
|
-
config_container = nil
|
16
|
-
if File.exists? @conf_file
|
17
|
-
config_container = File.read @conf_file
|
18
|
-
end
|
19
|
-
|
20
|
-
if @query
|
21
|
-
if config_container
|
22
|
-
STDOUT.puts(config_container)
|
23
|
-
exit
|
24
|
-
else
|
25
|
-
STDERR.puts("No container saved.")
|
26
|
-
exit!
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
if !@container
|
31
|
-
@container = config_container ? config_container : query_container
|
32
|
-
end
|
33
|
-
|
34
|
-
if @container != config_container
|
35
|
-
File.write(@conf_file, @container)
|
36
|
-
end
|
37
|
-
|
38
|
-
# If no command given, open a shell.
|
39
|
-
if (@command.strip.empty?)
|
40
|
-
@command = "if [ -e /usr/bin/fish ]; then /usr/bin/fish; elif [ -e /bin/bash ]; then /bin/bash; else /bin/sh; fi"
|
41
|
-
end
|
42
|
-
|
43
|
-
args = '-i'
|
44
|
-
args += 't' if STDIN.tty?
|
45
|
-
container_id = %x{docker-compose ps -q #{@container}}.chomp
|
46
|
-
command = "docker exec #{args} #{container_id} sh -c '#{@command}'"
|
47
|
-
STDERR.puts "Exec'ing: " + command if @verbose
|
48
|
-
exec command unless @dry_run
|
49
|
-
end
|
50
|
-
|
51
|
-
# Return path to the docker-compose.yml file
|
52
|
-
# Will exit with an error if not found
|
53
|
-
def docker_compose_file
|
54
|
-
unless @compose_file
|
55
|
-
dir = Dir.pwd()
|
56
|
-
while dir != "/"
|
57
|
-
file = File.join(dir, 'docker-compose.yml')
|
58
|
-
if FileTest.exists?(file)
|
59
|
-
@compose_file = file
|
60
|
-
break;
|
61
|
-
end
|
62
|
-
dir = File.dirname(dir)
|
63
|
-
end
|
7
|
+
# Redefine exec to save coverage first.
|
8
|
+
module Kernel
|
9
|
+
alias_method :exec_real, :exec
|
64
10
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
end
|
11
|
+
def exec(command)
|
12
|
+
SimpleCov.result
|
13
|
+
exec_real command
|
69
14
|
end
|
70
|
-
|
71
|
-
return @compose_file
|
72
15
|
end
|
73
16
|
|
74
|
-
|
75
|
-
|
76
|
-
# Not using a proper option parse library, as it will get confused
|
77
|
-
# by options for the command given. We use a simple parser.
|
78
|
-
while /^-/ =~ ARGV[0]
|
79
|
-
option = ARGV.shift
|
80
|
-
case option
|
81
|
-
when '-c', '--container'
|
82
|
-
@container = ARGV.shift
|
83
|
-
if !get_containers.include? @container
|
84
|
-
STDERR.puts "Unknown container #{@container}"
|
85
|
-
exit!
|
86
|
-
end
|
87
|
-
when '-v', '--verbose'
|
88
|
-
@verbose = true
|
89
|
-
when '-n', '--dry-run'
|
90
|
-
@verbose = true
|
91
|
-
@dry_run = true
|
92
|
-
when '-?', '--print-service'
|
93
|
-
@query = true
|
94
|
-
when '-h', '--help'
|
95
|
-
STDERR.puts <<-HEREDOC
|
96
|
-
Usage: #{File.basename($0)} [OPTIONS]... COMMAND
|
97
|
-
Runs COMMAND in docker-compose container.
|
98
|
-
|
99
|
-
On first run, asks for the service container to use and saves it to .dce_container next
|
100
|
-
to the docker-compose.yml file.
|
101
|
-
|
102
|
-
If no command given, opens a shell.
|
103
|
-
|
104
|
-
Options:
|
105
|
-
-c, --container SERVICE use the container of the specified service
|
106
|
-
replaces the selected container in the .dce_container
|
107
|
-
-v, --verbose print exec'ed command
|
108
|
-
-n, --dry-run only print exec'ed command, don't run
|
109
|
-
-?, --print-service print the service saved
|
110
|
-
-h, --help print this help and exit
|
111
|
-
|
112
|
-
HEREDOC
|
113
|
-
exit
|
114
|
-
else
|
115
|
-
STDERR.puts "Unknown option #{option}"
|
116
|
-
exit!
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
@command = ARGV.join(' ')
|
121
|
-
end
|
122
|
-
|
123
|
-
# Ask the user to select a container
|
124
|
-
# The options are taken from the docker-compose.yml file
|
125
|
-
def query_container
|
126
|
-
containers = get_containers
|
127
|
-
STDERR.puts "Please select container [#{containers.join(', ')}]"
|
128
|
-
choice = STDIN.gets.strip
|
129
|
-
exit if choice.empty?
|
130
|
-
if !containers.include?(choice)
|
131
|
-
STDERR.puts "Illegal choice."
|
132
|
-
exit!
|
133
|
-
end
|
134
|
-
choice
|
135
|
-
end
|
136
|
-
|
137
|
-
# Read containers from docker-compose.yml
|
138
|
-
def get_containers
|
139
|
-
content = YAML::load(File.read(docker_compose_file))
|
140
|
-
content.has_key?('version') ? content['services'].keys : content.keys
|
17
|
+
SimpleCov.at_exit do
|
18
|
+
SimpleCov.result
|
141
19
|
end
|
142
20
|
end
|
143
21
|
|
22
|
+
# Possibly not the right way to load file.
|
23
|
+
require_relative '../lib/dce.rb'
|
24
|
+
|
144
25
|
app = DCE.new
|
145
26
|
app.run
|
data/lib/dce.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
# Wishlist:
|
2
|
+
# Option to delete .dce_container.
|
3
|
+
# Option for using run instead of exec?
|
4
|
+
|
5
|
+
class DCE
|
6
|
+
# Run the command
|
7
|
+
def run
|
8
|
+
parse_args
|
9
|
+
|
10
|
+
@conf_file = File.join(File.dirname(docker_compose_file), '.dce_container')
|
11
|
+
config_container = nil
|
12
|
+
if File.exists? @conf_file
|
13
|
+
config_container = File.read @conf_file
|
14
|
+
end
|
15
|
+
|
16
|
+
if @list_containers
|
17
|
+
STDOUT.puts(get_containers.join(', '))
|
18
|
+
exit
|
19
|
+
end
|
20
|
+
|
21
|
+
if @query
|
22
|
+
if config_container
|
23
|
+
STDOUT.puts(config_container)
|
24
|
+
exit
|
25
|
+
else
|
26
|
+
abort "No container saved."
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
if !@container
|
31
|
+
@container = config_container ? config_container : query_container
|
32
|
+
end
|
33
|
+
|
34
|
+
if @container != config_container
|
35
|
+
File.write(@conf_file, @container)
|
36
|
+
end
|
37
|
+
|
38
|
+
# If no command given, open a shell.
|
39
|
+
if (@command.strip.empty?)
|
40
|
+
@command = "if [ -e /usr/bin/fish ]; then /usr/bin/fish; elif [ -e /bin/bash ]; then /bin/bash; else /bin/sh; fi"
|
41
|
+
end
|
42
|
+
|
43
|
+
args = '-i'
|
44
|
+
args += 't' if STDIN.tty?
|
45
|
+
container_id = %x{docker-compose ps -q #{@container}}.chomp
|
46
|
+
|
47
|
+
abort("Container #{@container} not created.") if container_id.empty?
|
48
|
+
|
49
|
+
command = "docker exec #{args} #{container_id} sh -c '#{@command}'"
|
50
|
+
STDERR.puts "Exec'ing: " + command if @verbose
|
51
|
+
exec command unless @dry_run
|
52
|
+
end
|
53
|
+
|
54
|
+
# Return path to the docker-compose.yml file
|
55
|
+
# Will exit with an error if not found
|
56
|
+
def docker_compose_file
|
57
|
+
unless @compose_file
|
58
|
+
dir = Dir.pwd()
|
59
|
+
while dir != "/"
|
60
|
+
file = File.join(dir, 'docker-compose.yml')
|
61
|
+
if FileTest.exists?(file)
|
62
|
+
@compose_file = file
|
63
|
+
break;
|
64
|
+
end
|
65
|
+
dir = File.dirname(dir)
|
66
|
+
end
|
67
|
+
|
68
|
+
if !@compose_file
|
69
|
+
abort "No docker-compose.yml file found."
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
return @compose_file
|
74
|
+
end
|
75
|
+
|
76
|
+
# Parse command line arguments
|
77
|
+
def parse_args
|
78
|
+
# Not using a proper option parse library, as it will get confused
|
79
|
+
# by options for the command given. We use a simple parser.
|
80
|
+
while /^-/ =~ ARGV[0]
|
81
|
+
option = ARGV.shift
|
82
|
+
case option
|
83
|
+
when '-c', '--container'
|
84
|
+
@container = ARGV.shift
|
85
|
+
if !get_containers.include? @container
|
86
|
+
abort "Unknown container #{@container}"
|
87
|
+
end
|
88
|
+
when '-v', '--verbose'
|
89
|
+
@verbose = true
|
90
|
+
when '-n', '--dry-run'
|
91
|
+
@verbose = true
|
92
|
+
@dry_run = true
|
93
|
+
when '-?', '--print-service'
|
94
|
+
@query = true
|
95
|
+
when '-l', '--list-containers'
|
96
|
+
@list_containers = true
|
97
|
+
when '-h', '--help'
|
98
|
+
STDERR.puts <<-HEREDOC
|
99
|
+
Usage: #{File.basename($0)} [OPTIONS]... COMMAND
|
100
|
+
Runs COMMAND in docker-compose container.
|
101
|
+
|
102
|
+
On first run, asks for the service container to use and saves it to .dce_container next
|
103
|
+
to the docker-compose.yml file.
|
104
|
+
|
105
|
+
If no command given, opens a shell.
|
106
|
+
|
107
|
+
Options:
|
108
|
+
-c, --container SERVICE use the container of the specified service
|
109
|
+
replaces the selected container in the .dce_container
|
110
|
+
-v, --verbose print exec'ed command
|
111
|
+
-n, --dry-run only print exec'ed command, don't run
|
112
|
+
-?, --print-service print the service saved
|
113
|
+
-l, --list-containers print the containers available
|
114
|
+
-h, --help print this help and exit
|
115
|
+
|
116
|
+
HEREDOC
|
117
|
+
exit
|
118
|
+
else
|
119
|
+
abort "Unknown option #{option}"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
@command = ARGV.join(' ')
|
124
|
+
end
|
125
|
+
|
126
|
+
# Ask the user to select a container
|
127
|
+
# The options are taken from the docker-compose.yml file
|
128
|
+
def query_container
|
129
|
+
containers = get_containers
|
130
|
+
STDERR.puts "Please select container [#{containers.join(', ')}]"
|
131
|
+
choice = STDIN.gets.strip
|
132
|
+
exit if choice.empty?
|
133
|
+
if !containers.include?(choice)
|
134
|
+
abort "Illegal choice."
|
135
|
+
end
|
136
|
+
choice
|
137
|
+
end
|
138
|
+
|
139
|
+
# Read containers from docker-compose.yml
|
140
|
+
def get_containers
|
141
|
+
content = YAML::load(File.read(docker_compose_file))
|
142
|
+
content.has_key?('version') ? content['services'].keys : content.keys
|
143
|
+
end
|
144
|
+
end
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dce
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thomas Fini Hansen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: Run shell commands in docker.
|
14
14
|
email: xen@xen.dk
|
15
15
|
executables:
|
16
16
|
- dce
|
@@ -18,6 +18,7 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- bin/dce
|
21
|
+
- lib/dce.rb
|
21
22
|
homepage: https://github.com/xendk/dce
|
22
23
|
licenses:
|
23
24
|
- GPL-3.0
|
@@ -37,8 +38,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
37
38
|
- !ruby/object:Gem::Version
|
38
39
|
version: '0'
|
39
40
|
requirements: []
|
40
|
-
|
41
|
-
rubygems_version: 2.5.2
|
41
|
+
rubygems_version: 3.0.3
|
42
42
|
signing_key:
|
43
43
|
specification_version: 4
|
44
44
|
summary: DCE
|