dce 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/bin/dce +145 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b63433f17a92dff67527d1fa36c7f0098bd8a572
4
+ data.tar.gz: e35543115353f30be2f5985c73cb588e10e2c0c6
5
+ SHA512:
6
+ metadata.gz: 3d9cef81059cebb0f360b421ba212cf779ef31ee6e2d7bcf03af8c6772e8b4070dea2289a69e0f57979155ef773eb8aac7f71f0fc18fc3485f1ca333a5f2cee6
7
+ data.tar.gz: 464c3fb73e8c1725fdfe44b6748783fdeba2b88276f14e52863ebe737617f77c8b56b9473c38702e8f297cf4dd5408ff9c5205de0e7de3fc8dbc925be03d7cf8
data/bin/dce ADDED
@@ -0,0 +1,145 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Wishlist:
4
+ # Option to delete .dce_container.
5
+ # Option for using run instead of exec?
6
+
7
+ require 'yaml'
8
+
9
+ class DCE
10
+ # Run the command
11
+ def run
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
64
+
65
+ if !@compose_file
66
+ STDERR.puts "No docker-compose.yml file found."
67
+ exit!
68
+ end
69
+ end
70
+
71
+ return @compose_file
72
+ end
73
+
74
+ # Parse command line arguments
75
+ def parse_args
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
141
+ end
142
+ end
143
+
144
+ app = DCE.new
145
+ app.run
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dce
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Fini Hansen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'Run shell commands in docker '
14
+ email: xen@xen.dk
15
+ executables:
16
+ - dce
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/dce
21
+ homepage: https://github.com/xendk/dce
22
+ licenses:
23
+ - GPL-3.0
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.5.2
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: DCE
45
+ test_files: []