Class: Deployer

Inherits:
Object
  • Object
show all
Defined in:
app/deployer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(push) ⇒ Deployer

Returns a new instance of Deployer.



30
31
32
# File 'app/deployer.rb', line 30

def initialize(push)
  @push = push
end

Instance Attribute Details

#pushObject

Returns the value of attribute push.



28
29
30
# File 'app/deployer.rb', line 28

def push
  @push
end

Class Method Details

.deploy(push) ⇒ Object



34
35
36
37
# File 'app/deployer.rb', line 34

def self.deploy(push)
  push.deploy
  DeployerTask.async(:deploy, :id=>push.id)
end

.undeploy(push, async = true) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'app/deployer.rb', line 39

def self.undeploy(push, async = true)
  puts "Undeploying #{push.repo_name}/#{push.branch}"
  push.undeploy
  if async
    DeployerTask.async(:undeploy, :id=>push.id)
  else
    Deployer.new(push).undeploy
  end
end

.undeploy_branch(branch) ⇒ Object



49
50
51
52
53
# File 'app/deployer.rb', line 49

def self.undeploy_branch(branch)
  Deployment.all(:branch=>branch).each do |d|
    Deployer.undeploy(d.push, false)
  end
end

Instance Method Details

#deployObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/deployer.rb', line 61

def deploy
  begin
    clone_repo
    freeze_gems
    write_descriptor
    push.deployed
    push.save
    d = Deployment.create(
      :created_at=>Time.now, 
      :push=>push,
      :branch=>push.branch,
      :path=>deployment_path,
      :context=>"/#{repository_name}")
    d.save!
  rescue Exception => ex
    puts ex
    push.failed
  end
end

#deployment_fileObject



94
95
96
# File 'app/deployer.rb', line 94

def deployment_file
  "#{repository_name}-knob.yml"
end

#deployment_pathObject



86
87
88
# File 'app/deployer.rb', line 86

def deployment_path
  @deployment_path ||= ENV['DEPLOYMENTS']
end

#git_urlObject

Hack to avoid problems with github responses using git gem over https



82
83
84
# File 'app/deployer.rb', line 82

def git_url
  push.repo_url.sub('https://github.com/', 'git://github.com/')
end

#repository_nameObject



90
91
92
# File 'app/deployer.rb', line 90

def repository_name
  @repo_name ||= push.master? ? push.repo_name : "#{push.repo_name}-#{push.branch}"
end

#rootObject



98
99
100
# File 'app/deployer.rb', line 98

def root
  "#{deployment_path}/#{repository_name}"
end

#undeployObject



55
56
57
58
59
# File 'app/deployer.rb', line 55

def undeploy
  TorqueBox::DeployUtils.undeploy( deployment_file )
  remove_repo if ( File.exist?( root ) )
  push.undeployed
end

#write_descriptorObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/deployer.rb', line 102

def write_descriptor
  repo   = push.find_repository
  if repo
    config = repo.config.nil? ? {} : YAML.load(repo.config)
    config['application'] ||= {}
    config['application']['root'] = root
    config['application']['env'] ||= 'production'
    descriptor = TorqueBox::DeployUtils.basic_deployment_descriptor(config)
    descriptor.merge! config
    TorqueBox::DeployUtils.deploy_yaml( descriptor, deployment_file )
  else
    puts "Unable to find repository #{push.repo_name}/#{push.branch}"
  end
end