Class: StellarCoreBackup::Job

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

Defined Under Namespace

Classes: NoConfig

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Job

Returns a new instance of Job.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/stellar-core-backup/job.rb', line 6

def initialize(**args)
  if args.has_key?(:config) then
    @config       = StellarCoreBackup::Config.new(args[:config])
  else
    puts "info: no config provided"
    raise NoConfig
  end

  # Set run time options
  @verify   = args[:verify] if args.has_key?(:verify)
  @clean    = args[:clean] if args.has_key?(:clean)
  @listlen  = args[:listlen]

  # Set common run time parameters
  @job_type     = args[:type]
  @gpg_key      = @config.get('gpg_key')
  @working_dir  = StellarCoreBackup::Utils.create_working_dir(@config.get('working_dir'))
  @cmd          = StellarCoreBackup::Cmd.new(@working_dir)
  @select       = args[:select] if args.has_key?(:select)
  @s3           = StellarCoreBackup::S3.new(@config)

  # Set per operation type run time parameters
  if args.has_key?(:type) then
    case args[:type]
      when 'backup'
        puts 'info: backing up stellar-core'
        @backup_dir   = StellarCoreBackup::Utils.create_backup_dir(@config.get('backup_dir'))
        @db           = StellarCoreBackup::Database.new(@config)
        @fs           = StellarCoreBackup::Filesystem.new(@config)
      when 'restore'
        puts 'info: restoring stellar-core'
        @db_restore   = StellarCoreBackup::Restore::Database.new(@config)
        @fs_restore   = StellarCoreBackup::Restore::Filesystem.new(@config)
        @utils        = StellarCoreBackup::Utils.new(@config)
      when 'getkey'
        puts 'info: confirming public gpg key with key server'
      when 'list'
        puts "info: listing last #{@listlen} stellar-core backups"
    end
  end
end

Instance Method Details

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/stellar-core-backup/job.rb', line 48

def run()
  case @job_type
    when 'getkey'
      begin
        getkey = @cmd.run_and_capture('gpg', ['--keyserver', 'hkp://ipv4.pool.sks-keyservers.net', '--recv-key', @gpg_key, '2>&1'])
        puts 'info: public gpg key installed'
        if ! getkey.success then
          puts "error: failed to get gpg key"
          # dump the gpg output here for user level trouble shooting
          puts "#{getkey.out}"
          raise StandardError
        end
      rescue => e
        puts e
      end
    when 'list'
      begin
        list=@s3.latest(@listlen)
        puts "info: only #{list.length} backup files in bucket" if list.length < @listlen
        puts list
      rescue => e
        puts e
      end
    when 'backup'
      begin
        puts 'info: stopping stellar-core'
        # using sudo, if running as non root uid then you will need to configure sudoers
        stop_core = @cmd.run_and_capture('sudo', ['/bin/systemctl', 'stop', 'stellar-core'])
        # only proceed if core is stopped
        if stop_core.success then
          @db.backup
          @fs.backup
          if @verify then
            create_hash_file = @cmd.run_and_capture('find', ['.', '-type', 'f', '!', '-name', 'SHA256SUMS', '|', 'xargs', 'sha256sum', '>', 'SHA256SUMS'])
            if create_hash_file.success then
              puts "info: sha sums file created"
            else
              puts 'error: error creating sha sums file'
              raise StandardError
            end
            sign_hash_file = @cmd.run_and_capture('gpg', ['--local-user', @gpg_key, '--detach-sign', 'SHA256SUMS'])
            if sign_hash_file.success then
              puts "info: gpg signature created ok"
            else
              puts 'error: error signing sha256sum file'
              raise StandardError
            end
          end
          # create tar archive with fs, db backup files and if requested the file of shas256sums and corresponding gpg signature.
          @backup = StellarCoreBackup::Utils.create_backup_tar(@working_dir, @backup_dir)
          @s3.push(@backup)
        else
          puts 'error: can not stop stellar-core'
          raise StandardError
        end

        # restart stellar-core post backup
        puts 'info: starting stellar-core'
        # using sudo, if running as non root uid then you will need to configure sudoers
        start_core = @cmd.run_and_capture('sudo', ['/bin/systemctl', 'start', 'stellar-core'])
        if start_core.success then
          puts "info: stellar-core started"
        else
          puts 'error: can not start stellar-core'
          raise StandardError
        end

        # clean up working_dir
        StellarCoreBackup::Utils.cleanup(@working_dir)
      rescue => e
        puts e
        # clean up working_dir
        StellarCoreBackup::Utils.cleanup(@working_dir)
      end
    when 'restore'
      begin
        # confirm the bucket directory is set to be cleaned or is empty
        # the fs_restore.core_data_dir_empty? throws an exception if it's not empty
        if ! @clean then
          @fs_restore.core_data_dir_empty?()
        end
        # using sudo, if running as non root uid then you will need to configure sudoers
        stop_core = @cmd.run_and_capture('sudo', ['/bin/systemctl', 'stop', 'stellar-core'])
        # only proceed if core is stopped
        if stop_core.success then
          # if no manual selection has been made, use the latest as derived from the s3.latest method
          # this method returns an array so set @select to the first and only element
          @select=@s3.latest(1)[0] if ! @select
          @backup_archive = @s3.get(@select)
          @utils.extract_backup(@backup_archive)
          if @verify then
            verify_hash_file = @cmd.run_and_capture('gpg', ['--local-user', @gpg_key, '--verify', 'SHA256SUMS.sig', 'SHA256SUMS', '2>&1'])
            if verify_hash_file.success then
              puts "info: gpg signature processed ok"
            else
              puts 'error: error verifying gpg signature'
              raise StandardError
            end
            verify_sha_file_content = @cmd.run_and_capture('sha256sum', ['--status', '--strict', '-c', 'SHA256SUMS'])
            if verify_sha_file_content.success then
              puts "info: sha file sums match"
            else
              puts 'error: error processing sha256sum file'
              raise StandardError
            end
            if StellarCoreBackup::Utils.confirm_shasums_definitive(@working_dir, @backup_archive) then
              puts 'info: SHA256SUMS file list matches delivered archive'
            else
              puts 'error: unknown additional file(s) detected in archive'
              raise StandardError
            end
          end
          StellarCoreBackup::Utils.cleanbucket(@fs_restore.core_data_dir) if @clean
          @fs_restore.restore(@backup_archive)
          @db_restore.restore()

          # restart stellar-core post restore
          puts 'info: starting stellar-core'
          # using sudo, if running as non root uid then you will need to configure sudoers
          start_core = @cmd.run_and_capture('sudo', ['/bin/systemctl', 'start', 'stellar-core'])
          if start_core.success then
            puts "info: stellar-core started"
          else
            puts 'error: can not start stellar-core'
            raise StandardError
          end
        else
          puts 'error: can not stop stellar-core'
          raise StandardError
        end

        # clean up working_dir
        StellarCoreBackup::Utils.cleanup(@working_dir)
      rescue => e
        puts e
        # clean up working_dir
        StellarCoreBackup::Utils.cleanup(@working_dir)
      end
  end
end