From 1260b7989bc016b4ea219b72a453a1a032bac1fc Mon Sep 17 00:00:00 2001 From: Olivier Clavel Date: Mon, 24 Sep 2018 11:15:54 +0200 Subject: [PATCH 1/7] Set seconds on default backup cron expression Not setting seconds can end up with the task being run several times. --- README.md | 2 +- defaults/main.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 269805fd..249c035b 100644 --- a/README.md +++ b/README.md @@ -578,7 +578,7 @@ nexus_rut_auth_header: "CUSTOM_HEADER" ### Backups ```yaml nexus_backup_configure: false - nexus_backup_cron: '* 0 21 * * ?' # See cron expressions definition in nexus create task gui + nexus_backup_cron: '0 0 21 * * ?' # See cron expressions definition in nexus create task gui nexus_backup_dir: '/var/nexus-backup' nexus_backup_log: '{{ nexus_backup_dir }}/nexus-backup.log' nexus_restore_log: '{{ nexus_backup_dir }}/nexus-restore.log' diff --git a/defaults/main.yml b/defaults/main.yml index fe6b38d5..2ac91ea8 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -25,7 +25,7 @@ nexus_backup_dir: '/var/nexus-backup' nexus_restore_log: '{{ nexus_backup_dir }}/nexus-restore.log' nexus_backup_log: '{{ nexus_backup_dir }}/nexus-backup.log' nexus_backup_configure: false # Shall we configure backup ? -nexus_backup_cron: "* 0 21 * * ?" # See cron expression in nexus create task GUI +nexus_backup_cron: "0 0 21 * * ?" # See cron expression in nexus create task GUI nexus_backup_rotate: false # Shall we rotate backups nexus_backup_rotate_first: false # Shall we rotate before making the current backup ? nexus_backup_keep_rotations: 4 # Keep 4 backup rotations by default (current + last 3) From 9fc37a18379046e109fc1d2512c2e2c85cb6a290 Mon Sep 17 00:00:00 2001 From: Olivier Clavel Date: Mon, 24 Sep 2018 14:57:38 +0200 Subject: [PATCH 2/7] Move backup task log to nexus standard logging --- README.md | 3 --- defaults/main.yml | 1 - templates/backup.groovy.j2 | 29 +++++++++++------------------ 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 249c035b..7f0a8acd 100644 --- a/README.md +++ b/README.md @@ -580,7 +580,6 @@ nexus_rut_auth_header: "CUSTOM_HEADER" nexus_backup_configure: false nexus_backup_cron: '0 0 21 * * ?' # See cron expressions definition in nexus create task gui nexus_backup_dir: '/var/nexus-backup' - nexus_backup_log: '{{ nexus_backup_dir }}/nexus-backup.log' nexus_restore_log: '{{ nexus_backup_dir }}/nexus-restore.log' nexus_backup_rotate: false nexus_backup_rotate_first: false @@ -603,8 +602,6 @@ you can set `nexus_backup_rotate_first: true`. This will configure a pre-rotatio rather than the default post-rotation. Please note than in this case, old backup(s) is/are removed before the current one is done and successful. -Note that `nexus_backup_log` _must be writable_ by the nexus user or the **backup task will fail** - #### Restore procedure Run your playbook with parameter `-e nexus_restore_point=` (e.g. 2017-12-17-21-00-00 for 17th of December 2017 at 21h00m00s) diff --git a/defaults/main.yml b/defaults/main.yml index 2ac91ea8..815ea608 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -23,7 +23,6 @@ nexus_max_direct_memory: "2G" # Nexus Backup nexus_backup_dir: '/var/nexus-backup' nexus_restore_log: '{{ nexus_backup_dir }}/nexus-restore.log' -nexus_backup_log: '{{ nexus_backup_dir }}/nexus-backup.log' nexus_backup_configure: false # Shall we configure backup ? nexus_backup_cron: "0 0 21 * * ?" # See cron expression in nexus create task GUI nexus_backup_rotate: false # Shall we rotate backups diff --git a/templates/backup.groovy.j2 b/templates/backup.groovy.j2 index 9f317944..58e82d66 100644 --- a/templates/backup.groovy.j2 +++ b/templates/backup.groovy.j2 @@ -20,8 +20,6 @@ import java.util.concurrent.TimeUnit * 7- If is true and is false, post-rotate backup dirs keeping last backups */ - -nexusBackupLogFilePath = "{{ nexus_backup_log }}" nexusBackupDirPath = "{{ nexus_backup_dir }}" nexusDataDirPath = "{{ nexus_data_dir }}" nexusBackupRotate = Boolean.valueOf("{{ nexus_backup_rotate }}") @@ -31,11 +29,6 @@ nexusBackupKeepRotations = "{{ nexus_backup_keep_rotations }}".toInteger() backupDateString = LocalDateTime.now().format(DateTimeFormatter.ofPattern('YYYY-MM-dd-HH-mm-ss')) CurrentBackupDirPath = nexusBackupDirPath+'/blob-backup-'+backupDateString -backupLog = new File(nexusBackupLogFilePath) - -/** Clear log from previous backup */ -backupLog.text = "" - /** Helper function to rotate backups */ def rotateBackup() { if (nexusBackupRotateFirst) { @@ -46,7 +39,7 @@ def rotateBackup() { rotateMsg = "Post-rotating" } - backupLog << rotateMsg + " backups to keep only last "+nexusBackupKeepRotations.toString()+" Backups\n" + log.info(rotateMsg + " backups to keep only last "+nexusBackupKeepRotations.toString()+" Backups") backupDirs = [] backupCounter = 0 backupPattern = ~/blob-backup-.*/ @@ -56,27 +49,27 @@ def rotateBackup() { backupDirs.sort{ it.name }.reverse().each { dir -> backupCounter++ if (backupCounter > realKeep) { - backupLog << "Deleting backup "+ dir.name.toString() + "\n" + log.info("Deleting backup "+ dir.name.toString()) dir.deleteDir() } } if (backupCounter > realKeep) { backupDeleted = backupCounter - realKeep - backupLog << "Deleted a total of "+ backupDeleted + " backup directories\n" + log.info("Deleted a total of "+ backupDeleted + " backup directories") } else { - backupLog << "There where no backup directories to delete\n" + log.info("There were no backup directories to delete") } } try { - backupLog << "Backup directory is "+CurrentBackupDirPath+"\n" + log.info("Backup directory is "+CurrentBackupDirPath) /** pre backup rotation */ if (nexusBackupRotate && nexusBackupRotateFirst) { rotateBackup() } - backupLog << "Create a temporary task to backup nexus db in "+CurrentBackupDirPath+"/db\n" + log.info("Create a temporary task to backup nexus db in "+CurrentBackupDirPath+"/db") TaskScheduler taskScheduler = container.lookup(TaskScheduler.class.getName()) TaskConfiguration tempBackupTaskConfiguration = taskScheduler.createTaskConfigurationInstance('db.backup') tempBackupTaskConfiguration.setName('Temporary db.backup task') @@ -84,17 +77,17 @@ try { Schedule schedule = taskScheduler.scheduleFactory.manual() TaskInfo tempBackupTask = taskScheduler.scheduleTask(tempBackupTaskConfiguration, schedule) - backupLog << "Run the temporary db backup task\n" + log.info("Run the temporary db backup task") tempBackupTask.runNow() - backupLog << "Copy the blobstores into "+CurrentBackupDirPath+"\n" + log.info("Copy the blobstores into "+CurrentBackupDirPath) FileUtils.copyDirectory(new File(nexusDataDirPath+'/blobs'), new File(CurrentBackupDirPath)) - backupLog << "Wait for temporary db backup task to finish\n" + log.info("Wait for temporary db backup task to finish") while (tempBackupTask.currentState.state != TaskInfo.State.WAITING) { TimeUnit.SECONDS.sleep(1) } - backupLog << "Remove temporary task\n" + log.info("Remove temporary task") tempBackupTask.remove() /** Post backup rotation */ @@ -103,5 +96,5 @@ try { } } catch (Exception e) { - backupLog << e.toString() + log.error(e.toString()) } From fa11f9ea5970350c09056b532017ac8816d4f022 Mon Sep 17 00:00:00 2001 From: Olivier Clavel Date: Mon, 24 Sep 2018 16:36:34 +0200 Subject: [PATCH 3/7] Use proper string formatig for backup logging --- templates/backup.groovy.j2 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/templates/backup.groovy.j2 b/templates/backup.groovy.j2 index 58e82d66..981eb008 100644 --- a/templates/backup.groovy.j2 +++ b/templates/backup.groovy.j2 @@ -39,7 +39,7 @@ def rotateBackup() { rotateMsg = "Post-rotating" } - log.info(rotateMsg + " backups to keep only last "+nexusBackupKeepRotations.toString()+" Backups") + log.info("{} backups to keep only last {} backups", rotateMsg, nexusBackupKeepRotations.toString()) backupDirs = [] backupCounter = 0 backupPattern = ~/blob-backup-.*/ @@ -49,27 +49,27 @@ def rotateBackup() { backupDirs.sort{ it.name }.reverse().each { dir -> backupCounter++ if (backupCounter > realKeep) { - log.info("Deleting backup "+ dir.name.toString()) + log.info("Deleting backup {}", dir.name.toString()) dir.deleteDir() } } if (backupCounter > realKeep) { backupDeleted = backupCounter - realKeep - log.info("Deleted a total of "+ backupDeleted + " backup directories") + log.info("Deleted a total of {} backup directories", backupDeleted) } else { log.info("There were no backup directories to delete") } } try { - log.info("Backup directory is "+CurrentBackupDirPath) + log.info("Backup directory is {}", CurrentBackupDirPath) /** pre backup rotation */ if (nexusBackupRotate && nexusBackupRotateFirst) { rotateBackup() } - log.info("Create a temporary task to backup nexus db in "+CurrentBackupDirPath+"/db") + log.info("Create a temporary task to backup nexus db in {}/db", CurrentBackupDirPath) TaskScheduler taskScheduler = container.lookup(TaskScheduler.class.getName()) TaskConfiguration tempBackupTaskConfiguration = taskScheduler.createTaskConfigurationInstance('db.backup') tempBackupTaskConfiguration.setName('Temporary db.backup task') @@ -80,7 +80,7 @@ try { log.info("Run the temporary db backup task") tempBackupTask.runNow() - log.info("Copy the blobstores into "+CurrentBackupDirPath) + log.info("Copy the blobstores into {}", CurrentBackupDirPath) FileUtils.copyDirectory(new File(nexusDataDirPath+'/blobs'), new File(CurrentBackupDirPath)) log.info("Wait for temporary db backup task to finish") From be5fe6d7e5517c351fa64420da8f4f57812e8782 Mon Sep 17 00:00:00 2001 From: Olivier Clavel Date: Mon, 24 Sep 2018 16:39:34 +0200 Subject: [PATCH 4/7] Add some logging for setup_privilege --- files/groovy/setup_privilege.groovy | 2 ++ 1 file changed, 2 insertions(+) diff --git a/files/groovy/setup_privilege.groovy b/files/groovy/setup_privilege.groovy index e5d3112e..2b0b1f7d 100644 --- a/files/groovy/setup_privilege.groovy +++ b/files/groovy/setup_privilege.groovy @@ -32,6 +32,8 @@ privilege.setProperties([ if (update) { authManager.updatePrivilege(privilege) + log.info("Privilege {} updated", parsed_args.name) } else { authManager.addPrivilege(privilege) + log.info("Privilege {} created", parsed_args.name) } From 7d49a24d5d6e64e036bd2bfcd5bcbf3e7cd31e13 Mon Sep 17 00:00:00 2001 From: Olivier Clavel Date: Mon, 24 Sep 2018 16:40:29 +0200 Subject: [PATCH 5/7] Rewrite + add some logging for setup_role --- files/groovy/setup_role.groovy | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/files/groovy/setup_role.groovy b/files/groovy/setup_role.groovy index 4569c677..b1b78995 100644 --- a/files/groovy/setup_role.groovy +++ b/files/groovy/setup_role.groovy @@ -6,23 +6,18 @@ parsed_args = new JsonSlurper().parseText(args) authManager = security.getSecuritySystem().getAuthorizationManager(UserManager.DEFAULT_SOURCE) -def existingRole = null - -try { - existingRole = authManager.getRole(parsed_args.id) -} catch (NoSuchRoleException ignored) { - // could not find role -} - privileges = (parsed_args.privileges == null ? new HashSet() : parsed_args.privileges.toSet()) roles = (parsed_args.roles == null ? new HashSet() : parsed_args.roles.toSet()) -if (existingRole != null) { +try { + existingRole = authManager.getRole(parsed_args.id) existingRole.setName(parsed_args.name) existingRole.setDescription(parsed_args.description) existingRole.setPrivileges(privileges) existingRole.setRoles(roles) authManager.updateRole(existingRole) -} else { + log.info("Role {} updated", parsed_args.name) +} catch (NoSuchRoleException ignored) { security.addRole(parsed_args.id, parsed_args.name, parsed_args.description, privileges.toList(), roles.toList()) + log.info("Role {} created", parsed_args.name) } From ce12fc6663c21a819fbcec0d9f641019a9d3d7c4 Mon Sep 17 00:00:00 2001 From: Olivier Clavel Date: Mon, 24 Sep 2018 16:41:08 +0200 Subject: [PATCH 6/7] Add some logging for setup_user --- files/groovy/setup_user.groovy | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/files/groovy/setup_user.groovy b/files/groovy/setup_user.groovy index ec0d0bab..59c8acec 100644 --- a/files/groovy/setup_user.groovy +++ b/files/groovy/setup_user.groovy @@ -24,16 +24,19 @@ def updateUser(parsed_args) { security.securitySystem.updateUser(user) security.setUserRoles(parsed_args.username, parsed_args.roles) security.securitySystem.changePassword(parsed_args.username, parsed_args.password) + log.info("Updated user {}", parsed_args.username) } def addUser(parsed_args) { security.addUser(parsed_args.username, parsed_args.first_name, parsed_args.last_name, parsed_args.email, true, parsed_args.password, parsed_args.roles) + log.info("Created user {}", parsed_args.username) } def deleteUser(parsed_args) { try { security.securitySystem.deleteUser(parsed_args.username, UserManager.DEFAULT_SOURCE) + log.info("Deleted user {}", parsed_args.username) } catch (UserNotFoundException ignored) { - // No user, so nothing to do + log.info("Delete user: user {} does not exist", parsed_args.username) } } From 9ee79f127b59f5ebd24210f54d070f2fb5326f23 Mon Sep 17 00:00:00 2001 From: Olivier Clavel Date: Mon, 24 Sep 2018 16:43:28 +0200 Subject: [PATCH 7/7] Add some logging for create_blobstore --- files/groovy/create_blobstore.groovy | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/files/groovy/create_blobstore.groovy b/files/groovy/create_blobstore.groovy index 54c023fa..0dca740c 100644 --- a/files/groovy/create_blobstore.groovy +++ b/files/groovy/create_blobstore.groovy @@ -6,7 +6,14 @@ existingBlobStore = blobStore.getBlobStoreManager().get(parsed_args.name) if (existingBlobStore == null) { if (parsed_args.type == "S3") { blobStore.createS3BlobStore(parsed_args.name, parsed_args.config) + msg = "S3 blobstore {} created" } else { blobStore.createFileBlobStore(parsed_args.name, parsed_args.path) + msg = "Created blobstore {} created" } + log.info(msg, parsed_args.name) +} else { + msg = "Blobstore {} already exists. Left untouched" } + +log.info(msg, parsed_args.name)