Merge pull request 'Докеризация приложения' (#3) from docker into dev
Reviewed-on: http://git.hronos.su:8080/sabron/repcofkk/pulls/3
1
.dockerignore
Normal file
@ -0,0 +1 @@
|
||||
repcofkk/local_settings.py
|
||||
30
Dockerfile
@ -1,30 +0,0 @@
|
||||
FROM python:3.10
|
||||
|
||||
SHELL ["/bin/bash", "-c"]
|
||||
|
||||
# set environment variables
|
||||
ENV PYTHONDONTWRITEBYTECODE 1
|
||||
ENV PYTHONUNBUFFERED 1
|
||||
|
||||
RUN pip install --upgrade pip
|
||||
|
||||
RUN apt update && apt -qy install gcc libjpeg-dev libxslt-dev \
|
||||
libpq-dev libmariadb-dev libmariadb-dev-compat gettext cron openssh-client flake8 locales vim gunicorn
|
||||
|
||||
RUN useradd -rms /bin/bash cybercode && chmod 777 /opt /run
|
||||
|
||||
WORKDIR /cybercode
|
||||
|
||||
RUN mkdir /cybercode/static && mkdir /cybercode/media && chown -R cybercode:cybercode /cybercode && chmod 755 /cybercode
|
||||
|
||||
COPY --chown=cybercode:cybercode . .
|
||||
|
||||
RUN pip install -r req.txt
|
||||
|
||||
USER cybercode
|
||||
|
||||
COPY ./entrypoint.sh /
|
||||
ENTRYPOINT ["sh", "/entrypoint.sh"]
|
||||
|
||||
|
||||
#CMD ["gunicorn","-b","0.0.0.0:8000","repcofkk.wsgi:application"]
|
||||
19
Dockerfile__
@ -1,19 +0,0 @@
|
||||
FROM python:3.10
|
||||
|
||||
SHELL ["/bin/bash", "-c"]
|
||||
|
||||
# set environment variables
|
||||
ENV PYTHONDONTWRITEBYTECODE 1
|
||||
ENV PYTHONUNBUFFERED 1
|
||||
|
||||
RUN pip install --upgrade pip
|
||||
|
||||
RUN apt update && apt -qy install gcc libjpeg-dev libxslt-dev \
|
||||
libpq-dev libmariadb-dev libmariadb-dev-compat gettext cron openssh-client flake8 locales vim gunicorn
|
||||
|
||||
RUN useradd -rms /bin/bash cybercode && chmod 777 /opt /run
|
||||
|
||||
WORKDIR /cybercode
|
||||
|
||||
#COPY ./entrypoint.sh /
|
||||
#ENTRYPOINT ["sh", "/entrypoint.sh"]
|
||||
56
app/Dockerfile
Normal file
@ -0,0 +1,56 @@
|
||||
###########
|
||||
# BUILDER #
|
||||
###########
|
||||
|
||||
# pull official base image
|
||||
FROM python:3.10 as builder
|
||||
|
||||
# set work directory
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
# set environment variables
|
||||
ENV PYTHONDONTWRITEBYTECODE 1
|
||||
ENV PYTHONUNBUFFERED 1
|
||||
|
||||
# install system dependencies
|
||||
RUN apt-get update && \
|
||||
apt-get install -y --no-install-recommends gcc
|
||||
|
||||
# install python dependencies
|
||||
COPY ./requirements.txt .
|
||||
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt
|
||||
|
||||
|
||||
#########
|
||||
# FINAL #
|
||||
#########
|
||||
|
||||
# pull official base image
|
||||
FROM python:3.10
|
||||
|
||||
# create directory for the app user
|
||||
RUN mkdir -p /home/app
|
||||
|
||||
# create the app user
|
||||
RUN addgroup --system app && adduser --system --group app
|
||||
|
||||
# create the appropriate directories
|
||||
ENV HOME=/home/app
|
||||
ENV APP_HOME=/home/app/web
|
||||
RUN mkdir $APP_HOME
|
||||
RUN mkdir $APP_HOME/static
|
||||
WORKDIR $APP_HOME
|
||||
|
||||
# install dependencies
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends netcat-traditional
|
||||
COPY --from=builder /usr/src/app/wheels /wheels
|
||||
COPY --from=builder /usr/src/app/requirements.txt .
|
||||
RUN pip install --upgrade pip
|
||||
RUN pip install --no-cache /wheels/*
|
||||
|
||||
# copy project
|
||||
COPY . $APP_HOME
|
||||
|
||||
RUN chown -R app:app $APP_HOME
|
||||
|
||||
USER app
|
||||
@ -1,5 +1,4 @@
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from django.http import JsonResponse
|
||||
from django.utils import timezone
|
||||
|
||||
from apps.main.services.base_service import DashboardBaseService
|
||||
@ -2,7 +2,6 @@ from django.http import JsonResponse
|
||||
|
||||
from apps.main.models.worktime import FabricWorkTime
|
||||
from apps.main.services.worktime.service import FabricWorkTimeService
|
||||
from apps.main.tasks import create_worktime_data
|
||||
|
||||
from apps.util.base_view import base_view
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
import decimal
|
||||
import os
|
||||
import traceback
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
import psycopg2
|
||||
@ -8,6 +11,7 @@ from celery import shared_task
|
||||
|
||||
from django.db.models.functions import TruncMinute
|
||||
from django.utils import timezone
|
||||
from psycopg2 import OperationalError
|
||||
|
||||
from apps.main.models.scales_control import ScalesControl, Position
|
||||
from apps.main.models.worktime import FabricWorkTime
|
||||
@ -67,7 +71,8 @@ def create_scales_control_data():
|
||||
print(f"Task started at {datetime.now()}")
|
||||
scales_data = get_scales_data()
|
||||
print(f"Task: selected data at {datetime.now()}")
|
||||
write_scales_data(scales_data)
|
||||
res = write_scales_data(scales_data)
|
||||
|
||||
print(f"Task performed at {datetime.now()}")
|
||||
return 0
|
||||
|
||||
@ -77,12 +82,12 @@ def thin_out_scales():
|
||||
positions = Position.objects.all()
|
||||
for position in positions:
|
||||
print(position)
|
||||
thin_out_scales(position.aver_smena_tag)
|
||||
thin_out_scales(position.aver_pred_smena_tag)
|
||||
thin_out_scales(position.smena_tag)
|
||||
thin_out_scales(position.pred_smena_tag)
|
||||
thin_out_scales(position.month_tag)
|
||||
thin_out_scales(position.sutki_tag)
|
||||
thin_out_scale(position.aver_smena_tag)
|
||||
thin_out_scale(position.aver_pred_smena_tag)
|
||||
thin_out_scale(position.smena_tag)
|
||||
thin_out_scale(position.pred_smena_tag)
|
||||
thin_out_scale(position.month_tag)
|
||||
thin_out_scale(position.sutki_tag)
|
||||
|
||||
|
||||
@shared_task
|
||||
@ -95,8 +100,7 @@ def thin_out_worktime():
|
||||
prev = cur_obj
|
||||
|
||||
|
||||
def thin_out_scales(tag):
|
||||
print(tag)
|
||||
def thin_out_scale(tag):
|
||||
qs = list(ScalesControl.objects.filter(name=tag,
|
||||
time__lte=timezone.now() - timedelta(hours=12)).order_by('time'))
|
||||
|
||||
@ -122,42 +126,36 @@ def thin_out_scales(tag):
|
||||
i += 1
|
||||
|
||||
|
||||
def get_scales_data() -> list:
|
||||
conn_postgres = psycopg2.connect(
|
||||
dbname="vesi_db",
|
||||
user="ktp_user",
|
||||
password="rb,thrjl",
|
||||
host="192.168.10.2",
|
||||
port="5432"
|
||||
)
|
||||
|
||||
with conn_postgres.cursor() as cursor:
|
||||
cursor.execute("""
|
||||
SELECT "Name", "Values", "Date", "Check"
|
||||
FROM public.vesi
|
||||
ORDER BY id DESC
|
||||
LIMIT 3000;
|
||||
""")
|
||||
|
||||
data = cursor.fetchall()
|
||||
|
||||
conn_postgres.close()
|
||||
return data[::-1]
|
||||
def get_scales_data() -> bool | list:
|
||||
try:
|
||||
with psycopg2.connect(dbname="vesi_db", user="ktp_user", password="rb,thrjl", host="192.168.10.2",
|
||||
port="5432") as conn_postgres:
|
||||
with conn_postgres.cursor() as cursor:
|
||||
cursor.execute("""
|
||||
SELECT "Name", "Values", "Date", "Check"
|
||||
FROM public.vesi
|
||||
ORDER BY id DESC
|
||||
LIMIT 3000;
|
||||
""")
|
||||
data = cursor.fetchall()
|
||||
return data[::-1]
|
||||
except OperationalError:
|
||||
return False
|
||||
|
||||
|
||||
def write_scales_data(data: list):
|
||||
postgres_conn = psycopg2.connect(
|
||||
dbname="repcofkk_db",
|
||||
user="repcofkk_user",
|
||||
password="Angstrem*45",
|
||||
host="127.0.0.1",
|
||||
port="5432"
|
||||
)
|
||||
if not data:
|
||||
logging.error('No scales data')
|
||||
return False
|
||||
try:
|
||||
with postgres_conn.cursor() as cursor: # TODO при настройке ОПС на запись в БД - убрать первое условие
|
||||
for row in data:
|
||||
name, value, date, check = row
|
||||
cursor.execute("""
|
||||
with psycopg2.connect(dbname=os.environ.get('POSTGRES_DB', 'repcofkk_db'),
|
||||
user=os.environ.get('POSTGRES_USER', 'repcofkk_user'),
|
||||
password=os.environ.get('POSTGRES_PASSWORD', 'Angstrem*45'),
|
||||
host=os.environ.get('POSTGRES_HOST', '127.0.0.1'), port="5432") as postgres_conn:
|
||||
with postgres_conn.cursor() as cursor: # TODO при настройке ОПС на запись в БД - убрать первое условие
|
||||
for row in data:
|
||||
name, value, date, check = row
|
||||
cursor.execute("""
|
||||
DO $$
|
||||
DECLARE
|
||||
value_exists BOOLEAN;
|
||||
@ -168,7 +166,7 @@ def write_scales_data(data: list):
|
||||
FROM public.main_scalescontrol
|
||||
WHERE "Name" = %s AND "Values" = %s
|
||||
) INTO value_exists;
|
||||
|
||||
|
||||
IF value_exists THEN
|
||||
UPDATE public.main_scalescontrol
|
||||
SET "Date" = %s
|
||||
@ -180,14 +178,15 @@ def write_scales_data(data: list):
|
||||
END IF;
|
||||
END $$;
|
||||
""", (
|
||||
name, name,
|
||||
name, value,
|
||||
date, name, value,
|
||||
name, value, date, check
|
||||
))
|
||||
postgres_conn.commit()
|
||||
name, name,
|
||||
name, value,
|
||||
date, name, value,
|
||||
name, value, date, check
|
||||
))
|
||||
postgres_conn.commit()
|
||||
return True
|
||||
except Exception:
|
||||
logging.error('CELERY: ' + traceback.format_exc())
|
||||
print(traceback.format_exc())
|
||||
print(row)
|
||||
finally:
|
||||
postgres_conn.close()
|
||||
@ -69,4 +69,4 @@
|
||||
"sutki_tag": "SIEMENSPLC.AZG01.ReportProdDB.ProdPos22_3.Sutki"
|
||||
}
|
||||
}
|
||||
]
|
||||
]
|
||||
@ -85,8 +85,8 @@ LOGIN_REDIRECT_URL = '/'
|
||||
|
||||
logging.set()
|
||||
|
||||
REDIS_HOST = "localhost"
|
||||
REDIS_PORT = "6379"
|
||||
REDIS_HOST = os.getenv('REDIS_HOST')
|
||||
REDIS_PORT = os.getenv('REDIS_PORT')
|
||||
|
||||
CELERY_BROKER_URL = "redis://" + REDIS_HOST + ":" + REDIS_PORT + "/0"
|
||||
CELERY_BROKER_TRANSPORT_OPTIONS = {"visibility_timeout": 3600}
|
||||
@ -1,10 +1,10 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from django.contrib import admin
|
||||
|
||||
|
||||
from .models import Settings
|
||||
|
||||
|
||||
@admin.register(Settings)
|
||||
class SettingsAdmin(admin.ModelAdmin):
|
||||
list_display = ('name','value')
|
||||
|
||||
list_display = ('name', 'value')
|
||||
list_editable = ('value',)
|
||||
28
app/sabron/util/sabronmodule.py
Normal file
@ -0,0 +1,28 @@
|
||||
from django.db import ProgrammingError
|
||||
|
||||
from sabron.models import Settings
|
||||
|
||||
|
||||
def set_settings(name, value):
|
||||
settings_token = Settings.objects.filter(name=name).first()
|
||||
if settings_token is None:
|
||||
Settings.objects.create(
|
||||
name=name,
|
||||
value=value)
|
||||
else:
|
||||
settings_token.value = value
|
||||
settings_token.save()
|
||||
|
||||
|
||||
def get_settings(name: str) -> str:
|
||||
try:
|
||||
settings_token = Settings.objects.filter(name=name).first()
|
||||
if settings_token is None:
|
||||
Settings.objects.create(
|
||||
name=name,
|
||||
value='')
|
||||
return ''
|
||||
else:
|
||||
return settings_token.value
|
||||
except ProgrammingError:
|
||||
return ''
|
||||
34
app/settings.json
Normal file
@ -0,0 +1,34 @@
|
||||
[
|
||||
{
|
||||
"model": "sabron.settings",
|
||||
"pk": 1,
|
||||
"fields": {
|
||||
"name": "api_login",
|
||||
"value": "user_api"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "sabron.settings",
|
||||
"pk": 2,
|
||||
"fields": {
|
||||
"name": "api_password",
|
||||
"value": "qwerty*1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "sabron.settings",
|
||||
"pk": 3,
|
||||
"fields": {
|
||||
"name": "api_host",
|
||||
"value": "http://192.168.10.3"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "sabron.settings",
|
||||
"pk": 4,
|
||||
"fields": {
|
||||
"name": "control_conveyor",
|
||||
"value": "SIEMENSPLC.AZG01.ReportProdDB.Pos4AverSmena"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
|
Before Width: | Height: | Size: 331 B After Width: | Height: | Size: 331 B |
|
Before Width: | Height: | Size: 504 B After Width: | Height: | Size: 504 B |
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
|
Before Width: | Height: | Size: 380 B After Width: | Height: | Size: 380 B |
|
Before Width: | Height: | Size: 677 B After Width: | Height: | Size: 677 B |