Browse Source

Always scale up but never down when matching scale on alias (#504)

master
Jarmo Isotalo 8 years ago
committed by Matheus Fernandes
parent
commit
a09abbcad4
  1. 110
      lib/alias.js

110
lib/alias.js

@ -176,9 +176,7 @@ module.exports = class Alias extends Now {
const code = body.error.code const code = body.error.code
if (code === 'cert_missing') { if (code === 'cert_missing') {
console.log( console.log(`> Provisioning certificate for ${chalk.underline(chalk.bold(alias))}`)
`> Provisioning certificate for ${chalk.underline(chalk.bold(alias))}`
)
try { try {
await this.createCert(alias) await this.createCert(alias)
@ -194,9 +192,7 @@ module.exports = class Alias extends Now {
} }
if (code === 'cert_expired') { if (code === 'cert_expired') {
console.log( console.log(`> Renewing certificate for ${chalk.underline(chalk.bold(alias))}`)
`> Renewing certificate for ${chalk.underline(chalk.bold(alias))}`
)
try { try {
await this.createCert(alias, { renew: true }) await this.createCert(alias, { renew: true })
@ -256,9 +252,7 @@ module.exports = class Alias extends Now {
console.log(err) console.log(err)
} }
} else { } else {
console.log( console.log(`Overwriting path alias with ${aliasDepl.rules.length} rule${aliasDepl.rules.length > 1 ? 's' : ''} to be a normal alias.`)
`Overwriting path alias with ${aliasDepl.rules.length} rule${aliasDepl.rules.length > 1 ? 's' : ''} to be a normal alias.`
)
} }
} }
@ -269,17 +263,13 @@ module.exports = class Alias extends Now {
aliasedDeployment = await this.findDeployment(aliasDepl.deploymentId) aliasedDeployment = await this.findDeployment(aliasDepl.deploymentId)
if ( if (
aliasedDeployment && aliasedDeployment &&
aliasedDeployment.scale.current > depl.scale.current && aliasedDeployment.scale.current >= depl.scale.current &&
aliasedDeployment.scale.current >= aliasedDeployment.scale.min && (aliasedDeployment.scale.min > depl.scale.min ||
aliasedDeployment.scale.current <= aliasedDeployment.scale.max aliasedDeployment.scale.max > depl.scale.max)
) { ) {
shouldScaleDown = true shouldScaleDown = true
console.log( console.log(`> Alias ${alias} points to ${chalk.bold(aliasedDeployment.url)} (${chalk.bold(aliasedDeployment.scale.current + ' instances')})`)
`> Alias ${alias} points to ${chalk.bold(aliasedDeployment.url)} (${chalk.bold(aliasedDeployment.scale.current + ' instances')})` console.log(`> Scaling ${depl.url} to ${chalk.bold(aliasedDeployment.scale.current + ' instances')} atomically`)
)
console.log(
`> Scaling ${depl.url} to ${chalk.bold(aliasedDeployment.scale.current + ' instances')} atomically`
)
if (depl.scale.max < 1) { if (depl.scale.max < 1) {
if (this._debug) { if (this._debug) {
@ -298,9 +288,7 @@ module.exports = class Alias extends Now {
} }
await this.unfreeze(depl) await this.unfreeze(depl)
if (this._debug) { if (this._debug) {
console.log( console.log(`> Deployment is now unfrozen, scaling it to match current instance count`)
`> Deployment is now unfrozen, scaling it to match current instance count`
)
} }
} }
// Scale it to current limit // Scale it to current limit
@ -317,8 +305,8 @@ module.exports = class Alias extends Now {
} }
await this.setScale(depl.uid, { await this.setScale(depl.uid, {
min: aliasedDeployment.scale.min, min: Math.max(aliasedDeployment.scale.min, depl.scale.min),
max: aliasedDeployment.scale.max max: Math.max(aliasedDeployment.scale.max, depl.scale.max)
}) })
} }
} }
@ -346,25 +334,19 @@ module.exports = class Alias extends Now {
console.log(output) console.log(output)
} }
} else { } else {
console.log( console.log(`${chalk.cyan('> Success!')} Alias already exists ${chalk.dim(`(${uid})`)}.`)
`${chalk.cyan('> Success!')} Alias already exists ${chalk.dim(`(${uid})`)}.`
)
} }
if (aliasedDeployment && shouldScaleDown) { if (aliasedDeployment && shouldScaleDown) {
const scaleDown = Date.now() const scaleDown = Date.now()
await this.setScale(aliasedDeployment.uid, { min: 0, max: 1 }) await this.setScale(aliasedDeployment.uid, { min: 0, max: 1 })
console.log( console.log(`> Scaled ${chalk.gray(aliasedDeployment.url)} down to 1 instance ${chalk.gray('[' + ms(Date.now() - scaleDown) + ']')}`)
`> Scaled ${chalk.gray(aliasedDeployment.url)} down to 1 instance ${chalk.gray('[' + ms(Date.now() - scaleDown) + ']')}`
)
} }
} }
createAlias(depl, alias) { createAlias(depl, alias) {
return this.retry(async (bail, attempt) => { return this.retry(async (bail, attempt) => {
if (this._debug) { if (this._debug) {
console.time( console.time(`> [debug] /now/deployments/${depl.uid}/aliases #${attempt}`)
`> [debug] /now/deployments/${depl.uid}/aliases #${attempt}`
)
} }
const res = await this._fetch(`/now/deployments/${depl.uid}/aliases`, { const res = await this._fetch(`/now/deployments/${depl.uid}/aliases`, {
@ -374,9 +356,7 @@ module.exports = class Alias extends Now {
const body = await res.json() const body = await res.json()
if (this._debug) { if (this._debug) {
console.timeEnd( console.timeEnd(`> [debug] /now/deployments/${depl.uid}/aliases #${attempt}`)
`> [debug] /now/deployments/${depl.uid}/aliases #${attempt}`
)
} }
// 409 conflict is returned if it already exists // 409 conflict is returned if it already exists
@ -424,9 +404,7 @@ module.exports = class Alias extends Now {
} }
if (code === 'cert_missing') { if (code === 'cert_missing') {
console.log( console.log(`> Provisioning certificate for ${chalk.underline(chalk.bold(alias))}`)
`> Provisioning certificate for ${chalk.underline(chalk.bold(alias))}`
)
try { try {
await this.createCert(alias) await this.createCert(alias)
@ -442,9 +420,7 @@ module.exports = class Alias extends Now {
} }
if (code === 'cert_expired') { if (code === 'cert_expired') {
console.log( console.log(`> Renewing certificate for ${chalk.underline(chalk.bold(alias))}`)
`> Renewing certificate for ${chalk.underline(chalk.bold(alias))}`
)
try { try {
await this.createCert(alias, { renew: true }) await this.createCert(alias, { renew: true })
@ -546,12 +522,8 @@ module.exports = class Alias extends Now {
if (canBePurchased) { if (canBePurchased) {
const price = await pricePromise const price = await pricePromise
info( info(`The domain ${aliasParam} is ${chalk.bold('available for purchase')}! ${elapsed()}`)
`The domain ${aliasParam} is ${chalk.bold('available for purchase')}! ${elapsed()}` const confirmation = await promptBool(`Buy now for ${chalk.bold(`$${price}`)} (${chalk.bold((currentTeam && currentTeam.slug) || user.username || user.email)})?`)
)
const confirmation = await promptBool(
`Buy now for ${chalk.bold(`$${price}`)} (${chalk.bold((currentTeam && currentTeam.slug) || user.username || user.email)})?`
)
eraseLines(1) eraseLines(1)
if (!confirmation) { if (!confirmation) {
info('Aborted') info('Aborted')
@ -591,16 +563,12 @@ module.exports = class Alias extends Now {
error( error(
'The nameservers are pending propagation. Please try again shortly' 'The nameservers are pending propagation. Please try again shortly'
) )
info( info(`The ${tld} servers might take some extra time to reflect changes`)
`The ${tld} servers might take some extra time to reflect changes`
)
gracefulExit() gracefulExit()
} }
} }
console.log( console.log(`> Verifying the DNS settings for ${chalk.bold(chalk.underline(alias))} (see ${chalk.underline('https://zeit.world')} for help)`)
`> Verifying the DNS settings for ${chalk.bold(chalk.underline(alias))} (see ${chalk.underline('https://zeit.world')} for help)`
)
const _domain = publicSuffixList.parse(alias).domain const _domain = publicSuffixList.parse(alias).domain
const _domainInfo = await this.getDomain(_domain) const _domainInfo = await this.getDomain(_domain)
@ -617,13 +585,9 @@ module.exports = class Alias extends Now {
if (this._debug) { if (this._debug) {
if (domainInfo) { if (domainInfo) {
console.log( console.log(`> [debug] Found domain ${domain} with verified:${domainInfo.verified}`)
`> [debug] Found domain ${domain} with verified:${domainInfo.verified}`
)
} else { } else {
console.log( console.log(`> [debug] Found domain ${domain} and nameservers ${nameservers}`)
`> [debug] Found domain ${domain} and nameservers ${nameservers}`
)
} }
} }
@ -641,9 +605,7 @@ module.exports = class Alias extends Now {
e.userError = true e.userError = true
throw e throw e
} }
console.log( console.log(`${chalk.cyan('> Success!')} Domain ${chalk.bold(chalk.underline(domain))} verified`)
`${chalk.cyan('> Success!')} Domain ${chalk.bold(chalk.underline(domain))} verified`
)
} }
} }
@ -658,9 +620,7 @@ module.exports = class Alias extends Now {
// configuration (if we can!) // configuration (if we can!)
try { try {
if (usingZeitWorld) { if (usingZeitWorld) {
console.log( console.log(`> Detected ${chalk.bold(chalk.underline('zeit.world'))} nameservers! Configuring records.`)
`> Detected ${chalk.bold(chalk.underline('zeit.world'))} nameservers! Configuring records.`
)
const record = alias.substr(0, alias.length - domain.length) const record = alias.substr(0, alias.length - domain.length)
// Lean up trailing and leading dots // Lean up trailing and leading dots
@ -690,14 +650,10 @@ module.exports = class Alias extends Now {
throw e throw e
} }
} else { } else {
console.log( console.log(`> Resolved IP: ${err.ip ? `${chalk.underline(err.ip)} (unknown)` : chalk.dim('none')}`)
`> Resolved IP: ${err.ip ? `${chalk.underline(err.ip)} (unknown)` : chalk.dim('none')}` console.log(`> Nameservers: ${nameservers && nameservers.length ? nameservers
) .map(ns => chalk.underline(ns))
console.log( .join(', ') : chalk.dim('none')}`)
`> Nameservers: ${nameservers && nameservers.length ? nameservers
.map(ns => chalk.underline(ns))
.join(', ') : chalk.dim('none')}`
)
throw err throw err
} }
} catch (e) { } catch (e) {
@ -714,9 +670,7 @@ module.exports = class Alias extends Now {
if (!usingZeitWorld && !skipDNSVerification) { if (!usingZeitWorld && !skipDNSVerification) {
if (this._debug) { if (this._debug) {
console.log( console.log(`> [debug] Trying to register a non-ZeitWorld domain ${domain} for the current user`)
`> [debug] Trying to register a non-ZeitWorld domain ${domain} for the current user`
)
} }
const { uid, verified, created } = await this.setupDomain(domain, { const { uid, verified, created } = await this.setupDomain(domain, {
@ -729,9 +683,7 @@ module.exports = class Alias extends Now {
e.userError = true e.userError = true
throw e throw e
} }
console.log( console.log(`${chalk.cyan('> Success!')} Domain ${chalk.bold(chalk.underline(domain))} ${chalk.dim(`(${uid})`)} added`)
`${chalk.cyan('> Success!')} Domain ${chalk.bold(chalk.underline(domain))} ${chalk.dim(`(${uid})`)} added`
)
} }
console.log(`> Verification ${chalk.bold('OK')}!`) console.log(`> Verification ${chalk.bold('OK')}!`)

Loading…
Cancel
Save