Init push copied from production

Signed-off-by: Benjamin Copeland <ben.copeland@linaro.org>
Change-Id: Icc7f9066abef878f32f783de4c60f20e1c6e40d8
diff --git a/ci-job-configs-sanity-check.yaml b/ci-job-configs-sanity-check.yaml
new file mode 100644
index 0000000..711838e
--- /dev/null
+++ b/ci-job-configs-sanity-check.yaml
@@ -0,0 +1,83 @@
+- job:
+    name: ci-job-configs-sanity-check-next
+    project-type: freestyle
+    defaults: global
+    properties:
+        - authorization:
+            anonymous:
+                - job-read
+                - job-extended-read
+        - build-discarder:
+            days-to-keep: 30
+            num-to-keep: 30
+    disabled: false
+    node: master
+    display-name: 'CI job configs sanity check (Next)'
+    scm:
+        - git:
+            url: https://review.trustedfirmware.org/${GERRIT_PROJECT}
+            refspec: ${GERRIT_REFSPEC}
+            branches:
+                - ${GERRIT_BRANCH}
+            skip-tag: true
+            clean:
+                before: true
+            choosing-strategy: gerrit
+            basedir: configs
+    triggers:
+        - gerrit:
+            server-name: 'review.trustedfirmware.org'
+            trigger-on:
+                - patchset-created-event
+            projects:
+                - project-compare-type: 'PLAIN'
+                  project-pattern: 'next/ci/tf-ci-scripts'
+                  branches:
+                    - branch-pattern: 'master'
+                - project-compare-type: 'PLAIN'
+                  project-pattern: 'next/ci/tf-m-job-configs'
+                  branches:
+                    - branch-pattern: 'master'
+                - project-compare-type: 'PLAIN'
+                  project-pattern: 'next/ci/tf-a-job-configs'
+                  branches:
+                    - branch-pattern: 'master'
+                - project-compare-type: 'PLAIN'
+                  project-pattern: 'next/ci/hafnium-job-configs'
+                  branches:
+                    - branch-pattern: 'master'
+            silent-start: true
+    wrappers:
+        - timestamps
+    builders:
+        - shell: |
+            #!/bin/bash -e
+            echo "#${BUILD_NUMBER}-${GERRIT_PATCHSET_REVISION:0:8}" > ${WORKSPACE}/version.txt
+        - build-name-setter:
+            name: 'version.txt'
+            file: true
+        - shell: |
+            #!/bin/bash
+
+            set -e
+
+            echo ""
+            echo "########################################################################"
+            echo "    Gerrit Environment"
+            env |grep '^GERRIT'
+            echo "########################################################################"
+
+            cd configs/
+
+            mkdir -p ci/ && wget -q https://git.trustedfirmware.org/ci/tf-ci-scripts.git/plain/ci/run-jjb.py -O ci/run-jjb.py
+            # FIXME run-jjb.py was meant to be used for deployment only
+            # use JJB 'test' command instead of 'update' command
+            sed -i "s|update|test|" ci/run-jjb.py
+
+            export GIT_PREVIOUS_COMMIT=$(git rev-parse HEAD~1)
+            export GIT_COMMIT=${GERRIT_PATCHSET_REVISION}
+            jenkins-jobs --version
+            python ci/run-jjb.py
+    publishers:
+        - email:
+            recipients: 'ben.copeland@linaro.org riku.voipio@linaro.org kelley.spoon@linaro.org fathi.boudra@linaro.org'
diff --git a/ci/run-jjb.py b/ci/run-jjb.py
new file mode 100755
index 0000000..fe93e6a
--- /dev/null
+++ b/ci/run-jjb.py
@@ -0,0 +1,176 @@
+#!/usr/bin/python
+
+import os
+import shutil
+import signal
+import string
+import subprocess
+import sys
+import xml.etree.ElementTree
+from distutils.spawn import find_executable
+
+
+def findparentfiles(fname):
+    filelist = []
+    newlist = []
+    args = ['grep', '-rl', '--exclude-dir=.git', fname]
+    proc = subprocess.Popen(args,
+                            stdin=subprocess.PIPE,
+                            stdout=subprocess.PIPE,
+                            universal_newlines=False,
+                            preexec_fn=lambda:
+                            signal.signal(signal.SIGPIPE, signal.SIG_DFL))
+    data = proc.communicate()[0]
+    if proc.returncode != 0:
+        return filelist
+    for filename in data.splitlines():
+        if filename.endswith('.yaml') and '/' not in filename:
+            filelist.append(filename)
+        else:
+            newlist = findparentfiles(filename)
+            for tempname in newlist:
+                filelist.append(tempname)
+    return filelist
+
+
+jjb_cmd = find_executable('jenkins-jobs') or sys.exit('jenkins-jobs is not found.')
+jjb_args = [jjb_cmd]
+
+jjb_user = os.environ.get('JJB_USER')
+jjb_password = os.environ.get('JJB_PASSWORD')
+if jjb_user is not None and jjb_password is not None:
+    jenkins_jobs_ini = ('[job_builder]\n'
+                        'ignore_cache=True\n'
+                        'keep_descriptions=False\n'
+                        '\n'
+                        '[jenkins]\n'
+                        'user=%s\n'
+                        'password=%s\n'
+                        'url=https://ci.trustedfirmware.org/\n' % (jjb_user, jjb_password))
+    with open('jenkins_jobs.ini', 'w') as f:
+        f.write(jenkins_jobs_ini)
+    jjb_args.append('--conf=jenkins_jobs.ini')
+
+jjb_test_args = list(jjb_args)
+jjb_delete_args = list(jjb_args)
+
+# !!! "update" below and through out this file is replaced by "test" (using sed)
+# !!! in the sanity-check job.
+main_action = 'update'
+jjb_args.extend([main_action, 'template.yaml'])
+jjb_test_args.extend(['test', '-o', 'out/', 'template.yaml'])
+jjb_delete_args.extend(['delete'])
+
+if main_action == 'test':
+    # Dry-run, don't delete jobs.
+    jjb_delete_args.insert(0, 'echo')
+
+try:
+    git_args = ['git', 'diff', '--name-only',
+                os.environ.get('GIT_PREVIOUS_COMMIT'),
+                os.environ.get('GIT_COMMIT')]
+    proc = subprocess.Popen(git_args,
+                            stdin=subprocess.PIPE,
+                            stdout=subprocess.PIPE,
+                            universal_newlines=False,
+                            preexec_fn=lambda:
+                            signal.signal(signal.SIGPIPE, signal.SIG_DFL))
+except (OSError, ValueError) as e:
+    raise ValueError("%s" % e)
+
+data = proc.communicate()[0]
+if proc.returncode != 0:
+    raise ValueError("command has failed with code '%s'" % proc.returncode)
+
+filelist = []
+files = []
+for filename in data.splitlines():
+    if filename.endswith('.yaml') and '/' not in filename:
+        filelist.append(filename)
+    else:
+        files = findparentfiles(filename)
+        for tempname in files:
+            filelist.append(tempname)
+
+# Remove dplicate entries in the list
+filelist = list(set(filelist))
+
+for conf_filename in filelist:
+    with open(conf_filename) as f:
+        buffer = f.read()
+        template = string.Template(buffer)
+        buffer = template.safe_substitute(
+            AUTH_TOKEN=os.environ.get('AUTH_TOKEN'),
+            LAVA_USER=os.environ.get('LAVA_USER'),
+            LAVA_TOKEN=os.environ.get('LAVA_TOKEN'))
+        with open('template.yaml', 'w') as f:
+            f.write(buffer)
+        try:
+            proc = subprocess.Popen(jjb_args,
+                                    stdin=subprocess.PIPE,
+                                    stdout=subprocess.PIPE,
+                                    universal_newlines=False,
+                                    preexec_fn=lambda:
+                                    signal.signal(signal.SIGPIPE, signal.SIG_DFL))
+        except (OSError, ValueError) as e:
+            raise ValueError("%s" % e)
+
+        data = proc.communicate()[0]
+        if proc.returncode != 0:
+            raise ValueError("command has failed with code '%s'" % proc.returncode)
+
+        try:
+            shutil.rmtree('out/', ignore_errors=True)
+
+            proc = subprocess.Popen(jjb_test_args,
+                                    stdin=subprocess.PIPE,
+                                    stdout=subprocess.PIPE,
+                                    universal_newlines=False,
+                                    preexec_fn=lambda:
+                                    signal.signal(signal.SIGPIPE, signal.SIG_DFL))
+            data = proc.communicate()[0]
+            if proc.returncode != 0:
+                raise ValueError("command has failed with code '%s'" % proc.returncode)
+
+            proc = subprocess.Popen(['ls', 'out/'],
+                                    stdin=subprocess.PIPE,
+                                    stdout=subprocess.PIPE,
+                                    universal_newlines=False,
+                                    preexec_fn=lambda:
+                                    signal.signal(signal.SIGPIPE, signal.SIG_DFL))
+            data = proc.communicate()[0]
+            if proc.returncode != 0:
+                raise ValueError("command has failed with code '%s'" % proc.returncode)
+
+            for filename in data.splitlines():
+                try:
+                    xmlroot = xml.etree.ElementTree.parse('out/' + filename).getroot()
+                    disabled = next(xmlroot.iterfind('disabled')).text
+                    if disabled != 'true':
+                        continue
+                    displayName = next(xmlroot.iterfind('displayName')).text
+                    if displayName != 'DELETE ME':
+                        continue
+                except:
+                    continue
+
+                delete_args = list(jjb_delete_args)
+                delete_args.extend([filename])
+                proc = subprocess.Popen(delete_args,
+                                        stdin=subprocess.PIPE,
+                                        stdout=subprocess.PIPE,
+                                        universal_newlines=False,
+                                        preexec_fn=lambda:
+                                        signal.signal(signal.SIGPIPE, signal.SIG_DFL))
+                data = proc.communicate()[0]
+                if proc.returncode != 0:
+                    raise ValueError("command has failed with code '%s'" % proc.returncode)
+                print data
+        except (OSError, ValueError) as e:
+            raise ValueError("%s" % e)
+
+        shutil.rmtree('out/', ignore_errors=True)
+        os.remove('template.yaml')
+
+if os.path.exists('jenkins_jobs.ini'):
+    os.remove('jenkins_jobs.ini')
diff --git a/hello_world.yml b/hello_world.yml
new file mode 100644
index 0000000..5c7265e
--- /dev/null
+++ b/hello_world.yml
@@ -0,0 +1,59 @@
+- job:
+    name: hello-world
+    project-type: freestyle
+    defaults: global
+    properties:
+        - authorization:
+            anonymous:
+                - job-read
+                - job-extended-read
+        - build-discarder:
+            days-to-keep: 30
+            num-to-keep: 30
+    disabled: false
+    node: amd64-bionic
+    display-name: 'CI job to test jenkins slave'
+    scm:
+        - git:
+            url: https://review.trustedfirmware.org/${GERRIT_PROJECT}
+            refspec: ${GERRIT_REFSPEC}
+            branches:
+                - ${GERRIT_BRANCH}
+            skip-tag: true
+            clean:
+                before: true
+            choosing-strategy: gerrit
+            basedir: configs
+    triggers:
+        - gerrit:
+            server-name: 'review.trustedfirmware.org'
+            trigger-on:
+                - patchset-created-event
+            projects:
+                - project-compare-type: 'PLAIN'
+                  project-pattern: 'ci/tf-ci-scripts'
+                  branches:
+                    - branch-pattern: 'master'
+            silent-start: true
+    wrappers:
+        - timestamps
+    builders:
+        - shell: |
+            #!/bin/bash -e
+            echo "#${BUILD_NUMBER}-${GERRIT_PATCHSET_REVISION:0:8}" > ${WORKSPACE}/version.txt
+        - build-name-setter:
+            name: 'version.txt'
+            file: true
+        - shell: |
+            #!/bin/bash
+
+            set -e
+
+            echo ""
+            echo "########################################################################"
+            echo "    Gerrit Environment"
+            env |grep '^GERRIT'
+            echo "########################################################################"
+    publishers:
+        - email:
+            recipients: 'ben.copeland@linaro.org'
diff --git a/trigger-ci-job-configs.yaml b/trigger-ci-job-configs.yaml
new file mode 100644
index 0000000..dfbf662
--- /dev/null
+++ b/trigger-ci-job-configs.yaml
@@ -0,0 +1,93 @@
+- job:
+    name: trigger-ci-job-configs-next
+    project-type: freestyle
+    defaults: global
+    properties:
+        - authorization:
+            anonymous:
+                - job-discover
+            linaro:
+                - job-read
+                - job-extended-read
+        - build-discarder:
+            days-to-keep: 30
+            num-to-keep: 200
+    disabled: false
+    node: master
+    display-name: 'CI job configs deployment (Next)'
+    scm:
+        - git:
+            url: https://review.trustedfirmware.org/${GERRIT_PROJECT}
+            refspec: ${GERRIT_REFSPEC}
+            branches:
+                - ${GERRIT_BRANCH}
+            skip-tag: true
+            clean:
+                before: true
+            choosing-strategy: gerrit
+            basedir: configs
+    triggers:
+        - gerrit:
+            server-name: 'review.trustedfirmware.org'
+            trigger-on:
+                - change-merged-event
+            projects:
+                - project-compare-type: 'PLAIN'
+                  project-pattern: 'next/ci/tf-ci-scripts'
+                  branches:
+                    - branch-pattern: 'master'
+                - project-compare-type: 'PLAIN'
+                  project-pattern: 'next/ci/tf-m-job-configs'
+                  branches:
+                    - branch-pattern: 'master'
+                - project-compare-type: 'PLAIN'
+                  project-pattern: 'next/ci/tf-a-job-configs'
+                  branches:
+                    - branch-pattern: 'master'
+                - project-compare-type: 'PLAIN'
+                  project-pattern: 'next/ci/hafnium-job-configs'
+                  branches:
+                    - branch-pattern: 'master'
+            silent-start: true
+    wrappers:
+        - timestamps
+        - credentials-binding:
+            - text:
+                credential-id: JJB_USER
+                variable: JJB_USER
+        - credentials-binding:
+            - text:
+                credential-id: JJB_PASSWORD
+                variable: JJB_PASSWORD
+        - credentials-binding:
+            - text:
+                credential-id: AUTH_TOKEN
+                variable: AUTH_TOKEN
+    builders:
+        - shell: |
+            #!/bin/bash -e
+            echo "#${BUILD_NUMBER}-${GERRIT_PATCHSET_REVISION:0:8}" > ${WORKSPACE}/version.txt
+        - build-name-setter:
+            name: 'version.txt'
+            file: true
+        - shell: |
+            #!/bin/bash
+
+            set -e
+
+            echo ""
+            echo "########################################################################"
+            echo "    Gerrit Environment"
+            env |grep '^GERRIT'
+            echo "########################################################################"
+
+            cd configs/
+
+            export GIT_PREVIOUS_COMMIT=$(git rev-parse HEAD~1)
+            export GIT_COMMIT=${GERRIT_PATCHSET_REVISION}
+            jenkins-jobs --version
+            mkdir -p ci/ && wget -q https://git.trustedfirmware.org/ci/tf-ci-scripts.git/plain/ci/run-jjb.py -O ci/run-jjb.py
+            python ci/run-jjb.py
+    publishers:
+        - email:
+            recipients: 'ben.copeland@linaro.org riku.voipio@linaro.org kelley.spoon@linaro.org fathi.boudra@linaro.org'