From 06e497cd0fabc7d256065ea5facd5ece9b5f01d2 Mon Sep 17 00:00:00 2001 From: Varik Matevosyan Date: Tue, 23 Jul 2024 18:50:00 +0400 Subject: [PATCH] set domain only after creating dns record --- config.rb | 2 +- model/lantern/lantern_server.rb | 2 +- prog/lantern/lantern_server_nexus.rb | 22 ++++++++++++++----- routes/api/project/location/lantern.rb | 5 +++++ routes/web/project/location/lantern.rb | 6 ++++- .../prog/lantern/lantern_server_nexus_spec.rb | 15 +++++++++---- spec/routes/api/project/lantern_spec.rb | 14 +++++++----- .../web/project/location/lantern_spec.rb | 2 +- 8 files changed, 48 insertions(+), 20 deletions(-) diff --git a/config.rb b/config.rb index ffdab8fe6..d1db590da 100644 --- a/config.rb +++ b/config.rb @@ -149,7 +149,7 @@ def self.e2e_test? override :lantern_dns_email, "varik@lantern.dev", string override :lantern_default_version, "0.3.1", string override :lantern_extras_default_version, "0.2.3", string - override :lantern_minor_default_version, "1", string + override :lantern_minor_default_version, "2", string override :lantern_backup_bucket, "walg-dev-backups" override :e2e_test, "0" override :backup_retention_days, 7, int diff --git a/model/lantern/lantern_server.rb b/model/lantern/lantern_server.rb index 12f7ca8a7..4ffcb025a 100644 --- a/model/lantern/lantern_server.rb +++ b/model/lantern/lantern_server.rb @@ -64,7 +64,7 @@ def display_state return "domain setup" if strand.label.include?("domain") return "ssl setup" if strand.label == "setup_ssl" return "updating" if vm.display_state == "updating" || strand.label.include?("update") || strand.label == "init_sql" - return "unavailable" if strand.label == "wait_db_available" + return "unavailable" if ["wait_db_available", "unavailable"].include?(strand.label) return "running" if strand.label == "wait" return "failover" if ["take_over", "wait_swap_ip", "promote_server"].include?(strand.label) "creating" diff --git a/prog/lantern/lantern_server_nexus.rb b/prog/lantern/lantern_server_nexus.rb index 3fdff3008..30af0cde6 100644 --- a/prog/lantern/lantern_server_nexus.rb +++ b/prog/lantern/lantern_server_nexus.rb @@ -48,11 +48,11 @@ def self.assemble( timeline_access: timeline_access, timeline_id: timeline_id, representative_at: representative_at, - synchronization_status: representative_at ? "ready" : "catching_up", - domain: domain + synchronization_status: representative_at ? "ready" : "catching_up" ) - Strand.create(prog: "Lantern::LanternServerNexus", label: "start") { _1.id = lantern_server.id } + stack_frame = domain.nil? ? {} : {domain: domain} + Strand.create(prog: "Lantern::LanternServerNexus", label: "start", stack: [stack_frame]) { _1.id = lantern_server.id } end end @@ -127,7 +127,7 @@ def before_run case vm.sshable.cmd("common/bin/daemonizer --check configure_lantern") when "Succeeded" vm.sshable.cmd("common/bin/daemonizer --clean configure_lantern") - if !lantern_server.domain.nil? + if !frame["domain"].nil? lantern_server.incr_add_domain end @@ -314,16 +314,26 @@ def before_run end label def add_domain + if frame["domain"].nil? + raise "no domain in stack" + end + cf_client = Dns::Cloudflare.new begin - cf_client.upsert_dns_record(lantern_server.domain, lantern_server.vm.sshable.host) + cf_client.upsert_dns_record(frame["domain"], lantern_server.vm.sshable.host) rescue => e Clog.emit("Error while adding domain") { {error: e} } - lantern_server.update(domain: nil) decr_add_domain hop_wait end + lantern_server.update(domain: frame["domain"]) + + current_frame = strand.stack.first + current_frame.delete("domain") + strand.modified!(:stack) + strand.save_changes + decr_add_domain register_deadline(:wait, 5 * 60) hop_setup_ssl diff --git a/routes/api/project/location/lantern.rb b/routes/api/project/location/lantern.rb index 70ec3b605..444caf312 100644 --- a/routes/api/project/location/lantern.rb +++ b/routes/api/project/location/lantern.rb @@ -101,6 +101,11 @@ class CloverApi r.post "add-domain" do Authorization.authorize(@current_user.id, "Postgres:edit", pg.id) DB.transaction do + strand = pg.representative_server.strand + strand.stack.first["domain"] = r.params["domain"] + strand.modified!(:stack) + strand.save_changes + pg.representative_server.update(domain: r.params["domain"]) pg.representative_server.incr_add_domain end diff --git a/routes/web/project/location/lantern.rb b/routes/web/project/location/lantern.rb index 87ce1417e..706c95dba 100644 --- a/routes/web/project/location/lantern.rb +++ b/routes/web/project/location/lantern.rb @@ -65,7 +65,11 @@ class CloverWeb r.post "add-domain" do Authorization.authorize(@current_user.id, "Postgres:edit", pg.id) DB.transaction do - pg.representative_server.update(domain: r.params["domain"]) + strand = pg.representative_server.strand + strand.stack.first["domain"] = r.params["domain"] + strand.modified!(:stack) + strand.save_changes + pg.representative_server.incr_add_domain end r.redirect "#{@project.path}#{pg.path}" diff --git a/spec/prog/lantern/lantern_server_nexus_spec.rb b/spec/prog/lantern/lantern_server_nexus_spec.rb index cf36be454..c9db6a458 100644 --- a/spec/prog/lantern/lantern_server_nexus_spec.rb +++ b/spec/prog/lantern/lantern_server_nexus_spec.rb @@ -235,7 +235,7 @@ expect(Config).to receive(:gcp_creds_gcr_b64).and_return("test-creds") expect(lantern_server.vm.sshable).to receive(:cmd).with("common/bin/daemonizer --check configure_lantern").and_return("Succeeded") expect(lantern_server.vm.sshable).to receive(:cmd).with("common/bin/daemonizer --clean configure_lantern") - expect(lantern_server).to receive(:domain).and_return("db.lantern.dev") + expect(nx).to receive(:frame).and_return({"domain" => "db.lantern.dev"}) expect(lantern_server).to receive(:incr_add_domain) expect(lantern_server).to receive(:primary?).and_return(true) expect(nx).to receive(:register_deadline).with(:wait, 40 * 60) @@ -246,7 +246,7 @@ expect(Config).to receive(:gcp_creds_gcr_b64).and_return("test-creds") expect(lantern_server.vm.sshable).to receive(:cmd).with("common/bin/daemonizer --check configure_lantern").and_return("Succeeded") expect(lantern_server.vm.sshable).to receive(:cmd).with("common/bin/daemonizer --clean configure_lantern") - expect(lantern_server).to receive(:domain).and_return(nil) + expect(nx).to receive(:frame).and_return({}) expect(lantern_server).to receive(:primary?).and_return(false) expect(nx).to receive(:register_deadline).with(:wait, 120 * 60) expect { nx.setup_docker_stack }.to hop("wait_db_available") @@ -557,12 +557,17 @@ end describe "#add_domain" do + it "raises error" do + expect(nx).to receive(:frame).and_return({}) + expect { nx.add_domain }.to raise_error "no domain in stack" + end + it "fails to add domain" do + expect(nx).to receive(:frame).and_return({"domain" => "db.lantern.dev"}).at_least(:once) expect(lantern_server.vm.sshable).to receive(:host).and_return("1.1.1.1") cf_client = instance_double(Dns::Cloudflare) expect(Dns::Cloudflare).to receive(:new).and_return(cf_client) expect(cf_client).to receive(:upsert_dns_record).and_raise - allow(lantern_server).to receive(:update).with(domain: nil) expect { nx.add_domain }.to hop("wait") end @@ -570,7 +575,9 @@ expect(lantern_server.vm.sshable).to receive(:host).and_return("1.1.1.1") cf_client = instance_double(Dns::Cloudflare) expect(Dns::Cloudflare).to receive(:new).and_return(cf_client) - expect(lantern_server).to receive(:domain).and_return("test.lantern.dev") + + expect(nx).to receive(:frame).and_return({"domain" => "test.lantern.dev"}).at_least(:once) + expect(lantern_server).to receive(:update).with({domain: "test.lantern.dev"}) expect(cf_client).to receive(:upsert_dns_record).with("test.lantern.dev", "1.1.1.1") expect { nx.add_domain }.to hop("setup_ssl") end diff --git a/spec/routes/api/project/lantern_spec.rb b/spec/routes/api/project/lantern_spec.rb index 7e76a0d86..c8346b257 100644 --- a/spec/routes/api/project/lantern_spec.rb +++ b/spec/routes/api/project/lantern_spec.rb @@ -67,6 +67,7 @@ body = JSON.parse(last_response.body) expect(last_response.status).to eq(200) + serv = LanternResource[name: "instance-2"].representative_server expect(body["name"]).to eq("instance-2") expect(body["label"]).to eq("test-label") expect(body["state"]).to eq("creating") @@ -77,7 +78,7 @@ expect(body["minor_version"]).to eq("1") expect(body["org_id"]).to eq(0) expect(body["storage_size_gib"]).to eq(100) - expect(body["domain"]).to eq("test.db.lantern.dev") + expect(serv.strand.stack.first["domain"]).to eq("test.db.lantern.dev") expect(body["app_env"]).to eq("test") expect(body["debug"]).to be(false) expect(body["enable_telemetry"]).to be(true) @@ -99,6 +100,7 @@ expect(last_response.status).to eq(200) + serv = LanternResource[name: "instance-from-backup"].representative_server expect(body["name"]).to eq("instance-from-backup") expect(body["state"]).to eq("creating") expect(body["instance_type"]).to eq("writer") @@ -108,7 +110,7 @@ expect(body["minor_version"]).to eq(pg.representative_server.minor_version) expect(body["org_id"]).to eq(0) expect(body["storage_size_gib"]).to eq(pg.representative_server.target_storage_size_gib) - expect(body["domain"]).to eq("test.db.lantern.dev") + expect(serv.strand.stack.first["domain"]).to eq("test.db.lantern.dev") expect(body["app_env"]).to eq("test") expect(body["debug"]).to be(false) expect(body["enable_telemetry"]).to be(true) @@ -130,7 +132,7 @@ expect(pg.timeline).to receive(:refresh_earliest_backup_completion_time).at_least(:once) expect(pg.timeline).to receive(:earliest_restore_time).and_return(Time.new - 1000000).at_least(:once) expect(pg.timeline).to receive(:latest_restore_time).and_return(Time.new).at_least(:once) - post "/api/project/#{project.ubid}/lantern", {size: "n1-standard-2", name: "instance-from-backup", org_id: 0, location: "us-central1", domain: "test.db.lantern.dev", parent_id: pg.id, restore_target: Time.now - 1000} + post "/api/project/#{project.ubid}/lantern", {size: "n1-standard-2", name: "instance-from-backup", org_id: 0, location: "us-central1", parent_id: pg.id, restore_target: Time.now - 1000} body = JSON.parse(last_response.body) @@ -145,7 +147,6 @@ expect(body["minor_version"]).to eq(pg.representative_server.minor_version) expect(body["org_id"]).to eq(0) expect(body["storage_size_gib"]).to eq(pg.representative_server.target_storage_size_gib) - expect(body["domain"]).to eq("test.db.lantern.dev") expect(body["app_env"]).to eq("test") expect(body["debug"]).to be(false) expect(body["enable_telemetry"]).to be(true) @@ -158,10 +159,11 @@ expect(Config).to receive(:lantern_top_domain).and_return("db.lantern.dev") post "/api/project/#{project.ubid}/lantern", {size: "n1-standard-2", name: "instance-2", org_id: 0, location: "us-central1", storage_size_gib: 100, lantern_version: "0.2.2", extras_version: "0.1.4", minor_version: "1", subdomain: "test", app_env: "test", repl_password: "test-repl-pass", enable_telemetry: true, postgres_password: "test-pg-pass"} - body = JSON.parse(last_response.body) + JSON.parse(last_response.body) expect(last_response.status).to eq(200) - expect(body["domain"]).to eq("test.db.lantern.dev") + serv = LanternResource[name: "instance-2"].representative_server + expect(serv.strand.stack.first["domain"]).to eq("test.db.lantern.dev") end end end diff --git a/spec/routes/web/project/location/lantern_spec.rb b/spec/routes/web/project/location/lantern_spec.rb index 091cd609f..ce10df0a3 100644 --- a/spec/routes/web/project/location/lantern_spec.rb +++ b/spec/routes/web/project/location/lantern_spec.rb @@ -171,7 +171,7 @@ fill_in "domain", with: "example.com" click_button "Add domain" pg = LanternResource.first - expect(pg.representative_server.domain).to eq("example.com") + expect(pg.representative_server.strand.stack.first["domain"]).to eq("example.com") end end