@cloud-cli/dx 1.3.2 → 1.5.0

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/README.md CHANGED
@@ -12,10 +12,9 @@ npm i @cloud-cli/dx
12
12
 
13
13
  ```bash
14
14
  cy dx.list
15
- cy dx.status
16
15
  cy dx.prune
17
- cy dx.pull node:latest
18
- cy dx.add --name node-app --image node --version latest --command 'node --version' --volumes 'volume:/container/path' --ports '1234:80' --host 'foo.example.com'
16
+ cy dx.pull --image node:latest
17
+ cy dx.add --name node-app --image node:latest --volumes 'volume:/container/path' --ports '1234:80' --host 'foo.example.com'
19
18
  cy dx.start --name node-app --env 'FOO=bar' --ports '1234:80'
20
19
  cy dx.logs --name node-app
21
20
  cy dx.stop --name node-app
package/cloud.db ADDED
Binary file
@@ -1,17 +1,15 @@
1
+ import { ServerParams } from '@cloud-cli/cli';
1
2
  export declare function getRunningContainers(): Promise<string[]>;
2
3
  interface GetLogsOptions {
3
4
  name: string;
4
5
  lines?: string;
5
6
  }
6
- export declare function getLogs({ name, lines }: GetLogsOptions): Promise<string>;
7
- interface RunOptions {
8
- name: string;
9
- env?: string[] | string;
10
- ports?: string;
11
- }
12
- export declare function startContainer(options: RunOptions, { run }: any): Promise<boolean>;
13
- export interface StopOptions {
7
+ interface ContainerName {
14
8
  name: string;
15
9
  }
16
- export declare function stopContainer(options: StopOptions): Promise<boolean>;
10
+ export declare function getLogs({ name, lines }: GetLogsOptions): Promise<string>;
11
+ export declare function startAll(_: any, cli: any): Promise<boolean>;
12
+ export declare function refreshContainer(options: ContainerName, { run }: ServerParams): Promise<void>;
13
+ export declare function startContainer(options: ContainerName, { run }: ServerParams): Promise<boolean>;
14
+ export declare function stopContainer(options: ContainerName, { run }: ServerParams): Promise<boolean>;
17
15
  export {};
@@ -1,6 +1,6 @@
1
1
  import { exec } from '@cloud-cli/exec';
2
- import { findContainer } from './store.js';
3
- import getPort from 'get-port';
2
+ import { findContainer, listContainers } from './store.js';
3
+ import { addExecFlag, getEnvVars, getListFromString, getPorts } from './utils.js';
4
4
  export async function getRunningContainers() {
5
5
  const ps = await exec('docker', ['ps', '--format', '{{.Names}}']);
6
6
  if (!ps.ok) {
@@ -22,23 +22,40 @@ export async function getLogs({ name, lines }) {
22
22
  }
23
23
  return '';
24
24
  }
25
- const defaultRunOptions = { ports: '', env: [] };
25
+ export async function startAll(_, cli) {
26
+ const list = await listContainers();
27
+ const running = await getRunningContainers();
28
+ const notRunning = list.filter(({ name }) => !running.includes(name));
29
+ for (const app of notRunning) {
30
+ await refreshContainer({ name: app.name }, cli);
31
+ }
32
+ return true;
33
+ }
34
+ export async function refreshContainer(options, { run }) {
35
+ const { name } = options;
36
+ const container = await findContainer(name);
37
+ await run('dx.pull', { image: container.image });
38
+ await run('dx.stop', { name });
39
+ await run('dx.prune', {});
40
+ await run('dx.start', { name });
41
+ }
26
42
  export async function startContainer(options, { run }) {
27
- options = { ...defaultRunOptions, ...options };
28
43
  const { name } = options;
29
44
  if (!name) {
30
45
  throw new Error('Name is required');
31
46
  }
32
47
  const vars = (await run('env.show', { app: name }));
33
- const { env, envKeys } = await getEnvVars(options.env, vars);
48
+ const { env, envKeys } = await getEnvVars(vars);
34
49
  const container = await findContainer(name);
35
50
  const volumes = addExecFlag(container.volumes, 'v');
36
- const ports = getPorts([...container.ports.split(','), ...options.ports.split(','), env.PORT + ':' + env.PORT]);
51
+ const ports = getPorts([...container.ports.split(','), env.PORT + ':' + env.PORT]);
37
52
  if (container.host) {
38
53
  await run('px.remove', { domain: container.host });
39
54
  await run('px.add', { domain: container.host, target: 'http://localhost:' + env.PORT, cors: true, redirect: true });
55
+ await run('dns.add', { domain: container.host });
56
+ await run('dns.reload', {});
40
57
  }
41
- await exec('docker', [
58
+ const execArgs = [
42
59
  'run',
43
60
  '--detach',
44
61
  '--restart',
@@ -49,49 +66,22 @@ export async function startContainer(options, { run }) {
49
66
  ...ports,
50
67
  ...envKeys.map((e) => '-e' + e),
51
68
  container.image,
52
- ], { env });
53
- return true;
54
- }
55
- function getPorts(ports) {
56
- const map = {};
57
- ports.filter(Boolean).forEach((str) => {
58
- const [left, right] = str.split(':');
59
- map[left] = right;
60
- });
61
- return Object.entries(map).map(([left, right]) => '-p' + left + ':' + right);
62
- }
63
- async function getEnvVars(inputEnv, vars) {
64
- const port = await getPort();
65
- const envVars = vars.concat({ key: 'PORT', value: String(port) });
66
- const additionalEnv = [inputEnv].flat();
67
- if (Array.isArray(additionalEnv)) {
68
- additionalEnv.forEach((str) => {
69
- let [key, value] = str.split('=');
70
- envVars.push({ key, value });
71
- });
69
+ ];
70
+ const output = await exec('docker', execArgs, { env });
71
+ if (!output.ok) {
72
+ throw new Error('Failed to start ' + name);
72
73
  }
73
- const envKeys = [];
74
- const env = {};
75
- envVars.forEach(({ key, value }) => {
76
- envKeys.push(key);
77
- env[key] = String(value);
78
- });
79
- return { env, envKeys: [...new Set(envKeys)] };
74
+ return true;
80
75
  }
81
- export async function stopContainer(options) {
82
- if (!options.name) {
76
+ export async function stopContainer(options, { run }) {
77
+ let { name } = options;
78
+ if (!name) {
83
79
  throw new Error('Name is required');
84
80
  }
85
- await exec('docker', ['stop', '-t', '5', options.name]);
86
- await exec('docker', ['rm', options.name]);
81
+ const container = await findContainer(name);
82
+ await exec('docker', ['stop', '-t', '5', name]);
83
+ await exec('docker', ['rm', name]);
84
+ await run('dns.remove', { domain: container.host });
85
+ await run('dns.reload', {});
87
86
  return true;
88
87
  }
89
- function getListFromString(string) {
90
- return string.trim().split('\n').filter(Boolean);
91
- }
92
- function addExecFlag(string, flag) {
93
- return string
94
- .split(',')
95
- .filter(Boolean)
96
- .map((p) => `-${flag}${p}`);
97
- }
package/dist/index.d.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  import { init } from '@cloud-cli/cli';
2
- import { getRunningContainers, getLogs, startContainer, stopContainer } from './containers.js';
2
+ import { getRunningContainers, getLogs, startContainer, stopContainer, refreshContainer, startAll } from './containers.js';
3
3
  import { addContainer, removeContainer, listContainers, updateContainer, getContainer } from './store.js';
4
4
  import { pull, prune } from './images.js';
5
- declare function reload(): Promise<void>;
6
5
  declare const _default: {
7
6
  pull: typeof pull;
8
7
  prune: typeof prune;
@@ -10,12 +9,13 @@ declare const _default: {
10
9
  remove: typeof removeContainer;
11
10
  get: typeof getContainer;
12
11
  list: typeof listContainers;
12
+ refresh: typeof refreshContainer;
13
13
  update: typeof updateContainer;
14
+ startAll: typeof startAll;
14
15
  start: typeof startContainer;
15
16
  stop: typeof stopContainer;
16
17
  ps: typeof getRunningContainers;
17
18
  logs: typeof getLogs;
18
- reload: typeof reload;
19
- [init]: typeof reload;
19
+ [init](): Promise<void>;
20
20
  };
21
21
  export default _default;
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import { init } from '@cloud-cli/cli';
2
2
  import { Resource, SQLiteDriver } from '@cloud-cli/store';
3
3
  import { Container } from './store.js';
4
- import { getRunningContainers, getLogs, startContainer, stopContainer } from './containers.js';
4
+ import { getRunningContainers, getLogs, startContainer, stopContainer, refreshContainer, startAll } from './containers.js';
5
5
  import { addContainer, removeContainer, listContainers, updateContainer, getContainer } from './store.js';
6
6
  import { pull, prune } from './images.js';
7
- async function reload() {
7
+ async function initResource() {
8
8
  Resource.use(new SQLiteDriver());
9
9
  await Resource.create(Container);
10
10
  }
@@ -15,11 +15,14 @@ export default {
15
15
  remove: removeContainer,
16
16
  get: getContainer,
17
17
  list: listContainers,
18
+ refresh: refreshContainer,
18
19
  update: updateContainer,
20
+ startAll,
19
21
  start: startContainer,
20
22
  stop: stopContainer,
21
23
  ps: getRunningContainers,
22
24
  logs: getLogs,
23
- reload,
24
- [init]: reload
25
+ async [init]() {
26
+ await initResource();
27
+ }
25
28
  };
@@ -0,0 +1,13 @@
1
+ export type EnvList = Array<{
2
+ key: string;
3
+ value: string;
4
+ }>;
5
+ export declare function getPorts(ports: string[]): string[];
6
+ export declare function getEnvVars(vars: EnvList): Promise<{
7
+ env: {
8
+ [x: string]: string;
9
+ };
10
+ envKeys: string[];
11
+ }>;
12
+ export declare function getListFromString(string: string): string[];
13
+ export declare function addExecFlag(string: string, flag: string): string[];
package/dist/utils.js ADDED
@@ -0,0 +1,28 @@
1
+ import getPort from 'get-port';
2
+ export function getPorts(ports) {
3
+ const map = {};
4
+ ports.filter(Boolean).forEach((str) => {
5
+ const [left, right] = str.split(':');
6
+ map[left] = right;
7
+ });
8
+ return Object.entries(map).map(([left, right]) => '-p' + left + ':' + right);
9
+ }
10
+ export async function getEnvVars(vars) {
11
+ const port = await getPort();
12
+ const envKeys = [];
13
+ const env = { ...process.env };
14
+ vars.concat({ key: 'PORT', value: String(port) }).forEach(({ key, value }) => {
15
+ envKeys.push(key);
16
+ env[key] = String(value);
17
+ });
18
+ return { env, envKeys: [...new Set(envKeys)] };
19
+ }
20
+ export function getListFromString(string) {
21
+ return string.trim().split('\n').filter(Boolean);
22
+ }
23
+ export function addExecFlag(string, flag) {
24
+ return string
25
+ .split(',')
26
+ .filter(Boolean)
27
+ .map((p) => `-${flag}${p}`);
28
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud-cli/dx",
3
- "version": "1.3.2",
3
+ "version": "1.5.0",
4
4
  "description": "Docker container management",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",