Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set domain only after creating dns record #75

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion model/lantern/lantern_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 16 additions & 6 deletions prog/lantern/lantern_server_nexus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions routes/api/project/location/lantern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion routes/web/project/location/lantern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
15 changes: 11 additions & 4 deletions spec/prog/lantern/lantern_server_nexus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -557,20 +557,27 @@
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

it "adds domain and setup ssl" do
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
Expand Down
14 changes: 8 additions & 6 deletions spec/routes/api/project/lantern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/routes/web/project/location/lantern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down