@cloud-cli/dx 1.4.0 → 1.5.1
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/dist/containers.d.ts +7 -8
- package/dist/containers.js +25 -36
- package/dist/index.d.ts +3 -4
- package/dist/index.js +6 -4
- package/dist/store.js +2 -1
- package/dist/utils.d.ts +13 -0
- package/dist/utils.js +28 -0
- package/package.json +1 -1
package/dist/containers.d.ts
CHANGED
|
@@ -1,16 +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
|
-
|
|
7
|
-
interface RunOptions {
|
|
8
|
-
name: string;
|
|
9
|
-
}
|
|
10
|
-
export declare function refreshContainer(options: RunOptions, { run }: any): Promise<void>;
|
|
11
|
-
export declare function startContainer(options: RunOptions, { run }: any): Promise<boolean>;
|
|
12
|
-
export interface StopOptions {
|
|
7
|
+
interface ContainerName {
|
|
13
8
|
name: string;
|
|
14
9
|
}
|
|
15
|
-
export declare function
|
|
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>;
|
|
16
15
|
export {};
|
package/dist/containers.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { exec } from '@cloud-cli/exec';
|
|
2
|
-
import { findContainer } from './store.js';
|
|
3
|
-
import
|
|
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) {
|
|
7
7
|
throw new Error('Failed to list containers: ' + ps.stderr);
|
|
8
8
|
}
|
|
9
|
-
return getListFromString(ps.stdout);
|
|
9
|
+
return getListFromString(ps.stdout).sort();
|
|
10
10
|
}
|
|
11
11
|
export async function getLogs({ name, lines }) {
|
|
12
12
|
if (!name) {
|
|
@@ -22,12 +22,24 @@ export async function getLogs({ name, lines }) {
|
|
|
22
22
|
}
|
|
23
23
|
return '';
|
|
24
24
|
}
|
|
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
|
+
try {
|
|
31
|
+
await refreshContainer({ name: app.name }, cli);
|
|
32
|
+
}
|
|
33
|
+
catch { }
|
|
34
|
+
}
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
25
37
|
export async function refreshContainer(options, { run }) {
|
|
26
38
|
const { name } = options;
|
|
27
39
|
const container = await findContainer(name);
|
|
28
|
-
await run('dx.stop', { name });
|
|
29
40
|
await run('dx.pull', { image: container.image });
|
|
30
|
-
await run('dx.
|
|
41
|
+
await run('dx.stop', { name });
|
|
42
|
+
await run('dx.prune', {});
|
|
31
43
|
await run('dx.start', { name });
|
|
32
44
|
}
|
|
33
45
|
export async function startContainer(options, { run }) {
|
|
@@ -44,9 +56,9 @@ export async function startContainer(options, { run }) {
|
|
|
44
56
|
await run('px.remove', { domain: container.host });
|
|
45
57
|
await run('px.add', { domain: container.host, target: 'http://localhost:' + env.PORT, cors: true, redirect: true });
|
|
46
58
|
await run('dns.add', { domain: container.host });
|
|
47
|
-
await run('dns.reload');
|
|
59
|
+
await run('dns.reload', {});
|
|
48
60
|
}
|
|
49
|
-
|
|
61
|
+
const execArgs = [
|
|
50
62
|
'run',
|
|
51
63
|
'--detach',
|
|
52
64
|
'--restart',
|
|
@@ -57,27 +69,13 @@ export async function startContainer(options, { run }) {
|
|
|
57
69
|
...ports,
|
|
58
70
|
...envKeys.map((e) => '-e' + e),
|
|
59
71
|
container.image,
|
|
60
|
-
]
|
|
72
|
+
];
|
|
73
|
+
const output = await exec('docker', execArgs, { env });
|
|
74
|
+
if (!output.ok) {
|
|
75
|
+
throw new Error('Failed to start ' + name);
|
|
76
|
+
}
|
|
61
77
|
return true;
|
|
62
78
|
}
|
|
63
|
-
function getPorts(ports) {
|
|
64
|
-
const map = {};
|
|
65
|
-
ports.filter(Boolean).forEach((str) => {
|
|
66
|
-
const [left, right] = str.split(':');
|
|
67
|
-
map[left] = right;
|
|
68
|
-
});
|
|
69
|
-
return Object.entries(map).map(([left, right]) => '-p' + left + ':' + right);
|
|
70
|
-
}
|
|
71
|
-
async function getEnvVars(vars) {
|
|
72
|
-
const port = await getPort();
|
|
73
|
-
const envKeys = [];
|
|
74
|
-
const env = { ...process.env };
|
|
75
|
-
vars.concat({ key: 'PORT', value: String(port) }).forEach(({ key, value }) => {
|
|
76
|
-
envKeys.push(key);
|
|
77
|
-
env[key] = String(value);
|
|
78
|
-
});
|
|
79
|
-
return { env, envKeys: [...new Set(envKeys)] };
|
|
80
|
-
}
|
|
81
79
|
export async function stopContainer(options, { run }) {
|
|
82
80
|
let { name } = options;
|
|
83
81
|
if (!name) {
|
|
@@ -87,15 +85,6 @@ export async function stopContainer(options, { run }) {
|
|
|
87
85
|
await exec('docker', ['stop', '-t', '5', name]);
|
|
88
86
|
await exec('docker', ['rm', name]);
|
|
89
87
|
await run('dns.remove', { domain: container.host });
|
|
90
|
-
await run('dns.reload');
|
|
88
|
+
await run('dns.reload', {});
|
|
91
89
|
return true;
|
|
92
90
|
}
|
|
93
|
-
function getListFromString(string) {
|
|
94
|
-
return string.trim().split('\n').filter(Boolean);
|
|
95
|
-
}
|
|
96
|
-
function addExecFlag(string, flag) {
|
|
97
|
-
return string
|
|
98
|
-
.split(',')
|
|
99
|
-
.filter(Boolean)
|
|
100
|
-
.map((p) => `-${flag}${p}`);
|
|
101
|
-
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { init } from '@cloud-cli/cli';
|
|
2
|
-
import { getRunningContainers, getLogs, startContainer, stopContainer, refreshContainer } 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;
|
|
@@ -12,11 +11,11 @@ declare const _default: {
|
|
|
12
11
|
list: typeof listContainers;
|
|
13
12
|
refresh: typeof refreshContainer;
|
|
14
13
|
update: typeof updateContainer;
|
|
14
|
+
startAll: typeof startAll;
|
|
15
15
|
start: typeof startContainer;
|
|
16
16
|
stop: typeof stopContainer;
|
|
17
17
|
ps: typeof getRunningContainers;
|
|
18
18
|
logs: typeof getLogs;
|
|
19
|
-
|
|
20
|
-
[init]: typeof reload;
|
|
19
|
+
[init](): Promise<void>;
|
|
21
20
|
};
|
|
22
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, refreshContainer } 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
|
|
7
|
+
async function initResource() {
|
|
8
8
|
Resource.use(new SQLiteDriver());
|
|
9
9
|
await Resource.create(Container);
|
|
10
10
|
}
|
|
@@ -17,10 +17,12 @@ export default {
|
|
|
17
17
|
list: listContainers,
|
|
18
18
|
refresh: refreshContainer,
|
|
19
19
|
update: updateContainer,
|
|
20
|
+
startAll,
|
|
20
21
|
start: startContainer,
|
|
21
22
|
stop: stopContainer,
|
|
22
23
|
ps: getRunningContainers,
|
|
23
24
|
logs: getLogs,
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
async [init]() {
|
|
26
|
+
await initResource();
|
|
27
|
+
}
|
|
26
28
|
};
|
package/dist/store.js
CHANGED
|
@@ -84,7 +84,8 @@ export async function updateContainer(options) {
|
|
|
84
84
|
return container;
|
|
85
85
|
}
|
|
86
86
|
export async function listContainers() {
|
|
87
|
-
|
|
87
|
+
const list = await Resource.find(Container, new Query());
|
|
88
|
+
return list.sort((a, b) => Number(a.name > b.name) || -1);
|
|
88
89
|
}
|
|
89
90
|
export async function findContainer(name) {
|
|
90
91
|
const found = await Resource.find(Container, new Query().where('name').is(name));
|
package/dist/utils.d.ts
ADDED
|
@@ -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
|
+
}
|