Class: HeadChef::Sync

Inherits:
Object
  • Object
show all
Defined in:
lib/head_chef/tasks/sync.rb

Class Method Summary collapse

Class Method Details

.sync(environment, force) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/head_chef/tasks/sync.rb', line 3

def self.sync(environment, force)
  # Check if environment exits, if not create it
  # Perform first, if it fails no need to continue
  unless HeadChef.chef_server.environment.find(environment)
    HeadChef.chef_server.environment.create(name: environment)
  end

  # Diff now performs all Berkshelf/lockfile dependency operations
  HeadChef.ui.say("Determing side effects of sync with chef environment "\
                  "#{environment}...", :cyan)
  cookbook_diff = HeadChef.ui.mute { Diff.diff(environment) }

  unless force
    if cookbook_diff.conflicts?
      HeadChef.ui.error 'The following cookbooks are in conflict:'
      cookbook_diff.conflicts.each do |cookbook|
        HeadChef.ui.error "#{cookbook.name}: #{cookbook.berkshelf_version}"
      end
      HeadChef.ui.error 'Use --force to sync environment'
      Kernel.exit(1337)
    end
  end

  # Retrieve berksfile
  berksfile = HeadChef.berksfile

  HeadChef.ui.say('Uploading cookbooks to chef server...', :cyan)
  berksfile.upload({force: force})

  # Apply without lock options argument
  HeadChef.ui.say("Applying Berksfile.lock cookbook version to " \
                  "environment #{environment}...", :cyan)
  berksfile.apply(environment, {})
end