Custom domains (#34)
* UI to create custom domains * Only call ssh once per domain * Update copy for custom domain setup * Use correct user for sshpull/36/head
parent
da58d8d87e
commit
79b9f72b52
@ -0,0 +1,30 @@
|
||||
defmodule Mix.Tasks.ObtainSslCertificates do
|
||||
use Plausible.Repo
|
||||
|
||||
def run(_args) do
|
||||
Application.ensure_all_started(:plausible)
|
||||
execute()
|
||||
end
|
||||
|
||||
def execute(system \\ System) do
|
||||
recent_custom_domains = Repo.all(
|
||||
from cd in Plausible.Site.CustomDomain,
|
||||
where: cd.updated_at > fragment("now() - '3 days'::interval"),
|
||||
where: not cd.has_ssl_certificate
|
||||
)
|
||||
|
||||
for domain <- recent_custom_domains do
|
||||
system.cmd("ssh", ["-t", "ubuntu@custom.plausible.io", "sudo certbot certonly --nginx -n -d #{domain.domain}"])
|
||||
|> report_result(domain)
|
||||
end
|
||||
end
|
||||
|
||||
defp report_result({_, 0}, domain) do
|
||||
Ecto.Changeset.change(domain, has_ssl_certificate: true) |> Repo.update!
|
||||
Plausible.Slack.notify("Obtained SSL cert for #{domain.domain}")
|
||||
end
|
||||
|
||||
defp report_result({error_msg, error_code}, domain) do
|
||||
Sentry.capture_message("Error obtaining SSL certificate", extra: %{error_msg: error_msg, error_code: error_code, domain: domain.domain})
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,20 @@
|
||||
defmodule Plausible.Site.CustomDomain do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
@domain_name_regex ~r/(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/
|
||||
|
||||
schema "custom_domains" do
|
||||
field :domain, :string
|
||||
field :has_ssl_certificate, :boolean
|
||||
belongs_to :site, Plausible.Site
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
def changeset(custom_domain, attrs) do
|
||||
custom_domain
|
||||
|> cast(attrs, [:domain, :site_id])
|
||||
|> validate_required([:domain, :site_id])
|
||||
|> validate_format(:domain, @domain_name_regex, message: "please enter a valid domain name")
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,27 @@
|
||||
<%= form_for @conn, "/", [class: "max-w-sm w-full mx-auto bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %>
|
||||
<div class="flex items-center justify-between">
|
||||
<h2>Update javascript snippet</h2>
|
||||
</div>
|
||||
<div class="my-4">
|
||||
<p>
|
||||
Allow up to 4 hours for DNS changes to propagate and for us to obtain an SSL certificate for <code><%= @site.custom_domain.domain %></code>
|
||||
</p>
|
||||
<p class="mt-4">
|
||||
The setup is working when <a href="//<%= @site.custom_domain.domain %>/js/plausible.js" target="_blank" class="text-indigo"><%= @site.custom_domain.domain %>/js/plausible.js</a> loads the javascript tracker.
|
||||
</p>
|
||||
<p class="mt-4">
|
||||
To finish your setup, please update the tracking snippet on your site with your custom domain.
|
||||
</p>
|
||||
|
||||
<div class="relative">
|
||||
<%= textarea f, :domain, id: "snippet_code", class: "transition overflow-hidden bg-grey-lighter appearance-none border border-transparent rounded w-full p-2 pr-6 text-grey-darker leading-normal appearance-none focus:outline-none focus:bg-white focus:border-grey-light text-xs mt-4 resize-none", value: snippet(@site), rows: 2, readonly: "readonly" %>
|
||||
<a onclick="var textarea = document.getElementById('snippet_code'); textarea.focus(); textarea.select(); document.execCommand('copy');" href="javascript:void(0)" class="no-underline text-indigo text-sm hover:underline">
|
||||
<svg class="absolute text-indigo" style="top: 24px; right: 12px;" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-copy"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
<%= link("Back to settings →", class: "button mt-4 w-full", to: "/#{URI.encode_www_form(@site.domain)}/settings#custom-domain") %>
|
||||
<p class="mt-4 text-grey-dark text-sm">
|
||||
Problems? <%= link("Get help via email", to: "mailto:uku@plausible.io", class: "text-indigo-darker underline") %>
|
||||
</p>
|
||||
<% end %>
|
||||
@ -0,0 +1,15 @@
|
||||
<%= form_for @changeset, "/sites/#{URI.encode_www_form(@site.domain)}/custom-domains", [class: "max-w-sm w-full mx-auto bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %>
|
||||
<h2>Setup custom domain</h2>
|
||||
<div class="my-6">
|
||||
We recommend using a subdomain of the website you're running Plausible on.
|
||||
If your site is on <code>example.com</code> you can use <code>stats.example.com</code>.
|
||||
<br /><br /> The name of the subdomain can be anything, it doesn't have to be <code>stats</code>.
|
||||
</div>
|
||||
<div class="my-6">
|
||||
<%= label f, :domain, class: "block text-sm font-bold" %>
|
||||
<%= text_input f, :domain, class: "transition mt-3 bg-grey-lighter appearance-none border border-transparent rounded w-full p-2 text-grey-darker leading-normal appearance-none focus:outline-none focus:bg-white focus:border-grey-light", placeholder: "stats.[yourdomain].com" %>
|
||||
<%= error_tag f, :domain %>
|
||||
</div>
|
||||
|
||||
<%= submit "DNS setup ->", class: "button mt-4 w-full" %>
|
||||
<% end %>
|
||||
@ -0,0 +1,15 @@
|
||||
defmodule Plausible.Repo.Migrations.CreateCustomDomains do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create table(:custom_domains) do
|
||||
add :domain, :text, null: false
|
||||
add :site_id, references(:sites), null: false
|
||||
add :has_ssl_certificate, :boolean, null: false, default: false
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
create unique_index(:custom_domains, :site_id)
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,37 @@
|
||||
defmodule Mix.Tasks.ObtainSslCertificatesTest do
|
||||
use Plausible.DataCase
|
||||
alias Mix.Tasks.ObtainSslCertificates
|
||||
import Double
|
||||
|
||||
test "makes ssh call to certbot" do
|
||||
site = insert(:site)
|
||||
insert(:custom_domain, site: site, domain: "custom-site.com")
|
||||
|
||||
system_stub = stub(System, :cmd, fn(_cmd, _args) -> {"", 0} end)
|
||||
ObtainSslCertificates.execute(system_stub)
|
||||
|
||||
assert_receive({System, :cmd, ["ssh", ["-t", "ubuntu@custom.plausible.io", "sudo certbot certonly --nginx -n -d custom-site.com"]]})
|
||||
end
|
||||
|
||||
test "sets has_ssl_certficate=true if the ssh command is succesful" do
|
||||
site = insert(:site)
|
||||
insert(:custom_domain, site: site, domain: "custom-site.com")
|
||||
|
||||
system_stub = stub(System, :cmd, fn(_cmd, _args) -> {"", 0} end)
|
||||
ObtainSslCertificates.execute(system_stub)
|
||||
|
||||
domain = Repo.get_by(Plausible.Site.CustomDomain, site_id: site.id)
|
||||
assert domain.has_ssl_certificate
|
||||
end
|
||||
|
||||
test "does not set has_ssl_certficate=true if the ssh command fails" do
|
||||
site = insert(:site)
|
||||
insert(:custom_domain, site: site, domain: "custom-site.com")
|
||||
|
||||
failing_system_stub = stub(System, :cmd, fn(_cmd, _args) -> {"", 1} end)
|
||||
ObtainSslCertificates.execute(failing_system_stub)
|
||||
|
||||
domain = Repo.get_by(Plausible.Site.CustomDomain, site_id: site.id)
|
||||
refute domain.has_ssl_certificate
|
||||
end
|
||||
end
|
||||
@ -1,3 +1,4 @@
|
||||
{:ok, _} = Application.ensure_all_started(:ex_machina)
|
||||
ExUnit.start()
|
||||
Application.ensure_all_started(:double)
|
||||
Ecto.Adapters.SQL.Sandbox.mode(Plausible.Repo, :manual)
|
||||
|
||||
Loading…
Reference in New Issue