Class: DreamOps::SoloDeployer

Inherits:
BaseDeployer show all
Defined in:
lib/dream-ops/deployment/solo.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



6
7
8
# File 'lib/dream-ops/deployment/solo.rb', line 6

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

#__cookbook_was_updated(target, cookbooks) ⇒ Object



10
11
12
# File 'lib/dream-ops/deployment/solo.rb', line 10

def __cookbook_was_updated(target, cookbooks)
  return cookbooks.any? {|c| c[:targets].include? target }
end

#__wait_for_pid(pid) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dream-ops/deployment/solo.rb', line 14

def __wait_for_pid(pid)
  while true do
    begin
      Timeout.timeout(5) do
        Process.wait pid
      end
      print "\b"
      return $?.exitstatus == 0
    rescue Timeout::Error
      print "\r#{@@spinner.next}"
    end
  end
end

#analyze(targets) ⇒ Hash

Analyze the SSH hosts 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.tar.gz",
          :sha_filename => "chef-app-dev_SHA.txt",
          :name => "chef-app",
          :path => "./chef",
          :local_sha => "7bfa19491170563f422a321c144800f4435323b1",
          :targets => [ "[email protected]" ]
        }
      ],
      deploy_targets: [
        {
          :host => "[email protected]"
          :remote_sha => ""
        }
      ]
    }
    


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
80
81
82
83
84
# File 'lib/dream-ops/deployment/solo.rb', line 54

def analyze(targets)
  @ssh_opts = "-i #{DreamOps.ssh_key} -o LogLevel=ERROR -o StrictHostKeyChecking=no"
  @q_all = "> /dev/null 2>&1"
  @q_stdout = "> /dev/null"

  # Collect and print target info
  result = { cookbooks: [], deploy_targets: [] }
  targets.each do |target|
    target_result = analyze_target(target)
    # Add the target to the deploy targets
    result[:deploy_targets] << target_result[:deploy_target]
    # Determine whether the cookbook needs to be built
    cbook = target_result[:cookbook]
    if !cbook.nil? && !cbook[:local_sha].nil?
      # We only build the cookbook if we don't have this version remotely
      if target_result[:deploy_target][:remote_sha] != cbook[:local_sha]
        # Don't build the same destination cookbook more than once
        if !__cookbook_in_array(cbook, result[:cookbooks])
          cbook[:targets] = [target]
          result[:cookbooks] << cbook
        else
          # Add target to be deployed to
          cbook_index = result[:cookbooks].index { |c| c[:name] == cbook[:name] }
          result[:cookbooks][cbook_index][:targets] << target
        end
      end
    end
  end

  return result
end

#analyze_target(target) ⇒ Object



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

def analyze_target(target)
  # Validate SSH creds
  if DreamOps.ssh_key.empty?
    __bail_with_fatal_error(NoSshKeyError.new())
  end
  hostname = `ssh #{@ssh_opts} #{target} sudo hostname 2>&1`.chomp
  if ! $?.success?
    __bail_with_fatal_error(InvalidSshKeyError.new(target, hostname))
  end

  DreamOps.ui.info "Target: #{hostname}"

  result = { host: target }
  cookbook = { }
  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[:sha_filename] = "#{.name}_SHA.txt"
    cookbook[:cookbook_filename] = "#{.name}.tar.gz"
    cookbook[:name] = .name
    cookbook[:path] = path
    cookbook[:local_sha] = `git log --pretty=%H -1 #{cookbook[:path]}`.chomp

    `ssh #{@ssh_opts} #{target} sudo mkdir -p /var/chef/cookbooks`

    if system("ssh #{@ssh_opts} #{target} stat /var/chef/#{cookbook[:sha_filename]} #{@q_all}")
      result[:remote_sha] = `ssh #{@ssh_opts} #{target} cat /var/chef/#{cookbook[:sha_filename]}`.chomp
    else
      result[:remote_sha] = ""
    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

  suffix = if cookbook[:local_sha] != result[:remote_sha] then "(outdated)" else "(up to date)" end
  DreamOps.ui.info "--- Cookbook: #{cookbook[:name]} #{suffix}"

  return { deploy_target: result, cookbook: cookbook }
end

#deploy_cookbook(cookbook) ⇒ Object

Deploys cookbook to server



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/dream-ops/deployment/solo.rb', line 138

def deploy_cookbook(cookbook)
  cookbook[:targets].each do |target|
    if system("scp #{@ssh_opts} #{cookbook[:cookbook_filename]} #{target}:/tmp #{@q_all}")
      `ssh #{@ssh_opts} #{target} sudo rm -rf /var/chef/cookbooks/* #{@q_all}`
      `ssh #{@ssh_opts} #{target} sudo tar -xzvf /tmp/#{cookbook[:cookbook_filename]} -C /var/chef/cookbooks #{@q_all}`
      `ssh #{@ssh_opts} #{target} sudo rm -rf /tmp/#{cookbook[:cookbook_filename]} #{@q_all}`
    else
      DreamOps.ui.error "Failed to copy cookbook to host '#{target}'"
    end

    if !system(
      "ssh #{@ssh_opts} #{target} "+
      "\"echo '#{cookbook[:local_sha]}' | sudo tee /var/chef/#{cookbook[:sha_filename]}\" #{@q_all}"
    )
      DreamOps.ui.error "Failed to update remote cookbook sha"
    end
  end
end

#deploy_target(target, cookbooks) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/dream-ops/deployment/solo.rb', line 185

def deploy_target(target, cookbooks)
  # Bail if chef-solo is not installed
  if !system("ssh #{@ssh_opts} #{target[:host]} which chef-solo #{@q_all}")
    __bail_with_fatal_error(ChefSoloNotInstalledError.new(target[:host]))
  end

  # Bail if chef.json doesn't exist
  if !system("ssh #{@ssh_opts} #{target[:host]} stat /var/chef/chef.json #{@q_all}")
    __bail_with_fatal_error(ChefJsonNotFoundError.new(target[:host]))
  end

  # Bail if setup.json doesn't exist
  if !system("ssh #{@ssh_opts} #{target[:host]} stat /var/chef/roles/setup.json #{@q_all}")
    __bail_with_fatal_error(RoleNotFoundError.new(target[:host], "setup"))
  end

  # Bail if deploy.json doesn't exist
  if !system("ssh #{@ssh_opts} #{target[:host]} stat /var/chef/roles/deploy.json #{@q_all}")
    __bail_with_fatal_error(RoleNotFoundError.new(target[:host], "deploy"))
  end

  # If this stack has a new cookbook, re-run the setup role
  if (
    __cookbook_was_updated(target[:host], cookbooks) ||
    DreamOps.force_setup
  )
    DreamOps.ui.info "...Running setup role [target=\"#{target[:host]}\"]"
    run_chef_role(target, "setup")
  end

  DreamOps.ui.info "...Running deploy role [target=\"#{target[:host]}\"]"
  run_chef_role(target, "deploy")
end

#run_chef_role(target, role) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/dream-ops/deployment/solo.rb', line 157

def run_chef_role(target, role)
  if !system("ssh #{@ssh_opts} #{target[:host]} stat /var/log/chef #{@q_all}")
    `ssh #{@ssh_opts} #{target[:host]} sudo mkdir -p /var/log/chef`
  end

  uuid = SecureRandom.uuid

  chef_cmd = "cinc-solo"
  if !system("ssh #{@ssh_opts} #{target[:host]} which cinc-solo #{@q_all}")
    chef_cmd = "chef-solo --chef-license accept"
  end

  pid = fork do
    if ! system(
      "ssh #{@ssh_opts} #{target[:host]} \"" +
        "set -o pipefail && " +
        "sudo #{chef_cmd} -j /var/chef/chef.json -o \"role[#{role}]\" 2>&1 | sudo tee /var/log/chef/#{uuid}.log #{@q_all}\""
    )
      exit 1
    end
    exit 0
  end

  if !__wait_for_pid(pid)
    __bail_with_fatal_error(ChefSoloFailedError.new(target[:host], "/var/log/chef/#{uuid}.log"))
  end
end