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

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


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


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
  "#!/bin/bash\n\ncd \#{work_dir}\n\n# Generate a connection yaml file with given parameters\nfunction create_yml () {\necho \"Generating connection YAML file...\"\n(\numask 077\necho -e \"\#{conn_params.map { |p| \"\#{p}: $\#{p}\" }.join('\\n')}\" > \"\#{conn_file}\"\n)\n}\n\n# Cleanliness is next to Godliness\nfunction clean_up () {\necho \"Cleaning up...\"\n\#{clean_script.gsub(/\\n(?=[^\\s])/, \"\\n  \")}\npkill -P $$\nexit ${1:-0}\n}\n\n\#{bash_helpers}\n\n\#{before_script}\n\necho \"Script starting...\"\n\#{run_script} &\nSCRIPT_PID=$!\n\n\#{after_script}\n\n# Create the connection yaml file\ncreate_yml\n\n# Wait for script process to finish\nwait ${SCRIPT_PID} || clean_up 1\n\n# Exit cleanly\nclean_up\n".gsub(/^ {10}/, '')
end