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 collapse

GCR_REGISTRIES =
%w(asia.gcr.io eu.gcr.io gcr.io marketplace.gcr.io staging-k8s.gcr.io us.gcr.io)

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

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_cleanup, #ruby2go_dappdeps, #ruby2go_deploy, #ruby2go_deploy_watcher, #ruby2go_docker_registry, #ruby2go_git_artifact, #ruby2go_git_repo, #ruby2go_image, #ruby2go_init, #ruby2go_slug

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_slugify

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.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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
   unless !!options[:ignore_try_host_docker_login]
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.docker_credentialsObject



228
229
230
231
232
233
234
235
# 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?Boolean

Returns:

  • (Boolean)


237
238
239
240
# 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_dirObject



182
183
184
# File 'lib/dapp/dapp.rb', line 182

def home_dir
  ENV["DAPP_HOME"] || File.join(Dir.home, ".dapp")
end

.host_dockerObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# 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_binObject

Raises:



205
206
207
208
# 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_dirObject



214
215
216
217
218
219
220
221
222
# 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_versionObject



210
211
212
# 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_dirObject



242
243
244
# 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

.optionsObject



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

def options
  @options ||= {}
end

.options_with_docker_credentials?Boolean

Returns:

  • (Boolean)


224
225
226
# File 'lib/dapp/dapp.rb', line 224

def options_with_docker_credentials?
  !docker_credentials.nil?
end

.tmp_base_dirObject



246
247
248
# File 'lib/dapp/dapp.rb', line 246

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

Instance Method Details

#build_dirObject



131
132
133
134
135
136
137
138
139
140
# 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) ⇒ Object



142
143
144
# File 'lib/dapp/dapp.rb', line 142

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

#get_ruby2go_state_hashObject



43
44
45
46
47
48
# File 'lib/dapp/dapp.rb', line 43

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

#git_own_repoObject



111
112
113
114
115
116
# 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?Boolean

Returns:

  • (Boolean)


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

def git_own_repo_exist?
  !git_own_repo.nil?
end

#git_urlObject



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

def git_url
  return unless git_own_repo_exist?
  git_own_repo.remote_origin_url
end

#host_dockerObject



160
161
162
# File 'lib/dapp/dapp.rb', line 160

def host_docker
  self.class.host_docker
end

#host_docker_login(repo) ⇒ Object



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

def (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_dirObject



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

def host_docker_tmp_config_dir
  self.class.host_docker_tmp_config_dir
end

#nameObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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) ⇒ Object



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

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

#settingsObject



69
70
71
72
73
74
75
76
77
78
79
# 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_cacheObject



146
147
148
# File 'lib/dapp/dapp.rb', line 146

def stage_cache
  "dimgstage-#{name}"
end

#stage_dapp_labelObject



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

def stage_dapp_label
  name
end

#terminateObject



154
155
156
157
158
# 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_dirObject



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

def tmp_base_dir
  self.class.tmp_base_dir
end

#try_host_docker_loginObject



168
169
170
171
172
# File 'lib/dapp/dapp.rb', line 168

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

#work_dirObject



118
119
120
# File 'lib/dapp/dapp.rb', line 118

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