Files
komodo/bin/periphery/src/api/mod.rs
T
5b211fb8f0 1.17.1 (#383)
* interpolate into slack / discord url

* fix js client docs

* js client should be type: module

* click table tags to toggle tag filter

* git token helper early return when empty provider

* reorder Stack fields

* action support interpolation doc

* Fix for the {account} login fails when the account name contains '$' (#385)

* GetDeploymentsSummary (#386)

* added GetDockerContainersSummary endpoint in rust api

* typescript stuff

* more autogenned typescript stuff

* fixed comments to be in line with actual behaviour

* fixed ReadResponse for GetDockerContainersSummary

* I64 -> u32 for response types

* more accurate error context

* backend for build files on host / ui defined

* core api supports non repo based build

* Ntfy as Alerter (#404)

* add ntfy alerter

* add ntfy alerter

* add ntfy alerter

---------

Co-authored-by: GFXSpeed <github@schafhaupt.com>

* Improve docs around running periphery in a container (#402)

* Add ports that need to be exposed if periphery is remote from core

* Spelling: overide -> override

* Add info about using a custom config file when running periphery in docker

* clean up ntfy alerter

* clean up ResourceSyncConfig

* update build cache after create / update

* refresh stack cache log

* Build UI Defined / file on host frontend

* update clap + rustls

* don't cleanup build files

* clean up dockerfile full path

* update BuildListItemInfo + UI table

* add Other Resources page

* add 5 second ws reconnection timeout

* Make listener address configurable & Add support for IPv6 (#405)

* Make listener address configurable

* Make listener address configurable for periphery

* rename listener_address -> bind_ip

---------

Co-authored-by: Jacky Fong <hello@huzky.dev>
Co-authored-by: Alex Shore <kntrllr@gmail.com>
Co-authored-by: Niklas <108990063+GFXSpeed@users.noreply.github.com>
Co-authored-by: GFXSpeed <github@schafhaupt.com>
Co-authored-by: theRAAPster <theraapster@gmail.com>
Co-authored-by: Daniel Vos <45820840+vosdev@users.noreply.github.com>
2025-04-14 14:48:12 -07:00

267 lines
6.6 KiB
Rust

use anyhow::Context;
use command::run_komodo_command;
use derive_variants::EnumVariants;
use futures::TryFutureExt;
use komodo_client::entities::{
SystemCommand,
config::{DockerRegistry, GitProvider},
update::Log,
};
use periphery_client::api::{
GetDockerLists, GetDockerListsResponse, GetHealth,
GetHealthResponse, GetVersion, GetVersionResponse,
ListDockerRegistries, ListGitProviders, ListSecrets, PruneSystem,
RunCommand, build::*, compose::*, container::*, git::*, image::*,
network::*, stats::*, volume::*,
};
use resolver_api::Resolve;
use response::Response;
use serde::{Deserialize, Serialize};
use crate::{config::periphery_config, docker::docker_client};
mod build;
mod compose;
mod container;
mod deploy;
mod git;
mod image;
mod network;
mod stats;
mod volume;
pub struct Args;
#[derive(
Serialize, Deserialize, Debug, Clone, Resolve, EnumVariants,
)]
#[args(Args)]
#[response(Response)]
#[error(serror::Error)]
#[variant_derive(Debug)]
#[serde(tag = "type", content = "params")]
#[allow(clippy::enum_variant_names, clippy::large_enum_variant)]
pub enum PeripheryRequest {
GetVersion(GetVersion),
GetHealth(GetHealth),
// Config (Read)
ListGitProviders(ListGitProviders),
ListDockerRegistries(ListDockerRegistries),
ListSecrets(ListSecrets),
// Stats / Info (Read)
GetSystemInformation(GetSystemInformation),
GetSystemStats(GetSystemStats),
GetSystemProcesses(GetSystemProcesses),
GetLatestCommit(GetLatestCommit),
// Generic shell execution
RunCommand(RunCommand),
// Repo (Write)
CloneRepo(CloneRepo),
PullRepo(PullRepo),
RenameRepo(RenameRepo),
DeleteRepo(DeleteRepo),
// Build
GetDockerfileContentsOnHost(GetDockerfileContentsOnHost),
WriteDockerfileContentsToHost(WriteDockerfileContentsToHost),
Build(Build),
PruneBuilders(PruneBuilders),
PruneBuildx(PruneBuildx),
// Compose (Read)
GetComposeContentsOnHost(GetComposeContentsOnHost),
GetComposeLog(GetComposeLog),
GetComposeLogSearch(GetComposeLogSearch),
// Compose (Write)
WriteComposeContentsToHost(WriteComposeContentsToHost),
WriteCommitComposeContents(WriteCommitComposeContents),
ComposePull(ComposePull),
ComposeUp(ComposeUp),
ComposeExecution(ComposeExecution),
// Container (Read)
InspectContainer(InspectContainer),
GetContainerLog(GetContainerLog),
GetContainerLogSearch(GetContainerLogSearch),
GetContainerStats(GetContainerStats),
GetContainerStatsList(GetContainerStatsList),
// Container (Write)
Deploy(Deploy),
StartContainer(StartContainer),
RestartContainer(RestartContainer),
PauseContainer(PauseContainer),
UnpauseContainer(UnpauseContainer),
StopContainer(StopContainer),
StartAllContainers(StartAllContainers),
RestartAllContainers(RestartAllContainers),
PauseAllContainers(PauseAllContainers),
UnpauseAllContainers(UnpauseAllContainers),
StopAllContainers(StopAllContainers),
RemoveContainer(RemoveContainer),
RenameContainer(RenameContainer),
PruneContainers(PruneContainers),
// Networks (Read)
InspectNetwork(InspectNetwork),
// Networks (Write)
CreateNetwork(CreateNetwork),
DeleteNetwork(DeleteNetwork),
PruneNetworks(PruneNetworks),
// Image (Read)
InspectImage(InspectImage),
ImageHistory(ImageHistory),
// Image (Write)
PullImage(PullImage),
DeleteImage(DeleteImage),
PruneImages(PruneImages),
// Volume (Read)
InspectVolume(InspectVolume),
// Volume (Write)
DeleteVolume(DeleteVolume),
PruneVolumes(PruneVolumes),
// All in one (Read)
GetDockerLists(GetDockerLists),
// All in one (Write)
PruneSystem(PruneSystem),
}
//
impl Resolve<Args> for GetHealth {
#[instrument(name = "GetHealth", level = "debug", skip_all)]
async fn resolve(
self,
_: &Args,
) -> serror::Result<GetHealthResponse> {
Ok(GetHealthResponse {})
}
}
//
impl Resolve<Args> for GetVersion {
#[instrument(name = "GetVersion", level = "debug", skip(self))]
async fn resolve(
self,
_: &Args,
) -> serror::Result<GetVersionResponse> {
Ok(GetVersionResponse {
version: env!("CARGO_PKG_VERSION").to_string(),
})
}
}
//
impl Resolve<Args> for ListGitProviders {
#[instrument(name = "ListGitProviders", level = "debug", skip_all)]
async fn resolve(
self,
_: &Args,
) -> serror::Result<Vec<GitProvider>> {
Ok(periphery_config().git_providers.clone())
}
}
impl Resolve<Args> for ListDockerRegistries {
#[instrument(
name = "ListDockerRegistries",
level = "debug",
skip_all
)]
async fn resolve(
self,
_: &Args,
) -> serror::Result<Vec<DockerRegistry>> {
Ok(periphery_config().docker_registries.clone())
}
}
//
impl Resolve<Args> for ListSecrets {
#[instrument(name = "ListSecrets", level = "debug", skip_all)]
async fn resolve(self, _: &Args) -> serror::Result<Vec<String>> {
Ok(
periphery_config()
.secrets
.keys()
.cloned()
.collect::<Vec<_>>(),
)
}
}
impl Resolve<Args> for GetDockerLists {
#[instrument(name = "GetDockerLists", level = "debug", skip_all)]
async fn resolve(
self,
_: &Args,
) -> serror::Result<GetDockerListsResponse> {
let docker = docker_client();
let containers =
docker.list_containers().await.map_err(Into::into);
// Should still try to retrieve other docker lists, but "in_use" will be false for images, networks, volumes
let _containers = match &containers {
Ok(containers) => containers.as_slice(),
Err(_) => &[],
};
let (networks, images, volumes, projects) = tokio::join!(
docker.list_networks(_containers).map_err(Into::into),
docker.list_images(_containers).map_err(Into::into),
docker.list_volumes(_containers).map_err(Into::into),
ListComposeProjects {}
.resolve(&Args)
.map_err(|e| e.error.into())
);
Ok(GetDockerListsResponse {
containers,
networks,
images,
volumes,
projects,
})
}
}
impl Resolve<Args> for RunCommand {
#[instrument(name = "RunCommand")]
async fn resolve(self, _: &Args) -> serror::Result<Log> {
let RunCommand {
command: SystemCommand { path, command },
} = self;
let res = tokio::spawn(async move {
let command = if path.is_empty() {
command
} else {
format!("cd {path} && {command}")
};
run_komodo_command("run command", None, command).await
})
.await
.context("failure in spawned task")?;
Ok(res)
}
}
impl Resolve<Args> for PruneSystem {
#[instrument(name = "PruneSystem", skip_all)]
async fn resolve(self, _: &Args) -> serror::Result<Log> {
let command = String::from("docker system prune -a -f --volumes");
Ok(run_komodo_command("Prune System", None, command).await)
}
}