Commit 9665d53a authored by Alex Iribarren's avatar Alex Iribarren
Browse files

Bare-bones skeleton project

parents
Pipeline #525602 passed with stages
in 1 minute and 4 seconds
---
variables:
VAULT_VERSION: '0.11.1'
NOMAD_VERSION: '0.8.4'
NOMAD_ADDR: "https://${CLUSTER_ADDR}:4646"
VAULT_ADDR: "https://${CLUSTER_ADDR}:8200"
before_script:
- if [[ $CI_COMMIT_REF_NAME == 'master' || -n ${CI_COMMIT_TAG+x} ]]; then PREFIX='prod'; else PREFIX='dev'; fi
- set -a
- source ${PREFIX}.variables.sh
stages:
- prepare
- build
- deploy_test
- deploy
- cleanup
build_hello:
stage: build
tags:
- docker-image-build
script: "echo" # unused but this line is required by GitLab CI
variables:
CONTEXT_DIR: hello
TO: $CI_REGISTRY_IMAGE/say_hello:$CI_COMMIT_REF_NAME
.nomad_deps_template: &nomad_deps
curl https://releases.hashicorp.com/vault/${VAULT_VERSION}/vault_${VAULT_VERSION}_linux_amd64.zip -o /tmp/vault.zip &&
curl https://releases.hashicorp.com/nomad/${NOMAD_VERSION}/nomad_${NOMAD_VERSION}_linux_amd64.zip -o /tmp/nomad.zip &&
unzip /tmp/vault.zip -d /usr/local/bin/ &&
unzip /tmp/nomad.zip -d /usr/local/bin/ &&
vault read nomad/creds/submitjobs -format=json > token.json &&
export `jq -r '"NOMAD_TOKEN=" + .data.secret_id' token.json` &&
if [ -z ${CI_COMMIT_TAG+x} ]; then export FORCE_PULL="true"; else export FORCE_PULL="false"; fi
deploy_test:
stage: deploy_test
dependencies: []
script:
- *nomad_deps
- for j in *.nomad; do echo -e "\n${j}:"; nomad job validate <(envsubst < $j); done
- for j in *.nomad; do echo -e "\n${j}:"; (nomad job plan -verbose <(envsubst < $j) || [[ $? -eq 0 || $? -eq 1 ]]) || break; done
# plan returns 0 or 1 on success
deploy:
stage: deploy
dependencies:
- deploy_test
script:
- *nomad_deps
- for j in *.nomad; do echo -e "\n${j}:"; nomad job run <(envsubst < $j); done
stop_testing:
stage: cleanup
dependencies:
- deploy
script:
- *nomad_deps
- JOB_PREFIX="`source dev.variables.sh && echo $JOB_PREFIX`"
- for j in *.nomad; do JOB=`grep task <(envsubst < $j) | sed 's/.*"\([^"]*\)".*/\1/'`; echo -e "\nDeleting job ${JOB}:"; nomad job stop -verbose ${JOB} || true; done
only:
- tags
- master
JOB_PREFIX="dev_"
SCHEDULE="* * * * *"
job "${JOB_PREFIX}hello" {
datacenters = ["meyrin"]
type = "batch"
periodic {
// Launch every minute
cron = "${SCHEDULE}"
time_zone = "Europe/Zurich"
prohibit_overlap = true
}
task "${JOB_PREFIX}hello" {
driver = "docker"
config {
image = "https://gitlab-registry.cern.ch/linuxsupport/cronjobs/hello_world/hello:${CI_COMMIT_REF_NAME}"
force_pull = ${FORCE_PULL}
logging {
type = "gelf"
config {
gelf-address = "udp://127.0.0.1:12201"
env = "NOMAD_JOB_NAME,NOMAD_GROUP_NAME,NOMAD_TASK_NAME,NOMAD_ALLOC_ID,NOMAD_ALLOC_NAME,NOMAD_ALLOC_INDEX,NOMAD_META_PARENT_JOB"
}
}
}
vault {
policies = ["nomad_write"]
}
template {
data = <<EOF
NOMAD_TOKEN="{{ with secret "nomad/creds/write" }}{{.Data.secret_id}}{{end}}"
EOF
destination = "secrets/nomad_token.env"
env = true
}
env {
NOMAD_ADDR = "$NOMAD_ADDR"
}
resources {
cpu = 100 # Mhz
memory = 128 # MB
network {
mbits = 1
}
}
}
}
FROM cern/cc7-base
COPY hello.py /root/
WORKDIR /root
ENTRYPOINT ["/root/hello.py"]
#!/usr/bin/python
import json
import os
import sys
def log(**kwargs):
print(json.dumps(kwargs))
def debug(**kwargs):
kwargs.update({'message_type': 'debug'})
log(**kwargs)
def error(**kwargs):
kwargs.update({'message_type': 'error'})
log(**kwargs)
try:
nomad_token = os.environ['NOMAD_TOKEN']
except KeyError:
error(comment='Error obtaining Nomad token!')
sys.exit(1)
log(comment='Hello world!')
JOB_PREFIX="prod_"
SCHEDULE="0 */12 * * *"
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment