@platformatic/control 1.24.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/LICENSE +201 -0
- package/NOTICE +13 -0
- package/README.md +13 -0
- package/control.js +83 -0
- package/help/config.txt +16 -0
- package/help/env.txt +16 -0
- package/help/help.txt +11 -0
- package/help/inject.txt +29 -0
- package/help/logs.txt +21 -0
- package/help/ps.txt +10 -0
- package/help/reload.txt +19 -0
- package/help/restart.txt +16 -0
- package/help/services.txt +16 -0
- package/help/stop.txt +16 -0
- package/index.js +7 -0
- package/lib/config.js +29 -0
- package/lib/env.js +29 -0
- package/lib/errors.js +21 -0
- package/lib/inject.js +72 -0
- package/lib/logs.js +46 -0
- package/lib/ps.js +96 -0
- package/lib/reload.js +25 -0
- package/lib/restart.js +32 -0
- package/lib/runtime-api-client.js +295 -0
- package/lib/services.js +73 -0
- package/lib/stop.js +25 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2022 Platformatic
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @platformatic/control
|
|
2
|
+
|
|
3
|
+
Check out the full documentation for Platformatic Runtime on [our website](https://docs.platformatic.dev/docs/guides/runtime-cli-managing).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @platformatic/control
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## License
|
|
12
|
+
|
|
13
|
+
Apache 2.0
|
package/control.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { join } = require('node:path')
|
|
4
|
+
const { parseArgs } = require('node:util')
|
|
5
|
+
const commist = require('commist')
|
|
6
|
+
const helpMe = require('help-me')
|
|
7
|
+
|
|
8
|
+
const getRuntimesCommand = require('./lib/ps')
|
|
9
|
+
const getRuntimesEnvCommand = require('./lib/env')
|
|
10
|
+
const getRuntimeServicesCommand = require('./lib/services')
|
|
11
|
+
const getRuntimeConfigCommand = require('./lib/config')
|
|
12
|
+
const stopRuntimeCommand = require('./lib/stop')
|
|
13
|
+
const reloadRuntimeCommand = require('./lib/reload')
|
|
14
|
+
const restartRuntimeCommand = require('./lib/restart')
|
|
15
|
+
const injectRuntimeCommand = require('./lib/inject')
|
|
16
|
+
const streamRuntimeLogsCommand = require('./lib/logs')
|
|
17
|
+
|
|
18
|
+
const help = helpMe({
|
|
19
|
+
dir: join(__dirname, 'help'),
|
|
20
|
+
ext: '.txt'
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
const program = commist({ maxDistance: 2 })
|
|
24
|
+
|
|
25
|
+
program.register('ps', wrapCommand(getRuntimesCommand))
|
|
26
|
+
program.register('stop', wrapCommand(stopRuntimeCommand))
|
|
27
|
+
program.register('reload', wrapCommand(reloadRuntimeCommand))
|
|
28
|
+
program.register('restart', wrapCommand(restartRuntimeCommand))
|
|
29
|
+
program.register('logs', wrapCommand(streamRuntimeLogsCommand))
|
|
30
|
+
program.register('env', wrapCommand(getRuntimesEnvCommand))
|
|
31
|
+
program.register('services', wrapCommand(getRuntimeServicesCommand))
|
|
32
|
+
program.register('config', wrapCommand(getRuntimeConfigCommand))
|
|
33
|
+
program.register('inject', wrapCommand(injectRuntimeCommand))
|
|
34
|
+
program.register('help', help.toStdout)
|
|
35
|
+
|
|
36
|
+
async function runControl (argv) {
|
|
37
|
+
if (argv.length === 0) {
|
|
38
|
+
help.toStdout()
|
|
39
|
+
return {}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const args = parseArgs({
|
|
43
|
+
args: argv,
|
|
44
|
+
options: {
|
|
45
|
+
version: { type: 'boolean', short: 'v' }
|
|
46
|
+
},
|
|
47
|
+
strict: false
|
|
48
|
+
}).values
|
|
49
|
+
|
|
50
|
+
if (args.version) {
|
|
51
|
+
const packageJson = require(join(__dirname, 'package.json'))
|
|
52
|
+
console.log(packageJson.version)
|
|
53
|
+
process.exit(0)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const output = await program.parseAsync(argv)
|
|
57
|
+
return { output }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (require.main === module) {
|
|
61
|
+
runControl(process.argv.slice(2))
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function wrapCommand (fn) {
|
|
65
|
+
return async function (...args) {
|
|
66
|
+
try {
|
|
67
|
+
return await fn(...args)
|
|
68
|
+
} catch (err) {
|
|
69
|
+
console.log(err.message)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
module.exports = {
|
|
75
|
+
runControl,
|
|
76
|
+
getRuntimesCommand,
|
|
77
|
+
getRuntimesEnvCommand,
|
|
78
|
+
getRuntimeServicesCommand,
|
|
79
|
+
stopRuntimeCommand,
|
|
80
|
+
restartRuntimeCommand,
|
|
81
|
+
injectRuntimeCommand,
|
|
82
|
+
streamRuntimeLogsCommand
|
|
83
|
+
}
|
package/help/config.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Prints runtime or runtime service config file
|
|
2
|
+
|
|
3
|
+
``` bash
|
|
4
|
+
$ platformatic ctl config -n runtime-name
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
Options:
|
|
8
|
+
|
|
9
|
+
* `-p, --pid <number>` - The process id of the runtime.
|
|
10
|
+
* `-n, --name <string>` - The name of the runtime.
|
|
11
|
+
|
|
12
|
+
The `config` command uses the Platformatic Runtime Management API. To enable it
|
|
13
|
+
set the `managementApi` option to `true` in the runtime configuration file.
|
|
14
|
+
|
|
15
|
+
To get the list of runtimes with enabled management API use the
|
|
16
|
+
`platformatic ctl ps` command.
|
package/help/env.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Lists platformatic runtime application environment variables
|
|
2
|
+
|
|
3
|
+
``` bash
|
|
4
|
+
$ platformatic ctl env -n runtime-name
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
Options:
|
|
8
|
+
|
|
9
|
+
* `-p, --pid <number>` - The process id of the runtime.
|
|
10
|
+
* `-n, --name <string>` - The name of the runtime.
|
|
11
|
+
|
|
12
|
+
The `env` command uses the Platformatic Runtime Management API. To enable it
|
|
13
|
+
set the `managementApi` option to `true` in the runtime configuration file.
|
|
14
|
+
|
|
15
|
+
To get the list of runtimes with enabled management API use the
|
|
16
|
+
`platformatic ctl ps` command.
|
package/help/help.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Available commands:
|
|
2
|
+
|
|
3
|
+
* `ps` - lists all platformatic runtime applications.
|
|
4
|
+
* `stop` - stops a platformatic runtime application.
|
|
5
|
+
* `restart` - restarts all platformatic runtime services.
|
|
6
|
+
* `reload` - reloads all platformatic runtime services.
|
|
7
|
+
* `services` - lists the runtime services.
|
|
8
|
+
* `config` - prints runtime or runtime service config file.
|
|
9
|
+
* `env` - lists the runtime environment variables.
|
|
10
|
+
* `logs` - shows the runtime logs.
|
|
11
|
+
* `inject` - injects a request to the runtime service.
|
package/help/inject.txt
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Injects a request to the platformatic runtime service.
|
|
2
|
+
|
|
3
|
+
``` bash
|
|
4
|
+
$ platformatic ctl inject -n runtime-name /hello
|
|
5
|
+
-X POST
|
|
6
|
+
-H "Content-Type: application/json"
|
|
7
|
+
-d '{"key": "value"}'
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Options:
|
|
11
|
+
|
|
12
|
+
* `-p, --pid <number>` - The process id of the runtime.
|
|
13
|
+
* `-n, --name <string>` - The name of the runtime.
|
|
14
|
+
* `-s, --service <string>` - The name of the runtime service.
|
|
15
|
+
* `-X, --request <string>` - The request HTTP method. Default is `GET`.
|
|
16
|
+
* `-H, --header <string>` - The request header. Can be used multiple times.
|
|
17
|
+
* `-d, --data <string>` - The request data.
|
|
18
|
+
* `-i, --include <boolean>` - Include the response headers in the output. Default is `false`.
|
|
19
|
+
* `-o, --output <file>` - Write the response to the specified file.
|
|
20
|
+
|
|
21
|
+
The `inject` command sends a request to the runtime service and prints the
|
|
22
|
+
response to the standard output. If the `--service` option is not specified the
|
|
23
|
+
request is sent to the runtime entrypoint.
|
|
24
|
+
|
|
25
|
+
The `inject` command uses the Platformatic Runtime Management API. To enable it
|
|
26
|
+
set the `managementApi` option to `true` in the runtime configuration file.
|
|
27
|
+
|
|
28
|
+
To get the list of runtimes with enabled management API use the
|
|
29
|
+
`platformatic ctl ps` command.
|
package/help/logs.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Streams logs from the platformatic runtime application.
|
|
2
|
+
|
|
3
|
+
``` bash
|
|
4
|
+
$ platformatic ctl logs -n runtime-name
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
Options:
|
|
8
|
+
|
|
9
|
+
* `-p, --pid <number>` - The process id of the runtime.
|
|
10
|
+
* `-n, --name <string>` - The name of the runtime.
|
|
11
|
+
* `-l, --level <string>` - The pino log level to stream. Default is `info`.
|
|
12
|
+
* `-s, --service <string>` - The name of the service to stream logs from.
|
|
13
|
+
* `--pretty` <boolean> - Pretty print the logs. Default is `true`.
|
|
14
|
+
|
|
15
|
+
If `--service` is not specified, the command will stream logs from all services.
|
|
16
|
+
|
|
17
|
+
The `logs` command uses the Platformatic Runtime Management API. To enable it
|
|
18
|
+
set the `managementApi` option to `true` in the runtime configuration file.
|
|
19
|
+
|
|
20
|
+
To get the list of runtimes with enabled management API use the
|
|
21
|
+
`platformatic ctl ps` command.
|
package/help/ps.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Lists all running platformatic runtime applications.
|
|
2
|
+
|
|
3
|
+
``` bash
|
|
4
|
+
$ platformatic ctl ps
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
To see the list of all available control commands, run `platformatic ctl help`.
|
|
8
|
+
|
|
9
|
+
The `ps` command uses the Platformatic Runtime Management API. To enable it
|
|
10
|
+
set the `managementApi` option to `true` in the runtime configuration file.
|
package/help/reload.txt
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Reloads a platformatic runtime application.
|
|
2
|
+
|
|
3
|
+
``` bash
|
|
4
|
+
$ platformatic ctl reload -n runtime-name
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
Options:
|
|
8
|
+
|
|
9
|
+
* `-p, --pid <number>` - The process id of the runtime to reload.
|
|
10
|
+
* `-n, --name <string>` - The name of the runtime to reload.
|
|
11
|
+
|
|
12
|
+
The difference between `reload` and `restart` is that `reload` does not kill
|
|
13
|
+
the runtime process. It stops and starts all the runtime services.
|
|
14
|
+
|
|
15
|
+
The `reload` command uses the Platformatic Runtime Management API. To enable it
|
|
16
|
+
set the `managementApi` option to `true` in the runtime configuration file.
|
|
17
|
+
|
|
18
|
+
To get the list of runtimes with enabled management API use the
|
|
19
|
+
`platformatic ctl ps` command.
|
package/help/restart.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Restarts a platformatic runtime application.
|
|
2
|
+
|
|
3
|
+
``` bash
|
|
4
|
+
$ platformatic ctl restart -n runtime-name
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
Options:
|
|
8
|
+
|
|
9
|
+
* `-p, --pid <number>` - The process id of the runtime to restart.
|
|
10
|
+
* `-n, --name <string>` - The name of the runtime to restart.
|
|
11
|
+
|
|
12
|
+
The `restart` command uses the Platformatic Runtime Management API. To enable it
|
|
13
|
+
set the `managementApi` option to `true` in the runtime configuration file.
|
|
14
|
+
|
|
15
|
+
To get the list of runtimes with enabled management API use the
|
|
16
|
+
`platformatic ctl ps` command.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Lists the platformatic runtime services.
|
|
2
|
+
|
|
3
|
+
``` bash
|
|
4
|
+
$ platformatic ctl services -n runtime-name
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
Options:
|
|
8
|
+
|
|
9
|
+
* `-p, --pid <number>` - The process id of the runtime.
|
|
10
|
+
* `-n, --name <string>` - The name of the runtime.
|
|
11
|
+
|
|
12
|
+
The `services` command uses the Platformatic Runtime Management API. To enable it
|
|
13
|
+
set the `managementApi` option to `true` in the runtime configuration file.
|
|
14
|
+
|
|
15
|
+
To get the list of runtimes with enabled management API use the
|
|
16
|
+
`platformatic ctl ps` command.
|
package/help/stop.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Stops a platformatic runtime application.
|
|
2
|
+
|
|
3
|
+
``` bash
|
|
4
|
+
$ platformatic ctl stop -n runtime-name
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
Options:
|
|
8
|
+
|
|
9
|
+
* `-p, --pid <number>` - The process id of the runtime to stop.
|
|
10
|
+
* `-n, --name <string>` - The name of the runtime to stop.
|
|
11
|
+
|
|
12
|
+
The `stop` command uses the Platformatic Runtime Management API. To enable it
|
|
13
|
+
set the `managementApi` option to `true` in the runtime configuration file.
|
|
14
|
+
|
|
15
|
+
To get the list of runtimes with enabled management API use the
|
|
16
|
+
`platformatic ctl ps` command.
|
package/index.js
ADDED
package/lib/config.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { parseArgs } = require('node:util')
|
|
4
|
+
const RuntimeApiClient = require('./runtime-api-client')
|
|
5
|
+
|
|
6
|
+
async function getRuntimeConfigCommand (argv) {
|
|
7
|
+
const args = parseArgs({
|
|
8
|
+
args: argv,
|
|
9
|
+
options: {
|
|
10
|
+
pid: { type: 'string', short: 'p' },
|
|
11
|
+
name: { type: 'string', short: 'n' },
|
|
12
|
+
service: { type: 'string', short: 's' }
|
|
13
|
+
},
|
|
14
|
+
strict: false
|
|
15
|
+
}).values
|
|
16
|
+
|
|
17
|
+
const client = new RuntimeApiClient()
|
|
18
|
+
const runtime = await client.getMatchingRuntime(args)
|
|
19
|
+
|
|
20
|
+
const config = args.service
|
|
21
|
+
? await client.getRuntimeServiceConfig(runtime.pid, args.service)
|
|
22
|
+
: await client.getRuntimeConfig(runtime.pid)
|
|
23
|
+
|
|
24
|
+
console.log(JSON.stringify(config, null, 2))
|
|
25
|
+
|
|
26
|
+
await client.close()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports = getRuntimeConfigCommand
|
package/lib/env.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { parseArgs } = require('node:util')
|
|
4
|
+
const RuntimeApiClient = require('./runtime-api-client')
|
|
5
|
+
|
|
6
|
+
async function getRuntimeEnvCommand (argv) {
|
|
7
|
+
const args = parseArgs({
|
|
8
|
+
args: argv,
|
|
9
|
+
options: {
|
|
10
|
+
pid: { type: 'string', short: 'p' },
|
|
11
|
+
name: { type: 'string', short: 'n' }
|
|
12
|
+
},
|
|
13
|
+
strict: false
|
|
14
|
+
}).values
|
|
15
|
+
|
|
16
|
+
const client = new RuntimeApiClient()
|
|
17
|
+
const runtime = await client.getMatchingRuntime(args)
|
|
18
|
+
|
|
19
|
+
const runtimeEnv = await client.getRuntimeEnv(runtime.pid)
|
|
20
|
+
let output = ''
|
|
21
|
+
for (const key in runtimeEnv) {
|
|
22
|
+
output += `${key}=${runtimeEnv[key]}\n`
|
|
23
|
+
}
|
|
24
|
+
console.log(output)
|
|
25
|
+
|
|
26
|
+
await client.close()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports = getRuntimeEnvCommand
|
package/lib/errors.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const createError = require('@fastify/error')
|
|
4
|
+
|
|
5
|
+
const ERROR_PREFIX = 'PLT_CTR'
|
|
6
|
+
|
|
7
|
+
module.exports = {
|
|
8
|
+
RuntimeNotFound: createError(`${ERROR_PREFIX}_RUNTIME_NOT_FOUND`, 'Runtime not found.'),
|
|
9
|
+
MissingRuntimeIdentifier: createError(`${ERROR_PREFIX}_MISSING_RUNTIME_IDENTIFIER`, 'Runtime name or PID is required.'),
|
|
10
|
+
MissingRequestURL: createError(`${ERROR_PREFIX}_MISSING_REQUEST_URL`, 'Request URL is required.'),
|
|
11
|
+
FailedToGetRuntimeMetadata: createError(`${ERROR_PREFIX}_FAILED_TO_GET_RUNTIME_METADATA`, 'Failed to get runtime metadata %s.'),
|
|
12
|
+
FailedToGetRuntimeServices: createError(`${ERROR_PREFIX}_FAILED_TO_GET_RUNTIME_SERVICES`, 'Failed to get runtime services %s.'),
|
|
13
|
+
FailedToGetRuntimeEnv: createError(`${ERROR_PREFIX}_FAILED_TO_GET_RUNTIME_ENV`, 'Failed to get runtime environment variables %s.'),
|
|
14
|
+
FailedToInjectRuntime: createError(`${ERROR_PREFIX}_FAILED_TO_INJECT_RUNTIME`, 'Failed to inject runtime %s.'),
|
|
15
|
+
FailedToStreamRuntimeLogs: createError(`${ERROR_PREFIX}_FAILED_TO_STREAM_RUNTIME_LOGS`, 'Failed to stream runtime logs %s.'),
|
|
16
|
+
FailedToRestartRuntime: createError(`${ERROR_PREFIX}_FAILED_TO_RESTART_RUNTIME`, 'Failed to restart runtime %s.'),
|
|
17
|
+
FailedToStopRuntime: createError(`${ERROR_PREFIX}_FAILED_TO_STOP_RUNTIME`, 'Failed to stop the runtime %s.'),
|
|
18
|
+
FailedToReloadRuntime: createError(`${ERROR_PREFIX}_FAILED_TO_RELOAD_RUNTIME`, 'Failed to reload the runtime %s.'),
|
|
19
|
+
FailedToGetRuntimeConfig: createError(`${ERROR_PREFIX}_FAILED_TO_GET_RUNTIME_CONFIG`, 'Failed to get runtime config %s.'),
|
|
20
|
+
FailedToGetRuntimeServiceConfig: createError(`${ERROR_PREFIX}_FAILED_TO_GET_RUNTIME_SERVICE_CONFIG`, 'Failed to get runtime service config %s.')
|
|
21
|
+
}
|
package/lib/inject.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { parseArgs } = require('node:util')
|
|
4
|
+
const { writeFile } = require('node:fs/promises')
|
|
5
|
+
const RuntimeApiClient = require('./runtime-api-client')
|
|
6
|
+
const errors = require('./errors')
|
|
7
|
+
|
|
8
|
+
async function injectRuntimeCommand (argv) {
|
|
9
|
+
const { values: args, positionals } = parseArgs({
|
|
10
|
+
args: argv,
|
|
11
|
+
options: {
|
|
12
|
+
pid: { type: 'string', short: 'p' },
|
|
13
|
+
name: { type: 'string', short: 'n' },
|
|
14
|
+
service: { type: 'string', short: 's' },
|
|
15
|
+
request: { type: 'string', short: 'X', default: 'GET' },
|
|
16
|
+
header: { type: 'string', short: 'H', multiple: true },
|
|
17
|
+
data: { type: 'string', short: 'd' },
|
|
18
|
+
include: { type: 'boolean', short: 'i', default: false },
|
|
19
|
+
output: { type: 'string', short: 'o' }
|
|
20
|
+
},
|
|
21
|
+
strict: false
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
const client = new RuntimeApiClient()
|
|
25
|
+
const runtime = await client.getMatchingRuntime(args)
|
|
26
|
+
|
|
27
|
+
let serviceId = args.service
|
|
28
|
+
if (!serviceId) {
|
|
29
|
+
const runtimeServices = await client.getRuntimeServices(runtime.pid)
|
|
30
|
+
serviceId = runtimeServices.entrypoint
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (positionals.length === 0) {
|
|
34
|
+
throw errors.MissingRequestURL()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const fullUrl = new URL(positionals[0], runtime.url)
|
|
38
|
+
const injectPath = fullUrl.href.slice(runtime.url.length)
|
|
39
|
+
|
|
40
|
+
const method = args.request
|
|
41
|
+
const body = args.data
|
|
42
|
+
|
|
43
|
+
const headers = {}
|
|
44
|
+
for (const header of args.header || []) {
|
|
45
|
+
const [name, value] = header.split(':')
|
|
46
|
+
headers[name] = value
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const injectOptions = { url: injectPath, method, headers, body }
|
|
50
|
+
|
|
51
|
+
const response = await client.injectRuntime(runtime.pid, serviceId, injectOptions)
|
|
52
|
+
|
|
53
|
+
let result = ''
|
|
54
|
+
if (args.include) {
|
|
55
|
+
result += `HTTP/1.1 ${response.statusCode}\n`
|
|
56
|
+
for (const header in response.headers) {
|
|
57
|
+
result += `${header}: ${response.headers[header]}\n`
|
|
58
|
+
}
|
|
59
|
+
result += '\n'
|
|
60
|
+
}
|
|
61
|
+
result += await response.body.text()
|
|
62
|
+
|
|
63
|
+
if (args.output) {
|
|
64
|
+
await writeFile(args.output, result)
|
|
65
|
+
} else {
|
|
66
|
+
console.log(result)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
await client.close()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
module.exports = injectRuntimeCommand
|
package/lib/logs.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { parseArgs } = require('node:util')
|
|
4
|
+
const RuntimeApiClient = require('./runtime-api-client')
|
|
5
|
+
|
|
6
|
+
async function streamRuntimeLogsCommand (argv) {
|
|
7
|
+
const args = parseArgs({
|
|
8
|
+
args: argv,
|
|
9
|
+
options: {
|
|
10
|
+
pid: { type: 'string', short: 'p' },
|
|
11
|
+
name: { type: 'string', short: 'n' },
|
|
12
|
+
level: { type: 'string', short: 'l', default: 'info' },
|
|
13
|
+
pretty: { type: 'boolean', default: true },
|
|
14
|
+
service: { type: 'string', short: 's' }
|
|
15
|
+
},
|
|
16
|
+
strict: false
|
|
17
|
+
}).values
|
|
18
|
+
|
|
19
|
+
const client = new RuntimeApiClient()
|
|
20
|
+
const runtime = await client.getMatchingRuntime(args)
|
|
21
|
+
|
|
22
|
+
const options = {}
|
|
23
|
+
if (args.level !== undefined) {
|
|
24
|
+
options.level = args.level
|
|
25
|
+
}
|
|
26
|
+
if (args.pretty !== undefined) {
|
|
27
|
+
options.pretty = args.pretty
|
|
28
|
+
}
|
|
29
|
+
if (args.service !== undefined) {
|
|
30
|
+
options.serviceId = args.service
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const logsStream = client.getRuntimeLogsStream(runtime.pid, options)
|
|
34
|
+
logsStream.pipe(process.stdout)
|
|
35
|
+
|
|
36
|
+
process.on('SIGINT', () => {
|
|
37
|
+
logsStream.destroy()
|
|
38
|
+
process.exit(0)
|
|
39
|
+
})
|
|
40
|
+
process.on('SIGTERM', () => {
|
|
41
|
+
logsStream.destroy()
|
|
42
|
+
process.exit(0)
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
module.exports = streamRuntimeLogsCommand
|
package/lib/ps.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { table, getBorderCharacters } = require('table')
|
|
4
|
+
const RuntimeApiClient = require('./runtime-api-client')
|
|
5
|
+
|
|
6
|
+
const tableColumns = [
|
|
7
|
+
{
|
|
8
|
+
value: 'pid',
|
|
9
|
+
alias: 'PID'
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
value: 'packageName',
|
|
13
|
+
alias: 'NAME'
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
value: 'platformaticVersion',
|
|
17
|
+
alias: 'PLT'
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
value: 'uptimeSeconds',
|
|
21
|
+
alias: 'TIME',
|
|
22
|
+
formatter: formatRuntimeTime
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
value: 'url',
|
|
26
|
+
alias: 'URL'
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
value: 'projectDir',
|
|
30
|
+
alias: 'PWD'
|
|
31
|
+
}
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
function formatRuntimeTime (timeSeconds) {
|
|
35
|
+
let result = ''
|
|
36
|
+
if (timeSeconds < 3600) {
|
|
37
|
+
const seconds = Math.floor(timeSeconds % 60)
|
|
38
|
+
result = `${seconds}s`
|
|
39
|
+
}
|
|
40
|
+
if (timeSeconds >= 60) {
|
|
41
|
+
const minutes = Math.floor((timeSeconds % 3600) / 60)
|
|
42
|
+
result = `${minutes}m ` + result
|
|
43
|
+
}
|
|
44
|
+
if (timeSeconds >= 3600) {
|
|
45
|
+
const hours = Math.floor((timeSeconds % (3600 * 24)) / 3600)
|
|
46
|
+
result = `${hours}h ` + result
|
|
47
|
+
}
|
|
48
|
+
if (timeSeconds >= 3600 * 24) {
|
|
49
|
+
const days = Math.floor(timeSeconds / (3600 * 24))
|
|
50
|
+
result = `${days}d ` + result
|
|
51
|
+
}
|
|
52
|
+
return result
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const tableConfig = {
|
|
56
|
+
border: getBorderCharacters('void'),
|
|
57
|
+
columnDefault: {
|
|
58
|
+
paddingLeft: 1,
|
|
59
|
+
paddingRight: 1
|
|
60
|
+
},
|
|
61
|
+
drawHorizontalLine: () => false
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function printRuntimes (runtimes) {
|
|
65
|
+
const raws = [tableColumns.map(column => column.alias)]
|
|
66
|
+
|
|
67
|
+
for (const runtime of runtimes) {
|
|
68
|
+
const raw = []
|
|
69
|
+
for (const column of tableColumns) {
|
|
70
|
+
let value = runtime[column.value]
|
|
71
|
+
if (column.formatter) {
|
|
72
|
+
value = column.formatter(value)
|
|
73
|
+
}
|
|
74
|
+
value ??= '-----'
|
|
75
|
+
raw.push(value)
|
|
76
|
+
}
|
|
77
|
+
raws.push(raw)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const runtimesTable = table(raws, tableConfig)
|
|
81
|
+
console.log(runtimesTable)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function listRuntimesCommand () {
|
|
85
|
+
const client = new RuntimeApiClient()
|
|
86
|
+
const runtimes = await client.getRuntimes()
|
|
87
|
+
if (runtimes.length === 0) {
|
|
88
|
+
console.log('No platformatic runtimes found.')
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
printRuntimes(runtimes)
|
|
92
|
+
|
|
93
|
+
await client.close()
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
module.exports = listRuntimesCommand
|
package/lib/reload.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { parseArgs } = require('node:util')
|
|
4
|
+
const RuntimeApiClient = require('./runtime-api-client')
|
|
5
|
+
|
|
6
|
+
async function reloadRuntimeCommand (argv) {
|
|
7
|
+
const args = parseArgs({
|
|
8
|
+
args: argv,
|
|
9
|
+
options: {
|
|
10
|
+
pid: { type: 'string', short: 'p' },
|
|
11
|
+
name: { type: 'string', short: 'n' }
|
|
12
|
+
},
|
|
13
|
+
strict: false
|
|
14
|
+
}).values
|
|
15
|
+
|
|
16
|
+
const client = new RuntimeApiClient()
|
|
17
|
+
const runtime = await client.getMatchingRuntime(args)
|
|
18
|
+
|
|
19
|
+
await client.reloadRuntime(runtime.pid)
|
|
20
|
+
console.log(`Reloaded runtime "${runtime.packageName}".`)
|
|
21
|
+
|
|
22
|
+
await client.close()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = reloadRuntimeCommand
|
package/lib/restart.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { parseArgs } = require('node:util')
|
|
4
|
+
const RuntimeApiClient = require('./runtime-api-client')
|
|
5
|
+
|
|
6
|
+
async function restartRuntimeCommand (argv) {
|
|
7
|
+
const args = parseArgs({
|
|
8
|
+
args: argv,
|
|
9
|
+
options: {
|
|
10
|
+
pid: { type: 'string', short: 'p' },
|
|
11
|
+
name: { type: 'string', short: 'n' }
|
|
12
|
+
},
|
|
13
|
+
strict: false
|
|
14
|
+
}).values
|
|
15
|
+
|
|
16
|
+
const client = new RuntimeApiClient()
|
|
17
|
+
const runtime = await client.getMatchingRuntime(args)
|
|
18
|
+
|
|
19
|
+
const runtimeProcess = await client.restartRuntime(runtime.pid, { stdio: 'inherit' })
|
|
20
|
+
|
|
21
|
+
process.on('SIGINT', () => {
|
|
22
|
+
runtimeProcess.kill('SIGINT')
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
process.on('SIGTERM', () => {
|
|
26
|
+
runtimeProcess.kill('SIGTERM')
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
await client.close()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = restartRuntimeCommand
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { tmpdir, platform, EOL } = require('node:os')
|
|
4
|
+
const { join } = require('node:path')
|
|
5
|
+
const { exec, spawn } = require('node:child_process')
|
|
6
|
+
const { readdir } = require('node:fs/promises')
|
|
7
|
+
const { Readable } = require('node:stream')
|
|
8
|
+
const { Client } = require('undici')
|
|
9
|
+
const WebSocket = require('ws')
|
|
10
|
+
const errors = require('./errors.js')
|
|
11
|
+
|
|
12
|
+
const PLATFORMATIC_TMP_DIR = join(tmpdir(), 'platformatic', 'pids')
|
|
13
|
+
const PLATFORMATIC_PIPE_PREFIX = '\\\\.\\pipe\\platformatic-'
|
|
14
|
+
|
|
15
|
+
class RuntimeApiClient {
|
|
16
|
+
#undiciClients = new Map()
|
|
17
|
+
#webSockets = new Set()
|
|
18
|
+
|
|
19
|
+
async getMatchingRuntime (opts) {
|
|
20
|
+
const runtimes = await this.getRuntimes()
|
|
21
|
+
|
|
22
|
+
let runtime = null
|
|
23
|
+
if (opts.pid) {
|
|
24
|
+
runtime = runtimes.find(runtime => runtime.pid === parseInt(opts.pid))
|
|
25
|
+
} else if (opts.name) {
|
|
26
|
+
runtime = runtimes.find(runtime => runtime.packageName === opts.name)
|
|
27
|
+
} else if (runtimes.length === 1) {
|
|
28
|
+
runtime = runtimes[0]
|
|
29
|
+
}
|
|
30
|
+
if (!runtime) {
|
|
31
|
+
throw errors.RuntimeNotFound()
|
|
32
|
+
}
|
|
33
|
+
return runtime
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async getRuntimes () {
|
|
37
|
+
const runtimePIDs = platform() === 'win32'
|
|
38
|
+
? await this.#getWindowsRuntimePIDs()
|
|
39
|
+
: await this.#getUnixRuntimePIDs()
|
|
40
|
+
|
|
41
|
+
const getMetadataRequests = await Promise.allSettled(
|
|
42
|
+
runtimePIDs.map(async (runtimePID) => {
|
|
43
|
+
return this.getRuntimeMetadata(runtimePID)
|
|
44
|
+
})
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
return getMetadataRequests
|
|
48
|
+
.filter(result => result.status === 'fulfilled')
|
|
49
|
+
.map(result => result.value)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async getRuntimeMetadata (pid) {
|
|
53
|
+
const client = this.#getUndiciClient(pid)
|
|
54
|
+
|
|
55
|
+
const { statusCode, body } = await client.request({
|
|
56
|
+
path: '/api/metadata',
|
|
57
|
+
method: 'GET'
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
if (statusCode !== 200) {
|
|
61
|
+
const error = await body.text()
|
|
62
|
+
throw new errors.FailedToGetRuntimeMetadata(error)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const metadata = await body.json()
|
|
66
|
+
return metadata
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async getRuntimeServices (pid) {
|
|
70
|
+
const client = this.#getUndiciClient(pid)
|
|
71
|
+
|
|
72
|
+
const { statusCode, body } = await client.request({
|
|
73
|
+
path: '/api/services',
|
|
74
|
+
method: 'GET'
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
if (statusCode !== 200) {
|
|
78
|
+
const error = await body.text()
|
|
79
|
+
throw new errors.FailedToGetRuntimeServices(error)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const runtimeServices = await body.json()
|
|
83
|
+
return runtimeServices
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async getRuntimeServiceConfig (pid, serviceId) {
|
|
87
|
+
const client = this.#getUndiciClient(pid)
|
|
88
|
+
|
|
89
|
+
const { statusCode, body } = await client.request({
|
|
90
|
+
path: `/api/services/${serviceId}/config`,
|
|
91
|
+
method: 'GET'
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
if (statusCode !== 200) {
|
|
95
|
+
const error = await body.text()
|
|
96
|
+
throw new errors.FailedToGetRuntimeServiceConfig(error)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const serviceConfig = await body.json()
|
|
100
|
+
return serviceConfig
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async getRuntimeConfig (pid) {
|
|
104
|
+
const client = this.#getUndiciClient(pid)
|
|
105
|
+
|
|
106
|
+
const { statusCode, body } = await client.request({
|
|
107
|
+
path: '/api/config',
|
|
108
|
+
method: 'GET'
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
if (statusCode !== 200) {
|
|
112
|
+
const error = await body.text()
|
|
113
|
+
throw new errors.FailedToGetRuntimeConfig(error)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const runtimeConfig = await body.json()
|
|
117
|
+
return runtimeConfig
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async getRuntimeEnv (pid) {
|
|
121
|
+
const client = this.#getUndiciClient(pid)
|
|
122
|
+
|
|
123
|
+
const { statusCode, body } = await client.request({
|
|
124
|
+
path: '/api/env',
|
|
125
|
+
method: 'GET'
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
if (statusCode !== 200) {
|
|
129
|
+
const error = await body.text()
|
|
130
|
+
throw new errors.FailedToGetRuntimeEnv(error)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const runtimeEnv = await body.json()
|
|
134
|
+
return runtimeEnv
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async restartRuntime (pid, options = {}) {
|
|
138
|
+
const runtime = await this.getMatchingRuntime({ pid })
|
|
139
|
+
|
|
140
|
+
await this.stopRuntime(pid)
|
|
141
|
+
|
|
142
|
+
const [startCommand, ...startArgs] = runtime.argv
|
|
143
|
+
const child = spawn(startCommand, startArgs, { cwd: runtime.cwd, ...options })
|
|
144
|
+
return child
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async reloadRuntime (pid) {
|
|
148
|
+
const client = this.#getUndiciClient(pid)
|
|
149
|
+
|
|
150
|
+
const { statusCode, body } = await client.request({
|
|
151
|
+
path: '/api/reload',
|
|
152
|
+
method: 'POST'
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
if (statusCode !== 200) {
|
|
156
|
+
const error = await body.text()
|
|
157
|
+
throw new errors.FailedToReloadRuntime(error)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async stopRuntime (pid) {
|
|
162
|
+
const client = this.#getUndiciClient(pid)
|
|
163
|
+
|
|
164
|
+
const { statusCode, body } = await client.request({
|
|
165
|
+
path: '/api/stop',
|
|
166
|
+
method: 'POST'
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
if (statusCode !== 200) {
|
|
170
|
+
const error = await body.text()
|
|
171
|
+
throw new errors.FailedToStopRuntime(error)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
getRuntimeLogsStream (pid, options) {
|
|
176
|
+
const socketPath = this.#getSocketPathFromPid(pid)
|
|
177
|
+
let query = ''
|
|
178
|
+
if (options.level || options.pretty || options.serviceId) {
|
|
179
|
+
query = '?' + new URLSearchParams(options).toString()
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const protocol = platform() === 'win32' ? 'ws+unix:' : 'ws+unix://'
|
|
183
|
+
const webSocketUrl = protocol + socketPath + ':/api/logs' + query
|
|
184
|
+
const webSocketStream = new WebSocketStream(webSocketUrl)
|
|
185
|
+
this.#webSockets.add(webSocketStream.ws)
|
|
186
|
+
|
|
187
|
+
return webSocketStream
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async injectRuntime (pid, serviceId, options) {
|
|
191
|
+
const client = this.#getUndiciClient(pid)
|
|
192
|
+
|
|
193
|
+
const response = await client.request({
|
|
194
|
+
path: `/api/services/${serviceId}/proxy` + options.url,
|
|
195
|
+
method: options.method,
|
|
196
|
+
headers: options.headers,
|
|
197
|
+
query: options.query,
|
|
198
|
+
body: options.body
|
|
199
|
+
})
|
|
200
|
+
return response
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async close () {
|
|
204
|
+
for (const client of this.#undiciClients.values()) {
|
|
205
|
+
client.close()
|
|
206
|
+
}
|
|
207
|
+
for (const webSocket of this.#webSockets) {
|
|
208
|
+
webSocket.close()
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
#getUndiciClient (pid) {
|
|
213
|
+
let undiciClient = this.#undiciClients.get(pid)
|
|
214
|
+
if (!undiciClient) {
|
|
215
|
+
const socketPath = this.#getSocketPathFromPid(pid)
|
|
216
|
+
undiciClient = new Client({
|
|
217
|
+
hostname: 'localhost',
|
|
218
|
+
protocol: 'http:'
|
|
219
|
+
}, { socketPath })
|
|
220
|
+
|
|
221
|
+
this.#undiciClients.set(pid, undiciClient)
|
|
222
|
+
}
|
|
223
|
+
return undiciClient
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
#getSocketPathFromPid (pid) {
|
|
227
|
+
if (platform() === 'win32') {
|
|
228
|
+
return PLATFORMATIC_PIPE_PREFIX + pid
|
|
229
|
+
}
|
|
230
|
+
return join(PLATFORMATIC_TMP_DIR, `${pid}.sock`)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async #getUnixRuntimePIDs () {
|
|
234
|
+
const socketNames = await readdir(PLATFORMATIC_TMP_DIR)
|
|
235
|
+
const runtimePIDs = []
|
|
236
|
+
for (const socketName of socketNames) {
|
|
237
|
+
const runtimePID = socketName.replace('.sock', '')
|
|
238
|
+
runtimePIDs.push(parseInt(runtimePID))
|
|
239
|
+
}
|
|
240
|
+
return runtimePIDs
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async #getWindowsRuntimePIDs () {
|
|
244
|
+
const pipeNames = await this.#getWindowsNamedPipes()
|
|
245
|
+
const runtimePIDs = []
|
|
246
|
+
for (const pipeName of pipeNames) {
|
|
247
|
+
if (pipeName.startsWith(PLATFORMATIC_PIPE_PREFIX)) {
|
|
248
|
+
const runtimePID = pipeName.replace(PLATFORMATIC_PIPE_PREFIX, '')
|
|
249
|
+
runtimePIDs.push(parseInt(runtimePID))
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return runtimePIDs
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
async #getWindowsNamedPipes () {
|
|
256
|
+
return new Promise((resolve, reject) => {
|
|
257
|
+
exec(
|
|
258
|
+
'[System.IO.Directory]::GetFiles("\\\\.\\pipe\\")',
|
|
259
|
+
{ shell: 'powershell.exe' },
|
|
260
|
+
(err, stdout) => {
|
|
261
|
+
if (err) {
|
|
262
|
+
reject(err)
|
|
263
|
+
return
|
|
264
|
+
}
|
|
265
|
+
const namedPipes = stdout.split(EOL)
|
|
266
|
+
resolve(namedPipes)
|
|
267
|
+
}
|
|
268
|
+
)
|
|
269
|
+
})
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
class WebSocketStream extends Readable {
|
|
274
|
+
constructor (url) {
|
|
275
|
+
super()
|
|
276
|
+
this.ws = new WebSocket(url)
|
|
277
|
+
|
|
278
|
+
this.ws.on('message', (data) => {
|
|
279
|
+
this.push(data)
|
|
280
|
+
})
|
|
281
|
+
this.ws.on('close', () => {
|
|
282
|
+
this.push(null)
|
|
283
|
+
})
|
|
284
|
+
this.ws.on('error', (err) => {
|
|
285
|
+
this.emit('error', new errors.FailedToStreamRuntimeLogs(err.message))
|
|
286
|
+
})
|
|
287
|
+
this.on('close', () => {
|
|
288
|
+
this.ws.close()
|
|
289
|
+
})
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
_read () {}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
module.exports = RuntimeApiClient
|
package/lib/services.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { parseArgs } = require('node:util')
|
|
4
|
+
const { table, getBorderCharacters } = require('table')
|
|
5
|
+
const RuntimeApiClient = require('./runtime-api-client')
|
|
6
|
+
|
|
7
|
+
const tableColumns = [
|
|
8
|
+
{
|
|
9
|
+
value: 'id',
|
|
10
|
+
alias: 'NAME'
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
value: 'type',
|
|
14
|
+
alias: 'TYPE'
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
value: 'entrypoint',
|
|
18
|
+
alias: 'ENTRYPOINT',
|
|
19
|
+
formatter: (entrypoint) => {
|
|
20
|
+
return entrypoint ? 'yes' : 'no'
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
const tableConfig = {
|
|
26
|
+
border: getBorderCharacters('void'),
|
|
27
|
+
columnDefault: {
|
|
28
|
+
paddingLeft: 1,
|
|
29
|
+
paddingRight: 1
|
|
30
|
+
},
|
|
31
|
+
drawHorizontalLine: () => false
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function printRuntimeServices (services) {
|
|
35
|
+
const raws = [tableColumns.map(column => column.alias)]
|
|
36
|
+
|
|
37
|
+
for (const service of services.services) {
|
|
38
|
+
const raw = []
|
|
39
|
+
for (const column of tableColumns) {
|
|
40
|
+
let value = service[column.value]
|
|
41
|
+
if (column.formatter) {
|
|
42
|
+
value = column.formatter(value)
|
|
43
|
+
}
|
|
44
|
+
value ??= '-----'
|
|
45
|
+
raw.push(value)
|
|
46
|
+
}
|
|
47
|
+
raws.push(raw)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const servicesTable = table(raws, tableConfig)
|
|
51
|
+
console.log(servicesTable)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function getRuntimeServicesCommand (argv) {
|
|
55
|
+
const args = parseArgs({
|
|
56
|
+
args: argv,
|
|
57
|
+
options: {
|
|
58
|
+
pid: { type: 'string', short: 'p' },
|
|
59
|
+
name: { type: 'string', short: 'n' }
|
|
60
|
+
},
|
|
61
|
+
strict: false
|
|
62
|
+
}).values
|
|
63
|
+
|
|
64
|
+
const client = new RuntimeApiClient()
|
|
65
|
+
const runtime = await client.getMatchingRuntime(args)
|
|
66
|
+
|
|
67
|
+
const runtimeServices = await client.getRuntimeServices(runtime.pid)
|
|
68
|
+
printRuntimeServices(runtimeServices)
|
|
69
|
+
|
|
70
|
+
await client.close()
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
module.exports = getRuntimeServicesCommand
|
package/lib/stop.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { parseArgs } = require('node:util')
|
|
4
|
+
const RuntimeApiClient = require('./runtime-api-client')
|
|
5
|
+
|
|
6
|
+
async function stopRuntimeCommand (argv) {
|
|
7
|
+
const args = parseArgs({
|
|
8
|
+
args: argv,
|
|
9
|
+
options: {
|
|
10
|
+
pid: { type: 'string', short: 'p' },
|
|
11
|
+
name: { type: 'string', short: 'n' }
|
|
12
|
+
},
|
|
13
|
+
strict: false
|
|
14
|
+
}).values
|
|
15
|
+
|
|
16
|
+
const client = new RuntimeApiClient()
|
|
17
|
+
const runtime = await client.getMatchingRuntime(args)
|
|
18
|
+
|
|
19
|
+
await client.stopRuntime(runtime.pid)
|
|
20
|
+
console.log(`Stopped runtime "${runtime.packageName}".`)
|
|
21
|
+
|
|
22
|
+
await client.close()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = stopRuntimeCommand
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@platformatic/control",
|
|
3
|
+
"version": "1.24.0",
|
|
4
|
+
"description": "Platformatic Control",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"plt-ctl": "./control.js"
|
|
8
|
+
},
|
|
9
|
+
"author": "Matteo Collina <hello@matteocollina.com>",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/platformatic/platformatic.git"
|
|
13
|
+
},
|
|
14
|
+
"license": "Apache-2.0",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/platformatic/platformatic/issues"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/platformatic/platformatic#readme",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"borp": "^0.9.0",
|
|
21
|
+
"desm": "^1.3.0",
|
|
22
|
+
"execa": "^8.0.1",
|
|
23
|
+
"snazzy": "^9.0.0",
|
|
24
|
+
"split2": "^4.2.0",
|
|
25
|
+
"standard": "^17.1.0",
|
|
26
|
+
"tsd": "^0.30.4",
|
|
27
|
+
"@platformatic/runtime": "1.24.0"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@fastify/error": "^3.4.1",
|
|
31
|
+
"commist": "^3.2.0",
|
|
32
|
+
"pino": "^8.17.2",
|
|
33
|
+
"pino-pretty": "^10.3.1",
|
|
34
|
+
"help-me": "^5.0.0",
|
|
35
|
+
"table": "^6.8.1",
|
|
36
|
+
"undici": "^6.6.0",
|
|
37
|
+
"ws": "^8.16.0"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"test": "pnpm run lint && pnpm run unit",
|
|
41
|
+
"unit": "borp --concurrency=1 --timeout 60000",
|
|
42
|
+
"build": "node lib/schema.js | json2ts > config.d.ts",
|
|
43
|
+
"lint": "standard | snazzy"
|
|
44
|
+
}
|
|
45
|
+
}
|