module ChefBackup::Helpers
Common helper methods that are usefull in many classes
Constants
- DEFAULT_PG_OPTIONS
- SERVER_ADD_ONS
rubocop:enable IndentationWidth
Public Instance Methods
addon_install_dir(name)
click to toggle source
# File lib/chef_backup/helpers.rb, line 121 def addon_install_dir(name) # can use extra field in SERVER_ADD_ONS to extend if someone isn't following this pattern. "/opt/#{name}" end
all_services()
click to toggle source
# File lib/chef_backup/helpers.rb, line 144 def all_services Dir["#{base_install_dir}/sv/*"].map { |f| File.basename(f) }.sort end
backend?()
click to toggle source
# File lib/chef_backup/helpers.rb, line 223 def backend? service_config['role'] =~ /backend|standalone/ end
base_config_dir()
click to toggle source
# File lib/chef_backup/helpers.rb, line 126 def base_config_dir "/etc/#{project_name}" end
base_install_dir()
click to toggle source
# File lib/chef_backup/helpers.rb, line 117 def base_install_dir "/opt/#{project_name}" end
chpst()
click to toggle source
# File lib/chef_backup/helpers.rb, line 130 def chpst "#{base_install_dir}/embedded/bin/chpst" end
cleanup()
click to toggle source
# File lib/chef_backup/helpers.rb, line 260 def cleanup log "Cleaning up #{tmp_dir}" FileUtils.rm_r(tmp_dir) rescue Errno::ENOENT true end
config()
click to toggle source
# File lib/chef_backup/helpers.rb, line 46 def config ChefBackup::Config end
config_base()
click to toggle source
# File lib/chef_backup/helpers.rb, line 50 def config_base ChefBackup::Config['config_base'] end
ctl_command()
click to toggle source
# File lib/chef_backup/helpers.rb, line 58 def ctl_command service_config['backup']['ctl-command'] end
database_name()
click to toggle source
# File lib/chef_backup/helpers.rb, line 66 def database_name service_config['backup']['database_name'] end
disabled_services()
click to toggle source
# File lib/chef_backup/helpers.rb, line 152 def disabled_services all_services.reject { |sv| service_enabled?(sv) } end
enabled_addons()
click to toggle source
# File lib/chef_backup/helpers.rb, line 203 def enabled_addons SERVER_ADD_ONS.select do |name, config| !config['config_file'].nil? && File.directory?(File.dirname(config['config_file'])) && File.directory?(addon_install_dir(name)) end end
enabled_services()
click to toggle source
# File lib/chef_backup/helpers.rb, line 148 def enabled_services all_services.select { |sv| service_enabled?(sv) } end
ensure_file!(file, exception, message)
click to toggle source
@param file [String] A path to a file on disk @param exception [Exception] An exception to raise if file is not present @param message [String] Exception message to raise
@return [TrueClass, FalseClass]
# File lib/chef_backup/helpers.rb, line 94 def ensure_file!(file, exception, message) File.exist?(file) ? true : raise(exception, message) end
frontend?()
click to toggle source
# File lib/chef_backup/helpers.rb, line 219 def frontend? service_config['role'] == 'frontend' end
ha?()
click to toggle source
# File lib/chef_backup/helpers.rb, line 231 def ha? topology == 'ha' end
log(message, level = :info)
click to toggle source
# File lib/chef_backup/helpers.rb, line 70 def log(message, level = :info) ChefBackup::Logger.logger.log(message, level) end
marketplace?()
click to toggle source
# File lib/chef_backup/helpers.rb, line 243 def marketplace? shell_out('which chef-marketplace-ctl').exitstatus == 0 end
online?()
click to toggle source
# File lib/chef_backup/helpers.rb, line 227 def online? service_config['backup']['mode'] == 'online' end
pg_options()
click to toggle source
# File lib/chef_backup/helpers.rb, line 138 def pg_options config['pg_options'] || (service_config && service_config['backup']['pg_options']) || DEFAULT_PG_OPTIONS end
pgsql()
click to toggle source
# File lib/chef_backup/helpers.rb, line 134 def pgsql "#{base_install_dir}/embedded/bin/psql" end
project_name()
click to toggle source
# File lib/chef_backup/helpers.rb, line 113 def project_name service_config['backup']['project_name'] end
reconfigure_add_ons()
click to toggle source
# File lib/chef_backup/helpers.rb, line 186 def reconfigure_add_ons enabled_addons.each do |_name, config| shell_out("#{config['ctl_command']} reconfigure") if config.key?('ctl_command') end end
reconfigure_marketplace()
click to toggle source
# File lib/chef_backup/helpers.rb, line 198 def reconfigure_marketplace log 'Setting up Chef Marketplace' shell_out('chef-marketplace-ctl reconfigure') end
restart_add_ons()
click to toggle source
# File lib/chef_backup/helpers.rb, line 192 def restart_add_ons enabled_addons.each do |_name, config| shell_out("#{config['ctl_command']} restart") if config.key?('ctl_command') end end
restart_chef_server()
click to toggle source
# File lib/chef_backup/helpers.rb, line 182 def restart_chef_server shell_out("#{ctl_command} restart #{service}") end
running_filepath()
click to toggle source
# File lib/chef_backup/helpers.rb, line 62 def running_filepath service_config['backup']['running_filepath'] end
service_config()
click to toggle source
# File lib/chef_backup/helpers.rb, line 54 def service_config ChefBackup::Config[config_base] end
service_enabled?(service)
click to toggle source
# File lib/chef_backup/helpers.rb, line 156 def service_enabled?(service) File.symlink?("#{base_install_dir}/service/#{service}") end
shell_out(*command)
click to toggle source
# File lib/chef_backup/helpers.rb, line 98 def shell_out(*command) options = command.last.is_a?(Hash) ? command.pop : {} opts_with_defaults = { 'timeout' => shell_timeout }.merge(options) cmd = Mixlib::ShellOut.new(*command, opts_with_defaults) cmd.live_stream ||= $stdout.tty? ? $stdout : nil cmd.run_command cmd end
shell_out!(*command)
click to toggle source
# File lib/chef_backup/helpers.rb, line 107 def shell_out!(*command) cmd = shell_out(*command) cmd.error! cmd end
shell_timeout()
click to toggle source
Note that when we are in the backup codepath, we have access to a running chef server and hence, the ctl command puts all our flags under the current running service namespace. The lets the default configuration of the server provide flags that the user doesn't necessarily provide on the command line.
During the restore codepath, there may be no running chef server. This means that we need to be paranoid about the existence of the service_config
hash.
# File lib/chef_backup/helpers.rb, line 81 def shell_timeout option = config['shell_out_timeout'] || (service_config && service_config['backup']['shell_out_timeout']) option.to_f unless option.nil? end
standalone?()
click to toggle source
# File lib/chef_backup/helpers.rb, line 239 def standalone? topology == 'standalone' end
start_chef_server()
click to toggle source
# File lib/chef_backup/helpers.rb, line 177 def start_chef_server log 'Bringing up the Chef Server' enabled_services.each { |sv| start_service(sv) } end
start_service(service)
click to toggle source
# File lib/chef_backup/helpers.rb, line 165 def start_service(service) res = shell_out("#{ctl_command} start #{service}") res end
stop_chef_server(params = {})
click to toggle source
# File lib/chef_backup/helpers.rb, line 170 def stop_chef_server(params = {}) log 'Bringing down the Chef Server' services = enabled_services services -= params[:except].map(&:to_s) if params.key?(:except) services.each { |sv| stop_service(sv) } end
stop_service(service)
click to toggle source
# File lib/chef_backup/helpers.rb, line 160 def stop_service(service) res = shell_out("#{ctl_command} stop #{service}") res end
strategy()
click to toggle source
# File lib/chef_backup/helpers.rb, line 211 def strategy service_config['backup']['strategy'] end
tier?()
click to toggle source
# File lib/chef_backup/helpers.rb, line 235 def tier? topology == 'tier' end
tmp_dir()
click to toggle source
# File lib/chef_backup/helpers.rb, line 247 def tmp_dir @tmp_dir ||= begin dir = safe_key { config['tmp_dir'] } || safe_key { service_config['backup']['tmp_dir'] } if dir FileUtils.mkdir_p(dir) unless File.directory?(dir) dir else Dir.mktmpdir('chef_backup') end end end
topology()
click to toggle source
# File lib/chef_backup/helpers.rb, line 215 def topology service_config['topology'] end
version_from_manifest_file(file)
click to toggle source
# File lib/chef_backup/helpers.rb, line 267 def version_from_manifest_file(file) return :no_version if file.nil? path = File.expand_path(file) if File.exist?(path) config = JSON.parse(File.read(path)) { 'version' => config['build_version'], 'revision' => config['build_git_revision'], 'path' => path } else :no_version end end
Private Instance Methods
safe_key() { || ... }
click to toggle source
# File lib/chef_backup/helpers.rb, line 283 def safe_key yield rescue NameError nil end