build_env

in cookbooks/aws-parallelcluster-scheduler-plugin/resources/execute_event_handler.rb [49:122]


  def build_env
    
    target_cluster_config = "#{node['cluster']['scheduler_plugin']['handler_dir']}/cluster-config.yaml"
    copy_config("cluster configuration", node.dig(:cluster, :cluster_config_path), target_cluster_config)

    
    target_previous_cluster_config = "#{node['cluster']['scheduler_plugin']['handler_dir']}/previous-cluster-config.yaml"
    if new_resource.event_name == 'HeadClusterUpdate'
      copy_config("previous cluster configuration", node.dig(:cluster, :previous_cluster_config_path), target_previous_cluster_config)
    end

    
    target_computefleet_status = "#{node['cluster']['scheduler_plugin']['handler_dir']}/computefleet-status.json"
    if new_resource.event_name == 'HeadComputeFleetUpdate'
      copy_config("computefleet status", node.dig(:cluster, :computefleet_status_path), target_computefleet_status)
    end

    
    target_launch_templates = "#{node['cluster']['scheduler_plugin']['handler_dir']}/launch-templates-config.json"
    copy_config("launch templates", node.dig(:cluster, :launch_templates_config_path), target_launch_templates)

    
    target_instance_types_data = "#{node['cluster']['scheduler_plugin']['handler_dir']}/instance-types-data.json"
    copy_config("instance types data", node.dig(:cluster, :instance_types_data_path), target_instance_types_data)

    
    source_scheduler_plugin_substack_outputs = node['cluster']['scheduler_plugin']['scheduler_plugin_substack_outputs_path']
    target_scheduler_plugin_substack_outputs = "#{node['cluster']['scheduler_plugin']['handler_dir']}/scheduler-plugin-substack-outputs.json"
    scheduler_plugin_substack_arn = node.dig(:cluster, :scheduler_plugin_substack_arn)
    if scheduler_plugin_substack_arn && !scheduler_plugin_substack_arn.empty?
      Chef::Log.info("Found scheduler plugin substack (#{scheduler_plugin_substack_arn})")
      if !::File.exist?(source_scheduler_plugin_substack_outputs) || new_resource.event_name == 'HeadClusterUpdate'
        scheduler_plugin_substack_outputs = { 'Outputs' => {} }
        Chef::Log.info("Executing describe-stack on scheduler plugin substack (#{scheduler_plugin_substack_arn})")
        cmd = command_with_retries("aws cloudformation describe-stacks --region #{node.dig(:ec2, :region)} --stack-name #{scheduler_plugin_substack_arn}", 3, 'root', nil, nil)
        raise "Unable to execute describe-stack on scheduler plugin substack (#{scheduler_plugin_substack_arn})\n #{format_stderr(cmd)}" if cmd.error?

        if cmd.stdout && !cmd.stdout.empty?
          Chef::Log.debug("Output of describe-stacks on substack (#{scheduler_plugin_substack_arn}): (#{cmd.stdout})")
          substack_describe = JSON.parse(cmd.stdout)
          substack_outputs = substack_describe['Stacks'][0]['Outputs']
          if substack_outputs && !substack_outputs.empty?
            substack_outputs.each do |substack_output|
              scheduler_plugin_substack_outputs['Outputs'].merge!({ substack_output['OutputKey'] => substack_output['OutputValue'] })
            end
          end
          ::File.write(source_scheduler_plugin_substack_outputs, scheduler_plugin_substack_outputs.to_json(:only))
        end
      end
    end

    if ::File.exist?(source_scheduler_plugin_substack_outputs)
      copy_config("scheduler plugin substack outputs", source_scheduler_plugin_substack_outputs, target_scheduler_plugin_substack_outputs)
    end

    
    source_handler_env = "#{node['cluster']['shared_dir']}/handler-env.json"
    if ::File.exist?(source_handler_env)
      Chef::Log.info("Found handler environment file (#{source_handler_env})")
      env = JSON.load_file(source_handler_env)
      Chef::Log.debug("Loaded handler environment #{env}")
    else
      Chef::Log.info("No handler environment file found, building it")
      env = build_static_env(target_cluster_config, target_launch_templates, target_instance_types_data, target_scheduler_plugin_substack_outputs)

      Chef::Log.info("Dumping handler environment to file (#{source_handler_env})")
      ::File.write(source_handler_env, env.to_json(:only))
    end

    
    env.merge!(build_dynamic_env(target_previous_cluster_config, target_computefleet_status))
    env
  end