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/#{ChefUtils::Dist::Org::LEGACY_CONF_DIR}/#{ChefUtils::Dist::Server::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

Instance Method Details

#addon_install_dir(name) ⇒ Object



122
123
124
125
# File 'lib/chef_backup/helpers.rb', line 122

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_servicesObject



145
146
147
# File 'lib/chef_backup/helpers.rb', line 145

def all_services
  Dir["#{base_install_dir}/sv/*"].map { |f| File.basename(f) }.sort
end

#backend?Boolean

Returns:

  • (Boolean)


224
225
226
# File 'lib/chef_backup/helpers.rb', line 224

def backend?
  service_config['role'] =~ /backend|standalone/
end

#base_config_dirObject



127
128
129
# File 'lib/chef_backup/helpers.rb', line 127

def base_config_dir
  "/etc/#{project_name}"
end

#base_install_dirObject



118
119
120
# File 'lib/chef_backup/helpers.rb', line 118

def base_install_dir
  "/opt/#{project_name}"
end

#chpstObject



131
132
133
# File 'lib/chef_backup/helpers.rb', line 131

def chpst
  "#{base_install_dir}/embedded/bin/chpst"
end

#cleanupObject



261
262
263
264
265
266
# File 'lib/chef_backup/helpers.rb', line 261

def cleanup
  log "Cleaning up #{tmp_dir}"
  FileUtils.rm_r(tmp_dir)
rescue Errno::ENOENT
  true
end

#configObject



47
48
49
# File 'lib/chef_backup/helpers.rb', line 47

def config
  ChefBackup::Config
end

#config_baseObject



51
52
53
# File 'lib/chef_backup/helpers.rb', line 51

def config_base
  ChefBackup::Config['config_base']
end

#ctl_commandObject



59
60
61
# File 'lib/chef_backup/helpers.rb', line 59

def ctl_command
  service_config['backup']['ctl-command']
end

#database_nameObject



67
68
69
# File 'lib/chef_backup/helpers.rb', line 67

def database_name
  service_config['backup']['database_name']
end

#disabled_servicesObject



153
154
155
# File 'lib/chef_backup/helpers.rb', line 153

def disabled_services
  all_services.reject { |sv| service_enabled?(sv) }
end

#enabled_addonsObject



204
205
206
207
208
209
210
# File 'lib/chef_backup/helpers.rb', line 204

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_servicesObject



149
150
151
# File 'lib/chef_backup/helpers.rb', line 149

def enabled_services
  all_services.select { |sv| service_enabled?(sv) }
end

#ensure_file!(file, exception, message) ⇒ TrueClass, FalseClass

Parameters:

  • file (String)

    A path to a file on disk

  • exception (Exception)

    An exception to raise if file is not present

  • message (String)

    Exception message to raise

Returns:

  • (TrueClass, FalseClass)


95
96
97
# File 'lib/chef_backup/helpers.rb', line 95

def ensure_file!(file, exception, message)
  File.exist?(file) ? true : raise(exception, message)
end

#frontend?Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/chef_backup/helpers.rb', line 220

def frontend?
  service_config['role'] == 'frontend'
end

#ha?Boolean

Returns:

  • (Boolean)


232
233
234
# File 'lib/chef_backup/helpers.rb', line 232

def ha?
  topology == 'ha'
end

#log(message, level = :info) ⇒ Object



71
72
73
# File 'lib/chef_backup/helpers.rb', line 71

def log(message, level = :info)
  ChefBackup::Logger.logger.log(message, level)
end

#marketplace?Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/chef_backup/helpers.rb', line 244

def marketplace?
  shell_out('which chef-marketplace-ctl').exitstatus == 0
end

#online?Boolean

Returns:

  • (Boolean)


228
229
230
# File 'lib/chef_backup/helpers.rb', line 228

def online?
  service_config['backup']['mode'] == 'online'
end

#pg_optionsObject



139
140
141
142
143
# File 'lib/chef_backup/helpers.rb', line 139

def pg_options
  config['pg_options'] ||
    (service_config && service_config['backup']['pg_options']) ||
    DEFAULT_PG_OPTIONS
end

#pgsqlObject



135
136
137
# File 'lib/chef_backup/helpers.rb', line 135

def pgsql
  "#{base_install_dir}/embedded/bin/psql"
end

#project_nameObject



114
115
116
# File 'lib/chef_backup/helpers.rb', line 114

def project_name
  service_config['backup']['project_name']
end

#reconfigure_add_onsObject



187
188
189
190
191
# File 'lib/chef_backup/helpers.rb', line 187

def reconfigure_add_ons
  enabled_addons.each do |_name, config|
    shell_out("#{config['ctl_command']} reconfigure") if config.key?('ctl_command')
  end
end

#reconfigure_marketplaceObject



199
200
201
202
# File 'lib/chef_backup/helpers.rb', line 199

def reconfigure_marketplace
  log 'Setting up Chef Marketplace'
  shell_out('chef-marketplace-ctl reconfigure')
end

#restart_add_onsObject



193
194
195
196
197
# File 'lib/chef_backup/helpers.rb', line 193

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_serverObject



183
184
185
# File 'lib/chef_backup/helpers.rb', line 183

def restart_chef_server
  shell_out("#{ctl_command} restart #{service}")
end

#running_filepathObject



63
64
65
# File 'lib/chef_backup/helpers.rb', line 63

def running_filepath
  service_config['backup']['running_filepath']
end

#service_configObject



55
56
57
# File 'lib/chef_backup/helpers.rb', line 55

def service_config
  ChefBackup::Config[config_base]
end

#service_enabled?(service) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/chef_backup/helpers.rb', line 157

def service_enabled?(service)
  File.symlink?("#{base_install_dir}/service/#{service}")
end

#shell_out(*command) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/chef_backup/helpers.rb', line 99

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) ⇒ Object



108
109
110
111
112
# File 'lib/chef_backup/helpers.rb', line 108

def shell_out!(*command)
  cmd = shell_out(*command)
  cmd.error!
  cmd
end

#shell_timeoutObject

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.



82
83
84
85
86
# File 'lib/chef_backup/helpers.rb', line 82

def shell_timeout
  option = config['shell_out_timeout'] ||
           (service_config && service_config['backup']['shell_out_timeout'])
  option.to_f unless option.nil?
end

#standalone?Boolean

Returns:

  • (Boolean)


240
241
242
# File 'lib/chef_backup/helpers.rb', line 240

def standalone?
  topology == 'standalone'
end

#start_chef_serverObject



178
179
180
181
# File 'lib/chef_backup/helpers.rb', line 178

def start_chef_server
  log 'Bringing up the Chef Server'
  enabled_services.each { |sv| start_service(sv) }
end

#start_service(service) ⇒ Object



166
167
168
169
# File 'lib/chef_backup/helpers.rb', line 166

def start_service(service)
  res = shell_out("#{ctl_command} start #{service}")
  res
end

#stop_chef_server(params = {}) ⇒ Object



171
172
173
174
175
176
# File 'lib/chef_backup/helpers.rb', line 171

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



161
162
163
164
# File 'lib/chef_backup/helpers.rb', line 161

def stop_service(service)
  res = shell_out("#{ctl_command} stop #{service}")
  res
end

#strategyObject



212
213
214
# File 'lib/chef_backup/helpers.rb', line 212

def strategy
  service_config['backup']['strategy']
end

#tier?Boolean

Returns:

  • (Boolean)


236
237
238
# File 'lib/chef_backup/helpers.rb', line 236

def tier?
  topology == 'tier'
end

#tmp_dirObject



248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/chef_backup/helpers.rb', line 248

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

#topologyObject



216
217
218
# File 'lib/chef_backup/helpers.rb', line 216

def topology
  service_config['topology']
end

#version_from_manifest_file(file) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/chef_backup/helpers.rb', line 268

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