class Docman::Application

Attributes

commit_count[RW]
config[R]
deploy_target[RW]
docroot_config[R]
force[RW]
options[RW]

Public Class Methods

bin() click to toggle source
# File lib/application.rb, line 258
def self.bin
  File.join root, 'bin'
end
lib() click to toggle source
# File lib/application.rb, line 262
def self.lib
  File.join root, 'lib'
end
new() click to toggle source
# File lib/application.rb, line 51
def initialize
  # TODO: Define workspace properly
  @workspace_dir = Dir.pwd
  @config = Docman::Config.new(File.join(Pathname(__FILE__).dirname.parent, 'config', 'config.yaml'))
  @force = false
  @commit_count = 0
end
root() click to toggle source
# File lib/application.rb, line 250
def self.root
  Pathname(__FILE__).dirname.parent
end

Public Instance Methods

build(deploy_target_name, state, options = false) click to toggle source
# File lib/application.rb, line 84
def build(deploy_target_name, state, options = false)
  with_rescue do
    @options = options
    @docroot_config = DocrootConfig.new(@workspace_dir, deploy_target_name, options, state)
    @deploy_target = @docroot_config.deploy_target
    execute('build', state, nil, options['tag'])
  end
end
config_dirs(options) click to toggle source
# File lib/application.rb, line 229
def config_dirs(options)
  config_dirs = []
  if options.key? :config_dir
    config_dirs_cli = options[:config_dir].split(',')
    config_dirs_cli.each { |dir|
      config_dirs.push(File.join(dir, '.unipipe'))
      config_dirs.push(File.join(dir, '.drupipe'))
      config_dirs.push('')
    }
  else
    config_dirs.push('.unipipe')
    config_dirs.push('.drupipe')
    config_dirs.push('')
  end
  config_dirs
end
deploy(deploy_target_name, name, type, version, options = false) click to toggle source
# File lib/application.rb, line 93
def deploy(deploy_target_name, name, type, version, options = false)
  result = nil
  with_rescue do
    @options = options
    @docroot_config = DocrootConfig.new(@workspace_dir, deploy_target_name, options)
    @deploy_target = @docroot_config.deploy_target
    @docroot_config.states_dependin_on(name, version).keys.each do |state|
      execute('deploy', state, name)
      write_environment(@deploy_target['states'][state], name)
      write_state state
      result = state
    end
  end
  result
end
drush(env, site, command) click to toggle source
# File lib/application.rb, line 122
def drush(env, site, command)
  with_rescue(false) do
    cmd = "drush env: '#{env}', site: '#{site}', '#{command}'"
    log cmd
    path = Dir.pwd
    branch = 'commands'
    current_branch = GitUtil.branch
    GitUtil.exec("fetch")
    have_branch = Exec.do("git ls-remote --exit-code . origin/#{branch} &> /dev/null")
    log have_branch
    if have_branch
      GitUtil.exec("checkout #{branch}")
      GitUtil.exec("pull origin #{branch}")
    else
      GitUtil.exec("checkout --orphan #{branch}")
      GitUtil.exec("rm --cached -r .", false)
      GitUtil.exec("clean -f -d", false)
    end
    File.open(File.join(path, 'commands'), 'a') {|f| f.puts cmd}
    GitUtil.exec("add commands")
    GitUtil.exec("commit -m 'Added command'")
    GitUtil.exec("push origin #{branch}")
    GitUtil.exec("checkout #{current_branch}")
  end
end
environment(name) click to toggle source
# File lib/application.rb, line 254
def environment(name)
  @config['environments'][name]
end
execute(action, state, name = nil, tag = nil) click to toggle source
# File lib/application.rb, line 218
def execute(action, state, name = nil, tag = nil)
  params = Marshal.load(Marshal.dump(@deploy_target))
  params['state'] = state
  params['action'] = action
  params['name'] = name
  params['tag'] = tag ? tag : state + '-' + Time.now.strftime("%Y-%m-%d-%H-%M-%S")
  params['environment'] = @config['environments'][@deploy_target['states'][state]]
  params['environment_name'] = @deploy_target['states'][state]
  Docman::Deployers::Deployer.create(params, nil, self).perform
end
force?() click to toggle source
# File lib/application.rb, line 246
def force?
  @force or @options[:force]
end
info(command, file, options = false) click to toggle source
# File lib/application.rb, line 148
def info(command, file, options = false)
  result = {}
  @docroot_config = DocrootConfig.new(@workspace_dir, nil, options)
  if (command == 'full')
    result['states'] = Docman::Application.instance.config['deploy_targets']['git_target']['states']
    result['environments'] = Docman::Application.instance.config['environments']

    projects = {}
    info = @docroot_config.structure
    @docroot_config.chain(info).values.each do |item|
      projects.merge! info_recursive(item, command)
    end
    result['projects'] = projects
  else
    info = @docroot_config.structure
    @docroot_config.chain(info).values.each do |item|
      result.merge! info_recursive(item, command)
    end
  end
  File.open(file, 'w') {|f| f.write result.to_json}
  result
end
info_recursive(info, command) click to toggle source
# File lib/application.rb, line 171
def info_recursive(info, command)
  result = {}
  case command
    when 'list'
      result[info['name']] = info['repo'] if info.key?('repo')
    when 'full'
      info_clone = info.clone
      info_clone.delete('docroot_config')
      info_clone.delete('children')
      info_clone.delete('parent')
      info_clone.delete('root')
      result[info['name']] = info_clone
  end
  info['children'].each do |child|
    result.merge! info_recursive(child, command)
  end
  result
end
init(name, repo, options) click to toggle source
# File lib/application.rb, line 59
def init(name, repo, options)
  branch = options['branch'] ? options['branch'] : 'master'
  `mkdir #{name}`
  Dir.chdir name
  GitUtil.clone_repo(repo, 'config', 'branch', branch, true, 1)
  #Dir.chdir File.join(name, 'config')
  #`git checkout #{branch} & git branch -u origin #{branch}`
end
template(name, options = false) click to toggle source
# File lib/application.rb, line 109
def template(name, options = false)
  with_rescue(false) do
    @options = options
    @docroot_config = DocrootConfig.new(@workspace_dir, nil)
    project = @docroot_config.project(name)
    unless project['template'].nil?
      Dir.chdir project['full_build_path']
      Exec.do "#{Application::bin}/project-template.sh #{project['template']}"
      log "Project had been initialized with template: #{project['template']}"
    end
  end
end
with_rescue(write_to_file = true) { || ... } click to toggle source
# File lib/application.rb, line 68
def with_rescue(write_to_file = true)
  failed_filepath = File.join(@workspace_dir, 'failed')
  if File.file?(failed_filepath)
    puts 'Last operation failed, forced rebuild mode'
    FileUtils.rm_f failed_filepath
    @force = true
  end
  yield
rescue Exception => e
  puts "Operation failed: #{e.message}", 'error'
  if write_to_file
    File.open(failed_filepath, 'w') {|f| f.write(e.message) }
  end
  raise e
end
write_environment(env, name) click to toggle source
# File lib/application.rb, line 195
def write_environment(env, name)
  environment = environment(env)

  properties = {}
  properties['ENV'] = env
  unless environment.nil?
    unless environment['previous'].nil?
      unless environment['previous'][name].nil?
        properties['project_last_result'] = environment['previous'][name]['result'] unless environment['previous'][name]['result'].nil?
        unless environment['previous'][name]['context'].nil?
          properties['temp_path'] = environment['previous'][name]['context']['temp_path'] unless environment['previous'][name]['context']['temp_path'].nil?
        end
      end
    end
  end

  properties['last_project'] = name
  filepath = File.join(@workspace_dir, 'last_deploy.properties')
  File.open(filepath, 'w') do |file|
    properties.each {|key, value| file.puts "#{key}=#{value}\n" }
  end
end
write_state(state) click to toggle source
# File lib/application.rb, line 190
def write_state state
  filepath = File.join(@workspace_dir, 'state')
  File.open(filepath, 'w') { |file| file.write(state) }
end