import { SendMailOptions } from 'nodemailer' ; import { SmtpOptions } from 'nodemailer-smtp-transport' ; import { CliArgumentsInterface , ConfigInterface , DockerInitOptions , PromOptions , InfluxOptions , } from './interfaces' ; export class Config implements ConfigInterface { runOnce : boolean ; cleanImage : boolean ; dockerHost : string | undefined ; watchInterval : number | undefined ; containersToMonitor : string [ ] | null ; containersToIgnore : string [ ] | null ; repoUser : string | null ; repoPass : string | null ; emailConfig ? : SmtpOptions ; emailOptions ? : SendMailOptions ; webhookUrls ? : string [ ] | undefined ; pushoverAppToken ? : string | undefined ; pushoverUserKey ? : string | undefined ; pushoverDevice ? : string | undefined ; telegramBotToken ? : string | undefined ; telegramChatId ? : string | undefined ; prometheusConfig ? : PromOptions ; influxConfig ? : InfluxOptions ; privateRegistry ? : string | undefined ; semverUpdate ? : boolean ; allowMajorUpdates ? : boolean ; allowPatchOnly ? : boolean ; allowPrereleaseUpdates ? : boolean ; constructor ( { runonce , cleanup , host , interval , monitor , ignore , user , pass , smtpHost , smtpPort , smtpUsername , smtpPassword , smtpSender , smtpRecipients , webhookUrls , pushoverToken , pushoverUser , pushoverDevice , telegramToken , telegramChat , prometheusHost , prometheusPort , influxUrl , influxToken , influxOrg , influxBucket , privateRegistry , semverUpdate , allowMajorUpdates , allowPatchOnly , allowPrereleaseUpdates , } ) { const toMonitor = monitor ? parseContainersToFilterInput ( monitor ) : [ ] ; const toIgnore = ignore ? parseContainersToFilterInput ( ignore ) : [ ] ; this . semverUpdate = semverUpdate ; this . allowMajorUpdates = allowMajorUpdates ; this . allowPatchOnly = allowPatchOnly ; this . allowPrereleaseUpdates = allowPrereleaseUpdates ; this . runOnce = runonce ; this . cleanImage = cleanup ; this . dockerHost = host ; this . watchInterval = interval ; this . containersToMonitor = toMonitor ; this . containersToIgnore = toIgnore ; this . repoUser = user || process . env . REPO_USER ; this . repoPass = pass || process . env . REPO_PASS ; this . privateRegistry = privateRegistry || process . env . PRIVATE_REGISTRY ; this . emailConfig = { host : smtpHost || process . env . SMTP_HOST , port : Number ( smtpPort || process . env . SMTP_PORT ) , secure : false , auth : { user : smtpUsername || process . env . SMTP_USER , pass : smtpPassword || process . env . SMTP_PASS , } , tls : { rejectUnauthorized : false , } , } ; this . emailOptions = { from : smtpSender , to : parseEmailRecipients ( smtpRecipients ) , } ; this . webhookUrls = parseWebhookUrlsInput ( webhookUrls , ! ! ( pushoverUser && pushoverToken && pushoverDevice ) , ! ! ( telegramToken && telegramChat ) ) ; this . pushoverAppToken = pushoverToken ; this . pushoverUserKey = pushoverUser ; this . pushoverDevice = pushoverDevice ; this . telegramBotToken = telegramToken ; this . telegramChatId = telegramChat ; this . prometheusConfig = null ; if ( validHttpUrl ( prometheusHost || process . env . PROM_HOST ) && ( prometheusPort || process . env . PROM_PORT ) ) { this . prometheusConfig = { host : prometheusHost || process . env . PROM_HOST , port : Number ( prometheusPort || process . env . PROM_PORT ) , } ; } if ( validHttpUrl ( influxUrl || process . env . INFLUX_URL ) && ( influxToken || process . env . INFLUX_TOKEN ) ) { this . influxConfig = { url : influxUrl || process . env . INFLUX_URL , token : influxToken || process . env . INFLUX_TOKEN , org : influxOrg || process . env . INFLUX_ORG , bucket : influxBucket || process . env . INFLUX_BUCKET , } ; } } return { * } memberof return { DockerInitOptions } extractDockerConfig ( ) { const isValidUri = checkValidTCPUri ( this . dockerHost ) ; const dockerConfig = isValidUri ? { host : this . dockerHost } : { socketPath : '/var/run/docker.sock' } ; return dockerConfig ; } } param { ( string | undefined ) } containerstoMonitor return { ( string [ ] | undefined ) } function parseContainersToFilterInput ( containerstoFilter ) { if ( ! containerstoFilter ) return undefined ; return containerstoFilter . split ( ',' ) ; } function parseEmailRecipients ( recipients ) { if ( ! recipients ) return undefined ; return recipients . split ( ',' ) ; } function parseWebhookUrlsInput ( webhookUrls , broadcastToPuhshover , broadcastToTelegram ) { if ( ! webhookUrls ) return undefined ; const parsedUrlsInput = webhookUrls . split ( ',' ) ; if ( broadcastToPuhshover ) parsedUrlsInput . push ( 'https://api.pushover.net/1/messages.json' ) ; if ( broadcastToTelegram ) parsedUrlsInput . push ( 'https://api.telegram.org/bot' ) ; return parsedUrlsInput . filter ( ( url ) => validHttpUrl ( url ) ) ; } param { string } hostUri return { boolean } function checkValidTCPUri ( hostUri ) { const regexForValidTCP = new RegExp ( '^(?:tcp)s?://(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\\.)+(?:[A-Z]{2,6}\\.?|[A-Z0-9-]{2,}\\.?)|localhost|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})(?::\\d+)?(?:/?|[/?]\\S+)$' ) ; return regexForValidTCP . test ( hostUri ) ; } const validHttpUrl = ( url ) => { const pattern = new RegExp ( '^(https?:\\/\\/)?' + '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + '((\\d{1,3}\\.){3}\\d{1,3}))' + '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + '(\\?[;&a-z\\d%_.~+=-]*)?' + '(\\#[-a-z\\d_]*)?$' , 'i' ) ; return ! ! pattern . test ( url ) ; } ;