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

Loading…
Cancel
Save