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, branch) ⇒ Object



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
86
87
88
89
90
91
92
93
94
# File 'lib/qtc/cli/mar/debug.rb', line 55

def build_slug(app_home, stack, branch)
  docker_id = nil
  run_opts = [
      '-i',
      '-a stdin'
  ]
  if File.exists?("#{app_home}/.env")
    run_opts << "--env-file=#{app_home}/.env"
  end
  Open3.popen3("git archive #{branch} | docker run #{run_opts.join(' ')} qtcs/slugbuilder:#{stack}") {|stdin, stdout, stderr, wait_thr|
    stdin.close
    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



45
46
47
48
49
50
51
52
53
# File 'lib/qtc/cli/mar/debug.rb', line 45

def local_build_slug(options)
  stack = options.stack || 'cedar-14'
  branch = options.branch || 'master'
  app_home = File.realpath('.')
  puts "-----> Starting to build MAR app locally"
  puts "       Using stack: #{stack}"
  puts "       Using git branch: #{branch}"
  build_slug(app_home, stack, branch)
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
42
43
# File 'lib/qtc/cli/mar/debug.rb', line 8

def local_debug(commands, options)
    app_home = File.realpath('.')
    stack = options.stack || 'cedar-14'
    branch = options.branch || 'master'
    puts "-----> Starting to build MAR app locally"
    puts "       Using stack: #{stack}"
    puts "       Using git branch: #{branch}"

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

    unless File.exists?("#{app_home}/slug.tgz")
      build_slug(app_home, stack, branch)
    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