scripts/nginx_config_maker/index.mjs

107 lines
3.5 KiB
JavaScript
Raw Normal View History

2023-06-12 23:13:15 +02:00
/**
* setup domains
* @type {{framework: string, domain: string}[]}
*/
import fs from 'node-fs'
import { makeHostFileForWordpress } from './model.wordpress.mjs'
import { makeHostFileForSymfony } from './model.symfony.mjs'
2023-06-15 19:42:12 +02:00
import { makeHostFileForPhpPages } from './model.php-website.mjs'
2023-06-27 10:28:46 +02:00
import { domainsConfig } from './domains.mjs'
2023-06-27 13:22:03 +02:00
import { makeHostFileForNextcloud } from './model.nextcloud.mjs'
2023-06-12 23:33:07 +02:00
2023-06-12 23:21:42 +02:00
// autres frameworks:
// nextcloud: cloud.tykayn.fr
// vaultwarden: pass.cipherbliss.com
// mastodon: mastodon.cipherbliss.com
// peertube: peertube.cipherbliss.com
//
// sites statiques
// meltingpot.cipherbliss.com
// joinfediverse
// coussinet
2023-06-27 13:22:03 +02:00
let domainsForHostFile = []
let createFoldersScript = ''
2023-06-27 10:28:46 +02:00
2023-06-27 13:22:03 +02:00
let domainsSorted = domainsConfig.sort((a, b) => {
return a.domain - b.domain
2023-06-27 10:28:46 +02:00
})
for (let configDomain of domainsSorted) {
2023-06-12 23:13:15 +02:00
console.log('domaine :', configDomain.name)
2023-06-27 10:28:46 +02:00
domainsForHostFile.push(configDomain.domain)
2023-06-12 23:13:15 +02:00
let hostFile
if (configDomain.framework === 'wordpress') {
hostFile = makeHostFileForWordpress(configDomain)
}
if (configDomain.framework === 'symfony') {
hostFile = makeHostFileForSymfony(configDomain)
}
2023-06-27 13:22:03 +02:00
if (configDomain.framework === 'nextcloud') {
hostFile = makeHostFileForNextcloud(configDomain)
}
2023-06-12 23:41:37 +02:00
if (configDomain.framework === 'static') {
hostFile = makeHostFileForPhpPages(configDomain)
2023-06-27 13:22:03 +02:00
createFoldersScript += '# --------- \n'
createFoldersScript += 'mkdir -p /home/www/' + configDomain.domain + ' \n'
createFoldersScript += 'touch /home/www/' + configDomain.domain + '/index.html \n'
createFoldersScript += 'echo "coucou ' + configDomain.domain + '" > /home/www/' + configDomain.domain + '/index.html \n'
createFoldersScript += ' \n'
2023-06-27 13:46:47 +02:00
createFoldersScript += 'touch /etc/nginx/sites-available/' + configDomain.domain + '" \n'
createFoldersScript += 'rm /etc/nginx/sites-available/' + configDomain.domain + '" /etc/nginx/sites-enabled/' + configDomain.domain + '" \n'
createFoldersScript += 'ln -s /etc/nginx/sites-available/' + configDomain.domain + '" /etc/nginx/sites-enabled/ \n'
createFoldersScript += ' \n'
2023-06-27 13:22:03 +02:00
writeFile(configDomain.domain + '_direct.conf', hostFile.noContainerNginxConf)
}else{
2023-06-12 23:33:07 +02:00
writeFile(configDomain.domain + '_host.conf', hostFile.homeNginxConf)
writeFile(configDomain.domain + '_container.conf', hostFile.containerNginxConf)
2023-06-27 13:22:03 +02:00
}
let renewCertbotScript = domainsForHostFile.map(domain => {
if (domain.redirectToNoWWW || domain.includes('www.')) {
return ' certbot certonly -a webroot --webroot-path=/tmp/letsencrypt-auto -d ' + domain + ' -d ' + domain.replace('www.', '') + ' \n'
} else {
return ' certbot certonly -a webroot --webroot-path=/tmp/letsencrypt-auto -d ' + domain + ' \n'
}
}
)
let hostfileDomains = domainsForHostFile.sort((a, b) => {
return a - b
}).map(domain => ' 127.0.0.1\t' + domain + ' \n')
2023-06-27 13:46:47 +02:00
console.log('hostfileDomains', hostfileDomains.join('')
.replace(',', ''))
writeFile('certbot_renew.sh', '!#/bin/bash\n' + renewCertbotScript.join('').replace(',', '')+ '\n149.202.77.27 riseup\n' +
'::1 localhost ip6-localhost ip6-loopback\n' +
'# coussinet chatons\n' +
'ff02::1 ip6-allnodes\n' +
'ff02::2 ip6-allrouters\n' +
'# ici c\'est 149.202.77.27\n')
2023-06-27 10:28:46 +02:00
2023-06-27 13:22:03 +02:00
writeFile('folders_create.sh', createFoldersScript.replace(',', ''))
writeFile('hosts', hostfileDomains.join('')
.replace(',', ''))
2023-06-12 23:13:15 +02:00
}
function writeFile (fileName, fileContent) {
console.log('write file', fileName)
return fs.writeFile(
`./output/${fileName}`,
fileContent,
'utf8',
(err) => {
if (err) {
console.log(`Error writing file: ${err}`)
}
}
)
}