Templates for groovy init scripts 27/128927/2
authorhyokeun <hyokeun.jeon@samsung.com>
Fri, 12 May 2017 02:42:12 +0000 (11:42 +0900)
committerHyokeun Jeon <hyokeun.jeon@samsung.com>
Fri, 12 May 2017 05:48:54 +0000 (05:48 +0000)
This is used for initializing JENKINS CI server after bootup.
Rename setup.properties.example to setup.properties file
and add required parameters to this property file.

No-operation if missing setup.properties file
or null parameters in each module.

Change-Id: I86faa7677f3e572db72e23a9d33491a7dbc5e2b5

17 files changed:
debian/control
debian/jenkins-scripts-init.install [new file with mode: 0644]
debian/rules
groovy_init_scripts/Module_AmazonEC2Cloud [new file with mode: 0755]
groovy_init_scripts/Module_AmazonEC2Slave [new file with mode: 0755]
groovy_init_scripts/Module_AwsCredentials [new file with mode: 0755]
groovy_init_scripts/Module_Executors [new file with mode: 0755]
groovy_init_scripts/Module_GerritServer [new file with mode: 0755]
groovy_init_scripts/Module_Ldap [new file with mode: 0755]
groovy_init_scripts/Module_Mailer [new file with mode: 0755]
groovy_init_scripts/Module_MatrixAuthorization [new file with mode: 0755]
groovy_init_scripts/Module_Node [new file with mode: 0755]
groovy_init_scripts/Module_RabbitmqConsumer [new file with mode: 0755]
groovy_init_scripts/Module_SshCredentials [new file with mode: 0755]
groovy_init_scripts/init.groovy [new file with mode: 0755]
groovy_init_scripts/setup.properties.example [new file with mode: 0755]
packaging/jenkins-scripts.spec

index 2a03fb8..1c8d9da 100644 (file)
@@ -18,9 +18,10 @@ Depends:  ${python:Depends},
  jenkins-scripts-common,
  jenkins-scripts-submitobs,
  jenkins-scripts-tzs,
+ jenkins-scripts-init,
  python-mysqldb
-Description: image creator for Linux distributions
-  The tool createrep is used to ppfarm test
+Description: Jenkins build job will call this scripts
+  when build job is triggered by Gerrit/OBS event.
 
 Package: jenkins-scripts-common
 Architecture: all
@@ -42,23 +43,21 @@ Depends:  ${python:Depends},
  python-yaml,
  python-lxml,
  gbs-api,
-Description: image creator for Linux distributions
-  The tool createrep is used to ppfarm test
-
-
+Description: Common part of jenkins scripts
 
 Package: jenkins-scripts-tzs
 Architecture: all
 Depends:  ${python:Depends},
  jenkins-scripts-common
-Description: image creator for Linux distributions
-  The tool createrep is used to ppfarm test
-
-
+Description: Jenkins scripts for tzs customer appliance instance
 
 Package: jenkins-scripts-submitobs
 Architecture: all
 Depends:  ${python:Depends},
  jenkins-scripts-common
-Description: image creator for Linux distributions
-  The tool createrep is used to ppfarm test
+Description: Isolated job_submitobs to avoid package installation conflicts
+
+Package: jenkins-scripts-init
+Architecture: all
+Description: Groovy init scripts for jenkins startup
+
diff --git a/debian/jenkins-scripts-init.install b/debian/jenkins-scripts-init.install
new file mode 100644 (file)
index 0000000..4580f69
--- /dev/null
@@ -0,0 +1 @@
+debian/tmp/groovy_init_scripts/* /var/lib/jenkins/init.groovy.d/
index 4f02fae..8e22636 100755 (executable)
@@ -23,7 +23,7 @@ install: build
        # Installing package
        mkdir -p $(CURDIR)/debian/tmp/
        install -d $(CURDIR)/debian/tmp/
-       cp -r job_*.py dir-purge-tool.sh logs-collector.sh common obs_requests templates scripts dep_graph vis $(CURDIR)/debian/tmp/
+       cp -r job_*.py dir-purge-tool.sh logs-collector.sh common obs_requests templates scripts dep_graph vis groovy_init_scripts $(CURDIR)/debian/tmp/
 binary-indep: build install
        dh_testdir
        dh_testroot
diff --git a/groovy_init_scripts/Module_AmazonEC2Cloud b/groovy_init_scripts/Module_AmazonEC2Cloud
new file mode 100755 (executable)
index 0000000..b7e22fe
--- /dev/null
@@ -0,0 +1,18 @@
+
+def create_ec2_cloud = { instance, cloud_name, cloud_credentials_id, cloud_region, cloud_priv_key, cloud_instance_cap, ec2_workers ->
+    def new_cloud = new hudson.plugins.ec2.AmazonEC2Cloud(
+      cloud_name,           // Amazon EC2 Cloud Name
+      false,                // Use EC2 instance profile for credentials
+      cloud_credentials_id, // Credentials ID
+      cloud_region,         // Region
+      cloud_priv_key,       // Private Key
+      cloud_instance_cap,   // Instance Cap
+      ec2_workers           // List<? extends SlaveTemplate> templates
+    )
+    println '+++++ START CLOUD ADDING ' + cloud_name
+    instance.clouds.each {
+      instance.clouds.remove(it)
+    }
+    instance.clouds.add(new_cloud)
+}
+
diff --git a/groovy_init_scripts/Module_AmazonEC2Slave b/groovy_init_scripts/Module_AmazonEC2Slave
new file mode 100755 (executable)
index 0000000..b2cac54
--- /dev/null
@@ -0,0 +1,42 @@
+
+// Configure EC2 cloud auto-scailing workers
+def create_ec2_slave = { ami_id, availability_zone, security_groups, remote_fs, instance_type, labels, description, num_executors, remote_user, subnet_id, tag_name, tag_env, tag_hostname, idle_termination, instance_cap, launch_timeout, ec2_workers ->
+    def worker_ami = new hudson.plugins.ec2.SlaveTemplate(
+      ami_id,                                            // AMI ID
+      availability_zone,                                 // Availability Zone
+      null,                                              // Spot Configuration
+      security_groups,                                   // Security group names
+      remote_fs,                                         // Remote FS root
+      com.amazonaws.services.ec2.model.InstanceType.fromValue(instance_type),             // Instance Type
+      false,                                             // EBS Optimized
+      labels,                                            // Labels
+      hudson.model.Node.Mode.NORMAL,                     // Node.Mode mode
+      description,                                       // Description
+      '',                                                // Init script
+      '',                                                // temporary dir location
+      '',                                                // User Data
+      num_executors,                                     // Number of Executors
+      remote_user,                                       // Remote user
+      new hudson.plugins.ec2.UnixData(null, null),                          // AMI Type
+      '',                                                // JVM Options
+      false,                                             // Stop/Disconnect on Idle Timeout
+      subnet_id,                                         // Subnet ID for VPC
+      [ new hudson.plugins.ec2.EC2Tag('Name', tag_name), // Tags
+        new hudson.plugins.ec2.EC2Tag('env',  tag_env),
+        new hudson.plugins.ec2.EC2Tag('hostname', tag_hostname) ],
+      idle_termination,                                  // Idle termination time
+      false,                                             // Use private NDS
+      instance_cap,                                      // Instance Cap
+      '',                                                // IAM Instance Profile
+      false,                                             // Use ephemeral devices
+      false,                                             // Use dedicated tenancy
+      launch_timeout,                                    // Launch Timeout in seconds
+      false,                                             // Associate Public IP
+      '',                                                // Block device mapping
+      false,                                             // Connect by SSH Process
+      false                                              // Connect using Public IP
+    )
+    ec2_workers.add(worker_ami)
+    println 'WORKER ' + ami_id + ' added...'
+}
+
diff --git a/groovy_init_scripts/Module_AwsCredentials b/groovy_init_scripts/Module_AwsCredentials
new file mode 100755 (executable)
index 0000000..a1401db
--- /dev/null
@@ -0,0 +1,21 @@
+
+// Configure global aws credentials
+def aws_credentials = { instance, id, accessKey, secretKey, description ->
+  // Retrieve the Global credential store
+  def domain = com.cloudbees.plugins.credentials.domains.Domain.global()
+  def store = instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore()
+
+  // Create the AWS credential
+  def my_creds = new com.cloudbees.jenkins.plugins.awscredentials.AWSCredentialsImpl(
+    com.cloudbees.plugins.credentials.CredentialsScope.GLOBAL,
+    id,
+    accessKey,
+    secretKey,
+    description,
+    '',
+    ''
+  )
+
+  store.addCredentials(domain, my_creds)
+}
+
diff --git a/groovy_init_scripts/Module_Executors b/groovy_init_scripts/Module_Executors
new file mode 100755 (executable)
index 0000000..5a5d309
--- /dev/null
@@ -0,0 +1,6 @@
+
+// Set the global number of master executor slots
+def set_num_executors = { instance, num ->
+    instance.setNumExecutors(num)
+}
+
diff --git a/groovy_init_scripts/Module_GerritServer b/groovy_init_scripts/Module_GerritServer
new file mode 100755 (executable)
index 0000000..ee4f9ae
--- /dev/null
@@ -0,0 +1,51 @@
+
+// Configure gerrit trigger servers
+def add_gerrit_trigger = { instance, serverName, noConnectionOnStartup, gerritHostName, gerritSshPort, gerritUserName, gerritAuthKeyFile, gerritFrontEndUrl ->
+
+    def gerritPlugin = instance.getPlugin(com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl.class)
+    gerritPlugin.getPluginConfig().setNumberOfReceivingWorkerThreads(3)
+    gerritPlugin.getPluginConfig().setNumberOfSendingWorkerThreads(1)
+
+    println "  :: Set gerrit trigger server " + serverName
+    def server = new com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer(serverName, noConnectionOnStartup)
+    def config = server.getConfig()
+
+    def triggerConfig = [
+      'gerritHostName':gerritHostName,
+      'gerritSshPort':gerritSshPort,
+      'gerritUserName':gerritUserName,
+      'gerritFrontEndUrl':gerritFrontEndUrl,
+      'gerritAuthKeyFile':gerritAuthKeyFile,
+      'gerritBuildCurrentPatchesOnly':true,
+      'gerritBuildStartedVerifiedValue':0,
+      'gerritBuildStartedCodeReviewValue':0,
+      'gerritBuildSuccessfulVerifiedValue':1,
+      'gerritBuildSuccessfulCodeReviewValue':0,
+      'gerritBuildFailedVerifiedValue':-1,
+      'gerritBuildFailedCodeReviewValue':0,
+      'gerritBuildUnstableVerifiedValue':-1,
+      'gerritBuildUnstableCodeReviewValue':0,
+      'gerritBuildNotBuiltVerifiedValue':0,
+      'gerritBuildNotBuiltCodeReviewValue':0,
+      'enableManualTrigger':true,
+      'enablePluginMessages':true,
+      'buildScheduleDelay':3,
+      'dynamicConfigRefreshInterval':30,
+      'watchdogTimeoutMinutes':0,
+      'verdictCategories': [
+        [ 'verdictValue':'CRVW', 'verdictDescription':'Code Review'],
+        [ 'verdictValue':'VRIF', 'verdictDescription':'Verified']
+      ] as LinkedList
+    ]
+
+    config.setValues(net.sf.json.JSONObject.fromObject(triggerConfig))
+    server.setConfig(config)
+
+    // avoid duplicate servers on the server list
+    if ( gerritPlugin.containsServer(serverName) ) {
+      gerritPlugin.removeServer(gerritPlugin.getServer(serverName))
+    }
+    gerritPlugin.addServer(server)
+
+}
+
diff --git a/groovy_init_scripts/Module_Ldap b/groovy_init_scripts/Module_Ldap
new file mode 100755 (executable)
index 0000000..c3cece8
--- /dev/null
@@ -0,0 +1,25 @@
+
+// Configure global security realm
+def configure_ldap = { instance, ldap_addr, ldap_rootDN, ldap_managerDN, ldap_managerPass, ldap_userSearchBase='', ldap_userSearch='uid={0}', ldap_groupSearchBase='' ->
+    def securityRealm = new hudson.security.LDAPSecurityRealm(
+      server=ldap_addr,
+      rootDN=ldap_rootDN,
+      userSearchBase=ldap_userSearchBase,
+      userSearch=ldap_userSearch,
+      groupSearchBase=ldap_groupSearchBase,
+      groupSearchFilter='',
+      groupMembershipStrategy=null,
+      managerDN=ldap_managerDN,
+      managerPasswordSecret=hudson.util.Secret.fromString(ldap_managerPass),
+      inhibitInferRootDN=false,
+      disableMailAddressResolver=false,
+      cache=null,
+      environmentProperties=null,
+      displayNameAttributeName='',
+      mailAddressAttributeName='',
+      userIdStrategy=null,
+      groupIdStrategy=null
+    )
+    instance.setSecurityRealm(securityRealm)
+}
+
diff --git a/groovy_init_scripts/Module_Mailer b/groovy_init_scripts/Module_Mailer
new file mode 100755 (executable)
index 0000000..1a3400b
--- /dev/null
@@ -0,0 +1,11 @@
+
+// Configure global mail settings
+def set_mailer = { instance, smtp_host, replyto_addr, email_suffix ->
+      def mailer = instance.getDescriptor('hudson.tasks.Mailer')
+      mailer.setSmtpHost(smtp_host)
+      mailer.setReplyToAddress(replyto_addr)
+      mailer.setCharset('UTF-8')
+      mailer.setDefaultSuffix(email_suffix)
+      mailer.save()
+}
+
diff --git a/groovy_init_scripts/Module_MatrixAuthorization b/groovy_init_scripts/Module_MatrixAuthorization
new file mode 100755 (executable)
index 0000000..d7df025
--- /dev/null
@@ -0,0 +1,93 @@
+
+// Configure global matrix authorization priviledges
+def matrix_authorization = { instance, user_mappings ->
+  //def new_strategy = instance.getAuthorizationStrategy() instanceof hudson.security.ProjectMatrixAuthorizationStrategy ? new hudson.security.ProjectMatrixAuthorizationStrategy() : new hudson.security.GlobalMatrixAuthorizationStrategy()
+  def new_strategy = instance.getAuthorizationStrategy()
+  if (new_strategy == null) {
+    new_strategy = new hudson.security.ProjectMatrixAuthorizationStrategy()
+  }
+
+  // http://javadoc.jenkins-ci.org/hudson/security/class-use/Permission.html#hudson.slaves
+  def known_perms_map = [
+     global_admin:                 [ clazz: "jenkins.model.Jenkins", field: "ADMINISTER" ],
+     global_read:                  [ clazz: "jenkins.model.Jenkins", field: "READ" ],
+     global_run_scripts:           [ clazz: "jenkins.model.Jenkins", field: "RUN_SCRIPTS" ],
+     global_upload_plugins:        [ clazz: "hudson.PluginManager",  field: "UPLOAD_PLUGINS" ],
+     global_configure_updatecenter:[ clazz: "hudson.PluginManager",  field: "CONFIGURE_UPDATECENTER" ],
+
+     credentials_create:           [ clazz: "com.cloudbees.plugins.credentials.CredentialsProvider", field: "CREATE" ],
+     credentials_delete:           [ clazz: "com.cloudbees.plugins.credentials.CredentialsProvider", field: "DELETE" ],
+     credentials_manage_domains:   [ clazz: "com.cloudbees.plugins.credentials.CredentialsProvider", field: "MANAGE_DOMAINS" ],
+     credentials_update:           [ clazz: "com.cloudbees.plugins.credentials.CredentialsProvider", field: "UPDATE" ],
+     credentials_view:             [ clazz: "com.cloudbees.plugins.credentials.CredentialsProvider", field: "VIEW" ],
+
+     agent_build:                  [ clazz: "hudson.model.Computer", field: "BUILD" ],
+     agent_configure:              [ clazz: "hudson.model.Computer", field: "CONFIGURE" ],
+     agent_connect:                [ clazz: "hudson.model.Computer", field: "CONNECT" ],
+     agent_create:                 [ clazz: "hudson.model.Computer", field: "CREATE" ],
+     agent_delete:                 [ clazz: "hudson.model.Computer", field: "DELETE" ],
+     agent_disconnect:             [ clazz: "hudson.model.Computer", field: "DISCONNECT" ],
+
+     job_build:                    [ clazz: "hudson.model.Item", field: "BUILD" ],
+     job_cancel:                   [ clazz: "hudson.model.Item", field: "CANCEL" ],
+     job_configure:                [ clazz: "hudson.model.Item", field: "CONFIGURE" ],
+     job_create:                   [ clazz: "hudson.model.Item", field: "CREATE" ],
+     job_delete:                   [ clazz: "hudson.model.Item", field: "DELETE" ],
+     job_discover:                 [ clazz: "hudson.model.Item", field: "DISCOVER" ],
+     job_read:                     [ clazz: "hudson.model.Item", field: "READ" ],
+     job_workspace:                [ clazz: "hudson.model.Item", field: "WORKSPACE" ],
+
+     run_delete:                   [ clazz: "hudson.model.Run", field: "DELETE" ],
+     run_update:                   [ clazz: "hudson.model.Run", field: "UPDATE" ],
+
+     view_configure:               [ clazz: "hudson.model.View", field: "CONFIGURE" ],
+     view_create:                  [ clazz: "hudson.model.View", field: "CREATE" ],
+     view_delete:                  [ clazz: "hudson.model.View", field: "DELETE" ],
+     view_read:                    [ clazz: "hudson.model.View", field: "READ" ],
+
+     scm_tag:                      [ clazz: "hudson.scm", field: "SCM.TAG"],
+     metrics_health_check:         [ clazz: "jenkins.metrics.api.Metrics", field: "HEALTH_CHECK" ],
+     metrics_thread_dump:          [ clazz: "jenkins.metrics.api.Metrics", field: "THREAD_DUMP" ],
+     metrics_view:                [ clazz: "jenkins.metrics.api.Metrics", field: "VIEW" ],
+
+     job_extendedread:             [ clazz: "hudson.model.Item", field: "EXTENDED_READ" ],
+     job_move:                     [ clazz: "com.cloudbees.hudson.plugins.folder.relocate.RelocationAction", field: "RELOCATE"],
+
+     view_replay:                  [ clazz: "org.jenkinsci.plugins.workflow.cps.replay.ReplayAction", field: "REPLAY"],
+  ]
+
+  // create a map of valid permissions depending on what plugins/classes are available
+  def valid_perms_map = [:]
+  known_perms_map.each { key, val ->
+    try {
+      def clazz = Class.forName(val['clazz'], false, this.getClass().getClassLoader())
+      def field = clazz.getField(val['field'])
+      if(field) {
+        valid_perms_map.put(key,field.get(clazz))
+      }
+    } catch (Exception e) {
+    } finally {
+    }
+  }
+
+  def heading = []
+  def user_vals = [:]
+
+
+  user_mappings.eachLine { line ->
+    oneline = line.split(/:/)
+    username = oneline[0]
+    permissions = oneline[1].split(',')
+    user_vals[username] = permissions
+  }
+  user_vals.each { user, list_permissions ->
+    list_permissions.each { k ->
+      if (valid_perms_map.containsKey(k)) {
+        println '  ** ' + user + ' will be added to ' + k
+      }
+    }
+  }
+
+  instance.setAuthorizationStrategy(new_strategy)
+}
+
diff --git a/groovy_init_scripts/Module_Node b/groovy_init_scripts/Module_Node
new file mode 100755 (executable)
index 0000000..2b3a55d
--- /dev/null
@@ -0,0 +1,21 @@
+
+// Configure Slaves
+def create_slave_node = { instance, name, remoteFS, numExecutors, labelString, sshHost, sshPort, sshCredentials, userId ->
+    def myLauncher = new hudson.plugins.sshslaves.SSHLauncher(sshHost, sshPort.toInteger(), sshCredentials, null, null, null, null, 0, 0, 0);
+    def slave = new hudson.slaves.DumbSlave(name,
+                              '',
+                              remoteFS,
+                              numExecutors,
+                              hudson.model.Node.Mode.EXCLUSIVE,
+                              labelString,
+                              myLauncher,
+                              new hudson.slaves.RetentionStrategy.Always())
+    //def slave = new DumbSlave(name, remoteFS, myLauncher)
+    //slave.setLabelString(labelString)
+    //slave.setMode(Node.Mode.EXCLUSIVE)
+    //slave.setNumExecutors(numExecutors.toInteger())
+    //slave.setNodeProperties(new LinkedList())
+    slave.setUserId(userId)
+    instance.addNode(slave)
+}
+
diff --git a/groovy_init_scripts/Module_RabbitmqConsumer b/groovy_init_scripts/Module_RabbitmqConsumer
new file mode 100755 (executable)
index 0000000..95a434c
--- /dev/null
@@ -0,0 +1,19 @@
+
+// Configure rabbit mq consumer settings
+def set_rabbitmq = { instance, enableConsumer, serviceUri, userName, userPassword, applicationId, queueName ->
+      def rabbitmq_consumer = instance.getDescriptor('org.jenkinsci.plugins.rabbitmqconsumer.GlobalRabbitmqConfiguration')
+      def consumeItems = rabbitmq_consumer.getConsumeItems()
+      if (consumeItems == null || consumeItems.empty) {
+          consumeItems = []
+      } else {
+        consumeItems.removeAll { it.getAppId().equals(applicationId) && it.getQueueName().equals(queueName) }
+      }
+      consumeItems.add(new org.jenkinsci.plugins.rabbitmqconsumer.RabbitmqConsumeItem(applicationId, queueName))
+      rabbitmq_consumer.setEnableConsumer(enableConsumer)
+      rabbitmq_consumer.setServiceUri(serviceUri)
+      rabbitmq_consumer.setUserName(userName)
+      rabbitmq_consumer.setUserPassword(userPassword)
+      rabbitmq_consumer.setConsumeItems(consumeItems)
+      rabbitmq_consumer.save()
+}
+
diff --git a/groovy_init_scripts/Module_SshCredentials b/groovy_init_scripts/Module_SshCredentials
new file mode 100755 (executable)
index 0000000..96e7178
--- /dev/null
@@ -0,0 +1,21 @@
+
+// Configure global ssh credentials
+def ssh_credentials = { instance, id, username, privateKeyFile, description, passphrase='' ->
+  // Retrieve the Global credential store
+  def domain = com.cloudbees.plugins.credentials.domains.Domain.global()
+  def store = instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore()
+
+  def privateKeySource = new com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.FileOnMasterPrivateKeySource(privateKeyFile)
+
+  // Create the SSH credential
+  def my_creds = new com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey(
+    com.cloudbees.plugins.credentials.CredentialsScope.GLOBAL,
+    id,
+    username,
+    privateKeySource,
+    passphrase,
+    description
+  )
+  store.addCredentials(domain, my_creds)
+}
+
diff --git a/groovy_init_scripts/init.groovy b/groovy_init_scripts/init.groovy
new file mode 100755 (executable)
index 0000000..3361a2c
--- /dev/null
@@ -0,0 +1,232 @@
+import hudson.model.*
+import jenkins.model.*
+
+import java.util.logging.Logger
+Logger logger = Logger.getLogger("ecs-cluster")
+
+def env = System.getenv()
+def j = Jenkins.getInstance()
+def scripts_dir = env['JENKINS_HOME'] + '/init.groovy.d/'
+
+def e = { filepath ->
+  evaluate(new File(scripts_dir + filepath))
+}
+
+def mx = { filepath ->
+  def script = new GroovyScriptEngine( '.' ).with {
+    loadScriptByName( scripts_dir + filepath )
+  }
+  this.metaClass.mixin script
+}
+
+Properties properties = new Properties()
+try {
+  File propertiesFile = new File(scripts_dir + 'setup.properties')
+  propertiesFile.withInputStream {
+    properties.load(it)
+  }
+} catch (Exception err) {
+  println ' Sorry no such file...'
+  return -1
+}
+
+///////////////////////////////////////////////////////////////////
+///////////////////  E-mail Notification  /////////////////////////
+///////////////////////////////////////////////////////////////////
+if (properties.SMTP_HOST != null && properties.SMTP_HOST != '') {
+  logger.info('++++ SET MAILER ' + properties.SMTP_HOST);
+  def set_mailer = e('Module_Mailer')
+  set_mailer(
+    instance     = j,
+    smtp_host    = properties.SMTP_HOST,
+    replyto_addr = properties.REPLYTO_ADDR,
+    email_suffix = ''
+  )
+  instance.save()
+}
+
+///////////////////////////////////////////////////////////////////
+////////////////////  LDAP configuration  /////////////////////////
+///////////////////////////////////////////////////////////////////
+if (properties.LDAP_ADDR != null && properties.LDAP_ADDR != '') {
+  logger.info('++++ SET LDAP ' + properties.LDAP_ADDR);
+  def configure_ldap       = e('Module_Ldap')
+  configure_ldap(
+    instance         = j,
+    ldap_addr        = properties.LDAP_ADDR,
+    ldap_rootDN      = properties.LDAP_ROOT_DN,
+    ldap_managerDN   = properties.LDAP_MANAGER_DN,
+    ldap_managerPass = properties.LDAP_MANAGER_PASS,
+    ldap_userSearchBase  = properties.LDAP_USER_SEARCH_BASE,
+    ldap_userSearch      = properties.LDAP_USER_SEARCH,
+    ldap_groupSearchBase = properties.LDAP_GROUP_SEARCH_BASE
+  )
+  instance.save()
+}
+
+///////////////////////////////////////////////////////////////////
+//////////////  Number of executors on Master node  ///////////////
+///////////////////////////////////////////////////////////////////
+if (properties.NUM_EXECUTORS != null && properties.NUM_EXECUTORS != '') {
+  logger.info('++++ SET ECECUTORS ' + properties.NUM_EXECUTORS.toInteger());
+  def set_num_executors    = e('Module_Executors')
+  set_num_executors(
+    instance = j,
+    num      = properties.NUM_EXECUTORS.toInteger()
+  )
+  instance.save()
+}
+
+///////////////////////////////////////////////////////////////////
+/////////////////////  SSH Credentials  ///////////////////////////
+///////////////////////////////////////////////////////////////////
+if (properties.CRED_LISTS != null && properties.CRED_LISTS != '') {
+  def create_ssh_credential = e('Module_SshCredentials')
+  properties.CRED_LISTS.eachLine { line ->
+    oneline = line.split(/:/)
+    id = oneline[0]
+    configs = oneline[1].split(',')
+    logger.info('++++ CREATE SSH CREDENTIAL ' + id + ' ' + configs);
+    create_ssh_credential(
+        instance       = j,
+        id             = id,
+        username       = configs[0],
+        privateKeyFile = configs[1],
+        description    = configs[2],
+        passphrase     = ''
+    )
+    instance.save()
+  }
+}
+
+///////////////////////////////////////////////////////////////////
+//////////////////////  AWS Credentials  //////////////////////////
+///////////////////////////////////////////////////////////////////
+if (properties.CRED_AWS_ID != null && properties.CRED_AWS_ID != '') {
+  logger.info('++++ CREATE AWS CREDENTIAL ' + properties.CRED_AWS_DESC);
+  def create_aws_credential = e('Module_AwsCredentials')
+  create_aws_credential(
+    instance    = j,
+    id          = properties.CRED_AWS_ID,
+    accessKey   = properties.CRED_AWS_ACCESSKEY,
+    secretKey   = properties.CRED_AWS_SECRETKEY,
+    description = properties.CRED_AWS_DESC
+  )
+  instance.save()
+}
+
+///////////////////////////////////////////////////////////////////
+///////////////////  Authorization strategy  //////////////////////
+///////////////////////////////////////////////////////////////////
+if (properties.USER_AUTH_MAP != null && properties.USER_AUTH_MAP != '') {
+  logger.info('++++ SET AUTHORIZATION ');
+  def set_matrix_authorization = e('Module_MatrixAuthorization')
+  set_matrix_authorization(
+    instance = j,
+    user_mappings = properties.USER_AUTH_MAP
+  )
+  instance.save()
+}
+
+///////////////////////////////////////////////////////////////////
+////////////////////////  Slave Nodes  ////////////////////////////
+///////////////////////////////////////////////////////////////////
+if (properties.NODES_LISTS != null && properties.NODES_LISTS != '') {
+  def create_slave_node    = e('Module_Node')
+  properties.NODES_LISTS.eachLine { line ->
+    oneline = line.split(/:/)
+    name = oneline[0]
+    configs = oneline[1].split(',')
+    logger.info('++++ CREATE SLAVE NODE ' + name + ' ' + configs);
+    create_slave_node(
+        instance = j,
+        name           = name,
+        remoteFS       = configs[0],
+        numExecutors   = configs[1],
+        labelString    = configs[2],
+        sshHost        = configs[3],
+        sshPort        = configs[4],
+        sshCredentials = configs[5],
+        userId         = configs[6]
+    )
+    instance.save()
+  }
+}
+
+///////////////////////////////////////////////////////////////////
+///////////////////////  Gerrit Trigger  //////////////////////////
+///////////////////////////////////////////////////////////////////
+if (properties.GERRIT_SERVER_NAME != null && properties.GERRIT_SERVER_NAME != '') {
+  logger.info('++++ SET GERRIT TRIGGER ' + properties.GERRIT_HOST_NAME);
+  def add_gerrit_trigger   = e('Module_GerritServer')
+  add_gerrit_trigger(
+    instance             = j,
+    serverName           = properties.GERRIT_SERVER_NAME,
+    noConnetionOnStartup = properties.GERRIT_NO_CONNECTION_ON_STARTUP.toBoolean(),
+    gerritHostName       = properties.GERRIT_HOST_NAME,
+    gerritSshPort        = properties.GERRIT_SSH_PORT,
+    gerritUserName       = properties.GERRIT_USER_NAME,
+    gerritAuthKeyFile    = properties.GERRIT_AUTH_KEYFILE,
+    gerritFrontEndUrl    = properties.GERRIT_FRONTEND_URL
+  )
+  instance.save()
+}
+
+///////////////////////////////////////////////////////////////////
+/////////////////////  Rabbitmq consumer  /////////////////////////
+///////////////////////////////////////////////////////////////////
+if (properties.RABBITMQ_SERVICE_URI != null && properties.RABBITMQ_SERVICE_URI != '') {
+  logger.info('++++ SET RABBIT MQ CONSUMER ' + properties.RABBITMQ_SERVICE_URI);
+  def set_rabbitmq = e('Module_RabbitmqConsumer')
+  set_rabbitmq(
+    instance       = j,
+    enableConsumer = properties.RABBITMQ_ENABLE.toBoolean(),
+    serviceUri     = properties.RABBITMQ_SERVICE_URI,
+    userName       = properties.RABBITMQ_USER_NAME,
+    userPassword   = properties.RABBITMQ_USER_PASSOWRD,
+    applicationId  = properties.RABBITMQ_APP_ID,
+    queueName      = properties.RABBITMQ_QUEUE_NAME
+  )
+  instance.save()
+}
+
+///////////////////////////////////////////////////////////////////
+//////////////////////  Amazon EC2 Cloud  /////////////////////////
+///////////////////////////////////////////////////////////////////
+if (properties.EC2_CLOUD_NAME != null && properties.EC2_CLOUD_NAME != '') {
+  logger.info('++++ SET AWS WORKERS ' + properties.AMI_ID);
+  def ec2_workers = []
+  def create_ec2_slave     = e('Module_AmazonEC2Slave')
+  def create_ec2_cloud     = e('Module_AmazonEC2Cloud')
+  create_ec2_slave(
+    ami_id               = properties.AMI_ID,
+    availability_zone    = properties.AV_ZONE,
+    security_groups      = properties.SECURITY_GROUPS,
+    remote_fs            = properties.REMOTE_FS,
+    instance_type        = properties.INSTANCE_TYPE,
+    lagels               = properties.WORKER_LABEL_STRING,
+    description          = properties.WORKER_DESCRIPTION,
+    num_executors        = properties.NUMBER_OF_EXECUTORS,
+    remote_user          = properties.REMOTE_ADMIN,
+    subnet_id            = properties.SUBNET_ID,
+    tag_name             = properties.EC2_TAG_NAME,
+    tag_env              = properties.EC2_TAG_ENV,
+    tag_hostname         = properties.EC2_TAG_HOSTNAME,
+    idle_termination     = properties.IDLE_TERMINATION_MINUTES,
+    instance_cap         = properties.INSTANCE_CAP_STR,
+    launch_timeout       = properties.LAUNCH_TIMEOUT,
+    ec2_workers          = ec2_workers
+  )
+
+  create_ec2_cloud(
+    instance             = j,
+    cloud_name           = properties.EC2_CLOUD_NAME,
+    cloud_credentials_id = properties.EC2_CLOUD_CREDENTIALS_ID,
+    cloud_region         = properties.EC2_CLOUD_REGION,
+    cloud_priv_key       = properties.EC2_CLOUD_PRIV_KEY_TXT,
+    cloud_instance_cap   = properties.EC2_CLOUD_INSTANCE_CAP,
+    ec2_workers          = ec2_workers
+  )
+  instance.save()
+}
+
diff --git a/groovy_init_scripts/setup.properties.example b/groovy_init_scripts/setup.properties.example
new file mode 100755 (executable)
index 0000000..f37b1ec
--- /dev/null
@@ -0,0 +1,84 @@
+# Number of executors on master node
+NUM_EXECUTORS=
+
+# System-wide E-mail notification
+SMTP_HOST=
+REPLYTO_ADDR=
+
+# Ldap configuration
+LDAP_ADDR=
+LDAP_ROOT_DN=
+LDAP_MANAGER_DN=
+LDAP_MANAGER_PASS=
+LDAP_USER_SEARCH_BASE=
+LDAP_USER_SEARCH=
+LDAP_GROUP_SEARCH_BASE=
+
+# User ahthorization mapping
+# username:[permissions,]
+#   Possible permissions: 
+#     global_admin, global_read, global_run_scripts, global_upload_plugins, global_configure_updatecenter
+#     credentials_create, credentials_delete, credentials_manage_domains, credentials_update, credentials_view
+#     agent_build, agent_configure, agent_connect, agent_create, agent_delete, agent_disconnect
+#     job_build, job_cancel, job_configure, job_create, job_delete, job_discover, job_read, job_workspace
+#     run_delete, run_update
+#     view_configure, view_create, view_delete, view_read
+#USER_AUTH_MAP=username:global_read,job_read,job_build
+USER_AUTH_MAP=
+
+# SSH Credentials for master, imager, backend, download
+#CRED_LISTS=credential_id_jenkins:jenkins,/var/lib/jenkins/.ssh/id_rsa,jenkins
+CRED_LISTS=
+
+# AWS Credentials for image worker
+CRED_AWS_ID=
+CRED_AWS_ACCESSKEY=
+CRED_AWS_SECRETKEY=
+CRED_AWS_DESC=
+
+# Gerrit trigger configuration
+GERRIT_SERVER_NAME=
+GERRIT_NO_CONNECTION_ON_STARTUP=
+GERRIT_HOST_NAME=
+GERRIT_SSH_PORT=
+GERRIT_USER_NAME=
+GERRIT_FRONTEND_URL=
+GERRIT_AUTH_KEYFILE=
+
+# Slave node configuration
+#NODES_LISTS=node_name:/home/jenkins,1,label_name,ssh_host,ssh_port,credential_id,username
+NODES_LISTS=
+
+# Rabbitmq consumer configuration
+RABBITMQ_ENABLE=
+RABBITMQ_SERVICE_URI=
+RABBITMQ_USER_NAME=
+RABBITMQ_USER_PASSOWRD=
+RABBITMQ_APP_ID=
+RABBITMQ_QUEUE_NAME=
+
+# AWS EC2 Cloud configuration
+EC2_CLOUD_NAME=
+EC2_CLOUD_CREDENTIALS_ID=
+EC2_CLOUD_REGION=
+EC2_CLOUD_INSTANCE_CAP=
+EC2_CLOUD_PRIV_KEY_TXT=
+
+# AWS EC2 ami configuraion for image worker
+AMI_ID=
+AV_ZONE=
+SECURITY_GROUPS=
+REMOTE_FS=
+INSTANCE_TYPE=
+WORKER_LABEL_STRING=
+WORKER_DESCRIPTION=
+NUMBER_OF_EXECUTORS=
+REMOTE_ADMIN=
+SUBNET_ID=
+EC2_TAG_NAME=
+EC2_TAG_ENV=
+EC2_TAG_HOSTNAME=
+IDLE_TERMINATION_MINUTES=
+INSTANCE_CAP_STR=
+LAUNCH_TIMEOUT=
+
index 300256f..23a85bd 100644 (file)
@@ -17,9 +17,11 @@ Source:         %{name}-%{version}.tar.gz
 Requires:       %{name}-common = %{version}-%{release}
 Requires:       %{name}-submitobs = %{version}-%{release}
 Requires:       %{name}-tzs = %{version}-%{release}
+Requires:       %{name}-init = %{version}-%{release}
 BuildArch:      noarch
 
 %define destdir /var/lib/jenkins/%{name}
+%define destinitdir /var/lib/jenkins/init.groovy.d
 
 %description
 Jenkins build job will call this scripts when build job is triggered by
@@ -52,6 +54,13 @@ Requires:   %{name}-common = %{version}-%{release}
 %description tzs
 Jenkins scripts for tzs customer appliance instance
 
+%package init
+Summary:    Groovy init scripts for jenkins startup
+Group:      Development/Tools/Building
+
+%description init
+Groovy init scripts for jenkins startup
+
 %package submitobs
 Summary:    Temporary package to isolate job_submitobs
 Group:      Development/Tools/Building
@@ -68,6 +77,8 @@ Isolated job_submitobs to avoid package installation conflicts
 %install
 install -d %{buildroot}%{destdir}
 cp -r job_*.py dir-purge-tool.sh logs-collector.sh common obs_requests templates scripts vis dep_graph %{buildroot}%{destdir}/
+install -d %{buildroot}%{destinitdir}
+cp -r groovy_init_scripts/* %{buildroot}%{destinitdir}/
 
 %post common
 if [ ! -d /var/lib/jenkins/userContent ]; then
@@ -212,6 +223,24 @@ fi
 %defattr(-,jenkins,jenkins)
 %{destdir}/job_submit.py
 
+%files init
+%defattr(-,jenkins,jenkins)
+%dir /var/lib/jenkins/
+%dir %{destinitdir}/
+%{destinitdir}/init.groovy
+%{destinitdir}/setup.properties.example
+%{destinitdir}/Module_AmazonEC2Cloud
+%{destinitdir}/Module_AmazonEC2Slave
+%{destinitdir}/Module_AwsCredentials
+%{destinitdir}/Module_Executors
+%{destinitdir}/Module_GerritServer
+%{destinitdir}/Module_Ldap
+%{destinitdir}/Module_Mailer
+%{destinitdir}/Module_MatrixAuthorization
+%{destinitdir}/Module_Node
+%{destinitdir}/Module_RabbitmqConsumer
+%{destinitdir}/Module_SshCredentials
+
 %files submitobs
 %defattr(-,jenkins,jenkins)
 %{destdir}/job_submitobs.py