diff --git a/bin/core/src/api/terminal.rs b/bin/core/src/api/terminal.rs index 6e2180354..570a76897 100644 --- a/bin/core/src/api/terminal.rs +++ b/bin/core/src/api/terminal.rs @@ -1,7 +1,7 @@ use anyhow::Context; use axum::{Extension, Router, middleware, routing::post}; use komodo_client::{ - api::terminal::*, + api::{terminal::*, write::TerminalRecreateMode}, entities::{ deployment::Deployment, permission::PermissionLevel, server::Server, stack::Stack, user::User, @@ -43,6 +43,7 @@ async fn execute_terminal( server, terminal, command, + init, }): Json, ) -> serror::Result { info!("/terminal/execute request | user: {}", user.username); @@ -54,8 +55,14 @@ async fn execute_terminal( ) .await?; - let stream = periphery_client(&server) - .await? + let periphery = periphery_client(&server).await?; + + // Maybe init terminal. + if let Some(request) = init { + periphery.request(request).await?; + } + + let stream = periphery .execute_terminal(terminal, command) .await .context("Failed to execute command on periphery")?; @@ -100,7 +107,12 @@ async fn execute_container_exec( let periphery = periphery_client(&server).await?; let stream = periphery - .execute_container_exec(container, shell, command, recreate) + .execute_container_exec( + container, + shell, + command, + recreate.unwrap_or(TerminalRecreateMode::DifferentCommand), + ) .await .context( "Failed to execute container exec command on periphery", @@ -146,7 +158,12 @@ async fn execute_deployment_exec( let periphery = periphery_client(&server).await?; let stream = periphery - .execute_container_exec(deployment.name, shell, command, recreate) + .execute_container_exec( + deployment.name, + shell, + command, + recreate.unwrap_or(TerminalRecreateMode::DifferentCommand), + ) .await .context( "Failed to execute container exec command on periphery", @@ -209,7 +226,12 @@ async fn execute_stack_exec( let periphery = periphery_client(&server).await?; let stream = periphery - .execute_container_exec(container, shell, command, recreate) + .execute_container_exec( + container, + shell, + command, + recreate.unwrap_or(TerminalRecreateMode::DifferentCommand), + ) .await .context( "Failed to execute container exec command on periphery", diff --git a/bin/core/src/ws/container.rs b/bin/core/src/ws/container.rs index b8a08e054..8995c1935 100644 --- a/bin/core/src/ws/container.rs +++ b/bin/core/src/ws/container.rs @@ -4,8 +4,11 @@ use axum::{ }; use futures_util::SinkExt; use komodo_client::{ - api::terminal::{ - ConnectContainerAttachQuery, ConnectContainerExecQuery, + api::{ + terminal::{ + ConnectContainerAttachQuery, ConnectContainerExecQuery, + }, + write::TerminalRecreateMode, }, entities::{permission::PermissionLevel, server::Server}, }; @@ -52,7 +55,7 @@ pub async fn exec( &server, container, shell, - recreate, + recreate.unwrap_or(TerminalRecreateMode::DifferentCommand), ) .await }) @@ -96,7 +99,7 @@ pub async fn attach( client_socket, &server, container, - recreate, + recreate.unwrap_or(TerminalRecreateMode::DifferentCommand), ) .await }) diff --git a/bin/core/src/ws/deployment.rs b/bin/core/src/ws/deployment.rs index a24f3f81d..c0bfff2d8 100644 --- a/bin/core/src/ws/deployment.rs +++ b/bin/core/src/ws/deployment.rs @@ -4,8 +4,11 @@ use axum::{ }; use futures_util::SinkExt; use komodo_client::{ - api::terminal::{ - ConnectDeploymentAttachQuery, ConnectDeploymentExecQuery, + api::{ + terminal::{ + ConnectDeploymentAttachQuery, ConnectDeploymentExecQuery, + }, + write::TerminalRecreateMode, }, entities::{ deployment::Deployment, permission::PermissionLevel, @@ -67,7 +70,7 @@ pub async fn exec( &server, deployment.name, shell, - recreate, + recreate.unwrap_or(TerminalRecreateMode::DifferentCommand), ) .await }) @@ -123,7 +126,7 @@ pub async fn attach( client_socket, &server, deployment.name, - recreate, + recreate.unwrap_or(TerminalRecreateMode::DifferentCommand), ) .await }) diff --git a/bin/core/src/ws/stack.rs b/bin/core/src/ws/stack.rs index 6535a1549..7bca22a3c 100644 --- a/bin/core/src/ws/stack.rs +++ b/bin/core/src/ws/stack.rs @@ -4,7 +4,10 @@ use axum::{ }; use futures_util::SinkExt; use komodo_client::{ - api::terminal::{ConnectStackAttachQuery, ConnectStackExecQuery}, + api::{ + terminal::{ConnectStackAttachQuery, ConnectStackExecQuery}, + write::TerminalRecreateMode, + }, entities::{ permission::PermissionLevel, server::Server, stack::Stack, }, @@ -37,7 +40,7 @@ pub async fn exec( &server, container, shell, - recreate, + recreate.unwrap_or(TerminalRecreateMode::DifferentCommand), ) .await }) @@ -63,7 +66,7 @@ pub async fn attach( client_socket, &server, container, - recreate, + recreate.unwrap_or(TerminalRecreateMode::DifferentCommand), ) .await }) diff --git a/client/core/rs/src/api/terminal.rs b/client/core/rs/src/api/terminal.rs index 103204c44..809dfa15d 100644 --- a/client/core/rs/src/api/terminal.rs +++ b/client/core/rs/src/api/terminal.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use typeshare::typeshare; -use crate::api::write::TerminalRecreateMode; +use crate::api::write::{CreateTerminal, TerminalRecreateMode}; /// Query to connect to a terminal (interactive shell over websocket) on the given server. #[typeshare] @@ -26,6 +26,9 @@ pub struct ExecuteTerminalBody { pub terminal: String, /// The command to execute. pub command: String, + /// Pass to init the terminal session + /// for when the terminal doesn't already exist. + pub init: Option, } /// Query to connect to a container exec session (interactive shell over websocket) on the given server. @@ -40,8 +43,7 @@ pub struct ConnectContainerExecQuery { pub shell: String, /// Specify the recreate behavior. /// Default is 'DifferentCommand' - #[serde(default = "default_container_recreate_mode")] - pub recreate: TerminalRecreateMode, + pub recreate: Option, } /// Query to connect to a container attach session (interactive shell over websocket) on the given server. @@ -54,8 +56,7 @@ pub struct ConnectContainerAttachQuery { pub container: String, /// Specify the recreate behavior. /// Default is 'DifferentCommand' - #[serde(default = "default_container_recreate_mode")] - pub recreate: TerminalRecreateMode, + pub recreate: Option, } /// Execute a command in the given containers shell. @@ -72,8 +73,7 @@ pub struct ExecuteContainerExecBody { pub command: String, /// Specify the recreate behavior. /// Default is 'DifferentCommand' - #[serde(default = "default_container_recreate_mode")] - pub recreate: TerminalRecreateMode, + pub recreate: Option, } /// Query to connect to a container exec session (interactive shell over websocket) on the given Deployment. @@ -87,8 +87,7 @@ pub struct ConnectDeploymentExecQuery { pub shell: String, /// Specify the recreate behavior. /// Default is 'DifferentCommand' - #[serde(default = "default_container_recreate_mode")] - pub recreate: TerminalRecreateMode, + pub recreate: Option, } /// Query to connect to a container attach session (interactive shell over websocket) on the given Deployment. @@ -100,8 +99,7 @@ pub struct ConnectDeploymentAttachQuery { pub deployment: String, /// Specify the recreate behavior. /// Default is 'DifferentCommand' - #[serde(default = "default_container_recreate_mode")] - pub recreate: TerminalRecreateMode, + pub recreate: Option, } /// Execute a command in the given containers shell. @@ -116,8 +114,7 @@ pub struct ExecuteDeploymentExecBody { pub command: String, /// Specify the recreate behavior. /// Default is 'DifferentCommand' - #[serde(default = "default_container_recreate_mode")] - pub recreate: TerminalRecreateMode, + pub recreate: Option, } /// Query to connect to a container exec session (interactive shell over websocket) on the given Stack / service. @@ -133,8 +130,7 @@ pub struct ConnectStackExecQuery { pub shell: String, /// Specify the recreate behavior. /// Default is 'DifferentCommand' - #[serde(default = "default_container_recreate_mode")] - pub recreate: TerminalRecreateMode, + pub recreate: Option, } /// Query to connect to a container attach session (interactive shell over websocket) on the given Stack / service. @@ -148,8 +144,7 @@ pub struct ConnectStackAttachQuery { pub service: String, /// Specify the recreate behavior. /// Default is 'DifferentCommand' - #[serde(default = "default_container_recreate_mode")] - pub recreate: TerminalRecreateMode, + pub recreate: Option, } /// Execute a command in the given containers shell. @@ -166,10 +161,5 @@ pub struct ExecuteStackExecBody { pub command: String, /// Specify the recreate behavior. /// Default is 'DifferentCommand' - #[serde(default = "default_container_recreate_mode")] - pub recreate: TerminalRecreateMode, -} - -fn default_container_recreate_mode() -> TerminalRecreateMode { - TerminalRecreateMode::DifferentCommand + pub recreate: Option, } diff --git a/client/core/ts/src/types.ts b/client/core/ts/src/types.ts index f43629f65..5882c3b74 100644 --- a/client/core/ts/src/types.ts +++ b/client/core/ts/src/types.ts @@ -3561,6 +3561,8 @@ export type ListAllDockerContainersResponse = ContainerListItem[]; export interface TerminalInfoWithServer { /** The server id. */ server_id: string; + /** The server name. */ + server_name: string; /** The name of the terminal. */ name: string; /** The root program / args of the pty */ @@ -4672,7 +4674,7 @@ export interface ConnectContainerAttachQuery { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** Query to connect to a container exec session (interactive shell over websocket) on the given server. */ @@ -4687,7 +4689,7 @@ export interface ConnectContainerExecQuery { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** @@ -4701,7 +4703,7 @@ export interface ConnectDeploymentAttachQuery { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** @@ -4717,7 +4719,7 @@ export interface ConnectDeploymentExecQuery { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** @@ -4733,7 +4735,7 @@ export interface ConnectStackAttachQuery { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** @@ -4751,7 +4753,7 @@ export interface ConnectStackExecQuery { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** Query to connect to a terminal (interactive shell over websocket) on the given server. */ @@ -5733,7 +5735,7 @@ export interface ExecuteContainerExecBody { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** Execute a command in the given containers shell. */ @@ -5748,7 +5750,7 @@ export interface ExecuteDeploymentExecBody { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** Execute a command in the given containers shell. */ @@ -5765,7 +5767,7 @@ export interface ExecuteStackExecBody { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** Execute a terminal command on the given server. */ @@ -5776,6 +5778,11 @@ export interface ExecuteTerminalBody { terminal: string; /** The command to execute. */ command: string; + /** + * Pass to init the terminal session + * for when the terminal doesn't already exist. + */ + init?: CreateTerminal; } /** diff --git a/frontend/public/client/types.d.ts b/frontend/public/client/types.d.ts index ebdc386bb..3e5c9a08d 100644 --- a/frontend/public/client/types.d.ts +++ b/frontend/public/client/types.d.ts @@ -3600,6 +3600,8 @@ export type ListAllDockerContainersResponse = ContainerListItem[]; export interface TerminalInfoWithServer { /** The server id. */ server_id: string; + /** The server name. */ + server_name: string; /** The name of the terminal. */ name: string; /** The root program / args of the pty */ @@ -4559,7 +4561,7 @@ export interface ConnectContainerAttachQuery { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** Query to connect to a container exec session (interactive shell over websocket) on the given server. */ export interface ConnectContainerExecQuery { @@ -4573,7 +4575,7 @@ export interface ConnectContainerExecQuery { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** * Query to connect to a container attach session (interactive shell over websocket) on the given Deployment. @@ -4586,7 +4588,7 @@ export interface ConnectDeploymentAttachQuery { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** * Query to connect to a container exec session (interactive shell over websocket) on the given Deployment. @@ -4601,7 +4603,7 @@ export interface ConnectDeploymentExecQuery { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** * Query to connect to a container attach session (interactive shell over websocket) on the given Stack / service. @@ -4616,7 +4618,7 @@ export interface ConnectStackAttachQuery { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** * Query to connect to a container exec session (interactive shell over websocket) on the given Stack / service. @@ -4633,7 +4635,7 @@ export interface ConnectStackExecQuery { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** Query to connect to a terminal (interactive shell over websocket) on the given server. */ export interface ConnectTerminalQuery { @@ -5540,7 +5542,7 @@ export interface ExecuteContainerExecBody { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** Execute a command in the given containers shell. */ export interface ExecuteDeploymentExecBody { @@ -5554,7 +5556,7 @@ export interface ExecuteDeploymentExecBody { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** Execute a command in the given containers shell. */ export interface ExecuteStackExecBody { @@ -5570,7 +5572,7 @@ export interface ExecuteStackExecBody { * Specify the recreate behavior. * Default is 'DifferentCommand' */ - recreate: TerminalRecreateMode; + recreate?: TerminalRecreateMode; } /** Execute a terminal command on the given server. */ export interface ExecuteTerminalBody { @@ -5580,6 +5582,11 @@ export interface ExecuteTerminalBody { terminal: string; /** The command to execute. */ command: string; + /** + * Pass to init the terminal session + * for when the terminal doesn't already exist. + */ + init?: CreateTerminal; } /** * Get pretty formatted monrun sync toml for all resources