Skip to content

Commit

Permalink
Always scale up but never down when matching scale on alias (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamo authored and matheuss committed May 4, 2017
1 parent c05405e commit a09abbc
Showing 1 changed file with 31 additions and 79 deletions.
110 changes: 31 additions & 79 deletions lib/alias.js
Expand Up @@ -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)
Expand All @@ -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 })
Expand Down Expand Up @@ -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.`)
}
}

Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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)
})
}
}
Expand Down Expand Up @@ -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`, {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 })
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand All @@ -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}`)
}
}

Expand All @@ -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`)
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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, {
Expand All @@ -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')}!`)
Expand Down

0 comments on commit a09abbc

Please sign in to comment.