@metaplay/metaplay-auth 1.1.5 → 1.1.7
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/CHANGELOG.md +22 -0
- package/README.md +44 -2
- package/dist/auth.js +47 -32
- package/dist/auth.js.map +1 -1
- package/dist/deployment.js +218 -0
- package/dist/deployment.js.map +1 -0
- package/dist/index.js +51 -6
- package/dist/index.js.map +1 -1
- package/dist/secret_store.js.map +1 -1
- package/dist/src/auth.js +335 -0
- package/dist/src/auth.js.map +1 -0
- package/dist/src/deployment.js +218 -0
- package/dist/src/deployment.js.map +1 -0
- package/dist/src/index.js +215 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/logging.js +18 -0
- package/dist/src/logging.js.map +1 -0
- package/dist/src/secret_store.js +79 -0
- package/dist/src/secret_store.js.map +1 -0
- package/dist/src/stackapi.js +111 -0
- package/dist/src/stackapi.js.map +1 -0
- package/dist/src/utils.js +41 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/stackapi.js +8 -13
- package/dist/stackapi.js.map +1 -1
- package/dist/tests/utils.spec.js +18 -0
- package/dist/tests/utils.spec.js.map +1 -0
- package/package.json +3 -2
- package/src/auth.ts +59 -36
- package/src/deployment.ts +260 -0
- package/src/index.ts +55 -6
- package/src/secret_store.ts +2 -2
- package/src/stackapi.ts +8 -13
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.1.7] - Unpublished
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
* The `machine-login` command now works with machine user credentials in the `METAPLAY_CREDENTIALS` environment variable. You can get the credentials from the developer portal.
|
|
8
|
+
|
|
9
|
+
### Changed
|
|
10
|
+
|
|
11
|
+
* The `show-tokens` command is changed back to print more human-friendly output and the `--format` flag is removed.
|
|
12
|
+
* Improve `check-deployment` error checking and clean up logging.
|
|
13
|
+
|
|
14
|
+
## [1.1.6] - 2024-03-12
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
* Added experimental `check-deployment` command for checking the status of a game server after deploying it.
|
|
19
|
+
* Added experimental `machine-login` command to login using machine credentials.
|
|
20
|
+
|
|
21
|
+
### Changed
|
|
22
|
+
|
|
23
|
+
* The `show-tokens` now outputs the values by default in JSON, you can use `metaplay-auth show-tokens -f pretty` to get the old formatting.
|
|
24
|
+
|
|
3
25
|
## [1.1.5] - 2024-02-26
|
|
4
26
|
|
|
5
27
|
### Changed
|
package/README.md
CHANGED
|
@@ -8,18 +8,60 @@
|
|
|
8
8
|
|
|
9
9
|
You can install `metaplay-auth` via NPM:
|
|
10
10
|
|
|
11
|
-
```
|
|
11
|
+
```bash
|
|
12
12
|
npm install -g @metaplay/metaplay-auth
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
Alternatively, you can quickly locally run the latest version using `npx`:
|
|
16
16
|
|
|
17
|
-
```
|
|
17
|
+
```bash
|
|
18
18
|
npx @metaplay/metaplay-auth@latest login
|
|
19
19
|
npx @metaplay/metaplay-auth@latest show-tokens
|
|
20
20
|
npx @metaplay/metaplay-auth@latest logout
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
+
## Running locally
|
|
24
|
+
|
|
25
|
+
When making changes to the `metaplay-auth`, it's easiest to test by running it locally:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
MetaplaySDK/AuthCLI$ pnpm dev login
|
|
29
|
+
MetaplaySDK/AuthCLI$ pnpm dev show-tokens
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Building from sources
|
|
33
|
+
|
|
34
|
+
The tool is also provided as part of the Metaplay SDK package (available via the [Metaplay Portal](https://portal.metaplay.dev/) under `MetaplaySDK/AuthCLI/` and utilizes the same TypeScript toolchains as other components in the Metaplay SDK.
|
|
35
|
+
|
|
36
|
+
You can build the tool using [Moon](https://www.npmjs.com/package/@moonrepo/cli):
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
MetaplaySDK/AuthCLI$ moon run build
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Now, you can run the application with node:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
MetaplaySDK/AuthCLI$ node dist/index.js
|
|
46
|
+
Usage: metaplay-auth [options] [command]
|
|
47
|
+
|
|
48
|
+
Authenticate with Metaplay and get AWS and Kubernetes credentials for game servers.
|
|
49
|
+
|
|
50
|
+
Options:
|
|
51
|
+
-V, --version output the version number
|
|
52
|
+
-d, --debug enable debug output
|
|
53
|
+
-h, --help display help for command
|
|
54
|
+
|
|
55
|
+
Commands:
|
|
56
|
+
login login to your Metaplay account
|
|
57
|
+
logout log out of your Metaplay account
|
|
58
|
+
show-tokens show loaded tokens
|
|
59
|
+
get-kubeconfig [options] [gameserver] get kubeconfig for deployment
|
|
60
|
+
get-aws-credentials [options] [gameserver] get AWS credentials for deployment
|
|
61
|
+
get-environment [options] [gameserver] get environment details for deployment
|
|
62
|
+
help [command] display help for command
|
|
63
|
+
```
|
|
64
|
+
|
|
23
65
|
## License
|
|
24
66
|
|
|
25
67
|
See the LICENSE file.
|
package/dist/auth.js
CHANGED
|
@@ -16,7 +16,6 @@ const tokenEndpoint = `${baseURL}/oauth2/token`;
|
|
|
16
16
|
const wellknownApi = new WellknownApi(new Configuration({
|
|
17
17
|
basePath: baseURL,
|
|
18
18
|
}));
|
|
19
|
-
const tokenList = ['id_token', 'access_token', 'refresh_token']; // List of compulsory tokens
|
|
20
19
|
/**
|
|
21
20
|
* A helper function which generates a code verifier and challenge for exchaning code from Ory server.
|
|
22
21
|
* @returns
|
|
@@ -91,7 +90,8 @@ export async function loginAndSaveTokens() {
|
|
|
91
90
|
try {
|
|
92
91
|
logger.debug(`Received callback request with code ${code}. Preparing to exchange for tokens...`);
|
|
93
92
|
const tokens = await getTokensWithAuthorizationCode(state, redirectUri, verifier, code);
|
|
94
|
-
|
|
93
|
+
// Only save access_token, id_token, and refresh_token
|
|
94
|
+
await saveTokens({ access_token: tokens.access_token, id_token: tokens.id_token, refresh_token: tokens.refresh_token });
|
|
95
95
|
console.log('You are now logged in and can call the other commands.');
|
|
96
96
|
// TODO: Could return a pre-generated HTML page instead of text?
|
|
97
97
|
return 'Authentication successful! You can close this window.';
|
|
@@ -115,21 +115,51 @@ export async function loginAndSaveTokens() {
|
|
|
115
115
|
console.log(`Attempting to open a browser to log in. If a browser did not open up, you can copy-paste the following URL to authenticate:\n\n${authorizationUrl}\n`);
|
|
116
116
|
void open(authorizationUrl);
|
|
117
117
|
}
|
|
118
|
+
export async function machineLoginAndSaveTokens(clientId, clientSecret) {
|
|
119
|
+
// Get a fresh access token from Metaplay Auth.
|
|
120
|
+
const params = new URLSearchParams();
|
|
121
|
+
params.set('grant_type', 'client_credentials');
|
|
122
|
+
params.set('client_id', clientId);
|
|
123
|
+
params.set('client_secret', clientSecret);
|
|
124
|
+
params.set('scope', 'openid email profile offline_access');
|
|
125
|
+
const res = await fetch(tokenEndpoint, {
|
|
126
|
+
method: 'POST',
|
|
127
|
+
headers: {
|
|
128
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
129
|
+
},
|
|
130
|
+
body: params.toString(),
|
|
131
|
+
});
|
|
132
|
+
// Return type checked manually by Teemu on 2024-3-7.
|
|
133
|
+
const tokens = await res.json();
|
|
134
|
+
logger.debug('Received machine authentication tokens, saving them for future use...');
|
|
135
|
+
await saveTokens({ access_token: tokens.access_token });
|
|
136
|
+
const userInfoResponse = await fetch('https://portal.metaplay.dev/api/external/userinfo', {
|
|
137
|
+
headers: {
|
|
138
|
+
Authorization: `Bearer ${tokens.access_token}`
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
const userInfo = await userInfoResponse.json();
|
|
142
|
+
console.log(`You are now logged in with machine user ${userInfo.given_name} ${userInfo.family_name} (clientId=${clientId}) and can execute the other commands.`);
|
|
143
|
+
}
|
|
118
144
|
/**
|
|
119
145
|
* Refresh access and ID token with a refresh token.
|
|
120
146
|
*/
|
|
121
147
|
export async function extendCurrentSession() {
|
|
122
148
|
try {
|
|
123
149
|
const tokens = await loadTokens();
|
|
124
|
-
logger.debug('
|
|
150
|
+
logger.debug('Check if access token still valid...');
|
|
125
151
|
if (await validateToken(tokens.access_token)) {
|
|
126
152
|
// Access token is not expired, return to the caller
|
|
127
|
-
logger.debug('Access token is valid,
|
|
153
|
+
logger.debug('Access token is valid, no need to refresh.');
|
|
128
154
|
return;
|
|
129
155
|
}
|
|
156
|
+
// Check that the refresh_token exists (machine users don't have it)
|
|
157
|
+
if (!tokens.refresh_token) {
|
|
158
|
+
throw new Error('Cannot refresh an access_token without a refresh_token. With machine users, should just login again instead.');
|
|
159
|
+
}
|
|
130
160
|
logger.debug('Access token is no longer valid, trying to extend the current session with a refresh token.');
|
|
131
161
|
const refreshedTokens = await extendCurrentSessionWithRefreshToken(tokens.refresh_token);
|
|
132
|
-
await saveTokens(refreshedTokens);
|
|
162
|
+
await saveTokens({ access_token: refreshedTokens.access_token, id_token: refreshedTokens.id_token, refresh_token: refreshedTokens.refresh_token });
|
|
133
163
|
}
|
|
134
164
|
catch (error) {
|
|
135
165
|
if (error instanceof Error) {
|
|
@@ -144,7 +174,7 @@ export async function extendCurrentSession() {
|
|
|
144
174
|
* @returns A promise that resolves to a new set of tokens.
|
|
145
175
|
*/
|
|
146
176
|
async function extendCurrentSessionWithRefreshToken(refreshToken) {
|
|
147
|
-
// TODO:
|
|
177
|
+
// TODO: similar to the todo task in getTokensWithAuthorizationCode, http request can be handled by ory/client.
|
|
148
178
|
const params = new URLSearchParams({
|
|
149
179
|
grant_type: 'refresh_token',
|
|
150
180
|
refresh_token: refreshToken,
|
|
@@ -197,7 +227,7 @@ async function getTokensWithAuthorizationCode(state, redirectUri, verifier, code
|
|
|
197
227
|
if (error instanceof Error) {
|
|
198
228
|
logger.error(`Error exchanging code for tokens: ${error.message}`);
|
|
199
229
|
}
|
|
200
|
-
|
|
230
|
+
throw error;
|
|
201
231
|
}
|
|
202
232
|
}
|
|
203
233
|
/**
|
|
@@ -205,17 +235,11 @@ async function getTokensWithAuthorizationCode(state, redirectUri, verifier, code
|
|
|
205
235
|
*/
|
|
206
236
|
export async function loadTokens() {
|
|
207
237
|
try {
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
if (idToken == null || accessToken == null || refreshToken == null) {
|
|
212
|
-
throw new Error('Metaplay tokens not found. Are you logged in?');
|
|
238
|
+
const tokens = await getSecret('tokens');
|
|
239
|
+
if (!tokens) {
|
|
240
|
+
throw new Error('Unable to load tokens. You need to login first.');
|
|
213
241
|
}
|
|
214
|
-
return
|
|
215
|
-
id_token: idToken,
|
|
216
|
-
access_token: accessToken,
|
|
217
|
-
refresh_token: refreshToken
|
|
218
|
-
};
|
|
242
|
+
return tokens;
|
|
219
243
|
}
|
|
220
244
|
catch (error) {
|
|
221
245
|
if (error instanceof Error) {
|
|
@@ -231,19 +255,14 @@ export async function loadTokens() {
|
|
|
231
255
|
export async function saveTokens(tokens) {
|
|
232
256
|
try {
|
|
233
257
|
logger.debug('Received new tokens, verifying...');
|
|
234
|
-
//
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
throw new Error(`Metaplay token ${tokenName} not found. Please log in again and make sure all checkboxes of permissions are selected before proceeding.`);
|
|
238
|
-
}
|
|
258
|
+
// All tokens must have an access_token (machine users only have it)
|
|
259
|
+
if (!tokens.access_token) {
|
|
260
|
+
throw new Error('Metaplay token has no access_token. Please log in again and make sure all checkboxes of permissions are selected before proceeding.');
|
|
239
261
|
}
|
|
240
262
|
logger.debug('Token verification completed, storing tokens...');
|
|
241
|
-
await setSecret('
|
|
242
|
-
await setSecret('access_token', tokens.access_token);
|
|
243
|
-
await setSecret('refresh_token', tokens.refresh_token);
|
|
263
|
+
await setSecret('tokens', tokens);
|
|
244
264
|
logger.debug('Tokens successfully stored.');
|
|
245
265
|
await showTokenInfo(tokens.access_token);
|
|
246
|
-
// await showTokenInfo(tokens.id_token)
|
|
247
266
|
}
|
|
248
267
|
catch (error) {
|
|
249
268
|
if (error instanceof Error) {
|
|
@@ -257,12 +276,8 @@ export async function saveTokens(tokens) {
|
|
|
257
276
|
*/
|
|
258
277
|
export async function removeTokens() {
|
|
259
278
|
try {
|
|
260
|
-
await removeSecret('
|
|
261
|
-
logger.debug('Removed
|
|
262
|
-
await removeSecret('access_token');
|
|
263
|
-
logger.debug('Removed access_token.');
|
|
264
|
-
await removeSecret('refresh_token');
|
|
265
|
-
logger.debug('Removed refresh_token.');
|
|
279
|
+
await removeSecret('tokens');
|
|
280
|
+
logger.debug('Removed tokens.');
|
|
266
281
|
}
|
|
267
282
|
catch (error) {
|
|
268
283
|
if (error instanceof Error) {
|
package/dist/auth.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA,2DAA2D;AAE3D,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,kBAAkB,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,IAAI,CAAA;AAEvF,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACrD,OAAO,GAAG,MAAM,cAAc,CAAA;AAC9B,OAAO,QAAQ,MAAM,YAAY,CAAA;AACjC,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AACzD,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAEtE,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAErC,wHAAwH;AACxH,MAAM,QAAQ,GAAG,sCAAsC,CAAA;AACvD,MAAM,OAAO,GAAG,2BAA2B,CAAA;AAC3C,MAAM,qBAAqB,GAAG,GAAG,OAAO,cAAc,CAAA;AACtD,MAA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AAC;AAED;;;GAGG;AACH,KAAK,UAAU,aAAa,CAAE,KAAa;IACzC,MAAM,CAAC,KAAK,CAAC,8BAA8B,CAAC,CAAA;IAC5C,mBAAmB;IACnB,MAAM,iBAAiB,GAAG,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAA;IAE/D,oBAAoB;IACpB,IAAI,CAAC,iBAAiB,EAAE;QACtB,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAA;KACtC;IAED,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAC,CAAC,CAAA;AACjD,CAAC"}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { promises as fs, existsSync } from 'fs';
|
|
2
|
+
import { KubeConfig, CoreV1Api, V1Pod } from '@kubernetes/client-node';
|
|
3
|
+
import { logger } from './logging.js';
|
|
4
|
+
import { error } from 'console';
|
|
5
|
+
var GameServerPodPhase;
|
|
6
|
+
(function (GameServerPodPhase) {
|
|
7
|
+
GameServerPodPhase["Pending"] = "Pending";
|
|
8
|
+
GameServerPodPhase["Ready"] = "Ready";
|
|
9
|
+
GameServerPodPhase["Failed"] = "Failed";
|
|
10
|
+
GameServerPodPhase["Unknown"] = "Unknown";
|
|
11
|
+
})(GameServerPodPhase || (GameServerPodPhase = {}));
|
|
12
|
+
async function fetchGameServerPods(k8sApi, namespace) {
|
|
13
|
+
// Define label selector for gameserver
|
|
14
|
+
const labelSelector = 'app=metaplay-server';
|
|
15
|
+
try {
|
|
16
|
+
// Get gameserver pods in the namespace
|
|
17
|
+
const response = await k8sApi.listNamespacedPod(namespace, undefined, undefined, undefined, undefined, labelSelector);
|
|
18
|
+
// Return pod statuses
|
|
19
|
+
return response.body.items;
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
// \todo Better error handling ..
|
|
23
|
+
console.log('Failed to fetch pods from Kubernetes:', error);
|
|
24
|
+
throw new Error('Failed to fetch pods from Kubernetes');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function resolvePodContainersConditions(pod) {
|
|
28
|
+
const containerStatuses = pod.status?.containerStatuses;
|
|
29
|
+
if (!containerStatuses || containerStatuses.length === 0) {
|
|
30
|
+
return { phase: GameServerPodPhase.Unknown, message: 'Unable to determine pod container statuses: pod.status.containerStatuses is empty' };
|
|
31
|
+
}
|
|
32
|
+
// Only one container allowed in the game server pod
|
|
33
|
+
if (containerStatuses.length !== 1) {
|
|
34
|
+
throw new Error(`Internal error: Expecting only one container in the game server pod, got ${containerStatuses.length}`);
|
|
35
|
+
}
|
|
36
|
+
// Handle missing container state
|
|
37
|
+
const containerStatus = containerStatuses[0];
|
|
38
|
+
console.log('Container status:', containerStatus);
|
|
39
|
+
const containerState = containerStatus.state;
|
|
40
|
+
if (!containerState) {
|
|
41
|
+
return { phase: GameServerPodPhase.Unknown, message: 'Unable to get container state' };
|
|
42
|
+
}
|
|
43
|
+
// Check if container running & ready
|
|
44
|
+
const containerName = containerStatus.name;
|
|
45
|
+
if (containerStatus.ready && containerState.running) {
|
|
46
|
+
return { phase: GameServerPodPhase.Ready, message: `Container ${containerName} is in ready phase and was started at ${containerState.running.startedAt}`, details: containerState.running };
|
|
47
|
+
}
|
|
48
|
+
// \note these may not be complete (or completely accurate)
|
|
49
|
+
const knownContainerFailureReasons = ['CrashLoopBackOff', 'Error', 'ImagePullBackOff', 'CreateContainerConfigError', 'OOMKilled', 'ContainerCannotRun', 'BackOff', 'InvalidImageName'];
|
|
50
|
+
const knownContainerPendingReasons = ['Init', 'Pending', 'PodInitializing'];
|
|
51
|
+
// Check if there's a previous terminated state (usually indicates a crash during server initialization)
|
|
52
|
+
const lastState = containerStatus.lastState;
|
|
53
|
+
if (lastState) {
|
|
54
|
+
if (lastState.terminated) {
|
|
55
|
+
// Try to detecth why the previous launch failed
|
|
56
|
+
if (containerState.waiting) {
|
|
57
|
+
const reason = containerState.waiting.reason;
|
|
58
|
+
if (knownContainerFailureReasons.includes(reason)) {
|
|
59
|
+
return { phase: GameServerPodPhase.Failed, message: `Container ${containerName} is in waiting state, reason=${reason}`, details: containerState.waiting };
|
|
60
|
+
}
|
|
61
|
+
else if (knownContainerPendingReasons.includes(reason)) {
|
|
62
|
+
return { phase: GameServerPodPhase.Pending, message: `Container ${containerName} is in waiting state, reason=${reason}`, details: containerState.waiting };
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
return { phase: GameServerPodPhase.Unknown, message: `Container ${containerName} is in waiting state, reason=${reason}`, details: containerState.waiting };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else if (containerState.running) {
|
|
69
|
+
// This happens when the container is still initializing
|
|
70
|
+
return { phase: GameServerPodPhase.Pending, message: `Container ${containerName} is in running state`, details: containerState.running };
|
|
71
|
+
}
|
|
72
|
+
else if (containerState.terminated) {
|
|
73
|
+
return { phase: GameServerPodPhase.Failed, message: `Container ${containerName} is in terminated state`, details: containerState.terminated };
|
|
74
|
+
}
|
|
75
|
+
// Unable to determine launch failure reason, just return previous launch
|
|
76
|
+
return { phase: GameServerPodPhase.Failed, message: `Container ${containerName} previous launch failed with exitCode=${lastState.terminated.exitCode} and reason=${lastState.terminated.reason}`, details: lastState.terminated };
|
|
77
|
+
}
|
|
78
|
+
// \todo handle containerState.running states (including various initialization states)
|
|
79
|
+
// \todo handle containerState.terminated states (what do these even mean)
|
|
80
|
+
}
|
|
81
|
+
console.log('Game server pod container in unknown state:', containerState);
|
|
82
|
+
return { phase: GameServerPodPhase.Unknown, message: 'Container in unknown state', details: containerState };
|
|
83
|
+
}
|
|
84
|
+
function resolvePodStatusConditions(pod) {
|
|
85
|
+
const conditions = pod.status?.conditions;
|
|
86
|
+
if (!conditions || conditions.length === 0) {
|
|
87
|
+
return { phase: GameServerPodPhase.Unknown, message: 'Unable to determine pod status: pod.status.conditions is empty', details: pod.status };
|
|
88
|
+
}
|
|
89
|
+
// Bail if 'PodScheduled' is not yet true
|
|
90
|
+
const condPodScheduled = conditions.find(cond => cond.type === 'PodScheduled');
|
|
91
|
+
if (condPodScheduled?.status !== 'True') {
|
|
92
|
+
return { phase: GameServerPodPhase.Pending, message: `Pod has not yet been scheduled on a node: ${condPodScheduled?.message}`, details: condPodScheduled };
|
|
93
|
+
}
|
|
94
|
+
// Bail if 'Initialized' not is yet true
|
|
95
|
+
const condInitialized = conditions.find(cond => cond.type === 'Initialized');
|
|
96
|
+
if (condInitialized?.status !== 'True') {
|
|
97
|
+
return { phase: GameServerPodPhase.Pending, message: `Pod has not yet been initialized: ${condInitialized?.message}`, details: condInitialized };
|
|
98
|
+
}
|
|
99
|
+
// Bail if 'ContainersReady' is not yet true
|
|
100
|
+
const condContainersReady = conditions.find(cond => cond.type === 'ContainersReady');
|
|
101
|
+
if (condContainersReady?.status !== 'True') {
|
|
102
|
+
if (condContainersReady?.reason === 'ContainersNotReady') {
|
|
103
|
+
return resolvePodContainersConditions(pod);
|
|
104
|
+
}
|
|
105
|
+
return { phase: GameServerPodPhase.Pending, message: `Pod containers are not yet ready: ${condContainersReady?.message}`, details: condContainersReady };
|
|
106
|
+
}
|
|
107
|
+
// Bail if 'Ready' is not yet true
|
|
108
|
+
const condReady = conditions.find(cond => cond.type === 'Ready');
|
|
109
|
+
if (condReady?.status !== 'True') {
|
|
110
|
+
return { phase: GameServerPodPhase.Pending, message: `Pod is not yet ready: ${condReady?.message}`, details: condReady };
|
|
111
|
+
}
|
|
112
|
+
// resolvePodContainersConditions(pod) // DEBUG DEBUG enable to print container state for running pods
|
|
113
|
+
return { phase: GameServerPodPhase.Ready, message: 'Pod is ready to serve traffic' };
|
|
114
|
+
}
|
|
115
|
+
function resolvePodStatus(pod) {
|
|
116
|
+
// console.log('Pod.status:', JSON.stringify(pod.status, undefined, 2))
|
|
117
|
+
if (!pod.status) {
|
|
118
|
+
return { phase: GameServerPodPhase.Unknown, message: 'Unable to access pod.status from Kubernetes' };
|
|
119
|
+
}
|
|
120
|
+
// Handle status.phase
|
|
121
|
+
const podPhase = pod.status?.phase;
|
|
122
|
+
switch (podPhase) {
|
|
123
|
+
case 'Pending':
|
|
124
|
+
// Pod not yet scheduled
|
|
125
|
+
return { phase: GameServerPodPhase.Pending, message: 'Pod is still in Pending phase' };
|
|
126
|
+
case 'Running':
|
|
127
|
+
// Pod has been scheduled and start -- note that the containers may have failed!
|
|
128
|
+
return resolvePodStatusConditions(pod);
|
|
129
|
+
case 'Succeeded': // Should not happen, the game server pods should never stop
|
|
130
|
+
case 'Failed': // Should not happen, the game server pods should never stop
|
|
131
|
+
case 'Unknown':
|
|
132
|
+
default:
|
|
133
|
+
return { phase: GameServerPodPhase.Unknown, message: `Invalid pod.status.phase: ${podPhase}` };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
async function fetchPodLogs(k8sApi, pod) {
|
|
137
|
+
console.log('Fetching logs for pod..');
|
|
138
|
+
const podName = pod.metadata?.name;
|
|
139
|
+
const namespace = pod.metadata?.namespace;
|
|
140
|
+
const containerName = pod.spec?.containers[0].name; // \todo Handle multiple containers?
|
|
141
|
+
if (!podName || !namespace || !containerName) {
|
|
142
|
+
throw new Error('Unable to determine pod metadata');
|
|
143
|
+
}
|
|
144
|
+
const pretty = 'True';
|
|
145
|
+
const previous = false;
|
|
146
|
+
const tailLines = 100;
|
|
147
|
+
const timestamps = true;
|
|
148
|
+
try {
|
|
149
|
+
const response = await k8sApi.readNamespacedPodLog(podName, namespace, containerName, undefined, undefined, undefined, pretty, previous, undefined, tailLines, timestamps);
|
|
150
|
+
return response.body;
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
// \todo Better error handling ..
|
|
154
|
+
console.log('Failed to fetch pod logs from Kubernetes:', error);
|
|
155
|
+
throw new Error('Failed to fetch pod logs from Kubernetes');
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
async function checkGameServerPod(k8sApi, pod) {
|
|
159
|
+
// console.log('Pod:', JSON.stringify(pod, undefined, 2))
|
|
160
|
+
// Classify game server status
|
|
161
|
+
const podStatus = resolvePodStatus(pod);
|
|
162
|
+
// If game server launch failed, get the error logs
|
|
163
|
+
if (podStatus.phase === GameServerPodPhase.Failed) {
|
|
164
|
+
const logs = await fetchPodLogs(k8sApi, pod);
|
|
165
|
+
console.log('Pod logs:\n' + logs);
|
|
166
|
+
}
|
|
167
|
+
console.log(`Pod ${pod.metadata?.name} status:`, podStatus);
|
|
168
|
+
return podStatus;
|
|
169
|
+
}
|
|
170
|
+
async function delay(ms) {
|
|
171
|
+
return await new Promise(resolve => setTimeout(resolve, ms));
|
|
172
|
+
}
|
|
173
|
+
export async function checkGameServerDeployment(namespace) {
|
|
174
|
+
logger.info(`Validating game server deployment in namespace ${namespace}`);
|
|
175
|
+
// Check that the KUBECONFIG environment variable exists
|
|
176
|
+
const kubeconfigPath = process.env.KUBECONFIG;
|
|
177
|
+
if (!kubeconfigPath) {
|
|
178
|
+
throw new Error('The KUBECONFIG environment variable must be specified');
|
|
179
|
+
}
|
|
180
|
+
// Check that the kubeconfig file exists
|
|
181
|
+
if (!await existsSync(kubeconfigPath)) {
|
|
182
|
+
throw new Error(`The environment variable KUBECONFIG points to a file '${kubeconfigPath}' that doesn't exist`);
|
|
183
|
+
}
|
|
184
|
+
// Create Kubernetes API instance (with default kubeconfig)
|
|
185
|
+
const kc = new KubeConfig();
|
|
186
|
+
kc.loadFromFile(kubeconfigPath);
|
|
187
|
+
const k8sApi = kc.makeApiClient(CoreV1Api);
|
|
188
|
+
// Figure out when to stop
|
|
189
|
+
const startTime = Date.now();
|
|
190
|
+
const timeoutAt = startTime + 1 * 60 * 1000; // 5min
|
|
191
|
+
while (true) {
|
|
192
|
+
// Check pod states
|
|
193
|
+
const pods = await fetchGameServerPods(k8sApi, namespace);
|
|
194
|
+
const podStatus = await checkGameServerPod(k8sApi, pods[0]); // \todo Handle all pods
|
|
195
|
+
switch (podStatus.phase) {
|
|
196
|
+
case GameServerPodPhase.Ready:
|
|
197
|
+
console.log('Gameserver successfully started');
|
|
198
|
+
// \todo add further readiness checks -- ping endpoint, ping dashboard, other checks?
|
|
199
|
+
return 0;
|
|
200
|
+
case GameServerPodPhase.Failed:
|
|
201
|
+
console.log('Gameserver failed to start');
|
|
202
|
+
return 1;
|
|
203
|
+
case GameServerPodPhase.Pending:
|
|
204
|
+
console.log('Gameserver still starting');
|
|
205
|
+
break;
|
|
206
|
+
case GameServerPodPhase.Unknown:
|
|
207
|
+
default:
|
|
208
|
+
console.log('Gameserver in unknown state');
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
if (Date.now() >= timeoutAt) {
|
|
212
|
+
logger.error('Timeout while waiting for gameserver to initialize');
|
|
213
|
+
return 124; // timeout
|
|
214
|
+
}
|
|
215
|
+
await delay(1000);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
//# sourceMappingURL=deployment.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deployment.js","sourceRoot":"","sources":["../src/deployment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,UAAU,EAAE,MAAM,IAAI,CAAA;AAC/C,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAA;AACtE,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAE/B,IAAK,kBAKJ;AALD,WAAK,kBAAkB;IACrB,yCAAmB,CAAA;IACnB,qCAAe,CAAA;IACf,uCAAiB,CAAA;IACjB,yCAAmB,CAAA;AACrB,CAAC,EALI,kBAAkB,KAAlB,kBAAkB,QAKtB;AAQD,KAAK,UAAU,mBAAmB,CAAE,MAAiB,EAAE,SAAiB;IACtE,uCAAuC;IACvC,MAAM,aAAa,GAAG,qBAAqB,CAAA;IAE3C,IAAI;QACF,uCAAuC;QACvC,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,iB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MAAM;gBAC5B,OAAO,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAA;gBACzC,OAAO,CAAC,CAAA;YAEV,KAAK,kBAAkB,CAAC,OAAO;gBAC7B,OAAO,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAA;gBACxC,MAAK;YAEP,KAAK,kBAAkB,CAAC,OAAO,CAAC;YAChC;gBACE,OAAO,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAA;gBAC1C,MAAK;SACR;QAED,IAAI,IAAI,CAAC,GAAG,EAAE,IAAI,SAAS,EAAE;YAC3B,MAAM,CAAC,KAAK,CAAC,oDAAoD,CAAC,CAAA;YAClE,OAAO,GAAG,CAAA,CAAC,UAAU;SACtB;QAED,MAAM,KAAK,CAAC,IAAI,CAAC,CAAA;KAClB;AACH,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { Command } from 'commander';
|
|
3
|
-
import { loginAndSaveTokens, extendCurrentSession, loadTokens, removeTokens } from './auth.js';
|
|
3
|
+
import { loginAndSaveTokens, machineLoginAndSaveTokens, extendCurrentSession, loadTokens, removeTokens } from './auth.js';
|
|
4
4
|
import { StackAPI } from './stackapi.js';
|
|
5
|
+
import { checkGameServerDeployment } from './deployment.js';
|
|
5
6
|
import { setLogLevel } from './logging.js';
|
|
6
7
|
import { exit } from 'process';
|
|
7
8
|
const program = new Command();
|
|
8
9
|
program
|
|
9
10
|
.name('metaplay-auth')
|
|
10
11
|
.description('Authenticate with Metaplay and get AWS and Kubernetes credentials for game servers.')
|
|
11
|
-
.version('1.1.
|
|
12
|
+
.version('1.1.7')
|
|
12
13
|
.option('-d, --debug', 'enable debug output')
|
|
13
14
|
.hook('preAction', (thisCommand) => {
|
|
14
15
|
// Handle debug flag for all commands.
|
|
@@ -25,6 +26,32 @@ program.command('login')
|
|
|
25
26
|
.action(async () => {
|
|
26
27
|
await loginAndSaveTokens();
|
|
27
28
|
});
|
|
29
|
+
program.command('machine-login')
|
|
30
|
+
.description('login to the Metaplay cloud using a machine account (using credentials in environment variable METAPLAY_CREDENTIALS)')
|
|
31
|
+
.option('--dev-credentials', 'machine user credentials to use, only for dev purposes, use METAPLAY_CREDENTIALS env variable for better safety!')
|
|
32
|
+
.action(async (options) => {
|
|
33
|
+
// Get credentials from command line or from METAPLAY_CREDENTIALS environment variable
|
|
34
|
+
let credentials;
|
|
35
|
+
if (options.devCredentials) {
|
|
36
|
+
credentials = options.devCredentials;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
credentials = process.env.METAPLAY_CREDENTIALS;
|
|
40
|
+
if (!credentials || credentials === '') {
|
|
41
|
+
throw new Error('Unable to find the credentials, the environment variable METAPLAY_CREDENTIALS is not defined!');
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// Parse the clientId and clientSecret from the credentials (separate by a '+' character)
|
|
45
|
+
// \note We can't be certain that the secret does not contain pluses so split at the first occurrence
|
|
46
|
+
const splitOffset = credentials.indexOf('+');
|
|
47
|
+
if (splitOffset === -1) {
|
|
48
|
+
throw new Error('Invalid format for credentials, you should copy-paste the value from the developer portal verbatim');
|
|
49
|
+
}
|
|
50
|
+
const clientId = credentials.substring(0, splitOffset);
|
|
51
|
+
const clientSecret = credentials.substring(splitOffset + 1);
|
|
52
|
+
// Login with machine user and save the tokens
|
|
53
|
+
await machineLoginAndSaveTokens(clientId, clientSecret);
|
|
54
|
+
});
|
|
28
55
|
program.command('logout')
|
|
29
56
|
.description('log out of your Metaplay account')
|
|
30
57
|
.action(async () => {
|
|
@@ -46,7 +73,7 @@ program.command('show-tokens')
|
|
|
46
73
|
.hook('preAction', async () => {
|
|
47
74
|
await extendCurrentSession();
|
|
48
75
|
})
|
|
49
|
-
.action(async () => {
|
|
76
|
+
.action(async (options) => {
|
|
50
77
|
try {
|
|
51
78
|
// TODO: Could detect if not logged in and fail more gracefully?
|
|
52
79
|
const tokens = await loadTokens();
|
|
@@ -75,7 +102,7 @@ program.command('get-kubeconfig')
|
|
|
75
102
|
if (!gameserver && !(options.organization && options.project && options.environment)) {
|
|
76
103
|
throw new Error('Could not determine a deployment to fetch the KubeConfigs from. You need to specify either a gameserver or an organization, project, and environment');
|
|
77
104
|
}
|
|
78
|
-
const stackApi = new StackAPI(tokens.
|
|
105
|
+
const stackApi = new StackAPI(tokens.access_token);
|
|
79
106
|
if (options.stackApi) {
|
|
80
107
|
stackApi.stack_api_base_uri = options.stackApi;
|
|
81
108
|
}
|
|
@@ -110,7 +137,7 @@ program.command('get-aws-credentials')
|
|
|
110
137
|
if (!gameserver && !(options.organization && options.project && options.environment)) {
|
|
111
138
|
throw new Error('Could not determine a deployment to fetch the AWS credentials from. You need to specify either a gameserver or an organization, project, and environment');
|
|
112
139
|
}
|
|
113
|
-
const stackApi = new StackAPI(tokens.
|
|
140
|
+
const stackApi = new StackAPI(tokens.access_token);
|
|
114
141
|
if (options.stackApi) {
|
|
115
142
|
stackApi.stack_api_base_uri = options.stackApi;
|
|
116
143
|
}
|
|
@@ -151,7 +178,7 @@ program.command('get-environment')
|
|
|
151
178
|
if (!gameserver && !(options.organization && options.project && options.environment)) {
|
|
152
179
|
throw new Error('Could not determine a deployment to fetch environment details from. You need to specify either a gameserver or an organization, project, and environment');
|
|
153
180
|
}
|
|
154
|
-
const stackApi = new StackAPI(tokens.
|
|
181
|
+
const stackApi = new StackAPI(tokens.access_token);
|
|
155
182
|
if (options.stackApi) {
|
|
156
183
|
stackApi.stack_api_base_uri = options.stackApi;
|
|
157
184
|
}
|
|
@@ -166,5 +193,23 @@ program.command('get-environment')
|
|
|
166
193
|
exit(1);
|
|
167
194
|
}
|
|
168
195
|
});
|
|
196
|
+
program.command('check-deployment')
|
|
197
|
+
.description('[experimental] check that a game server was successfully deployed, or print out useful error messages in case of failure')
|
|
198
|
+
.argument('[namespace]', 'kubernetes namespace of the deployment')
|
|
199
|
+
.action(async (namespace) => {
|
|
200
|
+
setLogLevel(0);
|
|
201
|
+
try {
|
|
202
|
+
if (!namespace) {
|
|
203
|
+
throw new Error('Must specify value for argument "namespace"');
|
|
204
|
+
}
|
|
205
|
+
// Run the checks and exit with success/failure exitCode depending on result
|
|
206
|
+
const exitCode = await checkGameServerDeployment(namespace);
|
|
207
|
+
exit(exitCode);
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
console.error(`Failed to check deployment status: ${error}`);
|
|
211
|
+
exit(1);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
169
214
|
void program.parseAsync();
|
|
170
215
|
//# sourceMappingURL=index.js.map
|