Module: ChefBackup::Helpers
- Included in:
- Runner, Strategy::TarBackup, Strategy::TarRestore
- Defined in:
- lib/chef_backup/helpers.rb
Overview
Common helper methods that are usefull in many classes
Constant Summary collapse
- SERVER_ADD_ONS =
rubocop:enable IndentationWidth
{ 'opscode-manage' => { 'config_file' => '/etc/opscode-manage/manage.rb', 'ctl_command' => 'opscode-manage-ctl' }, 'opscode-reporting' => { 'config_file' => '/etc/opscode-reporting/opscode-reporting.rb', 'ctl_command' => 'opscode-reporting-ctl' }, 'opscode-push-jobs-server' => { 'config_file' => '/etc/opscode-push-jobs-server/opscode-push-jobs-server.rb', 'ctl_command' => 'opscode-push-jobs-server-ctl' }, 'opscode-analytics' => { 'config_file' => '/etc/opscode-analytics/opscode-analytics.rb', 'ctl_command' => 'opscode-analytics-ctl' }, 'chef-ha' => { 'config_file' => 'etc/opscode/chef-server.rb' }, 'chef-sync' => { 'config_file' => '/etc/chef-sync/chef-sync.rb', 'ctl_command' => 'chef-sync-ctl' }, 'chef-marketplace' => { 'config_file' => '/etc/chef-marketplace/marketplace.rb', 'ctl_command' => 'chef-marketplace-ctl' } }.freeze
- DEFAULT_PG_OPTIONS =
'-c statement_timeout=3600000'.freeze
Instance Method Summary collapse
- #all_services ⇒ Object
- #backend? ⇒ Boolean
- #base_config_dir ⇒ Object
- #base_install_dir ⇒ Object
- #chpst ⇒ Object
- #cleanup ⇒ Object
- #config ⇒ Object
- #config_base ⇒ Object
- #ctl_command ⇒ Object
- #database_name ⇒ Object
- #disabled_services ⇒ Object
- #enabled_addons ⇒ Object
- #enabled_services ⇒ Object
- #ensure_file!(file, exception, message) ⇒ TrueClass, FalseClass
- #frontend? ⇒ Boolean
- #ha? ⇒ Boolean
- #log(message, level = :info) ⇒ Object
- #marketplace? ⇒ Boolean
- #online? ⇒ Boolean
- #pg_options ⇒ Object
- #pgsql ⇒ Object
- #project_name ⇒ Object
- #reconfigure_add_ons ⇒ Object
- #reconfigure_marketplace ⇒ Object
- #restart_add_ons ⇒ Object
- #restart_chef_server ⇒ Object
- #running_filepath ⇒ Object
- #service_config ⇒ Object
- #service_enabled?(service) ⇒ Boolean
- #shell_out(*command) ⇒ Object
- #shell_out!(*command) ⇒ Object
- #standalone? ⇒ Boolean
- #start_chef_server ⇒ Object
- #start_service(service) ⇒ Object
- #stop_chef_server(params = {}) ⇒ Object
- #stop_service(service) ⇒ Object
- #strategy ⇒ Object
- #tier? ⇒ Boolean
- #tmp_dir ⇒ Object
- #topology ⇒ Object
Instance Method Details
#all_services ⇒ Object
120 121 122 |
# File 'lib/chef_backup/helpers.rb', line 120 def all_services Dir["#{base_install_dir}/sv/*"].map { |f| File.basename(f) }.sort end |
#backend? ⇒ Boolean
201 202 203 |
# File 'lib/chef_backup/helpers.rb', line 201 def backend? service_config['role'] =~ /backend|standalone/ end |
#base_config_dir ⇒ Object
104 105 106 |
# File 'lib/chef_backup/helpers.rb', line 104 def base_config_dir "/etc/#{project_name}" end |
#base_install_dir ⇒ Object
100 101 102 |
# File 'lib/chef_backup/helpers.rb', line 100 def base_install_dir "/opt/#{project_name}" end |
#chpst ⇒ Object
108 109 110 |
# File 'lib/chef_backup/helpers.rb', line 108 def chpst "#{base_install_dir}/embedded/bin/chpst" end |
#cleanup ⇒ Object
238 239 240 241 242 243 |
# File 'lib/chef_backup/helpers.rb', line 238 def cleanup log "Cleaning up #{tmp_dir}" FileUtils.rm_r(tmp_dir) rescue Errno::ENOENT true end |
#config ⇒ Object
44 45 46 |
# File 'lib/chef_backup/helpers.rb', line 44 def config ChefBackup::Config end |
#config_base ⇒ Object
48 49 50 |
# File 'lib/chef_backup/helpers.rb', line 48 def config_base ChefBackup::Config['config_base'] end |
#ctl_command ⇒ Object
56 57 58 |
# File 'lib/chef_backup/helpers.rb', line 56 def ctl_command service_config['backup']['ctl-command'] end |
#database_name ⇒ Object
64 65 66 |
# File 'lib/chef_backup/helpers.rb', line 64 def database_name service_config['backup']['database_name'] end |
#disabled_services ⇒ Object
128 129 130 |
# File 'lib/chef_backup/helpers.rb', line 128 def disabled_services all_services.select { |sv| !service_enabled?(sv) } end |
#enabled_addons ⇒ Object
179 180 181 182 183 184 185 186 187 |
# File 'lib/chef_backup/helpers.rb', line 179 def enabled_addons SERVER_ADD_ONS.select do |_name, config| begin File.directory?(File.dirname(config['config_file'])) rescue false end end end |
#enabled_services ⇒ Object
124 125 126 |
# File 'lib/chef_backup/helpers.rb', line 124 def enabled_services all_services.select { |sv| service_enabled?(sv) } end |
#ensure_file!(file, exception, message) ⇒ TrueClass, FalseClass
79 80 81 |
# File 'lib/chef_backup/helpers.rb', line 79 def ensure_file!(file, exception, ) File.exist?(file) ? true : raise(exception, ) end |
#frontend? ⇒ Boolean
197 198 199 |
# File 'lib/chef_backup/helpers.rb', line 197 def frontend? service_config['role'] == 'frontend' end |
#ha? ⇒ Boolean
209 210 211 |
# File 'lib/chef_backup/helpers.rb', line 209 def ha? topology == 'ha' end |
#log(message, level = :info) ⇒ Object
68 69 70 |
# File 'lib/chef_backup/helpers.rb', line 68 def log(, level = :info) ChefBackup::Logger.logger.log(, level) end |
#marketplace? ⇒ Boolean
221 222 223 |
# File 'lib/chef_backup/helpers.rb', line 221 def marketplace? shell_out('which chef-marketplace-ctl').exitstatus == 0 end |
#online? ⇒ Boolean
205 206 207 |
# File 'lib/chef_backup/helpers.rb', line 205 def online? service_config['backup']['mode'] == 'online' end |
#pg_options ⇒ Object
116 117 118 |
# File 'lib/chef_backup/helpers.rb', line 116 def config['pg_options'] || DEFAULT_PG_OPTIONS end |
#pgsql ⇒ Object
112 113 114 |
# File 'lib/chef_backup/helpers.rb', line 112 def pgsql "#{base_install_dir}/embedded/bin/psql" end |
#project_name ⇒ Object
96 97 98 |
# File 'lib/chef_backup/helpers.rb', line 96 def project_name service_config['backup']['project_name'] end |
#reconfigure_add_ons ⇒ Object
162 163 164 165 166 |
# File 'lib/chef_backup/helpers.rb', line 162 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 ⇒ Object
174 175 176 177 |
# File 'lib/chef_backup/helpers.rb', line 174 def reconfigure_marketplace log 'Setting up Chef Marketplace' shell_out('chef-marketplace-ctl reconfigure') end |
#restart_add_ons ⇒ Object
168 169 170 171 172 |
# File 'lib/chef_backup/helpers.rb', line 168 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 ⇒ Object
158 159 160 |
# File 'lib/chef_backup/helpers.rb', line 158 def restart_chef_server shell_out("#{ctl_command} restart #{service}") end |
#running_filepath ⇒ Object
60 61 62 |
# File 'lib/chef_backup/helpers.rb', line 60 def running_filepath service_config['backup']['running_filepath'] end |
#service_config ⇒ Object
52 53 54 |
# File 'lib/chef_backup/helpers.rb', line 52 def service_config ChefBackup::Config[config_base] end |
#service_enabled?(service) ⇒ Boolean
132 133 134 |
# File 'lib/chef_backup/helpers.rb', line 132 def service_enabled?(service) File.symlink?("#{base_install_dir}/service/#{service}") end |
#shell_out(*command) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/chef_backup/helpers.rb', line 83 def shell_out(*command) cmd = Mixlib::ShellOut.new(*command) cmd.live_stream ||= $stdout.tty? ? $stdout : nil cmd.run_command cmd end |
#shell_out!(*command) ⇒ Object
90 91 92 93 94 |
# File 'lib/chef_backup/helpers.rb', line 90 def shell_out!(*command) cmd = shell_out(*command) cmd.error! cmd end |
#standalone? ⇒ Boolean
217 218 219 |
# File 'lib/chef_backup/helpers.rb', line 217 def standalone? topology == 'standalone' end |
#start_chef_server ⇒ Object
153 154 155 156 |
# File 'lib/chef_backup/helpers.rb', line 153 def start_chef_server log 'Bringing up the Chef Server' enabled_services.each { |sv| start_service(sv) } end |
#start_service(service) ⇒ Object
141 142 143 144 |
# File 'lib/chef_backup/helpers.rb', line 141 def start_service(service) res = shell_out("#{ctl_command} start #{service}") res end |
#stop_chef_server(params = {}) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/chef_backup/helpers.rb', line 146 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) ⇒ Object
136 137 138 139 |
# File 'lib/chef_backup/helpers.rb', line 136 def stop_service(service) res = shell_out("#{ctl_command} stop #{service}") res end |
#strategy ⇒ Object
189 190 191 |
# File 'lib/chef_backup/helpers.rb', line 189 def strategy service_config['backup']['strategy'] end |
#tier? ⇒ Boolean
213 214 215 |
# File 'lib/chef_backup/helpers.rb', line 213 def tier? topology == 'tier' end |
#tmp_dir ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/chef_backup/helpers.rb', line 225 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 ⇒ Object
193 194 195 |
# File 'lib/chef_backup/helpers.rb', line 193 def topology service_config['topology'] end |