From 9333185e4dbe53b9ef3f20e2f3c1335f56c568af Mon Sep 17 00:00:00 2001 From: Vincent BRIDIER Date: Fri, 24 Jan 2025 15:32:29 +0100 Subject: [PATCH] feat: Clean and add CICD for K8S migration --- .dockerignore | 2 + .env.example | 1 - .gitlab-ci.yml | 29 ++- Dockerfile | 5 + README.md | 5 +- app/Console/Commands/LDCWebStatusChecker.php | 76 -------- config/app.php | 2 +- config/ldcweb.php | 5 - config/logging.php | 17 ++ config/mail.php | 4 +- k8s-queues.sh | 30 ++++ manifest.yaml | 175 +++++++++++++++++++ production-resources.yaml | 13 ++ webpack.mix.js | 8 +- 14 files changed, 270 insertions(+), 102 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile delete mode 100644 app/Console/Commands/LDCWebStatusChecker.php delete mode 100644 config/ldcweb.php create mode 100644 k8s-queues.sh create mode 100644 manifest.yaml create mode 100644 production-resources.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..fac5ce78 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +.git +*.md diff --git a/.env.example b/.env.example index 970a158d..e519da66 100644 --- a/.env.example +++ b/.env.example @@ -3,7 +3,6 @@ APP_ENV=local APP_KEY= APP_DEBUG=true APP_URL=http://localhost -APP_THEME=basic LOG_CHANNEL=stack LOG_LEVEL=debug diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8670d546..5093f3fe 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,12 +1,23 @@ include: - - project: 'ldc-web/shared-ci' - ref: 'main' - file: 'lint/lint7.3.yml' - - project: 'ldc-web/shared-ci' - ref: 'main' - file: 'security/security7.3.yml' - +- project: "ldc-web/shared-ci" + ref: "main" + file: "lint/lint7.3.yml" +- project: "ldc-web/shared-ci" + ref: "main" + file: "security/security7.3.yml" +- project: "ldc-web/shared-ci" + ref: "main" + file: "build/build7.3.yml" +- project: "ldc-web/shared-ci" + ref: "main" + file: "test/test.yml" +- project: "ldc-web/shared-ci" + ref: "main" + file: "deploy/deploy.yml" stages: - - lint - - security \ No newline at end of file +- lint +- security +- build +- test +- deploy diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..520028d3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM registry.s-web.fr/ldc-web/projets/ldc-web-images-docker/ldcweb-production-7.3:latest +COPY --chown=www-data:www-data . /app/srcs +USER root +RUN chmod -R 755 /app/srcs/storage +USER www-data diff --git a/README.md b/README.md index 631920cd..a47c1cb0 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,5 @@ -## Boutique Click N Collect +## Boutique Click N Collect Sablé -## Required systems - -* supervisor ## Installation diff --git a/app/Console/Commands/LDCWebStatusChecker.php b/app/Console/Commands/LDCWebStatusChecker.php deleted file mode 100644 index 07a08fa9..00000000 --- a/app/Console/Commands/LDCWebStatusChecker.php +++ /dev/null @@ -1,76 +0,0 @@ - 0) { - foreach ($files as $file) { - $data['logs'] = $data['logs'] + filesize($file); - } - } - - $data['code']['version'] = app()->version(); - - // Send by cURL. - $url = 'https://ldc-web.ldc.fr/api/status-checker'; - $ch = curl_init($url); - $payload = ['token' => 'CE8S52Qq7stf5k8frF4T', 'datas' => json_encode($data)]; - curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - $result = curl_exec($ch); - curl_close($ch); - } -} diff --git a/config/app.php b/config/app.php index f9fb03fc..caa32c32 100644 --- a/config/app.php +++ b/config/app.php @@ -24,7 +24,7 @@ return [ | */ - 'theme' => env('APP_THEME', 'basic'), + 'theme' => 'sable', /* |-------------------------------------------------------------------------- diff --git a/config/ldcweb.php b/config/ldcweb.php deleted file mode 100644 index 7c62d716..00000000 --- a/config/ldcweb.php +++ /dev/null @@ -1,5 +0,0 @@ - env('PROJECT_UUID') -]; diff --git a/config/logging.php b/config/logging.php index 0c87551c..b8c17735 100644 --- a/config/logging.php +++ b/config/logging.php @@ -112,6 +112,23 @@ return [ 'emergency' => [ 'path' => storage_path('logs/laravel.log'), ], + + 'stdout' => [ + 'driver' => 'monolog', + 'level' => env('LOG_LEVEL', 'debug'), + 'handler' => StreamHandler::class, + 'formatter' => env('LOG_STDOUT_FORMATTER'), + 'with' => [ + 'stream' => 'php://stdout', + ], + ], + + 'k8s' => [ + 'driver' => 'stack', + 'channels' => ['daily', 'stdout'], + 'ignore_exceptions' => false, + ], + ], ]; diff --git a/config/mail.php b/config/mail.php index 8fffd3d4..1032a96b 100644 --- a/config/mail.php +++ b/config/mail.php @@ -93,10 +93,10 @@ return [ ], 'contactsList' => [ - 'serviceweb' => explode(",", env('MAIL_LIST_SERVICEWEB', 'informatique.internet@ldc.fr')), + 'serviceweb' => explode(",", env('MAIL_LIST_SERVICEWEB', 'digital@ldc.fr')), 'invalidInco' => explode( ",", - env('MAIL_LIST_INVALIDINCO', 'informatique.internet@ldc.fr,vincent.pierre@ldc.fr,christelle.manceau@ldc.fr') + env('MAIL_LIST_INVALIDINCO', 'digital@ldc.fr,lise.de-wever@ldc.fr,christelle.manceau@ldc.fr') ), ], diff --git a/k8s-queues.sh b/k8s-queues.sh new file mode 100644 index 00000000..228c9bde --- /dev/null +++ b/k8s-queues.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +# From https://stackoverflow.com/a/76488746/17320077 + +work=true + +function stopQueues() { + echo "Stopping queues..." + work=false + php artisan queue:restart + sleep 45s + exit 0 +} + +# handle graceful shutdown +trap "" SIGPIPE +trap stopQueues EXIT + +echo "Starting PHP queue workers..." + +while $work; do + php artisan queue:work database --sleep=3 --tries=3 --max-time=3600 --no-interaction + sleep 1 +done + +# Si besoin de plusieurs threads puis terminer par un done sans & +#(while $work; do +# php artisan queue:work --queue="$(grep -oP 'SQS_QUEUE=\K.*' .env)" --no-interaction +# sleep 1 +#done &) diff --git a/manifest.yaml b/manifest.yaml new file mode 100644 index 00000000..e1f59a03 --- /dev/null +++ b/manifest.yaml @@ -0,0 +1,175 @@ +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: ${CI_PROJECT_PATH_SLUG}-config-map +data: + APP_NAME: "${APP_APP_NAME}" + APP_ENV: "${APP_APP_ENV}" + APP_KEY: "${APP_APP_KEY}" + APP_DEBUG: "${APP_APP_DEBUG}" + APP_URL: "https://${REDIRECT}${APP_DOMAIN}" + LOG_CHANNEL: "k8s" + DB_HOST: "mariadb.common-services" + DB_DATABASE: "${APP_DB_DATABASE}" + DB_USERNAME: "${APP_DB_USERNAME}" + DB_PASSWORD: "${APP_DB_PASSWORD}" + MAIL_HOST: "${APP_MAIL_HOST}" + MAIL_PORT: "${APP_MAIL_PORT}" + MAIL_USERNAME: "${APP_MAIL_USERNAME}" + MAIL_PASSWORD: "${APP_MAIL_PASSWORD}" + MAIL_ENCRYPTION: "${APP_MAIL_ENCRYPTION}" + MAIL_FROM_ADDRESS: "${APP_MAIL_FROM_ADDRESS}" + MAIL_FROM_NAME: "${APP_APP_NAME}" + CRM_ENDPOINT: "${APP_CRM_ENDPOINT}" + CRM_TOKEN: "${APP_CRM_TOKEN}" + CRM_PASSWORD: "${APP_CRM_PASSWORD}" + SENTRY_LARAVEL_DSN: "${APP_SENTRY_LARAVEL_DSN}" + MATOMO_SITE_ID: "${APP_MATOMO_SITE_ID}" +--- +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: ${CI_PROJECT_PATH_SLUG}-pvc-storage +spec: + storageClassName: "csi-cinder-classic" + accessModes: + - ReadWriteOnce + resources: + requests: + storage: $APP_PVC_SIZE +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: ${CI_PROJECT_PATH_SLUG}-deployment +spec: + replicas: 1 + selector: + matchLabels: + app: ${CI_PROJECT_PATH_SLUG}-app + template: + metadata: + labels: + app: ${CI_PROJECT_PATH_SLUG}-app + spec: + containers: + - name: ${CI_PROJECT_PATH_SLUG}-container + image: $IMAGE_TAG + envFrom: + - configMapRef: + name: ${CI_PROJECT_PATH_SLUG}-config-map + volumeMounts: + - name: storage + mountPath: /app/srcs/storage + - name: ${CI_PROJECT_PATH_SLUG}-queue-worker + image: $IMAGE_TAG + lifecycle: + preStop: + exec: + command: [ "/bin/sh", "-c", "kill -s TERM 1; while test $(ps ax | grep -v grep | grep bash | wc -l) -gt 0; do sleep 2s; done" ] + resources: + limits: + memory: 512Mi + cpu: "1" + command: + - "bash" + args: + - "k8s-queues.sh" + envFrom: + - configMapRef: + name: ${CI_PROJECT_PATH_SLUG}-config-map + volumeMounts: + - name: storage + mountPath: /app/srcs/storage + imagePullSecrets: + - name: docker-registry-credentials + securityContext: + runAsUser: 33 + runAsGroup: 33 + fsGroup: 33 + fsGroupChangePolicy: "OnRootMismatch" + volumes: + - name: storage + persistentVolumeClaim: + claimName: ${CI_PROJECT_PATH_SLUG}-pvc-storage +--- +apiVersion: v1 +kind: Service +metadata: + name: ${CI_PROJECT_PATH_SLUG}-service +spec: + selector: + app: ${CI_PROJECT_PATH_SLUG}-app + ports: + - port: 80 + targetPort: 443 +--- +apiVersion: traefik.io/v1alpha1 +kind: IngressRoute +metadata: + name: ${CI_PROJECT_PATH_SLUG}-ingress-route +spec: + entryPoints: + - web + - websecure + routes: + - match: Host(`${APP_DOMAIN}`) || Host(`www.${APP_DOMAIN}`) + kind: Rule + services: + - name: ${CI_PROJECT_PATH_SLUG}-service + port: 80 + middlewares: + - name: redirect-no-www-to-www + tls: + secretName: ${CRT_TLS_NAME} +--- +apiVersion: batch/v1 +kind: CronJob +metadata: + name: ${CI_PROJECT_PATH_SLUG}-cronjob +spec: + schedule: "* * * * *" + concurrencyPolicy: Forbid + jobTemplate: + spec: + template: + spec: + affinity: + podAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + - labelSelector: + matchExpressions: + - key: app + operator: In + values: + - ${CI_PROJECT_PATH_SLUG}-app + topologyKey: "kubernetes.io/hostname" + namespaces: [ $KUBE_NAMESPACE ] + containers: + - name: ${CI_PROJECT_PATH_SLUG}-scheduled + image: $IMAGE_TAG + imagePullPolicy: IfNotPresent + command: + - "php" + args: + - "artisan" + - "schedule:run" + envFrom: + - configMapRef: + name: ${CI_PROJECT_PATH_SLUG}-config-map + volumeMounts: + - name: storage + mountPath: /app/srcs/storage + imagePullSecrets: + - name: docker-registry-credentials + volumes: + - name: storage + persistentVolumeClaim: + claimName: ${CI_PROJECT_PATH_SLUG}-pvc-storage + securityContext: + runAsUser: 33 + runAsGroup: 33 + fsGroup: 33 + fsGroupChangePolicy: "OnRootMismatch" + restartPolicy: OnFailure diff --git a/production-resources.yaml b/production-resources.yaml new file mode 100644 index 00000000..6de4b3de --- /dev/null +++ b/production-resources.yaml @@ -0,0 +1,13 @@ +--- +apiVersion: cert-manager.io/v1 +kind: Certificate +metadata: + name: $APP_DOMAIN +spec: + dnsNames: + - $APP_DOMAIN + - www.${APP_DOMAIN} + secretName: $APP_DOMAIN + issuerRef: + name: $APP_CERT_ISSUER + kind: ClusterIssuer diff --git a/webpack.mix.js b/webpack.mix.js index 14d5eff1..e08e03fd 100644 --- a/webpack.mix.js +++ b/webpack.mix.js @@ -28,13 +28,13 @@ mix.js("resources/js/app.js", "public/b-o/js") ); //add front theme mix -mix.js('resources/views/front/' + process.env.APP_THEME + '/js/app.js', 'public/' + process.env.APP_THEME + '/js') - .sass('resources/views/front/' + process.env.APP_THEME + '/sass/app.scss', 'public/' + process.env.APP_THEME + '/css') +mix.js('resources/views/front/sable/js/app.js', 'public/sable/js') + .sass('resources/views/front/sable/sass/app.scss', 'public/sable/css') .options({ processCssUrls: false }) .version(); -mix.copyDirectory('resources/views/front/' + process.env.APP_THEME + '/img', 'public/' + process.env.APP_THEME + '/img/') - .copyDirectory('resources/views/front/' + process.env.APP_THEME + '/fonts', 'public/' + process.env.APP_THEME + '/fonts/'); +mix.copyDirectory('resources/views/front/sable/img', 'public/sable/img/') + .copyDirectory('resources/views/front/sable/fonts', 'public/sable/fonts/');