class OpenShift::ApplicationRepository

This class represents an Application's Git repository

Constants

GIT_CONFIG
GIT_DEPLOY

TODO: submodule support

GIT_DESCRIPTION
GIT_INIT
GIT_LOCAL_CLONE
LOAD_ENV
POST_RECEIVE

FIXME: Broker host should not be defined here, rather nuture script should look it up currently broker_host is tagged at the end of all the build scripts. Kinda like an egg race!

PRE_RECEIVE

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 36
def initialize(user)
  @user = user
  @path = File.join(@user.homedir, 'git', "#{@user.app_name}.git")
end

Public Instance Methods

configure_repository() click to toggle source

Install Git repository hooks and set permissions

# File lib/openshift-origin-node/model/application_repository.rb, line 136
def configure_repository
  UnixUser.match_ownership(@user.homedir, @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
deploy_repository() click to toggle source
# File lib/openshift-origin-node/model/application_repository.rb, line 120
def deploy_repository
  # expose variables for ERB processing
  @application_name = @user.app_name
  @user_homedir     = @user.homedir

  # FIXME: See below
  @broker_host      = OpenShift::Config.new.get('BROKER_HOST')

  Utils.oo_spawn(ERB.new(GIT_DEPLOY).result(binding),
                 chdir:               @path,
                 uid:                 @user.uid,
                 expected_exitstatus: 0)
end
exists?() click to toggle source
# File lib/openshift-origin-node/model/application_repository.rb, line 41
def exists?
  File.directory?(@path)
end
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

If the directory template exists it will be installed in the application's repository. If the directory template.git exists it will be cloned as the application's repository.

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

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

  cartridge_template     = File.join(@user.homedir, cartridge_name, 'template')
  cartridge_template_git = File.join(@user.homedir, cartridge_name, 'template.git')

  have_template = (File.exist? cartridge_template or File.exist? cartridge_template_git)
  return nil unless have_template

  # TODO: Support tar balls etc...
  raise NotImplementedError.new(
            "#{File.join(cartridge_name, 'template')}: files are not support for initializing a git repository"
        ) if File.file? cartridge_template

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

  # FIXME: See below
  @broker_host      = OpenShift::Config.new.get('BROKER_HOST')

  case
    when File.exists?(cartridge_template)
      pull_directory(cartridge_template)
    when File.exist?(cartridge_template_git)
      pull_bare_repository(cartridge_template_git)
  end

  configure_repository
end
pull_bare_repository(path) click to toggle source

Copy bare git repository to be used as application repository

# File lib/openshift-origin-node/model/application_repository.rb, line 88
def pull_bare_repository(path)
  FileUtils.cp_r(path, @path)
end
pull_directory(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 94
def pull_directory(path)
  template = File.join(@user.homedir, 'git', 'template')
  FileUtils.rm_r(template) if File.exist? template

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

  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 "loose" 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