Module: Dapp::Dapp::Ruby2Go

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

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Instance Method Details

#_download_ruby2go_bin(progname, bin_path) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/dapp/dapp/ruby2go.rb', line 104

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/dapp/ruby2go/#{::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/dapp/ruby2go/#{::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, tmp_dir: nil) ⇒ Object



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
95
96
97
98
# File 'lib/dapp/dapp/ruby2go.rb', line 57

def _ruby2go(progname, args_hash, tmp_dir: nil)
  tmp_dir = _ruby2go_tmp_dir if tmp_dir.nil?

  call_id = SecureRandom.uuid

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

  res_file = File.join(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

  env_hash = ENV.map {|k, v| [k, v]}.to_h

  begin
    exec(env_hash, "#{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::Dapp::Ruby2Go::Error, code: :ruby2go_command_unexpected_exitstatus, data: { progname: progname, status_code: status_code }
  end
end

#_ruby2go_bin_path_env_var_name(progname) ⇒ Object



53
54
55
# File 'lib/dapp/dapp/ruby2go.rb', line 53

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

#_ruby2go_tmp_dirObject



100
101
102
# File 'lib/dapp/dapp/ruby2go.rb', line 100

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

#ruby2go_builder(args_hash) ⇒ Object



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

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

#ruby2go_cleanup(args_hash) ⇒ Object



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

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

#ruby2go_dappdeps(args_hash) ⇒ Object



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

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

#ruby2go_deploy(args_hash) ⇒ Object



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

def ruby2go_deploy(args_hash)
  _ruby2go("deploy", args_hash)
end

#ruby2go_deploy_watcher(args_hash, **kwargs) ⇒ Object



39
40
41
# File 'lib/dapp/dapp/ruby2go.rb', line 39

def ruby2go_deploy_watcher(args_hash, **kwargs)
  _ruby2go("deploy-watcher", args_hash, **kwargs)
end

#ruby2go_docker_registry(args_hash) ⇒ Object



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

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

#ruby2go_git_artifact(args_hash) ⇒ Object



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

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

#ruby2go_git_repo(args_hash) ⇒ Object



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

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

#ruby2go_image(args_hash) ⇒ Object



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

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

#ruby2go_initObject



47
48
49
50
51
# File 'lib/dapp/dapp/ruby2go.rb', line 47

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

#ruby2go_slug(args_hash) ⇒ Object



35
36
37
# File 'lib/dapp/dapp/ruby2go.rb', line 35

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