fix: load PM2 from node_modules

This commit is contained in:
Eli Bosley
2024-11-04 15:59:40 -05:00
parent 50376a0d66
commit 2eaf175515
11 changed files with 65 additions and 53 deletions

View File

@@ -21,4 +21,4 @@ dynamicRemoteAccessType="DISABLED"
[upc]
apikey="unupc_fab6ff6ffe51040595c6d9ffb63a353ba16cc2ad7d93f813a2e80a5810"
[connectionStatus]
minigraph="PRE_INIT"
minigraph="ERROR_RETRYING"

View File

@@ -9,6 +9,7 @@
"scripts": {
"build:docker": "./scripts/dc.sh run --rm builder",
"build": "vite build",
"postbuild": "chmod +x dist/main.js && chmod +x dist/cli.js",
"build-and-pack": "./scripts/build.mjs",
"codegen": "MOTHERSHIP_GRAPHQL_LINK='https://staging.mothership.unraid.net/ws' graphql-codegen --config codegen.yml -r dotenv/config './.env.staging'",
"codegen:watch": "DOTENV_CONFIG_PATH='./.env.staging' graphql-codegen --config codegen.yml --watch -r dotenv/config",

View File

@@ -64,7 +64,6 @@ try {
await $`npm prune --omit=dev`;
await $`npm install --omit=dev`;
await $`chmod +x ./dist/cli.js`;
// Now we'll pack everything in the pre-pack directory
await $`tar -czf ../unraid-api-${deploymentVersion}.tgz .`;

View File

@@ -1,3 +1,4 @@
import { PM2_PATH } from '@app/consts';
import { cliLogger } from '@app/core/log';
import { API_VERSION } from '@app/environment';
import { execSync } from 'child_process';
@@ -7,6 +8,6 @@ import { execSync } from 'child_process';
export const start = async () => {
cliLogger.info('Starting unraid-api@v%s', API_VERSION);
execSync(`pm2 start ecosystem.config.json --update-env`, { env: process.env, stdio: 'inherit' });
execSync(`${PM2_PATH} start ecosystem.config.json --update-env`, { env: process.env, stdio: 'inherit' });
// Start API
};

View File

@@ -1,5 +1,6 @@
import { PM2_PATH } from '@app/consts';
import { execSync } from 'child_process';
export const stop = async () => {
execSync('pm2 stop unraid-api', { stdio: 'inherit' });
execSync(`${PM2_PATH} stop unraid-api`, { stdio: 'inherit' });
};

View File

@@ -1,5 +1,6 @@
import { PORT } from '@app/environment';
import { type JSONWebKeySet } from 'jose';
import { join } from 'path';
export const getInternalApiAddress = (isHttp = true, nginxPort = 80) => {
const envPort = PORT;
@@ -80,3 +81,5 @@ export const KEYSERVER_VALIDATION_ENDPOINT =
/** Set the max retries for the GraphQL Client */
export const MAX_RETRIES_FOR_LINEAR_BACKOFF = 100;
export const PM2_PATH = join(import.meta.dirname, '../../', 'node_modules', '.bin', 'pm2');

View File

@@ -1,6 +1,8 @@
import { config } from 'dotenv';
export const env = process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test'
? config({ debug: true, path: `./.env.${process.env.NODE_ENV}` }) : config({
path: '/usr/local/unraid-api/.env',
});
export const env =
process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test'
? config({ debug: true, path: `./.env.${process.env.NODE_ENV}` })
: config({
path: '/usr/local/unraid-api/.env',
});

View File

@@ -3,7 +3,7 @@ import { version } from 'package.json';
export const API_VERSION =
process.env.npm_package_version ?? version ?? new Error('API_VERSION not set');
export const NODE_ENV = process.env.NODE_ENV as 'development' | 'test' | 'staging' | 'production';
export const NODE_ENV = process.env.NODE_ENV as 'development' | 'test' | 'staging' | 'production' ?? 'production';
export const environment = {
IS_MAIN_PROCESS: false,
};

View File

@@ -1,7 +1,5 @@
import 'reflect-metadata';
import 'global-agent/bootstrap.js';
// Configure dotenv
import 'dotenv/config';
import http from 'http';
import https from 'https';
import CacheableLookup from 'cacheable-lookup';

View File

@@ -9,49 +9,55 @@ import { isApiKeyValid } from '@app/store/getters/index';
import { isApiKeyCorrectLength } from '@app/mothership/api-key/is-api-key-correct-length';
import { NODE_ENV } from '@app/environment';
export const apiKeyCheckJob = async (getState: () => RootState, dispatch: AppDispatch, count?: number): Promise<boolean> => {
keyServerLogger.debug('Running keyserver validation number: %s', count);
const state = getState();
if (state.apiKey.status === API_KEY_STATUS.NO_API_KEY) {
// Stop Job
return false;
}
export const apiKeyCheckJob = async (
getState: () => RootState,
dispatch: AppDispatch,
count?: number
): Promise<boolean> => {
keyServerLogger.debug('Running keyserver validation number: %s', count);
const state = getState();
if (state.apiKey.status === API_KEY_STATUS.NO_API_KEY) {
// Stop Job
return false;
}
if (isAPIStateDataFullyLoaded(state)) {
if (isApiKeyValid(state)) {
return true;
}
if (isAPIStateDataFullyLoaded(state)) {
if (isApiKeyValid(state)) {
return true;
}
if (!isApiKeyCorrectLength(state.config.remote.apikey)) {
keyServerLogger.error('API Key has invalid length, logging you out.');
await dispatch(logoutUser({ reason: 'API Key has invalid length' }));
return false;
}
if (!isApiKeyCorrectLength(state.config.remote.apikey)) {
keyServerLogger.error('API Key has invalid length, logging you out.');
await dispatch(logoutUser({ reason: 'API Key has invalid length' }));
return false;
}
if (['development'].includes(NODE_ENV)) {
keyServerLogger.debug('In dev environment, marking API Key as Valid');
dispatch(setApiKeyState(API_KEY_STATUS.API_KEY_VALID));
return true;
}
if (['development'].includes(NODE_ENV)) {
keyServerLogger.debug('In dev environment, marking API Key as Valid');
dispatch(setApiKeyState(API_KEY_STATUS.API_KEY_VALID));
return true;
}
const validationResponse = await validateApiKeyWithKeyServer({ flashGuid: state.emhttp.var.flashGuid, apiKey: state.config.remote.apikey });
switch (validationResponse) {
case API_KEY_STATUS.API_KEY_VALID:
keyServerLogger.info('Stopping API Key Job as Keyserver Marked API Key Valid');
dispatch(setApiKeyState(validationResponse));
return true;
case API_KEY_STATUS.API_KEY_INVALID:
await dispatch(logoutUser({ reason: 'Invalid API Key' }));
return false;
default:
keyServerLogger.info('Request failed with status:', validationResponse);
dispatch(setApiKeyState(validationResponse));
throw new Error('Keyserver Failure, must retry');
}
} else {
keyServerLogger.warn('State Data Has Not Fully Loaded, this should not be possible');
dispatch(setApiKeyState(API_KEY_STATUS.NO_API_KEY));
return false;
}
const validationResponse = await validateApiKeyWithKeyServer({
flashGuid: state.emhttp.var.flashGuid,
apiKey: state.config.remote.apikey,
});
switch (validationResponse) {
case API_KEY_STATUS.API_KEY_VALID:
keyServerLogger.info('Stopping API Key Job as Keyserver Marked API Key Valid');
dispatch(setApiKeyState(validationResponse));
return true;
case API_KEY_STATUS.API_KEY_INVALID:
await dispatch(logoutUser({ reason: 'Invalid API Key' }));
return false;
default:
keyServerLogger.info('Request failed with status:', validationResponse);
dispatch(setApiKeyState(validationResponse));
throw new Error('Keyserver Failure, must retry');
}
} else {
keyServerLogger.warn('State Data Has Not Fully Loaded, this should not be possible');
dispatch(setApiKeyState(API_KEY_STATUS.NO_API_KEY));
return false;
}
};

View File

@@ -24,6 +24,7 @@ import { safelySerializeObjectToIni } from '@app/core/utils/files/safe-ini-seria
import { PUBSUB_CHANNEL, pubsub } from '@app/core/pubsub';
import { isEqual } from 'lodash-es';
import { setupRemoteAccessThunk } from '@app/store/actions/setup-remote-access';
import { NODE_ENV } from '@app/environment';
export type SliceState = {
status: FileLoadStatus;
@@ -32,7 +33,7 @@ export type SliceState = {
export const initialState: SliceState = {
status: FileLoadStatus.UNLOADED,
nodeEnv: process.env.NODE_ENV ?? 'production',
nodeEnv: NODE_ENV,
remote: {
'2Fa': '',
wanaccess: '',