Try to fix Snap automatic upgrade.

Thanks to xet7 !
This commit is contained in:
Lauri Ojansivu 2025-10-11 04:02:02 +03:00
parent d88d197de9
commit 4ec4e19e63
3 changed files with 76 additions and 75 deletions

21
.vscode/settings.json vendored
View file

@ -1,12 +1,13 @@
{ {
"editor.tabSize": 2, "terminal.integrated.env.linux": {
"editor.rulers": [80], "TERM": "xterm-256color"
"files.trimTrailingWhitespace": true, },
"files.insertFinalNewline": true, "terminal.integrated.env.osx": {
"files.exclude": { "TERM": "xterm-256color"
"**/.git": true, },
"**/.DS_Store": true, "terminal.integrated.env.windows": {
"node_modules": true, "TERM": "xterm-256color"
".meteor/local": true },
} "terminal.integrated.shell.linux": "/bin/bash",
"terminal.integrated.shellArgs.linux": ["-l"]
} }

View file

@ -185,12 +185,12 @@ if [ -z "${MONGO_URL}" ]; then
#echo "mongodb log destination: ${MONGO_LOG_DESTINATION}" >> "${SNAP_COMMON}/settings.log" #echo "mongodb log destination: ${MONGO_LOG_DESTINATION}" >> "${SNAP_COMMON}/settings.log"
# Disable MongoDB telemetry and free monitoring # Disable MongoDB telemetry and free monitoring
/snap/${SNAP_NAME}/current/usr/bin/mongosh wekan --eval 'disableTelemetry();' --port ${MONGODB_PORT} /snap/${SNAP_NAME}/current/bin/mongosh wekan --eval 'disableTelemetry();' --port ${MONGODB_PORT}
/snap/${SNAP_NAME}/current/usr/bin/mongosh wekan --eval 'db.disableFreeMonitoring();' --port ${MONGODB_PORT} /snap/${SNAP_NAME}/current/bin/mongosh wekan --eval 'db.disableFreeMonitoring();' --port ${MONGODB_PORT}
# Snap: Disable apparmor="DENIED" at syslog # Snap: Disable apparmor="DENIED" at syslog
# https://github.com/wekan/wekan/issues/4855 # https://github.com/wekan/wekan/issues/4855
/snap/${SNAP_NAME}/current/usr/bin/mongosh wekan --eval 'db.adminCommand({ setParameter: 1, diagnosticDataCollectionEnabled: false});' --port ${MONGODB_PORT} /snap/${SNAP_NAME}/current/bin/mongosh wekan --eval 'db.adminCommand({ setParameter: 1, diagnosticDataCollectionEnabled: false});' --port ${MONGODB_PORT}
# Drop indexes on database upgrade, when starting MongoDB # Drop indexes on database upgrade, when starting MongoDB
#mongosh wekan --eval "db.getCollectionNames().forEach(function(col_name) { var coll = db.getCollection(col_name); coll.dropIndexes(); });" $BIND_OPTIONS #mongosh wekan --eval "db.getCollectionNames().forEach(function(col_name) { var coll = db.getCollection(col_name); coll.dropIndexes(); });" $BIND_OPTIONS
@ -199,7 +199,7 @@ if [ -z "${MONGO_URL}" ]; then
#mongosh wekan --eval 'db.adminCommand({ setFeatureCompatibilityVersion: "4.4" });' ${BIND_OPTIONS} #mongosh wekan --eval 'db.adminCommand({ setFeatureCompatibilityVersion: "4.4" });' ${BIND_OPTIONS}
# Delete incomplete uploads so that they would not prevent starting WeKan # Delete incomplete uploads so that they would not prevent starting WeKan
/snap/${SNAP_NAME}/current/usr/bin/mongosh wekan --eval 'db.getCollection("cfs.attachments.filerecord").find( { "uploadedAt": { "$exists": true }, "copies.attachments" : null,"failures.copies.attachments.doneTrying" : {"$ne" : true}});' --port ${MONGODB_PORT} /snap/${SNAP_NAME}/current/bin/mongosh wekan --eval 'db.getCollection("cfs.attachments.filerecord").find( { "uploadedAt": { "$exists": true }, "copies.attachments" : null,"failures.copies.attachments.doneTrying" : {"$ne" : true}});' --port ${MONGODB_PORT}
else else
@ -219,20 +219,20 @@ else
fi fi
# Disable MongoDB telemetry and free monitoring # Disable MongoDB telemetry and free monitoring
/snap/${SNAP_NAME}/current/usr/bin/mongosh ${MONGO_URL} --eval 'disableTelemetry();' /snap/${SNAP_NAME}/current/bin/mongosh ${MONGO_URL} --eval 'disableTelemetry();'
/snap/${SNAP_NAME}/current/usr/bin/mongosh ${MONGO_URL} --eval 'db.disableFreeMonitoring();' /snap/${SNAP_NAME}/current/bin/mongosh ${MONGO_URL} --eval 'db.disableFreeMonitoring();'
# Snap: Disable apparmor="DENIED" at syslog # Snap: Disable apparmor="DENIED" at syslog
# https://github.com/wekan/wekan/issues/4855 # https://github.com/wekan/wekan/issues/4855
/snap/${SNAP_NAME}/current/usr/bin/mongosh ${MONGO_URL} --eval 'db.adminCommand({ setParameter: 1, diagnosticDataCollectionEnabled: false});' /snap/${SNAP_NAME}/current/bin/mongosh ${MONGO_URL} --eval 'db.adminCommand({ setParameter: 1, diagnosticDataCollectionEnabled: false});'
# Drop indexes on database upgrade, when starting MongoDB # Drop indexes on database upgrade, when starting MongoDB
#mongosh wekan --eval "db.getCollectionNames().forEach(function(col_name) { var coll = db.getCollection(col_name); coll.dropIndexes(); });" $BIND_OPTIONS #mongosh wekan --eval "db.getCollectionNames().forEach(function(col_name) { var coll = db.getCollection(col_name); coll.dropIndexes(); });" $BIND_OPTIONS
# Set MongoDB feature compatibility version # Set MongoDB feature compatibility version
#/snap/${SNAP_NAME}/current/usr/bin/mongosh ${MONGO_URL} --eval 'db.adminCommand({ setFeatureCompatibilityVersion: "4.4" });' #/snap/${SNAP_NAME}/current/bin/mongosh ${MONGO_URL} --eval 'db.adminCommand({ setFeatureCompatibilityVersion: "4.4" });'
# Delete incomplete uploads so that they would not prevent starting WeKan # Delete incomplete uploads so that they would not prevent starting WeKan
/snap/${SNAP_NAME}/current/usr/bin/mongosh ${MONGO_URL} --eval 'db.getCollection("cfs.attachments.filerecord").find( { "uploadedAt": { "$exists": true }, "copies.attachments" : null,"failures.copies.attachments.doneTrying" : {"$ne" : true}});' /snap/${SNAP_NAME}/current/bin/mongosh ${MONGO_URL} --eval 'db.getCollection("cfs.attachments.filerecord").find( { "uploadedAt": { "$exists": true }, "copies.attachments" : null,"failures.copies.attachments.doneTrying" : {"$ne" : true}});'
fi fi

View file

@ -20,15 +20,18 @@ TEMP_DIR="${SNAP_COMMON}/mongodb-migration-temp"
BACKUP_DIR="${SNAP_COMMON}/mongodb-backup-$(date +%Y%m%d-%H%M%S)" BACKUP_DIR="${SNAP_COMMON}/mongodb-backup-$(date +%Y%m%d-%H%M%S)"
# MongoDB paths # MongoDB paths
MONGO3_BIN="/snap/${SNAP_NAME}/current/bin" MONGO3_BIN="/snap/${SNAP_NAME}/current/migratemongo/bin"
MONGO7_BIN="/snap/${SNAP_NAME}/current/usr/bin" MONGO7_BIN="/snap/${SNAP_NAME}/current/bin"
MONGO3_LIB="/snap/${SNAP_NAME}/current/lib" MONGO3_LIB="/snap/${SNAP_NAME}/current/migratemongo/lib"
MONGO7_LIB="/snap/${SNAP_NAME}/current/lib" MONGO7_LIB="/snap/${SNAP_NAME}/current/usr/lib"
# Set up environment for MongoDB 3 tools # Set up environment for MongoDB 3 tools
export LD_LIBRARY_PATH="${MONGO3_LIB}:${MONGO3_LIB}/x86_64-linux-gnu:${LD_LIBRARY_PATH}" export LD_LIBRARY_PATH="${MONGO3_LIB}:${MONGO3_LIB}/x86_64-linux-gnu:${LD_LIBRARY_PATH}"
export PATH="${MONGO3_BIN}:${MONGO7_BIN}:${PATH}" export PATH="${MONGO3_BIN}:${MONGO7_BIN}:${PATH}"
# Set MongoDB log destination to snapcommon for log file detection
export MONGO_LOG_DESTINATION="snapcommon"
# Validate that all operations are within SNAP_COMMON # Validate that all operations are within SNAP_COMMON
validate_snap_common_path() { validate_snap_common_path() {
local path="$1" local path="$1"
@ -209,9 +212,9 @@ check_migration_needed() {
return 0 return 0
fi fi
# Check for MongoDB 3 raw database files # Check for MongoDB upgrade needed by examining log file
if detect_mongodb3_raw_files; then if detect_mongodb_upgrade_needed; then
log_message "MongoDB 3 raw database files detected" log_message "MongoDB upgrade needed detected from log file"
return 0 return 0
fi fi
@ -219,56 +222,45 @@ check_migration_needed() {
return 1 return 1
} }
# Detect MongoDB 3 raw database files # Detect if MongoDB upgrade is needed by checking log file
detect_mongodb3_raw_files() { detect_mongodb_upgrade_needed() {
# Look for MongoDB 3 specific files and directories local mongodb_log="${SNAP_COMMON}/mongodb.log"
local mongodb3_indicators=(
"${SNAP_COMMON}/local.0"
"${SNAP_COMMON}/local.ns"
"${SNAP_COMMON}/local.1"
"${SNAP_COMMON}/wekan.0"
"${SNAP_COMMON}/wekan.ns"
"${SNAP_COMMON}/wekan.1"
"${SNAP_COMMON}/storage.bson"
"${SNAP_COMMON}/_tmp"
)
# Check for MongoDB 3 journal files # Check if MongoDB log file exists
local journal_files=( if [ ! -f "$mongodb_log" ]; then
"${SNAP_COMMON}/journal" log_message "MongoDB log file not found: $mongodb_log"
"${SNAP_COMMON}/j._0" return 1
)
# Check for MongoDB 3 lock file
if [ -f "${SNAP_COMMON}/mongod.lock" ]; then
log_message "MongoDB lock file found, checking for MongoDB 3 data"
# Check if any MongoDB 3 indicators exist
for indicator in "${mongodb3_indicators[@]}"; do
if [ -e "$indicator" ]; then
log_message "MongoDB 3 indicator found: $indicator"
return 0
fi
done
# Check for journal files
for journal in "${journal_files[@]}"; do
if [ -e "$journal" ]; then
log_message "MongoDB journal file found: $journal"
return 0
fi
done
# Check for any .0, .1, .ns files (MongoDB 3 data files)
if find "${SNAP_COMMON}" -maxdepth 1 -name "*.0" -o -name "*.1" -o -name "*.ns" | grep -q .; then
log_message "MongoDB 3 data files found"
return 0
fi
fi fi
# Check for the specific error message indicating upgrade is needed
if grep -q "This version of MongoDB is too recent to start up on the existing data files. Try MongoDB 4.2 or earlier." "$mongodb_log"; then
log_message "MongoDB upgrade needed detected in log file"
return 0
fi
# Also check for similar error messages that might indicate upgrade issues
if grep -q "too recent to start up on the existing data files" "$mongodb_log"; then
log_message "MongoDB upgrade needed detected in log file (alternative message)"
return 0
fi
log_message "No MongoDB upgrade needed detected in log file"
return 1 return 1
} }
# Reset MONGO_LOG_DESTINATION to devnull after successful migration
reset_mongo_log_destination() {
log_message "Resetting MONGO_LOG_DESTINATION to devnull after successful migration"
# Use snap set to change the setting back to devnull
if snap set wekan mongo-log-destination="devnull" 2>/dev/null; then
log_success "MONGO_LOG_DESTINATION reset to devnull successfully"
else
log_error "Failed to reset MONGO_LOG_DESTINATION to devnull"
# Don't fail the migration for this setting issue
fi
}
# Migrate raw MongoDB 3 database files # Migrate raw MongoDB 3 database files
migrate_raw_database_files() { migrate_raw_database_files() {
log_message "Starting raw MongoDB 3 database files migration" log_message "Starting raw MongoDB 3 database files migration"
@ -372,6 +364,10 @@ migrate_raw_database_files() {
fi fi
log_success "Raw database files migration completed successfully" log_success "Raw database files migration completed successfully"
# Reset MONGO_LOG_DESTINATION to devnull after successful migration
reset_mongo_log_destination
return 0 return 0
} }
@ -508,8 +504,8 @@ perform_migration() {
mkdir -p "$TEMP_DIR" mkdir -p "$TEMP_DIR"
# Check if we need to migrate raw database files # Check if we need to migrate raw database files
if detect_mongodb3_raw_files; then if detect_mongodb_upgrade_needed; then
log_message "Raw MongoDB 3 database files detected, starting raw file migration" log_message "MongoDB upgrade needed detected, starting raw file migration"
if ! migrate_raw_database_files; then if ! migrate_raw_database_files; then
log_error "Failed to migrate raw database files" log_error "Failed to migrate raw database files"
return 1 return 1
@ -575,6 +571,10 @@ EOF
fi fi
log_success "MongoDB migration completed successfully" log_success "MongoDB migration completed successfully"
# Reset MONGO_LOG_DESTINATION to devnull after successful migration
reset_mongo_log_destination
return 0 return 0
} }