Build the Apple Container extension
Turns the empty scaffold into a working Nova extension for Apple Container and container-compose, in the shape of the Docker Suite extension. Sidebar (Scripts/Sidebar): - System, Containers, Images, Volumes and Networks sections - Containers grouped into compose projects via the com.docker.compose.project label container-compose stamps; project rows drive compose up/down/build against the matching workspace file - Lifecycle, shell, logs, inspect, browse and copy commands per resource - Apple Container publishes no event stream, so the sidebar polls while visible and only reloads a section when a fingerprint of its data changes, which keeps selection and expansion intact Hostnames (Scripts/Hostnames.js): - A container's hostname is <name>.<domain> and the domain lives in Apple Container's config.toml, which has no CLI setter, so the Hostname Domain preference reads and writes that file directly - Changing it offers to register the domain with macOS and restart the services; the write preserves the file's other tables and its mode Language support: - Dockerfile and Compose syntaxes backed by tree-sitter grammars built by Tools/build-syntaxes.sh, pinned to revisions that generate ABI 14 - The Compose syntax is named dockercompose because that is the languageId docker-language-server recognises compose files by - docker-language-server is downloaded on first use rather than bundled, at roughly 40 MB Icons are generated by Tools/make-icons.py so they can be regenerated rather than hand-maintained. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01UnSBcR5Lywz5Fbj5FmVbfc
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
//
|
||||
// Owns the sidebar's controllers and the polling that keeps them current.
|
||||
//
|
||||
// Apple Container publishes no event stream, so the sidebar polls on an
|
||||
// interval while it is visible. Controllers only reload their tree when the
|
||||
// data actually changed, which keeps selection and expansion intact.
|
||||
//
|
||||
|
||||
const { SystemController } = require("./TreeViews/System");
|
||||
const { ContainersController } = require("./TreeViews/Containers");
|
||||
const { ImagesController } = require("./TreeViews/Images");
|
||||
const { VolumesController } = require("./TreeViews/Volumes");
|
||||
const { NetworksController } = require("./TreeViews/Networks");
|
||||
const { ContainerCLI } = require("../Interfaces/ContainerCLI");
|
||||
const notify = require("../Utilities/Notify");
|
||||
|
||||
class ContainerSidebar {
|
||||
constructor() {
|
||||
this.controllers = null;
|
||||
this.timer = null;
|
||||
this.refreshing = false;
|
||||
this.watchers = new CompositeDisposable();
|
||||
|
||||
this.watchers.add(
|
||||
nova.config.onDidChange("apple_container.sidebar.disabled", () => this.restart())
|
||||
);
|
||||
this.watchers.add(
|
||||
nova.config.onDidChange("apple_container.sidebar.refresh", () => this.schedule())
|
||||
);
|
||||
|
||||
this.start();
|
||||
}
|
||||
|
||||
start() {
|
||||
if (nova.config.get("apple_container.sidebar.disabled", "boolean")) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.system = new SystemController(this);
|
||||
this.containers = new ContainersController(this);
|
||||
this.images = new ImagesController(this);
|
||||
this.volumes = new VolumesController(this);
|
||||
this.networks = new NetworksController(this);
|
||||
|
||||
this.controllers = [this.system, this.containers, this.images, this.volumes, this.networks];
|
||||
|
||||
this.bootstrap();
|
||||
this.schedule();
|
||||
}
|
||||
|
||||
stop() {
|
||||
if (this.timer) {
|
||||
clearInterval(this.timer);
|
||||
this.timer = null;
|
||||
}
|
||||
|
||||
if (this.controllers) {
|
||||
for (let controller of this.controllers) {
|
||||
controller.dispose();
|
||||
}
|
||||
this.controllers = null;
|
||||
}
|
||||
}
|
||||
|
||||
dispose() {
|
||||
this.stop();
|
||||
this.watchers.dispose();
|
||||
}
|
||||
|
||||
restart() {
|
||||
this.stop();
|
||||
this.start();
|
||||
}
|
||||
|
||||
// First pass: start the services when asked to, then fill every section.
|
||||
async bootstrap() {
|
||||
if (nova.config.get("apple_container.sidebar.autostart", "boolean")) {
|
||||
let status = await ContainerCLI.systemStatus();
|
||||
|
||||
if (status.status !== "running") {
|
||||
try {
|
||||
await ContainerCLI.systemStart();
|
||||
} catch (error) {
|
||||
notify.configFailure(
|
||||
"Unable to start container services",
|
||||
error,
|
||||
"apple-container-autostart"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this.refresh({ force: true });
|
||||
}
|
||||
|
||||
schedule() {
|
||||
if (this.timer) {
|
||||
clearInterval(this.timer);
|
||||
this.timer = null;
|
||||
}
|
||||
|
||||
if (!this.controllers) {
|
||||
return;
|
||||
}
|
||||
|
||||
let seconds = nova.config.get("apple_container.sidebar.refresh", "number");
|
||||
|
||||
if (typeof seconds !== "number" || seconds <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.timer = setInterval(() => this.refresh(), Math.max(1, seconds) * 1000);
|
||||
}
|
||||
|
||||
// One refresh at a time, and only for sections the user can actually see.
|
||||
async refresh(options = {}) {
|
||||
if (!this.controllers || this.refreshing) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.refreshing = true;
|
||||
|
||||
try {
|
||||
await this.system.refresh(options);
|
||||
|
||||
if (!this.system.running) {
|
||||
for (let controller of this.sections()) {
|
||||
controller.clear();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
let pending = this.sections()
|
||||
.filter((controller) => options.force || controller.visible)
|
||||
.map((controller) => controller.refresh(options));
|
||||
|
||||
await Promise.all(pending);
|
||||
} finally {
|
||||
this.refreshing = false;
|
||||
}
|
||||
}
|
||||
|
||||
sections() {
|
||||
return [this.containers, this.images, this.volumes, this.networks];
|
||||
}
|
||||
|
||||
reload() {
|
||||
if (!this.controllers) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
for (let controller of this.controllers) {
|
||||
controller.invalidate();
|
||||
}
|
||||
|
||||
return this.refresh({ force: true });
|
||||
}
|
||||
|
||||
reloadContainers() {
|
||||
if (this.containers) {
|
||||
this.containers.reload();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { ContainerSidebar };
|
||||
@@ -0,0 +1,211 @@
|
||||
//
|
||||
// Containers, and the compose projects they are grouped into.
|
||||
//
|
||||
// container-compose stamps com.docker.compose.project and
|
||||
// com.docker.compose.service on the containers it creates, which is the only
|
||||
// link back to a project; containers created any other way fall into an
|
||||
// ungrouped bucket.
|
||||
//
|
||||
|
||||
const { Element } = require("./Element");
|
||||
const format = require("../../Utilities/Format");
|
||||
|
||||
const PROJECT_LABEL = "com.docker.compose.project";
|
||||
const SERVICE_LABEL = "com.docker.compose.service";
|
||||
const UNGROUPED = "Containers";
|
||||
|
||||
class ContainerModel extends Element {
|
||||
constructor(json) {
|
||||
let configuration = (json && json.configuration) || {};
|
||||
let status = (json && json.status) || {};
|
||||
|
||||
super(`container:${configuration.id || json.id}`);
|
||||
|
||||
this.id = configuration.id || json.id;
|
||||
this.name = this.id;
|
||||
this.state = typeof status.state === "string" ? status.state : "unknown";
|
||||
this.labels = configuration.labels || {};
|
||||
this.image = (configuration.image && configuration.image.reference) || "";
|
||||
this.imageName = format.reference(this.image).short;
|
||||
this.platform = configuration.platform || {};
|
||||
this.startedDate = status.startedDate || null;
|
||||
this.project = this.labels[PROJECT_LABEL] || null;
|
||||
this.service = this.labels[SERVICE_LABEL] || null;
|
||||
|
||||
this.cpus = (configuration.resources && configuration.resources.cpus) || null;
|
||||
this.memory = (configuration.resources && configuration.resources.memoryInBytes) || null;
|
||||
|
||||
this.ports = (configuration.publishedPorts || []).map((port) => ({
|
||||
hostAddress: port.hostAddress,
|
||||
hostPort: port.hostPort,
|
||||
containerPort: port.containerPort,
|
||||
proto: port.proto || "tcp"
|
||||
}));
|
||||
|
||||
this.mounts = (configuration.mounts || []).map((mount) => ({
|
||||
destination: mount.destination,
|
||||
source: mount.source,
|
||||
volume: mount.type && mount.type.volume ? mount.type.volume.name : null
|
||||
}));
|
||||
|
||||
// Addresses only exist while the container is running.
|
||||
this.addresses = (status.networks || []).map((attachment) => ({
|
||||
network: attachment.network,
|
||||
hostname: stripTrailingDot(attachment.hostname),
|
||||
address: stripPrefix(attachment.ipv4Address)
|
||||
}));
|
||||
|
||||
this.hostname = this.addresses.length > 0 ? this.addresses[0].hostname : null;
|
||||
this.address = this.addresses.length > 0 ? this.addresses[0].address : null;
|
||||
}
|
||||
|
||||
get running() {
|
||||
return this.state === "running";
|
||||
}
|
||||
|
||||
get revision() {
|
||||
return `${this.state}:${this.address || ""}:${this.portSummary}`;
|
||||
}
|
||||
|
||||
get portSummary() {
|
||||
return this.ports
|
||||
.map((port) => `${port.hostPort}→${port.containerPort}/${port.proto}`)
|
||||
.join(" ");
|
||||
}
|
||||
|
||||
// Every way this container can be reached over HTTP, most useful first:
|
||||
// published ports resolve from the host unconditionally, while the DNS
|
||||
// hostname only resolves once its domain is registered with macOS.
|
||||
get urls() {
|
||||
let urls = this.ports
|
||||
.filter((port) => port.proto === "tcp")
|
||||
.map((port) => {
|
||||
let host =
|
||||
!port.hostAddress || port.hostAddress === "0.0.0.0" ? "localhost" : port.hostAddress;
|
||||
return {
|
||||
url: `http://${host}:${port.hostPort}`,
|
||||
label: `http://${host}:${port.hostPort} → container port ${port.containerPort}`
|
||||
};
|
||||
});
|
||||
|
||||
if (this.running && this.hostname) {
|
||||
urls.push({ url: `http://${this.hostname}`, label: `http://${this.hostname} (DNS name)` });
|
||||
}
|
||||
|
||||
if (this.running && this.address) {
|
||||
urls.push({ url: `http://${this.address}`, label: `http://${this.address} (container IP)` });
|
||||
}
|
||||
|
||||
return urls;
|
||||
}
|
||||
|
||||
get url() {
|
||||
let urls = this.urls;
|
||||
return urls.length > 0 ? urls[0].url : null;
|
||||
}
|
||||
|
||||
get tooltip() {
|
||||
let lines = [
|
||||
`Name:\t${this.name}`,
|
||||
`Image:\t${this.image}`,
|
||||
`State:\t${this.state}`
|
||||
];
|
||||
|
||||
if (this.service) {
|
||||
lines.push(`Service:\t${this.service}`);
|
||||
}
|
||||
if (this.address) {
|
||||
lines.push(`Address:\t${this.address}`);
|
||||
}
|
||||
if (this.hostname) {
|
||||
lines.push(`Hostname:\t${this.hostname}`);
|
||||
}
|
||||
if (this.ports.length > 0) {
|
||||
lines.push(`Ports:\t${this.portSummary}`);
|
||||
}
|
||||
if (this.memory) {
|
||||
lines.push(`Memory:\t${format.bytes(this.memory)}`);
|
||||
}
|
||||
if (this.cpus) {
|
||||
lines.push(`CPUs:\t${this.cpus}`);
|
||||
}
|
||||
if (this.startedDate) {
|
||||
lines.push(`Started:\t${format.age(this.startedDate)}`);
|
||||
}
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
}
|
||||
|
||||
class ProjectModel extends Element {
|
||||
constructor(name, compose) {
|
||||
super(`project:${name}`);
|
||||
this.name = name;
|
||||
// A compose project is one the sidebar can also drive with
|
||||
// container-compose; the ungrouped bucket is not.
|
||||
this.compose = compose;
|
||||
this.file = null;
|
||||
this.expanded = true;
|
||||
}
|
||||
|
||||
get running() {
|
||||
return this.children.some((child) => child.running);
|
||||
}
|
||||
|
||||
get summary() {
|
||||
let running = this.children.filter((child) => child.running).length;
|
||||
return `${running}/${this.children.length} running`;
|
||||
}
|
||||
|
||||
get revision() {
|
||||
return `${this.file || ""}:${this.summary}`;
|
||||
}
|
||||
}
|
||||
|
||||
function stripTrailingDot(hostname) {
|
||||
if (!hostname) {
|
||||
return null;
|
||||
}
|
||||
return String(hostname).replace(/\.$/, "");
|
||||
}
|
||||
|
||||
function stripPrefix(cidr) {
|
||||
if (!cidr) {
|
||||
return null;
|
||||
}
|
||||
return String(cidr).split("/")[0];
|
||||
}
|
||||
|
||||
// Builds the container tree. Returns projects when grouping is on, and a flat
|
||||
// list of containers when it is off.
|
||||
function build(list, options = {}) {
|
||||
let containers = (list || []).map((entry) => new ContainerModel(entry));
|
||||
|
||||
containers.sort((left, right) => left.name.localeCompare(right.name));
|
||||
|
||||
if (!options.group) {
|
||||
return containers;
|
||||
}
|
||||
|
||||
let projects = new Map();
|
||||
|
||||
for (let container of containers) {
|
||||
let name = container.project || UNGROUPED;
|
||||
|
||||
if (!projects.has(name)) {
|
||||
projects.set(name, new ProjectModel(name, container.project !== null));
|
||||
}
|
||||
|
||||
projects.get(name).addChild(container);
|
||||
}
|
||||
|
||||
// Ungrouped containers sort last, everything else alphabetically.
|
||||
return [...projects.values()].sort((left, right) => {
|
||||
if (left.compose !== right.compose) {
|
||||
return left.compose ? -1 : 1;
|
||||
}
|
||||
return left.name.localeCompare(right.name);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { ContainerModel, ProjectModel, build, PROJECT_LABEL, SERVICE_LABEL };
|
||||
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// Shared base for everything shown in the sidebar trees.
|
||||
//
|
||||
|
||||
class Element {
|
||||
constructor(identifier) {
|
||||
this.identifier = identifier;
|
||||
this.children = [];
|
||||
this.parent = null;
|
||||
this.expanded = false;
|
||||
}
|
||||
|
||||
addChild(child) {
|
||||
child.parent = this;
|
||||
this.children.push(child);
|
||||
return child;
|
||||
}
|
||||
|
||||
get hasChildren() {
|
||||
return this.children.length > 0;
|
||||
}
|
||||
|
||||
// Everything about the element that should trigger a tree reload when it
|
||||
// changes. Identity is compared separately.
|
||||
get revision() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { Element };
|
||||
@@ -0,0 +1,119 @@
|
||||
//
|
||||
// Images, optionally grouped by repository.
|
||||
//
|
||||
|
||||
const { Element } = require("./Element");
|
||||
const format = require("../../Utilities/Format");
|
||||
|
||||
const INFRA_PREFIXES = [
|
||||
"ghcr.io/apple/container-builder-shim/",
|
||||
"ghcr.io/apple/containerization/vminit"
|
||||
];
|
||||
|
||||
class ImageModel extends Element {
|
||||
constructor(json) {
|
||||
let configuration = (json && json.configuration) || {};
|
||||
|
||||
super(`image:${configuration.name}`);
|
||||
|
||||
this.reference = configuration.name || "";
|
||||
this.digest = (configuration.descriptor && configuration.descriptor.digest) || "";
|
||||
// The CLI identifies images by the first twelve hex digits of the digest.
|
||||
this.shortDigest = this.digest.replace(/^[a-z0-9]+:/, "").slice(0, 12);
|
||||
this.id = json.id || "";
|
||||
this.created = configuration.creationDate || null;
|
||||
|
||||
let parts = format.reference(this.reference);
|
||||
this.registry = parts.registry;
|
||||
this.repository = parts.repository;
|
||||
this.tag = parts.tag;
|
||||
this.short = parts.short;
|
||||
|
||||
this.variants = (json.variants || []).map((variant) => ({
|
||||
architecture: (variant.platform && variant.platform.architecture) || "",
|
||||
os: (variant.platform && variant.platform.os) || "",
|
||||
size: variant.size || 0
|
||||
}));
|
||||
|
||||
this.size = this.variants.reduce((total, variant) => total + variant.size, 0);
|
||||
}
|
||||
|
||||
get revision() {
|
||||
return `${this.digest}:${this.size}`;
|
||||
}
|
||||
|
||||
get infrastructure() {
|
||||
return INFRA_PREFIXES.some((prefix) => this.reference.startsWith(prefix));
|
||||
}
|
||||
|
||||
get platforms() {
|
||||
return this.variants
|
||||
.map((variant) => (variant.os ? `${variant.os}/${variant.architecture}` : variant.architecture))
|
||||
.filter((platform) => platform.length > 0)
|
||||
.join(" ");
|
||||
}
|
||||
|
||||
get tooltip() {
|
||||
let lines = [`Image:\t${this.reference}`, `Digest:\t${this.shortDigest}`];
|
||||
|
||||
if (this.platforms) {
|
||||
lines.push(`Platform:\t${this.platforms}`);
|
||||
}
|
||||
if (this.size) {
|
||||
lines.push(`Size:\t${format.bytes(this.size)}`);
|
||||
}
|
||||
if (this.created) {
|
||||
lines.push(`Created:\t${format.age(this.created)}`);
|
||||
}
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
}
|
||||
|
||||
class RepositoryModel extends Element {
|
||||
constructor(name) {
|
||||
super(`repository:${name}`);
|
||||
this.name = name;
|
||||
this.expanded = true;
|
||||
}
|
||||
|
||||
get size() {
|
||||
return this.children.reduce((total, image) => total + image.size, 0);
|
||||
}
|
||||
|
||||
get revision() {
|
||||
return String(this.size);
|
||||
}
|
||||
}
|
||||
|
||||
function build(list, options = {}) {
|
||||
let images = (list || []).map((entry) => new ImageModel(entry));
|
||||
|
||||
if (!options.infrastructure) {
|
||||
images = images.filter((image) => !image.infrastructure);
|
||||
}
|
||||
|
||||
let label = (image) => (options.registry ? image.reference : `${image.short}:${image.tag}`);
|
||||
|
||||
images.sort((left, right) => label(left).localeCompare(label(right)));
|
||||
|
||||
if (!options.group) {
|
||||
return images;
|
||||
}
|
||||
|
||||
let repositories = new Map();
|
||||
|
||||
for (let image of images) {
|
||||
let name = options.registry && image.registry ? `${image.registry}/${image.repository}` : image.short;
|
||||
|
||||
if (!repositories.has(name)) {
|
||||
repositories.set(name, new RepositoryModel(name));
|
||||
}
|
||||
|
||||
repositories.get(name).addChild(image);
|
||||
}
|
||||
|
||||
return [...repositories.values()].sort((left, right) => left.name.localeCompare(right.name));
|
||||
}
|
||||
|
||||
module.exports = { ImageModel, RepositoryModel, build };
|
||||
@@ -0,0 +1,60 @@
|
||||
//
|
||||
// Networks.
|
||||
//
|
||||
|
||||
const { Element } = require("./Element");
|
||||
|
||||
const ROLE_LABEL = "com.apple.container.resource.role";
|
||||
|
||||
class NetworkModel extends Element {
|
||||
constructor(json) {
|
||||
let configuration = (json && json.configuration) || {};
|
||||
let status = (json && json.status) || {};
|
||||
|
||||
super(`network:${configuration.name || json.id}`);
|
||||
|
||||
this.name = configuration.name || json.id;
|
||||
this.mode = configuration.mode || "";
|
||||
this.plugin = configuration.plugin || "";
|
||||
this.created = configuration.creationDate || null;
|
||||
this.labels = configuration.labels || {};
|
||||
this.subnet = status.ipv4Subnet || "";
|
||||
this.gateway = status.ipv4Gateway || "";
|
||||
this.subnet6 = status.ipv6Subnet || "";
|
||||
}
|
||||
|
||||
get revision() {
|
||||
return `${this.subnet}:${this.gateway}:${this.mode}`;
|
||||
}
|
||||
|
||||
get builtin() {
|
||||
return this.labels[ROLE_LABEL] === "builtin";
|
||||
}
|
||||
|
||||
get tooltip() {
|
||||
let lines = [`Network:\t${this.name}`];
|
||||
|
||||
if (this.mode) {
|
||||
lines.push(`Mode:\t${this.mode}`);
|
||||
}
|
||||
if (this.subnet) {
|
||||
lines.push(`Subnet:\t${this.subnet}`);
|
||||
}
|
||||
if (this.gateway) {
|
||||
lines.push(`Gateway:\t${this.gateway}`);
|
||||
}
|
||||
if (this.plugin) {
|
||||
lines.push(`Plugin:\t${this.plugin}`);
|
||||
}
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
}
|
||||
|
||||
function build(list) {
|
||||
return (list || [])
|
||||
.map((entry) => new NetworkModel(entry))
|
||||
.sort((left, right) => left.name.localeCompare(right.name));
|
||||
}
|
||||
|
||||
module.exports = { NetworkModel, build };
|
||||
@@ -0,0 +1,55 @@
|
||||
//
|
||||
// Volumes.
|
||||
//
|
||||
|
||||
const { Element } = require("./Element");
|
||||
const format = require("../../Utilities/Format");
|
||||
|
||||
class VolumeModel extends Element {
|
||||
constructor(json) {
|
||||
let configuration = (json && json.configuration) || {};
|
||||
|
||||
super(`volume:${configuration.name || json.id}`);
|
||||
|
||||
this.name = configuration.name || json.id;
|
||||
this.driver = configuration.driver || "";
|
||||
this.filesystem = configuration.format || "";
|
||||
this.source = configuration.source || "";
|
||||
// The volume image is sparse: sizeInBytes is the maximum it can grow
|
||||
// to, not what it occupies, so it is reported as capacity.
|
||||
this.capacity = configuration.sizeInBytes || 0;
|
||||
this.created = configuration.creationDate || null;
|
||||
this.labels = configuration.labels || {};
|
||||
}
|
||||
|
||||
get revision() {
|
||||
return `${this.capacity}:${this.filesystem}`;
|
||||
}
|
||||
|
||||
get tooltip() {
|
||||
let lines = [`Volume:\t${this.name}`, `Driver:\t${this.driver}`];
|
||||
|
||||
if (this.filesystem) {
|
||||
lines.push(`Format:\t${this.filesystem}`);
|
||||
}
|
||||
if (this.capacity) {
|
||||
lines.push(`Capacity:\t${format.bytes(this.capacity)}`);
|
||||
}
|
||||
if (this.created) {
|
||||
lines.push(`Created:\t${format.age(this.created)}`);
|
||||
}
|
||||
if (this.source) {
|
||||
lines.push(`Source:\t${this.source}`);
|
||||
}
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
}
|
||||
|
||||
function build(list) {
|
||||
return (list || [])
|
||||
.map((entry) => new VolumeModel(entry))
|
||||
.sort((left, right) => left.name.localeCompare(right.name));
|
||||
}
|
||||
|
||||
module.exports = { VolumeModel, build };
|
||||
@@ -0,0 +1,45 @@
|
||||
const { statusIcon } = require("./StatusIcon");
|
||||
|
||||
class ContainerTreeItem extends TreeItem {
|
||||
constructor(container) {
|
||||
super(label(container), TreeItemCollapsibleState.None);
|
||||
|
||||
this.identifier = container.identifier;
|
||||
this.image = statusIcon(container.running);
|
||||
this.contextValue = container.running ? "container.running" : "container.stopped";
|
||||
this.tooltip = container.tooltip;
|
||||
this.descriptiveText = description(container);
|
||||
}
|
||||
}
|
||||
|
||||
function label(container) {
|
||||
switch (nova.config.get("apple_container.containers.label", "string")) {
|
||||
case "Image":
|
||||
return container.imageName;
|
||||
case "Service":
|
||||
return container.service || container.name;
|
||||
default:
|
||||
return container.name;
|
||||
}
|
||||
}
|
||||
|
||||
function description(container) {
|
||||
let parts = [];
|
||||
|
||||
if (nova.config.get("apple_container.containers.desc.image", "boolean")) {
|
||||
parts.push(container.imageName);
|
||||
}
|
||||
if (nova.config.get("apple_container.containers.desc.ports", "boolean") && container.ports.length > 0) {
|
||||
parts.push(container.portSummary);
|
||||
}
|
||||
if (nova.config.get("apple_container.containers.desc.address", "boolean") && container.address) {
|
||||
parts.push(container.address);
|
||||
}
|
||||
if (nova.config.get("apple_container.containers.desc.state", "boolean")) {
|
||||
parts.push(container.state);
|
||||
}
|
||||
|
||||
return parts.join(" ");
|
||||
}
|
||||
|
||||
module.exports = { ContainerTreeItem };
|
||||
@@ -0,0 +1,38 @@
|
||||
const format = require("../../Utilities/Format");
|
||||
|
||||
class ImageTreeItem extends TreeItem {
|
||||
constructor(image, options = {}) {
|
||||
super(options.grouped ? image.tag : label(image), TreeItemCollapsibleState.None);
|
||||
|
||||
this.identifier = image.identifier;
|
||||
this.image = "icons/tag.png";
|
||||
this.contextValue = "image.tag";
|
||||
this.tooltip = image.tooltip;
|
||||
this.descriptiveText = [format.bytes(image.size), format.age(image.created)]
|
||||
.filter((part) => part.length > 0)
|
||||
.join(" ");
|
||||
}
|
||||
}
|
||||
|
||||
class RepositoryTreeItem extends TreeItem {
|
||||
constructor(repository) {
|
||||
super(
|
||||
repository.name,
|
||||
repository.expanded ? TreeItemCollapsibleState.Expanded : TreeItemCollapsibleState.Collapsed
|
||||
);
|
||||
|
||||
this.identifier = repository.identifier;
|
||||
this.image = "icons/layers.png";
|
||||
this.contextValue = "image.repo";
|
||||
this.descriptiveText = format.bytes(repository.size);
|
||||
this.tooltip = repository.name;
|
||||
}
|
||||
}
|
||||
|
||||
function label(image) {
|
||||
return nova.config.get("apple_container.images.registry", "boolean")
|
||||
? image.reference
|
||||
: `${image.short}:${image.tag}`;
|
||||
}
|
||||
|
||||
module.exports = { ImageTreeItem, RepositoryTreeItem };
|
||||
@@ -0,0 +1,15 @@
|
||||
class NetworkTreeItem extends TreeItem {
|
||||
constructor(network) {
|
||||
super(network.name, TreeItemCollapsibleState.None);
|
||||
|
||||
this.identifier = network.identifier;
|
||||
this.image = "icons/network.png";
|
||||
this.contextValue = "network";
|
||||
this.tooltip = network.tooltip;
|
||||
this.descriptiveText = [network.subnet, network.mode]
|
||||
.filter((part) => part && part.length > 0)
|
||||
.join(" ");
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { NetworkTreeItem };
|
||||
@@ -0,0 +1,18 @@
|
||||
class ProjectTreeItem extends TreeItem {
|
||||
constructor(project) {
|
||||
super(
|
||||
project.name,
|
||||
project.expanded ? TreeItemCollapsibleState.Expanded : TreeItemCollapsibleState.Collapsed
|
||||
);
|
||||
|
||||
this.identifier = project.identifier;
|
||||
this.image = project.compose ? "icons/layers.png" : "icons/folder.png";
|
||||
// Compose projects gain the compose commands; the ungrouped bucket only
|
||||
// gets the bulk start and stop.
|
||||
this.contextValue = project.compose ? "project.compose" : "project";
|
||||
this.descriptiveText = project.summary;
|
||||
this.tooltip = project.file ? `Compose file:\t${project.file}` : project.name;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { ProjectTreeItem };
|
||||
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// Maps a running/stopped state onto the icon set chosen in preferences.
|
||||
//
|
||||
|
||||
const SETS = {
|
||||
"Traffic Lights": ["icons/status/traffic-on.png", "icons/status/traffic-off.png"],
|
||||
Toggles: ["icons/status/toggle-on.png", "icons/status/toggle-off.png"],
|
||||
Power: ["icons/status/power-on.png", "icons/status/power-off.png"]
|
||||
};
|
||||
|
||||
function statusIcon(running) {
|
||||
let set = SETS[nova.config.get("apple_container.containers.icons", "string")] || SETS["Traffic Lights"];
|
||||
return running ? set[0] : set[1];
|
||||
}
|
||||
|
||||
module.exports = { statusIcon };
|
||||
@@ -0,0 +1,15 @@
|
||||
const { statusIcon } = require("./StatusIcon");
|
||||
|
||||
class SystemTreeItem extends TreeItem {
|
||||
constructor(row) {
|
||||
super(row.label, TreeItemCollapsibleState.None);
|
||||
|
||||
this.identifier = row.identifier;
|
||||
this.contextValue = row.contextValue;
|
||||
this.descriptiveText = row.description || "";
|
||||
this.tooltip = row.tooltip || row.label;
|
||||
this.image = row.running === undefined ? row.image || "icons/system.png" : statusIcon(row.running);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { SystemTreeItem };
|
||||
@@ -0,0 +1,17 @@
|
||||
class VolumeTreeItem extends TreeItem {
|
||||
constructor(volume) {
|
||||
super(volume.name, TreeItemCollapsibleState.None);
|
||||
|
||||
this.identifier = volume.identifier;
|
||||
this.image = "icons/volume.png";
|
||||
this.contextValue = "volume";
|
||||
this.tooltip = volume.tooltip;
|
||||
// Volume images are sparse, so a size here would only mislead; the
|
||||
// capacity is in the tooltip instead.
|
||||
this.descriptiveText = [volume.driver, volume.filesystem]
|
||||
.filter((part) => part && part.length > 0)
|
||||
.join(" ");
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { VolumeTreeItem };
|
||||
@@ -0,0 +1,388 @@
|
||||
//
|
||||
// Containers section, grouped by compose project.
|
||||
//
|
||||
|
||||
const { Controller } = require("./Controller");
|
||||
const { ContainerTreeItem } = require("../TreeItems/ContainerTreeItem");
|
||||
const { ProjectTreeItem } = require("../TreeItems/ProjectTreeItem");
|
||||
const { ContainerCLI } = require("../../Interfaces/ContainerCLI");
|
||||
const { ComposeCLI } = require("../../Interfaces/ComposeCLI");
|
||||
const { Terminal } = require("../../Interfaces/Terminal");
|
||||
const containers = require("../Models/Container");
|
||||
const notify = require("../../Utilities/Notify");
|
||||
const prompt = require("../../Utilities/Prompt");
|
||||
|
||||
class ContainersController extends Controller {
|
||||
constructor(sidebar) {
|
||||
super("apple_container.containers");
|
||||
this.sidebar = sidebar;
|
||||
this.registerCommands();
|
||||
}
|
||||
|
||||
async fetch() {
|
||||
let list = await ContainerCLI.listContainers();
|
||||
let group = nova.config.get("apple_container.containers.group", "boolean") !== false;
|
||||
let elements = containers.build(list, { group: group });
|
||||
|
||||
// Attach the compose file to each project so the compose commands and
|
||||
// the tooltip have something to work with.
|
||||
for (let element of elements) {
|
||||
if (element.compose) {
|
||||
element.file = this.composeFileFor(element.name);
|
||||
}
|
||||
}
|
||||
|
||||
return elements;
|
||||
}
|
||||
|
||||
getTreeItem(element) {
|
||||
return element instanceof containers.ProjectModel
|
||||
? new ProjectTreeItem(element)
|
||||
: new ContainerTreeItem(element);
|
||||
}
|
||||
|
||||
// Containers in the current selection, expanding any selected projects.
|
||||
selectedContainers() {
|
||||
let selected = [];
|
||||
|
||||
for (let element of this.selection) {
|
||||
if (element instanceof containers.ProjectModel) {
|
||||
selected = selected.concat(element.children);
|
||||
} else {
|
||||
selected.push(element);
|
||||
}
|
||||
}
|
||||
|
||||
return selected;
|
||||
}
|
||||
|
||||
selectedProjects() {
|
||||
return this.selection.filter((element) => element instanceof containers.ProjectModel);
|
||||
}
|
||||
|
||||
// Finds the workspace compose file whose project name matches, which is how
|
||||
// a sidebar row is tied back to a file on disk.
|
||||
composeFileFor(name) {
|
||||
for (let file of ComposeCLI.workspaceFiles()) {
|
||||
if (ComposeCLI.projectName(file) === name) {
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
let fallback = ComposeCLI.defaultFile();
|
||||
|
||||
if (fallback && ComposeCLI.projectName(fallback) === name) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// `build` receives the resolved compose file: the arguments cannot be built
|
||||
// before the file is known.
|
||||
async runCompose(project, build, title) {
|
||||
let file = project.file || this.composeFileFor(project.name);
|
||||
|
||||
if (!file) {
|
||||
notify.failure(
|
||||
`No compose file for “${project.name}”`,
|
||||
"Open the project's workspace, or set the compose file in workspace settings.",
|
||||
"apple-container-compose-missing"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let directory = nova.path.dirname(file);
|
||||
let args = build(file);
|
||||
|
||||
if (nova.config.get("apple_container.compose.terminal", "boolean") !== false) {
|
||||
await Terminal.run(Terminal.command([ComposeCLI.path()].concat(args)), directory);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await ComposeCLI.run(args, { cwd: directory });
|
||||
} catch (error) {
|
||||
notify.failure(title, error, "apple-container-compose");
|
||||
} finally {
|
||||
this.reload();
|
||||
}
|
||||
}
|
||||
|
||||
registerCommands() {
|
||||
this.register("apple_container.containers.reload", () => this.reload());
|
||||
|
||||
this.register("apple_container.containers.start", async () => {
|
||||
let ids = this.selectedContainers().map((container) => container.id);
|
||||
|
||||
try {
|
||||
await ContainerCLI.startContainers(ids);
|
||||
} catch (error) {
|
||||
notify.failure("Unable to start container", error, "apple-container-start");
|
||||
}
|
||||
|
||||
this.reload();
|
||||
});
|
||||
|
||||
this.register("apple_container.containers.stop", () =>
|
||||
this.runOnSelection((ids) => ContainerCLI.stopContainers(ids), "Unable to stop container")
|
||||
);
|
||||
|
||||
this.register("apple_container.containers.restart", () =>
|
||||
this.runOnSelection((ids) => ContainerCLI.restartContainers(ids), "Unable to restart container")
|
||||
);
|
||||
|
||||
this.register("apple_container.containers.kill", () =>
|
||||
this.runOnSelection((ids) => ContainerCLI.killContainers(ids), "Unable to kill container")
|
||||
);
|
||||
|
||||
this.register("apple_container.containers.startGroup", async () => {
|
||||
let ids = this.selectedProjects()
|
||||
.reduce((all, project) => all.concat(project.children), [])
|
||||
.filter((container) => !container.running)
|
||||
.map((container) => container.id);
|
||||
|
||||
try {
|
||||
await ContainerCLI.startContainers(ids);
|
||||
} catch (error) {
|
||||
notify.failure("Unable to start containers", error, "apple-container-start");
|
||||
}
|
||||
|
||||
this.reload();
|
||||
});
|
||||
|
||||
this.register("apple_container.containers.stopGroup", async () => {
|
||||
let ids = this.selectedProjects()
|
||||
.reduce((all, project) => all.concat(project.children), [])
|
||||
.filter((container) => container.running)
|
||||
.map((container) => container.id);
|
||||
|
||||
if (ids.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await ContainerCLI.stopContainers(ids);
|
||||
} catch (error) {
|
||||
notify.failure("Unable to stop containers", error, "apple-container-stop");
|
||||
}
|
||||
|
||||
this.reload();
|
||||
});
|
||||
|
||||
this.register("apple_container.containers.delete", async () => {
|
||||
let selected = this.selectedContainers();
|
||||
|
||||
if (selected.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
let running = selected.some((container) => container.running);
|
||||
let names = selected.map((container) => container.name).join(", ");
|
||||
let message = running
|
||||
? `Delete ${names}? Running containers will be stopped first.`
|
||||
: `Delete ${names}?`;
|
||||
|
||||
if (!(await prompt.confirm(message))) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await ContainerCLI.deleteContainers(selected.map((container) => container.id), running);
|
||||
} catch (error) {
|
||||
notify.failure("Unable to delete container", error, "apple-container-delete");
|
||||
}
|
||||
|
||||
this.reload();
|
||||
});
|
||||
|
||||
this.register("apple_container.containers.prune", async () => {
|
||||
if (!(await prompt.confirm("Remove all stopped containers?", "Remove"))) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await ContainerCLI.pruneContainers();
|
||||
} catch (error) {
|
||||
notify.failure("Unable to prune containers", error, "apple-container-prune");
|
||||
}
|
||||
|
||||
this.reload();
|
||||
});
|
||||
|
||||
this.register("apple_container.containers.shell", async () => {
|
||||
let shell = nova.config.get("apple_container.cli.shell", "string") || "/bin/sh";
|
||||
|
||||
for (let container of this.selectedContainers()) {
|
||||
await Terminal.run(
|
||||
Terminal.command([ContainerCLI.path(), "exec", "--interactive", "--tty", container.id, shell])
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.containers.follow", async () => {
|
||||
for (let container of this.selectedContainers()) {
|
||||
await Terminal.run(Terminal.command([ContainerCLI.path(), "logs", "--follow", container.id]));
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.containers.logs", () => this.openLogs([]));
|
||||
this.register("apple_container.containers.bootLogs", () => this.openLogs(["--boot"]));
|
||||
|
||||
this.register("apple_container.containers.browse", async () => {
|
||||
let url = await this.chooseURL();
|
||||
|
||||
if (url) {
|
||||
nova.openURL(url);
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.containers.inspect", async () => {
|
||||
for (let container of this.selectedContainers()) {
|
||||
try {
|
||||
let output = await ContainerCLI.inspectContainer(container.id);
|
||||
nova.workspace.openNewTextDocument({ content: output, syntax: "json" });
|
||||
} catch (error) {
|
||||
notify.failure("Unable to inspect container", error, "apple-container-inspect");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.containers.copy", async () => {
|
||||
let container = this.selectedContainers()[0];
|
||||
|
||||
if (!container) {
|
||||
return;
|
||||
}
|
||||
|
||||
let fields = [["Name", container.name], ["Image", container.image]];
|
||||
|
||||
if (container.address) {
|
||||
fields.push(["Address", container.address]);
|
||||
}
|
||||
if (container.hostname) {
|
||||
fields.push(["Hostname", container.hostname]);
|
||||
}
|
||||
if (container.url) {
|
||||
fields.push(["URL", container.url]);
|
||||
}
|
||||
|
||||
let choice = await prompt.choose(
|
||||
fields.map(([label, value]) => `${label}: ${value}`),
|
||||
{ placeholder: "Copy" }
|
||||
);
|
||||
|
||||
if (choice) {
|
||||
nova.clipboard.writeText(choice.slice(choice.indexOf(": ") + 2));
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.compose.upProject", async () => {
|
||||
for (let project of this.selectedProjects()) {
|
||||
await this.runCompose(project, (file) => ComposeCLI.upArgs(file), "Compose up failed");
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.compose.downProject", async () => {
|
||||
for (let project of this.selectedProjects()) {
|
||||
await this.runCompose(project, (file) => ComposeCLI.downArgs(file), "Compose down failed");
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.compose.buildProject", async () => {
|
||||
for (let project of this.selectedProjects()) {
|
||||
await this.runCompose(project, (file) => ComposeCLI.buildArgs(file), "Compose build failed");
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.compose.openFile", () => {
|
||||
for (let project of this.selectedProjects()) {
|
||||
let file = project.file || this.composeFileFor(project.name);
|
||||
|
||||
if (file) {
|
||||
nova.workspace.openFile(file);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
for (let key of [
|
||||
"apple_container.containers.group",
|
||||
"apple_container.containers.label",
|
||||
"apple_container.containers.icons",
|
||||
"apple_container.containers.desc.image",
|
||||
"apple_container.containers.desc.ports",
|
||||
"apple_container.containers.desc.address",
|
||||
"apple_container.containers.desc.state"
|
||||
]) {
|
||||
this.watch(key, () => this.reload());
|
||||
}
|
||||
}
|
||||
|
||||
async runOnSelection(action, message) {
|
||||
let ids = this.selectedContainers().map((container) => container.id);
|
||||
|
||||
if (ids.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await action(ids);
|
||||
} catch (error) {
|
||||
notify.failure(message, error, "apple-container-action");
|
||||
}
|
||||
|
||||
this.reload();
|
||||
}
|
||||
|
||||
// Picks the address to open: straight through when there is only one, and a
|
||||
// palette when a container publishes several ports.
|
||||
async chooseURL() {
|
||||
let container = this.selectedContainers()[0];
|
||||
|
||||
if (!container) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let urls = container.urls;
|
||||
|
||||
if (urls.length === 0) {
|
||||
notify.info(
|
||||
`Nothing to open for ${container.name}`,
|
||||
container.running
|
||||
? "The container publishes no ports and has no address yet."
|
||||
: "The container is not running.",
|
||||
"apple-container-browse"
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (urls.length === 1) {
|
||||
return urls[0].url;
|
||||
}
|
||||
|
||||
let choice = await prompt.choose(urls.map((entry) => entry.label), {
|
||||
placeholder: `Open ${container.name}`
|
||||
});
|
||||
|
||||
if (!choice) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let picked = urls.find((entry) => entry.label === choice);
|
||||
return picked ? picked.url : null;
|
||||
}
|
||||
|
||||
async openLogs(flags) {
|
||||
for (let container of this.selectedContainers()) {
|
||||
try {
|
||||
let result = await ContainerCLI.run(["logs"].concat(flags, [container.id]));
|
||||
let output = result.stdout || result.stderr || "No log output.";
|
||||
nova.workspace.openNewTextDocument({ content: output });
|
||||
} catch (error) {
|
||||
notify.failure("Unable to read logs", error, "apple-container-logs");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { ContainersController };
|
||||
@@ -0,0 +1,151 @@
|
||||
//
|
||||
// Base class for the sidebar's tree controllers.
|
||||
//
|
||||
// Apple Container has no event stream, so the sidebar polls. Reloading a
|
||||
// TreeView drops the user's selection, so each refresh compares a fingerprint
|
||||
// of the fetched data and only reloads when something actually changed.
|
||||
//
|
||||
|
||||
class Controller {
|
||||
constructor(id) {
|
||||
this.id = id;
|
||||
this.elements = [];
|
||||
this.fingerprint = null;
|
||||
// Collapsed rather than expanded, so groups keep their default of open.
|
||||
this.collapsed = new Set();
|
||||
this.disposables = new CompositeDisposable();
|
||||
|
||||
this.tree = new TreeView(id, { dataProvider: this });
|
||||
this.disposables.add(this.tree);
|
||||
|
||||
this.disposables.add(
|
||||
this.tree.onDidExpandElement((element) => {
|
||||
this.collapsed.delete(element.identifier);
|
||||
element.expanded = true;
|
||||
})
|
||||
);
|
||||
|
||||
this.disposables.add(
|
||||
this.tree.onDidCollapseElement((element) => {
|
||||
this.collapsed.add(element.identifier);
|
||||
element.expanded = false;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
// Subclasses return the elements to display, or null to leave the tree as
|
||||
// it is (used when the backing service is unavailable).
|
||||
async fetch() {
|
||||
return [];
|
||||
}
|
||||
|
||||
// A stable string describing the current data. When it does not change,
|
||||
// the tree is left alone.
|
||||
signature(elements) {
|
||||
let describe = (element) => [element.identifier, element.revision];
|
||||
|
||||
return JSON.stringify(
|
||||
elements.map((element) => describe(element).concat([(element.children || []).map(describe)]))
|
||||
);
|
||||
}
|
||||
|
||||
get visible() {
|
||||
// TreeView.visible is only meaningful once the sidebar has been shown.
|
||||
return this.tree.visible !== false;
|
||||
}
|
||||
|
||||
async refresh(options = {}) {
|
||||
let elements;
|
||||
|
||||
try {
|
||||
elements = await this.fetch();
|
||||
} catch (error) {
|
||||
if (nova.inDevMode()) {
|
||||
console.warn(`${this.id}: ${error.message}`);
|
||||
}
|
||||
elements = [];
|
||||
}
|
||||
|
||||
if (elements === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.restoreExpansion(elements);
|
||||
|
||||
let signature = this.signature(elements);
|
||||
|
||||
if (!options.force && signature === this.fingerprint) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.fingerprint = signature;
|
||||
this.elements = elements;
|
||||
this.tree.reload();
|
||||
}
|
||||
|
||||
// Rebuilt models are fresh objects, so expansion has to be carried over.
|
||||
restoreExpansion(elements) {
|
||||
for (let element of elements) {
|
||||
if (!element.children || element.children.length === 0) {
|
||||
continue;
|
||||
}
|
||||
if (this.collapsed.has(element.identifier)) {
|
||||
element.expanded = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Empties the tree, for when the backing service goes away.
|
||||
clear() {
|
||||
if (this.elements.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.elements = [];
|
||||
this.fingerprint = null;
|
||||
this.tree.reload();
|
||||
}
|
||||
|
||||
// Invalidates the fingerprint so the next refresh always reloads.
|
||||
invalidate() {
|
||||
this.fingerprint = null;
|
||||
}
|
||||
|
||||
reload() {
|
||||
this.invalidate();
|
||||
return this.refresh({ force: true });
|
||||
}
|
||||
|
||||
get selection() {
|
||||
return this.tree.selection || [];
|
||||
}
|
||||
|
||||
getChildren(element) {
|
||||
if (!element) {
|
||||
return this.elements;
|
||||
}
|
||||
return element.children || [];
|
||||
}
|
||||
|
||||
getParent(element) {
|
||||
return element.parent;
|
||||
}
|
||||
|
||||
getTreeItem() {
|
||||
throw new Error("getTreeItem must be implemented by the controller.");
|
||||
}
|
||||
|
||||
register(command, handler) {
|
||||
this.disposables.add(nova.commands.register(command, handler));
|
||||
}
|
||||
|
||||
watch(key, handler) {
|
||||
this.disposables.add(nova.config.onDidChange(key, handler));
|
||||
}
|
||||
|
||||
dispose() {
|
||||
this.disposables.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { Controller };
|
||||
@@ -0,0 +1,198 @@
|
||||
//
|
||||
// Images section, optionally grouped by repository.
|
||||
//
|
||||
|
||||
const { Controller } = require("./Controller");
|
||||
const { ImageTreeItem, RepositoryTreeItem } = require("../TreeItems/ImageTreeItem");
|
||||
const { ContainerCLI } = require("../../Interfaces/ContainerCLI");
|
||||
const { Terminal } = require("../../Interfaces/Terminal");
|
||||
const images = require("../Models/Image");
|
||||
const notify = require("../../Utilities/Notify");
|
||||
const prompt = require("../../Utilities/Prompt");
|
||||
|
||||
class ImagesController extends Controller {
|
||||
constructor(sidebar) {
|
||||
super("apple_container.images");
|
||||
this.sidebar = sidebar;
|
||||
this.registerCommands();
|
||||
}
|
||||
|
||||
async fetch() {
|
||||
let list = await ContainerCLI.listImages();
|
||||
|
||||
return images.build(list, {
|
||||
group: nova.config.get("apple_container.images.group", "boolean") !== false,
|
||||
registry: nova.config.get("apple_container.images.registry", "boolean") === true,
|
||||
infrastructure: nova.config.get("apple_container.images.infra", "boolean") === true
|
||||
});
|
||||
}
|
||||
|
||||
getTreeItem(element) {
|
||||
if (element instanceof images.RepositoryModel) {
|
||||
return new RepositoryTreeItem(element);
|
||||
}
|
||||
|
||||
return new ImageTreeItem(element, { grouped: element.parent !== null });
|
||||
}
|
||||
|
||||
// References for the current selection, expanding repositories into tags.
|
||||
selectedReferences() {
|
||||
let selected = [];
|
||||
|
||||
for (let element of this.selection) {
|
||||
if (element instanceof images.RepositoryModel) {
|
||||
selected = selected.concat(element.children.map((image) => image.reference));
|
||||
} else {
|
||||
selected.push(element.reference);
|
||||
}
|
||||
}
|
||||
|
||||
return selected;
|
||||
}
|
||||
|
||||
registerCommands() {
|
||||
this.register("apple_container.images.reload", () => this.reload());
|
||||
|
||||
this.register("apple_container.images.run", async () => {
|
||||
let reference = this.selectedReferences()[0];
|
||||
|
||||
if (!reference) {
|
||||
return;
|
||||
}
|
||||
|
||||
await runImage(reference);
|
||||
this.sidebar.reloadContainers();
|
||||
});
|
||||
|
||||
this.register("apple_container.images.pull", async () => {
|
||||
for (let reference of this.selectedReferences()) {
|
||||
try {
|
||||
await ContainerCLI.pullImage(reference);
|
||||
} catch (error) {
|
||||
notify.failure(`Unable to pull ${reference}`, error, "apple-container-pull");
|
||||
}
|
||||
}
|
||||
|
||||
this.reload();
|
||||
});
|
||||
|
||||
this.register("apple_container.images.push", async () => {
|
||||
for (let reference of this.selectedReferences()) {
|
||||
try {
|
||||
await ContainerCLI.pushImage(reference);
|
||||
notify.info("Push complete", reference, "apple-container-push");
|
||||
} catch (error) {
|
||||
notify.failure(`Unable to push ${reference}`, error, "apple-container-push");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.images.tag", async () => {
|
||||
let reference = this.selectedReferences()[0];
|
||||
|
||||
if (!reference) {
|
||||
return;
|
||||
}
|
||||
|
||||
let target = await prompt.input("New tag", { placeholder: reference, value: reference });
|
||||
|
||||
if (!target) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await ContainerCLI.tagImage(reference, target);
|
||||
} catch (error) {
|
||||
notify.failure("Unable to tag image", error, "apple-container-tag");
|
||||
}
|
||||
|
||||
this.reload();
|
||||
});
|
||||
|
||||
this.register("apple_container.images.inspect", async () => {
|
||||
for (let reference of this.selectedReferences()) {
|
||||
try {
|
||||
let output = await ContainerCLI.inspectImage(reference);
|
||||
nova.workspace.openNewTextDocument({ content: output, syntax: "json" });
|
||||
} catch (error) {
|
||||
notify.failure("Unable to inspect image", error, "apple-container-inspect");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.images.copy", () => {
|
||||
let reference = this.selectedReferences()[0];
|
||||
|
||||
if (reference) {
|
||||
nova.clipboard.writeText(reference);
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.images.delete", async () => {
|
||||
let references = this.selectedReferences();
|
||||
|
||||
if (references.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(await prompt.confirm(`Delete ${references.join(", ")}?`))) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await ContainerCLI.deleteImages(references);
|
||||
} catch (error) {
|
||||
notify.failure("Unable to delete image", error, "apple-container-delete-image");
|
||||
}
|
||||
|
||||
this.reload();
|
||||
});
|
||||
|
||||
this.register("apple_container.images.prune", async () => {
|
||||
if (!(await prompt.confirm("Remove images not referenced by a container?", "Remove"))) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await ContainerCLI.pruneImages();
|
||||
} catch (error) {
|
||||
notify.failure("Unable to prune images", error, "apple-container-prune-image");
|
||||
}
|
||||
|
||||
this.reload();
|
||||
});
|
||||
|
||||
for (let key of [
|
||||
"apple_container.images.group",
|
||||
"apple_container.images.registry",
|
||||
"apple_container.images.infra"
|
||||
]) {
|
||||
this.watch(key, () => this.reload());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Runs an image in the terminal, so its output is visible and an interactive
|
||||
// image still works. Arguments are offered because `container run` takes no
|
||||
// defaults worth guessing.
|
||||
async function runImage(reference) {
|
||||
let options = await prompt.input("Arguments for container run", {
|
||||
placeholder: "--detach --name my-container --publish 8080:80"
|
||||
});
|
||||
|
||||
if (options === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let parts = [ContainerCLI.path(), "run"];
|
||||
|
||||
if (options) {
|
||||
parts = parts.concat(options.split(/\s+/));
|
||||
}
|
||||
|
||||
parts.push(reference);
|
||||
|
||||
return Terminal.run(Terminal.command(parts));
|
||||
}
|
||||
|
||||
module.exports = { ImagesController, runImage };
|
||||
@@ -0,0 +1,114 @@
|
||||
//
|
||||
// Networks section.
|
||||
//
|
||||
|
||||
const { Controller } = require("./Controller");
|
||||
const { NetworkTreeItem } = require("../TreeItems/NetworkTreeItem");
|
||||
const { ContainerCLI } = require("../../Interfaces/ContainerCLI");
|
||||
const networks = require("../Models/Network");
|
||||
const notify = require("../../Utilities/Notify");
|
||||
const prompt = require("../../Utilities/Prompt");
|
||||
|
||||
class NetworksController extends Controller {
|
||||
constructor(sidebar) {
|
||||
super("apple_container.networks");
|
||||
this.sidebar = sidebar;
|
||||
this.registerCommands();
|
||||
}
|
||||
|
||||
async fetch() {
|
||||
return networks.build(await ContainerCLI.listNetworks());
|
||||
}
|
||||
|
||||
getTreeItem(element) {
|
||||
return new NetworkTreeItem(element);
|
||||
}
|
||||
|
||||
registerCommands() {
|
||||
this.register("apple_container.networks.reload", () => this.reload());
|
||||
|
||||
this.register("apple_container.networks.create", async () => {
|
||||
// A name on its own is the common case, but `container network
|
||||
// create` also takes --subnet, --internal and friends, so the whole
|
||||
// argument line is accepted here.
|
||||
let answer = await prompt.input("Network name, and any options", {
|
||||
placeholder: "backend --subnet 192.168.70.0/24"
|
||||
});
|
||||
|
||||
if (!answer) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await ContainerCLI.createNetwork(answer.split(/\s+/));
|
||||
} catch (error) {
|
||||
notify.failure("Unable to create network", error, "apple-container-network");
|
||||
}
|
||||
|
||||
this.reload();
|
||||
});
|
||||
|
||||
this.register("apple_container.networks.inspect", async () => {
|
||||
for (let network of this.selection) {
|
||||
try {
|
||||
let output = await ContainerCLI.inspectNetwork(network.name);
|
||||
nova.workspace.openNewTextDocument({ content: output, syntax: "json" });
|
||||
} catch (error) {
|
||||
notify.failure("Unable to inspect network", error, "apple-container-inspect");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.networks.copy", () => {
|
||||
let network = this.selection[0];
|
||||
|
||||
if (network) {
|
||||
nova.clipboard.writeText(network.subnet || network.name);
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.networks.delete", async () => {
|
||||
let selected = this.selection;
|
||||
let builtin = selected.filter((network) => network.builtin);
|
||||
|
||||
if (builtin.length > 0) {
|
||||
notify.info(
|
||||
"Built-in network",
|
||||
`${builtin.map((network) => network.name).join(", ")} is managed by Apple Container and cannot be deleted.`,
|
||||
"apple-container-network-builtin"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let names = selected.map((network) => network.name);
|
||||
|
||||
if (names.length === 0 || !(await prompt.confirm(`Delete ${names.join(", ")}?`))) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await ContainerCLI.deleteNetworks(names);
|
||||
} catch (error) {
|
||||
notify.failure("Unable to delete network", error, "apple-container-delete-network");
|
||||
}
|
||||
|
||||
this.reload();
|
||||
});
|
||||
|
||||
this.register("apple_container.networks.prune", async () => {
|
||||
if (!(await prompt.confirm("Remove networks with no connected containers?", "Remove"))) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await ContainerCLI.pruneNetworks();
|
||||
} catch (error) {
|
||||
notify.failure("Unable to prune networks", error, "apple-container-prune-network");
|
||||
}
|
||||
|
||||
this.reload();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { NetworksController };
|
||||
@@ -0,0 +1,221 @@
|
||||
//
|
||||
// System section: the container services, the builder, disk usage and any
|
||||
// local DNS domains.
|
||||
//
|
||||
|
||||
const { Controller } = require("./Controller");
|
||||
const { SystemTreeItem } = require("../TreeItems/SystemTreeItem");
|
||||
const { ContainerCLI } = require("../../Interfaces/ContainerCLI");
|
||||
const systemConfig = require("../../Utilities/SystemConfig");
|
||||
const format = require("../../Utilities/Format");
|
||||
const { Terminal } = require("../../Interfaces/Terminal");
|
||||
const notify = require("../../Utilities/Notify");
|
||||
const prompt = require("../../Utilities/Prompt");
|
||||
|
||||
class SystemController extends Controller {
|
||||
constructor(sidebar) {
|
||||
super("apple_container.system");
|
||||
this.sidebar = sidebar;
|
||||
this.running = false;
|
||||
this.registerCommands();
|
||||
}
|
||||
|
||||
async fetch() {
|
||||
let status = await ContainerCLI.systemStatus();
|
||||
let running = status.status === "running";
|
||||
let rows = [];
|
||||
|
||||
this.running = running;
|
||||
|
||||
rows.push({
|
||||
identifier: "system.services",
|
||||
label: "Services",
|
||||
running: running,
|
||||
contextValue: running ? "system.services.running" : "system.services.stopped",
|
||||
description: running ? version(status.apiServerVersion) : "stopped",
|
||||
tooltip: statusTooltip(status),
|
||||
copy: status.apiServerVersion || "",
|
||||
children: []
|
||||
});
|
||||
|
||||
if (!running) {
|
||||
return rows;
|
||||
}
|
||||
|
||||
let [builder, usage, domains] = await Promise.all([
|
||||
ContainerCLI.builderRunning(),
|
||||
ContainerCLI.systemDiskUsage(),
|
||||
ContainerCLI.dnsDomains()
|
||||
]);
|
||||
|
||||
rows.push({
|
||||
identifier: "system.builder",
|
||||
label: "Builder",
|
||||
running: builder,
|
||||
contextValue: builder ? "system.builder.running" : "system.builder.stopped",
|
||||
description: builder ? "running" : "stopped",
|
||||
tooltip: "The builder container backing `container build`.",
|
||||
copy: "builder",
|
||||
children: []
|
||||
});
|
||||
|
||||
if (usage) {
|
||||
rows.push({
|
||||
identifier: "system.disk",
|
||||
label: "Disk Usage",
|
||||
contextValue: "system.disk",
|
||||
image: "icons/system.png",
|
||||
description: usageSummary(usage),
|
||||
tooltip: usageTooltip(usage),
|
||||
copy: usageSummary(usage),
|
||||
children: []
|
||||
});
|
||||
}
|
||||
|
||||
// The domain containers are named under, which is separate from the
|
||||
// domains macOS has been told to resolve.
|
||||
let hostnameDomain = systemConfig.defaultDomain();
|
||||
let resolved = hostnameDomain !== null && domains.includes(hostnameDomain);
|
||||
|
||||
rows.push({
|
||||
identifier: "system.dns.default",
|
||||
label: "Hostname Domain",
|
||||
contextValue: "system.dns.default",
|
||||
image: hostnameDomain && !resolved ? "icons/status/warning.png" : "icons/network.png",
|
||||
description: hostnameDomain ? `.${hostnameDomain}` : "not set",
|
||||
tooltip: hostnameTooltip(hostnameDomain, resolved),
|
||||
copy: hostnameDomain || "",
|
||||
children: []
|
||||
});
|
||||
|
||||
for (let domain of domains) {
|
||||
rows.push({
|
||||
identifier: `system.dns.${domain}`,
|
||||
label: domain,
|
||||
contextValue: "system.dns",
|
||||
image: "icons/network.png",
|
||||
description: "resolved by macOS",
|
||||
tooltip: `macOS resolves *.${domain} through Apple Container's DNS service.`,
|
||||
copy: domain,
|
||||
children: []
|
||||
});
|
||||
}
|
||||
|
||||
return rows;
|
||||
}
|
||||
|
||||
signature(rows) {
|
||||
return JSON.stringify(rows.map((row) => [row.identifier, row.description, row.running]));
|
||||
}
|
||||
|
||||
getTreeItem(element) {
|
||||
return new SystemTreeItem(element);
|
||||
}
|
||||
|
||||
registerCommands() {
|
||||
this.register("apple_container.system.df", async () => {
|
||||
try {
|
||||
let result = await ContainerCLI.run(["system", "df", "--format", "json"]);
|
||||
nova.workspace.openNewTextDocument({ content: result.stdout, syntax: "json" });
|
||||
} catch (error) {
|
||||
notify.failure("Unable to read disk usage", error, "apple-container-df");
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.dns.unregister", async () => {
|
||||
let row = this.selection[0];
|
||||
|
||||
if (!row || !row.copy) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
!(await prompt.confirm(
|
||||
`Stop macOS resolving *.${row.copy}? This runs sudo in your terminal.`,
|
||||
"Unregister"
|
||||
))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
await Terminal.run(
|
||||
`sudo ${Terminal.command([ContainerCLI.path(), "system", "dns", "delete", row.copy])}`
|
||||
);
|
||||
});
|
||||
|
||||
this.register("apple_container.system.copy", () => {
|
||||
let row = this.selection[0];
|
||||
|
||||
if (row && row.copy) {
|
||||
nova.clipboard.writeText(row.copy);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function hostnameTooltip(domain, resolved) {
|
||||
if (!domain) {
|
||||
return [
|
||||
"Containers have unqualified hostnames.",
|
||||
"Set a domain to reach them as <name>.<domain>."
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
if (resolved) {
|
||||
return `Containers are reachable as <name>.${domain} from this Mac.`;
|
||||
}
|
||||
|
||||
return [
|
||||
`Containers are named <name>.${domain}, but macOS does not resolve that domain yet.`,
|
||||
"Use Set Up Container Hostnames to register it."
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function version(text) {
|
||||
if (!text) {
|
||||
return "running";
|
||||
}
|
||||
|
||||
let match = String(text).match(/version\s+(\S+)/);
|
||||
return match ? `running ${match[1]}` : "running";
|
||||
}
|
||||
|
||||
function statusTooltip(status) {
|
||||
if (status.status !== "running") {
|
||||
return "Container services are not running.\nUse Start Services to launch them.";
|
||||
}
|
||||
|
||||
return [
|
||||
`Status:\t${status.status}`,
|
||||
status.apiServerVersion ? `Version:\t${status.apiServerVersion}` : "",
|
||||
status.appRoot ? `App root:\t${status.appRoot}` : "",
|
||||
status.installRoot ? `Install:\t${status.installRoot}` : ""
|
||||
]
|
||||
.filter((line) => line.length > 0)
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
function usageSummary(usage) {
|
||||
let total = ["images", "containers", "volumes"].reduce(
|
||||
(sum, key) => sum + ((usage[key] && usage[key].sizeInBytes) || 0),
|
||||
0
|
||||
);
|
||||
let reclaimable = ["images", "containers", "volumes"].reduce(
|
||||
(sum, key) => sum + ((usage[key] && usage[key].reclaimable) || 0),
|
||||
0
|
||||
);
|
||||
|
||||
return `${format.bytes(total)} ${format.bytes(reclaimable)} reclaimable`;
|
||||
}
|
||||
|
||||
function usageTooltip(usage) {
|
||||
return ["images", "containers", "volumes"]
|
||||
.map((key) => {
|
||||
let entry = usage[key] || {};
|
||||
let name = key.charAt(0).toUpperCase() + key.slice(1);
|
||||
return `${name}:\t${entry.total || 0}\t${format.bytes(entry.sizeInBytes || 0)}`;
|
||||
})
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
module.exports = { SystemController };
|
||||
@@ -0,0 +1,116 @@
|
||||
//
|
||||
// Volumes section.
|
||||
//
|
||||
|
||||
const { Controller } = require("./Controller");
|
||||
const { VolumeTreeItem } = require("../TreeItems/VolumeTreeItem");
|
||||
const { ContainerCLI } = require("../../Interfaces/ContainerCLI");
|
||||
const { exec } = require("../../Utilities/Process");
|
||||
const volumes = require("../Models/Volume");
|
||||
const notify = require("../../Utilities/Notify");
|
||||
const prompt = require("../../Utilities/Prompt");
|
||||
|
||||
class VolumesController extends Controller {
|
||||
constructor(sidebar) {
|
||||
super("apple_container.volumes");
|
||||
this.sidebar = sidebar;
|
||||
this.registerCommands();
|
||||
}
|
||||
|
||||
async fetch() {
|
||||
return volumes.build(await ContainerCLI.listVolumes());
|
||||
}
|
||||
|
||||
getTreeItem(element) {
|
||||
return new VolumeTreeItem(element);
|
||||
}
|
||||
|
||||
registerCommands() {
|
||||
this.register("apple_container.volumes.reload", () => this.reload());
|
||||
|
||||
this.register("apple_container.volumes.create", async () => {
|
||||
let name = await prompt.input("Volume name", { placeholder: "database-data" });
|
||||
|
||||
if (!name) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await ContainerCLI.createVolume(name);
|
||||
} catch (error) {
|
||||
notify.failure("Unable to create volume", error, "apple-container-volume");
|
||||
}
|
||||
|
||||
this.reload();
|
||||
});
|
||||
|
||||
this.register("apple_container.volumes.inspect", async () => {
|
||||
for (let volume of this.selection) {
|
||||
try {
|
||||
let output = await ContainerCLI.inspectVolume(volume.name);
|
||||
nova.workspace.openNewTextDocument({ content: output, syntax: "json" });
|
||||
} catch (error) {
|
||||
notify.failure("Unable to inspect volume", error, "apple-container-inspect");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.volumes.reveal", async () => {
|
||||
for (let volume of this.selection) {
|
||||
if (!volume.source) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
await exec("/usr/bin/open", ["-R", volume.source]);
|
||||
} catch (error) {
|
||||
notify.failure("Unable to reveal volume", error, "apple-container-reveal");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.volumes.copy", () => {
|
||||
let volume = this.selection[0];
|
||||
|
||||
if (volume) {
|
||||
nova.clipboard.writeText(volume.name);
|
||||
}
|
||||
});
|
||||
|
||||
this.register("apple_container.volumes.delete", async () => {
|
||||
let names = this.selection.map((volume) => volume.name);
|
||||
|
||||
if (names.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(await prompt.confirm(`Delete ${names.join(", ")}? Their contents are lost.`))) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await ContainerCLI.deleteVolumes(names);
|
||||
} catch (error) {
|
||||
notify.failure("Unable to delete volume", error, "apple-container-delete-volume");
|
||||
}
|
||||
|
||||
this.reload();
|
||||
});
|
||||
|
||||
this.register("apple_container.volumes.prune", async () => {
|
||||
if (!(await prompt.confirm("Remove volumes no container references?", "Remove"))) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await ContainerCLI.pruneVolumes();
|
||||
} catch (error) {
|
||||
notify.failure("Unable to prune volumes", error, "apple-container-prune-volume");
|
||||
}
|
||||
|
||||
this.reload();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { VolumesController };
|
||||
Reference in New Issue
Block a user