<commit_before>#
# Cookbook Name:: habitat-build
# Recipe:: publish
#
# Copyright:: Copyright (c) 2016 Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

hab_pkgident = node['habitat-build']['hab-pkgident']
hab_studio_pkgident = node['habitat-build']['hab-studio-pkgident']

project_secrets = get_project_secrets
depot_token = project_secrets['habitat']['depot_token']
keyname = project_secrets['habitat']['keyname']

# e.g., `sample-verify-syntax`
studio_slug = [
  node['delivery']['change']['project'],
  node['delivery']['change']['stage'],
  node['delivery']['change']['phase']
].join('-')

# e.g., `/hab/studios/sample-verify-syntax`
studio_path = ::File.join('/hab/studios', studio_slug)

# set local variables we're going to use in `lazy` properties later in
# the chef run
artifact = nil
artifact_hash = nil
artifact_pkgident = nil
last_build_env = nil

ruby_block 'build-plan' do
  block do
    Dir.chdir(node['delivery']['workspace']['repo'])
    ENV['TERM'] = 'ansi'
    command = "sudo #{::File.join('/hab/pkgs', hab_studio_pkgident, 'bin/hab-studio')}"
    command << " -r /hab/studios/#{studio_slug}"
    # the `-k` option gets cranky unless you drop the revision timestamp from the keyname
    command << " -k #{keyname.split('-')[0...-1].join('-')}"
    command << " build #{habitat_plan_dir}"

    build = shell_out(command)

    if build.exitstatus > 0
      puts build.stdout
      puts build.stderr
      raise 'The plan.sh did NOT come together, bailing out!'
    end

    last_build_env = Hash[*::File.read(::File.join('/hab/studios',
                                                   studio_slug,
                                                   'src/results/last_build.env')).split(/[=\n]/)]

    artifact = last_build_env['pkg_artifact']
    artifact_pkgident = last_build_env['pkg_ident']
  end
end

ruby_block 'artifact-hash' do
  block do
    command = "/hab/pkgs/#{hab_pkgident}/bin/hab"
    command << " artifact hash #{::File.join(studio_path, '/src/results', artifact)}"
    artifact_hash = shell_out(command).stdout.chomp
  end
end

execute 'upload-artifact' do
  command lazy { "#{::File.join('/hab/pkgs', hab_pkgident, 'bin/hab')} artifact upload --url #{node['habitat-build']['depot-url']} --auth '#{depot_token}' #{::File.join(studio_path, '/src/results', artifact)}" }
end

# update a data bag with the artifact build info
# TODO: (jtimberman) This is not the first time this has been used in
# a delivery build cookbook. It's probably time to create a Chef
# resource for this functionality
ruby_block 'track-artifact-data' do # ~FC014
  block do
    load_delivery_chef_config
    proj = Chef::DataBag.new
    proj.name(project_slug)<commit_msg>The `hab` artifact subcommand was renamed to `pkg`<commit_after>#
# Cookbook Name:: habitat-build
# Recipe:: publish
#
# Copyright:: Copyright (c) 2016 Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

hab_pkgident = node['habitat-build']['hab-pkgident']
hab_studio_pkgident = node['habitat-build']['hab-studio-pkgident']

project_secrets = get_project_secrets
depot_token = project_secrets['habitat']['depot_token']
keyname = project_secrets['habitat']['keyname']

# e.g., `sample-verify-syntax`
studio_slug = [
  node['delivery']['change']['project'],
  node['delivery']['change']['stage'],
  node['delivery']['change']['phase']
].join('-')

# e.g., `/hab/studios/sample-verify-syntax`
studio_path = ::File.join('/hab/studios', studio_slug)

# set local variables we're going to use in `lazy` properties later in
# the chef run
artifact = nil
artifact_hash = nil
artifact_pkgident = nil
last_build_env = nil

ruby_block 'build-plan' do
  block do
    Dir.chdir(node['delivery']['workspace']['repo'])
    ENV['TERM'] = 'ansi'
    command = "sudo #{::File.join('/hab/pkgs', hab_studio_pkgident, 'bin/hab-studio')}"
    command << " -r /hab/studios/#{studio_slug}"
    # the `-k` option gets cranky unless you drop the revision timestamp from the keyname
    command << " -k #{keyname.split('-')[0...-1].join('-')}"
    command << " build #{habitat_plan_dir}"

    build = shell_out(command)

    if build.exitstatus > 0
      puts build.stdout
      puts build.stderr
      raise 'The plan.sh did NOT come together, bailing out!'
    end

    last_build_env = Hash[*::File.read(::File.join('/hab/studios',
                                                   studio_slug,
                                                   'src/results/last_build.env')).split(/[=\n]/)]

    artifact = last_build_env['pkg_artifact']
    artifact_pkgident = last_build_env['pkg_ident']
  end
end

ruby_block 'generate-pkg-hash' do
  block do
    command = "/hab/pkgs/#{hab_pkgident}/bin/hab"
    command << " pkg hash #{::File.join(studio_path, '/src/results', artifact)}"
    artifact_hash = shell_out(command).stdout.chomp
  end
end

execute 'upload-pkg' do
  command lazy { "#{::File.join('/hab/pkgs', hab_pkgident, 'bin/hab')} pkg upload --url #{node['habitat-build']['depot-url']} --auth '#{depot_token}' #{::File.join(studio_path, '/src/results', artifact)}" }
end

# update a data bag with the artifact build info
# TODO: (jtimberman) This is not the first time this has been used in
# a delivery build cookbook. It's probably time to create a Chef
# resource for this functionality
ruby_block 'track-artifact-data' do # ~FC014
  block do
    load_delivery_chef_config
    proj = Chef::DataBag.new
    proj.name(project_slug)
{'fraction_non_alphanumeric': 0.14099416999079473, 'fraction_numerical': 0.00490948143602332, 'mean_word_length': 3.7549234135667398, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2803561', 'n_tokens_mistral': 2092, 'n_tokens_neox': 2021, 'n_words': 582}