<commit_before>#
# Cookbook Name:: delivery-cluster
# Recipe:: setup
#
# Author:: <NAME> (<<EMAIL>>)
#
# Copyright 2015, Chef Software, Inc.
#
# All rights reserved - Do Not Redistribute
#

require 'chef/provisioning/aws_driver'

with_driver 'aws'

with_machine_options(
  bootstrap_options: {
    instance_type: node['delivery-cluster']['aws']['flavor'],
    key_name: node['delivery-cluster']['aws']['key_name'],
    security_group_ids: node['delivery-cluster']['aws']['security_group_ids']
  },
  ssh_username: node['delivery-cluster']['aws']['ssh_username'],
  image_id:     node['delivery-cluster']['aws']['image_id']
)

add_machine_options bootstrap_options: { subnet_id: node['delivery-cluster']['aws']['subnet_id'] } if node['delivery-cluster']['aws']['subnet_id']
add_machine_options use_private_ip_for_ssh: node['delivery-cluster']['aws']['use_private_ip_for_ssh'] if node['delivery-cluster']['aws']['use_private_ip_for_ssh']

################################################################################
# Phase 1: Bootstrap a Chef Server instance with Chef-Zero
################################################################################

# It's ugly but this must happen in the compile phase so we can switch out
# the Chef Server we are talking to for the remainder of the CCR.

# Provision the Chef Server with an empty runlist so we can extract
# it's primary ipaddress to use as the hostname in the initial
# `/etc/opscode/chef-server.rb` file
machine chef_server_hostname do
  add_machine_options bootstrap_options: { instance_type: node['delivery-cluster']['chef-server']['flavor'] } if node['delivery-cluster']['chef-server']['flavor']
  action :converge
end

# Now that we've extracted the Chef Server's ipaddress we can fully
# converge and complete the install.
machine chef_server_hostname do
  recipe "chef-server-12"
  attributes lazy { chef_server_attributes }
  action :converge
end

directory tmp_infra_dir do
  action :create
end

directory Chef::Config[:trusted_certs_dir] do
  action :create
end

# Fetch our client and validator pems from the provisioned Chef Server
machine_file "/tmp/validator.pem" do
  machine chef_server_hostname
  local_path "#{tmp_infra_dir}/validator.pem"
  action :download
end

machine_file "/tmp/delivery.pem" do
  machine chef_server_hostname
  local_path "#{tmp_infra_dir}/delivery.pem"
  action :download
end

machine_file 'chef-server-cert' do
  path lazy { "/var/opt/opscode/nginx/ca/#{chef_server_ip}.crt" }
  machine chef_server_hostname
  local_path lazy { "#{Chef::Config[:trusted_certs_dir]}/#{chef_server_ip}.crt" }
  action :download
end

################################################################################
# Phase 2: Bootstrap the rest of our infrastructure with the new Chef Server
################################################################################

# create an encrypted data bag secret
file "#{tmp_infra_dir}/encrypted_data_bag_secret" do
  mode    '0644'
  content encrypted_data_bag_secret
  sensitive true
  action :create
end

# create required builder keys
file "#{tmp_infra_dir}/builder_key.pub" do
  mode    '0644'
  content builder_public_key
  sensitive true
  action :create
end

file "#{tmp_infra_dir}/builder_key" do
  mode    '0600'
  content builder_private_key
  sensitive true
  action :create
end

# create the data bag (and item) to store our builder keys
chef_data_bag "keys" do
  chef_server lazy { chef_server_config }
  action :create
end

chef_data_bag_item "keys/delivery_builder_keys" do
  chef_server lazy { chef_server_config }
  raw_data(
    builder_key:  builder_private_key,
    delivery_pem: File.read("#{tmp_infra_dir}/delivery.pem")
  )
  secret_path "#{tmp_infra_dir}/encrypted_data_bag_secret"
  encryption_version 1
  encrypt true
  action :create
end

# generate a knife config file that points at the new Chef Server
file File.join(tmp_infra_dir, 'knife.rb') do
  content lazy {
    <<-EOH
node_name         'delivery'
chef_server_url   '#{chef_server_url}'
client_key        '#{tmp_infra_dir}/delivery.pem'
cookbook_path     '#{Chef::Config[:cookbook_path]}'
trusted_certs_dir '#{Chef::Config[:trusted_certs_dir]}'
    EOH
  }
end

execute "upload delivery cookbooks" do
  command "knife cookbook upload --all --cookbook-path #{Chef::Config[:cookbook_path]}"
  environment(
    'KNIFE_HOME' => tmp_infra_dir
  )
end

# Provision the Delivery server with an empty runlist so we can extract
# it's primary ipaddress to use as the hostname in the initial
# `/etc/opscode/delivery.rb` file
machine delivery_server_hostname do
  chef_server lazy { chef_server_config }<commit_msg>Update setup.rb<commit_after>#
# Cookbook Name:: delivery-cluster
# Recipe:: setup
#
# Author:: <NAME> (<<EMAIL>>)
#
# Copyright 2015, Chef Software, Inc.
#
# All rights reserved - Do Not Redistribute
#

require 'chef/provisioning/aws_driver'

with_driver 'aws'

with_machine_options(
  bootstrap_options: {
    instance_type: node['delivery-cluster']['aws']['flavor'],
    key_name: node['delivery-cluster']['aws']['key_name'],
    security_group_ids: node['delivery-cluster']['aws']['security_group_ids']
  },
  ssh_username: node['delivery-cluster']['aws']['ssh_username'],
  image_id:     node['delivery-cluster']['aws']['image_id']
)

add_machine_options bootstrap_options: { subnet_id: node['delivery-cluster']['aws']['subnet_id'] } if node['delivery-cluster']['aws']['subnet_id']
add_machine_options use_private_ip_for_ssh: node['delivery-cluster']['aws']['use_private_ip_for_ssh'] if node['delivery-cluster']['aws']['use_private_ip_for_ssh']

################################################################################
# Phase 1: Bootstrap a Chef Server instance with Chef-Zero
################################################################################

# It's ugly but this must happen in the compile phase so we can switch out
# the Chef Server we are talking to for the remainder of the CCR.

# Provision the Chef Server with an empty runlist so we can extract
# it's primary ipaddress to use as the hostname in the initial
# `/etc/opscode/chef-server.rb` file
machine chef_server_hostname do
  add_machine_options bootstrap_options: { instance_type: node['delivery-cluster']['chef-server']['flavor'] } if node['delivery-cluster']['chef-server']['flavor']
  action :converge
end

# Now that we've extracted the Chef Server's ipaddress we can fully
# converge and complete the install.
machine chef_server_hostname do
  recipe "chef-server-12"
  attributes lazy { chef_server_attributes }
  action :converge
end

directory tmp_infra_dir do
  action :create
end

directory Chef::Config[:trusted_certs_dir] do
  action :create
end

# Fetch our client and validator pems from the provisioned Chef Server
machine_file "/tmp/validator.pem" do
  machine chef_server_hostname
  local_path "#{tmp_infra_dir}/validator.pem"
  action :download
end

machine_file "/tmp/delivery.pem" do
  machine chef_server_hostname
  local_path "#{tmp_infra_dir}/delivery.pem"
  action :download
end

machine_file 'chef-server-cert' do
  path lazy { "/var/opt/opscode/nginx/ca/#{chef_server_ip}.crt" }
  machine chef_server_hostname
  local_path lazy { "#{Chef::Config[:trusted_certs_dir]}/#{chef_server_ip}.crt" }
  action :download
end

################################################################################
# Phase 2: Bootstrap the rest of our infrastructure with the new Chef Server
################################################################################

# create an encrypted data bag secret
file "#{tmp_infra_dir}/encrypted_data_bag_secret" do
  mode    '0644'
  content encrypted_data_bag_secret
  sensitive true
  action :create
end

# create required builder keys
file "#{tmp_infra_dir}/builder_key.pub" do
  mode    '0644'
  content builder_public_key
  sensitive true
  action :create
end

file "#{tmp_infra_dir}/builder_key" do
  mode    '0600'
  content builder_private_key
  sensitive true
  action :create
end

# create the data bag (and item) to store our builder keys
chef_data_bag "keys" do
  chef_server lazy { chef_server_config }
  action :create
end

chef_data_bag_item "keys/delivery_builder_keys" do
  chef_server lazy { chef_server_config }
  raw_data lazy {{
    builder_key:  builder_private_key,
    delivery_pem: File.read("#{tmp_infra_dir}/delivery.pem")
  }}
  secret_path "#{tmp_infra_dir}/encrypted_data_bag_secret"
  encryption_version 1
  encrypt true
  action :create
end

# generate a knife config file that points at the new Chef Server
file File.join(tmp_infra_dir, 'knife.rb') do
  content lazy {
    <<-EOH
node_name         'delivery'
chef_server_url   '#{chef_server_url}'
client_key        '#{tmp_infra_dir}/delivery.pem'
cookbook_path     '#{Chef::Config[:cookbook_path]}'
trusted_certs_dir '#{Chef::Config[:trusted_certs_dir]}'
    EOH
  }
end

execute "upload delivery cookbooks" do
  command "knife cookbook upload --all --cookbook-path #{Chef::Config[:cookbook_path]}"
  environment(
    'KNIFE_HOME' => tmp_infra_dir
  )
end

# Provision the Delivery server with an empty runlist so we can extract
# it's primary ipaddress to use as the hostname in the initial
# `/etc/opscode/delivery.rb` file
machine delivery_server_hostname do
  chef_server lazy { chef_server_config }
{'fraction_non_alphanumeric': 0.16785405872193437, 'fraction_numerical': 0.0045336787564766836, 'mean_word_length': 5.660675772825305, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4071199', 'n_tokens_mistral': 2960, 'n_tokens_neox': 2863, 'n_words': 766}