<commit_before>bash -c '

# This is the ubuntu-10.04-gems script from opscode, but it
# * installs ruby 1.9.2, not 1.8.7
# * upgrades rubygems rather than installing from source
# * pushes the node identity into the first-boot.json
# * installs the chef-client service and kicks off the first run of chef

<%= (@config[:verbosity].to_i > 1 ? 'set -v' : '') %>

mkdir -p /tmp/knife-bootstrap ; chmod 700 /tmp/knife-bootstrap 
cd /tmp/knife-bootstrap

RUBY_VERSION=1.9.2-p290

<%= "export http_proxy=\"#{knife_config[:bootstrap_proxy]}\"" if knife_config[:bootstrap_proxy] -%>
eval `cat /etc/lsb-release `
export DEBIAN_FRONTEND=noninteractive

if [ ! -f /usr/bin/chef-client ]; then
echo -e "`date` \n\n**** \n**** apt update:\n****\n"
apt-get    update
apt-get -y upgrade

echo -e "`date` \n\n**** \n**** Installing base packages:\n****\n"
apt-get install -y build-essential wget runit runit-services zlib1g-dev libssl-dev openssl libcurl4-openssl-dev libreadline6-dev libyaml-dev

echo -e "`date` \n\n**** \n**** Installing ruby version ${RUBY_VERSION}:\n****\n"

wget ftp://ftp.ruby-lang.org//pub/ruby/1.9/ruby-${RUBY_VERSION}.tar.gz
tar xzf ruby-${RUBY_VERSION}.tar.gz
cd ruby-${RUBY_VERSION}
./configure --with-ruby-version=${RUBY_VERSION} --prefix=/usr --program-suffix=${RUBY_VERSION}
make -j2
make install

sudo update-alternatives --remove-all gem && true
update-alternatives \
  --install /usr/bin/ruby ruby /usr/bin/ruby${RUBY_VERSION} 400 \
  --slave   /usr/bin/ri   ri   /usr/bin/ri${RUBY_VERSION}       \
  --slave   /usr/bin/irb  irb  /usr/bin/irb${RUBY_VERSION}      \
  --slave   /usr/bin/erb  erb  /usr/bin/erb${RUBY_VERSION}      \
  --slave   /usr/bin/gem  gem  /usr/bin/gem${RUBY_VERSION}      \
  --slave   /usr/share/man/man1/ruby.1.gz ruby.1.gz             \
  /usr/share/man/man1/ruby${RUBY_VERSION}.1

if ruby -e "exit(%x{gem --version} < \"1.6.2\" ? 0 : -1 )" ; then
  echo -e "`date` \n\n**** \n**** Updating rubygems:\n****\n"
  gem install --no-rdoc --no-ri rubygems-update --version=1.6.2
  update_rubygems --version=1.6.2
fi

echo -e "`date` \n\n**** \n**** Installing chef:\n****\n"
gem install ohai --no-rdoc --no-ri
gem install chef --no-rdoc --no-ri <%= bootstrap_version_string %>
gem install      --no-rdoc --no-ri extlib json ruby-shadow right_aws

else # no chef-client
echo -e "`date` \n\n**** \n**** Chef is present -- skipping apt/ruby/chef installation\n****\n"
fi 

echo -e "`date` \n\n**** \n**** Knifing in the chef client config files:\n****\n"
mkdir -p /etc/chef

<%- if @config[:client_key] %>
(
cat <<'EOP'
<%= @config[:client_key] %>
EOP
) > /tmp/knife-bootstrap/client.pem
awk NF /tmp/knife-bootstrap/client.pem > /etc/chef/client.pem
<%- else %>
(
cat <<'EOP'
<%= validation_key %>
EOP
) > /tmp/knife-bootstrap/validation.pem
awk NF /tmp/knife-bootstrap/validation.pem > /etc/chef/validation.pem
<%- end %>

echo -e "`date` \n\n**** \n**** Nuking our temp files:\n****\n"

cd /tmp
# rm -rf /tmp/knife-bootstrap

echo -e "`date` \n\n**** \n**** Creating chef client script:\n****\n"

(
cat <<'EOP'<commit_msg>patch around bug in chef templating rescue code<commit_after>bash -c '

# This is the ubuntu-10.04-gems script from opscode, but it
# * installs ruby 1.9.2, not 1.8.7
# * upgrades rubygems rather than installing from source
# * pushes the node identity into the first-boot.json
# * installs the chef-client service and kicks off the first run of chef

<%= (@config[:verbosity].to_i > 1 ? 'set -v' : '') %>

mkdir -p /tmp/knife-bootstrap ; chmod 700 /tmp/knife-bootstrap 
cd /tmp/knife-bootstrap

RUBY_VERSION=1.9.2-p290

<%= "export http_proxy=\"#{knife_config[:bootstrap_proxy]}\"" if knife_config[:bootstrap_proxy] -%>
eval `cat /etc/lsb-release `
export DEBIAN_FRONTEND=noninteractive

if [ ! -f /usr/bin/chef-client ]; then
echo -e "`date` \n\n**** \n**** apt update:\n****\n"
apt-get    update
apt-get -y upgrade

echo -e "`date` \n\n**** \n**** Installing base packages:\n****\n"
apt-get install -y build-essential wget runit runit-services zlib1g-dev libssl-dev openssl libcurl4-openssl-dev libreadline6-dev libyaml-dev

echo -e "`date` \n\n**** \n**** Installing ruby version ${RUBY_VERSION}:\n****\n"

wget ftp://ftp.ruby-lang.org//pub/ruby/1.9/ruby-${RUBY_VERSION}.tar.gz
tar xzf ruby-${RUBY_VERSION}.tar.gz
cd ruby-${RUBY_VERSION}
./configure --with-ruby-version=${RUBY_VERSION} --prefix=/usr --program-suffix=${RUBY_VERSION}
make -j2
make install

sudo update-alternatives --remove-all gem && true
update-alternatives \
  --install /usr/bin/ruby ruby /usr/bin/ruby${RUBY_VERSION} 400 \
  --slave   /usr/bin/ri   ri   /usr/bin/ri${RUBY_VERSION}       \
  --slave   /usr/bin/irb  irb  /usr/bin/irb${RUBY_VERSION}      \
  --slave   /usr/bin/erb  erb  /usr/bin/erb${RUBY_VERSION}      \
  --slave   /usr/bin/gem  gem  /usr/bin/gem${RUBY_VERSION}      \
  --slave   /usr/share/man/man1/ruby.1.gz ruby.1.gz             \
  /usr/share/man/man1/ruby${RUBY_VERSION}.1

if ruby -e "exit(%x{gem --version} < \"1.6.2\" ? 0 : -1 )" ; then
  echo -e "`date` \n\n**** \n**** Updating rubygems:\n****\n"
  gem install --no-rdoc --no-ri rubygems-update --version=1.6.2
  update_rubygems --version=1.6.2
fi

echo -e "`date` \n\n**** \n**** Installing chef:\n****\n"
gem install ohai --no-rdoc --no-ri
gem install chef --no-rdoc --no-ri <%= bootstrap_version_string %>
gem install      --no-rdoc --no-ri extlib json ruby-shadow right_aws

else # no chef-client
echo -e "`date` \n\n**** \n**** Chef is present -- skipping apt/ruby/chef installation\n****\n"
fi 

# fix a bug in chef that prevents debugging template errors
bad_template_file='/usr/lib/ruby/gems/1.9.2-p290/gems/chef-0.10.4/lib/chef/mixin/template.rb'
if  echo "0505c482b8b0b333ac71bbc8a1795d19  $bad_template_file" | md5sum -c - 2>/dev/null ; then
  curl https://github.com/mrflip/chef/commit/655a1967253a8759afb54f30b818bbcb7c309198.patch | sudo patch $bad_template_file
fi

echo -e "`date` \n\n**** \n**** Knifing in the chef client config files:\n****\n"
mkdir -p /etc/chef

<%- if @config[:client_key] %>
(
cat <<'EOP'
<%= @config[:client_key] %>
EOP
) > /tmp/knife-bootstrap/client.pem
awk NF /tmp/knife-bootstrap/client.pem > /etc/chef/client.pem
<%- else %>
(
cat <<'EOP'
<%= validation_key %>
EOP
) > /tmp/knife-bootstrap/validation.pem
awk NF /tmp/knife-bootstrap/validation.pem > /etc/chef/validation.pem
<%- end %>

echo -e "`date` \n\n**** \n**** Nuking our temp files:\n****\n"

cd /tmp
# rm -rf /tmp/knife-bootstrap

echo -e "`date` \n\n**** \n**** Creating chef client script:\n****\n"

(
cat <<'EOP'
{'fraction_non_alphanumeric': 0.19571537872991585, 'fraction_numerical': 0.02310635042081102, 'mean_word_length': 3.235904082955282, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 30, 'https://': 1, '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': '6642440', 'n_tokens_mistral': 2760, 'n_tokens_neox': 2535, 'n_words': 599}