Class: Building

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name, tag, options = {}) ⇒ Building

Returns a new instance of Building.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/building.rb', line 11

def initialize(app_name, tag, options={})
  @app_name = app_name
  @tag = tag || "latest"
  @buildpack_url = options[:buildpack_url]
  @includes = options[:includes]
  @file = options[:file]
  @from = options[:from]
  @fig = options[:fig]
  @port = options[:port]

  create_dockerfile
  build_container

  build_fig if @fig

  if @port
    run_container(@port)
  else
    explain_container(8080)
  end

  exit 0
end

Class Method Details

.convert(app_name, tag, options = {}) ⇒ Object



7
8
9
# File 'lib/building.rb', line 7

def self.convert(app_name, tag, options={})
  Building.new(app_name, tag, options)
end

Instance Method Details

#build_containerObject



125
126
127
128
129
130
# File 'lib/building.rb', line 125

def build_container
  build_cmd = "docker build -t #{@app_name}:#{@tag} ."
  say %{    <%= color('building', [BLUE, BOLD]) %>  #{build_cmd}}
  pid = fork { exec build_cmd }
  Process.waitpid(pid)
end

#build_figObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/building.rb', line 87

def build_fig
  figfile = <<-eof
web:
image: #{@app_name}:#{@tag}
command: /start web
environment:
  PORT: #{@port || 8080}
ports:
 - #{@port || 8080}
eof

  skip = false
  
  if File.exists?(@fig)
    if IO.read(@fig) == figfile
      skip = true
      say %{   <%= color('identical', [BLUE, BOLD]) %>  #{@fig}}
    else
      say %{    <%= color('conflict', [RED, BOLD]) %>  #{@fig}}
      choice = ask("Overwrite #{File.expand_path(@fig)}? [Yn] ")
      if choice.downcase == "n"
        say("Aborting...")
        exit 1
      else
        say %{       <%= color('force', [YELLOW, BOLD]) %>  #{@fig}}
      end
    end
  else
    say %{      <%= color('create', [GREEN, BOLD]) %>  #{@fig}}
  end

  if !skip
    File.open(@fig , "w") do |file|
      file << figfile
    end
  end
end

#create_dockerfileObject



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/building.rb', line 35

def create_dockerfile
  dockerfile = []
  dockerfile << "FROM #{@from || "ctlc/buildstep:ubuntu13.10"}\n"

  if @buildpack_url
    dockerfile << <<-eof
RUN git clone --depth 1 #{@buildpack_url} /build/buildpacks/#{@buildpack_url.split("/").last.sub(".git","")}
RUN echo #{@buildpack_url} | cat - /build/buildpacks.txt > /tmp/out && mv /tmp/out /build/buildpacks.txt
eof
  end

  if @includes
    dockerfile << "RUN #{@includes}\n"
  end

  if @file
    dockerfile << IO.read(@file)
  end

  dockerfile << <<-eof
ADD . /app
RUN /build/builder
CMD /start web
eof
  
  skip = false

  if File.exists?("Dockerfile")
    if IO.read("Dockerfile") == dockerfile.join("\n")
      skip = true
      say %{   <%= color('identical', [BLUE, BOLD]) %>  Dockerfile}
    else
      say %{    <%= color('conflict', [RED, BOLD]) %>  Dockerfile}
      choice = ask("Overwrite Dockerfile? [Yn] ")
      if choice.downcase == "n"
        say("Aborting...")
        exit 1
      else
        say %{       <%= color('force', [YELLOW, BOLD]) %>  Dockerfile}
      end
    end
  else
    say %{      <%= color('create', [GREEN, BOLD]) %>  Dockerfile}
  end

  if !skip
    File.open("Dockerfile" , "w") do |file|
      file << dockerfile.join("\n")
    end
  end
end

#explain_container(port) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/building.rb', line 132

def explain_container(port)
  if @fig
    run = "fig up -d"
  else
    run = "docker run -d -p #{port} -e \"PORT=#{port}\" #{@app_name}:#{@tag}"
  end
  rebuild = "docker build -t #{@app_name} ."

  say "        <%= color('hint', [YELLOW, BOLD]) %>       To run your app, try:  <%= color('#{run}', [BOLD]) %>"
  say "        <%= color('hint', [YELLOW, BOLD]) %>  To re-build your app, try:  <%= color('#{rebuild}', [BOLD]) %>"
end

#run_container(port) ⇒ Object



144
145
146
147
148
# File 'lib/building.rb', line 144

def run_container(port)
  run = "docker run -d -p #{port} -e \"PORT=#{port}\" #{@app_name}:#{@tag}"
  say "     <%= color('running', [BLUE, BOLD]) %>  #{run}"
  exec run
end