@cloud-cli/dx 1.2.1 → 1.3.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
@@ -13,9 +13,10 @@ npm i @cloud-cli/dx
13
13
  ```bash
14
14
  cy dx.list
15
15
  cy dx.status
16
- cy dx.add --name node-app --image node --version latest --command 'node --version' --volumes 'volume:/container/path' --ports '1234:80'
16
+ 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'
17
19
  cy dx.start --name node-app --env 'FOO=bar' --ports '1234:80'
18
- cy dx.status --name node-app
19
20
  cy dx.logs --name node-app
20
21
  cy dx.stop --name node-app
21
22
  cy dx.remove --name node-app
@@ -9,7 +9,7 @@ interface RunOptions {
9
9
  env?: string[] | string;
10
10
  ports?: string;
11
11
  }
12
- export declare function startContainer(options: RunOptions): Promise<boolean>;
12
+ export declare function startContainer(options: RunOptions, { run }: any): Promise<boolean>;
13
13
  export interface StopOptions {
14
14
  name: string;
15
15
  }
@@ -1,5 +1,6 @@
1
1
  import { exec } from '@cloud-cli/exec';
2
2
  import { findContainer } from './store.js';
3
+ import getPort from 'get-port';
3
4
  export async function getRunningContainers() {
4
5
  const ps = await exec('docker', ['ps', '--format', '{{.Names}}']);
5
6
  if (!ps.ok) {
@@ -22,23 +23,21 @@ export async function getLogs({ name, lines }) {
22
23
  return '';
23
24
  }
24
25
  const defaultRunOptions = { ports: '', env: [] };
25
- export async function startContainer(options) {
26
+ export async function startContainer(options, { run }) {
26
27
  options = { ...defaultRunOptions, ...options };
27
28
  const { name } = options;
28
29
  if (!name) {
29
30
  throw new Error('Name is required');
30
31
  }
31
- const env = [];
32
- if (typeof options.env === 'string') {
33
- options.env = [options.env];
34
- }
35
- if (Array.isArray(options.env)) {
36
- env.push(...options.env.flatMap((e) => ['-e', e]));
37
- }
32
+ const vars = (await run('env.show', { app: name }));
33
+ const { env, envKeys } = await getEnvVars(options.env, vars);
38
34
  const container = await findContainer(name);
39
35
  const volumes = addExecFlag(container.volumes, 'v');
40
- const ports = addExecFlag(container.ports, 'p');
41
- const extraPorts = addExecFlag(options.ports, 'p');
36
+ const ports = getPorts([...container.ports.split(','), ...options.ports.split(','), env.PORT + ':' + env.PORT]);
37
+ if (container.host) {
38
+ await run('px.remove', { domain: container.host });
39
+ await run('px.add', { domain: container.host, target: 'http://localhost:' + env.PORT, cors: true, redirect: true });
40
+ }
42
41
  await exec('docker', [
43
42
  'run',
44
43
  '--detach',
@@ -48,12 +47,37 @@ export async function startContainer(options) {
48
47
  name,
49
48
  ...volumes,
50
49
  ...ports,
51
- ...extraPorts,
52
- ...env,
50
+ ...envKeys.map((e) => '-e' + e),
53
51
  container.image,
54
- ]);
52
+ ], { env });
55
53
  return true;
56
54
  }
55
+ function getPorts(ports) {
56
+ const map = {};
57
+ ports.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
+ });
72
+ }
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)] };
80
+ }
57
81
  export async function stopContainer(options) {
58
82
  if (!options.name) {
59
83
  throw new Error('Name is required');
package/dist/images.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export declare function pull(options: {
2
2
  image: string;
3
3
  }): Promise<boolean>;
4
+ export declare function prune(): Promise<boolean>;
package/dist/images.js CHANGED
@@ -2,3 +2,6 @@ import { exec } from '@cloud-cli/exec';
2
2
  export async function pull(options) {
3
3
  return (await exec('docker', ['pull', options.image])).ok;
4
4
  }
5
+ export async function prune() {
6
+ return (await exec('docker', ['image', 'prune', '-f'])).ok;
7
+ }
package/dist/index.d.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  import { init } from '@cloud-cli/cli';
2
2
  import { getRunningContainers, getLogs, startContainer, stopContainer } from './containers.js';
3
3
  import { addContainer, removeContainer, listContainers, updateContainer, getContainer } from './store.js';
4
- import { pull } from './images.js';
4
+ import { pull, prune } from './images.js';
5
5
  declare function reload(): Promise<void>;
6
6
  declare const _default: {
7
7
  pull: typeof pull;
8
+ prune: typeof prune;
8
9
  add: typeof addContainer;
9
10
  remove: typeof removeContainer;
10
11
  get: typeof getContainer;
package/dist/index.js CHANGED
@@ -3,13 +3,14 @@ import { Resource, SQLiteDriver } from '@cloud-cli/store';
3
3
  import { Container } from './store.js';
4
4
  import { getRunningContainers, getLogs, startContainer, stopContainer } from './containers.js';
5
5
  import { addContainer, removeContainer, listContainers, updateContainer, getContainer } from './store.js';
6
- import { pull } from './images.js';
6
+ import { pull, prune } from './images.js';
7
7
  async function reload() {
8
8
  Resource.use(new SQLiteDriver());
9
9
  await Resource.create(Container);
10
10
  }
11
11
  export default {
12
12
  pull,
13
+ prune,
13
14
  add: addContainer,
14
15
  remove: removeContainer,
15
16
  get: getContainer,
package/dist/store.d.ts CHANGED
@@ -3,6 +3,7 @@ export declare class Container extends Resource {
3
3
  id: number;
4
4
  name: string;
5
5
  image: string;
6
+ host: string;
6
7
  ports: string;
7
8
  volumes: string;
8
9
  }
@@ -11,6 +12,7 @@ interface ContainerName {
11
12
  }
12
13
  interface AddContainerOptions extends ContainerName {
13
14
  image: string;
15
+ host?: string;
14
16
  volumes?: string;
15
17
  ports?: string;
16
18
  }
@@ -21,6 +23,7 @@ interface UpdateOptions extends ContainerName {
21
23
  ports?: string;
22
24
  volumes?: string;
23
25
  image?: string;
26
+ host?: string;
24
27
  }
25
28
  export declare function updateContainer(options: UpdateOptions): Promise<Container>;
26
29
  export declare function listContainers(): Promise<Container[]>;
package/dist/store.js CHANGED
@@ -20,6 +20,10 @@ __decorate([
20
20
  NotNull(),
21
21
  Property(String)
22
22
  ], Container.prototype, "image", void 0);
23
+ __decorate([
24
+ NotNull(),
25
+ Property(String)
26
+ ], Container.prototype, "host", void 0);
23
27
  __decorate([
24
28
  NotNull(),
25
29
  Property(String)
@@ -40,7 +44,8 @@ export async function addContainer(options) {
40
44
  throw new Error('Image required');
41
45
  const volumes = (options.volumes ? sanitiseVolumes(options.volumes) : '');
42
46
  const ports = (options.ports ? sanitisePorts(options.ports) : '');
43
- const container = new Container({ name, image, volumes, ports });
47
+ const host = options.host;
48
+ const container = new Container({ name, image, volumes, ports, host });
44
49
  const id = await container.save();
45
50
  container.id = id;
46
51
  return container;
@@ -58,7 +63,7 @@ export async function getContainer(options) {
58
63
  }
59
64
  const optionSplitter = /,\s*/;
60
65
  export async function updateContainer(options) {
61
- let { ports, volumes, name, image } = options;
66
+ let { ports, volumes, name, image, host } = options;
62
67
  const container = await findContainer(name);
63
68
  if (!container) {
64
69
  throw new Error('Container not found');
@@ -72,6 +77,9 @@ export async function updateContainer(options) {
72
77
  if (image) {
73
78
  container.image = image;
74
79
  }
80
+ if (host) {
81
+ container.host = host;
82
+ }
75
83
  await container.save();
76
84
  return container;
77
85
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud-cli/dx",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "description": "Docker container management",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -32,6 +32,7 @@
32
32
  "dependencies": {
33
33
  "@cloud-cli/cli": "^1.4.4",
34
34
  "@cloud-cli/exec": "^1.0.4",
35
- "@cloud-cli/store": "^1.5.1"
35
+ "@cloud-cli/store": "^1.5.1",
36
+ "get-port": "^6.1.2"
36
37
  }
37
38
  }