Skip to content

Instantly share code, notes, and snippets.

@lathiat
Created July 26, 2023 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lathiat/d5ef07169559b955ac77ff589174074c to your computer and use it in GitHub Desktop.
Save lathiat/d5ef07169559b955ac77ff589174074c to your computer and use it in GitHub Desktop.
Fix broken series upgrade (Launchpad Bug #2008509)
import ssl
import pymongo
import yaml
import glob
import sys
import os
import urllib
# Find juju machine agent
agents = glob.glob('/var/lib/juju/agents/machine-*')
if len(agents) != 1:
print("Unable to find only a single machine agent in /var/lib/juju/agents")
sys.exit(1)
agent = os.path.basename(agents[0])
with open(os.path.join(agents[0], "agent.conf"), "r") as file:
agent_conf_data = yaml.safe_load(file)
# MongoDB connection details
host = "localhost"
port = 37017
username = urllib.parse.quote(agent)
password = urllib.parse.quote(agent_conf_data["statepassword"])
auth_db = "admin"
# Update parameters
match_text = "hook:\n kind: pre-series-upgrade\n"
search_text = "op: run-hook"
replacement_text = "op: continue"
# MongoDB connection string
connection_string = f"mongodb://{username}:{password}@{host}:{port}/{auth_db}"
def update_documents():
# Connect to the MongoDB instance with SSL configuration
client = pymongo.MongoClient(connection_string, ssl=True, ssl_cert_reqs=ssl.CERT_NONE)
db = client.juju
# Get the collection
collection = db.unitstates
# Find all documents with matching names and containing the search text
documents = collection.find({
"_id": {"$regex": ".*#charm"},
"uniter-state": {"$regex": ".*" + match_text + ".*"}
})
for document in documents:
print(document["_id"], document["uniter-state"])
# Update the "uniter-state" field for each document
new_uniter_state = document["uniter-state"].replace("op: run-hook", "op: continue")
new_uniter_state = document["uniter-state"].replace("hook:\n kind: pre-series-upgrade\nseries-upgrade-target: jammy\n", "")
collection.update_one(
{"_id": document["_id"]},
{"$set": {"uniter-state": new_uniter_state}}
)
print(f"Document with _id '{document['_id']}' updated successfully.")
# Close the connection
client.close()
if __name__ == "__main__":
update_documents()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment