Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
Merge pull request #39 from jaypoulz/dev-v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jaypoulz committed Jul 3, 2019
2 parents 890fe70 + 4e80ecd commit 0c1f6d3
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 34 deletions.
15 changes: 12 additions & 3 deletions resources/playbooks/run_scripts.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
---
- name: "Run Scripts on each Inventory Host"
hosts: all
hosts: master_node
gather_facts: true
vars_prompt:
- name: test_dir
prompt: "Please enter the path to your tests"

tasks:
- name: install rsync and the utilities to copy the tests to the remote host
become: yes
package:
name: "{{ item }}"
state: latest
Expand Down Expand Up @@ -41,14 +42,22 @@

- name: loop through script directories and create artifacts directory
file:
path: "{{ script_dir }}/artifacts"
path: "{{ script_dir }}/artifacts/{{ ansible_architecture }}"
state: directory
loop: "{{ scripts_dir.files | map(attribute='path') | list }}"
loop_control:
loop_var: script_dir

- name: loop through script directories and create inventory
copy:
src: "{{ inventory }}"
dest: "{{ script_dir }}/inventory"
loop: "{{ scripts_dir.files | map(attribute='path') | list }}"
loop_control:
loop_var: script_dir

- name: loop through script directories, run test, and store artifacts
shell: "bash -x test.sh {{ script_args | default('') }} &> artifacts/{{ ansible_architecture }}-runlog.txt"
shell: "bash -x test.sh {{ script_params | default('') }} &> artifacts/{{ ansible_architecture }}/{{ ansible_architecture }}-runlog.txt"
args:
chdir: "{{ script_dir }}"
loop: "{{ scripts_dir.files | map(attribute='path') | list }}"
Expand Down
9 changes: 5 additions & 4 deletions src/com/redhat/ci/Job.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Job {
void run() {
Map subJobs = [:]
for (targetHost in targetHosts) {
subJobs[targetHost.arch] = jobWrapper(targetHost)
subJobs[targetHost.name ?: targetHost.id] = jobWrapper(targetHost)
}

// Run each single host job in parallel on each specified host
Expand Down Expand Up @@ -113,7 +113,8 @@ class Job {
try {
host = provision(targetHost)
} catch (e) {
script.echo("Exception: ${e.message}")
script.echo("Exception: ${e}")
script.echo("Stacktrace: $e.stackTrace")
runInDirectory(SANDBOX_DIR) {
onFailure(e, host)
}
Expand All @@ -128,7 +129,7 @@ class Job {
body(host, config)
}
} catch (e) {
script.echo("Exception: ${e.message}")
script.echo("Exception: ${e}")
runInDirectory(SANDBOX_DIR) {
onFailure(e, host)
}
Expand All @@ -144,7 +145,7 @@ class Job {
body(host, config)
}
} catch (e) {
script.echo("Exception: ${e.message}")
script.echo("Exception: ${e}")
runInDirectory(SANDBOX_DIR) {
onFailure(e, host)
}
Expand Down
3 changes: 3 additions & 0 deletions src/com/redhat/ci/hosts/Host.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ package com.redhat.ci.hosts
* Host primitives.
*/
class Host {
// Name for the host
String name = null

// ID for the host
String id = UUID.randomUUID()

Expand Down
6 changes: 6 additions & 0 deletions src/com/redhat/ci/hosts/ProvisionedHost.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ProvisionedHost extends TargetHost {

ProvisionedHost(TargetHost target) {
super()
this.name = target.name
this.id = target.id
this.arch = target.arch
this.distro = target.distro
Expand All @@ -50,9 +51,14 @@ class ProvisionedHost extends TargetHost {
this.providerPriority = target.providerPriority
this.provisioner = target.provisioner
this.provisionerPriority = target.provisionerPriority
this.linchpinTargetEnabled = target.linchpinTargetEnabled
this.linchpinTarget = target.linchpinTarget
this.bkrHostRequires = target.bkrHostRequires
this.bkrKeyValue = target.bkrKeyValue
this.bkrJobGroup = target.bkrJobGroup
this.bkrKsMeta = target.bkrKsMeta
this.bkrKernelOptions = target.bkrKernelOptions
this.bkrKernelOptionsPost = target.bkrKernelOptionsPost
this.bkrMethod = target.bkrMethod
this.reserveDuration = target.reserveDuration
this.scriptParams = target.scriptParams
Expand Down
15 changes: 15 additions & 0 deletions src/com/redhat/ci/hosts/TargetHost.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,32 @@ class TargetHost extends Host {
// Provisioner type priority list
List<String> provisionerPriority = null

// Flag that allows linchpinTarget to be disabled for custom PinFiles
Boolean linchpinTargetEnabled = true

// Target of the linchpin PinFile (if linchpinTargetEnabled flag is set to true)
String linchpinTarget = null

// Beaker hostrequires
// Overrides ProvisioningConfig's hostrequires
List<Map> bkrHostRequires = null

// Beaker keyvalue
List<String> bkrKeyValue = null

// Beaker jobgroup
// Overrides ProvisioningConfig's jobgroup
String bkrJobGroup = null

// Beaker ks_meta
String bkrKsMeta = null

// Beaker kernel_options
String bkrKernelOptions = null

// Beaker kernel_options_post
String bkrKernelOptionsPost = null

// Beaker installation method
String bkrMethod = null

Expand Down
5 changes: 4 additions & 1 deletion src/com/redhat/ci/provisioner/ProvisioningConfig.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package com.redhat.ci.provisioner
* Configuration needed to provision resources with a Provisioner.
*/
class ProvisioningConfig {
private static final String RELEASE_VERSION = 'v1.2.2'
private static final String RELEASE_VERSION = 'v1.3.0'
private static final String KRB_PRINCIPAL_CREDENTIAL_ID_DEFAULT = 'redhat-multiarch-qe-krbprincipal'
private static final String KEYTAB_CREDENTIAL_ID_DEFAULT = 'redhat-multiarch-qe-keytab'
private static final String SSH_PRIV_KEY_CREDENTIAL_ID_DEFAULT = 'redhat-multiarch-qe-sshprivkey'
Expand Down Expand Up @@ -109,6 +109,9 @@ class ProvisioningConfig {
// This is only needed for tests that will use it to install from pkgs.devel.redhat.com
Boolean installRhpkg = false

// Whether the job should teardown resources when complete
Boolean teardown = true

@SuppressWarnings('AbcMetric')
ProvisioningConfig(Map params = [:], Map env = [:]) {
params = params ?: [:]
Expand Down
16 changes: 11 additions & 5 deletions src/com/redhat/ci/provisioners/LinchPinProvisioner.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LinchPinProvisioner extends AbstractProvisioner {

private static final String HYPERVISOR = 'hypervisor'

private static final Map<String, String> LINCHPIN_TARGETS = [
private static final Map<String, String> DEFAULT_TARGETS = [
(com.redhat.ci.provider.Type.BEAKER):'beaker-slave',
]

Expand Down Expand Up @@ -52,8 +52,10 @@ class LinchPinProvisioner extends AbstractProvisioner {
host.provider = com.redhat.ci.provider.Type.BEAKER
host.typePriority = filterSupportedHostTypes(host.typePriority)
host.type = host.typePriority.size() == 1 ? host.typePriority[0] : UNKNOWN
host.arch = host.arch ?: 'x86_64'
host.distro = host.distro ?: 'RHEL-ALT-7.5'
host.variant = host.variant ?: 'Server'
host.linchpinTarget = host.linchpinTarget ?: DEFAULT_TARGETS[host.provider]

// Install keys we can connect via JNLP or SSH
Utils.installCredentials(script, config)
Expand All @@ -80,7 +82,7 @@ class LinchPinProvisioner extends AbstractProvisioner {
"linchpin --workspace ${workspaceDir} " +
"--config ${workspaceDir}/linchpin.conf " +
"--template-data \'${getTemplateData(host, config)}\' " +
"--verbose up ${LINCHPIN_TARGETS[host.provider]}"
"--verbose up ${host.linchpinTargetEnabled ? host.linchpinTarget : ''}"
)
} catch (e) {
host.error = e.message
Expand All @@ -95,6 +97,7 @@ class LinchPinProvisioner extends AbstractProvisioner {

host.inventoryPath = getLinchpinInventoryPath(linchpinLatest, host)
script.echo("inventoryPath:${host.inventoryPath}")
script.sh("cat ${host.inventoryPath}")

host.hostname = getHostname(host)
script.echo("hostname:${host.hostname}")
Expand Down Expand Up @@ -128,6 +131,7 @@ class LinchPinProvisioner extends AbstractProvisioner {
} catch (e) {
host.error = host.error ? host.error + ", ${e.message}" : e.message
script.echo("Error provisioning from LinchPin: ${host.error}")
script.echo("Stacktrace: $e.stackTrace")
}

// An error occured, so we should ensure resources are cleaned up
Expand All @@ -145,7 +149,7 @@ class LinchPinProvisioner extends AbstractProvisioner {
*/
void teardown(ProvisionedHost host, ProvisioningConfig config) {
// Check if the host was even created
if (!host) {
if (!host || !config.teardown) {
script.echo(TEARDOWN_NOOP)
return
}
Expand Down Expand Up @@ -193,10 +197,13 @@ class LinchPinProvisioner extends AbstractProvisioner {
distro:host.distro,
variant:host.variant,
ks_meta:host.bkrKsMeta,
kernel_options:host.bkrKernelOptions,
kernel_options_post:host.bkrKernelOptionsPost,
method:host.bkrMethod,
reserve_duration:host.reserveDuration,
job_group:host.bkrJobGroup ?: config.jobgroup,
hostrequires:getHostRequires(host, config),
keyvalue:host.bkrKeyValue,
inventory_vars:host.inventoryVars,
]

Expand Down Expand Up @@ -247,8 +254,7 @@ class LinchPinProvisioner extends AbstractProvisioner {

private String getLinchpinInventoryPath(Map linchpinLatest, ProvisionedHost host) {
Map linchpinTargets = linchpinLatest["${host.linchpinTxId}"]['targets'][0]
String linchpinTarget = LINCHPIN_TARGETS[host.provider]
linchpinTargets[linchpinTarget]['outputs']['inventory_path'][0]
linchpinTargets[host.linchpinTarget]['outputs']['inventory_path'][0]
}

private String getHostname(ProvisionedHost host) {
Expand Down
2 changes: 1 addition & 1 deletion src/com/redhat/ci/provisioners/NoOpProvisioner.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class NoOpProvisioner extends AbstractProvisioner {
*/
void teardown(ProvisionedHost host, ProvisioningConfig config) {
// Check if the host was even created
if (!host) {
if (!host || !config.teardown) {
return
}

Expand Down
30 changes: 30 additions & 0 deletions test/TestUtilsTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,36 @@ class TestUtilsTest extends PipelineTestScript {
assertNoExceptions()
}

@Test
void shouldRunTestOnMultiHosts() {
ProvisioningConfig config = TestUtils.getProvisioningConfig(this)
List<TargetHost> targets = [
TestUtils.newTargetHost(
provisioner:com.redhat.ci.provisioner.Type.LINCHPIN,
provider:com.redhat.ci.provider.Type.BEAKER,
),
TestUtils.newTargetHost(
provisioner:com.redhat.ci.provisioner.Type.LINCHPIN,
provider:com.redhat.ci.provider.Type.BEAKER,
linchpinTargetEnabled:false
),
TestUtils.newTargetHost(
provisioner:com.redhat.ci.provisioner.Type.LINCHPIN,
provider:com.redhat.ci.provider.Type.BEAKER,
linchpinTarget:'custom-target'
),
]
TestUtils.runTest(
this,
targets,
config,
body,
onFailure,
onComplete)

assertNoExceptions()
}

@Test
void shouldRunTestOnBareMetalHost() {
ProvisioningConfig config = TestUtils.getProvisioningConfig(this)
Expand Down
2 changes: 1 addition & 1 deletion test/resources/linchpin.latest
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"4": {"action": "up", "targets": [{"beaker-slave": {"1": {"uhash": "178e46", "rc": 0}, "outputs": {"inventory_path": ["/home/jpoulin/Projects/scratch/postup-hook-test/inventories/beaker-slave-178e46.inventory"], "resources": {"os_keypair_res": [], "rax_server_res": [], "aws_ec2_res": [], "os_server_res": [], "ovirt_vms_res": [], "aws_ec2_key_res": [], "gcloud_gce_res": [], "aws_s3_res": [], "duffy_res": [], "os_sg_res": [], "dummy_res": [{"failed": false, "changed": true, "hosts": ["dummy-node-178e46-0.example.net"], "dummy_file": "/tmp/dummy.hosts"}], "beaker_res": [], "aws_cfn_res": [], "os_heat_res": [], "os_obj_res": [], "libvirt_res": [], "os_volume_res": []}}, "inputs": {"hooks_data": {"postup": [{"context": true, "type": "ansible", "name": "test", "actions": [{"extra_vars": {"test": "Hello World"}, "playbook": "site.yml"}]}]}, "layout_data": {"inventory_layout": {"hosts": [{"count": 1, "name": "beaker-slave", "host_groups": ["rhel7", "certificate_authority", "repositories", "jenkins_slave", "master_node"]}]}}, "topology_data": {"topology_name": "beaker-slave", "resource_groups": [{"resource_group_name": "dummy-slaves", "resource_definitions": [{"count": 1, "role": "dummy_node", "name": "dummy-node"}], "resource_group_type": "dummy"}]}}}}]}}
{"4": {"action": "up", "targets": [{"beaker-slave": {"1": {"uhash": "178e46", "rc": 0}, "outputs": {"inventory_path": ["/home/jpoulin/Projects/scratch/postup-hook-test/inventories/beaker-slave-178e46.inventory"], "resources": {"os_keypair_res": [], "rax_server_res": [], "aws_ec2_res": [], "os_server_res": [], "ovirt_vms_res": [], "aws_ec2_key_res": [], "gcloud_gce_res": [], "aws_s3_res": [], "duffy_res": [], "os_sg_res": [], "dummy_res": [{"failed": false, "changed": true, "hosts": ["dummy-node-178e46-0.example.net"], "dummy_file": "/tmp/dummy.hosts"}], "beaker_res": [], "aws_cfn_res": [], "os_heat_res": [], "os_obj_res": [], "libvirt_res": [], "os_volume_res": []}}, "inputs": {"hooks_data": {"postup": [{"context": true, "type": "ansible", "name": "test", "actions": [{"extra_vars": {"test": "Hello World"}, "playbook": "site.yml"}]}]}, "layout_data": {"inventory_layout": {"hosts": [{"count": 1, "name": "beaker-slave", "host_groups": ["rhel7", "certificate_authority", "repositories", "jenkins_slave", "master_node"]}]}}, "topology_data": {"topology_name": "beaker-slave", "resource_groups": [{"resource_group_name": "dummy-slaves", "resource_definitions": [{"count": 1, "role": "dummy_node", "name": "dummy-node"}], "resource_group_type": "dummy"}]}}}, "custom-target": {"1": {"uhash": "178e46", "rc": 0}, "outputs": {"inventory_path": ["/home/jpoulin/Projects/scratch/postup-hook-test/inventories/beaker-slave-178e46.inventory"], "resources": {"os_keypair_res": [], "rax_server_res": [], "aws_ec2_res": [], "os_server_res": [], "ovirt_vms_res": [], "aws_ec2_key_res": [], "gcloud_gce_res": [], "aws_s3_res": [], "duffy_res": [], "os_sg_res": [], "dummy_res": [{"failed": false, "changed": true, "hosts": ["dummy-node-178e46-0.example.net"], "dummy_file": "/tmp/dummy.hosts"}], "beaker_res": [], "aws_cfn_res": [], "os_heat_res": [], "os_obj_res": [], "libvirt_res": [], "os_volume_res": []}}, "inputs": {"hooks_data": {"postup": [{"context": true, "type": "ansible", "name": "test", "actions": [{"extra_vars": {"test": "Hello World"}, "playbook": "site.yml"}]}]}, "layout_data": {"inventory_layout": {"hosts": [{"count": 1, "name": "beaker-slave", "host_groups": ["rhel7", "certificate_authority", "repositories", "jenkins_slave", "master_node"]}]}}, "topology_data": {"topology_name": "beaker-slave", "resource_groups": [{"resource_group_name": "dummy-slaves", "resource_definitions": [{"count": 1, "role": "dummy_node", "name": "dummy-node"}], "resource_group_type": "dummy"}]}}}}]}}
8 changes: 4 additions & 4 deletions vars/TestUtils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class TestUtils {
Closure test,
Closure onFailure,
Closure postRun = { }) {
TargetHost target = new TargetHost()
target.arch = arch
TargetHost target = new TargetHost(arch:arch, name:arch)
runTest(
script,
target,
Expand Down Expand Up @@ -77,7 +76,7 @@ class TestUtils {
Closure postRun = { }) {
List<TargetHost> targets = []
for (arch in arches) {
targets.push(new TargetHost(arch:arch))
targets.push(new TargetHost(arch:arch, name:arch))
}
runTest(
script,
Expand Down Expand Up @@ -108,6 +107,7 @@ class TestUtils {
Closure test,
Closure onFailure,
Closure postRun = { }) {
target.name ?: target.arch
runTest(
script,
[ target ],
Expand Down Expand Up @@ -168,7 +168,7 @@ class TestUtils {
// This adds the custom provisioner slave container to the pod. Must be first with name 'jnlp'
script.containerTemplate(
name:'jnlp',
image:"${config.dockerUrl}/${config.tenant}/${config.provisioningImage}-${config.version}",
image:"${config.dockerUrl}/${config.tenant}/${config.provisioningImage}:${config.version}",
ttyEnabled:false,
args:'${computer.jnlpmac} ${computer.name}',
command:'',
Expand Down
33 changes: 22 additions & 11 deletions vars/installBrewPkgs.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
void call(Map params, Boolean privileged = false) {
String sudo = privileged ? 'sudo' : ''
import com.redhat.ci.Utils
import com.redhat.ci.hosts.ProvisionedHost
import com.redhat.ci.provisioner.ProvisioningConfig

void call(Map params, Boolean privileged = false, ProvisioningConfig config = null, ProvisionedHost host = null) {
Boolean taskRepoCreated = false
if (params.CI_MESSAGE != '') {
tid = getTaskId(params.CI_MESSAGE)
Expand All @@ -11,14 +14,22 @@ void call(Map params, Boolean privileged = false) {
}

if (taskRepoCreated == true) {
sh """
${sudo} yum install -y yum-utils
URL=\$(cat task-repo.properties | grep TASK_REPO_URLS= | sed 's/TASK_REPO_URLS=//' | sed 's/;/\\n/g')
${sudo} yum-config-manager --add-repo \${URL}
${sudo} cat /etc/yum.repos.d/*download.eng.bos.redhat.com*
${sudo} sed -i 's/gpgcheck=1/gpgcheck=0/g' /etc/yum.repos.d/*download.eng.bos.redhat.com*
echo "gpgcheck=0" | ${sudo} tee -a /etc/yum.repos.d/*download.eng.bos.redhat.com*
${sudo} yum clean all
"""
Utils.genericInstall(this, config ?: new ProvisioningConfig(), host) {
privilegedOverride, sh ->
context = [
env:[HOME:env.HOME],
files:['task-repo.properties'],
]
String sudo = (privileged || privilegedOverride) ? 'sudo' : ''
sh("""
${sudo} yum install -y yum-utils
URL=\$(cat task-repo.properties | grep TASK_REPO_URLS= | sed 's/TASK_REPO_URLS=//' | sed 's/;/\\n/g')
${sudo} yum-config-manager --add-repo \${URL}
${sudo} cat /etc/yum.repos.d/*download.eng.bos.redhat.com*
${sudo} sed -i 's/gpgcheck=1/gpgcheck=0/g' /etc/yum.repos.d/*download.eng.bos.redhat.com*
echo "gpgcheck=0" | ${sudo} tee -a /etc/yum.repos.d/*download.eng.bos.redhat.com*
${sudo} yum clean all
""", context)
}
}
}
7 changes: 4 additions & 3 deletions vars/runTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import com.redhat.ci.hosts.ProvisionedHost
import com.redhat.ci.provisioner.Mode

void call(ProvisioningConfig config, ProvisionedHost host) {
final String ACTIVATE_PROVISIONER = '. /home/jenkins/envs/provisioner/bin/activate;'
final String ACTIVATE_PROVISIONER = '. /home/jenkins/envs/ansible/bin/activate;'
List<Exception> exceptions = []

// JNLP Mode
Expand Down Expand Up @@ -43,8 +43,9 @@ void call(ProvisioningConfig config, ProvisionedHost host) {
writeFile(file:runScriptsPlaybook, text:runScripts)
sh("""
${ACTIVATE_PROVISIONER}
ansible-playbook -i '${host.inventoryPath}' --key-file "~/.ssh/id_rsa" \
-e '{"test_dir":"${params.TEST_DIR}", "script_params":"${host.scriptParams ?: ''}"}' \
ansible-playbook -i '${host.inventoryPath}' --limit master_node --key-file "~/.ssh/id_rsa" \
-e '{"test_dir":"${params.TEST_DIR}", "inventory":"${host.inventoryPath}", \
"script_params":"${host.scriptParams ?: ''}"}' \
${runScriptsPlaybook}
""")
} catch (e) {
Expand Down
Loading

0 comments on commit 0c1f6d3

Please sign in to comment.