class Dapp::Dapp

Constants

GCR_REGISTRIES

Attributes

options[R]

Public Class Methods

docker_credentials() click to toggle source
# File lib/dapp/dapp.rb, line 228
def docker_credentials
  if options[:registry_username] && options[:registry_password]
    [options[:registry_username], options[:registry_password]]
  elsif ENV.key?('DAPP_DOCKER_CONFIG')
  elsif !gcr_registry? && !ENV.key?('DAPP_IGNORE_CI_DOCKER_AUTOLOGIN') && ENV.key?('CI_REGISTRY') && ENV.key?('CI_JOB_TOKEN')
    ['gitlab-ci-token', ENV['CI_JOB_TOKEN']]
  end
end
gcr_registry?() click to toggle source
# File lib/dapp/dapp.rb, line 237
def gcr_registry?
  return false if options[:repo].nil?
  GCR_REGISTRIES.any? { |registry| options[:repo].to_s.start_with?(registry) }
end
home_dir() click to toggle source
# File lib/dapp/dapp.rb, line 182
def home_dir
  ENV["DAPP_HOME"] || File.join(Dir.home, ".dapp")
end
host_docker() click to toggle source
# File lib/dapp/dapp.rb, line 190
def host_docker
  @host_docker ||= begin
    min_docker_minor_version = Gem::Version.new('1.10')
    unless host_docker_minor_version > min_docker_minor_version
      raise Error::Dapp, code: :docker_version, data: { min_version: min_docker_minor_version.to_s,
                                                        version:     host_docker_minor_version.to_s }
    end

    [].tap do |cmd|
      cmd << host_docker_bin
      cmd << "--config #{host_docker_config_dir}"
    end.join(' ')
  end
end
host_docker_bin() click to toggle source
# File lib/dapp/dapp.rb, line 205
def host_docker_bin
  raise Error::Dapp, code: :docker_not_found if (res = shellout('which docker')).exitstatus.nonzero?
  res.stdout.strip
end
host_docker_config_dir() click to toggle source
# File lib/dapp/dapp.rb, line 214
def host_docker_config_dir
  if options_with_docker_credentials? && !options[:repo].nil?
    host_docker_tmp_config_dir
  elsif ENV.key?('DAPP_DOCKER_CONFIG')
    ENV['DAPP_DOCKER_CONFIG']
  else
    File.join(Dir.home, '.docker')
  end
end
host_docker_minor_version() click to toggle source
# File lib/dapp/dapp.rb, line 210
def host_docker_minor_version
  Gem::Version.new(shellout!("#{host_docker_bin} --version").stdout.strip[/\d+\.\d+/])
end
host_docker_tmp_config_dir() click to toggle source
# File lib/dapp/dapp.rb, line 242
def host_docker_tmp_config_dir
  @host_docker_tmp_config_dir ||= Dir.mktmpdir('dapp-', tmp_base_dir)
end
new(options: {}) click to toggle source
# File lib/dapp/dapp.rb, line 50
def initialize(options: {})
  self.class.options.merge!(options)
  Logging::I18n.initialize
  validate_config_options!
  Logging::Paint.initialize(option_color)

  @_call_before_terminate = []
  @_call_after_before_terminate = []

  ruby2go_init

  setup_ssh_agent
  try_host_docker_login unless !!options[:ignore_try_host_docker_login]
end
options() click to toggle source
# File lib/dapp/dapp.rb, line 186
def options
  @options ||= {}
end
options_with_docker_credentials?() click to toggle source
# File lib/dapp/dapp.rb, line 224
def options_with_docker_credentials?
  !docker_credentials.nil?
end
tmp_base_dir() click to toggle source
# File lib/dapp/dapp.rb, line 246
def tmp_base_dir
  File.expand_path(options[:tmp_dir_prefix] || '/tmp')
end

Public Instance Methods

build_dir() click to toggle source
# File lib/dapp/dapp.rb, line 131
def build_dir
  @build_dir ||= begin
    if option_build_dir
      Pathname.new(option_build_dir)
    else
      dir = File.join(self.class.home_dir, "builds", self.name)
      Pathname.new(dir)
    end.expand_path.tap(&:mkpath)
  end
end
build_path(*path) click to toggle source
# File lib/dapp/dapp.rb, line 142
def build_path(*path)
  make_path(build_dir, *path)
end
get_ruby2go_state_hash() click to toggle source
# File lib/dapp/dapp.rb, line 43
def get_ruby2go_state_hash
  {
    "Name" => name.to_s,
    "WorkDir" => work_dir.to_s,
  }
end
git_own_repo() click to toggle source
# File lib/dapp/dapp.rb, line 111
def git_own_repo
  @git_own_repo ||= Dimg::GitRepo::Own.new(self)
rescue Dimg::Error::Rugged => e
  raise unless e.net_status[:code] == :local_git_repository_does_not_exist
  nil
end
git_own_repo_exist?() click to toggle source
# File lib/dapp/dapp.rb, line 107
def git_own_repo_exist?
  !git_own_repo.nil?
end
git_url() click to toggle source
# File lib/dapp/dapp.rb, line 102
def git_url
  return unless git_own_repo_exist?
  git_own_repo.remote_origin_url
end
host_docker() click to toggle source
# File lib/dapp/dapp.rb, line 160
def host_docker
  self.class.host_docker
end
host_docker_login(repo) click to toggle source
# File lib/dapp/dapp.rb, line 174
def host_docker_login(repo)
  return unless self.class.options_with_docker_credentials?

  username, password = self.class.docker_credentials
  ::Dapp::Dimg::Image::Stage.ruby2go_command(self, command: :login, options: { username: username, password: password, repository: repo })
end
host_docker_tmp_config_dir() click to toggle source
# File lib/dapp/dapp.rb, line 164
def host_docker_tmp_config_dir
  self.class.host_docker_tmp_config_dir
end
name() click to toggle source
# File lib/dapp/dapp.rb, line 81
def name
  @name ||= begin
    n = begin
      if (name = options[:name])
        name
      elsif git_own_repo_exist?
        if git_url
          repo_name = git_url.split('/').last
          repo_name = repo_name[/.*(?=\.git)/] if repo_name.end_with? '.git'
          repo_name
        else
          File.basename(File.dirname(git_own_repo.path)).to_s
        end
      else
        path.basename.to_s
      end
    end
    consistent_uniq_slugify(n)
  end
end
path(*path) click to toggle source
# File lib/dapp/dapp.rb, line 122
def path(*path)
  @path ||= make_path(work_dir)
  make_path(@path, *path)
end
settings() click to toggle source
# File lib/dapp/dapp.rb, line 69
def settings
  @settings ||= begin
    settings_path = File.join(self.class.home_dir, "settings.toml")

    if File.exists? settings_path
      TomlRB.load_file(settings_path)
    else
      {}
    end
  end
end
stage_cache() click to toggle source
# File lib/dapp/dapp.rb, line 146
def stage_cache
  "dimgstage-#{name}"
end
stage_dapp_label() click to toggle source
# File lib/dapp/dapp.rb, line 150
def stage_dapp_label
  name
end
terminate() click to toggle source
# File lib/dapp/dapp.rb, line 154
def terminate
  @_call_before_terminate.each {|on_terminate| on_terminate.call(self)}
  @_call_after_before_terminate.each {|on_terminate| on_terminate.call(self)}
  FileUtils.rmtree(host_docker_tmp_config_dir)
end
tmp_base_dir() click to toggle source
# File lib/dapp/dapp.rb, line 127
def tmp_base_dir
  self.class.tmp_base_dir
end
try_host_docker_login() click to toggle source
# File lib/dapp/dapp.rb, line 168
def try_host_docker_login
  return unless option_repo
  validate_repo_name!(option_repo)
  host_docker_login(option_repo)
end
work_dir() click to toggle source
# File lib/dapp/dapp.rb, line 118
def work_dir
  File.expand_path(options[:dir] || Dir.pwd)
end