feat: migrate old logging to new

pull/399/head
Kobi Meirson 2022-11-01 12:45:00 +02:00
parent b4fef1ab39
commit e02f20d993
No known key found for this signature in database
GPG Key ID: 5D66F732B037CDE1
7 changed files with 109 additions and 23 deletions

View File

@ -227,6 +227,7 @@ THE SOFTWARE.
import ExternalLink from 'do-vue/src/templates/external_link';
import delegatedFromDefaults from '../../util/delegated_from_defaults';
import computedFromDefaults from '../../util/computed_from_defaults';
import { serverDomainDefault } from '../../util/defaults';
import PrettyCheck from '../inputs/checkbox';
import PrettyRadio from '../inputs/radio';
@ -269,7 +270,7 @@ THE SOFTWARE.
},
letsEncryptEmail: {
default: '',
computed: 'info@example.com', // No default value, but a default computed
computed: `info@${serverDomainDefault}`, // No default value, but a default computed
enabled: true,
},
sslCertificate: {

View File

@ -44,7 +44,7 @@ THE SOFTWARE.
v-model="accessLogPath"
class="input"
type="text"
:placeholder="`/var/log/nginx/${$parent.$props.data.server.domain.computed}.access.log`"
:placeholder="$props.data.accessLogPath.default"
/>
</div>
</div>
@ -86,7 +86,7 @@ THE SOFTWARE.
v-model="errorLogPath"
class="input"
type="text"
:placeholder="`/var/log/nginx/${$parent.$props.data.server.domain.computed}.error.log`"
:placeholder="$props.data.errorLogPath.default"
/>
</div>
</div>
@ -119,7 +119,7 @@ THE SOFTWARE.
<script>
import delegatedFromDefaults from '../../util/delegated_from_defaults';
import computedFromDefaults from '../../util/computed_from_defaults';
import { accessLogParamsDefault, errorLogLevelDefault, errorLogLevelOptions } from '../../util/logging';
import { accessLogPathDefault, accessLogParamsDefault, errorLogPathDefault, errorLogLevelDefault, errorLogLevelOptions } from '../../util/logging';
import PrettyCheck from '../inputs/checkbox';
import PrettyRadio from '../inputs/radio';
@ -129,7 +129,7 @@ THE SOFTWARE.
enabled: true,
},
accessLogPath: {
default: '',
default: accessLogPathDefault,
enabled: true,
},
accessLogParameters: {
@ -141,7 +141,7 @@ THE SOFTWARE.
enabled: true,
},
errorLogPath: {
default: '',
default: errorLogPathDefault,
enabled: true,
},
errorLogLevel: {
@ -164,18 +164,5 @@ THE SOFTWARE.
data: Object, // Data delegated back to us from parent
},
computed: computedFromDefaults(defaults, 'logging'), // Getters & setters for the delegated data
watch: {
'$parent.$props.data.server.domain': {
handler(data) {
if (!this.$props.data.accessLogPath.value.trim()) {
this.$props.data.accessLogPath.computed = `/var/log/nginx/${data.computed}.access.log`;
}
if (!this.$props.data.errorLogPath.value.trim()) {
this.$props.data.errorLogPath.computed = `/var/log/nginx/${data.computed}.error.log`;
}
},
deep: true,
},
},
};
</script>

View File

@ -147,16 +147,17 @@ THE SOFTWARE.
<script>
import delegatedFromDefaults from '../../util/delegated_from_defaults';
import computedFromDefaults from '../../util/computed_from_defaults';
import { serverDomainDefault } from '../../util/defaults';
import PrettyCheck from '../inputs/checkbox';
const defaults = {
domain: {
default: 'example.com',
default: serverDomainDefault,
enabled: true,
},
path: {
default: '',
computed: '/var/www/example.com', // No default value, but a default computed
computed: `/var/www/${serverDomainDefault}`, // No default value, but a default computed
enabled: true,
},
documentRoot: {

View File

@ -166,7 +166,7 @@ THE SOFTWARE.
<script>
import delegatedFromDefaults from '../../util/delegated_from_defaults';
import computedFromDefaults from '../../util/computed_from_defaults';
import { errorLogLevelDefault, errorLogLevelOptions } from '../../util/logging';
import { errorLogPathDefault, errorLogLevelDefault, errorLogLevelOptions } from '../../util/logging';
import PrettyCheck from '../inputs/checkbox';
import PrettyRadio from '../inputs/radio';
@ -176,7 +176,7 @@ THE SOFTWARE.
enabled: true,
},
errorLogPath: {
default: '/var/log/nginx/error.log',
default: errorLogPathDefault,
enabled: true,
},
errorLogLevel: {

View File

@ -0,0 +1,27 @@
/*
Copyright 2022 DigitalOcean
This code is licensed under the MIT License.
You may obtain a copy of the License at
https://github.com/digitalocean/nginxconfig.io/blob/master/LICENSE or https://mit-license.org/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
export const serverDomainDefault = 'example.com';

View File

@ -43,7 +43,9 @@ export const getDomainErrorLog = (domain) => {
return `${path} ${domain.logging.errorLogLevel.computed}`;
};
export const accessLogPathDefault = '/var/log/nginx/access.log';
export const accessLogParamsDefault = 'buffer=512k flush=1m';
export const errorLogPathDefault = '/var/log/nginx/error.log';
export const errorLogLevelDefault = 'warn';
export const errorLogLevelOptions = Object.freeze(['debug', 'info', 'notice', 'warn', 'error', 'crit', 'alert', 'emerg']);

View File

@ -26,6 +26,72 @@ THE SOFTWARE.
import isObject from './is_object';
import deepMerge from './deep_merge';
import { accessLogPathDefault, accessLogParamsDefault, errorLogPathDefault, errorLogLevelDefault } from './logging';
import { serverDomainDefault } from './defaults';
// Migrate old logging settings to new ones
const migrateLogging = data => {
if (Object.keys(data).length === 0) {
// Not having anything to migrate, so skip this logic
}
const globalLogging = 'logging' in data.global && isObject(data.global.logging) ? data.global.logging : {};
// global access_log
const [globalAccessLogPath, ...globalAccessLogParameters] = (globalLogging.accessLog || accessLogPathDefault).split(' ');
const globalAccessLogEnabled =
!('accessLog' in globalLogging) || // accessLog was enabled by default and might not appear at all
(globalAccessLogPath !== '' && globalAccessLogPath !== 'off'); // *or* someone turned it off explicitly
// global error_log
const [globalErrorLogPath, ...globalErrorLogLevel] = (globalLogging.errorLog || `${errorLogPathDefault} ${errorLogLevelDefault}`).split(' ');
const globalErrorLogEnabled =
!('errorLog' in globalLogging) || // errorLog was enabled by default and might not appear at all
globalErrorLogPath !== '/dev/null'; // *or* someone turned it off explicitly
// set global access_log / error_log files for every domain UNLESS it was explicitly
// enabled for the domain
for (const key in data.domains) {
if (!Object.prototype.hasOwnProperty.call(data.domains, key)) continue;
const perDomainServer = 'server' in data.domains[key] && isObject(data.domains[key].server) ? data.domains[key].server : {
domain: serverDomainDefault,
};
const perDomainLogging = 'logging' in data.domains[key] && isObject(data.domains[key].logging) ? data.domains[key].logging : {};
// access_log
let accessLogEnabled = globalAccessLogEnabled,
accessLogPath = globalAccessLogPath;
const accessLogParameters = globalAccessLogParameters.join(' ') || accessLogParamsDefault;
const perDomainAccessLogEnabled = !!perDomainLogging.accessLog;
if (perDomainAccessLogEnabled) {
accessLogEnabled = true;
accessLogPath = `/var/log/nginx/${perDomainServer.domain}.access.log`;
}
// error_log
let errorLogEnabled = globalErrorLogEnabled,
errorLogPath = globalErrorLogPath;
const errorLogLevel = globalErrorLogLevel.join(' ') || errorLogLevelDefault;
const perDomainErrorLogEnabled = !!perDomainLogging.errorLog;
if (perDomainErrorLogEnabled) {
errorLogEnabled = true;
errorLogPath = `/var/log/nginx/${perDomainServer.domain}.error.log`;
}
data.domains[key].logging = {
...perDomainLogging,
accessLogEnabled,
accessLogPath,
accessLogParameters,
errorLogEnabled,
errorLogPath,
errorLogLevel,
};
}
};
// Handle converting the old query format to our new query params
export default data => {
@ -68,4 +134,6 @@ export default data => {
deepMerge(data.domains[key], mappedData);
}
}
migrateLogging(data);
};