Class: DreamOps::OpsWorksDeployer

Inherits:
BaseDeployer show all
Defined in:
lib/dream-ops/deployment/opsworks.rb

Instance Method Summary collapse

Methods inherited from BaseDeployer

#__bail_with_fatal_error, #__get_cookbook_paths, #build_cookbook, #cleanup_cookbooks, #deploy, deployer_method

Instance Method Details

#__cookbook_in_array(cb, cookbooks) ⇒ Object



254
255
256
257
258
# File 'lib/dream-ops/deployment/opsworks.rb', line 254

def __cookbook_in_array(cb, cookbooks)
  return cookbooks.any? {|c|
    c[:cookbook_filename] == cb[:cookbook_filename] and c[:bucket] == cb[:bucket]
  }
end

#analyze(stack_ids) ⇒ Hash

Analyze the OpsWorks stacks for deployment

Returns:

  • (Hash)

    a hash containing cookbooks that need to be built/updated and deploy targets

    Example:

    {
      :cookbooks => [
        {
          :bucket => "chef-app",
          :cookbook_filename => "chef-app-dev.zip",
          :sha_filename => "chef-app-dev_SHA.txt",
          :name => "chef-app",
          :path => "./chef",
          :local_sha => "7bfa19491170563f422a321c144800f4435323b1",
          :remote_sha => ""
        }
      ],
      deploy_targets: [
        {
          :stack => #<Stack: name="my-stack">,
          :apps => [
            #<App: name="my-app"
          ],
          :cookbook => {
            :bucket => "chef-app",
            :cookbook_filename => "chef-app-dev.zip",
            :sha_filename => "chef-app-dev_SHA.txt",
            :name => "chef-app",
            :path => "./chef",
            :local_sha => "7bfa19491170563f422a321c144800f4435323b1",
            :remote_sha => ""
          }
        }
      ]
    }
    


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dream-ops/deployment/opsworks.rb', line 47

def analyze(stack_ids)
  begin
    @opsworks = Aws::OpsWorks::Client.new
    stacks = @opsworks.describe_stacks({ stack_ids: stack_ids, }).stacks
  rescue => e
    DreamOps.ui.error "#{$!}"
    exit(1)
  end

  # Collect and print stack info
  result = { cookbooks: [], deploy_targets: [] }
  stacks.each do |stack|
    stack_result = analyze_stack(stack)
    # Add the stack to the deploy targets
    result[:deploy_targets] << {
      stack: stack,
      apps: stack_result[:apps],
      cookbook: stack_result[:cookbook]
    }
    # Determine whether the cookbook needs to be built
    cbook = stack_result[:cookbook]
    if !cbook.nil? && !cbook[:local_sha].nil?
      # We only build the cookbook if we don't have this version on S3
      if cbook[:remote_sha] != cbook[:local_sha]
        # Don't build the same destination cookbook more than once
        if !__cookbook_in_array(cbook, result[:cookbooks])
          result[:cookbooks] << cbook
        end
      end
    end
  end
  return result
end

#analyze_stack(stack) ⇒ Object

Retrieves stack apps and gets all information about remote/local cookbook



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
# File 'lib/dream-ops/deployment/opsworks.rb', line 82

def analyze_stack(stack)
  cookbook = nil

  DreamOps.ui.info "Stack: #{stack.name}"
  if !stack.custom_cookbooks_source.nil?
    source = stack.custom_cookbooks_source

    # Skip this step if the stack doesn't use S3
    if source.type == 's3'
      cookbookPath = URI.parse(source.url).path[1..-1]
      firstSlash = cookbookPath.index('/')
      cookbook = {
          bucket: cookbookPath[0..(firstSlash-1)],
          cookbook_filename: cookbookPath[(firstSlash+1)..-1],
          sha_filename: cookbookPath[firstSlash+1..-1].sub('.zip', '_SHA.txt')
      }

      cookbooks = __get_cookbook_paths()

      # For now we only handle if we find one cookbook
      if cookbooks.length == 1
        path = cookbooks[0]
        loader = Chef::Cookbook::CookbookVersionLoader.new(path)
        loader.load_cookbooks
        cookbook_version = loader.cookbook_version
         = cookbook_version.

        cookbook[:name] = .name
        cookbook[:path] = path
        if cookbook[:cookbook_filename].include? cookbook[:name]
          cookbook[:local_sha] = `git log --pretty=%H -1 #{cookbook[:path]}`.chomp

          begin
            obj = Aws::S3::Object.new(cookbook[:bucket], cookbook[:sha_filename])
            cookbook[:remote_sha] = obj.get.body.string
          rescue Aws::S3::Errors::NoSuchKey
            cookbook[:remote_sha] = ""
          end
        else
          DreamOps.ui.warn "Stack cookbook source is '#{cookbook[:cookbook_filename]}' but found '#{cookbook[:name]}' locally"
        end
      elsif cookbooks.length > 1
        DreamOps.ui.warn "Found more than one cookbook at paths #{cookbooks}.  Skipping build."
        cookbook[:name] = "???"
      else
        DreamOps.ui.warn "No cookbook found"
        cookbook[:name] = "???"
      end
      DreamOps.ui.info "--- Cookbook: #{cookbook[:name]}"
    end
  end

  apps = @opsworks.describe_apps({ stack_id: stack.stack_id }).apps
  if apps.length == 0
    DreamOps.ui.info "--- Apps: No apps"
  else
    DreamOps.ui.info "--- Apps: #{apps.map{|app| app.name}}"
  end

  return { apps: apps, cookbook: cookbook }
end

#deploy_app(app, stack) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/dream-ops/deployment/opsworks.rb', line 212

def deploy_app(app, stack)
  DreamOps.ui.info "...Deploying [stack=\"#{stack.name}\"] [app=\"#{app.name}\"]"
  begin
    response = @opsworks.create_deployment({
      stack_id: stack.stack_id,
      app_id: app.app_id,
      command: { name: "deploy" }
    })
  rescue Aws::OpsWorks::Errors::ValidationException
    __bail_with_fatal_error(NoRunningInstancesError.new(stack))
  end

  status = wait_for_deployment(response.deployment_id)
  if status != 'successful'
    __bail_with_fatal_error(OpsWorksCommandFailedError.new(
      stack, response.deployment_id, 'deploy')
    )
  end
end

#deploy_cookbook(cookbook) ⇒ Object

Deploys cookbook to S3



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/dream-ops/deployment/opsworks.rb', line 145

def deploy_cookbook(cookbook)
  begin
    archiveFile = File.open(cookbook[:cookbook_filename])
    remoteCookbook = Aws::S3::Object.new(cookbook[:bucket], cookbook[:cookbook_filename])
    response = remoteCookbook.put({ acl: "private", body: archiveFile })
    archiveFile.close

    remoteSha = Aws::S3::Object.new(cookbook[:bucket], cookbook[:sha_filename])
    response = remoteSha.put({ acl: "private", body: cookbook[:local_sha] })
  rescue => e
    DreamOps.ui.error "#{$!}"
  end
end

#deploy_target(target, cookbooks) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/dream-ops/deployment/opsworks.rb', line 160

def deploy_target(target, cookbooks)
  # If this stack has a new cookbook
  if !target[:cookbook].nil? || DreamOps.force_setup
    if __cookbook_in_array(target[:cookbook], cookbooks) || DreamOps.force_setup
      # Grab a fresh copy of the cookbook on all instances in the stack
      update_custom_cookbooks(target[:stack])

      # Re-run the setup step for all layers
      setup(target[:stack])
    end
  end

  # Deploy all apps for stack
  target[:apps].each do |app|
    deploy_app(app, target[:stack])
  end
end

#get_deployment_status(deployment_id) ⇒ Object



232
233
234
235
236
237
238
# File 'lib/dream-ops/deployment/opsworks.rb', line 232

def get_deployment_status(deployment_id)
  response = @opsworks.describe_deployments({ deployment_ids: [deployment_id] })
  if response[:deployments].length == 1
    return response[:deployments][0].status
  end
  return "failed"
end

#setup(stack) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/dream-ops/deployment/opsworks.rb', line 197

def setup(stack)
  DreamOps.ui.info "...Running setup command [stack=\"#{stack.name}\"]"
  response = @opsworks.create_deployment({
    stack_id: stack.stack_id,
    command: { name: "setup" }
  })
  
  status = wait_for_deployment(response.deployment_id)
  if status != 'successful'
    __bail_with_fatal_error(OpsWorksCommandFailedError.new(
      stack, response.deployment_id, 'setup')
    )
  end
end

#update_custom_cookbooks(stack) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/dream-ops/deployment/opsworks.rb', line 178

def update_custom_cookbooks(stack)
  DreamOps.ui.info "...Updating custom cookbooks [stack=\"#{stack.name}\"]"
  begin
    response = @opsworks.create_deployment({
      stack_id: stack.stack_id,
      command: { name: "update_custom_cookbooks" }
    })
  rescue Aws::OpsWorks::Errors::ValidationException
    __bail_with_fatal_error(NoRunningInstancesError.new(stack))
  end

  status = wait_for_deployment(response.deployment_id)
  if status != 'successful'
    __bail_with_fatal_error(OpsWorksCommandFailedError.new(
      stack, response.deployment_id, 'update_custom_cookbooks')
    )
  end
end

#wait_for_deployment(deployment_id) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/dream-ops/deployment/opsworks.rb', line 240

def wait_for_deployment(deployment_id)
  status = "failed"
  while true
    status = get_deployment_status(deployment_id)
    if ["successful", "failed"].include? status
      print "\b"
      break
    end
    sleep(2)
    print "\r#{@@spinner.next}"
  end
  return status
end