Module: DeepThought::Deployer

Defined in:
lib/deep_thought/deployer.rb,
lib/deep_thought/deployer/shell.rb,
lib/deep_thought/deployer/deployer.rb

Defined Under Namespace

Classes: Deployer, DeployerNotFoundError, DeployerSetupFailedError, DeploymentFailedError, DeploymentInProgressError, Shell

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.adaptersObject

Returns the value of attribute adapters.



12
13
14
# File 'lib/deep_thought/deployer.rb', line 12

def adapters
  @adapters
end

Class Method Details

.create(project, user, via, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/deep_thought/deployer.rb', line 23

def self.create(project, user, via, options = {})
  branch = options[:branch] || 'master'
  actions = options[:actions] if options[:actions]
  environment = options[:environment] if options[:environment]
  box = options[:box] if options[:box]
  variables = options[:variables] if options[:variables]
  on_behalf_of = options[:on_behalf_of] if options[:on_behalf_of]

  if is_deploying?
    raise DeploymentInProgressError, "Sorry, but I'm currently in mid-deployment. Ask me again when I'm done."
  end

  project.setup

  hash = Git.get_latest_commit_for_branch(project, branch)

  project_config = YAML.load_file(".projects/#{project.name}/.deepthought.yml")

  if project_config['ci']
    uses_ci = project_config['ci']['enabled'] || false
  else
    uses_ci = false
  end

  if DeepThought::CIService.ci_service
    if uses_ci
      ci_project_name = project_config['ci']['name'] || project.name
      if !DeepThought::CIService.is_branch_green?(ci_project_name, branch, hash)
        raise DeepThought::CIService::CIBuildNotGreenError, "Commit #{hash} on project #{app} (in branch #{branch}) is not green. Fix it before deploying."
      end
    end
  end

  deploy = DeepThought::Deploy.new
  deploy.project_id = project.id
  deploy.user_id = user.id
  deploy.branch = branch
  deploy.commit = hash.to_s
  deploy.via = via
  deploy.actions = actions.to_yaml if actions
  deploy.environment = environment if environment
  deploy.box = box if environment && box
  deploy.variables = variables.to_yaml if variables
  deploy.on_behalf_of = on_behalf_of if on_behalf_of
  deploy.save!
end

.execute(deploy) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/deep_thought/deployer.rb', line 70

def self.execute(deploy)
  if !is_deploying?
    Git.switch_to_branch(deploy.project, deploy.branch)

    deploy.project.setup

    project_config = YAML.load_file(".projects/#{deploy.project.name}/.deepthought.yml") || {}
    deploy_type = project_config['deploy_type'] || 'shell'

    if @adapters.keys.include?(deploy_type)
      lock_deployer

      deploy.started_at = DateTime.now
      deploy.in_progress = true
      deploy.save!

      klass = adapters[deploy_type]
      deployer = klass.new

      if !deployer.setup?(deploy.project, project_config)
        raise DeployerSetupFailedError, "Deployer setup failed - check the deployer and project settings."
      end

      deploy_status = deployer.execute?(deploy, project_config)

      unlock_deployer

      deploy.finished_at = DateTime.now
      deploy.in_progress = false

      deploy_summary = deploy.project.name

      if deploy.actions
        actions = YAML.load(deploy.actions)
        actions.each do |action|
          deploy_summary += "/#{action}"
        end
      end

      deploy_summary += " #{deploy.branch}"

      if deploy.environment
        deploy_summary += " to #{deploy.environment}"

        if deploy.box
          deploy_summary += "/#{deploy.box}"
        end
      end

      if deploy.variables
        variables = YAML.load(deploy.variables)
        variables.each do |k, v|
          deploy_summary += " #{k}=#{v}"
        end
      end

      if deploy_status
        deploy.was_successful = true
        deploy.save!

        DeepThought::Notifier.notify(deploy.user, "SUCCESS: #{deploy_summary}")

        true
      else
        deploy.was_successful = false
        deploy.save!

        DeepThought::Notifier.notify(deploy.user, "FAILED: #{deploy_summary}")

        raise DeploymentFailedError, "The deployment pondered its own short existence before hitting the ground with a sudden wet thud."
      end
    else
      raise DeployerNotFoundError, "I don't have a deployer called \"#{deploy_type}\"."
    end
  else
    raise DeploymentInProgressError, "Sorry, but I'm currently in mid-deployment. Ask me again when I'm done."
  end
end

.is_deploying?Boolean

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
157
# File 'lib/deep_thought/deployer.rb', line 149

def self.is_deploying?
  deployer_state = get_or_create_deployer_state

  if deployer_state.state == 'true'
    true
  else
    false
  end
end

.lock_deployerObject



159
160
161
162
163
164
# File 'lib/deep_thought/deployer.rb', line 159

def self.lock_deployer
  deployer_state = get_or_create_deployer_state

  deployer_state.state = 'true'
  deployer_state.save
end

.register_adapter(name, service) ⇒ Object



19
20
21
# File 'lib/deep_thought/deployer.rb', line 19

def self.register_adapter(name, service)
  self.adapters[name] = service
end

.unlock_deployerObject



166
167
168
169
170
171
# File 'lib/deep_thought/deployer.rb', line 166

def self.unlock_deployer
  deployer_state = get_or_create_deployer_state

  deployer_state.state = 'false'
  deployer_state.save
end