@runium/plugin-docker 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{spawn as a}from"node:child_process";import{stringify as p}from"yaml";import{DockerTaskBase as c}from"./docker-task-base.js";import{isDockerInstalled as m}from"../utils/is-docker-installed.js";import{isDockerRunning as d}from"../utils/is-docker-running.js";import{pullImage as g}from"../utils/pull-image.js";const{TaskStatus:o}=runium.enum;class
|
|
1
|
+
import{spawn as a}from"node:child_process";import{stringify as p}from"yaml";import{DockerTaskBase as c}from"./docker-task-base.js";import{isDockerInstalled as m}from"../utils/is-docker-installed.js";import{isDockerRunning as d}from"../utils/is-docker-running.js";import{pullImage as g}from"../utils/pull-image.js";import{isImagePulled as u}from"../utils/is-image-pulled.js";const{TaskStatus:o}=runium.enum;class x extends c{constructor(r){super(r);this.options=r;this.projectName=`runium-docker-compose-${Date.now()}`,this.composeFile=`docker-compose-${this.projectName}.yml`}projectName;composeFile;version="3.8";correctExitCodes=[130];generateComposeYaml(){const r={version:this.version,services:{}};for(const[s,e]of Object.entries(this.options.services)){const t={image:e.image};e.containerName&&(t.container_name=e.containerName),e.command&&(t.command=e.command),e.ports&&e.ports.length>0&&(t.ports=e.ports),e.volumes&&e.volumes.length>0&&(t.volumes=e.volumes),e.environment&&Object.keys(e.environment).length>0&&(t.environment=e.environment),e.networks&&e.networks.length>0&&(t.networks=e.networks),e.dependsOn&&e.dependsOn.length>0&&(t.depends_on=e.dependsOn),e.restart&&(t.restart=e.restart),e.user&&(t.user=e.user),e.workdir&&(t.working_dir=e.workdir),e.hostname&&(t.hostname=e.hostname),e.entrypoint&&(t.entrypoint=e.entrypoint),e.privileged&&(t.privileged=e.privileged),e.expose&&e.expose.length>0&&(t.expose=e.expose),e.healthcheck&&(t.healthcheck=e.healthcheck),r.services[s]=t}return this.options.networks&&Object.keys(this.options.networks).length>0&&(r.networks=this.options.networks),this.options.volumes&&Object.keys(this.options.volumes).length>0&&(r.volumes=this.options.volumes),p(r)}async pullAllImages(){const r=Object.values(this.options.services).map(e=>e.image),s=[...new Set(r)];for(const e of s){const t=e;await u(t)||(this.updateState({reason:`pull image: ${t}`}),await g(t,{onStdErr:this.onStdErrData.bind(this)}))}}prepareDockerComposeArgs(r){const s=["compose"];return s.push("-p",this.projectName),s.push("-f",runium.storage.getPath(["docker",this.composeFile])),s.push(r),s}async start(){if(this.canStart()){if(this.updateState({status:o.STARTING,iteration:this.state.iteration+1,pid:-1,exitCode:void 0,error:void 0,reason:void 0}),!await m()){this.onError(new Error("Docker is not installed"));return}if(!await d()){this.onError(new Error("Docker is not running"));return}try{const r=this.generateComposeYaml();await runium.storage.ensureDirExists(["docker"]),await runium.storage.write(["docker",this.composeFile],r),await this.initLogStreams(),await this.pullAllImages();const s=this.prepareDockerComposeArgs("up");this.process=a("docker",s,{stdio:["ignore","pipe","pipe"]}),this.addProcessListeners(),this.setTTLTimer(),this.updateState({status:o.STARTED,pid:this.process.pid,reason:void 0})}catch(r){this.onError(r)}}}async stop(r=""){if(this.canStop()){this.updateState({status:o.STOPPING,reason:r});try{const s=this.prepareDockerComposeArgs("down");await new Promise((e,t)=>{const i=a("docker",s);i.on("exit",n=>{n===0?e():t(new Error(`Docker compose down exited with code ${n}`))}),i.on("error",t)})}catch(s){this.onError(s)}}}}export{x as DockerComposeTask};
|
package/tasks/docker-task.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{spawn as n}from"node:child_process";import{DockerTaskBase as a}from"./docker-task-base.js";import{isDockerInstalled as p}from"../utils/is-docker-installed.js";import{isDockerRunning as h}from"../utils/is-docker-running.js";import{pullImage as c}from"../utils/pull-image.js";const{TaskStatus:o}=runium.enum;class
|
|
1
|
+
import{spawn as n}from"node:child_process";import{DockerTaskBase as a}from"./docker-task-base.js";import{isDockerInstalled as p}from"../utils/is-docker-installed.js";import{isDockerRunning as h}from"../utils/is-docker-running.js";import{pullImage as c}from"../utils/pull-image.js";import{isImagePulled as u}from"../utils/is-image-pulled.js";const{TaskStatus:o}=runium.enum;class E extends a{constructor(t){super(t);this.options=t;this.options.containerName??=`runium-${this.options.image.replaceAll("/","-").replaceAll(":","-")}-${Date.now()}`}correctExitCodes=[137];prepareDockerArgs(){const t=["run"];return this.options.autoRemove!==!1&&t.push("--rm"),this.options.containerName&&t.push("--name",this.options.containerName),this.options.ports&&this.options.ports.forEach(i=>{t.push("-p",i)}),this.options.volumes&&this.options.volumes.forEach(i=>{t.push("-v",i)}),this.options.env&&Object.entries(this.options.env).forEach(([i,s])=>{t.push("-e",`${i}=${s}`)}),this.options.network&&t.push("--network",this.options.network),this.options.user&&t.push("--user",this.options.user),this.options.workdir&&t.push("--workdir",this.options.workdir),this.options.entrypoint&&t.push("--entrypoint",this.options.entrypoint),this.options.privileged&&t.push("--privileged"),t.push(this.options.image),this.options.command&&t.push(this.options.command),this.options.arguments&&t.push(...this.options.arguments),t}async start(){if(this.canStart()){if(this.updateState({status:o.STARTING,iteration:this.state.iteration+1,pid:-1,containerName:void 0,exitCode:void 0,error:void 0,reason:void 0}),!await p()){this.onError(new Error("Docker is not installed"));return}if(!await h()){this.onError(new Error("Docker is not running"));return}try{await this.initLogStreams();const t=this.options.image;await u(t)||(this.updateState({reason:`pull image: ${t}`}),await c(t,{onStdErr:this.onStdErrData.bind(this)}));const i=this.prepareDockerArgs();this.process=n("docker",i,{stdio:["ignore","pipe","pipe"]}),this.addProcessListeners(),this.setTTLTimer(),this.updateState({status:o.STARTED,containerName:this.options.containerName,pid:this.process.pid,reason:void 0})}catch(t){this.onError(t)}}}async stop(t=""){if(this.canStop()){this.updateState({status:o.STOPPING,reason:t});try{await new Promise((i,s)=>{const e=n("docker",["stop",this.options.containerName]);e.on("exit",r=>{r===0?i():s(new Error(`Docker stop exited with code ${r}`))}),e.on("error",s)})}catch(i){this.onError(i)}finally{await new Promise(i=>{this.process?.kill("SIGTERM"),this.process?.on("exit",()=>{i()})})}}}}export{E as DockerTask};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{execAsync as r}from"./exec-async.js";async function n(e){try{return await r(`docker image inspect ${e}`),!0}catch{return!1}}export{n as isImagePulled};
|