class Chef::Knife::PdSync

Attributes

altered_cookbooks[R]

Public Instance Methods

check_commit() click to toggle source
# File lib/chef/knife/pd_sync.rb, line 124
def check_commit
  if origin_commit.nil?
    ui.confirm('failed to determine the origin/master. sync anyway?')
  elsif local_branch == 'master' && local_commit != origin_commit
    ui.confirm('local master branch is different than origin, sync anyway?')
  end
end
chef_server() click to toggle source
# File lib/chef/knife/pd_sync.rb, line 140
def chef_server
  URI(Chef::Config[:chef_server_url]).host
end
converge_by(msg) { || ... } click to toggle source
# File lib/chef/knife/pd_sync.rb, line 164
def converge_by(msg)
  if config[:why_run]
    ui.info('Will '+msg)
  else
    yield if block_given?
  end
end
local_branch() click to toggle source
# File lib/chef/knife/pd_sync.rb, line 136
def local_branch
  %x(git symbolic-ref --short HEAD).strip! || 'unknown'
end
local_commit() click to toggle source
# File lib/chef/knife/pd_sync.rb, line 156
def local_commit
  @local_commit ||= %x(git rev-parse master).strip! || 'unknown'
end
localhost() click to toggle source
# File lib/chef/knife/pd_sync.rb, line 160
def localhost
  @localhost ||= Socket.gethostname
end
origin_commit() click to toggle source
# File lib/chef/knife/pd_sync.rb, line 144
def origin_commit
  @origin_commit||= begin
    Timeout::timeout(5) do
      commit = Mixlib::ShellOut.new("git ls-remote origin master | awk '{ print $1 }'")
      commit.run_command
      commit.exitstatus == 0 ? commit.stdout.strip : nil
    end
  rescue Timeout::Error
    nil
  end
end
preflight_checks() click to toggle source
# File lib/chef/knife/pd_sync.rb, line 113
def preflight_checks
  vdir = File.join(Dir.pwd, 'vendor')
  if vendor_dir != vdir
    ui.confirm("vendor directory (#{vendor_dir}) is different than standard one(#{vdir}), continue?")
  end
  if local_branch != 'master'
    ui.confirm("You are deploying a non-master branch(#{local_branch}), continue?")
  end
  check_commit
end
run() click to toggle source
# File lib/chef/knife/pd_sync.rb, line 73
def run
  @altered_cookbooks = nil
  if config[:restore]
    ui.warn 'pd sync will delete and reupload all cookbooks!'
    plugin = Chef::Knife::CookbookBulkDelete.new
    plugin.name_args = Array('.')
    plugin.config[:yes] = true
    plugin.config[:purge] = true
    converge_by "delete all existing cookbooks" do
      plugin.run
    end
  end
  lockfile = '/tmp/restore_chef.lock'
  user = Chef::Config[:node_name] || 'unknown'
  converge_by 'perform pre-syn checks' do
    preflight_checks
  end
  lock = PagerDuty::ChefServer::SyncLock.new(
      lockfile, chef_server, localhost, user, local_branch
    )
  converge_by 'acquire lock' do
    lock.lock
  end
  sync = PagerDuty::ChefServer::Sync.new(
    vendor_dir: vendor_dir,
    why_run: config[:why_run]
    )
  begin
    @altered_cookbooks = sync.run
    update_commit
  rescue StandardError => e
    ui.warn(e.message)
    ui.warn(e.backtrace)
  ensure
    converge_by 'release lock' do
      lock.unlock
    end
  end
end
update_commit() click to toggle source
# File lib/chef/knife/pd_sync.rb, line 172
def update_commit
  ui.info("updating commit from #{origin_commit} => #{local_commit}")
  file = Tempfile.new(['restorechef', '.json'])

  unless Chef::DataBag.list.keys.include?('metadata')
    plugin = Chef::Knife::DataBagCreate.new
    plugin.name_args = Array('metadata')
    converge_by 'create data bag metadata' do
      plugin.run
    end
  end
  begin
    file.write(JSON.dump({ id: 'commit', commit: local_commit }))
    file.flush
    dbag = Chef::Knife::DataBagFromFile.new
    dbag.name_args = ['metadata', file.path]
    converge_by 'update commit' do
      dbag.run
    end
  ensure
    file.close
    file.unlink
  end
end
vendor_dir() click to toggle source
# File lib/chef/knife/pd_sync.rb, line 132
def vendor_dir
  Chef::Config[:cookbook_path].first
end