Module: GoldenBrindle::Command::Base
- Included in:
- Brindle::Configure, Brindle::Restart, Brindle::Start, Brindle::Stop, Cluster::Base
- Defined in:
- lib/golden_brindle/command.rb
Instance Attribute Summary collapse
-
#done_validating ⇒ Object
readonly
Returns the value of attribute done_validating.
-
#original_args ⇒ Object
readonly
Returns the value of attribute original_args.
-
#valid ⇒ Object
readonly
Returns the value of attribute valid.
Instance Method Summary collapse
- #can_change_user? ⇒ Boolean
- #config_keys ⇒ Object
- #configure ⇒ Object
-
#failure(message) ⇒ Object
Just a simple method to display failure until something better is developed.
-
#help ⇒ Object
Returns a help message.
-
#initialize(options = {}) ⇒ Object
Called by the subclass to setup the command and parse the argv arguments.
- #load_config ⇒ Object
-
#options(opts) ⇒ Object
Called by the implemented command to set the options for that command.
-
#run ⇒ Object
Runs the command doing it’s job.
-
#valid?(exp, message) ⇒ Boolean
Validates the given expression is true and prints the message if not, exiting.
-
#valid_dir?(file, message) ⇒ Boolean
Validates that the given directory exists.
-
#valid_exists?(file, message) ⇒ Boolean
Validates that a file exists and if not displays the message.
-
#valid_file?(file, message) ⇒ Boolean
Validates that the file is a file and not a directory or something else.
- #valid_group?(group) ⇒ Boolean
- #valid_user?(user) ⇒ Boolean
-
#validate ⇒ Object
Returns true/false depending on whether the command is configured properly.
Instance Attribute Details
#done_validating ⇒ Object (readonly)
Returns the value of attribute done_validating.
19 20 21 |
# File 'lib/golden_brindle/command.rb', line 19 def done_validating @done_validating end |
#original_args ⇒ Object (readonly)
Returns the value of attribute original_args.
19 20 21 |
# File 'lib/golden_brindle/command.rb', line 19 def original_args @original_args end |
#valid ⇒ Object (readonly)
Returns the value of attribute valid.
19 20 21 |
# File 'lib/golden_brindle/command.rb', line 19 def valid @valid end |
Instance Method Details
#can_change_user? ⇒ Boolean
131 132 133 |
# File 'lib/golden_brindle/command.rb', line 131 def can_change_user? valid?(Process.euid.to_i == 0, "if you want to change workers UID/GID you must run script from root") end |
#config_keys ⇒ Object
65 66 67 68 |
# File 'lib/golden_brindle/command.rb', line 65 def config_keys @config_keys ||= %w(address host port cwd log_file pid_file environment servers daemon debug config_script workers timeout user group prefix preload listen bundler) end |
#configure ⇒ Object
61 62 63 |
# File 'lib/golden_brindle/command.rb', line 61 def configure [] end |
#failure(message) ⇒ Object
Just a simple method to display failure until something better is developed.
157 158 159 |
# File 'lib/golden_brindle/command.rb', line 157 def failure() STDERR.puts "!!! #{}" end |
#help ⇒ Object
Returns a help message. Defaults to OptionParser#help which should be good.
97 98 99 |
# File 'lib/golden_brindle/command.rb', line 97 def help @opt.help end |
#initialize(options = {}) ⇒ Object
Called by the subclass to setup the command and parse the argv arguments. The call is destructive on argv since it uses the OptionParser#parse! function.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/golden_brindle/command.rb', line 36 def initialize(={}) argv = [:argv] || [] @opt = OptionParser.new @opt. = GoldenBrindle::Const::BANNER @valid = true # this is retarded, but it has to be done this way because -h and -v exit @done_validating = false @original_args = argv.dup configure # I need to add my own -h definition to prevent the -h by default from exiting. @opt.on_tail("-h", "--help", "Show this message") do @done_validating = true puts @opt end # I need to add my own -v definition to prevent the -v from exiting by default as well. @opt.on_tail("--version", "Show version") do @done_validating = true if VERSION puts "Version #{GoldenBrindle::Const::VERSION}" end end @opt.parse! argv end |
#load_config ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/golden_brindle/command.rb', line 70 def load_config settings = {} begin settings = YAML.load_file(@config_file) ensure STDERR.puts "** Loading settings from #{@config_file} (they override command line)." unless @daemon || settings[:daemon] end # Config file settings will override command line settings settings.each do |key, value| key = key.to_s if config_keys.include?(key) key = 'address' if key == 'host' self.instance_variable_set("@#{key}", value) else failure "Unknown configuration setting: #{key}" @valid = false end end end |
#options(opts) ⇒ Object
Called by the implemented command to set the options for that command. Every option has a short and long version, a description, a variable to set, and a default value. No exceptions.
24 25 26 27 28 29 30 31 32 |
# File 'lib/golden_brindle/command.rb', line 24 def (opts) # process the given options array opts.each do |short, long, help, variable, default| self.instance_variable_set(variable, default) @opt.on(short, long, help) do |arg| self.instance_variable_set(variable, arg) end end end |
#run ⇒ Object
Runs the command doing it’s job. You should implement this otherwise it will throw a NotImplementedError as a reminder.
103 104 105 |
# File 'lib/golden_brindle/command.rb', line 103 def run raise NotImplementedError end |
#valid?(exp, message) ⇒ Boolean
Validates the given expression is true and prints the message if not, exiting.
108 109 110 111 112 113 114 |
# File 'lib/golden_brindle/command.rb', line 108 def valid?(exp, ) if !@done_validating && !exp failure @valid = false @done_validating = true end end |
#valid_dir?(file, message) ⇒ Boolean
Validates that the given directory exists
127 128 129 |
# File 'lib/golden_brindle/command.rb', line 127 def valid_dir?(file, ) valid?(File.directory?(file), ) end |
#valid_exists?(file, message) ⇒ Boolean
Validates that a file exists and if not displays the message
117 118 119 |
# File 'lib/golden_brindle/command.rb', line 117 def valid_exists?(file, ) valid?(File.exist?(file), ) end |
#valid_file?(file, message) ⇒ Boolean
Validates that the file is a file and not a directory or something else.
122 123 124 |
# File 'lib/golden_brindle/command.rb', line 122 def valid_file?(file, ) valid?(File.file?(file), ) end |
#valid_group?(group) ⇒ Boolean
146 147 148 149 150 151 152 153 154 |
# File 'lib/golden_brindle/command.rb', line 146 def valid_group?(group) valid?(@user, "You must also specify a user.") begin Etc.getgrnam(group) rescue failure "Group does not exist: #{group}" @valid = false end end |
#valid_user?(user) ⇒ Boolean
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/golden_brindle/command.rb', line 135 def valid_user?(user) valid?(@group, "You must also specify a group.") can_change_user? begin Etc.getpwnam(user) rescue failure "User does not exist: #{user}" @valid = false end end |
#validate ⇒ Object
Returns true/false depending on whether the command is configured properly.
92 93 94 |
# File 'lib/golden_brindle/command.rb', line 92 def validate @valid end |