Class: Nucleus::Adapters::GitDeployer

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/nucleus/core/file_handling/git_deployer.rb

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #log, logger_for

Constructor Details

#initialize(repo_name, repo_url, user_email, repo_branch = 'master') ⇒ GitDeployer

Initialize a new instance of the GitDeployer

Parameters:

  • user_email (String)

    email address of the user, used as author of commits

  • repo_url (String)

    address where the repository can be retrieved

  • repo_name (String)

    name of the directory for the repository that shall be created in the tmp dir

  • repo_branch (String) (defaults to: 'master')

    branch to push to



11
12
13
14
15
16
# File 'lib/nucleus/core/file_handling/git_deployer.rb', line 11

def initialize(repo_name, repo_url, user_email, repo_branch = 'master')
  @repo_name = repo_name
  @repo_url = repo_url
  @repo_branch = repo_branch
  @user_email = user_email
end

Instance Method Details

#deploy(file, file_compression_format) ⇒ void

This method returns an undefined value.

Deploys the contents of the archive file to the repository that resides at the repo_url.

Parameters:

  • file (File)

    archive file whose contents shall be deployed to the repository

  • file_compression_format (String)

    compression format of the application archive, e.g. ‘zip’ or ‘tar.gz’



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/nucleus/core/file_handling/git_deployer.rb', line 38

def deploy(file, file_compression_format)
  extractor = Nucleus::ArchiveExtractor.new
  fail Errors::AdapterRequestError,
       'Unsupported format of the application archive' unless extractor.supports? file_compression_format

  push_repository_changes do |repo_dir|
    # now remove all current files, except the git db
    Find.find(repo_dir) do |f|
      next if f.start_with?("#{repo_dir}/.git") || f == repo_dir
      FileUtils.rm_rf(f) if File.directory?(f)
      FileUtils.rm_f(f) if File.file?(f)
    end

    # uncompress and extract to
    extracted = extractor.extract(file, repo_dir, file_compression_format)
    fail Errors::AdapterRequestError, 'Invalid application: Archive did not contain any files' if extracted == 0

    # if the application was wrapped within a directory, move all 1st level files and dirs to the root
    sanitizer = Nucleus::ApplicationRepoSanitizer.new
    sanitizer.sanitize(repo_dir)
  end
  nil
end

#download(format, exclude_git = true) ⇒ StringIO

Download the contents of a git repository in the requested format.

Parameters:

  • format (String)

    compression to be used for the download e.g. ‘zip’ or ‘tar.gz’

Returns:

  • (StringIO)

    data requested to be downloaded



65
66
67
68
69
70
71
# File 'lib/nucleus/core/file_handling/git_deployer.rb', line 65

def download(format, exclude_git = true)
  with_repository do |repo_dir|
    # TODO: maybe we can do this directly via SSH and prevent the disk writes?
    # download files temporarily from the repo
    Nucleus::Archiver.new(exclude_git).compress(repo_dir, format)
  end
end

#trigger_buildvoid

This method returns an undefined value.

Force a build using the latest git commit. To enforce the new build, a file ‘nucleus-rebuild-trigger’ gets created or updated in the repository and the changes will be pushed.



22
23
24
25
26
27
28
29
30
31
# File 'lib/nucleus/core/file_handling/git_deployer.rb', line 22

def trigger_build
  push_repository_changes do |repo_dir|
    # add a custom file that always changes the data and triggers a new build
    build_trigger_file = File.join(repo_dir, 'nucleus-rebuild-trigger')
    current_md5 = File.exist?(build_trigger_file) ? Digest::MD5.file(build_trigger_file).hexdigest : nil
    data = StringIO.new("Nucleus rebuild, triggered at #{Time.now}")
    FileManager.save_file_from_data(build_trigger_file, data, false, current_md5)
  end
  nil
end