Ravi Srinivasan
2019-01-14 49e3a8adcdc9805754f3c6f6c075f3a3e3571c6c
commit | author | age
5d0992 1 # The Manual Menace
1173e5 2 > In this exercise learners will use Ansible to drive automated provisioning of Projects in OpenShift, Git, Jenkins and Nexus.
c951f7 3
49e3a8 4 <!-- ![automation-xkcd](https://imgs.xkcd.com/comics/automation.png) -->
7c832b 5
D 6 ## Exercise Intro
e43fd2 7 In this exercise we will use automation tooling to create Project namespaces for our `CI/CD` tooling along with the `dev` and `test` namespaces for our deployments to live. We do this manually using the OpenShift CLI; but as we go from cluster to cluster or project to project Dev and Ops teams often find themselves having to redo these tasks again and again. Configuring our cluster using code; we can easily store this in Git and repeat the process again and again. By minimising the time taken to do these repetitive tasks we can accelerate our ability to deliver value to our customers; working on the hard problems they face.
7c832b 8
e43fd2 9 This exercise uses Ansible to drive the creation of the cluster content. In particular; we'll use a play book called the `OpenShift Applier`. Once the project namespace have been created; we will add some tools to support CI/CD such as Jenkins, Git and Nexus. These tools will be needed by later lessons to automate the build and deploy of our apps. Again; we will use OpenShift Templates and drive their creation in the cluster using Ansible. To prove things are working, finally we'll delete all our content and re-apply the inventory to re-create our cluster's content.
5d0992 10
1173e5 11 #### Why is config-as-code important?
14cd2d 12 * Assurance - Prevents unwanted config changes from people making arbitrary changes to environments. No more Snowflake servers!
1173e5 13 * Traceability - Committing config as code means a user has approved and changes can be tracked.
14cd2d 14 * Phoenix Server -  Burn it all to the ground and bring it back; exactly the way it was!
D 15
5d0992 16 _____
c951f7 17
D 18 ## Learning Outcomes
19 As a learner you will be able to
5d0992 20
D 21 1. Run the OpenShift Applier to automate creating cluster content
22 1. Create and admin project namespaces in OpenShift
23 1. Deploy commonly used applications to support the development process
c951f7 24
D 25 ## Tools and Frameworks
26
5d0992 27 * [GitLab](https://about.gitlab.com/) - Community driven Git server now with integrated DevOps Toolchain.
D 28 * [Nexus](https://www.sonatype.com/nexus-repository-sonatype) - Repository manager for storing lots of application types. Can also host `npm` and `Docker` registries.
29 * [Jenkins](https://jenkins.io/) - OpenSource Build automation server. Highly customisable with plugins.
30 * [Ansible](https://www.ansible.com/) - IT Automation tool used to provision and manage state of cloud and physical infrastructure.
dba30b 31 * [OpenShift Applier](https://github.com/redhat-cop/openshift-applier) - used to apply OpenShift objects to an OpenShift Cluster.
A 32
5d0992 33 ## Big Picture
1173e5 34 > The Big Picture is our emerging architecture; starting with an empty cluster we populate it with projects and some ci/cd tooling.
5d0992 35
664135 36 ![big-picture](../images/big-picture/big-picture-1.jpg)
f52b28 37
5d0992 38 _____
c951f7 39
49e3a8 40 <!-- ## 10,000 Ft View
1173e5 41 > This exercise is aimed at the creation of the tooling that will be used to support the rest of the Exercises. The high-level goal is to create a collection of project namespaces and populate them with Git, Jenkins & Nexus.
5d0992 42
1173e5 43 If you're feeling confident and don't want to follow the step-by-step guide these high-level instructions should provide a challenge for you:
5d0992 44
2533e0 45 1. Clone the repo `https://github.com/rht-labs/enablement-ci-cd` which contains the scaffold of the project. Ensure you get all remote branches.
3558ae 46
5d0992 47 2. Create `<your-name>-ci-cd`, `<your-name>-dev` and `<your-name>-test` project namespaces using the inventory and run them with the OpenShift Applier to populate the cluster
da614f 48
2533e0 49 3. Use the templates provided to create build of the jenkins-s2i. The templates are in `exercise1/jenkins-s2i`
3558ae 50
2533e0 51 4. Use the templates provided to create build and deployment configs in `<your-name>-ci-cd` for. Templates are on a branch called `exercise1/git-nexus` && `exercise1/jenkins`:
5d0992 52     * Nexus
D 53     * GitLab
1173e5 54     * Jenkins (using an s2i to pre-configure Jenkins)
3558ae 55
2533e0 56 5. Commit your `enablement-ci-cd` repository to the GitLab Instance you've created
3558ae 57
2533e0 58 6. Burn it all down and re-apply your inventory proving config-as-code works.
49e3a8 59 -->
c951f7 60
D 61 ## Step by Step Instructions
49e3a8 62 <!-- > This is a structured guide with references to exact filenames and explanations.  -->
c951f7 63
bc2e43 64 ### Part 1 - Create OpenShift Projects
de8ebb 65 > _Using the OpenShift Applier, we will add new project namespaces to the cluster which will be used throughout the exercise._
D 66
49e3a8 67 1. In this course three different git projects will be created. To setup your local machine for each of these, create a new folder on the terminal in the root of your HOME directory for convenience. To do this, open a new Terminal session and create the new folder using the following command (new terminal sessions will start in your HOME dir).
RS 68
69 For Linux and MacOS systems
bc2e43 70 ```bash
49e3a8 71 mkdir -p ~/do500-workspace && cd ~/do500-workspace
RS 72 ```
73
74 For Microsoft Windows systems, navigate to the `C:\do500-workspace` directory
75 ```bash
76 cd C:\do500-workspace
e7c877 77 ```
f5f1ff 78 <p class="tip">
49e3a8 79 <b>NOTE</b> - If you do not want to have this folder at the root of your home directory, that's fine, just ensure any parent directories of this `do500-workspace` folder do <b>NOT</b> have any spaces in them as it breaks Ansible in later labs...
f5f1ff 80 </p>
D 81
49e3a8 82 2. Clone the scaffold project to your local machine's `do500-workspace` folder and pull all remote branches for use in later exercises. You may see an error saying `fatal: A branch named 'develop' already exists.` This error can be safely ignored.
RS 83
f5f1ff 84 ```bash
D 85 git clone https://github.com/rht-labs/enablement-ci-cd && cd enablement-ci-cd
e7c877 86 ```
f5f1ff 87 ```bash
D 88 ./git-pull-all.sh
49e3a8 89 ```
RS 90
91 If you are using a Microsoft Windows system, run the above shell script using the `Git Bash` terminal instead of the default Windows command line. You need to navigate to the `C:\do500-workspace\enablement-ci-cd` directory by running
92 ```bash
93 cd /c/do500-workspace/enablement-ci-cd
94 ```
95 ```bash
96 ./git-pull-all.sh
97 ```
98 ```bash
99 exit
c951f7 100 ```
D 101
0b2c0f 102 3. Open the `enablement-ci-cd` folder in VSCode (or your favourite editor). The project is laid out as follows
bc2e43 103 ```
D 104 .
105 ├── README.md
62db52 106 ├── apply.yml
bc2e43 107 ├── docker
D 108 ├── inventory
62db52 109 │   ├── host_vars
D 110 │   │   ├── ci-cd-tooling.yml
111 │   │   └── projects-and-policies.yml
112 │   └── hosts
bc2e43 113 ├── jenkins-s2i
D 114 ├── params
62db52 115 │   └── project-requests-ci-cd
bc2e43 116 ├── requirements.yml
D 117 └── templates
62db52 118     └── project-requests.yml
bc2e43 119 ```
49e3a8 120  * `docker` folder contains sample Dockerfiles for our jenkins-slave images that will be used by the builds.
bc2e43 121  * `jenkins-s2i` contains the configuration and plugins we want to bring jenkins to life with
D 122  * `params` houses the variables we will load the templates with
123  * `templates` is a collection of OpenShift templates
62db52 124  * `inventory/host_vars/*.yml` is the collection of objects we want to insert into the cluster.
bc2e43 125  * `requirements.yml` is a manifest which contains the ansible modules needed to run the playbook
62db52 126  * `apply.yml` is a playbook that sets up some variables and runs the OpenShift Applier role.
bc2e43 127
49e3a8 128 4. Open the `apply.yml` file in the root of the project. Update the namespace variables by replacing the `<YOUR_NAME>` with your name or initials. Don't use uppercase or special characters. For example; my name is Dónal so I've created:
62db52 129 ```yaml
e6805f 130   hosts: "{{ target }}"
62db52 131   vars:
D 132     ci_cd_namespace: donal-ci-cd
133     dev_namespace: donal-dev
134     test_namespace: donal-test
e6805f 135   tasks:
62db52 136 ```
e6805f 137 <p class="tip">
49e3a8 138 NOTE - YAML is indentation sensitive so keep things lined up properly!
e6805f 139 </p>
62db52 140
49e3a8 141 5. Open the `inventory/host_vars/projects-and-policies.yml` file; you should see some variables setup already to create the `<YOUR_NAME>-ci-cd` namespace. This object is passed to the OpenShift Applier to call the `templates/project-requests.yml` template with the `params/project-requests-ci-cd` parameters. We will add some additional content here but first let's explore the parameters and the template
62db52 142
49e3a8 143 6. Open the `params/project-requests-ci-cd` and replace the `<YOUR_NAME>` with your name to create the corresponding projects in the cluster.
ff1bd7 144 ![new-item](../images/exercise1/ci-cd-project-namespace.png)
bc2e43 145
49e3a8 146 7. Let's add two more params files to pass to our template to be able to create a `dev` and `test` project.
1173e5 147   * Create another two params files `params/project-requests-dev` & `params/project-requests-test`. On the terminal run
da614f 148 ```bash
f5f1ff 149 touch params/project-requests-dev params/project-requests-test
da614f 150 ```
f5f1ff 151   * In your editor; Open `params/project-requests-dev` and add the following by substituting `<YOUR_NAME>` accordingly
62db52 152 ```
D 153 NAMESPACE=<YOUR_NAME>-dev
154 NAMESPACE_DISPLAY_NAME=<YOUR-NAME> Dev
155 ```
f5f1ff 156   * In your editor; Open `params/project-requests-test` and add the following by substituting `<YOUR_NAME>` accordingly
62db52 157 ```
D 158 NAMESPACE=<YOUR_NAME>-test
159 NAMESPACE_DISPLAY_NAME=<YOUR-NAME> Test
160 ```
bc2e43 161
49e3a8 162 8. In the `inventory/host_vars/projects-and-policies.yml` file; add the new objects for the projects you want to create (dev & test) by adding another object to the content array for each. You can copy and paste them from the `ci-cd` example and update them accordingly. If you do this; remember to change the params file! e.g.
bc2e43 163 ```yaml
62db52 164     - name: "{{ dev_namespace }}"
D 165       template: "{{ playbook_dir }}/templates/project-requests.yml"
ff1bd7 166       template_action: create
62db52 167       params: "{{ playbook_dir }}/params/project-requests-dev"
ff1bd7 168       tags:
D 169       - projects
62db52 170     - name: "{{ test_namespace }}"
D 171       template: "{{ playbook_dir }}/templates/project-requests.yml"
ff1bd7 172       template_action: create
62db52 173       params: "{{ playbook_dir }}/params/project-requests-test"
ff1bd7 174       tags:
D 175       - projects
bc2e43 176 ```
ff1bd7 177 ![project-request-yaml](../images/exercise1/project-request-yml.png)
bc2e43 178
49e3a8 179 For Microsoft Windows systems, you need to run Ansible and OpenShift client commands from inside the `do500-toolbox` container.
RS 180
181 <p class="tip">
182 NOTE - On Microsoft Windows systems, we recommend you keep the container running for the duration of the lab. Run all Ansible and OpenShift client ("oc") CLI commands from inside the container. Do NOT launch the container on Linux and MacOS systems, since you should already have Ansible and the OpenShift client natively installed on your system by following the pre-requisites setup guide.
183 </p>
184
185 9. Launch the toolbox container using the Windows command line terminal, and navigate to the `enablement-ci-cd` directory inside the container
186 ```bash
187 docker run -it -v C:/do500-workspace:/home/tool-box/workarea:Z quay.io/jrigsbee/do500-toolbox /bin/bash
188 bash-4.4$ cd workarea/enablement-ci-cd
189 ```
190
191 10. With the configuration in place; install the OpenShift Applier dependency
bc2e43 192 ```bash
f5f1ff 193 ansible-galaxy install -r requirements.yml --roles-path=roles
bc2e43 194 ```
D 195
49e3a8 196 11. Apply the inventory by logging into OpenShift on the terminal and running the playbook as follows (<CLUSTER_URL> should be replaced with the one you've been provided by the instructor). Accept any insecure connection warning 👍:
bc2e43 197 ```bash
530a25 198 oc login <CLUSTER_URL>
f5f1ff 199 ```
D 200 ```bash
201 ansible-playbook apply.yml -i inventory/ -e target=bootstrap
62db52 202 ```
D 203 where the `-e target=bootstrap` is passing an additional variable specifying that we run the `bootstrap` inventory
bc2e43 204
49e3a8 205 12. Once successful you should see an output similar to this (Cows not included): ![playbook-success](../images/exercise1/play-book-success.png)
bc2e43 206
49e3a8 207 13. You can check to see the projects have been created successfully by running
e6805f 208 ```bash
f5f1ff 209 oc projects
e6805f 210 ```
D 211 ![project-success](../images/exercise1/project-success.png)
212
f5f1ff 213 ### Part 2 - Nexus
3f16e0 214 > _Now that we have our Projects setup; we can start to populate them with Apps to be used in our dev lifecycle_
bc2e43 215
49e3a8 216 1. In the `enablement-ci-cd` repo, checkout the templates for Nexus by running
3f16e0 217 ```bash
f5f1ff 218 git checkout exercise1/git-nexus templates/nexus.yml
3558ae 219 ```
f79b6f 220 The template contains all the things needed to setup a persistent nexus server, exposing a service and route while also creating the persistent volume needed. Have a read through the template; at the bottom you'll see a collection of parameters we will pass to the template.
3558ae 221
49e3a8 222 2. Add some parameters for running the template by creating a new file in the `params` directory.
3558ae 223 ```bash
f5f1ff 224 touch params/nexus
3f16e0 225 ```
D 226
49e3a8 227 3. The essential params to include in this file are:
3558ae 228 ```bash
D 229 VOLUME_CAPACITY=5Gi
84a614 230 MEMORY_LIMIT=1Gi
3558ae 231 ```
D 232
8894bf 233 4. Create a new object in the inventory variables `inventory/host_vars/ci-cd-tooling.yml` called `ci-cd-tooling` and populate its `content` is as follows
62db52 234
3558ae 235 ```yaml
62db52 236 ---
D 237 ansible_connection: local
238 openshift_cluster_content:
239 - object: ci-cd-tooling
240   content:
f9e1bc 241   - name: "nexus"
D 242     namespace: "{{ ci_cd_namespace }}"
243     template: "{{ playbook_dir }}/templates/nexus.yml"
244     params: "{{ playbook_dir }}/params/nexus"
245     tags:
246     - nexus
3558ae 247 ```
ff1bd7 248 ![ci-cd-deployments-yml](../images/exercise1/ci-cd-deployments-yml.png)
3558ae 249
49e3a8 250 5. Run the OpenShift applier, specifying the tag `nexus` to speed up its execution (`-e target=tools` is to run the other inventory).
3558ae 251 ```bash
f5f1ff 252 ansible-playbook apply.yml -e target=tools \
3558ae 253      -i inventory/ \
ff1bd7 254      -e "filter_tags=nexus"
3558ae 255 ```
D 256
49e3a8 257 6. Once successful; login to the cluster through the browser (using cluster URL) and navigate to the `<YOUR_NAME>-ci-cd`. You should see Nexus up and running. You can login with default credentials (admin / admin123) ![nexus-up-and-running](../images/exercise1/nexus-up-and-running.png)
3558ae 258
f5f1ff 259 ### Part 3 - GitLab
D 260
49e3a8 261 <!-- #### 3a - GitLab install -->
b43054 262 <p class="tip">
49e3a8 263 NOTE - A Gitlab instance in the cloud has already been set up for you, please check with your instructor for the Gitlab instance URL.
b43054 264 </p>
D 265
49e3a8 266 <!-- 4. Now let's do the same thing for GitLab to get it up and running. Checkout the template and params provided by running
3558ae 267 ```bash
f5f1ff 268 git checkout exercise1/git-nexus templates/gitlab.yml params/gitlab
1173e5 269 ```
3558ae 270 Explore the template; it contains the PVC, buildConfig and services. The DeploymentConfig is made up of these apps
D 271  - Redis (3.2.3)
272  - PostgreSQL (9.4)
273  - GitLab CE (v10.2.3)
274
b47510 275 4. Open the `params/gitlab` file and complete the following params
RH 276 <p class="tip">
277 Note - The values here for the LDAP and BIND credentials will be provided by your tutor.
278 </p>
3558ae 279 ```
D 280 LDAP_BIND_DN=uid=<BIND_USER>,ou=People,dc=<YOUR_DOMAIN>,dc=com
281 LDAP_USER_FILTER=(memberof=CN=YourGroup,OU=Users,DC=<YOUR_DOMAIN>,DC=com)
282 LDAP_PASSWORD=<BIND_USER_PASSWORD>
283 LDAP_HOST=<LDAP_HOST>
284 LDAP_BASE=ou=People,dc=<YOUR_DOMAIN>,dc=com
285 LDAP_LABEL="<LDAP_DESCRIPTION>"
286 GITLAB_ROOT_PASSWORD=<GITLAB_ROOT_USER_PASSWORD>
287 GITLAB_DATA_VOL_SIZE=2Gi
288 POSTGRESQL_VOL_SIZE=1Gi
289 APPLICATION_HOSTNAME=<GITLAB_URL>
c41b51 290 NAMESPACE=<YOUR_NAME>-ci-cd
3558ae 291 ```
D 292 where the following need to be replaced by actual values:
293     * `<BIND_USER>` is the user used to query the LDAP
294     * `<BIND_USER_PASSWORD>` is the password used when querying the LDAP
295     * `<YOUR_DOMAIN>` is the domain the LDAP is hosted on
296     * `<LDAP_HOST>` is fqdn of the LDAP server
1173e5 297     * `<LDAP_DESCRIPTION>` is the description to be used on the sign-in header for GitLab e.g. "Name LDAP Login"
CM 298     * `<GITLAB_ROOT_USER_PASSWORD>` is the root user for GOD access on the GitLab instance e.g. password123
3558ae 299     * `<GITLAB_URL>` is the endpoint for gitlab. It will take the form `gitlab-<YOUR_NAME>-ci-cd.apps.<ENV_ID>.<YOUR_DOMAIN>.com`
D 300
62db52 301 4. Create another object in the inventory `inventory/host_vars/ci-cd-tooling.yml` file to run the build & deploy of this template. Add the following and update the `namespace:` accordingly
3558ae 302 ```yaml
ff1bd7 303     - name: "gitlab"
62db52 304       namespace: "{{ ci_cd_namespace }}"
D 305       template: "{{ playbook_dir }}/templates/gitlab.yml"
306       params: "{{ playbook_dir }}/params/gitlab"
ff1bd7 307       tags:
D 308       - gitlab
3558ae 309 ```
D 310
8894bf 311 4. Run the OpenShift applier, specifying the tag `gitlab` to speed up its execution.
3558ae 312 ```bash
f5f1ff 313 ansible-playbook apply.yml -e target=tools \
3558ae 314      -i inventory/ \
ff1bd7 315      -e "filter_tags=gitlab"
3558ae 316 ```
D 317
49e3a8 318 4. Once successful; login to the cluster and navigate to the `<YOUR_NAME>-ci-cd`. You should see GitLab up and running. ![gitlab-up-and-running](../images/exercise1/gitlab-up-and-running.png) -->
f5f1ff 319
49e3a8 320 <!-- #### 3b - Commit CI/CD -->
c41b51 321
49e3a8 322 1. Navigate to GitLab login page. You can login using your cluster credentials using the LDAP tab
ff1bd7 323 ![gitlab-ui](../images/exercise1/gitlab-ui.png)
c41b51 324
49e3a8 325 2. Once logged in create a new project called `enablement-ci-cd` and mark it as internal. Once created; copy out the `git url` for use on the next step.
ff1bd7 326 ![gitlab-new-project](../images/exercise1/gitlab-new-project.png)
49e3a8 327 <!-- <p class="tip">
e43fd2 328 Note - we would not normally make the project under your name but create a group and add the project there on residency but for simplicity of the exercise we'll do that here
49e3a8 329 </p> -->
ff1bd7 330
49e3a8 331 3. If you have not used Git before; you may need to tell Git who you are and what your email is before we commit. Run the following commands, substituting your email and "Your Name". If you've done this before move on to the next step. The last git config command is used to bypass SSL key verification for the GitLab server instance since we are using self-signed certificates on the sever.
RS 332
ff1bd7 333 ```bash
f5f1ff 334 git config --global user.email "yourname@mail.com"
ff1bd7 335 ```
f5f1ff 336 ```bash
D 337 git config --global user.name "Your Name"
49e3a8 338 ```
RS 339
340 ```bash
341 git config --global http.sslVerify false
f5f1ff 342 ```
D 343
1173e5 344 4. Commit your local project to this new remote by first removing the existing origin (github) where the Ansible project was cloned from in the first steps. Remember to substitute `<GIT_URL>` accordingly with the one created for your `enablement-ci-cd` repository a moment ago.
f5f1ff 345 ```bash
D 346 git remote set-url origin <GIT_URL>
347 ```
348 ```bash
349 git add .
350 ```
351 ```bash
352 git commit -m "Adding git and nexus config"
353 ```
354 ```bash
355 git push -u origin --all
356 ```
357
49e3a8 358 ### Part 4 - MongoDB for CI tests
RS 359 > In order to run our API tests in CI in later labs; we need a MongoDB server instance available for executing our tests. As this is part of our CI/CD Lifecycle; we will add it now.
0d4d53 360
49e3a8 361 1. In our `enablement-ci-cd` repo; checkout the mongo templates as shown below to bring in the template and params. 
RS 362 <!-- The mongodb template we're using is the same as the one for our `todolist-fe` created in previous exercise. -->
0d4d53 363 ```bash
d43bf4 364 git checkout exercise1/mongodb params/mongodb templates/mongodb.yml
0d4d53 365 ```
D 366
49e3a8 367 2. Open `enablement-ci-cd` in your favourite editor. Edit the `inventory/host_vars/ci-cd-tooling.yml` to include a new object for our mongodb as shown below. This item can be added below the Jenkins slave in the `ci-cd-tooling` section.
0d4d53 368 ```yaml
D 369   - name: "jenkins-mongodb"
370     namespace: "{{ ci_cd_namespace }}"
371     template: "{{ playbook_dir }}/templates/mongodb.yml"
372     params: "{{ playbook_dir }}/params/mongodb"
373     tags:
374     - mongodb
375 ```
d43bf4 376 ![jenkins-mongo](../images/exercise1/jenkins-mongo.png)
0d4d53 377
49e3a8 378 3. Git commit your updates to the inventory to git for traceability.
0d4d53 379 ```bash
D 380 git add .
381 ```
382 ```bash
383 git commit -m "ADD - mongodb for use in the pipeline"
384 ```
385 ```bash
386 git push
387 ```
388
1173e5 389 4. Apply this change as done previously using Ansible. The deployment can be validated by going to your `<YOUR_NAME>-ci-cd` namespace and checking if it is there!
0d4d53 390 ```bash
D 391 ansible-playbook apply.yml -e target=tools \
392   -i inventory/ \
393   -e "filter_tags=mongodb"
394 ```
395 ![ocp-mongo](../images/exercise3/ocp-mongo.png)
396
49e3a8 397 <p class="tip">
RS 398 Note - When making changes to the "enablement-ci-cd" repo, you should frequently commit the changes to git.
399 </p>
bc2e43 400
49e3a8 401 ### Part 5 - Jenkins & S2I
RS 402 > _Create a build and deployment config for Jenkins. Add new configuration and plugins to the OpenShift default Jenkins image using s2i
bc2e43 403
49e3a8 404 1. Add the Jenkins Build & Deployment configs to the `enablement-ci-cd` repo by merging the contents from the `exercise1/jenkins` branch
ff1bd7 405 ```bash
f5f1ff 406 git checkout exercise1/jenkins templates/jenkins.yml
ff1bd7 407 ```
49e3a8 408 <!-- The Jenkins template is essentially the standard persistent Jenkins one with OpenShift. -->
ff1bd7 409
49e3a8 410 2. As before; create a new set of params by creating a `params/jenkins` file and adding some overrides to the template and updating the `<YOUR_NAME>` value accordingly.
2e44a7 411 ```
84a614 412 MEMORY_LIMIT=3Gi
f54f70 413 VOLUME_CAPACITY=10Gi
ff1bd7 414 JVM_ARCH=x86_64
D 415 NAMESPACE=<YOUR_NAME>-ci-cd
416 JENKINS_OPTS=--sessionTimeout=720
417 ```
62db52 418
49e3a8 419 3. Add a `jenkins` variable to the Ansible inventory underneath the jenkins-mongo (and git if you have it) in  `inventory/host_vars/ci-cd-tooling.yml`.
ff1bd7 420 ```yaml
D 421     - name: "jenkins"
62db52 422       namespace: "{{ ci_cd_namespace }}"
D 423       template: "{{ playbook_dir }}/templates/jenkins.yml"
424       params: "{{ playbook_dir }}/params/jenkins"
ff1bd7 425       tags:
D 426       - jenkins
427 ```
f79b6f 428 This configuration, if applied now, will create the deployment configuration needed for Jenkins but the `${NAMESPACE}:${JENKINS_IMAGE_STREAM_TAG}` in the template won't exist yet.
ff1bd7 429
49e3a8 430 4. To create this image we will take the supported OpenShift Jenkins Image and bake into it some extra configuration using an [s2i](https://github.com/openshift/source-to-image) builder image. More information on Jenkins s2i is found on the [openshift/jenkins](https://github.com/openshift/jenkins#installing-using-s2i-build) GitHub page. To create an s2i configuration for Jenkins, check out the pre-canned configuration source in the `enablement-ci-cd` repo
ff1bd7 431 ```bash
f5f1ff 432 git checkout exercise1/jenkins-s2i jenkins-s2i
ff1bd7 433 ```
1173e5 434 The structure of the Jenkins s2i config is
ff1bd7 435 ```
D 436 jenkins-s2i
437 ├── README.md
438 ├── configuration
439 │   ├── build-failure-analyzer.xml
440 │   ├── init.groovy
441 │   ├── jenkins.plugins.slack.SlackNotifier.xml
2e44a7 442 │   ├── scriptApproval.xml
ff1bd7 443 │   └── jobs
D 444 │       └── seed-multibranch-job
445 │           └── config.xml
446 └── plugins.txt
447 ```
448  * `plugins.txt` is a list of `pluginId:version` for Jenkins to pre-install when starting
449  * `./configuration` contains content that is placed in `${JENKINS_HOME}`. A `config.xml` could be placed in here to control the bulk of Jenkins configuration.
450  * `./configuration/jobs/*` contains job names and xml config that jenkins loads when starting. The seed job in there we will return to in later lessons.
de8ebb 451  * `build-failure-analyzer.xml` is config for the plugin to read the logs and look for key items based on a Regex. More on this in later lessons.
ff1bd7 452  * `init.groovy` contains a collection of settings jenkins configures itself with when launching
D 453
1173e5 454 5. Let's add a plugin for Jenkins to be started with, [green-balls](https://plugins.jenkins.io/greenballs). This simply changes the default `SUCCESS` status of Jenkins from Blue to Green. Append the `jenkins-s2i/plugins.txt` file with
ff1bd7 455 ```txt
D 456 greenballs:1.15
1173e5 457 ```
ff1bd7 458 ![green-balls.png](../images/exercise1/green-balls.png)
D 459
49e3a8 460 <!-- Why does Jenkins use blue to represent success? More can be found [on reddit](https://www.reddit.com/r/programming/comments/4lu6q8/why_does_jenkins_have_blue_balls/) or the [Jenkins blog](https://jenkins.io/blog/2012/03/13/why-does-jenkins-have-blue-balls/). -->
RS 461
462 6. Before building and deploying the Jenkins s2i; add your git credentials to it. These will be used by Jenkins to access the Git Repositories where our apps will be stored. We want Jenkins to be able to push tags to it, so write access is required. 
463
464 There are a few ways we can do this; either adding them to the `template/jenkins.yml` as environment variables and then including them in the `params/jenkins` file.  We could also create a token in GitLab and use it as the source secret in the Jenkins template.
465
466 For the sake of simplicity, just replace the `<USERNAME>` && `<PASSWORD>` in the `jenkins-s2i/configuration/init.groovy` with your LDAP credentials as seen below. This init file gets run when Jenkins launches, and will setup the credentials for use in our Jobs in the next exercises
ff1bd7 467 ```groovy
D 468 gitUsername = System.getenv("GIT_USERNAME") ?: "<USERNAME>"
469 gitPassword = System.getenv("GIT_PASSWORD") ?: "<PASSWORD>"
470 ```
49e3a8 471 <!-- <p class="tip">
1173e5 472 Note in a residency we would not use your GitCredentials for pushing and pulling from Git, a service user would be created for this.
49e3a8 473 </p> -->
ff1bd7 474
49e3a8 475 7. Checkout the params and the templates for the `jenkins-s2i`
2e44a7 476 ```bash
f5f1ff 477 git checkout exercise1/jenkins-s2i params/jenkins-s2i templates/jenkins-s2i.yml
2e44a7 478 ```
D 479
49e3a8 480 8. Open `params/jenkins-s2i` and add the following content; replacing variables as appropriate.
ff1bd7 481 ```
da614f 482 SOURCE_REPOSITORY_URL=<GIT_URL>
ff1bd7 483 NAME=jenkins
D 484 SOURCE_REPOSITORY_CONTEXT_DIR=jenkins-s2i
485 IMAGE_STREAM_NAMESPACE=<YOUR_NAME>-ci-cd
da614f 486 SOURCE_REPOSITORY_USERNAME=<YOUR_LDAP_USERNAME>
D 487 SOURCE_REPOSITORY_PASSWORD=<YOUR_LDAP_PASSWORD>
ff1bd7 488 ```
1173e5 489 where
e43fd2 490     * `<GIT_URL>` is the full clone path of the repo where this project is stored (including the https && .git)
de8ebb 491     * `<YOUR_NAME>` is the prefix for your `-ci-cd` project.
D 492     * Explore some of the other parameters in `templates/jenkins-s2i.yml`
da614f 493     * `<YOUR_LDAP_USERNAME>` is the username builder pod will use to login and clone the repo with
D 494     * `<YOUR_LDAP_PASSWORD>` is the password the builder pod will use to authenticate and clone the repo using
49e3a8 495 <!-- <p class="tip">
da614f 496 Note in a residency we would not use your GitCredentials for pushing and pulling from Git, A service user would be created or a token generated.
49e3a8 497 </p> -->
ff1bd7 498
49e3a8 499 9. Create a new object `ci-cd-builds` in the Ansible `inventory/host_vars/ci-cd-tooling.yml` to drive the s2i build configuration.
ff1bd7 500 ```yaml
D 501   - object: ci-cd-builds
502     content:
503     - name: "jenkins-s2i"
62db52 504       namespace: "{{ ci_cd_namespace }}"
D 505       template: "{{ playbook_dir }}/templates/jenkins-s2i.yml"
506       params: "{{ playbook_dir }}/params/jenkins-s2i"
ff1bd7 507       tags:
D 508       - jenkins
509 ```
510
49e3a8 511 10. Commit your code to your GitLab instance
ff1bd7 512 ```bash
f5f1ff 513 git add .
D 514 ```
515 ```bash
516 git commit -m "Adding Jenkins and Jenkins s2i"
517 ```
518 ```bash
519 git push
ff1bd7 520 ```
D 521
49e3a8 522 11.  In order for Jenkins to be able to run `npm` builds and installs we must configure a `jenkins-build-slave` for Jenkins to use. This slave will be dynamically provisioned when we run a build. It needs to have NodeJS and npm installed in it. These slaves can take a time to build themselves so to speed up we have placed the slave within OpenShift and you can use the following commands to be able to use them in your project.
bce8a5 523 ```bash
D 524 oc project <YOUR_NAME>-ci-cd
525 ```
5b1604 526 ```bash
T 527 oc tag openshift/jenkins-slave-npm:latest jenkins-slave-npm:latest
528 ```
529 ```bash
530 oc label is jenkins-slave-npm role=jenkins-slave
531 ```
bce8a5 532 This is pulling the container image into your namespace and then adding a label which will allow Jenkins to take notice of it. Don't worry if the label is already there and this last command fails!
5b1604 533
49e3a8 534 12. Now your code is commited, and you have bought in the Jenkins slave; run the OpenShift Applier to add the config to the cluster
ff1bd7 535 ```bash
f5f1ff 536 ansible-playbook apply.yml -e target=tools \
ff1bd7 537      -i inventory/ \
D 538      -e "filter_tags=jenkins"
539 ```
540
49e3a8 541 13. This will trigger a build of the s2i and when it's complete it will add an imagestream of `<YOUR_NAME>-ci-cd/jenkins:latest` to the project. The Deployment config should kick in and deploy the image once it arrives. You can follow the build of the s2i by going to the OpenShift console's project
de8ebb 542 ![jenkins-s2i-log](../images/exercise1/jenkins-s2i-log.png)
bc2e43 543
49e3a8 544 14. When the Jenkins deployment has completed; login (using your OpenShift credentials) and accept the role permissions. You should now see a fairly empty Jenkins with just the seed job
3558ae 545
1173e5 546 ### Part 6 - Jenkins Hello World
49e3a8 547 > _To test things are working end-to-end; create a hello world job that doesn't do much but proves we can pull code from git and that our builds are green._
de8ebb 548
49e3a8 549 1. Log in to Jenkins and hit `New Item` ![new-item](../images/exercise1/new-item.png).
de8ebb 550
49e3a8 551 2. Create a freestyle job called `hello-world` ![jenkins-new-hello-world](../images/exercise1/jenkins-new-hello-world.png).
de8ebb 552
49e3a8 553 3. On the Source Code Management tab; add your `enablement-ci-cd` git repo and hit the dropdown to add your credentials we baked into the s2i on previous steps ![jenkins-scm-git](../images/exercise1/jenkins-scm-git.png)
de8ebb 554
49e3a8 555 4. On the build tab add an Execute Shell step and fill it with `echo "Hello World"` ![jenkins-hello-world](../images/exercise1/jenkins-hello-world.png).
de8ebb 556
49e3a8 557 5. Run the build and we should see it pass successfully and with a Green ball! ![jenkins-green-balls](../images/exercise1/jenkins-green-balls.png)
de8ebb 558
6c0958 559 ### Part 7 - Live, Die, Repeat
1173e5 560 > _In this section you will prove the infra as code is working by deleting your Cluster Content and recreating it all_
de8ebb 561
49e3a8 562 1. Commit your code to the new repo in GitLab
7c832b 563 ```bash
f5f1ff 564 git add .
D 565 ```
566 ```bash
567 git commit -m "ADD - all ci/cd contents"
568 ```
569 ```bash
570 git push
7c832b 571 ```
de8ebb 572
49e3a8 573 2. Burn your OpenShift project resources to the ground
7c832b 574 ```bash
f5f1ff 575 oc delete project <YOUR_NAME>-ci-cd
D 576 ```
577 ```bash
578 oc delete project <YOUR_NAME>-dev
579 ```
580 ```bash
581 oc delete project <YOUR_NAME>-test
7c832b 582 ```
de8ebb 583
49e3a8 584 3. Check to see the projects that were marked for deletion are removed.
da614f 585 ```bash
f5f1ff 586 oc get projects | egrep '<YOUR_NAME>-ci-cd|<YOUR_NAME>-dev|<YOUR_NAME>-test'
da614f 587 ```
D 588
49e3a8 589 4. Re-apply the inventory to re-create it all!
7c832b 590 ```bash
530a25 591 oc login <CLUSTER_URL>
f5f1ff 592 ```
D 593 ```bash
594 ansible-playbook apply.yml -i inventory/ -e target=bootstrap
595 ```
596 ```bash
597 ansible-playbook apply.yml -i inventory/ -e target=tools
7c832b 598 ```
c951f7 599
5d0992 600 _____
D 601
49e3a8 602 <!-- ## Extension Tasks
de8ebb 603 > _Ideas for go-getters. Advanced topic for doers to get on with if they finish early. These will usually not have a solution and are provided for additional scope._
c951f7 604
0d4d53 605  - Install Cowsay for 100% more Ansible Fun!
3558ae 606  - Add more secure access for Nexus (ie not admin / admin123) using the automation to drive secret creation
ff1bd7 607  - Add a SonarQube persistent deployment to the `ci-cd-deployments` section.
49e3a8 608  - Add `jenkins.plugins.slack.SlackNotifier.xml` to `jenkins-s2i/configuration` to include URL of Slack for team build notifications and rebuild Jenkins s2i
c951f7 609
49e3a8 610 _____ -->
5d0992 611
c951f7 612 ## Additional Reading
49e3a8 613 > List of links and other reading material that might be of use for the exercise
01c4da 614
RH 615 ## Slide links
616
49e3a8 617 - TBD
RS 618 - TBD
619 - TBD
620
621 <!-- - [Intro](https://docs.google.com/presentation/d/1LsfAkH8GfIhulEoy_yd-usWBfDHnZEyQdNvYeTmAg4A/)
01c4da 622 - [Wrap-up](https://docs.google.com/presentation/d/1cfyJ6SHddZNbM61oz67r870rLYVKY335zGclXN2uLMY/)
49e3a8 623 - [All Material](https://drive.google.com/drive/folders/13Bt4BXf9P2OB8VI4YQNcNONF1786dqOx) -->