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].freeze
BOOL_OPTIONS =
[:daemonize, :keep_alive, :auto_start, :stop_on_delete, :clear_pid, :preserve_fds, :use_leaf_child,
:clear_env, :check_identity].freeze
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, :check_identity_period, :check_identity_grace].freeze

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, with_parsed_file

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) && parent.name != '__default__'
  @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[:type] = nac[:type]
  Eye::Checker.validate!(opts)

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

#clear_bundler_envObject



146
147
148
# File 'lib/eye/dsl/opts.rb', line 146

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



142
143
144
# File 'lib/eye/dsl/opts.rb', line 142

def daemonize!
  set_daemonize true
end

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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/eye/dsl/opts.rb', line 182

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

  unless file
    raise Eye::Dsl::Error, "load_env not found in #{fnames}" if raise_when_no_file
    warn "load_env not found file: '#{filename}'"
    return
  end

  info "load_env from '#{file}'"
  Eye::Utils.load_env(file).each { |k, v| env k => v }
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



150
151
152
153
154
# File 'lib/eye/dsl/opts.rb', line 150

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

#set_environment(value) ⇒ Object

Raises:



116
117
118
119
120
# File 'lib/eye/dsl/opts.rb', line 116

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:



137
138
139
140
# File 'lib/eye/dsl/opts.rb', line 137

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



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

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:



132
133
134
135
# File 'lib/eye/dsl/opts.rb', line 132

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

#skip_group_action(act, val = true) ⇒ Object



197
198
199
200
# File 'lib/eye/dsl/opts.rb', line 197

def skip_group_action(act, val = true)
  @config[:skip_group_actions] ||= {}
  @config[:skip_group_actions][act] = val
end

#stop_signals(*args) ⇒ Object

Raises:



102
103
104
105
106
107
108
109
110
# 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]

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

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

#stop_signals=(s) ⇒ Object



112
113
114
# File 'lib/eye/dsl/opts.rb', line 112

def stop_signals=(s)
  stop_signals(s)
end

#syslogObject



202
203
204
# File 'lib/eye/dsl/opts.rb', line 202

def syslog
  ':syslog'
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[: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



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

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