Class: Jets::Code::Stager

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Util::Git, Util::Logging, Util::Sh
Defined in:
lib/jets/code/stager.rb

Direct Known Subclasses

Dummy

Constant Summary collapse

@@rsync_installed =

only check once

nil

Instance Method Summary collapse

Methods included from Util::Sh

#capture, #quiet_sh, #sh

Methods included from Util::Logging

#log

Methods included from Util::Git

#git?, #git_installed?

Instance Method Details

#auto_strategyObject



42
43
44
45
46
47
48
49
50
# File 'lib/jets/code/stager.rb', line 42

def auto_strategy
  if File.exist?("#{Jets.root}/.git") && rsync_installed?
    Copy::Rsync
  elsif git? # .git folder exists and git command available
    Copy::GitCopy
  else # full copy
    Copy::Full # FileUtils.cp_r(Jets.root, "#{build_root}/stage/code")
  end
end

#buildObject



14
15
16
17
18
19
20
21
22
# File 'lib/jets/code/stager.rb', line 14

def build
  clean
  warn_large_codebase
  stage_code # interface method
  save_deploy_user
  save_project_name
  save_ruby_version
  touch_deployed_at
end

#cleanObject



98
99
100
101
102
# File 'lib/jets/code/stager.rb', line 98

def clean
  FileUtils.rm_rf("#{build_root}/stage/code")
  FileUtils.rm_rf("#{build_root}/stage/code-temp")
  FileUtils.mkdir_p(File.dirname("#{build_root}/stage/code"))
end

#copy_strategyObject



31
32
33
34
35
36
37
38
39
# File 'lib/jets/code/stager.rb', line 31

def copy_strategy
  strategy = config.code.copy.strategy.to_s
  if strategy == "auto"
    auto_strategy
  else # whatever user overrides it to
    class_name = strategy.camelize
    Copy.const_get(class_name) # Copy::GitInline
  end
end

#move_ruby_version(filename = ".ruby-version") ⇒ Object



85
86
87
88
89
90
# File 'lib/jets/code/stager.rb', line 85

def move_ruby_version(filename = ".ruby-version")
  if File.exist?(filename)
    FileUtils.mkdir_p(".jets")
    FileUtils.mv(filename, ".jets/#{filename}")
  end
end

#rsync_installed?Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/jets/code/stager.rb', line 53

def rsync_installed?
  return @@rsync_installed unless @@rsync_installed.nil?
  @@rsync_installed = system "type rsync > /dev/null 2>&1"
end

#save_deploy_userObject



58
59
60
# File 'lib/jets/code/stager.rb', line 58

def save_deploy_user
  User.new.save
end

#save_project_nameObject

Save the project_name in .jets/info.yml if inferred Better to write the file here instead of within the Jets::Core::Config::Info class. Because Info assumes Jets.root is Dir.pwd We need to ensure it’s written to stage/code not the current project root.



66
67
68
69
70
71
72
73
74
# File 'lib/jets/code/stager.rb', line 66

def save_project_name
  return unless Jets.project.name_inferred?

  jets_info = Jets::Core::Config::Info.instance
  data = jets_info.data.merge(project_name: Jets.project.name)
  dest = "#{build_root}/stage/code/#{jets_info.path}"
  FileUtils.mkdir_p(File.dirname(dest))
  IO.write(dest, data.to_h.to_yaml)
end

#save_ruby_versionObject

Save by moving .ruby-version to .jets/ruby-version So it does not affect codebuild ruby version.



78
79
80
81
82
83
# File 'lib/jets/code/stager.rb', line 78

def save_ruby_version
  Dir.chdir("#{build_root}/stage/code") do
    move_ruby_version(".ruby-version")
    move_ruby_version(".tool-versions") # future proofing for asdf
  end
end

#stage_codeObject

interface method: overridden by Dummy



25
26
27
28
29
# File 'lib/jets/code/stager.rb', line 25

def stage_code
  log.debug "Copying project to #{build_root}/stage/code"
  log.debug "Copy strategy: #{copy_strategy}"
  copy_strategy.run
end

#touch_deployed_atObject



92
93
94
95
96
# File 'lib/jets/code/stager.rb', line 92

def touch_deployed_at
  dest = "#{build_root}/stage/code/.jets/deployed_at"
  FileUtils.mkdir_p(File.dirname(dest))
  IO.write(dest, Time.now.utc.to_s)
end

#warn_large_codebaseObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/jets/code/stager.rb', line 104

def warn_large_codebase
  return unless Jets.bootstrap.code.copy.warn_large
  return unless system("type du > /dev/null 2>&1")
  bytes = `du -bs #{Jets.root}`.split("\t").first.to_i
  if bytes > 1024 * 1024 * 1024 # 1GB
    size = ActiveSupport::NumberHelper.number_to_human_size(bytes)
    log.info <<~EOL
      WARNING: Large codebase detected: #{size}
      Will take some time to zip and upload. Please be patient.
      You can turn off this warning with

      config/jets/bootstrap.rb

          Jets.bootstrap.configure do
            config.code.copy.warn_large = false
          end
    EOL
  end
end