This commit is contained in:
Waylon S. Walker 2024-09-23 10:06:32 -05:00
commit fddad41456
14 changed files with 576 additions and 0 deletions

View file

@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/

View file

@ -0,0 +1,24 @@
apiVersion: v2
name: temperature-cronjob
description: A Helm chart for Kubernetes
# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0
# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"

View file

@ -0,0 +1,36 @@
# temperature_fetcher.py
import datetime
import os
import requests
import sqlite3
print("hey im running")
city = os.getenv("CITY")
api_key = os.getenv("API_KEY")
mount_path = os.getenv("MOUNT_PATH", "/data")
try:
response = requests.get(
f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=imperial"
)
response.raise_for_status()
temp = response.json()["main"]["temp"]
db_path = f"{mount_path}/temperature.db"
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute(
"CREATE TABLE IF NOT EXISTS temperatures (timestamp TEXT, temperature REAL)"
)
c.execute(
"INSERT INTO temperatures VALUES (?, ?)",
(datetime.datetime.now().isoformat(), temp),
)
conn.commit()
conn.close()
print(
f"Successfully stored temperature {temp}°C for {city} at {datetime.datetime.now()}"
)
except Exception as e:
print(f"An error occurred: {e}")

View file

@ -0,0 +1,8 @@
# templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-script
data:
temperature_fetcher.py: |
{{ .Files.Get "temperature_fetcher.py" | indent 4 }}

View file

@ -0,0 +1,45 @@
# templates/cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: {{ .Release.Name }}-cronjob
spec:
schedule: "{{ .Values.schedule }}"
jobTemplate:
spec:
template:
spec:
containers:
- name: temperature-fetcher
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ["/bin/bash", "-c"]
args:
- |
echo "Running temperature_fetcher.py" && \
pip install requests && \
echo "Fetching temperature" && \
python /scripts/temperature_fetcher.py && \
sleep 300
env:
- name: CITY
value: "{{ .Values.city }}"
- name: API_KEY
value: "{{ .Values.apiKey }}"
- name: MOUNT_PATH
value: "{{ .Values.mountPath }}"
volumeMounts:
- name: sqlite-data
mountPath: "{{ .Values.mountPath }}"
- name: script-volume
mountPath: /scripts
restartPolicy: OnFailure
volumes:
- name: sqlite-data
hostPath:
path: "{{ .Values.hostPath }}"
type: Directory
- name: script-volume
configMap:
name: {{ .Release.Name }}-script

View file

@ -0,0 +1,10 @@
# values.yaml
image:
repository: python
tag: "3.9-slim"
pullPolicy: IfNotPresent
schedule: "*/1 * * * *" # Every minute
city: "Dunlap, US"
apiKey: "your_api_key"
mountPath: "/data"
hostPath: "/sqlite-data"