25 Commits

Author SHA1 Message Date
cacb1c0254 fix makefile concerning libconfig 2023-09-28 18:45:22 +02:00
8afcfef5e8 remove cluster test stuff 2023-09-28 18:39:47 +02:00
f798ef400d cluster test 2 2023-09-28 18:36:43 +02:00
a17ef7b6ec cluster test 2023-09-28 18:35:05 +02:00
104be55bdf cluster test 2023-09-28 18:33:03 +02:00
19eb9325ca add cluster agent configuration 2023-09-28 18:10:58 +02:00
5a34c3079f drop libconfig completely 2023-09-28 08:16:58 +02:00
e4177eeb27 disable deployment to ampere 2023-09-27 17:28:45 +02:00
c78c93db8a fix 2023-09-27 17:24:30 +02:00
4119765a87 deployment working so far 2023-09-27 16:46:58 +02:00
e5f9d3e3c8 docker hub, fix 2023-09-27 15:32:26 +02:00
6f2966800b docker hub 2023-09-27 15:28:18 +02:00
c0f1bddf29 more constants 2023-09-27 12:45:07 +02:00
f96aa7c798 more constants 2023-09-27 12:43:42 +02:00
314b9a421d use user nobody already in dockerfile 2023-09-27 12:41:44 +02:00
6cf3b46f76 use user nobody already in dockerfile 2023-09-27 12:39:18 +02:00
7cf68d5d55 use user nobody already in dockerfile 2023-09-27 12:38:54 +02:00
8817c3fa12 configuration only by env vars, fix 2023-09-27 12:35:18 +02:00
f90c1c2dbb configuration only by env vars, fix 2023-09-27 12:33:37 +02:00
c6fcce2d41 configuration only by env vars 2023-09-27 12:30:55 +02:00
828dffd523 change database configuration to env vars, fix 2023-09-27 12:02:37 +02:00
053927e329 change database configuration to env vars, fix 2023-09-27 12:01:15 +02:00
b555c9a37f change database configuration to env vars, fix 2023-09-27 11:56:46 +02:00
1adf5fa9d0 change database configuration to env vars, fix 2023-09-27 11:39:53 +02:00
73567d3230 change database configuration to env vars 2023-09-27 11:36:54 +02:00
8 changed files with 231 additions and 105 deletions

View File

@ -4,6 +4,7 @@ stages:
variables:
IMAGE_NAME: $CI_REGISTRY/$CI_PROJECT_PATH
HUB_IMAGE_NAME: wollud1969/sinkserver
build:
image: registry.hottis.de/dockerized/docker-bash:latest
@ -13,36 +14,42 @@ build:
- linux
- docker
script:
- docker build --tag $IMAGE_NAME:${CI_COMMIT_SHORT_SHA} .
- docker build --build-arg="VERSION=${CI_COMMIT_SHORT_SHA}-${CI_COMMIT_TAG}" --tag $IMAGE_NAME:${CI_COMMIT_SHORT_SHA} .
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY;
- docker push $IMAGE_NAME:${CI_COMMIT_SHORT_SHA}
- if [ "$CI_COMMIT_TAG" != "" ]; then
docker tag $IMAGE_NAME:${CI_COMMIT_SHORT_SHA} $IMAGE_NAME:${CI_COMMIT_TAG};
docker push $IMAGE_NAME:${CI_COMMIT_TAG};
fi
deploy:
image: registry.hottis.de/dockerized/docker-bash:latest
stage: deploy
tags:
- hottis
- linux
- docker
only:
- tags
variables:
GIT_STRATEGY: none
CONTAINER_NAME: sink
script:
- docker stop $CONTAINER_NAME || echo "container not running, never mind"
- docker rm $CONTAINER_NAME || echo "container not existing, never mind"
- docker run
-d
--network docker-server
--ip 172.16.10.42
-v sink_config:/etc/sink
--name $CONTAINER_NAME
--restart always
$IMAGE_NAME:$CI_COMMIT_TAG
environment:
name: production
- docker login -u $DOCKER_HUB_LOGIN -p $DOCKER_HUB_PASSWORD
- docker tag $IMAGE_NAME:${CI_COMMIT_SHORT_SHA} ${HUB_IMAGE_NAME}:${CI_COMMIT_SHORT_SHA}
- docker push ${HUB_IMAGE_NAME}:${CI_COMMIT_SHORT_SHA}
# deploy:
# image: registry.hottis.de/dockerized/docker-bash:latest
# stage: deploy
# tags:
# - hottis
# - linux
# - docker
# only:
# - tags
# variables:
# GIT_STRATEGY: none
# CONTAINER_NAME: sink
# script:
# - docker stop $CONTAINER_NAME || echo "container not running, never mind"
# - docker rm $CONTAINER_NAME || echo "container not existing, never mind"
# - docker run
# -d
# --network docker-server
# --ip 172.16.10.42
# -v sink_config:/etc/sink
# --name $CONTAINER_NAME
# --restart always
# $IMAGE_NAME:$CI_COMMIT_TAG
# environment:
# name: production

View File

@ -7,7 +7,6 @@ COPY sink/ /tmp/sink
RUN \
apk update && \
apk add alpine-sdk && \
apk add libconfig-dev && \
apk add postgresql-dev && \
cd /tmp/sink && \
make VERSION=${VERSION}
@ -15,18 +14,23 @@ RUN \
FROM alpine:3.18.3
ENV PGHOST=""
ENV PGDATABASE="mainscnt"
ENV PGUSER="sink"
ENV PGPASSWORD=""
ENV PGSSLMODE="require"
ENV LOWER_BOUND="44000"
ENV UPPER_BOUND="56000"
COPY --from=builder /tmp/sink/build/sink20169 /usr/local/bin/
RUN \
apk add --no-cache libpq && \
apk add --no-cache libconfig && \
mkdir /etc/sink
apk add --no-cache libpq
EXPOSE 20169/udp
USER nobody
VOLUME /etc/sink
CMD [ "/usr/local/bin/sink20169", "-f", "/etc/sink/sink20169.cfg", "-n", "nobody", "-v" ]
CMD [ "/usr/local/bin/sink20169", "-v", "-d" ]

141
deployment/deploy.yml Normal file
View File

@ -0,0 +1,141 @@
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
namespace: mainscnt
name: deny-all-but-dns
spec:
podSelector:
matchLabels: {}
policyTypes:
- Egress
- Ingress
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: sinkserver
namespace: mainscnt
labels:
app: sinkserver
spec:
replicas: 3
selector:
matchLabels:
app: sinkserver
template:
metadata:
labels:
app: sinkserver
spec:
containers:
- name: sinkserver
image: wollud1969/sinkserver:e5f9d3e3
ports:
- containerPort: 20169
protocol: UDP
env:
- name: PGHOST
valueFrom:
configMapKeyRef:
name: sinkserver-config
key: dbhost
- name: PGPASSWORD
valueFrom:
secretKeyRef:
name: sinkserver-secret
key: dbpass
---
apiVersion: v1
kind: Service
metadata:
name: sinkserver
namespace: mainscnt
labels:
app: sinkserver
spec:
type: LoadBalancer
selector:
app: sinkserver
ports:
- protocol: UDP
port: 20169
targetPort: 20169
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-database-sinkserver
namespace: database
spec:
podSelector:
matchLabels:
app: timescaledb
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: sinkserver
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: mainscnt
ports:
- protocol: TCP
port: 5432
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-sinkserver-database
namespace: mainscnt
spec:
podSelector:
matchLabels:
app: sinkserver
policyTypes:
- Egress
egress:
- to:
- podSelector:
matchLabels:
app: timescaledb
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: database
ports:
- protocol: TCP
port: 5432
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-sinkserver-ingress
namespace: mainscnt
spec:
podSelector:
matchLabels:
app: sinkserver
policyTypes:
- Ingress
ingress:
- from:
- ipBlock:
cidr: 0.0.0.0/0
ports:
- protocol: UDP
port: 20169

4
deployment/namespace.yml Normal file
View File

@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: mainscnt

30
schema/create.sql Normal file
View File

@ -0,0 +1,30 @@
create sequence device_s;
CREATE TABLE device_t (
id integer DEFAULT nextval('device_s') NOT NULL,
deviceid character varying(64) NOT NULL,
sharedsecret character varying(31) NOT NULL,
location character varying(128) NOT NULL,
active boolean DEFAULT false NOT NULL,
contact character varying(128),
flaky boolean DEFAULT false NOT NULL,
CONSTRAINT device_t_sharedsecret_check CHECK ((char_length((sharedsecret)::text) = 31))
);
ALTER TABLE ONLY device_t
ADD CONSTRAINT device_t_deviceid_key UNIQUE (deviceid);
ALTER TABLE ONLY device_t
ADD CONSTRAINT device_t_pkey PRIMARY KEY (id);
CREATE TABLE mainsfrequency (
"time" timestamp without time zone NOT NULL,
host text,
location text,
freq double precision,
valid smallint DEFAULT 1 NOT NULL
);
select create_hypertable('mainsfrequency', 'time');

View File

@ -9,10 +9,10 @@ VERSION ?= $(shell git rev-parse --short=8 HEAD)
UNAME_S := $(shell uname -s)
CC = gcc
CFLAGS = $(shell pkg-config --cflags libpq libconfig) -I. \
CFLAGS = $(shell pkg-config --cflags libpq) -I. \
-Wall -Werror -std=c99 \
-D$(UNAME_S)=1 -DVERSION="\"$(VERSION)\""
LDFLAGS = $(shell pkg-config --libs libpq libconfig)
LDFLAGS = $(shell pkg-config --libs libpq)
TARGET = sink20169
all: $(BUILD_DIR)/$(TARGET)

View File

@ -17,14 +17,12 @@
#include <string.h>
#include <getopt.h>
#include <pwd.h>
#include <libconfig.h>
#include <libpq-fe.h>
#include <sinkStruct.h>
#include <logging.h>
#include <sha256.h>
const char DEFAULT_CONFIG_FILENAME[] = "./sink20169.cfg";
typedef struct {
const char *deviceId;
@ -34,20 +32,12 @@ typedef struct {
PGresult *deviceResult;
} t_device;
typedef struct {
config_t cfg;
uint16_t numOfDevices;
t_device *devices;
} t_configHandle;
#define NUM_OF_STMT_PARAMS 4
typedef struct {
t_configHandle *configHandle;
int receiveSockFd;
int32_t lowerBound;
int32_t upperBound;
const char *postgresqlConnInfo;
PGconn *conn;
t_device foundDevice;
} t_commonHandle;
@ -61,7 +51,7 @@ int openDatabaseConnection(t_commonHandle *handle) {
if (! handle->conn) {
logmsg(LOG_DEBUG, "Opening connection to database");
handle->conn = PQconnectdb(handle->postgresqlConnInfo);
handle->conn = PQconnectdb("");
} else if (PQstatus(handle->conn) != CONNECTION_OK) {
logmsg(LOG_DEBUG, "Resetting connection to database");
PQreset(handle->conn);
@ -75,22 +65,6 @@ int openDatabaseConnection(t_commonHandle *handle) {
return res;
}
int initConfig(const char *configFilename, t_configHandle *configHandle) {
config_init(&(configHandle->cfg));
if (! config_read_file(&(configHandle->cfg), configFilename)) {
logmsg(LOG_ERR, "failed to read config file: %s:%d - %s\n",
config_error_file(&(configHandle->cfg)), config_error_line(&(configHandle->cfg)),
config_error_text(&(configHandle->cfg)));
config_destroy(&(configHandle->cfg));
return -1;
}
return 0;
}
void deinitConfig(t_configHandle *configHandle) {
config_destroy(&(configHandle->cfg));
}
// When you got a result here, remember to free it using freeDevice
int findDevice(t_commonHandle *handle, char *deviceId) {
@ -154,9 +128,7 @@ void freeDevice(t_commonHandle *handle) {
}
}
int initReceiver(t_configHandle *configHandle, t_commonHandle *handle) {
handle->configHandle = configHandle;
int initReceiver(t_commonHandle *handle) {
struct sockaddr_in servaddr;
handle->receiveSockFd = socket(AF_INET, SOCK_DGRAM, 0);
@ -166,11 +138,6 @@ int initReceiver(t_configHandle *configHandle, t_commonHandle *handle) {
}
int receivePort = 20169;
config_lookup_int(&(configHandle->cfg), "receivePort", &receivePort);
if (receivePort < 1 || receivePort > 65535) {
logmsg(LOG_ERR, "illegal receive port configured");
return -2;
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
@ -231,22 +198,13 @@ int receiveAndVerifyMinuteBuffer(t_commonHandle *handle, t_minuteBuffer *buf) {
}
int initForwarder(t_configHandle *configHandle, t_commonHandle *handle) {
handle->configHandle = configHandle;
handle->postgresqlConnInfo = NULL;
config_lookup_string(&(configHandle->cfg), "postgresqlConnInfo", &(handle->postgresqlConnInfo));
if (! handle->postgresqlConnInfo) {
logmsg(LOG_ERR, "no postgresql connInfo configured");
return -1;
}
int initForwarder(t_commonHandle *handle) {
handle->conn = NULL;
handle->lowerBound = 45000;
config_lookup_int(&(configHandle->cfg), "lowerBound", &(handle->lowerBound));
handle->upperBound = 55000;
config_lookup_int(&(configHandle->cfg), "upperBound", &(handle->upperBound));
char *lowerBoundStr = getenv("LOWER_BOUND");
handle->lowerBound = lowerBoundStr ? strtol(lowerBoundStr, NULL, 10) : 45000;
char *upperBoundStr = getenv("UPPER_BOUND");
handle->upperBound = upperBoundStr ? strtol(upperBoundStr, NULL, 10) : 55000;
logmsg(LOG_INFO, "lowerBound: %u, upperBound: %u", handle->lowerBound, handle->upperBound);
return 0;
@ -357,21 +315,16 @@ void usage() {
}
int main(int argc, char **argv) {
t_configHandle configHandle;
t_commonHandle commonHandle;
commonHandle.foundDevice.deviceResult = NULL;
const char *configFilename = DEFAULT_CONFIG_FILENAME;
const char *dropPrivilegesToUser = NULL;
bool doFork = false;
int c;
while ((c = getopt(argc, argv, "f:vds:hn:b")) != -1) {
while ((c = getopt(argc, argv, "vds:hn:b")) != -1) {
switch (c) {
case 'f':
configFilename = strdup(optarg);
break;
case 'v':
verbose = true;
break;
@ -410,11 +363,6 @@ int main(int argc, char **argv) {
logmsg(LOG_INFO, "Version: " VERSION);
if (0 != initConfig(configFilename, &configHandle)) {
logmsg(LOG_ERR, "error when reading configuration");
exit(3);
}
if (doFork) {
int pid = fork();
if (pid == -1) {
@ -427,12 +375,12 @@ int main(int argc, char **argv) {
}
}
if (0 != initReceiver(&configHandle, &commonHandle)) {
if (0 != initReceiver(&commonHandle)) {
logmsg(LOG_ERR, "error when initializing receiver");
exit(5);
}
if (0 != initForwarder(&configHandle, &commonHandle)) {
if (0 != initForwarder(&commonHandle)) {
logmsg(LOG_ERR, "error when initializing forwarder");
exit(6);
}
@ -458,5 +406,4 @@ int main(int argc, char **argv) {
deinitForwarder(&commonHandle);
deinitReceiver(&commonHandle);
deinitConfig(&configHandle);
}

View File

@ -1,7 +0,0 @@
postgresqlConnInfo = "host=172.16.3.32 dbname=mainscnt user=sink password=test123";
lowerBound = 45000;
upperBound = 55000;
receivePort = 20169;