add error status (#2746)

* add error status

* update types

* ṗ̶̰̙̓͒̈́ͅü̵̢̙̫̣ŗ̷̪̺̺͛g̴̲͉͎̬̒̇e̵̪̎̅͌ ̶̡̜̘͐͛t̶͎͍̣̿̍̐h̴͕̩͗̈́̎̑e̵͚͒̂͝ ̸̛͙̦͈͝v̶̱͙̬̽̔ọ̶̧̡̒̓i̸̬̲͍̋̈́d̴͉̀

* fix some extra voids

* add `package.rebuild`

* introduce error status and pkg rebuild and fix mocks

* minor fixes

* fix build

---------

Co-authored-by: Matt Hill <mattnine@protonmail.com>
This commit is contained in:
Aiden McClelland
2024-09-26 20:19:06 -06:00
committed by GitHub
parent db0695126f
commit e7fa94c3d3
49 changed files with 642 additions and 413 deletions

View File

@@ -173,7 +173,7 @@ export function makeEffects(context: EffectContext): Effects {
T.Effects["subcontainer"]["createFs"]
>
},
destroyFs(options: { guid: string }): Promise<void> {
destroyFs(options: { guid: string }): Promise<null> {
return rpcRound("subcontainer.destroy-fs", options) as ReturnType<
T.Effects["subcontainer"]["destroyFs"]
>
@@ -284,7 +284,7 @@ export function makeEffects(context: EffectContext): Effects {
>
},
setMainStatus(o: { status: "running" | "stopped" }): Promise<void> {
setMainStatus(o: { status: "running" | "stopped" }): Promise<null> {
return rpcRound("set-main-status", o) as ReturnType<
T.Effects["setHealth"]
>

View File

@@ -92,6 +92,7 @@ export class SystemForStartOs implements System {
const started = async (onTerm: () => Promise<void>) => {
await effects.setMainStatus({ status: "running" })
mainOnTerm = onTerm
return null
}
const daemons = await (
await this.abi.main({

View File

@@ -351,6 +351,14 @@ impl Debug for ErrorData {
}
}
impl std::error::Error for ErrorData {}
impl From<Error> for ErrorData {
fn from(value: Error) -> Self {
Self {
details: value.to_string(),
debug: format!("{:?}", value),
}
}
}
impl From<&RpcError> for ErrorData {
fn from(value: &RpcError) -> Self {
Self {

View File

@@ -5,6 +5,16 @@ use tracing::instrument;
use crate::util::Invoke;
use crate::Error;
pub async fn is_mountpoint(path: impl AsRef<Path>) -> Result<bool, Error> {
let is_mountpoint = tokio::process::Command::new("mountpoint")
.arg(path.as_ref())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.await?;
Ok(is_mountpoint.success())
}
#[instrument(skip_all)]
pub async fn bind<P0: AsRef<Path>, P1: AsRef<Path>>(
src: P0,
@@ -16,13 +26,7 @@ pub async fn bind<P0: AsRef<Path>, P1: AsRef<Path>>(
src.as_ref().display(),
dst.as_ref().display()
);
let is_mountpoint = tokio::process::Command::new("mountpoint")
.arg(dst.as_ref())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.await?;
if is_mountpoint.success() {
if is_mountpoint(&dst).await? {
unmount(dst.as_ref(), true).await?;
}
tokio::fs::create_dir_all(&src).await?;

View File

@@ -292,6 +292,13 @@ pub fn package<C: Context>() -> ParentHandler<C> {
.no_display()
.with_call_remote::<CliContext>(),
)
.subcommand(
"rebuild",
from_fn_async(service::rebuild)
.with_metadata("sync_db", Value::Bool(true))
.no_display()
.with_call_remote::<CliContext>(),
)
.subcommand("logs", logs::package_logs())
.subcommand(
"logs",

View File

@@ -126,7 +126,8 @@ impl LxcManager {
Path::new(LXC_CONTAINER_DIR).join(container).join("rootfs"),
true,
)
.await?;
.await
.log_err();
if tokio_stream::wrappers::ReadDirStream::new(
tokio::fs::read_dir(&rootfs_path).await?,
)

View File

@@ -589,6 +589,15 @@ impl ServiceActorSeed {
}
}
#[derive(Deserialize, Serialize, Parser, TS)]
pub struct RebuildParams {
pub id: PackageId,
}
pub async fn rebuild(ctx: RpcContext, RebuildParams { id }: RebuildParams) -> Result<(), Error> {
ctx.services.load(&ctx, &id, LoadDisposition::Retry).await?;
Ok(())
}
#[derive(Deserialize, Serialize, Parser, TS)]
pub struct ConnectParams {
pub id: PackageId,

View File

@@ -7,6 +7,7 @@ use futures::{Future, FutureExt};
use helpers::NonDetachingJoinHandle;
use imbl::OrdMap;
use imbl_value::InternedString;
use models::ErrorData;
use tokio::sync::{Mutex, OwnedRwLockReadGuard, OwnedRwLockWriteGuard, RwLock};
use tracing::instrument;
@@ -22,6 +23,7 @@ use crate::progress::{FullProgressTracker, PhaseProgressTrackerHandle, ProgressT
use crate::s9pk::manifest::PackageId;
use crate::s9pk::merkle_archive::source::FileSource;
use crate::s9pk::S9pk;
use crate::service::start_stop::StartStop;
use crate::service::{LoadDisposition, Service, ServiceRef};
use crate::status::MainStatus;
use crate::util::serde::Pem;
@@ -87,8 +89,30 @@ impl ServiceMap {
if let Some(service) = service.take() {
shutdown_err = service.shutdown().await;
}
// TODO: retry on error?
*service = Service::load(ctx, id, disposition).await?.map(From::from);
match Service::load(ctx, id, disposition).await {
Ok(s) => *service = s.into(),
Err(e) => {
let e = ErrorData::from(e);
ctx.db
.mutate(|db| {
if let Some(pde) = db.as_public_mut().as_package_data_mut().as_idx_mut(id) {
pde.as_status_mut().map_mutate(|s| {
Ok(MainStatus::Error {
on_rebuild: if s.running() {
StartStop::Start
} else {
StartStop::Stop
},
message: e.details,
debug: Some(e.debug),
})
})?;
}
Ok(())
})
.await?;
}
}
shutdown_err?;
Ok(())
}

View File

@@ -17,6 +17,11 @@ pub mod health_check;
#[serde(rename_all = "camelCase")]
#[serde(rename_all_fields = "camelCase")]
pub enum MainStatus {
Error {
on_rebuild: StartStop,
message: String,
debug: Option<String>,
},
Stopped,
Restarting,
Restoring,
@@ -43,12 +48,20 @@ impl MainStatus {
| MainStatus::Restarting
| MainStatus::BackingUp {
on_complete: StartStop::Start,
}
| MainStatus::Error {
on_rebuild: StartStop::Start,
..
} => true,
MainStatus::Stopped
| MainStatus::Restoring
| MainStatus::Stopping { .. }
| MainStatus::BackingUp {
on_complete: StartStop::Stop,
}
| MainStatus::Error {
on_rebuild: StartStop::Stop,
..
} => false,
}
}
@@ -70,7 +83,8 @@ impl MainStatus {
| MainStatus::Stopped
| MainStatus::Restoring
| MainStatus::Stopping { .. }
| MainStatus::Restarting => None,
| MainStatus::Restarting
| MainStatus::Error { .. } => None,
}
}
}

View File

@@ -31,14 +31,14 @@ export type Effects = {
constRetry: () => void
clearCallbacks: (
options: { only: number[] } | { except: number[] },
) => Promise<void>
) => Promise<null>
// action
action: {
/** Define an action that can be invoked by a user or service */
export(options: { id: ActionId; metadata: ActionMetadata }): Promise<void>
export(options: { id: ActionId; metadata: ActionMetadata }): Promise<null>
/** Remove all exported actions */
clear(options: { except: ActionId[] }): Promise<void>
clear(options: { except: ActionId[] }): Promise<null>
getInput(options: {
packageId?: PackageId
actionId: ActionId
@@ -50,23 +50,23 @@ export type Effects = {
}): Promise<ActionResult | null>
request<Input extends Record<string, unknown>>(
options: RequestActionParams,
): Promise<void>
): Promise<null>
clearRequests(
options: { only: ActionId[] } | { except: ActionId[] },
): Promise<void>
): Promise<null>
}
// control
/** restart this service's main function */
restart(): Promise<void>
restart(): Promise<null>
/** stop this service's main function */
shutdown(): Promise<void>
shutdown(): Promise<null>
/** indicate to the host os what runstate the service is in */
setMainStatus(options: SetMainStatus): Promise<void>
setMainStatus(options: SetMainStatus): Promise<null>
// dependency
/** Set the dependencies of what the service needs, usually run during the inputSpec action as a best practice */
setDependencies(options: { dependencies: Dependencies }): Promise<void>
setDependencies(options: { dependencies: Dependencies }): Promise<null>
/** Get the list of the dependencies, both the dynamic set by the effect of setDependencies and the end result any required in the manifest */
getDependencies(): Promise<DependencyRequirement[]>
/** Test whether current dependency requirements are satisfied */
@@ -86,11 +86,11 @@ export type Effects = {
/** Returns a list of the ids of all installed packages */
getInstalledPackages(): Promise<string[]>
/** grants access to certain paths in the store to dependents */
exposeForDependents(options: { paths: string[] }): Promise<void>
exposeForDependents(options: { paths: string[] }): Promise<null>
// health
/** sets the result of a health check */
setHealth(o: SetHealth): Promise<void>
setHealth(o: SetHealth): Promise<null>
// subcontainer
subcontainer: {
@@ -100,13 +100,13 @@ export type Effects = {
name: string | null
}): Promise<[string, string]>
/** A low level api used by SubContainer */
destroyFs(options: { guid: string }): Promise<void>
destroyFs(options: { guid: string }): Promise<null>
}
// net
// bind
/** Creates a host connected to the specified port with the provided options */
bind(options: BindParams): Promise<void>
bind(options: BindParams): Promise<null>
/** Get the port address for a service */
getServicePortForward(options: {
packageId?: PackageId
@@ -116,7 +116,7 @@ export type Effects = {
/** Removes all network bindings, called in the setupInputSpec */
clearBindings(options: {
except: { id: HostId; internalPort: number }[]
}): Promise<void>
}): Promise<null>
// host
/** Returns information about the specified host, if it exists */
getHostInfo(options: {
@@ -134,7 +134,7 @@ export type Effects = {
getContainerIp(): Promise<string>
// interface
/** Creates an interface bound to a specific host and port to show to the user */
exportServiceInterface(options: ExportServiceInterfaceParams): Promise<void>
exportServiceInterface(options: ExportServiceInterfaceParams): Promise<null>
/** Returns an exported service interface */
getServiceInterface(options: {
packageId?: PackageId
@@ -149,7 +149,7 @@ export type Effects = {
/** Removes all service interfaces */
clearServiceInterfaces(options: {
except: ServiceInterfaceId[]
}): Promise<void>
}): Promise<null>
// ssl
/** Returns a PEM encoded fullchain for the hostnames specified */
getSslCertificate: (options: {
@@ -178,10 +178,10 @@ export type Effects = {
/** Sets the value for the wrapper at the path, it will override, using the [JsonPath](https://jsonpath.com/) */
path: StorePath
value: ExtractStore
}): Promise<void>
}): Promise<null>
}
/** sets the version that this service's data has been migrated to */
setDataVersion(options: { version: string }): Promise<void>
setDataVersion(options: { version: string }): Promise<null>
/** returns the version that this service's data has been migrated to */
getDataVersion(): Promise<string | null>

View File

@@ -1,6 +1,7 @@
import { InputSpec } from "./input/builder"
import { ExtractInputSpecType } from "./input/builder/inputSpec"
import * as T from "../types"
import { once } from "../util"
export type Run<
A extends
@@ -130,21 +131,19 @@ export class Actions<
): Actions<Store, AllActions & { [id in A["id"]]: A }> {
return new Actions({ ...this.actions, [action.id]: action })
}
update(options: { effects: T.Effects }): Promise<void> {
const updater = async (options: { effects: T.Effects }) => {
for (let action of Object.values(this.actions)) {
await action.exportMetadata(options)
}
await options.effects.action.clear({ except: Object.keys(this.actions) })
async update(options: { effects: T.Effects }): Promise<null> {
options.effects = {
...options.effects,
constRetry: once(() => {
this.update(options) // yes, this reuses the options object, but the const retry function will be overwritten each time, so the once-ness is not a problem
}),
}
const updaterCtx = { options }
updaterCtx.options = {
effects: {
...options.effects,
constRetry: () => updater(updaterCtx.options),
},
for (let action of Object.values(this.actions)) {
await action.exportMetadata(options)
}
return updater(updaterCtx.options)
await options.effects.action.clear({ except: Object.keys(this.actions) })
return null
}
get<Id extends T.ActionId>(actionId: Id): AllActions[Id] {
return this.actions[actionId]

View File

@@ -13,15 +13,15 @@ export type CheckDependencies<DependencyId extends PackageId = PackageId> = {
) => boolean
satisfied: () => boolean
throwIfInstalledNotSatisfied: (packageId: DependencyId) => void
throwIfInstalledVersionNotSatisfied: (packageId: DependencyId) => void
throwIfRunningNotSatisfied: (packageId: DependencyId) => void
throwIfActionsNotSatisfied: (packageId: DependencyId) => void
throwIfInstalledNotSatisfied: (packageId: DependencyId) => null
throwIfInstalledVersionNotSatisfied: (packageId: DependencyId) => null
throwIfRunningNotSatisfied: (packageId: DependencyId) => null
throwIfActionsNotSatisfied: (packageId: DependencyId) => null
throwIfHealthNotSatisfied: (
packageId: DependencyId,
healthCheckId?: HealthCheckId,
) => void
throwIfNotSatisfied: (packageId?: DependencyId) => void
) => null
throwIfNotSatisfied: (packageId?: DependencyId) => null
}
export async function checkDependencies<
DependencyId extends PackageId = PackageId,
@@ -100,6 +100,7 @@ export async function checkDependencies<
if (!dep.result.installedVersion) {
throw new Error(`${dep.result.title || packageId} is not installed`)
}
return null
}
const throwIfInstalledVersionNotSatisfied = (packageId: DependencyId) => {
const dep = find(packageId)
@@ -117,12 +118,14 @@ export async function checkDependencies<
`Installed version ${dep.result.installedVersion} of ${dep.result.title || packageId} does not match expected version range ${dep.requirement.versionRange}`,
)
}
return null
}
const throwIfRunningNotSatisfied = (packageId: DependencyId) => {
const dep = find(packageId)
if (dep.requirement.kind === "running" && !dep.result.isRunning) {
throw new Error(`${dep.result.title || packageId} is not running`)
}
return null
}
const throwIfActionsNotSatisfied = (packageId: DependencyId) => {
const dep = find(packageId)
@@ -132,6 +135,7 @@ export async function checkDependencies<
`The following action requests have not been fulfilled: ${reqs.join(", ")}`,
)
}
return null
}
const throwIfHealthNotSatisfied = (
packageId: DependencyId,
@@ -158,6 +162,7 @@ export async function checkDependencies<
.join("; "),
)
}
return null
}
const throwIfPkgNotSatisfied = (packageId: DependencyId) => {
throwIfInstalledNotSatisfied(packageId)
@@ -165,6 +170,7 @@ export async function checkDependencies<
throwIfRunningNotSatisfied(packageId)
throwIfActionsNotSatisfied(packageId)
throwIfHealthNotSatisfied(packageId)
return null
}
const throwIfNotSatisfied = (packageId?: DependencyId) =>
packageId
@@ -182,6 +188,7 @@ export async function checkDependencies<
if (err.length) {
throw new Error(err.join("; "))
}
return null
})()
return {

View File

@@ -1,4 +1,5 @@
import * as T from "../types"
import { once } from "../util"
import { Dependency } from "./Dependency"
type DependencyType<Manifest extends T.Manifest> = {
@@ -17,40 +18,38 @@ type DependencyType<Manifest extends T.Manifest> = {
export function setupDependencies<Manifest extends T.Manifest>(
fn: (options: { effects: T.Effects }) => Promise<DependencyType<Manifest>>,
): (options: { effects: T.Effects }) => Promise<void> {
return (options: { effects: T.Effects }) => {
const updater = async (options: { effects: T.Effects }) => {
const dependencyType = await fn(options)
return await options.effects.setDependencies({
dependencies: Object.entries(dependencyType).map(
([
id,
{
data: { versionRange, ...x },
},
]) => ({
id,
...x,
...(x.type === "running"
? {
kind: "running",
healthChecks: x.healthChecks,
}
: {
kind: "exists",
}),
versionRange: versionRange.toString(),
}),
),
})
): (options: { effects: T.Effects }) => Promise<null> {
const cell = { updater: async (_: { effects: T.Effects }) => null }
cell.updater = async (options: { effects: T.Effects }) => {
options.effects = {
...options.effects,
constRetry: once(() => {
cell.updater(options)
}),
}
const updaterCtx = { options }
updaterCtx.options = {
effects: {
...options.effects,
constRetry: () => updater(updaterCtx.options),
},
}
return updater(updaterCtx.options)
const dependencyType = await fn(options)
return await options.effects.setDependencies({
dependencies: Object.entries(dependencyType).map(
([
id,
{
data: { versionRange, ...x },
},
]) => ({
id,
...x,
...(x.type === "running"
? {
kind: "running",
healthChecks: x.healthChecks,
}
: {
kind: "exists",
}),
versionRange: versionRange.toString(),
}),
),
})
}
return cell.updater
}

View File

@@ -1,4 +1,5 @@
import * as T from "../types"
import { once } from "../util"
import { AddressReceipt } from "./AddressReceipt"
declare const UpdateServiceInterfacesProof: unique symbol
@@ -21,34 +22,36 @@ export const setupServiceInterfaces: SetupServiceInterfaces = <
Output extends ServiceInterfacesReceipt,
>(
fn: SetServiceInterfaces<Output>,
) =>
((options: { effects: T.Effects }) => {
const updater = async (options: { effects: T.Effects }) => {
const bindings: T.BindId[] = []
const interfaces: T.ServiceInterfaceId[] = []
const res = await fn({
effects: {
...options.effects,
bind: (params: T.BindParams) => {
bindings.push({ id: params.id, internalPort: params.internalPort })
return options.effects.bind(params)
},
exportServiceInterface: (params: T.ExportServiceInterfaceParams) => {
interfaces.push(params.id)
return options.effects.exportServiceInterface(params)
},
},
})
await options.effects.clearBindings({ except: bindings })
await options.effects.clearServiceInterfaces({ except: interfaces })
return res
) => {
const cell = {
updater: (async (options: { effects: T.Effects }) =>
[] as any as Output) as UpdateServiceInterfaces<Output>,
}
cell.updater = (async (options: { effects: T.Effects }) => {
options.effects = {
...options.effects,
constRetry: once(() => {
cell.updater(options)
}),
}
const updaterCtx = { options }
updaterCtx.options = {
const bindings: T.BindId[] = []
const interfaces: T.ServiceInterfaceId[] = []
const res = await fn({
effects: {
...options.effects,
constRetry: () => updater(updaterCtx.options),
bind: (params: T.BindParams) => {
bindings.push({ id: params.id, internalPort: params.internalPort })
return options.effects.bind(params)
},
exportServiceInterface: (params: T.ExportServiceInterfaceParams) => {
interfaces.push(params.id)
return options.effects.exportServiceInterface(params)
},
},
}
return updater(updaterCtx.options)
})
await options.effects.clearBindings({ except: bindings })
await options.effects.clearServiceInterfaces({ except: interfaces })
return res
}) as UpdateServiceInterfaces<Output>
return cell.updater
}

View File

@@ -4,6 +4,12 @@ import type { NamedHealthCheckResult } from "./NamedHealthCheckResult"
import type { StartStop } from "./StartStop"
export type MainStatus =
| {
main: "error"
onRebuild: StartStop
message: string
debug: string | null
}
| { main: "stopped" }
| { main: "restarting" }
| { main: "restoring" }

View File

@@ -1,6 +1,6 @@
import { DataUrl, Manifest, MerkleArchiveCommitment } from "../osBindings"
import { ArrayBufferReader, MerkleArchive } from "./merkleArchive"
import mime from "mime"
import mime from "mime-types"
const magicAndVersion = new Uint8Array([59, 59, 2])
@@ -52,13 +52,14 @@ export class S9pk {
async icon(): Promise<DataUrl> {
const iconName = Object.keys(this.archive.contents.contents).find(
(name) =>
name.startsWith("icon.") && mime.getType(name)?.startsWith("image/"),
name.startsWith("icon.") &&
(mime.contentType(name) || null)?.startsWith("image/"),
)
if (!iconName) {
throw new Error("no icon found in archive")
}
return (
`data:${mime.getType(iconName)};base64,` +
`data:${mime.contentType(iconName)};base64,` +
Buffer.from(
await this.archive.contents.getPath([iconName])!.verifiedFileContents(),
).toString("base64")

View File

@@ -54,7 +54,7 @@ export namespace ExpectedExports {
*/
export type main = (options: {
effects: Effects
started(onTerm: () => PromiseLike<void>): PromiseLike<void>
started(onTerm: () => PromiseLike<void>): PromiseLike<null>
}) => Promise<DaemonBuildable>
/**
@@ -118,7 +118,7 @@ export type DaemonReceipt = {
}
export type Daemon = {
wait(): Promise<string>
term(): Promise<void>
term(): Promise<null>
[DaemonProof]: never
}
@@ -135,7 +135,7 @@ export type CommandType = string | [string, ...string[]]
export type DaemonReturned = {
wait(): Promise<unknown>
term(options?: { signal?: Signals; timeout?: number }): Promise<void>
term(options?: { signal?: Signals; timeout?: number }): Promise<null>
}
export declare const hostName: unique symbol

View File

@@ -1,17 +1,17 @@
import { boolean } from "ts-matches"
export type Vertex<VMetadata = void, EMetadata = void> = {
export type Vertex<VMetadata = null, EMetadata = null> = {
metadata: VMetadata
edges: Array<Edge<EMetadata, VMetadata>>
}
export type Edge<EMetadata = void, VMetadata = void> = {
export type Edge<EMetadata = null, VMetadata = null> = {
metadata: EMetadata
from: Vertex<VMetadata, EMetadata>
to: Vertex<VMetadata, EMetadata>
}
export class Graph<VMetadata = void, EMetadata = void> {
export class Graph<VMetadata = null, EMetadata = null> {
private readonly vertices: Array<Vertex<VMetadata, EMetadata>> = []
constructor() {}
addVertex(
@@ -46,7 +46,7 @@ export class Graph<VMetadata = void, EMetadata = void> {
}
findVertex(
predicate: (vertex: Vertex<VMetadata, EMetadata>) => boolean,
): Generator<Vertex<VMetadata, EMetadata>, void> {
): Generator<Vertex<VMetadata, EMetadata>, null> {
const veritces = this.vertices
function* gen() {
for (let vertex of veritces) {
@@ -54,6 +54,7 @@ export class Graph<VMetadata = void, EMetadata = void> {
yield vertex
}
}
return null
}
return gen()
}
@@ -75,13 +76,13 @@ export class Graph<VMetadata = void, EMetadata = void> {
from:
| Vertex<VMetadata, EMetadata>
| ((vertex: Vertex<VMetadata, EMetadata>) => boolean),
): Generator<Vertex<VMetadata, EMetadata>, void> {
): Generator<Vertex<VMetadata, EMetadata>, null> {
const visited: Array<Vertex<VMetadata, EMetadata>> = []
function* rec(
vertex: Vertex<VMetadata, EMetadata>,
): Generator<Vertex<VMetadata, EMetadata>, void> {
): Generator<Vertex<VMetadata, EMetadata>, null> {
if (visited.includes(vertex)) {
return
return null
}
visited.push(vertex)
yield vertex
@@ -99,6 +100,7 @@ export class Graph<VMetadata = void, EMetadata = void> {
}
}
}
return null
}
if (from instanceof Function) {
@@ -115,6 +117,7 @@ export class Graph<VMetadata = void, EMetadata = void> {
}
}
}
return null
})()
} else {
return rec(from)
@@ -124,13 +127,13 @@ export class Graph<VMetadata = void, EMetadata = void> {
to:
| Vertex<VMetadata, EMetadata>
| ((vertex: Vertex<VMetadata, EMetadata>) => boolean),
): Generator<Vertex<VMetadata, EMetadata>, void> {
): Generator<Vertex<VMetadata, EMetadata>, null> {
const visited: Array<Vertex<VMetadata, EMetadata>> = []
function* rec(
vertex: Vertex<VMetadata, EMetadata>,
): Generator<Vertex<VMetadata, EMetadata>, void> {
): Generator<Vertex<VMetadata, EMetadata>, null> {
if (visited.includes(vertex)) {
return
return null
}
visited.push(vertex)
yield vertex
@@ -148,6 +151,7 @@ export class Graph<VMetadata = void, EMetadata = void> {
}
}
}
return null
}
if (to instanceof Function) {
@@ -164,6 +168,7 @@ export class Graph<VMetadata = void, EMetadata = void> {
}
}
}
return null
})()
} else {
return rec(to)
@@ -176,7 +181,7 @@ export class Graph<VMetadata = void, EMetadata = void> {
to:
| Vertex<VMetadata, EMetadata>
| ((vertex: Vertex<VMetadata, EMetadata>) => boolean),
): Array<Edge<EMetadata, VMetadata>> | void {
): Array<Edge<EMetadata, VMetadata>> | null {
const isDone =
to instanceof Function
? to
@@ -186,12 +191,12 @@ export class Graph<VMetadata = void, EMetadata = void> {
function* check(
vertex: Vertex<VMetadata, EMetadata>,
path: Array<Edge<EMetadata, VMetadata>>,
): Generator<undefined, Array<Edge<EMetadata, VMetadata>> | undefined> {
): Generator<undefined, Array<Edge<EMetadata, VMetadata>> | null> {
if (isDone(vertex)) {
return path
}
if (visited.includes(vertex)) {
return
return null
}
visited.push(vertex)
yield
@@ -213,6 +218,7 @@ export class Graph<VMetadata = void, EMetadata = void> {
}
}
}
return null
}
if (from instanceof Function) {
@@ -240,5 +246,6 @@ export class Graph<VMetadata = void, EMetadata = void> {
}
}
}
return null
}
}

View File

@@ -13,13 +13,14 @@
"@noble/hashes": "^1.4.0",
"isomorphic-fetch": "^3.0.0",
"lodash.merge": "^4.6.2",
"mime": "^4.0.3",
"mime-types": "^2.1.35",
"ts-matches": "^5.5.1",
"yaml": "^2.2.2"
},
"devDependencies": {
"@types/jest": "^29.4.0",
"@types/lodash.merge": "^4.6.2",
"@types/mime-types": "^2.1.4",
"jest": "^29.4.3",
"peggy": "^3.0.2",
"prettier": "^3.2.5",
@@ -1249,6 +1250,13 @@
"@types/lodash": "*"
}
},
"node_modules/@types/mime-types": {
"version": "2.1.4",
"resolved": "https://registry.npmjs.org/@types/mime-types/-/mime-types-2.1.4.tgz",
"integrity": "sha512-lfU4b34HOri+kAY5UheuFMWPDOI+OPceBSHZKp69gEyTL/mmJ4cnU6Y/rlme3UL3GyOn6Y42hyIEw0/q8sWx5w==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/node": {
"version": "18.15.10",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.10.tgz",
@@ -3106,18 +3114,25 @@
"node": ">=8.6"
}
},
"node_modules/mime": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/mime/-/mime-4.0.3.tgz",
"integrity": "sha512-KgUb15Oorc0NEKPbvfa0wRU+PItIEZmiv+pyAO2i0oTIVTJhlzMclU7w4RXWQrSOVH5ax/p/CkIO7KI4OyFJTQ==",
"funding": [
"https://github.com/sponsors/broofa"
],
"bin": {
"mime": "bin/cli.js"
"node_modules/mime-db": {
"version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/mime-types": {
"version": "2.1.35",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
"license": "MIT",
"dependencies": {
"mime-db": "1.52.0"
},
"engines": {
"node": ">=16"
"node": ">= 0.6"
}
},
"node_modules/mimic-fn": {

View File

@@ -21,14 +21,14 @@
},
"homepage": "https://github.com/Start9Labs/start-sdk#readme",
"dependencies": {
"isomorphic-fetch": "^3.0.0",
"lodash.merge": "^4.6.2",
"mime": "^4.0.3",
"ts-matches": "^5.5.1",
"yaml": "^2.2.2",
"@iarna/toml": "^2.2.5",
"@noble/curves": "^1.4.0",
"@noble/hashes": "^1.4.0"
"@noble/hashes": "^1.4.0",
"isomorphic-fetch": "^3.0.0",
"lodash.merge": "^4.6.2",
"mime-types": "^2.1.35",
"ts-matches": "^5.5.1",
"yaml": "^2.2.2"
},
"prettier": {
"trailingComma": "all",
@@ -39,6 +39,7 @@
"devDependencies": {
"@types/jest": "^29.4.0",
"@types/lodash.merge": "^4.6.2",
"@types/mime-types": "^2.1.4",
"jest": "^29.4.3",
"peggy": "^3.0.2",
"prettier": "^3.2.5",

View File

@@ -563,7 +563,7 @@ export class StartSdk<Manifest extends T.Manifest, Store> {
setupMain: (
fn: (o: {
effects: Effects
started(onTerm: () => PromiseLike<void>): PromiseLike<void>
started(onTerm: () => PromiseLike<void>): PromiseLike<null>
}) => Promise<Daemons<Manifest, any>>,
) => setupMain<Manifest, Store>(fn),
/**
@@ -657,12 +657,12 @@ export class StartSdk<Manifest extends T.Manifest, Store> {
) => InputSpec.of<Spec, Store>(spec),
},
Daemons: {
of(inputSpec: {
of(options: {
effects: Effects
started: (onTerm: () => PromiseLike<void>) => PromiseLike<void>
started: (onTerm: () => PromiseLike<void>) => PromiseLike<null>
healthReceipts: HealthReceipt[]
}) {
return Daemons.of<Manifest>(inputSpec)
return Daemons.of<Manifest>(options)
},
},
List: {

View File

@@ -12,7 +12,7 @@ export function setupInit<Manifest extends T.Manifest, Store>(
install: Install<Manifest, Store>,
uninstall: Uninstall<Manifest, Store>,
setServiceInterfaces: UpdateServiceInterfaces<any>,
setDependencies: (options: { effects: T.Effects }) => Promise<void>,
setDependencies: (options: { effects: T.Effects }) => Promise<null>,
actions: Actions<Store, any>,
exposedStore: ExposedStorePaths,
): {

View File

@@ -2,7 +2,7 @@ import * as T from "../../../base/lib/types"
export type InstallFn<Manifest extends T.Manifest, Store> = (opts: {
effects: T.Effects
}) => Promise<void>
}) => Promise<null>
export class Install<Manifest extends T.Manifest, Store> {
private constructor(readonly fn: InstallFn<Manifest, Store>) {}
static of<Manifest extends T.Manifest, Store>(

View File

@@ -2,7 +2,7 @@ import * as T from "../../../base/lib/types"
export type UninstallFn<Manifest extends T.Manifest, Store> = (opts: {
effects: T.Effects
}) => Promise<void>
}) => Promise<null>
export class Uninstall<Manifest extends T.Manifest, Store> {
private constructor(readonly fn: UninstallFn<Manifest, Store>) {}
static of<Manifest extends T.Manifest, Store>(

View File

@@ -43,8 +43,8 @@ export class CommandController {
| undefined
cwd?: string | undefined
user?: string | undefined
onStdout?: (x: Buffer) => void
onStderr?: (x: Buffer) => void
onStdout?: (x: Buffer) => null
onStderr?: (x: Buffer) => null
},
) => {
const commands = splitCommand(command)

View File

@@ -37,8 +37,8 @@ export class Daemon {
| undefined
cwd?: string | undefined
user?: string | undefined
onStdout?: (x: Buffer) => void
onStderr?: (x: Buffer) => void
onStdout?: (x: Buffer) => null
onStderr?: (x: Buffer) => null
sigtermTimeout?: number
},
) => {

View File

@@ -74,7 +74,7 @@ export class Daemons<Manifest extends T.Manifest, Ids extends string>
{
private constructor(
readonly effects: T.Effects,
readonly started: (onTerm: () => PromiseLike<void>) => PromiseLike<void>,
readonly started: (onTerm: () => PromiseLike<void>) => PromiseLike<null>,
readonly daemons: Promise<Daemon>[],
readonly ids: Ids[],
readonly healthDaemons: HealthDaemon[],
@@ -86,17 +86,17 @@ export class Daemons<Manifest extends T.Manifest, Ids extends string>
*
* Daemons run in the order they are defined, with latter daemons being capable of
* depending on prior daemons
* @param inputSpec
* @param options
* @returns
*/
static of<Manifest extends T.Manifest>(inputSpec: {
static of<Manifest extends T.Manifest>(options: {
effects: T.Effects
started: (onTerm: () => PromiseLike<void>) => PromiseLike<void>
started: (onTerm: () => PromiseLike<void>) => PromiseLike<null>
healthReceipts: HealthReceipt[]
}) {
return new Daemons<Manifest, never>(
inputSpec.effects,
inputSpec.started,
options.effects,
options.started,
[],
[],
[],

View File

@@ -81,7 +81,7 @@ export class HealthDaemon {
}
}
private healthCheckCleanup: (() => void) | null = null
private healthCheckCleanup: (() => null) | null = null
private turnOffHealthCheck() {
this.healthCheckCleanup?.()
}
@@ -125,6 +125,7 @@ export class HealthDaemon {
this.healthCheckCleanup = () => {
setStatus({ done: true })
this.healthCheckCleanup = null
return null
}
}

View File

@@ -17,7 +17,7 @@ export const DEFAULT_SIGTERM_TIMEOUT = 30_000
export const setupMain = <Manifest extends T.Manifest, Store>(
fn: (o: {
effects: T.Effects
started(onTerm: () => PromiseLike<void>): PromiseLike<void>
started(onTerm: () => PromiseLike<void>): PromiseLike<null>
}) => Promise<Daemons<Manifest, any>>,
): T.ExpectedExports.main => {
return async (options) => {

View File

@@ -27,7 +27,7 @@ const TIMES_TO_WAIT_FOR_PROC = 100
* case where the subcontainer isn't owned by the process, the subcontainer shouldn't be destroyed.
*/
export interface ExecSpawnable {
get destroy(): undefined | (() => Promise<void>)
get destroy(): undefined | (() => Promise<null>)
exec(
command: string[],
options?: CommandOptions & ExecOptions,
@@ -47,7 +47,7 @@ export interface ExecSpawnable {
export class SubContainer implements ExecSpawnable {
private leader: cp.ChildProcess
private leaderExited: boolean = false
private waitProc: () => Promise<void>
private waitProc: () => Promise<null>
private constructor(
readonly effects: T.Effects,
readonly imageId: T.ImageId,
@@ -79,7 +79,7 @@ export class SubContainer implements ExecSpawnable {
}
await wait(1)
}
resolve()
resolve(null)
}),
)
}
@@ -180,12 +180,12 @@ export class SubContainer implements ExecSpawnable {
if (this.leaderExited) {
return
}
return new Promise<void>((resolve, reject) => {
return new Promise<null>((resolve, reject) => {
try {
let timeout = setTimeout(() => this.leader.kill("SIGKILL"), 30000)
this.leader.on("exit", () => {
clearTimeout(timeout)
resolve()
resolve(null)
})
if (!this.leader.kill("SIGTERM")) {
reject(new Error("kill(2) failed"))
@@ -201,6 +201,7 @@ export class SubContainer implements ExecSpawnable {
const guid = this.guid
await this.killLeader()
await this.effects.subcontainer.destroyFs({ guid })
return null
}
}
@@ -245,16 +246,16 @@ export class SubContainer implements ExecSpawnable {
options || {},
)
if (options?.input) {
await new Promise<void>((resolve, reject) =>
await new Promise<null>((resolve, reject) =>
child.stdin.write(options.input, (e) => {
if (e) {
reject(e)
} else {
resolve()
resolve(null)
}
}),
)
await new Promise<void>((resolve) => child.stdin.end(resolve))
await new Promise<null>((resolve) => child.stdin.end(resolve))
}
const pid = child.pid
const stdout = { data: "" as string | Buffer }

6
web/package-lock.json generated
View File

@@ -57,6 +57,7 @@
"ng-qrcode": "^7.0.0",
"node-jose": "^2.2.0",
"patch-db-client": "file:../patch-db/client",
"path-browserify": "^1.0.1",
"pbkdf2": "^3.1.2",
"rxjs": "^7.8.1",
"swiper": "^8.2.4",
@@ -123,13 +124,14 @@
"@noble/hashes": "^1.4.0",
"isomorphic-fetch": "^3.0.0",
"lodash.merge": "^4.6.2",
"mime": "^4.0.3",
"mime-types": "^2.1.35",
"ts-matches": "^5.5.1",
"yaml": "^2.2.2"
},
"devDependencies": {
"@types/jest": "^29.4.0",
"@types/lodash.merge": "^4.6.2",
"@types/mime-types": "^2.1.4",
"jest": "^29.4.3",
"peggy": "^3.0.2",
"prettier": "^3.2.5",
@@ -11731,7 +11733,7 @@
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz",
"integrity": "sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g==",
"optional": true
"license": "MIT"
},
"node_modules/path-exists": {
"version": "4.0.0",

View File

@@ -80,6 +80,7 @@
"ng-qrcode": "^7.0.0",
"node-jose": "^2.2.0",
"patch-db-client": "file:../patch-db/client",
"path-browserify": "^1.0.1",
"pbkdf2": "^3.1.2",
"rxjs": "^7.8.1",
"swiper": "^8.2.4",

View File

@@ -11,10 +11,19 @@
<ion-item-group *ngIf="pkg$ | async as pkg">
<!-- ** standard actions ** -->
<ion-item-divider>Standard Actions</ion-item-divider>
<app-actions-item
[action]="{
name: 'Rebuild Service',
description: 'Rebuilds the service container. It is harmless and only takes a few seconds to complete, but it should only be necessary if a StartOS bug is preventing dependencies, interfaces, or actions from synchronizing.',
visibility: 'enabled'
}"
icon="construct-outline"
(click)="rebuild(pkg.manifest.id)"
></app-actions-item>
<app-actions-item
[action]="{
name: 'Uninstall',
description: 'This will uninstall the service from StartOS and delete all data permanently.',
description: 'Uninstalls this service from StartOS and delete all data permanently.',
visibility: 'enabled'
}"
icon="trash-outline"

View File

@@ -1,14 +1,12 @@
import { ChangeDetectionStrategy, Component, Input } from '@angular/core'
import { ActivatedRoute } from '@angular/router'
import { AlertController, NavController } from '@ionic/angular'
import { ErrorService, getPkgId, LoadingService } from '@start9labs/shared'
import { getPkgId } from '@start9labs/shared'
import { T } from '@start9labs/start-sdk'
import { PatchDB } from 'patch-db-client'
import { ApiService } from 'src/app/services/api/embassy-api.service'
import { ActionService } from 'src/app/services/action.service'
import { StandardActionsService } from 'src/app/services/standard-actions.service'
import { DataModel } from 'src/app/services/patch-db/data-model'
import { getAllPackages, getManifest } from 'src/app/util/get-package-data'
import { hasCurrentDeps } from 'src/app/util/has-deps'
import { getManifest } from 'src/app/util/get-package-data'
import { filter, map } from 'rxjs'
@Component({
@@ -35,13 +33,9 @@ export class AppActionsPage {
constructor(
private readonly route: ActivatedRoute,
private readonly api: ApiService,
private readonly alertCtrl: AlertController,
private readonly errorService: ErrorService,
private readonly loader: LoadingService,
private readonly navCtrl: NavController,
private readonly patch: PatchDB<DataModel>,
private readonly actionService: ActionService,
private readonly standardActionsService: StandardActionsService,
) {}
async handleAction(
@@ -55,51 +49,12 @@ export class AppActionsPage {
)
}
async tryUninstall(manifest: T.Manifest): Promise<void> {
let message =
manifest.alerts.uninstall ||
`Uninstalling ${manifest.title} will permanently delete its data`
if (hasCurrentDeps(this.pkgId, await getAllPackages(this.patch))) {
message = `${message}. Services that depend on ${manifest.title} will no longer work properly and may crash`
}
const alert = await this.alertCtrl.create({
header: 'Warning',
message,
buttons: [
{
text: 'Cancel',
role: 'cancel',
},
{
text: 'Uninstall',
handler: () => {
this.uninstall()
},
cssClass: 'enter-click',
},
],
cssClass: 'alert-warning-message',
})
await alert.present()
async rebuild(id: string) {
return this.standardActionsService.rebuild(id)
}
private async uninstall() {
const loader = this.loader.open(`Beginning uninstall...`).subscribe()
try {
await this.api.uninstallPackage({ id: this.pkgId })
this.api
.setDbValue<boolean>(['ackInstructions', this.pkgId], false)
.catch(e => console.error('Failed to mark instructions as unseen', e))
this.navCtrl.navigateRoot('/services')
} catch (e: any) {
this.errorService.handleError(e)
} finally {
loader.unsubscribe()
}
async tryUninstall(manifest: T.Manifest) {
return this.standardActionsService.tryUninstall(manifest)
}
}

View File

@@ -18,6 +18,7 @@ import { AppShowDependenciesComponent } from './components/app-show-dependencies
import { AppShowMenuComponent } from './components/app-show-menu/app-show-menu.component'
import { AppShowHealthChecksComponent } from './components/app-show-health-checks/app-show-health-checks.component'
import { AppShowAdditionalComponent } from './components/app-show-additional/app-show-additional.component'
import { AppShowErrorComponent } from './components/app-show-error/app-show-error.component'
import { HealthColorPipe } from './pipes/health-color.pipe'
import { ToHealthChecksPipe } from './pipes/to-health-checks.pipe'
import { ToButtonsPipe } from './pipes/to-buttons.pipe'
@@ -43,6 +44,7 @@ const routes: Routes = [
AppShowMenuComponent,
AppShowHealthChecksComponent,
AppShowAdditionalComponent,
AppShowErrorComponent,
],
imports: [
CommonModule,

View File

@@ -16,9 +16,9 @@
<ion-item-group *ngIf="pkgPlus.status as status">
<!-- ** status ** -->
<app-show-status [pkg]="pkg" [status]="status"></app-show-status>
<!-- ** installed && !backingUp ** -->
<!-- ** installed && !backingUp && !error ** -->
<ng-container
*ngIf="isInstalled(pkg) && status.primary !== 'backingUp'"
*ngIf="isInstalled(pkg) && status.primary !== 'backingUp' && status.primary !== 'error'"
>
<!-- ** health checks ** -->
<app-show-health-checks
@@ -35,6 +35,11 @@
<!-- ** additional ** -->
<app-show-additional [pkg]="pkg"></app-show-additional>
</ng-container>
<app-show-error
*ngIf="pkg.status.main === 'error'"
[manifest]="pkgPlus.manifest"
[error]="pkg.status"
></app-show-error>
</ion-item-group>
</ng-template>
</ion-content>

View File

@@ -45,7 +45,7 @@ export interface DependencyInfo {
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppShowPage {
private readonly pkgId = getPkgId(this.route)
readonly pkgId = getPkgId(this.route)
readonly pkgPlus$ = combineLatest([
this.patch.watch$('packageData'),
@@ -58,9 +58,11 @@ export class AppShowPage {
}),
map(([allPkgs, depErrors]) => {
const pkg = allPkgs[this.pkgId]
const manifest = getManifest(pkg)
return {
pkg,
dependencies: this.getDepInfo(pkg, allPkgs, depErrors),
manifest,
dependencies: this.getDepInfo(pkg, manifest, allPkgs, depErrors),
status: renderPkgStatus(pkg, depErrors),
}
}),
@@ -84,11 +86,10 @@ export class AppShowPage {
private getDepInfo(
pkg: PackageDataEntry,
manifest: T.Manifest,
allPkgs: AllPackageData,
depErrors: PkgDependencyErrors,
): DependencyInfo[] {
const manifest = getManifest(pkg)
return Object.keys(pkg.currentDependencies).map(id =>
this.getDepValues(pkg, allPkgs, manifest, id, depErrors),
)

View File

@@ -0,0 +1,31 @@
<ion-item-divider>Message</ion-item-divider>
<div class="code-block ion-margin">
<code>
<ion-text color="warning">{{ error.message }}</ion-text>
</code>
</div>
<ion-item-divider>Actions</ion-item-divider>
<div class="ion-margin">
<p>
<b>Rebuild Container</b>
is harmless action that and only takes a few seconds to complete. It will
likely resolve this issue.
<b>Uninstall Service</b>
is a dangerous action that will remove the service from StartOS and wipe all
its data.
</p>
<ion-button class="ion-margin-end" (click)="rebuild()">
Rebuild Container
</ion-button>
<ion-button (click)="tryUninstall()" color="danger">
Uninstall Service
</ion-button>
</div>
<ng-container *ngIf="error.debug">
<ion-item-divider>Full Stack Trace</ion-item-divider>
<div class="code-block ion-margin">
<code>{{ error.message }}</code>
</div>
</ng-container>

View File

@@ -0,0 +1,45 @@
import { ChangeDetectionStrategy, Component, Input } from '@angular/core'
import { ToastController } from '@ionic/angular'
import { copyToClipboard } from '@start9labs/shared'
import { T } from '@start9labs/start-sdk'
import { StandardActionsService } from 'src/app/services/standard-actions.service'
@Component({
selector: 'app-show-error',
templateUrl: 'app-show-error.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppShowErrorComponent {
@Input()
manifest!: T.Manifest
@Input()
error!: T.MainStatus & { main: 'error' }
constructor(
private readonly toastCtrl: ToastController,
private readonly standardActionsService: StandardActionsService,
) {}
async copy(text: string): Promise<void> {
const success = await copyToClipboard(text)
const message = success
? 'Copied to clipboard!'
: 'Failed to copy to clipboard.'
const toast = await this.toastCtrl.create({
header: message,
position: 'bottom',
duration: 1000,
})
await toast.present()
}
async rebuild() {
return this.standardActionsService.rebuild(this.manifest.id)
}
async tryUninstall() {
return this.standardActionsService.tryUninstall(this.manifest)
}
}

View File

@@ -11,7 +11,14 @@
</ion-label>
</ion-item>
<ng-container *ngIf="isInstalled(pkg) && (connection$ | async)">
<ng-container
*ngIf="
isInstalled(pkg) &&
pkg.status.main !== 'backingUp' &&
pkg.status.main !== 'error' &&
(connection$ | async)
"
>
<ion-grid>
<ion-row style="padding-left: 12px">
<ion-col>

View File

@@ -257,6 +257,9 @@ export module RR {
export type StopPackageReq = { id: string } // package.stop
export type StopPackageRes = null
export type RebuildPackageReq = { id: string } // package.rebuild
export type RebuildPackageRes = null
export type UninstallPackageReq = { id: string } // package.uninstall
export type UninstallPackageRes = null

View File

@@ -249,6 +249,10 @@ export abstract class ApiService {
abstract stopPackage(params: RR.StopPackageReq): Promise<RR.StopPackageRes>
abstract rebuildPackage(
params: RR.RebuildPackageReq,
): Promise<RR.RebuildPackageRes>
abstract uninstallPackage(
params: RR.UninstallPackageReq,
): Promise<RR.UninstallPackageRes>

View File

@@ -498,6 +498,12 @@ export class LiveApiService extends ApiService {
return this.rpcRequest({ method: 'package.stop', params })
}
async rebuildPackage(
params: RR.RebuildPackageReq,
): Promise<RR.RebuildPackageRes> {
return this.rpcRequest({ method: 'package.rebuild', params })
}
async uninstallPackage(
params: RR.UninstallPackageReq,
): Promise<RR.UninstallPackageRes> {

View File

@@ -2,10 +2,12 @@ import { Injectable } from '@angular/core'
import { Log, RPCErrorDetails, RPCOptions, pauseFor } from '@start9labs/shared'
import { ApiService } from './embassy-api.service'
import {
AddOperation,
Operation,
PatchOp,
pathFromArray,
RemoveOperation,
ReplaceOperation,
Revision,
} from 'patch-db-client'
import {
@@ -636,14 +638,14 @@ export class MockApiService extends ApiService {
async createBackup(params: RR.CreateBackupReq): Promise<RR.CreateBackupRes> {
await pauseFor(2000)
const path = '/serverInfo/statusInfo/backupProgress'
const serverPath = '/serverInfo/statusInfo/backupProgress'
const ids = params.packageIds
setTimeout(async () => {
for (let i = 0; i < ids.length; i++) {
const id = ids[i]
const appPath = `/packageData/${id}/status/main/status`
const appPatch = [
const appPath = `/packageData/${id}/status/main/`
const appPatch: ReplaceOperation<T.MainStatus['main']>[] = [
{
op: PatchOp.REPLACE,
path: appPath,
@@ -660,40 +662,43 @@ export class MockApiService extends ApiService {
value: 'stopped',
},
])
this.mockRevision([
const serverPatch: ReplaceOperation<T.BackupProgress['complete']>[] = [
{
op: PatchOp.REPLACE,
path: `${path}/${id}/complete`,
path: `${serverPath}/${id}/complete`,
value: true,
},
])
]
this.mockRevision(serverPatch)
}
await pauseFor(1000)
// set server back to running
const lastPatch = [
// remove backupProgress
const lastPatch: ReplaceOperation<T.ServerStatus['backupProgress']>[] = [
{
op: PatchOp.REPLACE,
path,
path: serverPath,
value: null,
},
]
this.mockRevision(lastPatch)
}, 500)
const originalPatch = [
{
op: PatchOp.REPLACE,
path,
value: ids.reduce((acc, val) => {
return {
...acc,
[val]: { complete: false },
}
}, {}),
},
]
const originalPatch: ReplaceOperation<T.ServerStatus['backupProgress']>[] =
[
{
op: PatchOp.REPLACE,
path: serverPath,
value: ids.reduce((acc, val) => {
return {
...acc,
[val]: { complete: false },
}
}, {}),
},
]
this.mockRevision(originalPatch)
@@ -750,7 +755,7 @@ export class MockApiService extends ApiService {
this.installProgress(params.id)
}, 1000)
const patch: Operation<
const patch: AddOperation<
PackageDataEntry<InstallingState | UpdatingState>
>[] = [
{
@@ -799,7 +804,7 @@ export class MockApiService extends ApiService {
params: RR.RestorePackagesReq,
): Promise<RR.RestorePackagesRes> {
await pauseFor(2000)
const patch: Operation<PackageDataEntry>[] = params.ids.map(id => {
const patch: AddOperation<PackageDataEntry>[] = params.ids.map(id => {
setTimeout(async () => {
this.installProgress(id)
}, 2000)
@@ -826,76 +831,61 @@ export class MockApiService extends ApiService {
}
async startPackage(params: RR.StartPackageReq): Promise<RR.StartPackageRes> {
const path = `/packageData/${params.id}/status/main`
const path = `/packageData/${params.id}/status`
await pauseFor(2000)
setTimeout(async () => {
const patch2 = [
const patch2: ReplaceOperation<T.MainStatus & { main: 'running' }>[] = [
{
op: PatchOp.REPLACE,
path: path + '/status',
value: 'running',
},
{
op: PatchOp.REPLACE,
path: path + '/started',
value: new Date().toISOString(),
path,
value: {
main: 'running',
started: new Date().toISOString(),
health: {
'ephemeral-health-check': {
name: 'Ephemeral Health Check',
result: 'starting',
message: null,
},
'unnecessary-health-check': {
name: 'Unnecessary Health Check',
result: 'disabled',
message: 'Custom disabled message',
},
'chain-state': {
name: 'Chain State',
result: 'loading',
message: 'Bitcoin is syncing from genesis',
},
'p2p-interface': {
name: 'P2P Interface',
result: 'success',
message: null,
},
'rpc-interface': {
name: 'RPC Interface',
result: 'failure',
message: 'Custom failure message',
},
},
},
},
]
this.mockRevision(patch2)
const patch3 = [
{
op: PatchOp.REPLACE,
path: path + '/health',
value: {
'ephemeral-health-check': {
result: 'starting',
},
'unnecessary-health-check': {
result: 'disabled',
},
},
},
]
this.mockRevision(patch3)
await pauseFor(2000)
const patch4 = [
{
op: PatchOp.REPLACE,
path: path + '/health',
value: {
'ephemeral-health-check': {
result: 'starting',
},
'unnecessary-health-check': {
result: 'disabled',
},
'chain-state': {
result: 'loading',
message: 'Bitcoin is syncing from genesis',
},
'p2p-interface': {
result: 'success',
},
'rpc-interface': {
result: 'failure',
error: 'RPC interface unreachable.',
},
},
},
]
this.mockRevision(patch4)
}, 2000)
const originalPatch = [
const originalPatch: ReplaceOperation<
T.MainStatus & { main: 'starting' }
>[] = [
{
op: PatchOp.REPLACE,
path: path + '/status',
value: 'starting',
path,
value: {
main: 'starting',
health: {},
},
},
]
@@ -907,74 +897,57 @@ export class MockApiService extends ApiService {
async restartPackage(
params: RR.RestartPackageReq,
): Promise<RR.RestartPackageRes> {
// first enact stop
await pauseFor(2000)
const path = `/packageData/${params.id}/status/main`
const path = `/packageData/${params.id}/status`
setTimeout(async () => {
const patch2: Operation<any>[] = [
const patch2: ReplaceOperation<T.MainStatus & { main: 'running' }>[] = [
{
op: PatchOp.REPLACE,
path: path + '/status',
value: 'starting',
},
{
op: PatchOp.ADD,
path: path + '/restarting',
value: true,
path,
value: {
main: 'running',
started: new Date().toISOString(),
health: {
'ephemeral-health-check': {
name: 'Ephemeral Health Check',
result: 'starting',
message: null,
},
'unnecessary-health-check': {
name: 'Unnecessary Health Check',
result: 'disabled',
message: 'Custom disabled message',
},
'chain-state': {
name: 'Chain State',
result: 'loading',
message: 'Bitcoin is syncing from genesis',
},
'p2p-interface': {
name: 'P2P Interface',
result: 'success',
message: null,
},
'rpc-interface': {
name: 'RPC Interface',
result: 'failure',
message: 'Custom failure message',
},
},
},
},
]
this.mockRevision(patch2)
await pauseFor(2000)
const patch3: Operation<any>[] = [
{
op: PatchOp.REPLACE,
path: path + '/status',
value: 'running',
},
{
op: PatchOp.REMOVE,
path: path + '/restarting',
},
{
op: PatchOp.REPLACE,
path: path + '/health',
value: {
'ephemeral-health-check': {
result: 'starting',
},
'unnecessary-health-check': {
result: 'disabled',
},
'chain-state': {
result: 'loading',
message: 'Bitcoin is syncing from genesis',
},
'p2p-interface': {
result: 'success',
},
'rpc-interface': {
result: 'failure',
error: 'RPC interface unreachable.',
},
},
} as any,
]
this.mockRevision(patch3)
}, this.revertTime)
const patch = [
const patch: ReplaceOperation<T.MainStatus & { main: 'restarting' }>[] = [
{
op: PatchOp.REPLACE,
path: path + '/status',
value: 'restarting',
},
{
op: PatchOp.REPLACE,
path: path + '/health',
value: {},
path,
value: {
main: 'restarting',
},
},
]
@@ -985,29 +958,24 @@ export class MockApiService extends ApiService {
async stopPackage(params: RR.StopPackageReq): Promise<RR.StopPackageRes> {
await pauseFor(2000)
const path = `/packageData/${params.id}/status/main`
const path = `/packageData/${params.id}/status`
setTimeout(() => {
const patch2 = [
const patch2: ReplaceOperation<T.MainStatus & { main: 'stopped' }>[] = [
{
op: PatchOp.REPLACE,
path: path,
value: {
status: 'stopped',
},
value: { main: 'stopped' },
},
]
this.mockRevision(patch2)
}, this.revertTime)
const patch = [
const patch: ReplaceOperation<T.MainStatus & { main: 'stopping' }>[] = [
{
op: PatchOp.REPLACE,
path: path,
value: {
status: 'stopping',
timeout: '35s',
},
value: { main: 'stopping' },
},
]
@@ -1016,6 +984,12 @@ export class MockApiService extends ApiService {
return null
}
async rebuildPackage(
params: RR.RebuildPackageReq,
): Promise<RR.RebuildPackageRes> {
return this.restartPackage(params)
}
async uninstallPackage(
params: RR.UninstallPackageReq,
): Promise<RR.UninstallPackageRes> {
@@ -1031,7 +1005,7 @@ export class MockApiService extends ApiService {
this.mockRevision(patch2)
}, this.revertTime)
const patch = [
const patch: ReplaceOperation<T.PackageState['state']>[] = [
{
op: PatchOp.REPLACE,
path: `/packageData/${params.id}/stateInfo/state`,

View File

@@ -96,36 +96,14 @@ export const mockPatchData: DataModel = {
icon: '/assets/img/service-icons/bitcoind.svg',
lastBackup: null,
status: {
main: 'running',
started: '2021-06-14T20:49:17.774Z',
health: {
'ephemeral-health-check': {
name: 'Ephemeral Health Check',
result: 'starting',
message: null,
},
'chain-state': {
name: 'Chain State',
result: 'loading',
message: 'Bitcoin is syncing from genesis',
},
'p2p-interface': {
name: 'P2P',
result: 'success',
message: 'Health check successful',
},
'rpc-interface': {
name: 'RPC',
result: 'failure',
message: 'RPC interface unreachable.',
},
'unnecessary-health-check': {
name: 'Unnecessary Health Check',
result: 'disabled',
message: null,
},
},
main: 'stopped',
},
// status: {
// main: 'error',
// message: 'Bitcoin is erroring out',
// debug: 'This is a complete stack trace for bitcoin',
// onRebuild: 'start',
// },
actions: {
config: {
name: 'Bitcoin Config',

View File

@@ -80,6 +80,7 @@ export type PrimaryStatus =
| 'stopped'
| 'backingUp'
| 'needsConfig'
| 'error'
export type DependencyStatus = 'warning' | 'satisfied'
@@ -139,6 +140,11 @@ export const PrimaryRendering: Record<PrimaryStatus, StatusRendering> = {
color: 'warning',
showDots: false,
},
error: {
display: 'Service Launch Error',
color: 'danger',
showDots: false,
},
}
export const DependencyRendering: Record<DependencyStatus, StatusRendering> = {

View File

@@ -0,0 +1,85 @@
import { Injectable } from '@angular/core'
import { T } from '@start9labs/start-sdk'
import { hasCurrentDeps } from '../util/has-deps'
import { getAllPackages } from '../util/get-package-data'
import { PatchDB } from 'patch-db-client'
import { DataModel } from './patch-db/data-model'
import { AlertController, NavController } from '@ionic/angular'
import { ApiService } from './api/embassy-api.service'
import { ErrorService, LoadingService } from '@start9labs/shared'
@Injectable({
providedIn: 'root',
})
export class StandardActionsService {
constructor(
private readonly patch: PatchDB<DataModel>,
private readonly api: ApiService,
private readonly alertCtrl: AlertController,
private readonly errorService: ErrorService,
private readonly loader: LoadingService,
private readonly navCtrl: NavController,
) {}
async rebuild(id: string) {
const loader = this.loader.open(`Rebuilding Container...`).subscribe()
try {
await this.api.rebuildPackage({ id })
this.navCtrl.navigateBack('/services/' + id)
} catch (e: any) {
this.errorService.handleError(e)
} finally {
loader.unsubscribe()
}
}
async tryUninstall(manifest: T.Manifest): Promise<void> {
const { id, title, alerts } = manifest
let message =
alerts.uninstall ||
`Uninstalling ${title} will permanently delete its data`
if (hasCurrentDeps(id, await getAllPackages(this.patch))) {
message = `${message}. Services that depend on ${title} will no longer work properly and may crash`
}
const alert = await this.alertCtrl.create({
header: 'Warning',
message,
buttons: [
{
text: 'Cancel',
role: 'cancel',
},
{
text: 'Uninstall',
handler: () => {
this.uninstall(id)
},
cssClass: 'enter-click',
},
],
cssClass: 'alert-warning-message',
})
await alert.present()
}
private async uninstall(id: string) {
const loader = this.loader.open(`Beginning uninstall...`).subscribe()
try {
await this.api.uninstallPackage({ id })
this.api
.setDbValue<boolean>(['ackInstructions', id], false)
.catch(e => console.error('Failed to mark instructions as unseen', e))
this.navCtrl.navigateRoot('/services')
} catch (e: any) {
this.errorService.handleError(e)
} finally {
loader.unsubscribe()
}
}
}

View File

@@ -110,6 +110,12 @@ $subheader-height: 48px;
}
}
.code-block {
background-color: rgb(69, 69, 69);
padding: 12px;
margin-bottom: 32px;
}
.center {
display: block;
margin: auto;

View File

@@ -22,7 +22,8 @@
"paths": {
/* These paths are relative to each app base folder */
"@start9labs/marketplace": ["../marketplace/src/public-api"],
"@start9labs/shared": ["../shared/src/public-api"]
"@start9labs/shared": ["../shared/src/public-api"],
"path": ["../../node_modules/path-browserify"]
},
"typeRoots": ["node_modules/@types"],
"types": ["node"]