Parent

OpenShift::ApplicationRepository

This class represents an Application's Git repository

Attributes

path[R]

Public Class Methods

new(user) click to toggle source

Creates a new application Git repository from a template

user is of type UnixUser

# File lib/openshift-origin-node/model/application_repository.rb, line 43
def initialize(user)
  @user = user
  @path = PathUtils.join(@user.homedir, 'git', "#{@user.app_name}.git")
end

Public Instance Methods

archive() click to toggle source
# File lib/openshift-origin-node/model/application_repository.rb, line 132
def archive
  return unless exist?

  # expose variables for ERB processing
  @application_name = @user.app_name
  @user_homedir     = @user.homedir
  @target_dir       = PathUtils.join(@user.homedir, 'app-root', 'runtime', 'repo')

  FileUtils.rm_rf Dir.glob(PathUtils.join(@target_dir, '*'))
  FileUtils.rm_rf Dir.glob(PathUtils.join(@target_dir, '.[^\.]*'))

  Utils.oo_spawn(ERB.new(GIT_ARCHIVE).result(binding),
                 chdir:               @path,
                 uid:                 @user.uid,
                 expected_exitstatus: 0)

  return unless File.exist? PathUtils.join(@target_dir, '.gitmodules')

  env = Utils::Environ.load(PathUtils.join(@user.homedir, '.env'))

  cache = PathUtils.join(env['OPENSHIFT_TMP_DIR'], 'git_cache')
  FileUtils.rm_r(cache) if File.exist?(cache)
  FileUtils.mkpath(cache)

  Utils.oo_spawn("/bin/sh #{PathUtils.join('/usr/libexec/openshift/lib', "archive_git_submodules.sh")} #{@path} #{@target_dir}",
                 chdir:               @user.homedir,
                 env:                 env,
                 uid:                 @user.uid,
                 expected_exitstatus: 0)

  Utils.oo_spawn("/bin/rm -rf #{cache} &")
end
build_bare(path) click to toggle source

Copy a file tree structure and build an application repository

# File lib/openshift-origin-node/model/application_repository.rb, line 192
def build_bare(path)
  template = File.join(@user.homedir, 'git', 'template')
  FileUtils.rm_r(template) if File.exist? template

  git_path = File.join(@user.homedir, 'git')
  Utils.oo_spawn("/bin/cp -ad #{path} #{git_path}",
                 expected_exitstatus: 0)

  Utils.oo_spawn(ERB.new(GIT_INIT).result(binding),
                 chdir:               template,
                 expected_exitstatus: 0)
  begin
    # trying to clone as the user proved to be painful as git managed to "lose" the selinux context
    Utils.oo_spawn(ERB.new(GIT_LOCAL_CLONE).result(binding),
                   chdir:               git_path,
                   expected_exitstatus: 0)
  rescue ShellExecutionException => e
    FileUtils.rm_r(@path) if File.exist? @path

    raise ShellExecutionException.new(
              'Failed to clone application git repository from template repository',
              e.rc, e.stdout, e.stderr)
  ensure
    FileUtils.rm_r(template)
  end
end
configure() click to toggle source

Install Git repository hooks and set permissions

# File lib/openshift-origin-node/model/application_repository.rb, line 171
def configure
  FileUtils.chown_R(@user.uid, @user.uid, @path)
  Utils::SELinux.set_mcs_label(Utils::SELinux.get_mcs_label(@user.uid), @path)

  # application developer cannot change git hooks
  hooks = File.join(@path, 'hooks')
  FileUtils.chown_R(0, 0, hooks)

  render_file = lambda { |f, m, t|
    File.open(f, 'w', m) { |f| f.write(ERB.new(t).result(binding)) }
  }

  render_file.call(File.join(@path, 'description'), 0644, GIT_DESCRIPTION)
  render_file.call(File.join(@user.homedir, '.gitconfig'), 0644, GIT_CONFIG)

  render_file.call(File.join(hooks, 'pre-receive'), 0755, PRE_RECEIVE)
  render_file.call(File.join(hooks, 'post-receive'), 0755, POST_RECEIVE)
end
destroy() click to toggle source
# File lib/openshift-origin-node/model/application_repository.rb, line 165
def destroy
  FileUtils.rm_r(@path) if File.exist? @path
end
exist?() click to toggle source
# File lib/openshift-origin-node/model/application_repository.rb, line 48
def exist?
  File.directory?(@path)
end
Also aliased as: exists?
exists?() click to toggle source
Alias for: exist?
populate_from_cartridge(cartridge_name) click to toggle source

populate_from_cartridge uses the provided cartridge_name to install a template application for the gear

Template search locations:

* ~/<cartridge home>/template
* ~/<cartridge home>/template.git
* ~/<cartridge home>/usr/template
* ~/<cartridge home>/usr/template.git

return nil if application bare repository exists or no template found

otherwise path of template used
# File lib/openshift-origin-node/model/application_repository.rb, line 66
def populate_from_cartridge(cartridge_name)
  return nil if exists?

  FileUtils.mkpath(File.join(@user.homedir, 'git'))

  locations = [
      File.join(@user.homedir, cartridge_name, 'template'),
      File.join(@user.homedir, cartridge_name, 'template.git'),
      File.join(@user.homedir, cartridge_name, 'usr', 'template'),
      File.join(@user.homedir, cartridge_name, 'usr', 'template.git'),
  ]

  template = locations.find {|l| File.directory?(l)}
  logger.debug("Using '#{template}' to populate git repository for #{@user.uuid}")
  return nil unless template

  # expose variables for ERB processing
  @application_name = @user.app_name
  @cartridge_name   = cartridge_name
  @user_homedir     = @user.homedir

  if template.end_with? '.git'
    FileUtils.cp_r(template, @path, preserve: true)
  else
    build_bare(template)
  end

  configure
  template
end
populate_from_url(cartridge_name, url) click to toggle source

populate_from_url uses the provided cartridge_url to install a template application for the gear

# File lib/openshift-origin-node/model/application_repository.rb, line 101
def populate_from_url(cartridge_name, url)
  return nil if exists?

  supported = SUPPORTED_PROTOCOLS.any? { |k| url.start_with?(k) }
  raise Utils::ShellExecutionException.new(
            "CLIENT_ERROR: Source Code repository URL type must be one of: #{SUPPORTED_PROTOCOLS.join(', ')}", 130
        ) unless supported

  git_path = File.join(@user.homedir, 'git')
  FileUtils.mkpath(git_path)

  # expose variables for ERB processing
  @application_name = @user.app_name
  @cartridge_name   = cartridge_name
  @user_homedir     = @user.homedir
  @url              = url

  begin
    Utils.oo_spawn(ERB.new(GIT_URL_CLONE).result(binding),
                   chdir:               git_path,
                   expected_exitstatus: 0)
  rescue Utils::ShellExecutionException => e
    raise Utils::ShellExecutionException.new(
              "CLIENT_ERROR: Source Code repository could not be cloned: '#{url}'.  Please verify the repository is correct and contact support.",
              131
          )
  end

  configure
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.