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.



9
10
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 9

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

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

  if @fig
    build_fig
  end

  exit 0
end

Class Method Details

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



5
6
7
# File 'lib/building.rb', line 5

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

Instance Method Details

#build_containerObject



84
85
86
87
# File 'lib/building.rb', line 84

def build_container
  pid = fork { exec "docker build -t #{@app_name}:#{@tag} ." }
  Process.waitpid(pid)
end

#build_figObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/building.rb', line 70

def build_fig
  File.open(@fig , "w") do |file|
    file << <<-eof
web:
image: #{@app_name}:#{@tag}
command: /start web
environment:
  PORT: #{@port || 8080}
ports:
 - #{@port || 8080}
eof
  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
# File 'lib/building.rb', line 35

def create_dockerfile
  File.open("Dockerfile" , "w") do |file|
    file << "FROM #{@from || "ctlc/buildstep:ubuntu13.10"}\n"
  end

  if @buildpack_url
    File.open("Dockerfile" , "a") do |file|
      file << <<-eof
RUN git clone --depth 1 #{@buildpack_url} /build/buildpacks/#{@buildpack_url.split("/").last.sub(".git","")}
RUN echo #{@buildpack_url} >> /build/buildpacks.txt
eof
    end
  end

  if @includes
    File.open("Dockerfile" , "a") do |file|
      file << "RUN #{@includes}\n"
    end
  end

  if @file
    File.open("Dockerfile" , "a") do |file|
      file << IO.read(@file)
    end
  end

  File.open("Dockerfile" , "a") do |file|
    file << <<-eof
ADD . /app
RUN /build/builder
CMD /start web
eof
  end
end

#explain_container(port) ⇒ Object



89
90
91
92
# File 'lib/building.rb', line 89

def explain_container(port)
  run = "docker run -d -p #{port} -e \"PORT=#{port}\" #{@app_name}:#{@tag}"
  puts "\nTo run your app, try something like this:\n\n\t#{run}\n\n"
end

#run_container(port) ⇒ Object



94
95
96
97
98
# File 'lib/building.rb', line 94

def run_container(port)
  run = "\ndocker run -d -p #{port} -e \"PORT=#{port}\" #{@app_name}:#{@tag}"
  puts "#{run}"
  exec run
end