-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: import from old config file on each startup, as an editable over…
…rides
- Loading branch information
Showing
5 changed files
with
127 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
# | ||
# Since v1.7.0 of satellite this is no longer the main config file. | ||
# It is read at startup to import configuration to the main config file. | ||
# After it has been read, it gets reset back to defaults. | ||
# | ||
# More options are available in the web interface. | ||
# | ||
|
||
# Set this to the ip address or hostname of your companion installation | ||
# examples: | ||
# - COMPANION_IP=192.168.100.1 | ||
# - COMPANION_IP=companion.example.org | ||
COMPANION_IP=127.0.0.1 | ||
# COMPANION_IP=127.0.0.1 | ||
|
||
# If you are connecting through a router or firewall which has remapped the port, you will need to change that here to match | ||
COMPANION_PORT=16622 | ||
# COMPANION_PORT=16622 | ||
|
||
# Port for the REST server (0 to disable) | ||
REST_PORT=9999 | ||
# REST_PORT=9999 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* This is a small pre-launch step that gets run on SatellitePi. | ||
* It's purpose is to import user defined overrides from the 'boot' partition | ||
* Note: This gets run as root! | ||
*/ | ||
|
||
import { stat, readFile, copyFile, chown } from 'fs/promises' | ||
import { openHeadlessConfig } from './config' | ||
import path from 'path' | ||
|
||
const configFilePath = process.argv[2] | ||
if (!configFilePath) throw new Error(`Missing config file path parameter`) | ||
|
||
const appConfig = openHeadlessConfig(configFilePath) | ||
|
||
// Ensure the satellite user owns the file. This is a bit dodgey guessing the ids like this.. | ||
chown(appConfig.path, 1000, 1000).catch(() => null) | ||
|
||
const templatePathName = path.join(__dirname, '../pi-image/satellite-config') | ||
|
||
const importFromPaths = [ | ||
// Paths to search for a config file to 'import' from | ||
'/boot/satellite-config', | ||
'/boot/firmware/satellite-config', | ||
'/satellite-config', | ||
// templatePathName, // For testing | ||
] | ||
|
||
Promise.resolve() | ||
.then(async () => { | ||
for (const importPath of importFromPaths) { | ||
try { | ||
const fileStat = await stat(importPath) | ||
if (!fileStat.isFile()) throw new Error('Not a file') | ||
|
||
const fileContentStr = await readFile(importPath) | ||
const lines = fileContentStr.toString().split('\n') | ||
|
||
console.log(`Importing config from ${importPath}`) | ||
|
||
for (let line of lines) { | ||
line = line.trim() | ||
|
||
// Ignore any comments | ||
if (line.startsWith('#')) continue | ||
|
||
const splitIndex = line.indexOf('=') | ||
if (splitIndex == -1) continue | ||
|
||
const key = line.slice(0, splitIndex).trim() | ||
const value = line.slice(splitIndex + 1).trim() | ||
|
||
switch (key.toUpperCase()) { | ||
case 'COMPANION_IP': | ||
appConfig.set('remoteIp', value) | ||
break | ||
case 'COMPANION_PORT': { | ||
const port = Number(value) | ||
if (isNaN(port)) { | ||
console.log('COMPANION_PORT is not a number!') | ||
break | ||
} | ||
appConfig.set('remotePort', port) | ||
break | ||
} | ||
case 'REST_PORT': { | ||
const port = Number(value) | ||
if (isNaN(port)) { | ||
console.log('REST_PORT is not a number!') | ||
break | ||
} | ||
if (port > 0) { | ||
appConfig.set('restPort', port) | ||
appConfig.set('restEnabled', true) | ||
} else { | ||
appConfig.set('restEnabled', false) | ||
} | ||
break | ||
} | ||
default: | ||
console.log(`Unknown value: ${key}=${value}`) | ||
break | ||
} | ||
} | ||
|
||
if (templatePathName !== importPath) { | ||
await copyFile(templatePathName, importPath) | ||
} | ||
} catch (e: any) { | ||
if (e.code === 'ENOENT') continue | ||
// Failed, try next file | ||
console.log(`Unable to import from file "${importPath}"`, e) | ||
} | ||
} | ||
}) | ||
.catch(() => { | ||
// Ignore | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters