Module: Dapp::Dapp::Ruby2Go

Included in:
Dapp::Dapp
Defined in:
lib/dapp/dapp/ruby2go.rb

Instance Method Summary collapse

Instance Method Details

#_download_ruby2go_bin(progname, bin_path) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dapp/dapp/ruby2go.rb', line 89

def _download_ruby2go_bin(progname, bin_path)
  lock("downloader.bin.#{progname}", default_timeout: 1800) do
    return if File.exists? bin_path

    log_process("Downloading #{progname} dapp dependency") do
      location = URI("https://dl.bintray.com/flant/dapp/#{::Dapp::VERSION}/#{progname}")

      tmp_bin_path = File.join(self.class.tmp_base_dir, "#{progname}-#{SecureRandom.uuid}")
      ::Dapp::Downloader.download(location, tmp_bin_path, show_progress: true, progress_titile: bin_path)

      checksum_location = URI("https://dl.bintray.com/flant/dapp/#{::Dapp::VERSION}/#{progname}.sha")
      tmp_bin_checksum_path = tmp_bin_path + ".checksum"
      ::Dapp::Downloader.download(checksum_location, tmp_bin_checksum_path)

      if Digest::SHA256.hexdigest(File.read(tmp_bin_path)) != File.read(tmp_bin_checksum_path).strip
        raise ::Dapp::Error::Dapp, code: :ruby2go_download_failed_bad_checksum, data: {url: location.to_s, checksum_url: checksum_location.to_s, progname: progname}
      end

      File.chmod(0755, tmp_bin_path)
      FileUtils.mkdir_p File.dirname(bin_path)
      FileUtils.mv tmp_bin_path, bin_path
    end # log_process
  end # lock
end

#_ruby2go(progname, args_hash) ⇒ Object



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
# File 'lib/dapp/dapp/ruby2go.rb', line 46

def _ruby2go(progname, args_hash)
  call_id = SecureRandom.uuid

  args_file = File.join(_ruby2go_tmp_dir, "args.#{call_id}.json")
  File.open(args_file, "w") {|f| f.write JSON.dump(args_hash)}

  res_file = File.join(_ruby2go_tmp_dir, "res.#{call_id}.json")

  if bin_path = ENV[_ruby2go_bin_path_env_var_name(progname)]
    unless File.exists? bin_path
      raise ::Dapp::Error::Dapp,
        code: :ruby2go_bin_path_not_found,
        data: {env_var_name: _ruby2go_bin_path_env_var_name(progname), path: bin_path}
    end
  else
    bin_path = File.join(::Dapp::Dapp.home_dir, "bin", progname, ::Dapp::VERSION, progname)
    unless File.exists? bin_path
      _download_ruby2go_bin(progname, bin_path)
    end
  end

  begin
    exec("#{bin_path} -args-from-file #{args_file} -result-to-file #{res_file}") unless (pid = fork)
    pid, status = Process.waitpid2(pid)
  rescue Interrupt => _e
    Process.kill('INT', pid)
    raise
  end

  status_code = status.exitstatus
  if [0, 16].include?(status_code)
    res = nil
    File.open(res_file, "r") {|f| res = JSON.load(f.read)}
    res
  else
    raise ::Dapp::Error::Base, code: :ruby2go_command_unexpected_exitstatus, data: { progname: progname, status_code: status_code }
  end
end

#_ruby2go_bin_path_env_var_name(progname) ⇒ Object



42
43
44
# File 'lib/dapp/dapp/ruby2go.rb', line 42

def _ruby2go_bin_path_env_var_name(progname)
  "DAPP_BIN_#{progname.gsub("-", "_").upcase}"
end

#_ruby2go_tmp_dirObject



85
86
87
# File 'lib/dapp/dapp/ruby2go.rb', line 85

def _ruby2go_tmp_dir
  @_ruby2go_tmp_dir ||= Dir.mktmpdir('dapp-ruby2go-', tmp_base_dir)
end

#ruby2go_builder(args_hash) ⇒ Object



12
13
14
# File 'lib/dapp/dapp/ruby2go.rb', line 12

def ruby2go_builder(args_hash)
  _ruby2go("builder", args_hash)
end

#ruby2go_cleanup(args_hash) ⇒ Object



28
29
30
# File 'lib/dapp/dapp/ruby2go.rb', line 28

def ruby2go_cleanup(args_hash)
  _ruby2go("cleanup", args_hash)
end

#ruby2go_dappdeps(args_hash) ⇒ Object



20
21
22
# File 'lib/dapp/dapp/ruby2go.rb', line 20

def ruby2go_dappdeps(args_hash)
  _ruby2go("dappdeps", args_hash)
end

#ruby2go_docker_registry(args_hash) ⇒ Object



8
9
10
# File 'lib/dapp/dapp/ruby2go.rb', line 8

def ruby2go_docker_registry(args_hash)
  _ruby2go("docker_registry", args_hash)
end

#ruby2go_git_artifact(args_hash) ⇒ Object



16
17
18
# File 'lib/dapp/dapp/ruby2go.rb', line 16

def ruby2go_git_artifact(args_hash)
  _ruby2go("git-artifact", args_hash)
end

#ruby2go_git_repo(args_hash) ⇒ Object



24
25
26
# File 'lib/dapp/dapp/ruby2go.rb', line 24

def ruby2go_git_repo(args_hash)
  _ruby2go("git-repo", args_hash)
end

#ruby2go_image(args_hash) ⇒ Object



4
5
6
# File 'lib/dapp/dapp/ruby2go.rb', line 4

def ruby2go_image(args_hash)
  _ruby2go("image", args_hash)
end

#ruby2go_initObject



36
37
38
39
40
# File 'lib/dapp/dapp/ruby2go.rb', line 36

def ruby2go_init
  @_call_after_before_terminate << proc {
    FileUtils.rmtree(@_ruby2go_tmp_dir) if @_ruby2go_tmp_dir
  }
end

#ruby2go_slug(args_hash) ⇒ Object



32
33
34
# File 'lib/dapp/dapp/ruby2go.rb', line 32

def ruby2go_slug(args_hash)
  _ruby2go("slug", args_hash)
end