apiVersion: apps/v1
kind: Deployment
metadata:
  name: platform
spec:
  revisionHistoryLimit: 1
  replicas: 1
  selector:
    matchLabels:
      app: platform
  template:
    metadata:
      annotations:
        reloader.stakater.com/search: "true"
        configmap.reloader.stakater.com/reload: "platform-cm,deployment-cm,platform-secret-configmap"
        vault.hashicorp.com/agent-inject: "true"
        vault.hashicorp.com/agent-pre-populate-only: "true"
        vault.hashicorp.com/agent-configmap: $(VC_INSTANCE)-platform-secret-configmap   
      labels:
        app: platform
    spec:
      # create database in azure within a sql pool
      initContainers:
      - name: init-platform
        image: docker.pkg.github.com/virtocommerce/vc-deploy-containers/platform-installer:v2
        command:
          - "/bin/sh"
          - "-c"
        args:
          - "echo START INIT $(date +%Y-%m-%d-%H-%M-%S)
          && python3 install-modules.py \"$MODULES_CONFIG\" /mnt/Modules/*
          && if [ ! -d \"$ELECTRONICS_THEME_LOCATION\" ]; then mkdir -p \"$ELECTRONICS_THEME_LOCATION\"; fi
          && python3 install-theme.py $(ELECTRONICS_THEME_URL) $(ELECTRONICS_THEME_LOCATION)
          && echo ELECTRONICS THEME INSTALLED $(date +%Y-%m-%d-%H-%M-%S)
          && if [ ! -d \"$B2B_THEME_LOCATION\" ]; then mkdir -p \"$B2B_THEME_LOCATION\"; fi
          && python3 install-theme.py $(B2B_THEME_URL) $(B2B_THEME_LOCATION)
          && echo B2B THEME INSTALLED $(date +%Y-%m-%d-%H-%M-%S)
          && python3 upload-assets.py $(ASSETS_URL) $(ASSETS_LOCATION)
          && echo ASSETS INSTALLED $(date +%Y-%m-%d-%H-%M-%S)"
        envFrom:
          - configMapRef:
              name: deployment-cm
        env:
          - name: MODULES_CONFIG
            valueFrom:
              configMapKeyRef:
                name: deployment-cm
                key: modules.json 
        volumeMounts:
          - mountPath: /mnt/Modules
            name: modules-data
          - mountPath: /mnt/cms-content
            name: cms-content-data 
      - name: init-sqldatabase
        image: mcr.microsoft.com/mssql-tools
        command:
          - "/opt/mssql-tools/bin/sqlcmd"
          - "-S"
          - "$(VC_DB_HOST)"
          - "-U"
          - "$(VC_DB_USER)@$(VC_DB)"
          - "-P"
          - "$(VC_DBSERVER_MASTER_PASSWORD)"
          - "-q"
          - "CREATE DATABASE [$(VC_PLATFORM_SERVICE)_$(VC_NAMESPACE)] $(VC_DB_COPY_PARAM) ( SERVICE_OBJECTIVE = ELASTIC_POOL ( name = [$(VC_DB_POOL)] ) )"
        env:
          - name: VC_SAMPLE_DATA_DB
            value: vc-empty-db
          - name: VC_DB_COPY_PARAM
            value: "AS COPY OF [$(VC_SAMPLE_DATA_DB)]"  
          - name: VC_DBSERVER_MASTER_PASSWORD
            valueFrom:
              secretKeyRef:
                name: vc-dbserver-password
                key: password 
      containers:
      - name: vc-platform-web      
        image: docker.pkg.github.com/virtocommerce/vc-platform/platform:dev-linux-latest
        envFrom:
        - configMapRef:
            name: platform-cm
        command:
            - "/bin/bash"
            - "-c"
        args:
            - "source /vault/secrets/config_base && source /vault/secrets/config_custom && dotnet VirtoCommerce.Platform.Web.dll"
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: "/opt/virtocommerce/platform/wwwroot/cms-content"
          name: cms-content-data
        - mountPath: "/opt/virtocommerce/platform/modules"
          name: modules-data
      volumes:
      - name: cms-content-data
        persistentVolumeClaim:
          claimName: cms-content-volume
      - name: modules-data
        emptyDir: {}     
  strategy:
    rollingUpdate:
      maxSurge: 0
      maxUnavailable: 1
    type: RollingUpdate