Skip to content

Commit

Permalink
Merge pull request #115 from zeitounator/feat/scriptLogToNexusLog
Browse files Browse the repository at this point in the history
Groovy scripts should log to nexus log
  • Loading branch information
zeitounator authored Sep 24, 2018
2 parents 1260b79 + 9ee79f1 commit 042ef88
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 33 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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=<YYYY-MM-dd-HH-mm-ss>`
(e.g. 2017-12-17-21-00-00 for 17th of December 2017 at 21h00m00s)
Expand Down
1 change: 0 additions & 1 deletion defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions files/groovy/create_blobstore.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions files/groovy/setup_privilege.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
15 changes: 5 additions & 10 deletions files/groovy/setup_role.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 4 additions & 1 deletion files/groovy/setup_user.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
29 changes: 11 additions & 18 deletions templates/backup.groovy.j2
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import java.util.concurrent.TimeUnit
* 7- If <nexusBackupRotate> is true and <nexusBackupRotateFirst> is false, post-rotate backup dirs keeping last <nexusBackupKeepRotations> backups
*/


nexusBackupLogFilePath = "{{ nexus_backup_log }}"
nexusBackupDirPath = "{{ nexus_backup_dir }}"
nexusDataDirPath = "{{ nexus_data_dir }}"
nexusBackupRotate = Boolean.valueOf("{{ nexus_backup_rotate }}")
Expand All @@ -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) {
Expand All @@ -46,7 +39,7 @@ def rotateBackup() {
rotateMsg = "Post-rotating"
}

backupLog << rotateMsg + " backups to keep only last "+nexusBackupKeepRotations.toString()+" Backups\n"
log.info("{} backups to keep only last {} backups", rotateMsg, nexusBackupKeepRotations.toString())
backupDirs = []
backupCounter = 0
backupPattern = ~/blob-backup-.*/
Expand All @@ -56,45 +49,45 @@ 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 {} backup directories", backupDeleted)
} 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 {}/db", CurrentBackupDirPath)
TaskScheduler taskScheduler = container.lookup(TaskScheduler.class.getName())
TaskConfiguration tempBackupTaskConfiguration = taskScheduler.createTaskConfigurationInstance('db.backup')
tempBackupTaskConfiguration.setName('Temporary db.backup task')
tempBackupTaskConfiguration.setString('location', CurrentBackupDirPath+'/db')
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 */
Expand All @@ -103,5 +96,5 @@ try {
}

} catch (Exception e) {
backupLog << e.toString()
log.error(e.toString())
}

0 comments on commit 042ef88

Please sign in to comment.