Skip to content

Commit

Permalink
Merge pull request #2 from kalenpeterson/feature-userscript
Browse files Browse the repository at this point in the history
Feature userscript
  • Loading branch information
kalenpeterson committed Sep 20, 2023
2 parents eaf9bfe + fab68bd commit a615c6f
Show file tree
Hide file tree
Showing 19 changed files with 1,039 additions and 290 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,8 @@ ENV/

# Remove Shell Tests
tests/

# Remove Ansible files
inventory
all.yml
*.env
9 changes: 8 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ RUN pip install --upgrade pip && \

COPY src/ ./

ENTRYPOINT ["python", "main.py"]
COPY docker-entrypoint.sh ./

EXPOSE 8000

ENTRYPOINT ["/app/docker-entrypoint.sh"]

# Set the default startup service to chargeback
CMD ["chargeback"]
36 changes: 36 additions & 0 deletions build/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
# Build and Push the image for all defined ENVs

# A version tag is required input and will be appended to the image tag
VERSION_TAG=$1
if [[ -z ${VERSION_TAG} ]]; then
echo "A Version tag must be provided"
exit 1
fi

# Simple Logging function
log () {
echo $(date) [info] $1
}

# Build each environment found in this directory
# Use the provided my.env.example as a base for your ENV
# cp my.env.example .my.env" and edit as needed
for env in $(ls -a .*.env |awk -F. '{print $2}')
do

source .${env}.env
log "Building ${env} Image"
cat .${env}.env \
| xargs printf -- '--build-arg %s\n' \
| xargs podman build \
-t ${IMAGE_NAME}:${env}-v${VERSION_TAG} \
-t ${IMAGE_NAME}:${env}-latest ../ || exit 1

log "Pushing ${env} Image: ${IMAGE_NAME}:${env}-v${VERSION_TAG}"
podman push ${IMAGE_NAME}:${env}-v${VERSION_TAG} || exit 1
log "Pushing ${env} Image: ${IMAGE_NAME}:${env}-latest "
podman push ${IMAGE_NAME}:${env}-latest || exit 1
done

log "Build(s) Completed"
1 change: 1 addition & 0 deletions build/my.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IMAGE_NAME=docker.io/account/repository
36 changes: 0 additions & 36 deletions config/kubernetes-config.yml

This file was deleted.

35 changes: 0 additions & 35 deletions config/kubernetes-cronjob.sleep.yml

This file was deleted.

30 changes: 0 additions & 30 deletions config/kubernetes-cronjob.yml

This file was deleted.

81 changes: 81 additions & 0 deletions deploy/deploy-chargeback.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
- name: Deploy Chargeback Kubernetes Resources
hosts: provision
gather_facts: no

tasks:
- name: Create Tempfile for Kube Manifest
tempfile:
state: file
suffix: temp
register: temp_kube_manifest
notify: Cleanup Tempfile

- name: Generate the Manifest from Template
template:
src: templates/kubernetes.yml.j2
dest: "{{ temp_kube_manifest.path }}"

- name: Apply Deployment
shell: |
kubectl apply -n "{{ kubernetes.namespace }}" -f "{{ temp_kube_manifest.path }}"
register: kubectl_out

- name: Print Kubectl Apply Output
debug:
var: kubectl_out.stdout

handlers:
- name: Cleanup Tempfile
file:
state: absent
path: "{{ temp_kube_manifest }}"
when: temp_kube_manifest.path is defined and
kubernetes.cleanup_temp_manifest is true

- name: Deploy Chargeback Client Resources
hosts: mgmt
gather_facts: no

tasks:
- name: Install Python3 Devel
yum:
name: python3-devel
state: present
become: yes
delegate_to: localhost
run_once: true

- name: Install Python requirements
pip:
executable: pip3
name:
- requests
- prettytable
- cython
become: yes

- name: Compile Python to C Binary
shell: |
cython --embed -o /tmp/{{ chargeback.cli_binary_name }}.c ../src/cli.py
gcc -Os -I /usr/include/python3.6m -o /tmp/{{ chargeback.cli_binary_name }} /tmp/{{ chargeback.cli_binary_name }}.c -lpython3.6m -lpthread -lm -lutil -ldl
delegate_to: localhost
run_once: true

- name: Copy Binary to path
copy:
src: /tmp/{{ chargeback.cli_binary_name }}
dest: /usr/local/bin/{{ chargeback.cli_binary_name }}
mode: 755
owner: root
become: yes

- name: Cleanup Temp Files
file:
path: '{{ item }}'
state: absent
loop:
- /tmp/{{ chargeback.cli_binary_name }}
- /tmp/{{ chargeback.cli_binary_name }}.c
delegate_to: localhost
run_once: true
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CREATE TABLE `gpu_usage` (
`gpus_requested` INT(2) NULL DEFAULT '0' COMMENT 'Number of GPUs Requested',
`gpus_used` INT(2) NULL DEFAULT '0' COMMENT 'Number of GPUs Used',
`added` DATETIME NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Datetime when this record was added',
`partition` CHAR(128) NOT NULL DEFAULT '' COMMENT 'Slurm Partition Name' COLLATE 'latin1_swedish_ci'
PRIMARY KEY (`job_id`) USING BTREE
)
COLLATE='latin1_swedish_ci'
Expand Down
45 changes: 45 additions & 0 deletions deploy/group_vars/all.yml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
slurm:
job_prev_days: 14
assoc_backend: slurm_acctdb
partition_filter: ''
cluster_name:
db:
host:
port: 3306
username:
password:
chargeback:
db:
host:
port: 3306
username:
password:
schema_name:
table_name:
gpu_usd_cost_per_minute:
cli_binary_name: charges
ssh:
host:
port: 22
username:
password:
file_cleanup: False
email:
smtp:
host:
port:
username:
password:
to:
address: 'User <user.name@example.com>'
from:
address: 'DGX Chargeback <no-reply@example.com>'
kubernetes:
namespace: default
cleanup_temp_manifest: False
cronjob:
image: docker.io/kalenpeterson/dgx-chargeback:build-19
api:
replicas: 1
image: docker.io/kalenpeterson/dgx-chargeback:build-19
Loading

0 comments on commit a615c6f

Please sign in to comment.