chore: Update the start-sdk to use dependencies

This commit is contained in:
BluJ
2023-04-17 13:39:09 -06:00
parent 78076387ef
commit 4b74906ada
6 changed files with 73 additions and 110 deletions

View File

@@ -0,0 +1,15 @@
import { Dependency, PackageId } from "../types";
export function exists(id: PackageId) {
return {
id,
kind: "exists",
} as Dependency;
}
export function running(id: PackageId) {
return {
id,
kind: "running",
} as Dependency;
}

View File

@@ -2,3 +2,4 @@ export * as configBuilder from "./builder";
export { setupConfigExports } from "./setupConfigExports";
export { specToBuilder, specToBuilderFile } from "./specToBuilder";
export * as dependencies from './dependencies'

View File

@@ -1,5 +1,5 @@
import { Config } from "./builder";
import { DeepPartial, DependsOn, Effects, ExpectedExports } from "../types";
import { DeepPartial, Dependencies, DependsOn, Effects, ExpectedExports } from "../types";
import { InputSpec } from "./configTypes";
import { nullIfEmpty } from "../util";
import { TypeFromProps } from "../util/propertiesMatcher";
@@ -13,15 +13,8 @@ import { TypeFromProps } from "../util/propertiesMatcher";
*/
export function setupConfigExports<A extends InputSpec, ConfigType>(options: {
spec: Config<A>;
dependsOn: DependsOn;
write(options: {
effects: Effects;
input: TypeFromProps<A>;
}): Promise<ConfigType>;
read(options: {
effects: Effects;
config: ConfigType;
}): Promise<null | DeepPartial<TypeFromProps<A>>>;
write(options: { effects: Effects; input: TypeFromProps<A> }): Promise<[ConfigType, Dependencies]>;
read(options: { effects: Effects; config: ConfigType }): Promise<null | DeepPartial<TypeFromProps<A>>>;
}) {
const validator = options.spec.validator();
return {
@@ -30,17 +23,15 @@ export function setupConfigExports<A extends InputSpec, ConfigType>(options: {
await effects.error(String(validator.errorMessage(input)));
return { error: "Set config type error for config" };
}
const output = await options.write({ input, effects });
if (output) {
await effects.setWrapperData({ path: "config", value: output });
}
const [output, dependencies] = await options.write({ input, effects });
await effects.setDependencies(dependencies);
await effects.setWrapperData({ path: "config", value: output });
}) as ExpectedExports.setConfig,
getConfig: (async ({ effects, config }) => {
return {
spec: options.spec.build(),
config: nullIfEmpty(
await options.read({ effects, config: config as ConfigType })
),
config: nullIfEmpty(await options.read({ effects, config: config as ConfigType })),
};
}) as ExpectedExports.getConfig,
};

View File

@@ -4,10 +4,12 @@ import { LocalBinding } from "./LocalBinding";
export class LocalPort {
constructor(readonly effects: Effects, readonly id: string) {}
async bindLan(internalPort: number) {
const [localAddress, ipAddress] = await this.effects.bindLan({
const port = await this.effects.bindLan({
internalPort,
name: this.id,
});
const localAddress = `${await this.effects.getLocalHostname()}:${port}`;
const ipAddress = `${await this.effects.getIPHostname()}:${port}`;
return new LocalBinding(localAddress, ipAddress);
}
}

View File

@@ -14,4 +14,4 @@ export class NetworkBuilder {
getPort(id: string) {
return new LocalPort(this.effects, id);
}
}
}

View File

@@ -5,29 +5,17 @@ import { ActionReceipt } from "./init";
export namespace ExpectedExports {
version: 1;
/** Set configuration is called after we have modified and saved the configuration in the embassy ui. Use this to make a file for the docker to read from for configuration. */
export type setConfig = (options: {
effects: Effects;
input: Record<string, unknown>;
}) => Promise<unknown>;
export type setConfig = (options: { effects: Effects; input: Record<string, unknown> }) => Promise<void>;
/** Get configuration returns a shape that describes the format that the embassy ui will generate, and later send to the set config */
export type getConfig = (options: {
effects: Effects;
config: unknown;
}) => Promise<ConfigRes>;
export type getConfig = (options: { effects: Effects; config: unknown }) => Promise<ConfigRes>;
// /** These are how we make sure the our dependency configurations are valid and if not how to fix them. */
// export type dependencies = Dependencies;
/** For backing up service data though the embassyOS UI */
export type createBackup = (options: {
effects: Effects;
}) => Promise<unknown>;
export type createBackup = (options: { effects: Effects }) => Promise<unknown>;
/** For restoring service data that was previously backed up using the embassyOS UI create backup flow. Backup restores are also triggered via the embassyOS UI, or doing a system restore flow during setup. */
export type restoreBackup = (options: {
effects: Effects;
}) => Promise<unknown>;
export type restoreBackup = (options: { effects: Effects }) => Promise<unknown>;
/** Properties are used to get values from the docker, like a username + password, what ports we are hosting from */
export type properties = (options: {
effects: Effects;
}) => Promise<Properties | null | undefined | void>;
export type properties = (options: { effects: Effects }) => Promise<Properties | null | undefined | void>;
// /** Health checks are used to determine if the service is working properly after starting
// * A good use case is if we are using a web server, seeing if we can get to the web server.
@@ -43,43 +31,35 @@ export namespace ExpectedExports {
* service starting, and that file would indicate that it would rescan all the data.
*/
export type action = {
[id: string]: (options: {
effects: Effects;
input?: Record<string, unknown>;
}) => Promise<ActionResult>;
[id: string]: (options: { effects: Effects; input?: Record<string, unknown> }) => Promise<ActionResult>;
};
/**
* This is the entrypoint for the main container. Used to start up something like the service that the
* package represents, like running a bitcoind in a bitcoind-wrapper.
*/
export type main = (options: {
effects: Effects;
started(onTerm: () => void): null;
}) => Promise<unknown>;
export type main = (options: { effects: Effects; started(onTerm: () => void): null }) => Promise<unknown>;
/**
* After a shutdown, if we wanted to do any operations to clean up things, like
* set the action as unavailable or something.
*/
export type afterShutdown = (options: { effects: Effects }) => Promise<unknown>;
/**
* Every time a package completes an install, this function is called before the main.
* Can be used to do migration like things.
*/
export type init = (options: {
effects: Effects;
previousVersion: null | string;
}) => Promise<unknown>;
export type init = (options: { effects: Effects; previousVersion: null | string }) => Promise<unknown>;
/** This will be ran during any time a package is uninstalled, for example during a update
* this will be called.
*/
export type uninit = (options: {
effects: Effects;
nextVersion: null | string;
}) => Promise<unknown>;
export type uninit = (options: { effects: Effects; nextVersion: null | string }) => Promise<unknown>;
}
export type TimeMs = number;
export type VersionString = string;
export type ValidIfNoStupidEscape<A> = A extends
| `${string}'"'"'${string}`
| `${string}\\"${string}`
export type ValidIfNoStupidEscape<A> = A extends `${string}'"'"'${string}` | `${string}\\"${string}`
? never
: "" extends A & ""
? never
@@ -104,9 +84,7 @@ export type Daemon = {
export type HealthStatus = "passing" | "warning" | "failing" | "disabled";
export type CommandType<A extends string> =
| ValidIfNoStupidEscape<A>
| [string, ...string[]];
export type CommandType<A extends string> = ValidIfNoStupidEscape<A> | [string, ...string[]];
export type DaemonReturned = {
wait(): Promise<string>;
@@ -116,11 +94,7 @@ export type DaemonReturned = {
/** Used to reach out from the pure js runtime */
export type Effects = {
/** Usable when not sandboxed */
writeFile(input: {
path: string;
volumeId: string;
toWrite: string;
}): Promise<void>;
writeFile(input: { path: string; volumeId: string; toWrite: string }): Promise<void>;
readFile(input: { volumeId: string; path: string }): Promise<string>;
metadata(input: { volumeId: string; path: string }): Promise<Metadata>;
/** Create a directory. Usable when not sandboxed */
@@ -132,17 +106,10 @@ export type Effects = {
removeFile(input: { volumeId: string; path: string }): Promise<void>;
/** Write a json file into an object. Usable when not sandboxed */
writeJsonFile(input: {
volumeId: string;
path: string;
toWrite: Record<string, unknown>;
}): Promise<void>;
writeJsonFile(input: { volumeId: string; path: string; toWrite: Record<string, unknown> }): Promise<void>;
/** Read a json file into an object */
readJsonFile(input: {
volumeId: string;
path: string;
}): Promise<Record<string, unknown>>;
readJsonFile(input: { volumeId: string; path: string }): Promise<Record<string, unknown>>;
runCommand<A extends string>(
command: ValidIfNoStupidEscape<A> | [string, ...string[]],
@@ -154,9 +121,7 @@ export type Effects = {
wait(): Promise<string>;
term(): Promise<void>;
};
runDaemon<A extends string>(
command: ValidIfNoStupidEscape<A> | [string, ...string[]]
): DaemonReturned;
runDaemon<A extends string>(command: ValidIfNoStupidEscape<A> | [string, ...string[]]): DaemonReturned;
/** Uses the chown on the system */
chown(input: { volumeId: string; path: string; uid: string }): Promise<null>;
@@ -181,14 +146,12 @@ export type Effects = {
/** Check that a file exists or not */
exists(input: { volumeId: string; path: string }): Promise<boolean>;
/** Declaring that we are opening a interface on some protocal for local network */
bindLan(options: { internalPort: number; name: string }): Promise<string[]>;
/** Declaring that we are opening a interface on some protocal for local network
* Returns the port exposed
*/
bindLan(options: { internalPort: number; name: string }): Promise<number>;
/** Declaring that we are opening a interface on some protocal for tor network */
bindTor(options: {
internalPort: number;
name: string;
externalPort: number;
}): Promise<string>;
bindTor(options: { internalPort: number; name: string; externalPort: number }): Promise<string>;
/** Similar to the fetch api via the mdn, this is simplified but the point is
* to get something from some website, and return the response.
@@ -248,17 +211,11 @@ export type Effects = {
getLocalHostname(): Promise<string>;
getIPHostname(): Promise<string>;
/** Get the address for another service for tor interfaces */
getServiceTorHostname(
interfaceId: string,
packageId?: string
): Promise<string>;
getServiceTorHostname(interfaceId: string, packageId?: string): Promise<string>;
/**
* Get the port address for another service
*/
getServicePortForward(
internalPort: number,
packageId?: string
): Promise<number>;
getServicePortForward(internalPort: number, packageId?: string): Promise<number>;
/** When we want to create a link in the front end interfaces, and example is
* exposing a url to view a web service
@@ -328,21 +285,20 @@ export type Effects = {
*
* @returns PEM encoded fullchain (ecdsa)
*/
getSslCertificate: (
packageId: string,
algorithm?: "ecdsa" | "ed25519"
) => [string, string, string];
getSslCertificate: (packageId: string, algorithm?: "ecdsa" | "ed25519") => [string, string, string];
/**
* @returns PEM encoded ssl key (ecdsa)
*/
getSslKey: (packageId: string, algorithm?: "ecdsa" | "ed25519") => string;
setHealth(o: {
name: string;
status: HealthStatus;
message?: string;
}): Promise<void>;
setHealth(o: { name: string; status: HealthStatus; message?: string }): Promise<void>;
/** Set the dependencies of what the service needs, usually ran during the set config as a best practice */
setDependencies(dependencies: Dependencies): Promise<void>;
/** Exists could be useful during the runtime to know if some service exists, option dep */
exists(packageId: PackageId): Promise<boolean>;
/** Exists could be useful during the runtime to know if some service is running, option dep */
running(packageId: PackageId): Promise<boolean>;
restart(): void;
shutdown(): void;
};
@@ -423,6 +379,10 @@ export type SetResult = {
"depends-on": DependsOn;
};
export type PackageId = string;
export type Message = string;
export type DependencyKind = "running" | "exists";
export type DependsOn = {
[packageId: string]: string[];
};
@@ -463,16 +423,10 @@ export type Properties = {
data: PackagePropertiesV2;
};
export type Dependencies = {
/** Id is the id of the package, should be the same as the manifest */
[id: string]: {
/** Checks are called to make sure that our dependency is in the correct shape. If a known error is returned we know that the dependency needs modification */
check(effects: Effects, input: InputSpec): Promise<void | null>;
/** This is called after we know that the dependency package needs a new configuration, this would be a transform for defaults */
autoConfigure(effects: Effects, input: InputSpec): Promise<InputSpec>;
};
export type Dependency = {
id: PackageId;
kind: DependencyKind;
};
export type Dependencies = Array<Dependency>;
export type DeepPartial<T> = T extends {}
? { [P in keyof T]?: DeepPartial<T[P]> }
: T;
export type DeepPartial<T> = T extends {} ? { [P in keyof T]?: DeepPartial<T[P]> } : T;