Skip to content

Commit

Permalink
delete backups in storage after one month of db deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
var77 committed Aug 30, 2024
1 parent 0140ec1 commit 7390294
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
26 changes: 26 additions & 0 deletions lib/hosting/gcp_apis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,30 @@ def remove_ip_from_firewall(firewall_name, ip_address)
response = connection.put(path: "/compute/v1/projects/#{@project}/global/firewalls/#{firewall_name}", body: JSON.dump(firewall_rule), expects: [200, 400])
Hosting::GcpApis.check_errors(response)
end

def add_delete_lifecycle_rule(bucket, prefix)
connection = Excon.new("https://storage.googleapis.com", headers: @host[:headers])
query = {fields: "lifecycle"}

response = connection.get(path: "/storage/v1/b/#{bucket}", query: query, expects: [200, 400])
Hosting::GcpApis.check_errors(response)
data = JSON.parse(response.body)

lifecycle_rule = {
"action" => {"type" => "Delete"},
"condition" => {
age: 0,
matchesPrefix: [prefix]
}
}

if data.empty?
data = {"lifecycle" => {"rule" => [lifecycle_rule]}}
else
data["lifecycle"]["rule"].push(lifecycle_rule)
end

response = connection.patch(path: "/storage/v1/b/#{bucket}", query: query, body: JSON.dump(data), expects: [200, 400])
Hosting::GcpApis.check_errors(response)
end
end
9 changes: 6 additions & 3 deletions prog/lantern/lantern_timeline_nexus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,17 @@ def before_run
end

label def destroy
decr_destroy
when_destroy_set? do
decr_destroy
nap 60 * 60 * 24 * 30 # 30 days
end

destroy_blob_storage
lantern_timeline.destroy
pop "lantern timeline is deleted"
end

def destroy_blob_storage
# TODO
# Remove all backups
lantern_timeline.blob_storage_client.add_delete_lifecycle_rule(Config.lantern_backup_bucket, lantern_timeline.ubid)
end
end
22 changes: 22 additions & 0 deletions spec/lib/hosting/gcp_apis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -530,5 +530,27 @@
expect { api.remove_ip_from_firewall("my-firewall-rule", "203.0.113.0/24") }.not_to raise_error
end
end

describe "#add_delete_lifecycle_rule" do
it "adds lifecycle rule on empty object" do
stub_request(:post, "https://oauth2.googleapis.com/token").to_return(status: 200, body: JSON.dump({}), headers: {"Content-Type" => "application/json"})
stub_request(:get, "https://storage.googleapis.com/storage/v1/b/test?fields=lifecycle").to_return(status: 200, body: JSON.dump({}), headers: {"Content-Type" => "application/json"})
stub_request(:patch, "https://storage.googleapis.com/storage/v1/b/test?fields=lifecycle")
.with(body: JSON.dump({"lifecycle" => {"rule" => [{"action" => {"type" => "Delete"}, "condition" => {age: 0, matchesPrefix: ["test-ubid"]}}]}}))
.to_return(status: 200, body: JSON.dump({}), headers: {"Content-Type" => "application/json"})
api = described_class.new
expect { api.add_delete_lifecycle_rule("test", "test-ubid") }.not_to raise_error
end

it "adds lifecycle rule on existing object" do
stub_request(:post, "https://oauth2.googleapis.com/token").to_return(status: 200, body: JSON.dump({}), headers: {"Content-Type" => "application/json"})
stub_request(:get, "https://storage.googleapis.com/storage/v1/b/test?fields=lifecycle").to_return(status: 200, body: JSON.dump({"lifecycle" => {"rule" => [{"action" => {"type" => "Delete"}, "condition" => {age: 0, matchesPrefix: ["test-ubid2"]}}]}}), headers: {"Content-Type" => "application/json"})
stub_request(:patch, "https://storage.googleapis.com/storage/v1/b/test?fields=lifecycle")
.with(body: JSON.dump({"lifecycle" => {"rule" => [{"action" => {"type" => "Delete"}, "condition" => {age: 0, matchesPrefix: ["test-ubid2"]}}, {"action" => {"type" => "Delete"}, "condition" => {age: 0, matchesPrefix: ["test-ubid"]}}]}}))
.to_return(status: 200, body: JSON.dump({}), headers: {"Content-Type" => "application/json"})
api = described_class.new
expect { api.add_delete_lifecycle_rule("test", "test-ubid") }.not_to raise_error
end
end
end
end
17 changes: 16 additions & 1 deletion spec/prog/lantern/lantern_timeline_nexus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,24 @@
end

describe "#destroy" do
it "exits with message" do
it "naps for one month" do
expect(nx).to receive(:when_destroy_set?).and_yield
expect { nx.destroy }.to nap(60 * 60 * 24 * 30)
end

it "destroys storage" do
expect(nx).to receive(:destroy_blob_storage)
expect(lantern_server.timeline).to receive(:destroy)
expect { nx.destroy }.to exit({"msg" => "lantern timeline is deleted"})
end
end

describe "#destroy_blob_storage" do
it "deletes objects" do
api = instance_double(Hosting::GcpApis)
expect(lantern_server.timeline).to receive(:blob_storage_client).and_return(api)
expect(api).to receive(:add_delete_lifecycle_rule).with(Config.lantern_backup_bucket, lantern_server.timeline.ubid)
expect { nx.destroy_blob_storage }.not_to raise_error
end
end
end

0 comments on commit 7390294

Please sign in to comment.