-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add a new command to setup a cleanup policy for functions artifacts #8268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9fce589
Add new command to setup a cleanup policy
taeold 6967d7e
Refactor.
taeold 76be2df
Nits.
taeold 4d0bca5
nitnit
taeold 999b4d7
Merge branch 'master' into dl-artifacts-cleanup-policy
taeold 284a57d
Nits.
taeold 2f7f30d
Support --force.
taeold f236229
Respond to PR comments.
taeold 4536058
Fix command path.
taeold b20fa7d
Merge branch 'master' into dl-artifacts-cleanup-policy
taeold 5177a0b
One liners.
taeold a675b6a
Merge branch 'dl-artifacts-cleanup-policy' of https://github.com/fire…
taeold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
- Fix webframeworks deployments when using multiple hosting sites in `firebase.json`. (#8314) | ||
- Add a new command to setup a cleanup policy for functions artifacts. (#8268) | ||
- Support 3rd party builders for Angular. (#7557) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
import * as clc from "colorette"; | ||
|
||
import { Command } from "../command"; | ||
import { FirebaseError } from "../error"; | ||
import { needProjectId } from "../projectUtils"; | ||
import { confirm } from "../prompt"; | ||
import { requirePermissions } from "../requirePermissions"; | ||
import { requireAuth } from "../requireAuth"; | ||
import { logBullet, logSuccess } from "../utils"; | ||
import * as artifactregistry from "../gcp/artifactregistry"; | ||
import * as artifacts from "../functions/artifacts"; | ||
|
||
/** | ||
* Command to set up a cleanup policy for Cloud Run functions container images in Artifact Registry | ||
*/ | ||
export const command = new Command("functions:artifacts:setpolicy") | ||
.description( | ||
"Set up a cleanup policy for Cloud Run functions container images in Artifact Registry. " + | ||
"This policy will automatically delete old container images created during functions deployment.", | ||
) | ||
.option( | ||
"--location <location>", | ||
"Specify location to set up the cleanup policy. " + | ||
"If omitted, uses the default functions location.", | ||
"us-central1", | ||
) | ||
.option( | ||
"--days <days>", | ||
"Number of days to keep container images before deletion. Default is 3 days.", | ||
"3", | ||
) | ||
.option( | ||
"--none", | ||
"Opt-out from cleanup policy. This will prevent suggestions to set up a cleanup policy during initialization and deployment.", | ||
) | ||
.before((options) => { | ||
if (options.days && options.none) { | ||
Check warning on line 37 in src/commands/functions-artifacts-setpolicy.ts
|
||
throw new FirebaseError("Cannot specify both --days and --none options."); | ||
} | ||
}) | ||
.withForce("Automatically create or modify cleanup policy") | ||
.before(requireAuth) | ||
.before(async (options) => { | ||
const projectId = needProjectId(options); | ||
await artifactregistry.ensureApiEnabled(projectId); | ||
}) | ||
.before(requirePermissions, [ | ||
"artifactregistry.repositories.update", | ||
"artifactregistry.versions.delete", | ||
]) | ||
.action(async (options: any) => { | ||
const projectId = needProjectId(options); | ||
const location = options.location || "us-central1"; | ||
Check warning on line 53 in src/commands/functions-artifacts-setpolicy.ts
|
||
let daysToKeep = parseInt(options.days || "3", 10); | ||
Check warning on line 54 in src/commands/functions-artifacts-setpolicy.ts
|
||
|
||
const repoPath = artifacts.makeRepoPath(projectId, location); | ||
let repository: artifactregistry.Repository; | ||
try { | ||
repository = await artifactregistry.getRepository(repoPath); | ||
} catch (err: any) { | ||
if (err.status === 404) { | ||
logBullet(`Repository '${repoPath}' does not exist in Artifact Registry.`); | ||
logBullet( | ||
`Please deploy your functions first using: ` + | ||
`${clc.bold(`firebase deploy --only functions`)}`, | ||
); | ||
return; | ||
} | ||
throw err; | ||
} | ||
|
||
if (options.none) { | ||
const existingPolicy = artifacts.findExistingPolicy(repository); | ||
|
||
if (artifacts.hasCleanupOptOut(repository) && !existingPolicy) { | ||
logBullet(`Repository '${repoPath}' is already opted out from cleanup policies.`); | ||
logBullet(`No changes needed.`); | ||
return; | ||
} | ||
|
||
logBullet(`You are about to opt-out from cleanup policy for repository '${repoPath}'.`); | ||
logBullet( | ||
`This will prevent suggestions to set up cleanup policy during initialization and deployment.`, | ||
); | ||
|
||
if (existingPolicy) { | ||
logBullet(`Note: This will remove the existing cleanup policy from the repository.`); | ||
} | ||
|
||
const confirmOptOut = await confirm({ | ||
...options, | ||
default: true, | ||
message: "Do you want to continue?", | ||
}); | ||
|
||
if (!confirmOptOut) { | ||
throw new FirebaseError("Command aborted.", { exit: 1 }); | ||
} | ||
|
||
try { | ||
await artifacts.optOutRepository(repository); | ||
logSuccess(`Successfully opted out from cleanup policy for ${clc.bold(repoPath)}`); | ||
return; | ||
} catch (err: unknown) { | ||
throw new FirebaseError("Failed to opt-out from artifact registry cleanup policy", { | ||
original: err as Error, | ||
}); | ||
} | ||
} | ||
|
||
if (isNaN(daysToKeep) || daysToKeep < 0) { | ||
throw new FirebaseError("Days must be a non-negative number"); | ||
} | ||
|
||
if (daysToKeep === 0) { | ||
daysToKeep = 0.003472; // ~5 minutes in days | ||
} | ||
|
||
if (artifacts.hasSameCleanupPolicy(repository, daysToKeep)) { | ||
logBullet( | ||
`A cleanup policy already exists that deletes images older than ${clc.bold(daysToKeep)} days.`, | ||
); | ||
logBullet(`No changes needed.`); | ||
return; | ||
} | ||
|
||
logBullet( | ||
`You are about to set up a cleanup policy for Cloud Run functions container images in location ${clc.bold(location)}`, | ||
); | ||
logBullet( | ||
`This policy will automatically delete container images that are older than ${clc.bold(daysToKeep)} days`, | ||
); | ||
logBullet( | ||
"This helps reduce storage costs by removing old container images that are no longer needed", | ||
); | ||
|
||
const existingPolicy = artifacts.findExistingPolicy(repository); | ||
|
||
let isUpdate = false; | ||
if (existingPolicy && existingPolicy.condition?.olderThan) { | ||
const existingDays = artifacts.parseDaysFromPolicy(existingPolicy.condition.olderThan); | ||
if (existingDays) { | ||
isUpdate = true; | ||
logBullet( | ||
`Note: This will update an existing policy that currently deletes images older than ${clc.bold(existingDays)} days`, | ||
); | ||
} | ||
} | ||
|
||
if (artifacts.hasCleanupOptOut(repository)) { | ||
logBullet( | ||
`Note: This repository was previously opted out from cleanup policy. This action will remove the opt-out status.`, | ||
); | ||
} | ||
|
||
const confirmSetup = await confirm({ | ||
...options, | ||
default: true, | ||
message: "Do you want to continue?", | ||
}); | ||
|
||
if (!confirmSetup) { | ||
throw new FirebaseError("Command aborted.", { exit: 1 }); | ||
} | ||
|
||
try { | ||
await artifacts.setCleanupPolicy(repository, daysToKeep); | ||
const successMessage = isUpdate | ||
? `Successfully updated cleanup policy to delete images older than ${clc.bold(daysToKeep)} days` | ||
: `Successfully set up cleanup policy that deletes images older than ${clc.bold(daysToKeep)} days`; | ||
logSuccess(successMessage); | ||
logBullet(`Cleanup policy has been set for ${clc.bold(repoPath)}`); | ||
} catch (err: unknown) { | ||
throw new FirebaseError("Failed to set up artifact registry cleanup policy", { | ||
original: err as Error, | ||
}); | ||
} | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.