Class: Dockerfile2bash

Inherits:
Object
  • Object
show all
Defined in:
lib/dockerfile2bash.rb

Constant Summary collapse

VERSION =
'0.1.4'
FIELDS =
%w(from user run add copy arg env expose cmd onbuild)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dockerfile) ⇒ Dockerfile2bash

Returns a new instance of Dockerfile2bash.



9
10
11
12
13
# File 'lib/dockerfile2bash.rb', line 9

def initialize(dockerfile)
  @dockerfile = dockerfile
  @content = get_content
  @commands = []
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



6
7
8
# File 'lib/dockerfile2bash.rb', line 6

def commands
  @commands
end

Instance Method Details

#generate_bashObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dockerfile2bash.rb', line 66

def generate_bash()
  return unless @commands
  bash = "#!/usr/bin/env bash \n\n# The script is generated from a Dockerfile via Dockerfile2bash(v#{VERSION})\n# By B1nj0y <[email protected]>\n\n"
  @commands.each do |cmd|
    case cmd.keys[0]
    when "from"
      bash << "# The original Dockerfile is from a base image: <#{cmd["from"]}> \n\n"
    when "run"
      bash << cmd["run"] << "\n"
    when "arg"
      bash << cmd["arg"].join("=") << "\n"
    when "env"
      env_str = "export " << cmd["env"]
      bash << env_str << "\n"
      bash << "echo \'#{env_str}\' >> ~/.bashrc" << "\n"
    end
  end
  bash
end

#get_contentObject



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dockerfile2bash.rb', line 86

def get_content
  if @dockerfile =~ /^https:\/\/raw\.githubusercontent\.com\/.+$/
    content = RestClient.get(@dockerfile)
  elsif @dockerfile =~ /^https:\/\/github\.com\/(.+)$/
    # convert to its corresponding raw url
    url = "https://raw.githubusercontent.com/#{$1.sub('/blob/', '/')}"
    content = RestClient.get(url)
  else
    # local file
    content = File.read(@dockerfile)
  end
  content
end

#parseObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dockerfile2bash.rb', line 15

def parse
  return unless @content || @content.empty?
  @content.gsub!("\\\n", "")
  lines = @content.split(/\r?\n+/) || []

  lines.each do |line|
    # ignore blank and comment lines
    next if /^\s*$/ =~ line
    next if /^\s*#/ =~ line
    segments = line.split(" ", 2)
    next if segments.length < 2 or !FIELDS.include?(segments[0].downcase)

    case segments[0].downcase!
    when "from", "user", "run", "expose", "copy", "add"
      @commands << { segments[0] => segments[1] }
    when "cmd"
      @commands << { "cmd" => (JSON.parse(segments[1]) || []).join(" ") }
    when "arg"
      args = segments[1].split("=", 2)
      if args.length == 2
        @commands << { "arg" => args }
      end
    when "env"
      if segments[1] =~ /^[a-zA-Z_].[a-zA-Z0-9_]+?=("|')[^"']*?\1$/
        @commands << { "env" => segments[1] }
        next
      end

      envs = segments[1].split
      pattern = %r/^[a-zA-Z_].[a-zA-Z0-9_]+?=.+$/
      case envs.length
      when 1
        @commands << { "env" => segments[1] }
      when 2
        if envs.all? { |e| e =~ pattern }
          @commands << { "env" => segments[1] }
        else
          @commands << { "env" => envs.join("=") }
        end
      else
        if envs.all? { |e| e =~ pattern }
          @commands << { "env" => segments[1] }
        else
          @commands << { "env" => [envs[0], "\"#{envs[1..-1].join(" ")}\""].join("=") }
        end
      end
    end
  end
  @commands
end