Conf files should be treated as a dict, file names are unique

This commit is contained in:
MattIPv4 2020-06-04 14:58:41 +01:00
parent 7444fa6f77
commit bc04209423
3 changed files with 39 additions and 35 deletions

View file

@ -38,59 +38,56 @@ import drupalConf from './conf/drupal.conf';
import magentoConf from './conf/magento.conf'; import magentoConf from './conf/magento.conf';
export default (domains, global) => { export default (domains, global) => {
const files = []; const files = {};
// Base nginx config // Base nginx config
files.push(['nginx.conf', toConf(nginxConf(domains, global))]); files['nginx.conf'] = toConf(nginxConf(domains, global));
// Modularised configs // Modularised configs
if (global.tools.modularizedStructure.computed) { if (global.tools.modularizedStructure.computed) {
// Domain config // Domain config
for (const domain of domains) { for (const domain of domains) {
files.push([ files[`sites-${global.tools.symlinkVhost.computed ? 'available' : 'enabled'}/${domain.server.domain.computed}.conf`] = toConf(websiteConf(domain, domains, global));
`sites-${global.tools.symlinkVhost.computed ? 'available' : 'enabled'}/${domain.server.domain.computed}.conf`,
toConf(websiteConf(domain, domains, global)),
]);
} }
// Let's encrypt // Let's encrypt
if (domains.some(d => d.https.certType.computed === 'letsEncrypt')) if (domains.some(d => d.https.certType.computed === 'letsEncrypt'))
files.push(['nginxconfig.io/letsencrypt.conf', toConf(letsEncryptConf(global))]); files['nginxconfig.io/letsencrypt.conf'] = toConf(letsEncryptConf(global));
// Security // Security
files.push(['nginxconfig.io/security.conf', toConf(securityConf(domains, global))]); files['nginxconfig.io/security.conf'] = toConf(securityConf(domains, global));
// General // General
files.push(['nginxconfig.io/general.conf', toConf(generalConf(domains, global))]); files['nginxconfig.io/general.conf'] = toConf(generalConf(domains, global));
// PHP // PHP
if (domains.some(d => d.php.php.computed)) if (domains.some(d => d.php.php.computed))
files.push(['nginxconfig.io/php_fastcgi.conf', toConf(phpConf(domains, global))]); files['nginxconfig.io/php_fastcgi.conf'] = toConf(phpConf(domains, global));
// Python // Python
if (domains.some(d => d.python.python.computed)) if (domains.some(d => d.python.python.computed))
files.push(['nginxconfig.io/python_uwsgi.conf', toConf(pythonConf(global))]); files['nginxconfig.io/python_uwsgi.conf'] = toConf(pythonConf(global));
// Reverse proxy // Reverse proxy
if (domains.some(d => d.reverseProxy.reverseProxy.computed)) if (domains.some(d => d.reverseProxy.reverseProxy.computed))
files.push(['nginxconfig.io/proxy.conf', toConf(proxyConf())]); files['nginxconfig.io/proxy.conf'] = toConf(proxyConf());
// WordPress // WordPress
if (domains.some(d => d.php.wordPressRules.computed)) if (domains.some(d => d.php.wordPressRules.computed))
files.push(['nginxconfig.io/wordpress.conf', toConf(wordPressConf(global))]); files['nginxconfig.io/wordpress.conf'] = toConf(wordPressConf(global));
// Drupal // Drupal
if (domains.some(d => d.php.drupalRules.computed)) if (domains.some(d => d.php.drupalRules.computed))
files.push(['nginxconfig.io/drupal.conf', toConf(drupalConf(global))]); files['nginxconfig.io/drupal.conf'] = toConf(drupalConf(global));
// Drupal // Drupal
if (domains.some(d => d.php.magentoRules.computed)) if (domains.some(d => d.php.magentoRules.computed))
files.push(['nginxconfig.io/magento.conf', toConf(magentoConf())]); files['nginxconfig.io/magento.conf'] = toConf(magentoConf());
} else { } else {
// PHP // PHP
if (domains.some(d => d.php.wordPressRules.computed)) if (domains.some(d => d.php.wordPressRules.computed))
files.push(['nginxconfig.io/php_fastcgi.conf', toConf(phpConf(domains, global))]); files['nginxconfig.io/php_fastcgi.conf'] = toConf(phpConf(domains, global));
} }
return files; return files;

View file

@ -80,11 +80,11 @@ THE SOFTWARE.
<div :class="`column ${splitColumn ? 'is-half' : 'is-full'} is-full-mobile is-full-tablet`"> <div :class="`column ${splitColumn ? 'is-half' : 'is-full'} is-full-mobile is-full-tablet`">
<h2>{{ i18n.templates.app.configFiles }}</h2> <h2>{{ i18n.templates.app.configFiles }}</h2>
<div ref="files" class="columns is-multiline"> <div ref="files" class="columns is-multiline">
<NginxPrism v-for="(conf, i) in confFilesOutput" <NginxPrism v-for="(confContents, confName) in confFilesOutput"
:key="`${conf[0]}-${i}-${hash(conf[1])}`" :key="`${confName}-${hash(confContents)}`"
:name="`${nginxDir}/${conf[0]}`" :name="`${nginxDir}/${confName}`"
:conf="conf[1]" :conf="confContents"
:half="confFilesOutput.length > 1 && !splitColumn" :half="Object.keys(confFilesOutput).length > 1 && !splitColumn"
></NginxPrism> ></NginxPrism>
</div> </div>
</div> </div>
@ -132,8 +132,8 @@ THE SOFTWARE.
ready: false, ready: false,
splitColumn: false, splitColumn: false,
confWatcherWaiting: false, confWatcherWaiting: false,
confFilesPrevious: [], confFilesPrevious: {},
confFilesOutput: [], confFilesOutput: {},
}; };
}, },
computed: { computed: {
@ -222,17 +222,21 @@ THE SOFTWARE.
this.$nextTick(() => this.checkChange(this.confFiles)); this.$nextTick(() => this.checkChange(this.confFiles));
}, },
updateDiff(newConf, oldConf) { updateDiff(newConf, oldConf) {
const newFiles = {};
// Work through each file in the new config // Work through each file in the new config
const newFiles = []; for (const newFileName in newConf) {
for (const [newFileName, newFileConf] of newConf) { if (!Object.prototype.hasOwnProperty.call(newConf, newFileName)) continue;
const newFileConf = newConf[newFileName];
// If a file with the same name existed before, diff! // If a file with the same name existed before, diff!
// TODO: Handle diffing across file renames (eg. when a user changes a domain name) // TODO: Handle diffing across file renames (eg. when a user changes a domain name)
const old = oldConf && oldConf.find(c => c[0] === newFileName); const old = oldConf && oldConf[newFileName];
if (old && this.hash(old[1]) !== this.hash(newFileConf)) { if (old && this.hash(old) !== this.hash(newFileConf)) {
console.info(`Diffing ${newFileName}...`); console.info(`Diffing ${newFileName}...`);
// Get the diff // Get the diff
const diff = diffLines(old[1], newFileConf); const diff = diffLines(old, newFileConf);
// Wrap additions in <mark>, drop removals // Wrap additions in <mark>, drop removals
const output = diff.map((change, index, array) => { const output = diff.map((change, index, array) => {
@ -263,13 +267,15 @@ THE SOFTWARE.
}).join(''); }).join('');
// Store // Store
newFiles.push([newFileName, output]); newFiles[newFileName] = output;
continue; continue;
} }
// No diffing, just store the new file // No diffing, just store the new file
newFiles.push([newFileName, newFileConf]); newFiles[newFileName] = newFileConf;
} }
// Store
this.$data.confFilesOutput = newFiles; this.$data.confFilesOutput = newFiles;
this.$nextTick(() => this.$data.confWatcherWaiting = false); this.$nextTick(() => this.$data.confWatcherWaiting = false);
}, },

View file

@ -114,15 +114,16 @@ THE SOFTWARE.
const tar = pack(); const tar = pack();
// Add all our config files to the tar // Add all our config files to the tar
for (const conf of this.$props.data.confFiles) { for (const fileName in this.$props.data.confFiles) {
tar.entry({ name: conf[0] }, conf[1]); if (!Object.prototype.hasOwnProperty.call(this.$props.data.confFiles, fileName)) continue;
tar.entry({ name: fileName }, this.$props.data.confFiles[fileName]);
// If symlinks are enabled and this is in sites-available, symlink to sites-enabled // If symlinks are enabled and this is in sites-available, symlink to sites-enabled
if (this.$props.data.global.tools.symlinkVhost.computed && conf[0].startsWith('sites-available')) if (this.$props.data.global.tools.symlinkVhost.computed && fileName.startsWith('sites-available'))
tar.entry({ tar.entry({
name: conf[0].replace(/^sites-available/, 'sites-enabled'), name: fileName.replace(/^sites-available/, 'sites-enabled'),
type: 'symlink', type: 'symlink',
linkname: `../${conf[0]}`, linkname: `../${fileName}`,
}); });
} }