← Back to team overview

sts-sponsors team mailing list archive

[Merge] ~cgrabowski/maas-ci/+git/maas-ci-config:add_ansible_collection_tests into ~maas-committers/maas-ci/+git/maas-ci-config:master

 

Christian Grabowski has proposed merging ~cgrabowski/maas-ci/+git/maas-ci-config:add_ansible_collection_tests into ~maas-committers/maas-ci/+git/maas-ci-config:master.

Commit message:
add job definitions for ansible-collection



Requested reviews:
  MAAS Committers (maas-committers)

For more details, see:
https://code.launchpad.net/~cgrabowski/maas-ci/+git/maas-ci-config/+merge/440772
-- 
Your team MAAS Committers is requested to review the proposed merge of ~cgrabowski/maas-ci/+git/maas-ci-config:add_ansible_collection_tests into ~maas-committers/maas-ci/+git/maas-ci-config:master.
diff --git a/jenkins/jobs/ansible_collection.groovy b/jenkins/jobs/ansible_collection.groovy
new file mode 100644
index 0000000..be73dc5
--- /dev/null
+++ b/jenkins/jobs/ansible_collection.groovy
@@ -0,0 +1,194 @@
+def updateCommitStatus(repo, commit_sha, state, message) {
+    println "applying ${state} to ${repo} ${commit_sha} with message: ${message}"
+    step([
+        $class: "GitHubCommitStatusSetter",
+        reposSource: [$class: "ManuallyEnteredRepositorySource", url: "${repo}"],
+        commitShaSource: [$class: "ManuallyEnteredShaSource", sha: commit_sha],
+        statusResultSource: [
+            $class: "ConditionalStatusResultSource",
+            results: [[$class: "AnyBuildResult", state: state, message: message]]
+        ]
+    ])
+}
+pipeline {
+    agent {
+        docker {
+            image "ubuntu:${params.SERIES}"
+            args '-u 0:0 -m 4g'
+            label 'ci-lab'
+            registryUrl 'https://maas-ci.internal:5443'
+            registryCredentialsId 'maas-ci-registry'
+            reuseNode true
+        }
+    }
+    options {
+        disableConcurrentBuilds()
+        timeout(time: 6, unit: "HOURS")
+    }
+    stages {
+        stage('Prepare') {
+            steps {
+                withCredentials([
+                    sshUserPrivateKey(credentialsId: 'maas-lander-ssh-key', keyFileVariable: 'SSHKEY', usernameVariable: 'SSHUSER')
+                ]) {
+                    sh '''
+                        if [ ! -z \$http_proxy ]; then
+                            echo "Acquire::http::proxy \\"\$http_proxy\\"\\;" > /etc/apt/apt.conf.d/github-ci-proxy
+                            echo "Acquire::https::proxy \\"\$http_proxy\\"\\;" >> /etc/apt/apt.conf.d/github-ci-proxy
+
+                            mkdir -p ~/.ssh/
+                            echo "Host github.com\n  HostName ssh.github.com\n  Port 443\n  ProxyCommand /usr/bin/nc -X connect -x squid.internal %h %p\n" >> ~/.ssh/config
+                        fi
+                       
+                        export DEBIAN_FRONTEND=noninteractive
+                        apt-get -y update
+                        apt install -y git make sudo wget socat netcat python3-pip tox
+                        
+                        useradd ubuntu -d /home/ubuntu
+                        mkdir -p /home/ubuntu
+                        chown ubuntu:ubuntu /home/ubuntu
+                        echo "ubuntu ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/90-docker
+                    '''
+                }
+            }
+        }
+        stage('Checkout') {
+            steps{
+                script {
+                    currentBuild.description = "${params.GH_REPO}/${params.GH_BRANCH}"
+                }
+                sh '''
+                    mkdir -p /opt/ansible_collections/maas
+                    git clone ${GH_REPO} --branch ${GH_BRANCH} --depth 1 /opt/ansible_collections/maas/maas
+                    env -C /opt/ansible_collections/maas/maas git show --no-patch
+                '''    
+            }
+        }
+        stage('Fetch commit sha') {
+            steps {
+                script {
+                    env.COMMIT_SHA = sh(script: "git ls-remote ${params.GH_REPO} ${params.GH_BRANCH} | awk \'{print \$1}\'", returnStdout: true).trim()
+                    println "commit: ${env.COMMIT_SHA}"
+                }
+                updateCommitStatus(params.TARGET_REPO, env.COMMIT_SHA, "PENDING", "Tests Executing, Awaiting results")
+            }
+        }
+        stage('Install Dependencies') {
+            steps{
+                sh '''
+                    sudo -E -u ubuntu pip install git+https://github.com/maas/python-libmaas.git
+                '''
+            }
+        }
+        stage('Setup Env') {
+            steps {
+                withCredentials([string(credentialsId: 'jenkins-maas-token', variable: 'MAAS_API_TOKEN')]){
+                    sh '''
+                        export no_proxy="${no_proxy:-'localhost'},${MAAS_HOST}"
+                        export NO_PROXY="${NO_PROXY:-'localhost'},${MAAS_HOST}"
+                        export noproxy="${noproxy:-'localhost'},${MAAS_HOST}"
+                        export NOPROXY="${NOPROXY:-'localhost'},${MAAS_HOST}"
+                        
+                        INTEGRATION_CONFIG_PATH="/opt/ansible_collections/maas/maas/tests/integration/integration_config.yaml"
+                        MAAS_API_TOKEN_KEY="$(echo $MAAS_API_TOKEN | awk -F: '{print $2}')"
+                        MAAS_API_TOKEN_SECRET="$(echo $MAAS_API_TOKEN | awk -F: '{print $3}')"
+                        MAAS_API_TOKEN_CLIENT_KEY="$(echo $MAAS_API_TOKEN | awk -F: '{print $1}')"
+
+                        sudo ln -s /usr/bin/python3 /usr/bin/python
+
+                        cat <<EOF > ${INTEGRATION_CONFIG_PATH}
+                        host: "${MAAS_URL}"
+                        test_vm_host: "${MAAS_TEST_VMHOST}"
+                        test_vm_host_fqdn: "${MAAS_TEST_VMHOST_FQDN}"
+                        test_subnet: "${MAAS_TEST_SUBNET}"
+                        test_domain: "${MAAS_TEST_DOMAIN}"
+                        test_lxd_address: "${MAAS_TEST_LXD_ADDRESS}"
+                        test_virsh_host: "${MAAS_TEST_VIRSH_HOST}"
+                        token_key: "${MAAS_API_TOKEN_KEY}"
+                        token_secret: "${MAAS_API_TOKEN_SECRET}"
+                        customer_key: "${MAAS_API_TOKEN_CLIENT_KEY}"
+                        EOF
+
+                        sudo -E -u ubuntu python3 <<EOF >
+                        import os
+
+                        from maas.client import connect
+
+
+                        maas = connect(os.environ["MAAS_URL"], apikey=os.environ["MAAS_API_TOKEN"])
+
+                        rack_controller = maas.rack_controllers.get(system_id=os.environ["PRIMARY_RACK_CONTROLLER"])
+                        fabric = maas.fabrics.get(id=0)
+                        vlan = fabric.vlans.get_default()
+                        vlan.dhcp_on = True
+                        vlan.primary_rack = rack_controller
+                        vlan.save()
+                        EOF                    
+                    '''
+                }
+            }
+        }
+        stage('Test') {
+            steps {
+                withCredentials([string(credentialsId: 'jenkins-maas-token', variable: 'MAAS_API_TOKEN')]) {
+                    {% if test_lock is defined and test_lock %}
+                    lock('{{ test_lock }}') {
+                        sh '''
+                            export no_proxy="${no_proxy:-'localhost'},${MAAS_HOST}"
+                            export NO_PROXY="${NO_PROXY:-'localhost'},${MAAS_HOST}"
+                            export noproxy="${noproxy:-'localhost'},${MAAS_HOST}"
+                            export NOPROXY="${NOPROXY:-'localhost'},${MAAS_HOST}"
+                            cd /opt/ansible_collections/maas/maas
+                            sudo -E -H -u ubuntu tox -e integration
+                        '''
+                    }
+                    {% else %}
+                    sh '''
+                        export no_proxy="${no_proxy:-'localhost'},${MAAS_HOST}"
+                        export NO_PROXY="${NO_PROXY:-'localhost'},${MAAS_HOST}"
+                        export noproxy="${noproxy:-'localhost'},${MAAS_HOST}"
+                        export NOPROXY="${NOPROXY:-'localhost'},${MAAS_HOST}"
+                        cd /opt/ansible_collections/maas/maas
+                        sudo -E -H -u ubuntu tox -e integration
+                    '''
+                    {% endif %}
+                }
+            }
+        }
+    }
+    post {
+        always {
+            withCredentials([
+                string(credentialsId: 'jenkins-maas-token', variable: 'MAAS_API_TOKEN'),
+                sshUserPrivateKey(credentialsId: 'maas-lander-ssh-key', keyFileVariable: 'SSHKEY', usernameVariable: 'SSHUSER')
+            ]) {
+                sh '''
+                    sudo -E -u ubuntu python3 <<EOF>
+                        import os
+
+                        from maas.client import connect
+
+
+                        maas = connect(os.environ["MAAS_URL"], apikey=os.environ["MAAS_API_TOKEN"])
+
+                        fabric = maas.fabrics.get(id=0)
+                        vlan = fabric.vlans.get_default()
+                        vlan.dhcp_on = False
+                        vlan.save()
+                    EOF
+                '''
+            }    
+        }
+        success {
+            updateCommitStatus(params.TARGET_REPO, env.COMMIT_SHA, "SUCCESS", "Success")
+        }
+        failure {
+            script {
+                if (params.GITHUB_BRANCH == "main" && currentBuild.getBuildCauses("hudson.triggers.TimerTrigger$TimerTriggerCause")) {
+                    mattermostSend (color: 'red', message: "[${env.JOB_NAME} #${env.BUILD_NUMBER}](${env.BUILD_URL}) [${params.GH_BRANCH}](${params.GH_REPO}/commit/${env.COMMIT_SHA}) :fire: failed")
+                }
+            }
+            updateCommitStatus(params.TARGET_REPO, env.COMMIT_SHA, "FAILURE", "Failed")
+        }    
+    }
+}
diff --git a/jenkins/jobs/ansible_collection.yaml b/jenkins/jobs/ansible_collection.yaml
new file mode 100644
index 0000000..7076edc
--- /dev/null
+++ b/jenkins/jobs/ansible_collection.yaml
@@ -0,0 +1,83 @@
+---
+- project:
+    name: maas-ansible-collection
+    jobs:
+      - 'gh-maas-ansible-collection-creator'
+      - 'gh-maas-ansible-collection-tester'
+
+- job-template:
+    name: gh-maas-ansible-collection-creator
+    description: |
+      Periodically check Github for open Pull Requests against Main that can be tested.
+      Builds gh-maas-ansible-tester aganst each PR
+    project-type: pipeline
+    parameters:
+      - string:
+          name: TARGET_REPO
+          description: Git repo to merge Ansible Collections into
+          default: https://github.com/maas/ansible-collection
+    triggers:
+      - timed: "H/15 * * * *"
+    dsl: !include-jinja2: gh_repo_tester.groovy
+
+- job-template:
+    name: gh-maas-ansible-collection-tester
+    description: |
+      Run the Ansible Collection Integration Tests
+    project-type: pipeline
+    parameters:
+      - string:
+          name: GH_REPO
+          description: 'Github repo to clone'
+          default: 'ansible-collection'
+      - string:
+          name: GH_BRANCH
+          description: 'Github branch to clone'
+          default: 'main'
+      - string:
+          name: GH_ORG
+          description: 'Github org that owns the repo to clone'
+          default: 'maas'
+      - string:
+          name: SERIES
+          description: 'The Ubuntu series to build and with'
+          default: '22.04'
+      - string:
+          name: GH_USER_NAME
+          description: 'The name to use to commit results and push them'
+          default: 'MAAS Lander'
+      - string:
+          name: GH_USER_EMAIL
+          description: 'The email to use to commit results and push them'
+          default: 'maas-lander@xxxxxxxxxxxxx'
+      - string:
+          name: MAAS_HOST
+          description: "The IP or Hostname used in the MAAS_URL, to be added to NO_PROXY"
+          default: '10.245.136.7'
+      - string:
+          name: PRIMARY_RACK_CONTROLLER
+          description: "The rack controller to manage the VLAN DHCP to be enabled on"
+          default: "ckbafg" 
+      - string:
+          name: MAAS_URL
+          description: "The URL to configure the Ansible Collection to test against"
+          default: "http://10.245.136.7:5240/MAAS";
+      - string:
+          name: MAAS_TEST_VMHOST
+          description: "The existing VM Host to use within the ansible tests"
+          default: "natasha.labmaas"
+      - string:
+          name: MAAS_TEST_DOMAIN
+          description: "The default domain of the MAAS env being tested against"
+          default: "labmaas"
+      - string:
+          name: MAAS_TEST_LXD_ADDRESS
+          description: "The LXD address of a host to be added as a VM Host in the tests"
+          default: "opelt.labmaas:5443"
+      - string:
+          name: MAAS_TEST_VIRSH_HOST
+          description: "The host or address to add as a Virsh host in the tests"
+          default: "arm64.labmaas"
+    triggers:
+      - timed: '@daily'
+    dsl: !include-jinja2: ansible_collection.groovy

Follow ups