Skip to content

Commit

Permalink
Merge pull request #895 from kuisathaverat/feat/portable
Browse files Browse the repository at this point in the history
feat: make demo more portable
  • Loading branch information
kuisathaverat authored Jul 11, 2024
2 parents a3ddfca + 4b2052c commit b310419
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 51 deletions.
17 changes: 1 addition & 16 deletions demos/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.DEFAULT_GOAL := start

export OTELCOL_IMG?=otel/opentelemetry-collector-contrib-dev:latest
export ELASTIC_STACK_VERSION?=8.10.1
export ELASTIC_STACK_VERSION?=8.14.2

.PHONY: build
build:
Expand All @@ -18,18 +18,3 @@ stop:
.PHONY: clean
clean:
@env docker-compose --file docker-compose.yml down -v

.PHONY: start-all ## Start everything locally
start-all: start start-local-worker

.PHONY: stop-all ## Stop everything locally
stop-all: stop-local-worker stop

.PHONY: start-local-worker
start-local-worker: ## Start the local worker
[ ! -e agent.jar ] && wget --quiet -O agent.jar http://localhost:8080/jnlpJars/agent.jar || true
{ nohup java -jar agent.jar -jnlpUrl http://localhost:8080/computer/local/jenkins-agent.jnlp > local.log 2>&1 & echo $$! > local.pid; }

.PHONY: stop-local-worker
stop-local-worker: ## Stop the local worker
[ -e local.pid ] && ( kill -9 `cat local.pid` ; rm local.pid ) || true
37 changes: 28 additions & 9 deletions demos/config/ansible.dsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,44 @@ executable = /bin/bash
module_lang = en_US.UTF-8
callbacks_enabled = community.general.opentelemetry
'''
installHermit()
}
}
stage('run-ansible') {
steps {
script {
// some magic with -u root:root to bypass the due to: 'getpwuid(): uid not found in ansible
docker.image('geerlingguy/docker-ubuntu2004-ansible').inside('-u root:root --network demos_jenkins') {
// some magic withEnv to bypass Permission denied: b'/.ansible'
withEnv(["HOME=\${env.WORKSPACE}"]) {
sh(label: 'fetch the community.general collection', script: 'ansible-galaxy collection install community.general')
sh(label: 'pip3 install', script: 'pip3 install opentelemetry-api opentelemetry-sdk opentelemetry-exporter-otlp')
sh(label: 'run ansible', script: 'ansible-playbook playbook.yml')
runWithHermit(){
sh(label: 'fetch the community.general collection', script: 'ansible-galaxy collection install community.general')
sh(label: 'pip3 install', script: 'pip3 install opentelemetry-api opentelemetry-sdk opentelemetry-exporter-otlp')
sh(label: 'run ansible', script: 'ansible-playbook playbook.yml')
}
}
}
}
}
}
}"""
}

def runWithHermit(Closure body){
hermitEnvVars = sh(returnStdout: true, script: './hermit/bin/hermit env --raw').trim()
withEnv(hermitEnvVars.split('\\n').toList()) {
body()
}
}

def installHermit() {
sh(label: 'installHermit',
script: 'curl -fsSL https://github.com/cashapp/hermit/releases/download/stable/install.sh | /bin/bash')
sh(label: 'install tools',
script: '''
mkdir -p hermit
cd hermit
~/bin/hermit init
eval \$(./bin/hermit env --raw)
hermit install python3
pip install ansible
''')
}
"""

pipelineJob(NAME) {
definition {
Expand Down
6 changes: 1 addition & 5 deletions demos/config/gradle.dsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,18 @@ DSL = """pipeline {
stage('build') {
steps {
script {
docker.image('openjdk:8-jdk-alpine').inside('--network demos_jenkins') {
withEnv(["HOME=\${env.WORKSPACE}"]) {
sh(label: 'gradle build', script: './gradlew clean build')
sh(label: 'gradle build', script: './gradlew clean build')
}
}
}
}
}
stage('test') {
steps {
script {
docker.image('openjdk:8-jdk-alpine').inside('--network demos_jenkins') {
withEnv(["HOME=\${env.WORKSPACE}"]) {
sh(label: 'gradle test', script: './gradlew clean test')
}
}
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions demos/config/import_dashboard.dsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
NAME = 'import_dashboard'
DSL = """pipeline {
agent any
stages {
stage('Import Dashboard') {
steps {
sh(label: 'Download dashboard', script: 'curl -sSfL -o dashboard.ndjson https://raw.githubusercontent.com/jenkinsci/opentelemetry-plugin/main/src/main/kibana/jenkins-kibana-dashboards.ndjson')
sh(label: 'Import Dashboard', script: 'curl -X POST -u "admin:changeme" http://kibana:5601/api/saved_objects/_import -H "kbn-xsrf: true" --form [email protected]')
}
}
}
}
"""

pipelineJob(NAME) {
definition {
cps {
script(DSL.stripIndent())
}
}
}
4 changes: 4 additions & 0 deletions demos/config/jenkins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ credentials:
scope: GLOBAL
username: "jenkins_logs_user"
unclassified:
location:
url: "http://jenkins:8080/"
openTelemetry:
authentication: "noAuthentication"
endpoint: "http://otel-collector:4317"
Expand All @@ -53,3 +55,5 @@ jobs:
- file: "/var/jenkins_home/config/maven.dsl"
- file: "/var/jenkins_home/config/otel-cli.dsl"
- file: "/var/jenkins_home/config/otel-cli-make.dsl"
- file: "/var/jenkins_home/config/import_dashboard.dsl"
- file: "/var/jenkins_home/config/pytest-otel.dsl"
14 changes: 5 additions & 9 deletions demos/config/maven.dsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,25 @@ DSL = '''pipeline {
stage('prepare') {
steps {
sh (label: 'fetch opentelemetry-maven-extension',
script: 'curl -s https://repo.maven.apache.org/maven2/io/opentelemetry/contrib/opentelemetry-maven-extension/${OTEL_VERSION}/opentelemetry-maven-extension-${OTEL_VERSION}.jar > otel.jar')
script: 'curl -s https://repo.maven.apache.org/maven2/io/opentelemetry/contrib/opentelemetry-maven-extension/\${OTEL_VERSION}/opentelemetry-maven-extension-\${OTEL_VERSION}.jar > otel.jar')
}
}
stage('compile') {
steps {
script {
docker.image('openjdk:8-jdk-alpine').inside('--network demos_jenkins') {
withEnv(["HOME=${env.WORKSPACE}"]) {
sh(label: 'mvn compile', script: './mvnw -B ${MAVEN_OPTS} clean compile')
withEnv(["HOME=\${env.WORKSPACE}"]) {
sh(label: 'mvn compile', script: './mvnw -B \${MAVEN_OPTS} clean compile')
}
}
}

}
}
stage('test') {
steps {
script {
docker.image('openjdk:8-jdk-alpine').inside('--network demos_jenkins') {
withEnv(["HOME=${env.WORKSPACE}"]) {
sh(label: 'mvn test', script: './mvnw -B ${MAVEN_OPTS} test')
withEnv(["HOME=\${env.WORKSPACE}"]) {
sh(label: 'mvn test', script: './mvnw -B \${MAVEN_OPTS} test')
}
}
}
}
post {
Expand Down
31 changes: 28 additions & 3 deletions demos/config/otel-cli-make.dsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,60 @@ test:
stage('prepare') {
steps {
installOtelCli()
installHermit()
}
}
stage('make build') {
steps {
withEnv(["PATH+OTEL=\${OTEL_LOCATION}"]) {
sh(label: 'make build', script: "OTEL_EXPORTER_OTLP_ENDPOINT=\${env.OTEL_EXPORTER_OTLP_ENDPOINT.replaceAll('http://', '')} make build")
runWithHermit(){
sh(label: 'make build', script: "OTEL_EXPORTER_OTLP_ENDPOINT=\${env.OTEL_EXPORTER_OTLP_ENDPOINT.replaceAll('http://', '')} make build")
}
}
}
}
stage('make test') {
steps {
withEnv(["PATH+OTEL=\${OTEL_LOCATION}"]) {
sh(label: 'make test', script: "OTEL_EXPORTER_OTLP_ENDPOINT=\${env.OTEL_EXPORTER_OTLP_ENDPOINT.replaceAll('http://', '')} make test")
runWithHermit(){
sh(label: 'make test', script: "OTEL_EXPORTER_OTLP_ENDPOINT=\${env.OTEL_EXPORTER_OTLP_ENDPOINT.replaceAll('http://', '')} make test")
}
}
}
}
}
}

def runWithHermit(Closure body){
hermitEnvVars = sh(returnStdout: true, script: './hermit/bin/hermit env --raw').trim()
withEnv(hermitEnvVars.split('\\n').toList()) {
body()
}
}

def installOtelCli() {
def os = 'Linux'
if (sh(script: 'uname -a | grep -i Darwin', returnStatus: true) == 0) {
os = 'Darwin'
}
dir("\${OTEL_LOCATION}") {
sh(label: 'fetch otel-cli',
script: "wget -q 'https://github.com/equinix-labs/otel-cli/releases/download/v0.0.18/otel-cli-0.0.18-\${os}-x86_64.tar.gz' -O otel-cli.tar.gz && tar -xf otel-cli.tar.gz")
script: "curl -sSfL -o otel-cli.tar.gz 'https://github.com/equinix-labs/otel-cli/releases/download/v0.0.18/otel-cli-0.0.18-\${os}-x86_64.tar.gz' && tar -xf otel-cli.tar.gz")
}
}

def installHermit() {
sh(label: 'installHermit',
script: 'curl -fsSL https://github.com/cashapp/hermit/releases/download/stable/install.sh | /bin/bash')
sh(label: 'install tools',
script: '''
mkdir -p hermit
cd hermit
~/bin/hermit init
eval \$(./bin/hermit env --raw)
hermit install make
''')
}
"""

pipelineJob(NAME) {
Expand Down
29 changes: 26 additions & 3 deletions demos/config/otel-cli.dsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test:
stage('prepare') {
steps {
installOtelCli()
installHermit()
}
}
stage('make build') {
Expand All @@ -38,21 +39,43 @@ test:

def runWithOtelCli(def args=[:]) {
withEnv(["PATH+OTEL=\${OTEL_LOCATION}"]) {
sh 'env | sort'
sh(label: args.label, script: "OTEL_EXPORTER_OTLP_ENDPOINT=\${env.OTEL_EXPORTER_OTLP_ENDPOINT.replaceAll('http://', '')} otel-cli exec --name '\${args.label}'--tp-print \${args.script}")
runWithHermit{
sh 'env | sort'
sh(label: args.label, script: "OTEL_EXPORTER_OTLP_ENDPOINT=\${env.OTEL_EXPORTER_OTLP_ENDPOINT.replaceAll('http://', '')} otel-cli exec --name '\${args.label}'--tp-print \${args.script}")
}
}
}

def runWithHermit(Closure body){
hermitEnvVars = sh(returnStdout: true, script: './hermit/bin/hermit env --raw').trim()
withEnv(hermitEnvVars.split('\\n').toList()) {
body()
}
}

def installOtelCli() {
def os = 'Linux'
if (sh(script: 'uname -a | grep -i Darwin', returnStatus: true) == 0) {
os = 'Darwin'
}
dir("\${OTEL_LOCATION}") {
sh(label: 'fetch otel-cli',
script: "wget 'https://github.com/equinix-labs/otel-cli/releases/download/v0.0.18/otel-cli-0.0.18-\${os}-x86_64.tar.gz' -O otel-cli.tar.gz && tar -xf otel-cli.tar.gz")
script: "curl -sSfL -o otel-cli.tar.gz 'https://github.com/equinix-labs/otel-cli/releases/download/v0.0.18/otel-cli-0.0.18-\${os}-x86_64.tar.gz' && tar -xf otel-cli.tar.gz")
}
}

def installHermit() {
sh(label: 'installHermit',
script: 'curl -fsSL https://github.com/cashapp/hermit/releases/download/stable/install.sh | /bin/bash')
sh(label: 'install tools',
script: '''
mkdir -p hermit
cd hermit
~/bin/hermit init
eval \$(./bin/hermit env --raw)
hermit install make
''')
}
"""

pipelineJob(NAME) {
Expand Down
4 changes: 4 additions & 0 deletions demos/config/otel-collector-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318

exporters:
prometheus:
Expand Down Expand Up @@ -61,4 +64,5 @@ service:
exporters: [debug, prometheus, otlp/elastic]
logs:
receivers: [otlp]
processors: [batch]
exporters: [debug, otlp/elastic]
2 changes: 1 addition & 1 deletion demos/config/plugins.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ git
filesystem_scm
jdk-tool
job-dsl
opentelemetry
opentelemetry::https://repo.jenkins-ci.org/incrementals/io/jenkins/plugins/opentelemetry/3.1309.vb_ea_11a_392465/opentelemetry-3.1309.vb_ea_11a_392465.hpi
pipeline-model-definition
swarm
workflow-aggregator
Expand Down
55 changes: 55 additions & 0 deletions demos/config/pytest-otel.dsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
NAME = 'pytest-otel'
DSL = """pipeline {
agent any
stages {
stage('checkout') {
steps {
git(url: 'https://github.com/kuisathaverat/pytest_otel.git', branch: 'main')
}
}
stage('prepare-python') {
steps {
installHermit()
}
}
stage('test') {
steps {
script {
runWithHermit(){
sh(label: 'dependencies', script: 'pip install ".[test]"')
sh(label: 'pytest', script: 'python -m pytest --capture=no docs/demos/test/test_demo.py')
}
}
}
}
}
}

def runWithHermit(Closure body){
hermitEnvVars = sh(returnStdout: true, script: './hermit/bin/hermit env --raw').trim()
withEnv(hermitEnvVars.split('\\n').toList()) {
body()
}
}

def installHermit() {
sh(label: 'installHermit',
script: 'curl -fsSL https://github.com/cashapp/hermit/releases/download/stable/install.sh | /bin/bash')
sh(label: 'install tools',
script: '''
mkdir -p hermit
cd hermit
~/bin/hermit init
eval \$(./bin/hermit env --raw)
hermit install python3
''')
}
"""

pipelineJob(NAME) {
definition {
cps {
script(DSL.stripIndent())
}
}
}
Loading

0 comments on commit b310419

Please sign in to comment.