Class: StellarCoreBackup::Utils

Inherits:
Object
  • Object
show all
Includes:
Contracts
Defined in:
lib/stellar-core-backup/utils.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Utils

Returns a new instance of Utils.



12
13
14
15
16
17
# File 'lib/stellar-core-backup/utils.rb', line 12

def initialize(config)
  @config       = config
  @working_dir  = StellarCoreBackup::Utils.create_working_dir(@config.get('working_dir'))
  @db_restore   = StellarCoreBackup::Restore::Database.new(@config)
  @fs_restore   = StellarCoreBackup::Restore::Filesystem.new(@config)
end

Class Method Details

.cleanbucket(bucket_dir) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/stellar-core-backup/utils.rb', line 67

def self.cleanbucket(bucket_dir)
  if FileUtils.remove(Dir.glob(bucket_dir+'/*')) then
    puts 'info: cleaning up workspace'
    return true
  else
    return false
  end
end

.cleanup(working_dir) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/stellar-core-backup/utils.rb', line 77

def self.cleanup(working_dir)
  if FileUtils.remove_dir(working_dir) then
    puts 'info: cleaning up workspace'
    return true
  else
    return false
  end
end

.confirm_shasums_definitive(working_dir, backup_archive) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/stellar-core-backup/utils.rb', line 87

def self.confirm_shasums_definitive(working_dir, backup_archive)

  # create an array of filesunpacked into the working_dir
  Dir.chdir(working_dir)
  files_present=Dir.glob('./**/*')

  # remove directories and shasum details from file array
  files_present.delete('./'+File.basename(backup_archive))
  files_present.delete('./core-db')
  files_present.delete('./SHA256SUMS')
  files_present.delete('./SHA256SUMS.sig')

  # now delete the file names in the shasums file from the array
  # we are expecting an array of zero length after this process
  File.open("SHA256SUMS").each { |sha_file| files_present.delete(sha_file.split(' ')[1].chomp) }
  if files_present.none? then
    return true
  else
    return false
  end
end

.core_healthy?(config) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/stellar-core-backup/utils.rb', line 142

def self.core_healthy?(config)
  port = get_admin_port(config.get('core_config'))
  url = "http://127.0.0.1:%s/info" % port
  uri = URI(url)
  begin
    response = Net::HTTP.get(uri)
    state = JSON.parse(response)['info']['state']
    if state == 'Synced!' then
      puts "info: stellar-core up and synced"
      return true
    else
      puts "error: stellar-core status is: %s" % state
      return false
    end
  rescue
    puts "info: stellar-core down or not synced"
    return false
  end
end

.create_backup_dir(dir) ⇒ Object



36
37
38
39
40
41
# File 'lib/stellar-core-backup/utils.rb', line 36

def self.create_backup_dir(dir)
  unless Dir.exists?(dir) then
    Dir.mkdir dir
  end
  return dir
end

.create_backup_tar(working_dir, backup_dir) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/stellar-core-backup/utils.rb', line 44

def self.create_backup_tar(working_dir, backup_dir)
  puts 'info: creating backup tarball'
  tar_file = "#{backup_dir}/core-backup-#{Time.now.to_i}.tar"
  Dir.chdir(working_dir)
  # archive the working directory
  StellarCoreBackup::Tar.pack(tar_file, '.')
  return tar_file
end

.create_working_dir(dir) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/stellar-core-backup/utils.rb', line 20

def self.create_working_dir(dir)
  working_dir = dir + "/#{Process.pid}"
  unless Dir.exists?(working_dir) then
    Dir.mkdir working_dir
  end
  return working_dir
end

.num_cores?Boolean

Returns:

  • (Boolean)


135
136
137
138
# File 'lib/stellar-core-backup/utils.rb', line 135

def self.num_cores?()
  require 'etc'
  return Etc.nprocessors
end

.readable?(file) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
# File 'lib/stellar-core-backup/utils.rb', line 111

def self.readable?(file)
  if File.readable?(file) then
    puts "info: #{file} readable"
    return true
  else
    puts "error: cannot read #{file}"
    raise Errno::EACCES
  end
end

.remove_working_dir(working_dir) ⇒ Object



29
30
31
32
33
# File 'lib/stellar-core-backup/utils.rb', line 29

def self.remove_working_dir(working_dir)
  if Dir.exists?(working_dir) then
    Dir.rmdir working_dir + "/#{Process.pid}"
  end
end

.writable?(file) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
128
129
130
131
# File 'lib/stellar-core-backup/utils.rb', line 123

def self.writable?(file)
  if File.writable?(file) then
    puts "info: #{file} writeable"
    return true
  else
    puts "error: cannot write to #{file}"
    raise Errno::EACCES
  end
end

Instance Method Details

#extract_backup(backup_archive) ⇒ Object



54
55
56
57
58
# File 'lib/stellar-core-backup/utils.rb', line 54

def extract_backup(backup_archive)
  # extract the backup archive into the working directory
  StellarCoreBackup::Tar.unpack(backup_archive, @working_dir)
  return
end