Skip to content

Commit

Permalink
FEAT!: upgrade mongo to v7 (overhangio#1029)
Browse files Browse the repository at this point in the history
* FEAT!: upgrade mongo to v7
  • Loading branch information
DawoudSheraz authored Apr 30, 2024
1 parent 943bee6 commit db6f4d1
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 7 deletions.
10 changes: 10 additions & 0 deletions changelog.d/20240329_164354_dawoud.sheraz_mongo_update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- 💥[Feature] Update MongoDB to v7.0.7 (by @dawoudsheraz)

MongoDB is updating from 4.4 to 7. Since there have been major releases since 4.4, the upgrade would need to go through them before running Mongo 7. Mongo would need to follow 4.4 --> 5.0 --> 6.0 --> 7.0 upgrade path to work correctly.
The container will keep on restarting with featureCompatibility error if the upgrade path is not followed.

To upgrade mongo, run the following command based in the appropriate environment:
```
tutor <dev|local|k8s> upgrade --from=quince
```
For k8s only, the above command will not perform the upgrade automatically. Instead, the command will output a series of commands that would need to be run manually to carry out the upgrade.
2 changes: 1 addition & 1 deletion docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ This configuration parameter defines which Caddy Docker image to use.

This configuration parameter defines which Elasticsearch Docker image to use.

- ``DOCKER_IMAGE_MONGODB`` (default: ``"docker.io/mongo:4.4.22"``)
- ``DOCKER_IMAGE_MONGODB`` (default: ``"docker.io/mongo:7.0.7"``)

This configuration parameter defines which MongoDB Docker image to use.

Expand Down
19 changes: 19 additions & 0 deletions tutor/commands/upgrade/common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import click

from tutor import config as tutor_config
Expand Down Expand Up @@ -41,6 +43,23 @@ def upgrade_from_nutmeg(context: click.Context, config: Config) -> None:
)


def get_mongo_upgrade_parameters(
docker_version: str, compatibility_version: str
) -> tuple[int, dict[str, int | str]]:
"""
Helper utility to get parameters required during mongo upgrade.
"""
mongo_version = int(docker_version.split(".")[0])
admin_command: dict[str, int | str] = {
"setFeatureCompatibilityVersion": compatibility_version
}
if mongo_version == 7:
# Explicit confirmation is required to upgrade to 7 from 6
# https://www.mongodb.com/docs/manual/reference/command/setFeatureCompatibilityVersion/#confirm
admin_command.update({"confirm": 1})
return mongo_version, admin_command


PALM_RENAME_ORA2_FOLDER_COMMAND = """
if stat '/openedx/data/ora2/SET-ME-PLEASE (ex. bucket-name)' 2> /dev/null; then
echo "Renaming ora2 folder..."
Expand Down
18 changes: 16 additions & 2 deletions tutor/commands/upgrade/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def upgrade_from(context: click.Context, from_release: str) -> None:
if running_release == "palm":
running_release = "quince"

if running_release == "quince":
upgrade_from_quince(context, config)


def upgrade_from_ironwood(context: click.Context, config: Config) -> None:
click.echo(fmt.title("Upgrading from Ironwood"))
Expand Down Expand Up @@ -155,6 +158,13 @@ def upgrade_from_olive(context: click.Context, config: Config) -> None:
upgrade_mongodb(context, config, "4.4.22", "4.4")


def upgrade_from_quince(context: click.Context, config: Config) -> None:
click.echo(fmt.title("Upgrading from Quince"))
upgrade_mongodb(context, config, "5.0.26", "5.0")
upgrade_mongodb(context, config, "6.0.14", "6.0")
upgrade_mongodb(context, config, "7.0.7", "7.0")


def upgrade_mongodb(
context: click.Context,
config: Config,
Expand All @@ -168,7 +178,11 @@ def upgrade_mongodb(
)
return

mongo_version, admin_command = common_upgrade.get_mongo_upgrade_parameters(
to_docker_version, to_compatibility_version
)
click.echo(fmt.title(f"Upgrading MongoDb to v{to_docker_version}"))

# Note that the DOCKER_IMAGE_MONGODB value is never saved, because we only save the
# environment, not the configuration.
config["DOCKER_IMAGE_MONGODB"] = f"mongo:{to_docker_version}"
Expand All @@ -180,9 +194,9 @@ def upgrade_mongodb(
compose.execute,
args=[
"mongodb",
"mongo",
"mongosh" if mongo_version >= 6 else "mongo",
"--eval",
f'db.adminCommand({{ setFeatureCompatibilityVersion: "{to_compatibility_version}" }})',
f"db.adminCommand({admin_command})",
],
)
context.invoke(compose.stop)
17 changes: 16 additions & 1 deletion tutor/commands/upgrade/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def upgrade_from(context: click.Context, from_release: str) -> None:
if running_release == "palm":
running_release = "quince"

if running_release == "quince":
upgrade_from_quince(config)


def upgrade_from_ironwood(config: Config) -> None:
upgrade_mongodb(config, "3.4.24", "3.4")
Expand Down Expand Up @@ -162,6 +165,13 @@ def upgrade_from_olive(context: Context, config: Config) -> None:
upgrade_mongodb(config, "4.4.22", "4.4")


def upgrade_from_quince(config: Config) -> None:
click.echo(fmt.title("Upgrading from Quince"))
upgrade_mongodb(config, "5.0.26", "5.0")
upgrade_mongodb(config, "6.0.14", "6.0")
upgrade_mongodb(config, "7.0.7", "7.0")


def upgrade_mongodb(
config: Config, to_docker_version: str, to_compatibility_version: str
) -> None:
Expand All @@ -171,13 +181,18 @@ def upgrade_mongodb(
"responsibility to upgrade your MongoDb instance to {to_docker_version}."
)
return
mongo_version, admin_command = common_upgrade.get_mongo_upgrade_parameters(
to_docker_version, to_compatibility_version
)
mongo_binary = "mongosh" if mongo_version >= 6 else "mongo"

message = f"""Automatic release upgrade is unsupported in Kubernetes. You should manually upgrade
your MongoDb cluster to {to_docker_version} by running something similar to:
tutor k8s stop
tutor config save --set DOCKER_IMAGE_MONGODB=mongo:{to_docker_version}
tutor k8s start
tutor k8s exec mongodb mongo --eval 'db.adminCommand({{ setFeatureCompatibilityVersion: "{to_compatibility_version}" }})'
tutor k8s exec mongodb {mongo_binary} --eval 'db.adminCommand({admin_command})'
tutor config save --unset DOCKER_IMAGE_MONGODB
"""
fmt.echo_info(message)
2 changes: 1 addition & 1 deletion tutor/templates/config/defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ DOCKER_IMAGE_CADDY: "docker.io/caddy:2.7.4"
# https://hub.docker.com/_/elasticsearch/tags
DOCKER_IMAGE_ELASTICSEARCH: "docker.io/elasticsearch:7.17.13"
# https://hub.docker.com/_/mongo/tags
DOCKER_IMAGE_MONGODB: "docker.io/mongo:4.4.25"
DOCKER_IMAGE_MONGODB: "docker.io/mongo:7.0.7"
# https://hub.docker.com/_/mysql/tags
DOCKER_IMAGE_MYSQL: "docker.io/mysql:8.1.0"
DOCKER_IMAGE_PERMISSIONS: "{{ DOCKER_REGISTRY }}overhangio/openedx-permissions:{{ TUTOR_VERSION }}"
Expand Down
2 changes: 1 addition & 1 deletion tutor/templates/k8s/deployments.yml
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ spec:
containers:
- name: mongodb
image: {{ DOCKER_IMAGE_MONGODB }}
args: ["mongod", "--nojournal", "--storageEngine", "wiredTiger"]
args: ["mongod", "--storageEngine", "wiredTiger"]
ports:
- containerPort: 27017
volumeMounts:
Expand Down
2 changes: 1 addition & 1 deletion tutor/templates/local/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ services:
mongodb:
image: {{ DOCKER_IMAGE_MONGODB }}
# Use WiredTiger in all environments, just like at edx.org
command: mongod --nojournal --storageEngine wiredTiger
command: mongod --storageEngine wiredTiger
restart: unless-stopped
user: "999:999"
volumes:
Expand Down

0 comments on commit db6f4d1

Please sign in to comment.