-
Notifications
You must be signed in to change notification settings - Fork 511
Deploy notebooks from CLI #3034
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
Changes from 7 commits
e604e05
5160ae4
a99b687
ccc07af
01a2977
f14d436
01e5c71
ce0b7b2
ecfa697
86244ce
600c658
066df14
081d848
eab2d1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ defmodule Livebook.Teams.Requests do | |
| alias Livebook.Secrets.Secret | ||
| alias Livebook.Teams | ||
|
|
||
| @deploy_key_prefix "lb_dk_" | ||
| @error_message "Something went wrong, try again later or please file a bug if it persists" | ||
| @unauthorized_error_message "You are not authorized to perform this action, make sure you have the access and you are not in a Livebook App Server/Offline instance" | ||
|
|
||
|
|
@@ -14,6 +15,9 @@ defmodule Livebook.Teams.Requests do | |
| @doc false | ||
| def error_message(), do: @error_message | ||
|
|
||
| @doc false | ||
| def deploy_key_prefix(), do: @deploy_key_prefix | ||
|
|
||
| @doc """ | ||
| Send a request to Livebook Team API to create a new org. | ||
| """ | ||
|
|
@@ -227,6 +231,34 @@ defmodule Livebook.Teams.Requests do | |
| get("/api/v1/org/identity", %{access_token: access_token}, team) | ||
| end | ||
|
|
||
| @doc """ | ||
| Send a request to Livebook Team API to return a session using a deploy key. | ||
| """ | ||
| @spec fetch_cli_session(map()) :: api_result() | ||
| def fetch_cli_session(config) do | ||
| post("/api/v1/cli/auth", %{}, config) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We call this with
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To call this function, we don't really use the |
||
| end | ||
|
|
||
| @doc """ | ||
| Send a request to Livebook Team API to deploy an app using a deploy key. | ||
| """ | ||
| @spec deploy_app_from_cli(Team.t(), Teams.AppDeployment.t(), String.t()) :: api_result() | ||
| def deploy_app_from_cli(team, app_deployment, deployment_group_name) do | ||
| secret_key = Teams.derive_key(team.teams_key) | ||
|
|
||
| params = %{ | ||
| title: app_deployment.title, | ||
| slug: app_deployment.slug, | ||
| multi_session: app_deployment.multi_session, | ||
| access_type: app_deployment.access_type, | ||
| deployment_group_name: deployment_group_name, | ||
| sha: app_deployment.sha | ||
| } | ||
|
|
||
| encrypted_content = Teams.encrypt(app_deployment.file, secret_key) | ||
| upload("/api/v1/cli/org/apps", encrypted_content, params, team) | ||
| end | ||
|
|
||
| @doc """ | ||
| Normalizes errors map into errors for the given schema. | ||
| """ | ||
|
|
@@ -271,6 +303,7 @@ defmodule Livebook.Teams.Requests do | |
| defp upload(path, content, params, team) do | ||
| build_req(team) | ||
| |> Req.Request.put_header("content-length", "#{byte_size(content)}") | ||
| |> Req.Request.put_private(:cli, path =~ "cli") | ||
|
aleDsz marked this conversation as resolved.
Outdated
|
||
| |> Req.Request.put_private(:deploy, true) | ||
| |> Req.post(url: path, params: params, body: content) | ||
| |> handle_response() | ||
|
|
@@ -291,6 +324,11 @@ defmodule Livebook.Teams.Requests do | |
| Req.Request.append_request_steps(req, unauthorized: &{&1, Req.Response.new(status: 401)}) | ||
| end | ||
|
|
||
| defp add_team_auth(req, %{session_token: @deploy_key_prefix <> _} = team) do | ||
| token = "#{team.session_token}:#{Teams.Org.key_hash(%Teams.Org{teams_key: team.teams_key})}" | ||
| Req.Request.merge_options(req, auth: {:bearer, token}) | ||
| end | ||
|
|
||
| defp add_team_auth(req, %{user_id: nil} = team) do | ||
| agent_name = Livebook.Config.agent_name() | ||
| token = "#{team.session_token}:#{agent_name}:#{team.org_id}:#{team.org_key_id}" | ||
|
|
@@ -305,6 +343,14 @@ defmodule Livebook.Teams.Requests do | |
|
|
||
| defp transform_response({request, response}) do | ||
| case {request, response} do | ||
| {request, %{status: 404}} when request.private.cli and request.private.deploy -> | ||
|
aleDsz marked this conversation as resolved.
Outdated
|
||
| {request, | ||
| %{ | ||
| response | ||
| | status: 422, | ||
| body: %{"errors" => %{"deployment_group" => ["does not exist"]}} | ||
| }} | ||
|
|
||
| {request, %{status: 400, body: %{"errors" => %{"detail" => error}}}} | ||
| when request.private.deploy -> | ||
| {request, %{response | status: 422, body: %{"errors" => %{"file" => [error]}}}} | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.