<commit_before>
require 'stacker/stacker.rb'

class GlobalStack

  def initialize(options = {})
    @name = 'global'
    @template = options[:template] if options.has_key?(:template)

    #@bastion_ami should be given via parameters in create_or_update, template could be overwritten directly
    # if you knew everything at construction time you could pass parameters into the constructor, but then
    # GlobalStack behaves differently than ServiceStack, but maybe this is OK

    # gstack = GlobalStack.new
    # ....
    # gstack.template = IO.read('../global-stack/global-stack.json')
    # gstack.create_or_update(:BastionAmi => File.exists?('ami.txt') ? IO.read('ami.txt') : 'ami-28b7045f')
    #

    @version = options[:version] || ENV['GLOBAL_VERSION'] || ENV['GO_PIPELINE_LABEL'] || latest_version
    @sandbox = options[:sandbox] || ENV['GLOBAL_SANDBOX'] || (ENV['GO_JOB_NAME'].nil? && `whoami`.strip) # use whoami if no sandbox is given
    @stack_name = Stacker.sandboxed_stack_name(@sandbox, @name)
  end

  attr_reader :name, :sandbox, :version, :stack_name
  attr_writer :template

  def create_or_update(parameters = {})

    # if :bastion_ami not given fetch it from s3 "global-stack-template/#{version}/ami.txt"
    unless parameters.has_key?(:BastionAmi)
      s3_key = "global-stack-template/#{version}/ami.txt"
      puts "Fetching BastionAmi from #{s3_key}"
      parameters[:BastionAmi] = s3.get_object(bucket: 'as24.tatsu.artefacts', key: s3_key).body.read.strip
    end

    Stacker.create_or_update_stack(stack_name, template, parameters)
  end

  def delete
     Stacker.delete_stack(stack_name)
  end

  def template # if not set from the outside, fetch it from s3
    if @template.nil?
      s3_key = "global-stack-template/#{version}/global-stack.json"
      puts "Fetching global stack template from #{s3_key}"
      s3.get_object(bucket: 'as24.tatsu.artefacts', key: s3_key).body.read
    else<commit_msg>jmueller/kief: never delete the non-sandboxed global stack<commit_after>
require 'stacker/stacker.rb'

class GlobalStack

  def initialize(options = {})
    @name = 'global'
    @template = options[:template] if options.has_key?(:template)

    #@bastion_ami should be given via parameters in create_or_update, template could be overwritten directly
    # if you knew everything at construction time you could pass parameters into the constructor, but then
    # GlobalStack behaves differently than ServiceStack, but maybe this is OK

    # gstack = GlobalStack.new
    # ....
    # gstack.template = IO.read('../global-stack/global-stack.json')
    # gstack.create_or_update(:BastionAmi => File.exists?('ami.txt') ? IO.read('ami.txt') : 'ami-28b7045f')
    #

    @version = options[:version] || ENV['GLOBAL_VERSION'] || ENV['GO_PIPELINE_LABEL'] || latest_version
    @sandbox = options[:sandbox] || (ENV['GO_JOB_NAME'].nil? && `whoami`.strip) # use whoami if no sandbox is given
    @stack_name = Stacker.sandboxed_stack_name(@sandbox, @name)
  end

  attr_reader :name, :sandbox, :version, :stack_name
  attr_writer :template

  def create_or_update(parameters = {})

    # if :bastion_ami not given fetch it from s3 "global-stack-template/#{version}/ami.txt"
    unless parameters.has_key?(:BastionAmi)
      s3_key = "global-stack-template/#{version}/ami.txt"
      puts "Fetching BastionAmi from #{s3_key}"
      parameters[:BastionAmi] = s3.get_object(bucket: 'as24.tatsu.artefacts', key: s3_key).body.read.strip
    end

    Stacker.create_or_update_stack(stack_name, template, parameters)
  end

  def delete
     if @stack_name == @name
       puts "I will not delete a non-sandboxed global stack"
     else
       Stacker.delete_stack(stack_name)
     end
  end

  def template # if not set from the outside, fetch it from s3
    if @template.nil?
      s3_key = "global-stack-template/#{version}/global-stack.json"
      puts "Fetching global stack template from #{s3_key}"
      s3.get_object(bucket: 'as24.tatsu.artefacts', key: s3_key).body.read
    else
{'fraction_non_alphanumeric': 0.12122723871289598, 'fraction_numerical': 0.009977550511349464, 'mean_word_length': 3.896214896214896, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6315406', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1317, 'n_words': 318}