Advertisement
Guest User

Untitled

a guest
May 16th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. #!groovy
  2. /*
  3.  
  4. !! Managed by Puppet !!
  5. Setup initial bootstrapping user for puppet
  6.  
  7. */
  8.  
  9. import jenkins.model.*
  10. import hudson.security.*
  11. import com.cloudbees.plugins.credentials.*
  12. import hudson.plugins.active_directory.*
  13. import hudson.security.HudsonPrivateSecurityRealm.Details
  14.  
  15. // Code
  16. def instance = Jenkins.getInstance()
  17. def currentRealm = instance.getSecurityRealm().getClass().getName()
  18.  
  19. // bootstrap users
  20. def bootstrapUsers = [
  21. ]
  22.  
  23. if (currentRealm == 'hudson.security.HudsonPrivateSecurityRealm') {
  24. println "--> Jenkins Local Security Realm already configured (HudsonPrivateSecurityRealm)"
  25. } else {
  26. println "--> Configuring Jenkins Local Security Realm (HudsonPrivateSecurityRealm)"
  27. def hudsonRealm = new HudsonPrivateSecurityRealm(false)
  28. instance.setSecurityRealm(hudsonRealm)
  29. println "--> Done configuring Jenkins Local Security Realm"
  30. }
  31.  
  32.  
  33. bootstrapUsers.each { user ->
  34.  
  35. // User config
  36. def username = user['username']
  37. def password = user['password']
  38. def email = user['email']
  39. def full_name = user['full_name']
  40. def ssh_key = user['ssh_key']
  41.  
  42. println "--> Configuring Jenkins Automation User (${username} with ssh_key ${ssh_key})"
  43.  
  44.  
  45. // Get user and create if not exists
  46. def userObject = hudson.model.User.get(username,false)
  47. if (userObject == null) {
  48. println "--> creating new user '${username}'"
  49. userObject = instance.getSecurityRealm().createAccount(username,password)
  50. } else {
  51. println "--> using existing user '${username}'"
  52. }
  53.  
  54.  
  55. // set full name
  56. userObject.setFullName(full_name)
  57.  
  58.  
  59. // password
  60. userObject.addProperty(Details.fromPlainPassword(password))
  61.  
  62. // set email
  63. def email_param = this.class.classLoader.loadClass('hudson.tasks.Mailer$UserProperty').newInstance(email)
  64. userObject.addProperty(email_param)
  65.  
  66.  
  67. // set ssh_key
  68. if ( ssh_key != '' ) {
  69. def keys_param = new org.jenkinsci.main.modules.cli.auth.ssh.UserPropertyImpl(ssh_key)
  70. userObject.addProperty(keys_param)
  71. }
  72.  
  73. // save the user
  74. userObject.save()
  75.  
  76.  
  77. }
  78.  
  79.  
  80.  
  81. println "--> Saving Config"
  82. instance.save()
  83. println "--> Done Saving Config"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement