<commit_before>#!/opt/elasticbeanstalk/lib/ruby/bin/ruby
#encoding: utf-8

# Copyright 2015-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
# except in compliance with the License. A copy of the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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.

require 'json'
require 'open3'
require 'aws-sdk'
require 'open-uri'

# get the region that the instance is running in
$region = JSON.parse(open('http://169.254.169.254/latest/dynamic/instance-identity/document').read)["region"]

# create a new DynamoDB client
$ddb = Aws::DynamoDB::Client.new({ region: $region })

def main
  # read environment variables using the get-config utility
  env_vars = JSON.parse(%x(/opt/elasticbeanstalk/bin/get-config environment))

  # read the private IP of the instance from the metadata service
  private_ip = open('http://169.254.169.254/latest/meta-data/local-ipv4').read

  # get the number of CPU cores available
  num_cores = get_num_cores()

  eb_env_name = env_vars["EB_ENV_NAME"]
  master_ip_table = env_vars["MASTER_IP_TABLE"]

  # Use DynamoDB conditional update to select a master and save it's IP.
  # Only a single instance will be able to update the record, all others
  # will fail the conditional check
  is_master = write_master_ip(master_ip_table, eb_env_name, private_ip)

  puts "We are " + (is_master ? "master" : "follower")

  if is_master
    # since this instance is the master, save 127.0.0.1 as the master IP
    # to be used by the follower processes
    File.open('.masterIP', "w") { |f| f.print "127.0.0.1" }

    # write .foreman file with a single master process and the number of cores
    # available minus 1 follower processes
    File.open('.foreman', "w") { |f| f.print "concurrency: locust-master=1,locust-follower=#{num_cores - 1}" }
  else
    # since this instance is a follower, get the master IP from the DynamoDB table
    master_ip = get_master_ip(master_ip_table, eb_env_name)

    if master_ip
      # save the master IP to be used by the follower processes
      File.open('.masterIP', "w") { |f| f.print "#{master_ip}" }

      # update the nginx.conf to use the master instances IP for upstream
      run_command('sed -i -e "s|\(.*\)http://\(.*\):\(.*\)|\1http://' + master_ip + ':\3|g" '\
                  '.ebextensions/nginx/nginx.conf')
    end

    # write the .foreman file with zero master processes and number of cores
    # available follower proceses
    File.open('.foreman', "w") { |f| f.print "concurrency: locust-master=1,locust-follower=#{num_cores}" }
  end

  # Recreate the application.conf since we have modified the .foreman file
  run_command('HOME=/tmp /opt/elasticbeanstalk/lib/ruby/bin/ruby '\<commit_msg>Update build.rb<commit_after>#!/opt/elasticbeanstalk/lib/ruby/bin/ruby
#encoding: utf-8

# Copyright 2015-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
# except in compliance with the License. A copy of the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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.

require 'json'
require 'open3'
require 'aws-sdk'
require 'open-uri'

# get the region that the instance is running in
$region = JSON.parse(open('http://169.254.169.254/latest/dynamic/instance-identity/document').read)["region"]

# create a new DynamoDB client
$ddb = Aws::DynamoDB::Client.new({ region: $region })

def main
  # read environment variables using the get-config utility
  env_vars = JSON.parse(%x(/opt/elasticbeanstalk/bin/get-config environment))

  # read the private IP of the instance from the metadata service
  private_ip = open('http://169.254.169.254/latest/meta-data/local-ipv4').read

  # get the number of CPU cores available
  num_cores = get_num_cores()

  eb_env_name = env_vars["EB_ENV_NAME"]
  master_ip_table = env_vars["MASTER_IP_TABLE"]

  # Use DynamoDB conditional update to select a master and save it's IP.
  # Only a single instance will be able to update the record, all others
  # will fail the conditional check
  is_master = write_master_ip(master_ip_table, eb_env_name, private_ip)

  puts "We are " + (is_master ? "master" : "follower")

  if is_master
    # since this instance is the master, save 127.0.0.1 as the master IP
    # to be used by the follower processes
    File.open('.masterIP', "w") { |f| f.print "127.0.0.1" }

    # write .foreman file with a single master process and the number of cores
    # available minus 1 follower processes
    File.open('.foreman', "w") { |f| f.print "concurrency: locust-master=1,locust-follower=#{num_cores - 1}" }
  else
    # since this instance is a follower, get the master IP from the DynamoDB table
    master_ip = get_master_ip(master_ip_table, eb_env_name)

    if master_ip
      # save the master IP to be used by the follower processes
      File.open('.masterIP', "w") { |f| f.print "#{master_ip}" }

      # update the nginx.conf to use the master instances IP for upstream
      run_command('sed -i -e "s|\(.*\)http://\(.*\):\(.*\)|\1http://' + master_ip + ':\3|g" '\
                  '.ebextensions/nginx/nginx.conf')
    end

    # write the .foreman file with zero master processes and number of cores
    # available follower proceses
    File.open('.foreman', "w") { |f| f.print "concurrency: locust-master=0,locust-follower=#{num_cores}" }
  end

  # Recreate the application.conf since we have modified the .foreman file
  run_command('HOME=/tmp /opt/elasticbeanstalk/lib/ruby/bin/ruby '\
{'fraction_non_alphanumeric': 0.11400065423617926, 'fraction_numerical': 0.018645731108930325, 'mean_word_length': 3.6786534047436876, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, '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': '3411533', 'n_tokens_mistral': 2044, 'n_tokens_neox': 1871, 'n_words': 663}