Class: Dapp::Dapp

Inherits:
Object
  • Object
show all
Extended by:
Helper::Trivia
Includes:
Chef, Command::Common, Command::Sample::Common, Command::Sample::Create, Command::Sample::List, Command::Slug, DappConfig, Dappfile, Deps::Base, Deps::Common, Deps::Gitartifact, Deps::Toolchain, GitArtifact, Lock, Logging::Base, Logging::I18n, Logging::Paint, Logging::Process, OptionTags, Ruby2Go, Sentry, Shellout::Base, Slug, 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/slug.rb,
lib/dapp/dapp/sentry.rb,
lib/dapp/dapp/ruby2go.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/deps/common.rb,
lib/dapp/dapp/option_tags.rb,
lib/dapp/dapp/command/slug.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/command/common.rb,
lib/dapp/dapp/deps/toolchain.rb,
lib/dapp/dapp/logging/process.rb,
lib/dapp/dapp/deps/gitartifact.rb,
lib/dapp/dapp/shellout/streaming.rb,
lib/dapp/dapp/command/sample/list.rb,
lib/dapp/dapp/command/sample/common.rb,
lib/dapp/dapp/command/sample/create.rb

Defined Under Namespace

Modules: Chef, Command, DappConfig, Dappfile, Deps, GitArtifact, Lock, Logging, OptionTags, Ruby2Go, Sentry, Shellout, Slug, SshAgent

Constant Summary

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 Slug

Slug::SLUG_SEPARATOR, Slug::SLUG_V2_LIMIT_LENGTH

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

check_path?, check_subpath?, class_to_lowercase, class_to_lowercase, delete_file, ignore_path?, ignore_path_base, kwargs, make_path, path_checker, search_file_upward

Methods included from Shellout::Base

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

Methods included from Deps::Base

#base_container, #dappdeps_base_path, #sudo_command

Methods included from Deps::Gitartifact

#git_bin, #gitartifact_container

Methods included from Deps::Toolchain

#toolchain_container

Methods included from Deps::Common

#dappdeps_container, #dappdeps_containers, #ruby2go_dappdeps_command

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 Ruby2Go

#_download_ruby2go_bin, #_ruby2go, #_ruby2go_bin_path_env_var_name, #_ruby2go_tmp_dir, #ruby2go_builder, #ruby2go_dappdeps, #ruby2go_docker_registry, #ruby2go_git_artifact, #ruby2go_git_repo, #ruby2go_image, #ruby2go_init

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, #default_ssh_keys, included, #run_ssh_agent, #setup_ssh_agent, #ssh_agent_exist?, #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, #service_stream, #with_log_indent

Methods included from Command::Sample::Create

#_sample_directory, #_sample_repo_blobs_entries, #_sample_slice_cwd, #sample_create, #sample_exist!, #sample_exist?

Methods included from Command::Sample::List

#sample_list

Methods included from Command::Sample::Common

#_sample_git_repo, #_sample_list, #_samples_dir, #_samples_git_repo_branch, #_samples_git_repo_url

Methods included from Command::Slug

#slug

Methods included from Command::Common

#dimg_registry, #option_repo, #shortcut_or_key, #shortcuts

Methods included from Slug

#consistent_uniq_slug_reg, #consistent_uniq_slugify, #should_be_slugged?

Methods included from OptionTags

#branch_tags, #build_tags, #ci_tags, #commit_tags, #option_tags, #plain_tags, #slug_tags, #tagging_schemes, #tags_by_scheme

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?, #download_dappfile_yml_bin, #expand_path, #load_dappfile_ruby, #load_dappfile_yml

Methods included from GitArtifact

#dimgstage_g_a_commit_label, #dimgstage_g_a_type_label

Methods included from Lock

#_lock, #lock, #locks_dir

Constructor Details

#initialize(options: {}) ⇒ Dapp

Returns a new instance of Dapp.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dapp/dapp.rb', line 48

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
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.docker_credentialsObject



239
240
241
242
243
244
245
246
# File 'lib/dapp/dapp.rb', line 239

def docker_credentials
  if options[:registry_username] && options[:registry_password]
    [options[:registry_username], options[:registry_password]]
  elsif ENV.key?('DAPP_DOCKER_CONFIG')
  elsif !ENV.key?('DAPP_IGNORE_CI_DOCKER_AUTOLOGIN') && ENV.key?('CI_JOB_TOKEN')
    ['gitlab-ci-token', ENV['CI_JOB_TOKEN']]
  end
end

.home_dirObject



193
194
195
# File 'lib/dapp/dapp.rb', line 193

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

.host_dockerObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/dapp/dapp.rb', line 201

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_binObject

Raises:



216
217
218
219
# File 'lib/dapp/dapp.rb', line 216

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_dirObject



225
226
227
228
229
230
231
232
233
# File 'lib/dapp/dapp.rb', line 225

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_versionObject



221
222
223
# File 'lib/dapp/dapp.rb', line 221

def host_docker_minor_version
  Gem::Version.new(shellout!("#{host_docker_bin} --version").stdout.strip[/\d+\.\d+/])
end

.host_docker_tmp_config_dirObject



248
249
250
# File 'lib/dapp/dapp.rb', line 248

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

.optionsObject



197
198
199
# File 'lib/dapp/dapp.rb', line 197

def options
  @options ||= {}
end

.options_with_docker_credentials?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/dapp/dapp.rb', line 235

def options_with_docker_credentials?
  !docker_credentials.nil?
end

.tmp_base_dirObject



252
253
254
# File 'lib/dapp/dapp.rb', line 252

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

Instance Method Details

#build_dirObject



129
130
131
132
133
134
135
136
137
138
# File 'lib/dapp/dapp.rb', line 129

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) ⇒ Object



140
141
142
# File 'lib/dapp/dapp.rb', line 140

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

#get_ruby2go_state_hashObject



41
42
43
44
45
46
# File 'lib/dapp/dapp.rb', line 41

def get_ruby2go_state_hash
  {
    "Name" => name.to_s,
    "WorkDir" => work_dir.to_s,
  }
end

#git_own_repoObject



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

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?Boolean

Returns:

  • (Boolean)


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

def git_own_repo_exist?
  !git_own_repo.nil?
end

#git_urlObject



97
98
99
100
101
102
103
# File 'lib/dapp/dapp.rb', line 97

def git_url
  return unless git_own_repo_exist?
  git_own_repo.remote_origin_url
rescue Dimg::Error::Rugged => e
  return if e.net_status[:code] == :git_repository_without_remote_url
  raise
end

#host_dockerObject



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

def host_docker
  self.class.host_docker
end

#host_docker_login(repo) ⇒ Object



181
182
183
184
185
186
187
188
189
190
# File 'lib/dapp/dapp.rb', line 181

def (repo)
  return unless self.class.options_with_docker_credentials?

  username, password = self.class.docker_credentials
  if ::Dapp::Dapp.host_docker_minor_version >= Gem::Version.new('17.07')
    shellout!("#{host_docker} login --username '#{username}' --password-stdin '#{repo}'", input: password)
  else
    shellout!("#{host_docker} login --username '#{username}' --password '#{password}' '#{repo}'")
  end
end

#host_docker_tmp_config_dirObject



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

def host_docker_tmp_config_dir
  self.class.host_docker_tmp_config_dir
end

#local_git_artifact_exclude_paths(&blk) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/dapp/dapp.rb', line 144

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

    yield exclude_paths if block_given?
  end
end

#nameObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dapp/dapp.rb', line 76

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) ⇒ Object



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

def path(*path)
  @path ||= make_path(work_dir)
  make_path(@path, *path)
end

#settingsObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dapp/dapp.rb', line 64

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



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

def stage_cache
  "dimgstage-#{name}"
end

#stage_dapp_labelObject



157
158
159
# File 'lib/dapp/dapp.rb', line 157

def stage_dapp_label
  name
end

#terminateObject



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

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_dirObject



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

def tmp_base_dir
  self.class.tmp_base_dir
end

#try_host_docker_loginObject



175
176
177
178
179
# File 'lib/dapp/dapp.rb', line 175

def 
  return unless option_repo
  validate_repo_name!(option_repo)
  (option_repo)
end

#work_dirObject



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

def work_dir
  File.expand_path(options[:dir] || Dir.pwd)
end