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
        vault.hashicorp.com/agent-requests-cpu: "25m"
        vault.hashicorp.com/agent-limits-cpu: "50m"    
      labels:
        app: platform
    spec:
      # create database in azure within a sql pool
      initContainers: 
      - name: init-sqldatabase
        image: mcr.microsoft.com/mssql-tools
        command:
          - "/bin/sh"
          - "-c"
        args:
          - "/opt/mssql-tools/bin/sqlcmd -S $(VC_DB_HOST) -U $(VC_DB_USER)@$(VC_DB_HOST) -P '$(VC_DBSERVER_MASTER_PASSWORD)' -q \"CREATE LOGIN [$(VC_INSTANCE)_$(VC_NAMESPACE)_user] WITH PASSWORD = '$(VC_DB_PASSWORD)'\"
          && /opt/mssql-tools/bin/sqlcmd -S $(VC_DB_HOST) -U $(VC_DB_USER)@$(VC_DB_HOST) -P '$(VC_DBSERVER_MASTER_PASSWORD)' -q \"CREATE USER [$(VC_INSTANCE)_$(VC_NAMESPACE)_user] FROM LOGIN [$(VC_INSTANCE)_$(VC_NAMESPACE)_user]\"
          && /opt/mssql-tools/bin/sqlcmd -S $(VC_DB_HOST) -U $(VC_DB_USER)@$(VC_DB_HOST) -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: webstore-platform_source
          - 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
          - name: VC_DB_PASSWORD
            valueFrom:
              secretKeyRef:
                name: vc-db-pass
                key: password
      - name: init-platform
        image: docker.pkg.github.com/virtocommerce/vc-deploy-containers/platform-installer:v3
        command:
          - "/bin/sh"
          - "-c"
        args:
          - "echo START INIT $(date +%Y-%m-%d-%H-%M-%S)
          && python3 install-modules.py \"$MODULES_CONFIG\" /mnt/Modules/*"
        env:
          - name: MODULES_CONFIG
            valueFrom:
              configMapKeyRef:
                name: modules-cm
                key: modules.json 
        volumeMounts:
          - mountPath: /mnt/Modules
            name: modules-data
      - name: init-dbuser
        image: mcr.microsoft.com/mssql-tools
        command:
          - "/bin/sh"
          - "-c"
        args:
          - "/opt/mssql-tools/bin/sqlcmd -S $(VC_DB_HOST) -U $(VC_DB_USER)@$(VC_DB_HOST) -P '$(VC_DBSERVER_MASTER_PASSWORD)' -d $(VC_PLATFORM_SERVICE)_$(VC_NAMESPACE) -q \"CREATE USER [$(VC_INSTANCE)_$(VC_NAMESPACE)_user] FROM LOGIN [$(VC_INSTANCE)_$(VC_NAMESPACE)_user]; ALTER ROLE db_owner ADD MEMBER [$(VC_INSTANCE)_$(VC_NAMESPACE)_user];\""
        env:
          - name: VC_SAMPLE_DATA_DB
            value: webstore-platform_source  
          - name: VC_DBSERVER_MASTER_PASSWORD
            valueFrom:
              secretKeyRef:
                name: vc-dbserver-password
                key: password
          - name: VC_DB_PASSWORD
            valueFrom:
              secretKeyRef:
                name: vc-db-pass
                key: password
      containers:
      - name: vc-platform-web      
        image: docker.pkg.github.com/virtocommerce/vc-platform/platform:dev-linux-latest
        envFrom:
        - configMapRef:
            name: platform-cm
        env:
        - name: VC_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: vc-db-pass
              key: password
        - name: ConnectionStrings__VirtoCommerce
          value: "Server=tcp:$(VC_DB_HOST),1433;Database=$(VC_PLATFORM_SERVICE)_$(VC_NAMESPACE);User ID=$(VC_INSTANCE)_$(VC_NAMESPACE)_user@$(VC_DB_HOST);Password=$(VC_DB_PASSWORD);Trusted_Connection=False;Encrypt=True;"
        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: 1
      maxUnavailable: 0
    type: RollingUpdate