Class: OodCore::BatchConnect::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/ood_core/batch_connect/template.rb

Overview

A template class that renders a batch script designed to facilitate external connections to the running job

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = {}) ⇒ Template

Returns a new instance of Template

Parameters:

  • context (#to_h) (defaults to: {})

    the context used to render the template

Options Hash (context):

  • :work_dir (#to_s)

    Working directory for batch script

  • :conn_file (#to_s) — default: "connection.yml"

    The file that holds connection information

  • :conn_params (#to_sym, Array<#to_sym>) — default: []

    A list of connection parameters added to the connection file (`:host`, `:port`, and `:password` will always exist)

  • :bash_helpers (#to_s) — default: "..."

    Bash helper methods

  • :min_port (#to_i) — default: 2000

    Minimum port used when looking for available port

  • :max_port (#to_i) — default: 65535

    Maximum port used when looking for available port

  • :passwd_size (#to_i) — default: 32

    Length of randomly generated password

  • :before_script (#to_s) — default: "..."

    Bash code run before the main script is forked off

  • :before_file (#to_s) — default: "before.sh"

    Path to script that is sourced before main script is forked (assumes you don't modify `:before_script`)

  • :run_script (#to_s) — default: "..."

    Bash code that is forked off and treated as the main script

  • :script_file (#to_s) — default: "./script.sh"

    Path to script that is forked as the main scripta (assumes you don't modify `:run_script`)

  • :timeout (#to_s) — default: ""

    Timeout the main script in seconds, if empty then let script run for full walltime (assumes you don't modify `:run_script`)

  • :clean_script (#to_s) — default: "..."

    Bash code run during clean up after job finishes

  • :clean_file (#to_s) — default: "clean.sh"

    Path to script that is sourced during clean up (assumes you don't modify `:clean_script`)

Raises:

  • (ArgumentError)

45
46
47
48
# File 'lib/ood_core/batch_connect/template.rb', line 45

def initialize(context = {})
  @context = context.to_h.compact.symbolize_keys
  raise ArgumentError, "No work_dir specified. Missing argument: work_dir" unless context.include?(:work_dir)
end

Instance Attribute Details

#contextHash (readonly)

The context used to render this template

Returns:

  • (Hash)

    context hash


12
13
14
# File 'lib/ood_core/batch_connect/template.rb', line 12

def context
  @context
end

Instance Method Details

#to_sString

Render this template as string

Returns:

  • (String)

    rendered template


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ood_core/batch_connect/template.rb', line 52

def to_s
  <<-EOT.gsub(/^ {10}/, '')
    #!/bin/bash

    cd #{work_dir}

    # Generate a connection yaml file with given parameters
    function create_yml () {
      echo "Generating connection YAML file..."
      (
        umask 077
        echo -e "#{conn_params.map { |p| "#{p}: $#{p}" }.join('\n')}" > "#{conn_file}"
      )
    }

    # Cleanliness is next to Godliness
    function clean_up () {
      echo "Cleaning up..."
      #{clean_script.gsub(/\n(?=[^\s])/, "\n  ")}
      pkill -P $$
      exit ${1:-0}
    }

    #{bash_helpers}

    #{before_script}

    echo "Script starting..."
    #{run_script} &
    SCRIPT_PID=$!

    #{after_script}

    # Create the connection yaml file
    create_yml

    # Wait for script process to finish
    wait ${SCRIPT_PID} || clean_up 1

    # Exit cleanly
    clean_up
  EOT
end