diff --git a/Dockerfile-DB b/Dockerfile-DB deleted file mode 100644 index ae58143..0000000 --- a/Dockerfile-DB +++ /dev/null @@ -1,2 +0,0 @@ -FROM mysql:5.7 -ADD FabAccess.sql /docker-entrypoint-initdb.d \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..0a62865 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,54 @@ +pipeline { + agent any + post { + failure { + updateGitlabCommitStatus name: 'build', state: 'failed' + } + success { + updateGitlabCommitStatus name: 'build', state: 'success' + } + aborted { + updateGitlabCommitStatus name: 'build', state: 'canceled' + } + } + options { + gitLabConnection('GitLab') + } + triggers { + gitlab(triggerOnPush: true, triggerOnMergeRequest: true, branchFilterType: 'All') + } + stages { + stage("build") { + steps { + updateGitlabCommitStatus name: 'build', state: 'running' + podTemplate( + containers: [ + containerTemplate(name: 'docker-image-build', image: 'mgoltzsche/podman', ttyEnabled: true, command: 'cat', privileged: true), + ]) { + node(POD_LABEL) { + updateGitlabCommitStatus name: 'build', state: 'pending' + checkout([$class: 'GitSCM', branches: [ + [name: '*/main'] + ], userRemoteConfigs: [ + [url: 'https://gitlab.com/luca_lutz/fabaccess.git'] + ]]) + container('docker-image-build') { + withCredentials([ + usernamePassword(credentialsId: 'docker.credentials', + usernameVariable: 'DOCKER_USERNAME', + passwordVariable: 'DOCKER_PASSWORD') + ]) { + sh 'echo $(date +%s) > /time' + sh 'echo "nameserver 10.12.42.2" > /etc/resolv.conf' + sh 'podman build --tag docker.sfz-aalen.space/hackwerk/fabaccess:$(cat /time) .' + sh 'podman login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} docker.sfz-aalen.space' + sh 'podman push docker.sfz-aalen.space/hackwerk/fabaccess:$(cat /time)' + } + } + } + } + } + } + } +} + diff --git a/fab_access/mqtt_helper.py b/fab_access/mqtt_helper.py new file mode 100644 index 0000000..bd5fc50 --- /dev/null +++ b/fab_access/mqtt_helper.py @@ -0,0 +1,32 @@ +from paho.mqtt import client as mqtt_client + +import config + + +def on_connect(client, userdata, flags, rc): + if rc == 0: + print("Connected to MQTT Broker!") + else: + raise Exception("Failed to connect, return code %d\n", rc) + + +class MQTTHelper(): + def __init__(self, client_name: str, username: str, password: str, broker: str, port: int): + client = mqtt_client.Client(client_name) + if username and password: + client.username_pw_set(username, password) + else: + print("Connecting to MQTT without credentials") + client.on_connect = on_connect + try: + client.connect(broker, port) + except Exception as e: + raise Exception(f"Error connecting to MQTT {broker}:{port}") from e + self.client = client + + def subscribe(self, topic: str, handler): + self.client.subscribe(topic) + self.client.on_message = handler + + def loop_forever(self): + self.client.loop_forever() diff --git a/sql/FabAccess.sql b/sql/FabAccess.sql new file mode 100644 index 0000000..d1d9358 --- /dev/null +++ b/sql/FabAccess.sql @@ -0,0 +1,11 @@ +CREATE TABLE ReaderPlug( + ReaderID INT NOT NULL, + PlugName VARCHAR(255) NOT NULL, + PermissionPath VARCHAR(255) NOT NULL, + Status BOOLEAN NOT NULL, + LastUser VARCHAR(255) NOT NULL +); + +INSERT INTO ReaderPlug( + ReaderID, PlugName, PermissionPath, Status, LastUser +) VALUES (1, 'lasercutter', 'sfz.lasercutter.trotec', false, 'luca.lutz');