Closed L30Bola closed 5 years ago
The require_relative '../etc/docker-helper/docker_helper'
will load the docker_helper.rb
file, which will include the colorize
and color
methods.
This file also depends on shellwords
and English
. I think English
is a core dependency, but maybe you'll need to gem install shellwords
.
Sorry, this is a bit of a mess :)
Installed this English gems: https://rubygems.org/gems/rubysl-english/versions/2.0.0 https://rubygems.org/gems/english/versions/0.6.3
And this Shellwords: https://rubygems.org/gems/rubysl-shellwords/versions/2.0.0
Still getting the same error.
Could you try with the latest version of the script? I removed the dependencies to shellwords
and English
https://github.com/pixelastic/oroshi/blob/master/scripts/bin/docker-container-list
If it still does not work, could you try using an absolute path for the require_relative
instead of a relative one?
Tried with both the relative and the absolute path on require_relative
. On relative he wasn't able to find docker_helper. On absolute path, I got the same error:
/usr/local/bin/docker-container-list:39:in `output_name': undefined method `color' for #<DockerContainerList:0x0055e98e88fef0> (NoMethodError)
from /usr/local/bin/docker-container-list:71:in `block in run'
from /usr/local/bin/docker-container-list:70:in `each'
from /usr/local/bin/docker-container-list:70:in `run'
from /usr/local/bin/docker-container-list:79:in `<main>'
That's really weird. The include DockerHelper
tells Ruby to load all the classes defined in the docker_helper
module to the current class, so the class should be able to call color
...
What version of Ruby are you using? I'm using Ruby 2.2.
Otherwise, try to replace the require_relative
line with a copy-paste of the content of the helper file... maybe it's just a path issue.
I have installed the same Ruby version as you. (ruby 2.2.5p319 (2016-04-26 revision 54774) [x86_64-linux]
)
My require_relative
is like this:
require_relative '/usr/local/share/gems/gems/docker_helper-0.0.2/lib/docker_helper'
What should it become?
You can try to replace the whole script with this version:
#!/usr/bin/env ruby
module DockerHelper
@@colors = {
container: 69,
container_running: 35,
container_stopped: 241,
hash: 67,
image: 136,
image_ubuntu: 202,
ports: 241,
tag: 241
}
def color(type)
@@colors[type]
end
def image_color(image)
color_symbol = ('image_' + image.tr('-', '_')).to_sym
return @@colors[color_symbol] if @@colors[color_symbol]
@@colors[:image]
end
def colorize(text, color)
color = format('%03d', color)
"[38;5;#{color}m#{text}[00m"
end
def longest_by_type(list, type)
ordered = list.map { |obj| obj[type] }.group_by(&:size)
return nil if ordered.empty?
ordered.max.last[0]
end
end
Container = Struct.new(:name, :hash, :image, :ports, :is_running)
# Display the list of current docker containers
class DockerContainerList
include DockerHelper
def initialize(*args)
@args = args
@container_list = container_list
end
def container_list
options = [
'docker',
'ps',
'--format "{{.Names}};{{.ID}};{{.Image}};{{.Ports}};{{.Status}}"',
@args.join(' ')
]
output = `#{options.join(' ')}`
containers = []
output.each_line do |line|
name, hash, image, ports, status = line.strip.split(';')
containers << Container.new(name, hash, image, ports, running?(status))
end
containers.sort_by do |c|
[c[:is_running] ? 0 : 1, c[:name]]
end
end
def running?(status)
!status.match(/^Up/).nil?
end
def output_name(container)
if container[:is_running]
icon = '>'
color = color(:container_running)
else
icon = 'x'
color = color(:container_stopped)
end
name = container[:name].ljust(longest_name_length)
colorize("#{icon} #{name}", color)
end
def longest_name_length
@container_list.map { |obj| obj[:name] }.group_by(&:size).max.last[0].length
end
def longest_image_length
@container_list.map { |obj| obj[:image] }.group_by(&:size).max.last[0].length
end
def output_hash(container)
colorize(container[:hash], color(:hash))
end
def output_image(container)
image = container[:image].ljust(longest_image_length)
colorize(image, color(:image))
end
def output_ports(container)
colorize(container[:ports], color(:ports))
end
def run
@container_list.each do |container|
name = output_name(container)
hash = output_hash(container)
image = output_image(container)
ports = output_ports(container)
puts "#{name} #{image} #{hash} #{ports}"
end
end
end
DockerContainerList.new(*ARGV).run
I just inlined the required file inside the main script
Sorry for this time without any output to you. Had some problems. So with this new script, I don't get an error anymore but the output is like this:
�[38;5;035m> sisref_mysql_1 �[00m �[38;5;136mmysql:5.6 �[00m �[38;5;067m78c108617224�[00m �[38;5;241m0.0.0.0:443->3306/tcp�[00m
�[38;5;035m> sisref_sisref_1�[00m �[38;5;136mdebian_sisref�[00m �[38;5;067me9b9ad7718b9�[00m �[38;5;241m0.0.0.0:8080->80/tcp�[00m
Ah yes, seems like the copy-paste of a special character did not went well on my latest script. Here is how the image_color
method is displayed in my editor:
As you can see, there is a special ^[
character, which is actually an ESC
character (and not a literal ^[
), used in the ANSI Escape Codes. I'm not sure how to correctly copy/paste it on GitHub, but you should be able to find it in the Wikipedia page I just linked, or in the original script in the repo.
I get this error running docker-container-list:
And if running docker-image-list:
Are there any dependencies besides colorize, docker_helper and color? Never really programmed in ruby D: