Class: Dapp::Dapp

Inherits:
Object
  • Object
show all
Extended by:
Helper::Trivia
Includes:
Chef, DappConfig, Dappfile, Deps::Base, Deps::Gitartifact, GitArtifact, Lock, Logging::Base, Logging::I18n, Logging::Paint, Logging::Process, Shellout::Base, SshAgent, Helper::Sha256, Helper::Tar, Helper::Trivia
Defined in:
lib/dapp/dapp.rb,
lib/dapp/dapp/chef.rb,
lib/dapp/dapp/lock.rb,
lib/dapp/dapp/dappfile.rb,
lib/dapp/dapp/deps/base.rb,
lib/dapp/dapp/ssh_agent.rb,
lib/dapp/dapp/dapp_config.rb,
lib/dapp/dapp/git_artifact.rb,
lib/dapp/dapp/logging/base.rb,
lib/dapp/dapp/logging/i18n.rb,
lib/dapp/dapp/logging/paint.rb,
lib/dapp/dapp/shellout/base.rb,
lib/dapp/dapp/logging/process.rb,
lib/dapp/dapp/deps/gitartifact.rb,
lib/dapp/dapp/shellout/streaming.rb

Defined Under Namespace

Modules: Chef, DappConfig, Dappfile, Deps, GitArtifact, Lock, Logging, Shellout, SshAgent

Constant Summary

Constants included from Deps::Base

Deps::Base::BASE_VERSION

Constants included from Deps::Gitartifact

Deps::Gitartifact::GITARTIFACT_VERSION

Constants included from Logging::Paint

Logging::Paint::FORMAT

Constants included from Logging::Process

Logging::Process::DEFAULT_STYLE, Logging::Process::DEFAULT_TERMINAL_WIDTH

Constants included from DappConfig

DappConfig::SUPPORTED_CONFIG_OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper::Trivia

class_to_lowercase, class_to_lowercase, delete_file, kwargs, make_path, search_file_upward

Methods included from Shellout::Base

default_env_keys, included, #shellout, #shellout!, #shellout_pack

Methods included from Deps::Base

#base_container, #base_container_name, #sudo_command, #sudo_format_user

Methods included from Deps::Gitartifact

#git_bin, #gitartifact_container, #gitartifact_container_name

Methods included from Helper::Tar

#tar_gz_read, #tar_read, #tar_write

Methods included from Helper::Sha256

#hashsum, #paths_content_hashsum, #sha256

Methods included from SshAgent

#add_ssh_key, included, #run_ssh_agent, #setup_ssh_agent, #ssh_auth_sock

Methods included from Logging::Paint

included, initialize, #paint_string, #paint_style, #unpaint

Methods included from Logging::I18n

initialize, #t

Methods included from Logging::Process

#log_process, #log_secondary_process, #log_state

Methods included from Logging::Base

#dev_mode?, #dry_run?, #ignore_config_warning?, included, #introspect_before_error?, #introspect_error?, #log, #log_config_warning, #log_dimg_name_with_indent, #log_format_string, #log_indent, #log_indent_next, #log_indent_prev, #log_info, #log_quiet?, #log_secondary, #log_step, #log_step_with_indent, #log_time, #log_time?, #log_verbose?, #log_warning, #with_log_indent

Methods included from DappConfig

#config_options, #option_color, #option_dev, #validate_config_options!

Methods included from Chef

#builder_cookbook_path

Methods included from Dappfile

#config, #dappfile_exists?, #dappfile_path, #expand_path, #work_dir

Methods included from Lock

#_lock, #lock, #lock_path

Constructor Details

#initialize(options: {}) ⇒ Dapp

Returns a new instance of Dapp.



27
28
29
30
31
32
# File 'lib/dapp/dapp.rb', line 27

def initialize(options: {})
  self.class.options.merge!(options)
  Logging::I18n.initialize
  validate_config_options!
  Logging::Paint.initialize(option_color)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.host_dockerObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/dapp/dapp.rb', line 134

def host_docker
  @host_docker ||= begin
    raise Error::Dapp, code: :docker_not_found if (res = shellout('which docker')).exitstatus.nonzero?
    docker_bin = res.stdout.strip

    current_docker_version = shellout!("#{docker_bin} --version").stdout.strip
    required_docker_version = '1.10.0'

    if Gem::Version.new(required_docker_version) >= Gem::Version.new(current_docker_version[/(\d+\.)+\d+/])
      raise Error::Dapp, code: :docker_version, data: { version: required_docker_version }
    end

    [].tap do |cmd|
      cmd << docker_bin
      cmd << "--config #{host_docker_config_dir}"
    end.join(' ')
  end
end

.host_docker_config_dirObject



153
154
155
156
157
158
159
160
161
# File 'lib/dapp/dapp.rb', line 153

def host_docker_config_dir
  if options_with_docker_credentials?
    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_tmp_config_dirObject



167
168
169
# File 'lib/dapp/dapp.rb', line 167

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

.optionsObject



130
131
132
# File 'lib/dapp/dapp.rb', line 130

def options
  @options ||= {}
end

.options_with_docker_credentials?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/dapp/dapp.rb', line 163

def options_with_docker_credentials?
  (options.key?(:registry_username) && options.key?(:registry_password)) || ENV.key?('CI_JOB_TOKEN')
end

.tmp_base_dirObject



171
172
173
# File 'lib/dapp/dapp.rb', line 171

def tmp_base_dir
  File.expand_path(options[:tmp_dir_prefix] || '/tmp')
end

Instance Method Details

#build_path(*path) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/dapp/dapp.rb', line 78

def build_path(*path)
  @build_path ||= begin
    if option_build_dir
      Pathname.new(option_build_dir)
    else
      path('.dapp_build')
    end.expand_path.tap(&:mkpath)
  end
  make_path(@build_path, *path)
end

#git_configObject



57
58
59
60
61
# File 'lib/dapp/dapp.rb', line 57

def git_config
  @git_config ||= begin
    IniFile.load(File.join(git_path, 'config')) if git_path
  end
end

#git_pathObject



63
64
65
66
67
# File 'lib/dapp/dapp.rb', line 63

def git_path
  defined?(@git_path) ? @git_path : begin
    @git_path = search_file_upward('.git')
  end
end

#git_urlObject



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

def git_url
  return unless git_config
  (git_config['remote "origin"'] || {})['url']
end

#host_dockerObject



110
111
112
# File 'lib/dapp/dapp.rb', line 110

def host_docker
  self.class.host_docker
end

#host_docker_loginObject



118
119
120
121
122
123
124
125
126
127
# File 'lib/dapp/dapp.rb', line 118

def 
  return unless option_repo

   = proc {|u, p| shellout!("#{host_docker} login \"#{option_repo}\" -u \"#{u}\" -p \"#{p}\"")}
  if options.key?(:registry_username) && options.key?(:registry_password)
    .call(options[:registry_username], options[:registry_password])
  elsif ENV.key?('CI_JOB_TOKEN')
    .call('gitlab-ci-token', ENV['CI_JOB_TOKEN'])
  end
end

#host_docker_tmp_config_dirObject



114
115
116
# File 'lib/dapp/dapp.rb', line 114

def host_docker_tmp_config_dir
  self.class.host_docker_tmp_config_dir
end

#local_git_artifact_exclude_paths(&blk) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/dapp/dapp.rb', line 89

def local_git_artifact_exclude_paths(&blk)
  super do |exclude_paths|
    build_path_relpath = Pathname.new(build_path).subpath_of(File.dirname(git_path))
    exclude_paths << build_path_relpath.to_s if build_path_relpath

    yield exclude_paths if block_given?
  end
end

#nameObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dapp/dapp.rb', line 38

def name
  @name ||= begin
    if git_url
      repo_name = git_url.split('/').last
      repo_name = repo_name[/.*(?=\.git)/] if repo_name.end_with? '.git'
      repo_name
    elsif git_path
      File.basename(File.dirname(git_path)).to_s
    else
      path.basename.to_s
    end
  end
end

#path(*path) ⇒ Object



69
70
71
72
# File 'lib/dapp/dapp.rb', line 69

def path(*path)
  @path ||= expand_path(dappfile_path)
  make_path(@path, *path)
end

#stage_cacheObject



98
99
100
# File 'lib/dapp/dapp.rb', line 98

def stage_cache
  "dimgstage-#{name}"
end

#stage_dapp_labelObject



102
103
104
# File 'lib/dapp/dapp.rb', line 102

def stage_dapp_label
  name
end

#terminateObject



106
107
108
# File 'lib/dapp/dapp.rb', line 106

def terminate
  FileUtils.rmtree(host_docker_tmp_config_dir)
end

#tmp_base_dirObject



74
75
76
# File 'lib/dapp/dapp.rb', line 74

def tmp_base_dir
  self.class.tmp_base_dir
end