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

Instance Method Details

#all_servicesObject



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

Returns:

  • (Boolean)


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

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

#base_config_dirObject



104
105
106
# File 'lib/chef_backup/helpers.rb', line 104

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

#base_install_dirObject



100
101
102
# File 'lib/chef_backup/helpers.rb', line 100

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

#chpstObject



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

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

#cleanupObject



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

#configObject



44
45
46
# File 'lib/chef_backup/helpers.rb', line 44

def config
  ChefBackup::Config
end

#config_baseObject



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

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

#ctl_commandObject



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

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

#database_nameObject



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

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

#disabled_servicesObject



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

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

#enabled_addonsObject



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_servicesObject



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

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)


79
80
81
# File 'lib/chef_backup/helpers.rb', line 79

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

#frontend?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/chef_backup/helpers.rb', line 197

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

#ha?Boolean

Returns:

  • (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(message, level = :info)
  ChefBackup::Logger.logger.log(message, level)
end

#marketplace?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


205
206
207
# File 'lib/chef_backup/helpers.rb', line 205

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

#pg_optionsObject



116
117
118
# File 'lib/chef_backup/helpers.rb', line 116

def pg_options
  config['pg_options'] || DEFAULT_PG_OPTIONS
end

#pgsqlObject



112
113
114
# File 'lib/chef_backup/helpers.rb', line 112

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

#project_nameObject



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

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

#reconfigure_add_onsObject



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_marketplaceObject



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_onsObject



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_serverObject



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

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

#running_filepathObject



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

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

#service_configObject



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

def service_config
  ChefBackup::Config[config_base]
end

#service_enabled?(service) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


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

def standalone?
  topology == 'standalone'
end

#start_chef_serverObject



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

#strategyObject



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

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

#tier?Boolean

Returns:

  • (Boolean)


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

def tier?
  topology == 'tier'
end

#tmp_dirObject



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

#topologyObject



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

def topology
  service_config['topology']
end