Class: Eye::Dsl::Opts

Inherits:
PureOpts show all
Defined in:
lib/eye/dsl/opts.rb

Constant Summary collapse

STR_OPTIONS =
[ :pid_file, :working_dir, :stdout, :stderr, :stdall, :stdin, :start_command,
:stop_command, :restart_command, :uid, :gid ]
BOOL_OPTIONS =
[ :daemonize, :keep_alive, :auto_start, :stop_on_delete, :clear_pid, :preserve_fds, :use_leaf_child, :clear_env ]
INTERVAL_OPTIONS =
[ :check_alive_period, :start_timeout, :restart_timeout, :stop_timeout, :start_grace,
:restart_grace, :stop_grace, :children_update_period, :restore_in,
:auto_update_pidfile_grace, :revert_fuckup_pidfile_grace ]

Instance Attribute Summary

Attributes inherited from PureOpts

#config, #full_name, #name, #parent

Instance Method Summary collapse

Methods inherited from PureOpts

#allow_options, create_options_methods, #disallow_options, #nop, #not_seed_options, #use, #with_condition

Constructor Details

#initialize(name = nil, parent = nil) ⇒ Opts

Returns a new instance of Opts.



19
20
21
22
23
24
25
26
27
# File 'lib/eye/dsl/opts.rb', line 19

def initialize(name = nil, parent = nil)
  super(name, parent)

  @config[:application] = parent.name if parent.is_a?(Eye::Dsl::ApplicationOpts)
  @config[:group] = parent.name if parent.is_a?(Eye::Dsl::GroupOpts)

  # hack for full name
  @full_name = parent.full_name if @name == '__default__' && parent.respond_to?(:full_name)
end

Instance Method Details

#checks(type, opts = {}) ⇒ Object Also known as: check

Raises:



29
30
31
32
33
34
35
36
37
38
# File 'lib/eye/dsl/opts.rb', line 29

def checks(type, opts = {})
  nac = Eye::Checker.name_and_class(type.to_sym)
  raise Eye::Dsl::Error, "unknown checker type #{type}" unless nac

  opts.merge!(:type => nac[:type])
  Eye::Checker.validate!(opts)

  @config[:checks] ||= {}
  @config[:checks][nac[:name]] = opts
end

#clear_bundler_envObject



148
149
150
# File 'lib/eye/dsl/opts.rb', line 148

def clear_bundler_env
  env('GEM_PATH' => nil, 'GEM_HOME' => nil, 'RUBYOPT' => nil, 'BUNDLE_BIN_PATH' => nil, 'BUNDLE_GEMFILE' => nil)
end

#command(cmd, arg) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/eye/dsl/opts.rb', line 70

def command(cmd, arg)
  @config[:user_commands] ||= {}

  if arg.is_a?(Array)
    validate_signals(arg)
  elsif arg.is_a?(String)
  else
    raise Eye::Dsl::Error, "unknown command #{cmd.inspect} type should be String or Array"
  end

  @config[:user_commands][cmd.to_sym] = arg
end

#daemonize!Object



144
145
146
# File 'lib/eye/dsl/opts.rb', line 144

def daemonize!
  set_daemonize true
end

#load_env(filename = '~/.env', raise_when_no_file = true) ⇒ Object

Raises:



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/eye/dsl/opts.rb', line 184

def load_env(filename = '~/.env', raise_when_no_file = true)
  fnames = [File.expand_path(filename, @config[:working_dir]),
    File.expand_path(filename)].uniq
  filenames = fnames.select { |f| File.exists?(f) }

  if filenames.size < 1
    unless raise_when_no_file
      warn "load_env not found file: '#{filenames.first}'"
      return
    else
      raise Eye::Dsl::Error, "load_env not found in #{fnames}"
    end
  end
  raise Eye::Dsl::Error, "load_env conflict filenames: #{filenames}" if filenames.size > 1

  content = File.read(filenames.first)
  info "load_env from '#{filenames.first}'"

  env_vars = content.split("\n")
  env_vars.each do |e|
    next unless e.include?('=')
    k, *v = e.split('=')
    env k => v.join('=')
  end
end

#nochecks(type) ⇒ Object Also known as: nocheck

clear checks from parent

Raises:



52
53
54
55
56
# File 'lib/eye/dsl/opts.rb', line 52

def nochecks(type)
  nac = Eye::Checker.name_and_class(type.to_sym)
  raise Eye::Dsl::Error, "unknown checker type #{type}" unless nac
  @config[:checks].try :delete, nac[:name]
end

#nonotify(contact) ⇒ Object



92
93
94
95
# File 'lib/eye/dsl/opts.rb', line 92

def nonotify(contact)
  @config[:notify] ||= {}
  @config[:notify].delete(contact.to_s)
end

#notify(contact, level = :warn) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/eye/dsl/opts.rb', line 83

def notify(contact, level = :warn)
  unless Eye::Process::Notify::LEVELS[level]
    raise Eye::Dsl::Error, "level should be in #{Eye::Process::Notify::LEVELS.keys}"
  end

  @config[:notify] ||= {}
  @config[:notify][contact.to_s] = level
end

#notriggers(type) ⇒ Object Also known as: notrigger

clear triggers from parent

Raises:



59
60
61
62
63
# File 'lib/eye/dsl/opts.rb', line 59

def notriggers(type)
  nac = Eye::Trigger.name_and_class(type.to_sym)
  raise Eye::Dsl::Error, "unknown trigger type #{type}" unless nac
  @config[:triggers].try :delete, nac[:name]
end

#scoped(&block) ⇒ Object



152
153
154
155
156
# File 'lib/eye/dsl/opts.rb', line 152

def scoped(&block)
  h = self.class.new(self.name, self)
  h.instance_eval(&block)
  Eye::Utils.deep_merge!(config, h.config, [:groups, :processes])
end

#set_environment(value) ⇒ Object

Raises:



118
119
120
121
122
# File 'lib/eye/dsl/opts.rb', line 118

def set_environment(value)
  raise Eye::Dsl::Error, "environment should be a hash, but not #{value.inspect}" unless value.is_a?(Hash)
  @config[:environment] ||= {}
  @config[:environment].merge!(value)
end

#set_gid(value) ⇒ Object

Raises:



139
140
141
142
# File 'lib/eye/dsl/opts.rb', line 139

def set_gid(value)
  raise Eye::Dsl::Error, ':gid not supported (use ruby >= 2.0)' unless Eye::Local.supported_setsid?
  super
end

#set_stdall(value) ⇒ Object



127
128
129
130
131
132
# File 'lib/eye/dsl/opts.rb', line 127

def set_stdall(value)
  super

  set_stdout value
  set_stderr value
end

#set_stop_command(cmd) ⇒ Object

Raises:



97
98
99
100
# File 'lib/eye/dsl/opts.rb', line 97

def set_stop_command(cmd)
  raise Eye::Dsl::Error, "cannot use both stop_signals and stop_command" if @config[:stop_signals]
  super
end

#set_uid(value) ⇒ Object

Raises:



134
135
136
137
# File 'lib/eye/dsl/opts.rb', line 134

def set_uid(value)
  raise Eye::Dsl::Error, ':uid not supported (use ruby >= 2.0)' unless Eye::Local.supported_setsid?
  super
end

#stop_signals(*args) ⇒ Object

Raises:



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/eye/dsl/opts.rb', line 102

def stop_signals(*args)
  raise Eye::Dsl::Error, "cannot use both stop_signals and stop_command" if @config[:stop_command]

  if args.count == 0
    return @config[:stop_signals]
  end

  signals = Array(args).flatten
  validate_signals(signals)
  @config[:stop_signals] = signals
end

#stop_signals=(s) ⇒ Object



114
115
116
# File 'lib/eye/dsl/opts.rb', line 114

def stop_signals=(s)
  stop_signals(s)
end

#triggers(type, opts = {}) ⇒ Object Also known as: trigger

Raises:



40
41
42
43
44
45
46
47
48
49
# File 'lib/eye/dsl/opts.rb', line 40

def triggers(type, opts = {})
  nac = Eye::Trigger.name_and_class(type.to_sym)
  raise Eye::Dsl::Error, "unknown trigger type #{type}" unless nac

  opts.merge!(:type => nac[:type])
  Eye::Trigger.validate!(opts)

  @config[:triggers] ||= {}
  @config[:triggers][nac[:name]] = opts
end

#with_server(glob = nil, &block) ⇒ Object

execute part of config on particular server array of strings regexp string



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/eye/dsl/opts.rb', line 162

def with_server(glob = nil, &block)
  on_server = true

  if glob.present?
    host = Eye::Local.host

    if glob.is_a?(Array)
      on_server = !!glob.any?{|elem| elem == host}
    elsif glob.is_a?(Regexp)
      on_server = !!host.match(glob)
    elsif glob.is_a?(String) || glob.is_a?(Symbol)
      on_server = (host == glob.to_s)
    end
  end

  scoped do
    with_condition(on_server, &block)
  end

  on_server
end