Class: Grifork::DSL

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#configObject (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

Parameters:

  • path (String)

    path of DSL file

  • on_remote (Boolean) (defaults to: false)

    whether process is invoked by remote host in :grifork mode or not

Returns:



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

Note:

In :grifork mode, this is executed only at localhost

Define tasks to execute at localhost in the end of procedure

Parameters:



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

Note:

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

Parameters:



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

Parameters:

  • &command (Proc)

See Also:

  • Config::Grifork.initialize


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

Parameters:

  • hosts (Array<String>)

    List of resolvable hostnames



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

Note:

In :grifork mode, this is executed only at localhost

Define tasks to execute at localhost

Parameters:



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

Parameters:

  • args (Hash)

See Also:

  • Config::Log.initialize


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

Parameters:

  • m (Symbol)

    :standalone or :grifork. Defaults to :standalone



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.

Parameters:

  • how (:Symbol)

    :in_threads or :in_processes. Defaults to :in_threads

See Also:



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

Note:

In :grifork mode, this is executed only at localhost

Define tasks to execute at localhost before starting procedure

Parameters:



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

Note:

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

Parameters:



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

Note:

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

Parameters:



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

Examples:

# Available full Hash options are bellow
rsync delete: true, bwlimit: 4096, verbose: false, excludes: %w(.git* .svn*), rsh: nil, dry_run: false
# This is the same with above by Array format:
rsync %w(-az --delete --bwlimit=4096 --exclude=.git* --exclude=.svn*)
# You can set more options by Array format:
rsync %w(-azKc -e=rsh --delete --bwlimit=4096 --exclude-from=path/to/rsync.excludes)


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

Examples:

# Password authentication
ssh user: 'someone', password: 'xxx'
# Private key authentication
ssh user: 'someone', keys: ['path/to/priv_key'], passphrase: 'xxx'

See Also:



95
96
97
98
99
100
101
# File 'lib/grifork/dsl.rb', line 95

def ssh(props)
  invalid_options = props.keys - Net::SSH::VALID_OPTIONS
  if invalid_options.size > 0
    raise LoadError, "#{invalid_options} are invalid for Net::SSH!"
  end
  config_set(:ssh, Grifork::Config::SSH.new(props))
end

#to_configGrifork::Config

Creates Config object from holding properties

Returns:



27
28
29
# File 'lib/grifork/dsl.rb', line 27

def to_config
  Grifork::Config.new(@config)
end