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, Sentry, Shellout::Base, SshAgent, Helper::Sha256, Helper::Tar, Helper::Trivia, Helper::Url
Defined in:
lib/dapp/dapp.rb,
lib/dapp/dapp/chef.rb,
lib/dapp/dapp/lock.rb,
lib/dapp/dapp/sentry.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, Sentry, 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::Url

#get_host_from_git_url, #git_url_to_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 Sentry

#_make_sentry_params, #_sentry_extra_context, #_sentry_tags_context, #_sentry_user_context, #ensure_sentry_configured, #sentry_exception, #sentry_message

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.



30
31
32
33
34
35
# File 'lib/dapp/dapp.rb', line 30

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.



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

def options
  @options
end

Class Method Details

.home_dirObject



150
151
152
# File 'lib/dapp/dapp.rb', line 150

def home_dir
  File.join(Dir.home, ".dapp")
end

.host_dockerObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/dapp/dapp.rb', line 158

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



177
178
179
180
181
182
183
184
185
# File 'lib/dapp/dapp.rb', line 177

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_tmp_config_dirObject



191
192
193
# File 'lib/dapp/dapp.rb', line 191

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

.optionsObject



154
155
156
# File 'lib/dapp/dapp.rb', line 154

def options
  @options ||= {}
end

.options_with_docker_credentials?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/dapp/dapp.rb', line 187

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

.tmp_base_dirObject



195
196
197
# File 'lib/dapp/dapp.rb', line 195

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

Instance Method Details

#build_dirObject



93
94
95
96
97
98
99
100
101
# File 'lib/dapp/dapp.rb', line 93

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

#build_path(*path) ⇒ Object



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

def build_path(*path)
  make_path(build_dir, *path)
end

#git_configObject



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

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

#git_pathObject



78
79
80
81
82
# File 'lib/dapp/dapp.rb', line 78

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

#git_urlObject



67
68
69
70
# File 'lib/dapp/dapp.rb', line 67

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

#host_dockerObject



128
129
130
# File 'lib/dapp/dapp.rb', line 128

def host_docker
  self.class.host_docker
end

#host_docker_loginObject



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/dapp/dapp.rb', line 136

def 
  return unless option_repo

  validate_repo_name!(option_repo)

   = proc {|u, p| shellout!("#{host_docker} login -u '#{u}' -p '#{p}' '#{option_repo}'")}
  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



132
133
134
# File 'lib/dapp/dapp.rb', line 132

def host_docker_tmp_config_dir
  self.class.host_docker_tmp_config_dir
end

#local_git_artifact_exclude_paths(&blk) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/dapp/dapp.rb', line 107

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



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dapp/dapp.rb', line 53

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



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

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

#settingsObject



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

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_cacheObject



116
117
118
# File 'lib/dapp/dapp.rb', line 116

def stage_cache
  "dimgstage-#{name}"
end

#stage_dapp_labelObject



120
121
122
# File 'lib/dapp/dapp.rb', line 120

def stage_dapp_label
  name
end

#terminateObject



124
125
126
# File 'lib/dapp/dapp.rb', line 124

def terminate
  FileUtils.rmtree(host_docker_tmp_config_dir)
end

#tmp_base_dirObject



89
90
91
# File 'lib/dapp/dapp.rb', line 89

def tmp_base_dir
  self.class.tmp_base_dir
end