Skip to content

Commit 641801b

Browse files
committed
port setup_api.sh
1 parent 02e3f3e commit 641801b

File tree

5 files changed

+31
-61
lines changed

5 files changed

+31
-61
lines changed

api/src/environment.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export const MOTHERSHIP_GRAPHQL_LINK = process.env.MOTHERSHIP_GRAPHQL_LINK
9999
export const PM2_HOME = process.env.PM2_HOME ?? join(homedir(), '.pm2');
100100
export const PM2_PATH = join(import.meta.dirname, '../../', 'node_modules', 'pm2', 'bin', 'pm2');
101101
export const ECOSYSTEM_PATH = join(import.meta.dirname, '../../', 'ecosystem.config.json');
102+
export const LOGS_DIR = process.env.LOGS_DIR ?? '/var/log/unraid-api';
102103

103104
export const PATHS_CONFIG_MODULES =
104105
process.env.PATHS_CONFIG_MODULES ?? '/boot/config/plugins/dynamix.my.servers/configs';

api/src/unraid-api/cli/pm2.service.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Injectable } from '@nestjs/common';
22
import { existsSync } from 'node:fs';
3-
import { rm } from 'node:fs/promises';
3+
import { mkdir, rm } from 'node:fs/promises';
44
import { join } from 'node:path';
55

66
import type { Options, Result, ResultPromise } from 'execa';
77
import { execa, ExecaError } from 'execa';
88

9-
import { PM2_HOME, PM2_PATH } from '@app/environment.js';
9+
import { LOGS_DIR, PM2_HOME, PM2_PATH } from '@app/environment.js';
1010
import { LogService } from '@app/unraid-api/cli/log.service.js';
1111

1212
type CmdContext = Options & {
@@ -97,4 +97,11 @@ export class PM2Service {
9797
this.logger.trace('PM2 home directory does not exist.');
9898
}
9999
}
100+
101+
/**
102+
* Ensures that the dependencies necessary for PM2 to start and operate are present.
103+
*/
104+
async ensurePm2Dependencies() {
105+
await mkdir(LOGS_DIR, { recursive: true });
106+
}
100107
}

api/src/unraid-api/cli/start.command.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class StartCommand extends CommandRunner {
2020
}
2121

2222
async cleanupPM2State() {
23+
await this.pm2.ensurePm2Dependencies();
2324
await this.pm2.run({ tag: 'PM2 Stop' }, 'stop', ECOSYSTEM_PATH);
2425
await this.pm2.run({ tag: 'PM2 Update' }, 'update');
2526
await this.pm2.deleteDump();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
#!/bin/sh
22

3+
backup_file_if_exists() {
4+
if [ -f "$1" ]; then
5+
mv "$1" "$1.old"
6+
fi
7+
}
8+
9+
chmod 755 etc/rc.d/rc6.d/K*unraid-api
10+
chmod 755 etc/rc.d/rc6.d/K*flash-backup
11+
12+
chmod +x usr/local/unraid-api/dist/cli.js
13+
chmod +x usr/local/unraid-api/dist/main.js
14+
315
rm -rf usr/local/bin/unraid-api
416
ln -sf usr/local/unraid-api/dist/cli.js usr/local/bin/unraid-api
17+
# deprecated
18+
ln -sf usr/local/bin/unraid-api usr/local/sbin/unraid-api
19+
ln -sf usr/local/bin/unraid-api usr/bin/unraid-api
20+
21+
# By default, we want to overwrite the active api-specific .env configuration on every install.
22+
# We keep a backup in case a user needs to revert to their prior configuration.
23+
backup_file_if_exists usr/local/unraid-api/.env
24+
cp usr/local/unraid-api/.env.production usr/local/unraid-api/.env
525

626
# auto-generated actions from makepkg:

plugin/source/dynamix.unraid.net/usr/local/share/dynamix.unraid.net/install/scripts/setup_api.sh

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,65 +19,6 @@ else
1919
echo "Env file already exists"
2020
fi
2121

22-
# Create log directory (PM2 will not start without it)
23-
mkdir -p /var/log/unraid-api
24-
echo "Created log directory at /var/log/unraid-api"
25-
26-
# Create Symlinks for the Unraid API
27-
if [ -f "${API_BASE_DIR}/dist/cli.js" ]; then
28-
echo "Creating symlinks for unraid-api"
29-
ln -sf "${API_BASE_DIR}/dist/cli.js" "/usr/local/bin/unraid-api"
30-
ln -sf "/usr/local/bin/unraid-api" "/usr/local/sbin/unraid-api"
31-
ln -sf "/usr/local/bin/unraid-api" "/usr/bin/unraid-api"
32-
33-
# Verify symlinks were created
34-
if [ -L "/usr/local/bin/unraid-api" ]; then
35-
echo "Symlinks created successfully"
36-
else
37-
echo "ERROR: Failed to create symlinks"
38-
fi
39-
40-
# Make API scripts executable
41-
echo "Making API scripts executable"
42-
chmod +x "${API_BASE_DIR}/dist/cli.js"
43-
chmod +x "${API_BASE_DIR}/dist/main.js"
44-
echo "API scripts are now executable"
45-
else
46-
echo "ERROR: Source file ${API_BASE_DIR}/dist/cli.js does not exist"
47-
48-
# Check if the directory exists
49-
if [ -d "${API_BASE_DIR}" ]; then
50-
echo "API base directory exists"
51-
ls -la "${API_BASE_DIR}"
52-
53-
if [ -d "${API_BASE_DIR}/dist" ]; then
54-
echo "Dist directory exists"
55-
ls -la "${API_BASE_DIR}/dist"
56-
else
57-
echo "Dist directory does not exist"
58-
fi
59-
else
60-
echo "API base directory does not exist"
61-
fi
62-
fi
63-
64-
# Copy env file
65-
if [ -f "${API_BASE_DIR}/.env.production" ]; then
66-
echo "Copying .env.production to .env"
67-
cp "${API_BASE_DIR}/.env.production" "${API_BASE_DIR}/.env"
68-
else
69-
echo "ERROR: .env.production file not found"
70-
fi
71-
72-
# Ensure rc directories exist and scripts are executable
73-
echo "Ensuring shutdown scripts are executable"
74-
if [ -d "/etc/rc.d/rc6.d" ]; then
75-
chmod 755 /etc/rc.d/rc6.d/K*unraid-api 2>/dev/null
76-
chmod 755 /etc/rc.d/rc6.d/K*flash-backup 2>/dev/null
77-
else
78-
echo "Warning: rc6.d directory does not exist"
79-
fi
80-
8122
# Create symlink for rc0.d to rc6.d if needed
8223
if [ ! -L /etc/rc.d/rc0.d ] && [ ! -d /etc/rc.d/rc0.d ]; then
8324
echo "Creating symlink from /etc/rc.d/rc0.d to /etc/rc.d/rc6.d"

0 commit comments

Comments
 (0)