Class: Grifork::DSL
- Inherits:
-
Object
- Object
- Grifork::DSL
- Defined in:
- lib/grifork/dsl.rb
Overview
DSL parser for Griforkfile
Griforkfile is interpreted in instance context by an object of this Class.
Defined Under Namespace
Classes: LoadError
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
-
.load_file(path, on_remote: false) ⇒ Grifork::DSL
Load DSL file to object.
Instance Method Summary collapse
-
#branches(num) ⇒ Object
Branches number for tree of host nodes.
- #config_set(key, value) ⇒ Object private
-
#finish(&task) ⇒ Object
Define tasks to execute at localhost in the end of procedure.
-
#finish_remote(&task) ⇒ Object
Define tasks to execute at remote host which execute
grifork
tasks in:grifork
mode in the end of its procedure. -
#grifork(&command) ⇒ Object
Configure grifork settings for
:grifork
mode. -
#hosts(list) ⇒ Object
Host list as targets of tasks.
-
#initialize(on_remote) ⇒ DSL
constructor
A new instance of DSL.
-
#load_and_merge_config_by!(path) ⇒ Object
Load another DSL file and merge its config.
-
#local(&task) ⇒ Object
Define tasks to execute at localhost.
-
#log(args) ⇒ Object
Configure logging.
-
#mode(m) ⇒ Object
Grifork mode: How it works.
-
#parallel(how) ⇒ Object
Forking method to exec tasks in parallel.
-
#prepare(&task) ⇒ Object
Define tasks to execute at localhost before starting procedure.
-
#prepare_remote(&task) ⇒ Object
Define tasks to execute at remote host which execute
grifork
tasks in:grifork
mode before starting its procedure. -
#remote(&task) ⇒ Object
Define tasks to execute at remote host.
-
#rsync(props) ⇒ Object
Configure rsync command options.
-
#ssh(props) ⇒ Object
Configure net-ssh options.
-
#to_config ⇒ Grifork::Config
Creates Config object from holding properties.
Constructor Details
#initialize(on_remote) ⇒ DSL
Returns a new instance of DSL.
20 21 22 23 |
# File 'lib/grifork/dsl.rb', line 20 def initialize(on_remote) @config = {} @on_remote = on_remote end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
5 6 7 |
# File 'lib/grifork/dsl.rb', line 5 def config @config end |
Class Method Details
.load_file(path, on_remote: false) ⇒ Grifork::DSL
Load DSL file to object
13 14 15 16 17 18 |
# File 'lib/grifork/dsl.rb', line 13 def self.load_file(path, on_remote: false) content = File.binread(path) dsl = new(on_remote) dsl.instance_eval(content) dsl end |
Instance Method Details
#branches(num) ⇒ Object
Branches number for tree of host nodes
60 61 62 |
# File 'lib/grifork/dsl.rb', line 60 def branches(num) config_set(:branches, num) end |
#config_set(key, value) ⇒ Object (private)
172 173 174 175 176 177 |
# File 'lib/grifork/dsl.rb', line 172 def config_set(key, value) if @config[key] raise LoadError, %(Config "#{key}" is already defined!) end @config[key] = value end |
#finish(&task) ⇒ Object
In :grifork
mode, this is executed only at localhost
Define tasks to execute at localhost in the end of procedure
147 148 149 150 |
# File 'lib/grifork/dsl.rb', line 147 def finish(&task) return if @on_remote config_set(:finish_task, Grifork::Executor::Local.new(:finish, &task)) end |
#finish_remote(&task) ⇒ Object
In :standalone
mode, this is never executed
Define tasks to execute at remote host which execute grifork
tasks in :grifork
mode in the end of its procedure
165 166 167 168 |
# File 'lib/grifork/dsl.rb', line 165 def finish_remote(&task) return unless @on_remote config_set(:finish_task, Grifork::Executor::Local.new(:finish, &task)) end |
#grifork(&command) ⇒ Object
Configure grifork settings for :grifork
mode
51 52 53 54 55 56 57 |
# File 'lib/grifork/dsl.rb', line 51 def grifork(&command) if @config[:mode] == :standalone raise LoadError, "Can't configure grifork in standalone mode" end @config[:mode] = :grifork config_set(:grifork, Grifork::Config::Grifork.new(&command)) end |
#hosts(list) ⇒ Object
Host list as targets of tasks
83 84 85 |
# File 'lib/grifork/dsl.rb', line 83 def hosts(list) config_set(:hosts, list) end |
#load_and_merge_config_by!(path) ⇒ Object
Load another DSL file and merge its config
32 33 34 35 36 37 |
# File 'lib/grifork/dsl.rb', line 32 def load_and_merge_config_by!(path) content = File.binread(path) other = self.class.new(@on_remote) other.instance_eval(content) @config.merge!(other.config) end |
#local(&task) ⇒ Object
In :grifork
mode, this is executed only at localhost
Define tasks to execute at localhost
119 120 121 122 |
# File 'lib/grifork/dsl.rb', line 119 def local(&task) return if @on_remote config_set(:local_task, Grifork::Executor::Carrier.new(:local, &task)) end |
#log(args) ⇒ Object
Configure logging
67 68 69 |
# File 'lib/grifork/dsl.rb', line 67 def log(args) config_set(:log, Grifork::Config::Log.new(args)) end |
#mode(m) ⇒ Object
Grifork mode: How it works
41 42 43 44 45 46 |
# File 'lib/grifork/dsl.rb', line 41 def mode(m) unless Grifork::MODES.has_key?(m) raise LoadError, "Undefined mode! #{m}" end config_set(:mode, m) end |
#parallel(how) ⇒ Object
Forking method to exec tasks in parallel.
74 75 76 77 78 79 |
# File 'lib/grifork/dsl.rb', line 74 def parallel(how) unless %i(in_threads in_processes).include?(how) raise LoadError, "Invalid parallel mode! #{how.inspect} / must be :in_threads or :in_processes" end config_set(:parallel, how) end |
#prepare(&task) ⇒ Object
In :grifork
mode, this is executed only at localhost
Define tasks to execute at localhost before starting procedure
139 140 141 142 |
# File 'lib/grifork/dsl.rb', line 139 def prepare(&task) return if @on_remote config_set(:prepare_task, Grifork::Executor::Local.new(:prepare, &task)) end |
#prepare_remote(&task) ⇒ Object
In :standalone
mode, this is never executed
Define tasks to execute at remote host which execute grifork
tasks in :grifork
mode before starting its procedure
156 157 158 159 |
# File 'lib/grifork/dsl.rb', line 156 def prepare_remote(&task) return unless @on_remote config_set(:prepare_task, Grifork::Executor::Local.new(:prepare, &task)) end |
#remote(&task) ⇒ Object
In :standalone
mode, the task is executed at localhost actually. In :grifork
mode, it is executed at remote hosts via grifork
command on remote hosts
Define tasks to execute at remote host
128 129 130 131 132 133 134 |
# File 'lib/grifork/dsl.rb', line 128 def remote(&task) if @on_remote config_set(:local_task, Grifork::Executor::Carrier.new(:local, &task)) else config_set(:remote_task, Grifork::Executor::Carrier.new(:remote, &task)) end end |
#rsync(props) ⇒ Object
Configure rsync command options
112 113 114 |
# File 'lib/grifork/dsl.rb', line 112 def rsync(props) config_set(:rsync, Grifork::Config::Rsync.new(props)) end |
#ssh(props) ⇒ Object
Configure net-ssh options
95 96 97 98 99 100 101 |
# File 'lib/grifork/dsl.rb', line 95 def ssh(props) = props.keys - Net::SSH::VALID_OPTIONS if .size > 0 raise LoadError, "#{} are invalid for Net::SSH!" end config_set(:ssh, Grifork::Config::SSH.new(props)) end |
#to_config ⇒ Grifork::Config
Creates Config object from holding properties
27 28 29 |
# File 'lib/grifork/dsl.rb', line 27 def to_config Grifork::Config.new(@config) end |