class Indocker::Repositories::Cloner

Public Class Methods

new(configuration, logger) click to toggle source
# File lib/indocker/repositories/cloner.rb, line 4
def initialize(configuration, logger)
  @configuration = configuration
  @logger = logger
end

Public Instance Methods

clone(session, repository) click to toggle source
# File lib/indocker/repositories/cloner.rb, line 9
def clone(session, repository)
  raise ArgumenError.new("only git repositories should be cloned") if !repository.is_git?

  already_cloned = repository_already_cloned?(
    session:     session,
    target_path: repository.clone_path,
    remote_url:  repository.remote_url,
  )

  git_command = if already_cloned
    build_force_pull_command(
      target_path: repository.clone_path,
      branch_name: repository.branch,
    )
  else
    build_clone_command(
      target_path: repository.clone_path,
      branch_name: repository.branch,
      remote_url:  repository.remote_url,
    )
  end

  session.exec!("ssh-agent bash -c 'ssh-add ~/.ssh/#{repository.ssh_key}; #{git_command}'")
end

Private Instance Methods

build_clone_command(target_path:, branch_name:, remote_url:) click to toggle source
# File lib/indocker/repositories/cloner.rb, line 56
def build_clone_command(target_path:, branch_name:, remote_url:)
  [
    "rm -rf #{target_path}",
    "mkdir -p #{target_path}",
    "git clone -b #{branch_name} --depth 1 #{remote_url} #{target_path}",
  ].join(" && ")
end
build_force_pull_command(target_path:, branch_name:) click to toggle source
# File lib/indocker/repositories/cloner.rb, line 64
def build_force_pull_command(target_path:, branch_name:)
  [
    "cd #{target_path}",
    "git add .",
    "git reset HEAD --hard",
    "git checkout #{branch_name}",
    "git pull --force",
  ].join(" && ")
end
build_git_remote_url_command(path:) click to toggle source
# File lib/indocker/repositories/cloner.rb, line 48
def build_git_remote_url_command(path:)
  [
    "mkdir -p #{path}",
    "cd #{path}",
    "git config --get remote.origin.url",
  ].join(" && ")
end
repository_already_cloned?(session:, target_path:, remote_url:) click to toggle source
# File lib/indocker/repositories/cloner.rb, line 36
def repository_already_cloned?(session:, target_path:, remote_url:)
  target_remote_url = session.exec!(
    build_git_remote_url_command(
      path: target_path
    )
  ).stdout_data.chomp

  @logger.debug("target remote_url:  #{target_remote_url.inspect}, checked remote_url: #{remote_url.inspect}")

  target_remote_url == remote_url
end