Mount + proper init + sys/proc/tmp

This commit is contained in:
Marc
2021-12-01 21:26:11 +01:00
parent eedb9c067f
commit fbdabd66a5
16 changed files with 351 additions and 17 deletions
+84
View File
@@ -0,0 +1,84 @@
//@ts-check
const fs = require('fs')
const path = require('path')
/**
* @typedef {{ name: string, before: string[], after: string[], init(): undefined | Promise<undefined> }} InitScript
*/
async function init() {
const initdFiles = fs.readdirSync('/etc/init.d/')
const initdScripts = initdFiles.filter( (v) => v.endsWith('.js'))
/**
* @type { Array<String> }
*/
const finishedScripts = []
/**
* @type { Array<InitScript> }
*/
const scripts = []
for(const script of initdScripts) {
const scriptCandidate = require(path.join('/etc/init.d/', script))
if(scriptCandidate?.before && scriptCandidate?.after && scriptCandidate?.init)
scripts.push({ ...scriptCandidate, name: script})
else
console.error(`Invalid(missing exports) script error :${script}`)
}
const befores = processBefores(scripts)
let i = 0
while(scripts.length) {
const s = scripts[i % scripts.length]
i += 1
if(isSubSet(s.after, finishedScripts) && ( !befores[s.name] || isSubSet(befores[s.name], finishedScripts) )) {
//on supprime ce service
scripts.splice(scripts.indexOf(s))
const result = s.init()
if(typeof result?.then === 'function') {
await result
}
finishedScripts.push(s.name)
}
}
}
/**
* @template T
* @param {Array<T>} arraya
* @param {Array<T>} arrayb
*/
function isSubSet(arraya, arrayb) {
for(const a of arraya) {
if(!arrayb.includes(a)) {
return false
}
}
return true
}
/**
*
* @param {Array<InitScript>} scripts
* @returns {{ [key: string]: string[] }}
*/
function processBefores(scripts) {
/**
* @type {{ [key: string]: string[] }}
*/
const result = {}
for(const script of scripts) {
for(const before of script.before){
if(! result[before]) result[before] = [script.name]
else result[before].push(script.name)
}
}
return result
}
module.exports = init