/*
 * Copyright 2005-2015 Red Hat, Inc.
 *
 * Red Hat licenses this file to you under the Apache License, version
 * 2.0 (the "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */

package io.fabric8.apps.jenkins;

import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.generator.annotation.KubernetesModelProcessor;
import io.fabric8.openshift.api.model.TemplateBuilder;

import javax.inject.Named;

@KubernetesModelProcessor
public class JenkinsModelProcessor {

    @Named("jenkins")
    public void on(ContainerBuilder builder) {
        builder.withNewLifecycle()
                .withNewPostStart()
                .withNewExec()
                .addToCommand("/root/postStart.sh")
                .endExec()
                .endPostStart()
                .endLifecycle()
                .build();
    }

    public void on(TemplateBuilder builder) {
        String version = System.getProperty("project.version");
        String versionPostfix = "";
        if (version != null && version.length() > 0 && !version.endsWith("SNAPSHOT")) {
            versionPostfix = ":" + version;
        }
        
        builder.addNewServiceObject()
                .withNewMetadata()
                     //The name of the service is referenced by the client image.
                    .withName("jenkins")
                        .addToLabels("project", "jenkins")
                        .addToLabels("provider", "fabric8")
                    .endMetadata()
                .withNewSpec()
                .withType("LoadBalancer")
                .addNewPort()
                    .withName("http")
                    .withProtocol("TCP")
                    .withPort(80)
                    .withNewTargetPort(8080)
                .endPort()
                .addNewPort()
                    .withName("agent")
                    .withProtocol("TCP")
                    .withPort(50000)
                    .withNewTargetPort(50000)
                .endPort()
                    .addToSelector("project", "jenkins")
                    .addToSelector("provider", "fabric8")
                .endSpec()
                .endServiceObject()
                .build();

    }
}

@@ -713,16 +713,170 @@
 uilder;%0A
+import io.fabric8.kubernetes.api.model.PodSpecBuilder;%0Aimport io.fabric8.kubernetes.api.model.Volume;%0Aimport io.fabric8.kubernetes.api.model.VolumeMount;%0A
 import i
@@ -1203,32 +1203,36 @@
                 
+    
 .withNewPostStar
@@ -1227,32 +1227,40 @@
 hNewPostStart()%0A
+        
                 
@@ -1270,24 +1270,36 @@
 thNewExec()%0A
+            
             
@@ -1350,24 +1350,32 @@
             
+        
 .endExec()%0A 
@@ -1369,24 +1369,28 @@
  .endExec()%0A
+    
             
@@ -1440,33 +1440,755 @@
 ecycle()
-%0A                
+.build();%0A%0A        for (VolumeMount volumeMount : builder.getVolumeMounts()) %7B%0A            if (volumeMount.getName().equals(%22jenkins-docker-cfg%22)) %7B%0A                return;%0A            %7D%0A        %7D%0A        builder.addNewVolumeMount(%22/home/jenkins/.docker%22, %22jenkins-docker-cfg%22, false).build();%0A    %7D%0A%0A    public void on(PodSpecBuilder builder) %7B%0A        if (builder.getVolumes() != null) %7B%0A            for (Volume volume : builder.getVolumes()) %7B%0A                if (volume.getName().equals(%22jenkins-docker-cfg%22)) %7B%0A                    return;%0A                %7D%0A            %7D%0A        %7D%0A%0A        builder.addNewVolume()%0A                .withName(%22jenkins-docker-cfg%22)%0A                .withNewSecret(%22jenkins-docker-cfg%22)%0A        .endVolume()
 .build()
@@ -2484,24 +2484,16 @@
       %7D%0A
-        
 %0A       
@@ -2648,36 +2648,32 @@
                 
-    
 .withName(%22jenki
@@ -2673,32 +2673,24 @@
 (%22jenkins%22)%0A
-        
             
@@ -2740,32 +2740,24 @@
             
-    
-    
 .addToLabels
@@ -2772,36 +2772,32 @@
 er%22, %22fabric8%22)%0A
-    
                 
@@ -2922,36 +2922,32 @@
                 
-    
 .withName(%22http%22
@@ -2948,20 +2948,16 @@
 %22http%22)%0A
-    
         
@@ -2993,36 +2993,32 @@
                 
-    
 .withPort(80)%0A  
@@ -3011,28 +3011,24 @@
 ithPort(80)%0A
-    
             
@@ -3121,36 +3121,32 @@
                 
-    
 .withName(%22agent
@@ -3148,20 +3148,16 @@
 agent%22)%0A
-    
         
@@ -3193,36 +3193,32 @@
                 
-    
 .withPort(50000)
@@ -3210,36 +3210,32 @@
 withPort(50000)%0A
-    
                 
@@ -3295,36 +3295,32 @@
                 
-    
 .addToSelector(%22
@@ -3332,36 +3332,32 @@
 ct%22, %22jenkins%22)%0A
-    
                 

Add hostpath mount for docker.sock. Also mount jenkins docker config secret.