2021-04-08 17:41:52 +08:00
|
|
|
defmodule LivebookCLI.Server do
|
|
|
|
@behaviour LivebookCLI.Task
|
2021-04-27 22:34:02 +08:00
|
|
|
|
2021-04-15 21:50:29 +08:00
|
|
|
@external_resource "README.md"
|
|
|
|
|
|
|
|
[_, environment_variables, _] =
|
|
|
|
"README.md"
|
|
|
|
|> File.read!()
|
|
|
|
|> String.split("<!-- Environment variables -->")
|
|
|
|
|
|
|
|
@environment_variables String.trim(environment_variables)
|
2021-04-08 17:41:52 +08:00
|
|
|
|
|
|
|
@impl true
|
|
|
|
def usage() do
|
|
|
|
"""
|
2022-01-31 01:58:09 +08:00
|
|
|
Usage: livebook server [options] [open-command]
|
2021-04-08 17:41:52 +08:00
|
|
|
|
2022-01-31 01:36:30 +08:00
|
|
|
An optional open-command can be given as argument. It will open
|
|
|
|
up a browser window according these rules:
|
2022-01-29 17:36:32 +08:00
|
|
|
|
2023-04-17 21:40:48 +08:00
|
|
|
* If the open-command is "@home", the browser window will point
|
|
|
|
to the home page
|
|
|
|
|
|
|
|
* If the open-command is "@new", the browser window will point
|
2022-01-31 01:36:30 +08:00
|
|
|
to a new notebook
|
|
|
|
|
|
|
|
* If the open-command is a URL, the notebook at the given URL
|
|
|
|
will be imported
|
|
|
|
|
2022-02-04 03:26:18 +08:00
|
|
|
* If the open-command is a directory, the browser window will point
|
2023-04-17 21:40:48 +08:00
|
|
|
to the open page with the directory selected
|
2022-02-04 03:26:18 +08:00
|
|
|
|
|
|
|
* If the open-command is a notebook file, the browser window will point
|
|
|
|
to the opened notebook
|
|
|
|
|
2022-01-31 01:36:30 +08:00
|
|
|
The open-command runs after the server is started. If a server is
|
|
|
|
already running, the browser window will point to the server
|
|
|
|
currently running.
|
2022-01-29 17:36:32 +08:00
|
|
|
|
|
|
|
## Available options
|
2021-04-08 17:41:52 +08:00
|
|
|
|
2021-06-09 22:24:02 +08:00
|
|
|
--cookie Sets a cookie for the app distributed node
|
|
|
|
--ip The ip address to start the web application on, defaults to 127.0.0.1
|
|
|
|
Must be a valid IPv4 or IPv6 address
|
2023-02-11 02:55:47 +08:00
|
|
|
--name Sets a name for the app distributed node
|
2021-06-09 22:24:02 +08:00
|
|
|
-p, --port The port to start the web application on, defaults to 8080
|
2021-04-15 21:50:29 +08:00
|
|
|
|
|
|
|
The --help option can be given to print this notice.
|
|
|
|
|
|
|
|
## Environment variables
|
|
|
|
|
|
|
|
#{@environment_variables}
|
2021-04-08 17:41:52 +08:00
|
|
|
|
2022-01-31 01:36:30 +08:00
|
|
|
## Examples
|
|
|
|
|
|
|
|
Starts a server:
|
|
|
|
|
|
|
|
livebook server
|
|
|
|
|
|
|
|
Starts a server and opens up a browser at a new notebook:
|
|
|
|
|
2023-06-01 01:21:14 +08:00
|
|
|
livebook server @new
|
2022-01-31 01:36:30 +08:00
|
|
|
|
|
|
|
Starts a server and imports the notebook at the given URL:
|
|
|
|
|
|
|
|
livebook server https://example.com/my-notebook.livemd
|
|
|
|
|
2021-04-08 17:41:52 +08:00
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def call(args) do
|
2022-01-29 17:36:32 +08:00
|
|
|
{opts, extra_args} = args_to_options(args)
|
2021-07-01 18:17:49 +08:00
|
|
|
config_entries = opts_to_config(opts, [])
|
2021-04-08 17:41:52 +08:00
|
|
|
put_config_entries(config_entries)
|
|
|
|
|
2022-02-08 21:45:58 +08:00
|
|
|
case Livebook.Config.port() do
|
|
|
|
0 ->
|
|
|
|
# When a random port is configured, we can assume no collision
|
|
|
|
start_server(extra_args)
|
|
|
|
|
|
|
|
port ->
|
|
|
|
base_url = "http://localhost:#{port}"
|
|
|
|
|
|
|
|
case check_endpoint_availability(base_url) do
|
|
|
|
:livebook_running ->
|
|
|
|
IO.puts("Livebook already running on #{base_url}")
|
|
|
|
open_from_args(base_url, extra_args)
|
|
|
|
|
|
|
|
:taken ->
|
|
|
|
print_error(
|
|
|
|
"Another application is already running on port #{port}." <>
|
|
|
|
" Either ensure this port is free or specify a different port using the --port option"
|
|
|
|
)
|
|
|
|
|
|
|
|
:available ->
|
|
|
|
start_server(extra_args)
|
2021-07-01 18:17:49 +08:00
|
|
|
end
|
2021-04-08 17:41:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-08 21:45:58 +08:00
|
|
|
defp start_server(extra_args) do
|
|
|
|
# We configure the endpoint with `server: true`,
|
|
|
|
# so it's gonna start listening
|
|
|
|
case Application.ensure_all_started(:livebook) do
|
|
|
|
{:ok, _} ->
|
|
|
|
open_from_args(LivebookWeb.Endpoint.access_url(), extra_args)
|
|
|
|
Process.sleep(:infinity)
|
|
|
|
|
|
|
|
{:error, error} ->
|
|
|
|
print_error("Livebook failed to start with reason: #{inspect(error)}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-08 17:41:52 +08:00
|
|
|
# Takes a list of {app, key, value} config entries
|
|
|
|
# and overrides the current applications' configuration accordingly.
|
|
|
|
# Multiple values for the same key are deeply merged (provided they are keyword lists).
|
|
|
|
defp put_config_entries(config_entries) do
|
|
|
|
config_entries
|
|
|
|
|> Enum.reduce([], fn {app, key, value}, acc ->
|
|
|
|
acc = Keyword.put_new_lazy(acc, app, fn -> Application.get_all_env(app) end)
|
|
|
|
Config.Reader.merge(acc, [{app, [{key, value}]}])
|
|
|
|
end)
|
|
|
|
|> Application.put_all_env(persistent: true)
|
|
|
|
end
|
|
|
|
|
2021-08-31 05:23:05 +08:00
|
|
|
defp check_endpoint_availability(base_url) do
|
|
|
|
Application.ensure_all_started(:inets)
|
|
|
|
|
2022-02-19 03:57:03 +08:00
|
|
|
health_url = set_path(base_url, "/public/health")
|
2021-08-31 05:23:05 +08:00
|
|
|
|
|
|
|
case Livebook.Utils.HTTP.request(:get, health_url) do
|
|
|
|
{:ok, status, _headers, body} ->
|
|
|
|
with 200 <- status,
|
|
|
|
{:ok, body} <- Jason.decode(body),
|
|
|
|
%{"application" => "livebook"} <- body do
|
|
|
|
:livebook_running
|
|
|
|
else
|
|
|
|
_ -> :taken
|
|
|
|
end
|
|
|
|
|
|
|
|
{:error, _error} ->
|
|
|
|
:available
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-19 00:27:36 +08:00
|
|
|
defp open_from_args(_base_url, []) do
|
|
|
|
:ok
|
2022-01-31 01:36:30 +08:00
|
|
|
end
|
2021-08-31 05:23:05 +08:00
|
|
|
|
2023-04-17 21:40:48 +08:00
|
|
|
defp open_from_args(base_url, ["@home"]) do
|
|
|
|
Livebook.Utils.browser_open(base_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp open_from_args(base_url, ["@new"]) do
|
2022-01-31 01:36:30 +08:00
|
|
|
base_url
|
2023-07-04 20:46:19 +08:00
|
|
|
|> set_path("/new")
|
2022-01-31 01:36:30 +08:00
|
|
|
|> Livebook.Utils.browser_open()
|
2021-08-31 05:23:05 +08:00
|
|
|
end
|
|
|
|
|
2023-04-17 21:40:48 +08:00
|
|
|
defp open_from_args(base_url, ["new"]) do
|
|
|
|
IO.warn(~s/passing "new" as an argument is deprecated, use "@new" instead/, [])
|
|
|
|
open_from_args(base_url, ["@new"])
|
|
|
|
end
|
|
|
|
|
2022-02-04 03:26:18 +08:00
|
|
|
defp open_from_args(base_url, [url_or_file_or_dir]) do
|
|
|
|
url = URI.parse(url_or_file_or_dir)
|
2022-02-04 06:43:47 +08:00
|
|
|
path = Path.expand(url_or_file_or_dir)
|
2022-02-04 03:26:18 +08:00
|
|
|
|
|
|
|
cond do
|
|
|
|
url.scheme in ~w(http https file) ->
|
|
|
|
base_url
|
|
|
|
|> Livebook.Utils.notebook_import_url(url_or_file_or_dir)
|
|
|
|
|> Livebook.Utils.browser_open()
|
|
|
|
|
2023-03-07 01:14:33 +08:00
|
|
|
File.regular?(path) or File.dir?(path) ->
|
2022-02-04 03:26:18 +08:00
|
|
|
base_url
|
2022-02-07 20:39:23 +08:00
|
|
|
|> Livebook.Utils.notebook_open_url(url_or_file_or_dir)
|
2022-02-04 03:26:18 +08:00
|
|
|
|> Livebook.Utils.browser_open()
|
|
|
|
|
|
|
|
true ->
|
|
|
|
Livebook.Utils.browser_open(base_url)
|
|
|
|
end
|
2022-01-29 17:36:32 +08:00
|
|
|
end
|
|
|
|
|
2022-02-04 03:26:18 +08:00
|
|
|
defp open_from_args(_base_url, _extra_args) do
|
2022-01-29 17:36:32 +08:00
|
|
|
print_error(
|
|
|
|
"Too many arguments entered. Ensure only one argument is used to specify the file path and all other arguments are preceded by the relevant switch"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-04-20 01:50:09 +08:00
|
|
|
@switches [
|
2021-04-27 22:34:02 +08:00
|
|
|
cookie: :string,
|
|
|
|
ip: :string,
|
2021-04-20 01:50:09 +08:00
|
|
|
name: :string,
|
2021-04-27 22:34:02 +08:00
|
|
|
port: :integer,
|
2024-06-13 16:06:08 +08:00
|
|
|
home: :string
|
2021-04-20 01:50:09 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
@aliases [
|
|
|
|
p: :port
|
|
|
|
]
|
2021-04-08 17:41:52 +08:00
|
|
|
|
2021-07-01 18:17:49 +08:00
|
|
|
defp args_to_options(args) do
|
2024-06-13 16:06:08 +08:00
|
|
|
OptionParser.parse!(args, strict: @switches, aliases: @aliases)
|
2021-04-08 17:41:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
defp opts_to_config([], config), do: config
|
|
|
|
|
|
|
|
defp opts_to_config([{:port, port} | opts], config) do
|
|
|
|
opts_to_config(opts, [{:livebook, LivebookWeb.Endpoint, http: [port: port]} | config])
|
|
|
|
end
|
|
|
|
|
2021-04-27 22:34:02 +08:00
|
|
|
defp opts_to_config([{:ip, ip} | opts], config) do
|
|
|
|
ip = Livebook.Config.ip!("--ip", ip)
|
2022-05-13 21:22:41 +08:00
|
|
|
host = Livebook.Utils.ip_to_host(ip)
|
|
|
|
|
|
|
|
opts_to_config(opts, [
|
|
|
|
{:livebook, LivebookWeb.Endpoint, http: [ip: ip], url: [host: host]} | config
|
|
|
|
])
|
2021-04-27 22:34:02 +08:00
|
|
|
end
|
|
|
|
|
2021-04-08 17:41:52 +08:00
|
|
|
defp opts_to_config([{:name, name} | opts], config) do
|
|
|
|
name = String.to_atom(name)
|
2024-06-13 16:06:08 +08:00
|
|
|
opts_to_config(opts, [{:livebook, :node, name} | config])
|
2021-04-08 17:41:52 +08:00
|
|
|
end
|
|
|
|
|
2021-04-27 22:34:02 +08:00
|
|
|
defp opts_to_config([{:cookie, cookie} | opts], config) do
|
|
|
|
cookie = String.to_atom(cookie)
|
|
|
|
opts_to_config(opts, [{:livebook, :cookie, cookie} | config])
|
|
|
|
end
|
|
|
|
|
2021-04-08 17:41:52 +08:00
|
|
|
defp opts_to_config([_opt | opts], config), do: opts_to_config(opts, config)
|
2021-07-01 18:17:49 +08:00
|
|
|
|
2022-02-19 03:57:03 +08:00
|
|
|
defp set_path(url, path) do
|
2021-08-31 05:23:05 +08:00
|
|
|
url
|
2021-12-10 03:46:45 +08:00
|
|
|
|> URI.parse()
|
2022-02-19 03:57:03 +08:00
|
|
|
|> Map.put(:path, path)
|
2021-08-31 05:23:05 +08:00
|
|
|
|> URI.to_string()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp print_error(message) do
|
|
|
|
IO.ANSI.format([:red, message]) |> IO.puts()
|
|
|
|
end
|
2021-04-08 17:41:52 +08:00
|
|
|
end
|