Class: Qtc::Cli::Mar::Debug

Inherits:
Base
  • Object
show all
Defined in:
lib/qtc/cli/mar/debug.rb

Instance Attribute Summary

Attributes included from Common

#datacenter_id

Instance Method Summary collapse

Methods included from Common

#client, #current_cloud_dc, #current_cloud_id, #current_cloud_token, #extract_app_in_dir, #ini_filename, #inifile, #instance_info, #platform_base_url, #platform_client

Instance Method Details

#build_slug(app_home, stack) ⇒ Object



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/qtc/cli/mar/debug.rb', line 50

def build_slug(app_home, stack)
  docker_id = nil
  run_opts = ''
  if File.exists?("#{app_home}/.env")
    run_opts << "--env-file=#{app_home}/.env"
  end
  Open3.popen3("docker run -d #{run_opts} -v #{app_home}:/tmp/gitrepo:r qtcs/slugbuilder:#{stack}") {|stdin, stdout, stderr, wait_thr|
    docker_id = stdout.gets
    if docker_id
      docker_id.strip!
    else
      puts stderr.gets
    end
    exit_status = wait_thr.value
    unless exit_status.success?
      raise "ERROR: build failed to start"
    end
  }
  Open3.popen2('docker', 'attach', docker_id){|stdin, stdout, wait_thr|
    stdin.close
    while line = stdout.gets
      puts line
    end
    exit_status = wait_thr.value
    unless exit_status.success?
      raise "ERROR: build failed to complete"
    end
  }
  puts "-----> Extracting slug from build image to ./slug.tgz"
  system("docker cp #{docker_id}:/tmp/slug.tgz . > /dev/null")

  docker_id
rescue => exc
  system("docker rm -f #{docker_id}") if docker_id
  raise exc
end

#local_build_slug(options) ⇒ Object



43
44
45
46
47
48
# File 'lib/qtc/cli/mar/debug.rb', line 43

def local_build_slug(options)
  stack = options.stack || 'cedar-14'
  app_home = File.realpath('.')
  puts "-----> Starting to build MAR app locally"
  build_slug(app_home, stack)
end

#local_debug(commands, options) ⇒ Object



8
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
34
35
36
37
38
39
40
41
# File 'lib/qtc/cli/mar/debug.rb', line 8

def local_debug(commands, options)
    app_home = File.realpath('.')
    docker_id = nil
    stack = options.stack || 'cedar-14'
    puts "-----> Starting to build MAR app locally"

    if options.clean == true && File.exists?("#{app_home}/slug.tgz")
      File.delete("#{app_home}/slug.tgz")
    end

    unless File.exists?("#{app_home}/slug.tgz")
      docker_id = build_slug(app_home, stack)
    else
      puts "       Existing slug.tgz found, build not needed."
    end
    puts "-----> Starting app container"
    run_opts = [
      '-e PORT=5000',
      "-e STACK=#{stack}",
      '-e SLUG_URL=file:///tmp/fake_slug.tgz',
      '-p 5000',
      "-v #{app_home}/slug.tgz:/tmp/fake_slug.tgz"
    ]
    if File.exists?("#{app_home}/.env")
      run_opts << "--env-file=#{app_home}/.env"
    end
    if commands.size == 0
      cmd = 'start web'
    else
      cmd = commands.join(" ")
    end

    exec("docker run -it #{run_opts.join(" ")} qtcs/slugrunner:#{stack} #{cmd}")
end