browsertrix/backend/btrixcloud/migrations/migration_0003_mutable_crawl_configs.py
Ilya Kreymer 544346d1d4
backend: make crawlconfigs mutable! (#656) (#662)
* backend: make crawlconfigs mutable! (#656)
- crawlconfig PATCH /{id} can now receive a new JSON config to replace the old one (in addition to scale, schedule, tags)
- exclusions: add / remove APIs mutate the current crawlconfig, do not result in a new crawlconfig created
- exclusions: ensure crawl job 'config' is updated when exclusions are added/removed, unify add/remove exclusions on crawl
- k8s: crawlconfig json is updated along with scale
- k8s: stateful set is restarted by updating annotation, instead of changing template
- crawl object: now has 'config', as well as 'profileid', 'schedule', 'crawlTimeout', 'jobType' properties to ensure anything that is changeable is stored on the crawl
- crawlconfigcore: store share properties between crawl and crawlconfig in new crawlconfigcore (includes 'schedule', 'jobType', 'config', 'profileid', 'schedule', 'crawlTimeout', 'tags', 'oid')
- crawlconfig object: remove 'oldId', 'newId', disallow deactivating/deleting while crawl is running
- rename 'userid' -> 'createdBy'
- remove unused 'completions' field
- add missing return to fix /run response
- crawlout: ensure 'profileName' is resolved on CrawlOut from profileid
- crawlout: return 'name' instead of 'configName' for consistent response
- update: 'modified', 'modifiedBy' fields to set modification date and user modifying config
- update: ensure PROFILE_FILENAME is updated in configmap is profileid provided, clear if profileid==""
- update: return 'settings_changed' and 'metadata_changed' if either crawl settings or metadata changed
- tests: update tests to check settings_changed/metadata_changed return values

add revision tracking to crawlconfig:
- store each revision separate mongo db collection
- revisions accessible via /crawlconfigs/{cid}/revs
- store 'rev' int in crawlconfig and in crawljob
- only add revision history if crawl config changed

migration:
- update to db v3
- copy fields from crawlconfig -> crawl
- rename userid -> createdBy
- copy userid -> modifiedBy, created -> modified
- skip invalid crawls (missing config), make createdBy optional (just in case)

frontend: Update crawl config keys with new API (#681), update frontend to use new PATCH endpoint, load config from crawl object in details view

---------

Co-authored-by: Tessa Walsh <tessa@bitarchivist.net>
Co-authored-by: sua yoo <sua@webrecorder.org>
Co-authored-by: sua yoo <sua@suayoo.com>
2023-03-07 20:36:50 -08:00

116 lines
4.5 KiB
Python

"""
Migration 0003 - Mutable crawl configs and crawl revision history
"""
from datetime import datetime
from btrixcloud.crawlconfigs import CrawlConfig
from btrixcloud.crawls import Crawl
from btrixcloud.migrations import BaseMigration, MigrationError
MIGRATION_VERSION = "0003"
class Migration(BaseMigration):
"""Migration class."""
def __init__(self, mdb, migration_version=MIGRATION_VERSION):
super().__init__(mdb, migration_version)
async def migrate_up(self):
"""Perform migration up.
Set new crawl_configs fields and add config details to crawls.
"""
# pylint: disable=too-many-locals, too-many-branches
crawls = self.mdb["crawls"]
crawl_configs = self.mdb["crawl_configs"]
# Return early if there are no configs
crawl_config_results = [res async for res in crawl_configs.find({})]
if not crawl_config_results:
return
utc_now_datetime = datetime.utcnow().replace(microsecond=0, tzinfo=None)
await crawl_configs.update_many(
{"createdBy": None}, [{"$set": {"createdBy": "$userid"}}]
)
await crawl_configs.update_many(
{"modifiedBy": None}, [{"$set": {"modifiedBy": "$userid"}}]
)
await crawl_configs.update_many({}, {"$unset": {"userid": 1}})
await crawl_configs.update_many(
{"created": None}, {"$set": {"created": utc_now_datetime}}
)
await crawl_configs.update_many({}, [{"$set": {"modified": "$created"}}])
await crawl_configs.update_many({}, {"$set": {"rev": 0}})
# Return early if there are no crawls
crawl_results = [res async for res in crawls.find({})]
if not crawl_results:
return
await crawls.update_many({}, {"$set": {"cid_rev": 0}})
for crawl_result in crawl_results:
config_result = await crawl_configs.find_one({"_id": crawl_result["cid"]})
if not config_result:
continue
await crawls.find_one_and_update(
{"_id": crawl_result["_id"]},
{
"$set": {
"config": config_result["config"],
"profileid": config_result["profileid"],
"schedule": config_result["schedule"],
"crawlTimeout": config_result["crawlTimeout"],
"jobType": config_result["jobType"],
}
},
)
# Test that migration went as expected
sample_config_result = await crawl_configs.find_one({})
sample_config = CrawlConfig.from_dict(sample_config_result)
if not sample_config.createdBy or (
sample_config.createdBy != sample_config.modifiedBy
):
raise MigrationError(
"Crawl config createdBy and modifiedBy set incorrectly by migration"
)
if sample_config.modified != sample_config.created:
raise MigrationError(
"Crawl config modified set incorrectly by migration - should be equal to created"
)
if sample_config.rev != 0:
raise MigrationError("Crawl config rev set incorrectly by migration")
no_created_results = await crawl_configs.find_one({"created": None})
if no_created_results:
raise MigrationError("Crawl config created not set by migration")
sample_crawl_result = await crawls.find_one({})
sample_crawl = Crawl.from_dict(sample_crawl_result)
if sample_crawl.cid_rev != 0:
raise MigrationError("Crawl cid_rev set incorrectly by migration")
matching_config_result = await crawl_configs.find_one({"_id": sample_crawl.cid})
matching_config = CrawlConfig.from_dict(matching_config_result)
if sample_crawl.profileid != matching_config.profileid:
raise MigrationError(
f"Crawl profileid {sample_crawl.profileid} doesn't match config"
)
if sample_crawl.schedule != matching_config.schedule:
raise MigrationError(
f"Crawl schedule {sample_crawl.schedule} doesn't match config"
)
if sample_crawl.crawlTimeout != matching_config.crawlTimeout:
raise MigrationError(
f"Crawl timeout {sample_crawl.crawlTimeout} doesn't match config"
)
if not sample_crawl.config.seeds:
raise MigrationError("Crawl config missing after migration")