<commit_before>require 'chef/handler'
require 'mixlib/shellout'

class OmnibusHandler < Chef::Handler

  def report
    Chef::Log.info("Rolling back to previous delivery-cli")
    cmd = "rsync -aP /opt/delivery-cli-safe/ /opt/delivery-cli"
    so = Mixlib::ShellOut.new(cmd)
    so.run_command
    if so.error?
      Chef::Log.error("ROLLBACK FAILED")
      Chef::Log.error(so.stdout)
    end
  end
end

Chef::Config.exception_handlers << OmnibusHandler.new()
Chef::Config.report_handlers << OmnibusHandler.new()

git_ssh = File.join('/var/opt/delivery/workspace/bin', 'git_ssh')
omnibus_path = File.join(node['delivery']['workspace']['repo'], 'omnibus-delivery-cli')

## Make sure it builds!
execute "bundle install --binstubs=#{omnibus_path}/bin --path=#{File.join(node['delivery_builder']['cache'], 'gems')}" do
  cwd omnibus_path
end

execute "#{omnibus_path}/bin/omnibus build delivery-cli" do
  cwd omnibus_path
end

## Push it to Github
git_ssh = File.join(node['delivery']['workspace']['cache'], 'git_ssh')
deploy_key = File.join(node['delivery']['workspace']['cache'], 'github.pem')
secrets = get_project_secrets

file deploy_key do
  content secrets['github']
  owner 'dbuild'
  mode '0600'
  sensitive true
end

template git_ssh do
  source 'git_ssh.erb'
  owner 'dbuild'
  mode '0755'
end

execute "set_git_username" do
  command "git config user.name 'Delivery'"
  cwd node['delivery']['workspace']['repo']
  environment({"GIT_SSH" => git_ssh})
end

execute "set_git_email" do
  command "git config user.email '<EMAIL>'"
  cwd node['delivery']['workspace']['repo']
  environment({"GIT_SSH" => git_ssh})
end

execute "add_github_remote" do
  command "git remote add github <EMAIL>:chef/delivery-cli.git"
  cwd node['delivery']['workspace']['repo']
  environment({"GIT_SSH" => git_ssh})
  not_if "git remote --verbose | grep ^github"
end

execute "push_to_github" do
  command "git push github master"
  cwd node['delivery']['workspace']['repo']
  environment({"GIT_SSH" => git_ssh})
end

##############################################################

endpoint       = 'http://artifactory.chef.co/'
repository     = 'omnibus-current-local'
base_path      = 'com/getchef'
pattern        = './**/*.{deb,rpm}'
username       = 'delivery'
password       = secrets['artifactory_password']

omnibus_config         = ::File.join(node['delivery']['workspace']['cache'], 'omnibus-publish.rb')
platform_mappings_path = ::File.join(node['delivery']['workspace']['cache'], 'omnibus-platform-mappings.json')

# Build on one platform, but can install on several
file platform_mappings_path do
  content <<-EOH
{
  "ubuntu-12.04": [
    "ubuntu-12.04",
    "ubuntu-14.04"
  ],
  "el-6": [
    "el-6",
    "el-7"
  ]
}
EOH
  mode '0600'
  owner 'dbuild'
  group 'dbuild'
end

# Render an Omnibus config file for publishing
file omnibus_config do
  content <<-EOH
# This file is written by Chef for #{node['fqdn']}.
# Do NOT modify this file by hand.
artifactory_endpoint  '#{endpoint}'
artifactory_base_path '#{base_path}'
artifactory_username  '#{username}'
artifactory_password  '#{password}'
  EOH
  mode '0600'
  owner 'dbuild'
  group 'dbuild'
  sensitive true
end

# Use the CLI because RUBY INCEPTION
execute "upload to artifactory" do
  command "#{omnibus_path}/bin/omnibus publish artifactory #{repository} #{pattern} " \
          "--config #{omnibus_config} " \
          "--platform-mappings #{platform_mappings_path} " \
          "--version-manifest #{Dir.glob('./**/version-manifest.json').first}"
  cwd omnibus_path
end<commit_msg>Fix version manifest file for upload<commit_after>require 'chef/handler'
require 'mixlib/shellout'

class OmnibusHandler < Chef::Handler

  def report
    Chef::Log.info("Rolling back to previous delivery-cli")
    cmd = "rsync -aP /opt/delivery-cli-safe/ /opt/delivery-cli"
    so = Mixlib::ShellOut.new(cmd)
    so.run_command
    if so.error?
      Chef::Log.error("ROLLBACK FAILED")
      Chef::Log.error(so.stdout)
    end
  end
end

Chef::Config.exception_handlers << OmnibusHandler.new()
Chef::Config.report_handlers << OmnibusHandler.new()

git_ssh = File.join('/var/opt/delivery/workspace/bin', 'git_ssh')
omnibus_path = File.join(node['delivery']['workspace']['repo'], 'omnibus-delivery-cli')

## Make sure it builds!
execute "bundle install --binstubs=#{omnibus_path}/bin --path=#{File.join(node['delivery_builder']['cache'], 'gems')}" do
  cwd omnibus_path
end

execute "#{omnibus_path}/bin/omnibus build delivery-cli" do
  cwd omnibus_path
end

## Push it to Github
git_ssh = File.join(node['delivery']['workspace']['cache'], 'git_ssh')
deploy_key = File.join(node['delivery']['workspace']['cache'], 'github.pem')
secrets = get_project_secrets

file deploy_key do
  content secrets['github']
  owner 'dbuild'
  mode '0600'
  sensitive true
end

template git_ssh do
  source 'git_ssh.erb'
  owner 'dbuild'
  mode '0755'
end

execute "set_git_username" do
  command "git config user.name 'Delivery'"
  cwd node['delivery']['workspace']['repo']
  environment({"GIT_SSH" => git_ssh})
end

execute "set_git_email" do
  command "git config user.email '<EMAIL>'"
  cwd node['delivery']['workspace']['repo']
  environment({"GIT_SSH" => git_ssh})
end

execute "add_github_remote" do
  command "git remote add github <EMAIL>:chef/delivery-cli.git"
  cwd node['delivery']['workspace']['repo']
  environment({"GIT_SSH" => git_ssh})
  not_if "git remote --verbose | grep ^github"
end

execute "push_to_github" do
  command "git push github master"
  cwd node['delivery']['workspace']['repo']
  environment({"GIT_SSH" => git_ssh})
end

##############################################################

endpoint       = 'http://artifactory.chef.co/'
repository     = 'omnibus-current-local'
base_path      = 'com/getchef'
pattern        = './**/*.{deb,rpm}'
username       = 'delivery'
password       = secrets['artifactory_password']

omnibus_config         = ::File.join(node['delivery']['workspace']['cache'], 'omnibus-publish.rb')
platform_mappings_path = ::File.join(node['delivery']['workspace']['cache'], 'omnibus-platform-mappings.json')

# Build on one platform, but can install on several
file platform_mappings_path do
  content <<-EOH
{
  "ubuntu-12.04": [
    "ubuntu-12.04",
    "ubuntu-14.04"
  ],
  "el-6": [
    "el-6",
    "el-7"
  ]
}
EOH
  mode '0600'
  owner 'dbuild'
  group 'dbuild'
end

# Render an Omnibus config file for publishing
file omnibus_config do
  content <<-EOH
# This file is written by Chef for #{node['fqdn']}.
# Do NOT modify this file by hand.
artifactory_endpoint  '#{endpoint}'
artifactory_base_path '#{base_path}'
artifactory_username  '#{username}'
artifactory_password  <PASSWORD>}'
  EOH
  mode '0600'
  owner 'dbuild'
  group 'dbuild'
  sensitive true
end

# Use the CLI because RUBY INCEPTION
execute "upload to artifactory" do
  command "#{omnibus_path}/bin/omnibus publish artifactory #{repository} #{pattern} " \
          "--config #{omnibus_config} " \
          "--platform-mappings #{platform_mappings_path} " \
          "--version-manifest #{omnibus_path}/pkg/version-manifest.json"
  cwd omnibus_path
end
{'fraction_non_alphanumeric': 0.16896648831315123, 'fraction_numerical': 0.008729935229512813, 'mean_word_length': 4.501936483346244, 'pattern_counts': {'":': 4, '<': 26, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2546459', 'n_tokens_mistral': 2600, 'n_tokens_neox': 2457, 'n_words': 548}