apiVersion: workspace.devfile.io/v1alpha2
kind: DevWorkspaceTemplate
metadata:
  name: che-theia-nodejs-web-app
spec:
  commands:
    - id: init-container-command
      apply:
        component: remote-runtime-injector
  events:
    preStart:
      - init-container-command
  components:
    - name: theia-ide
      container:
        image: registry.redhat.io/devspaces/theia-rhel8:3.5
        env:
          - name: THEIA_PLUGINS
            value: local-dir:///plugins
          - name: HOSTED_PLUGIN_HOSTNAME
            value: 0.0.0.0
          - name: HOSTED_PLUGIN_PORT
            value: '3130'
          - name: THEIA_HOST
            value: 127.0.0.1
        volumeMounts:
          - name: plugins
            path: /plugins
          - name: theia-local
            path: /home/theia/.theia
        mountSources: true
        memoryLimit: 512M
        cpuLimit: 1500m
        cpuRequest: 100m
        endpoints:
          - name: theia
            attributes:
              type: main
              cookiesAuthEnabled: true
              discoverable: false
              urlRewriteSupported: true
            targetPort: 3100
            exposure: public
            secure: false
            protocol: https
          - name: webviews
            attributes:
              type: webview
              cookiesAuthEnabled: true
              discoverable: false
              unique: true
              urlRewriteSupported: true
            targetPort: 3100
            exposure: public
            secure: false
            protocol: https
          - name: mini-browser
            attributes:
              type: mini-browser
              cookiesAuthEnabled: true
              discoverable: false
              unique: true
              urlRewriteSupported: true
            targetPort: 3100
            exposure: public
            secure: false
            protocol: https
          - name: theia-dev
            attributes:
              type: ide-dev
              discoverable: false
              urlRewriteSupported: true
            targetPort: 3130
            exposure: public
            protocol: http
          - name: theia-redirect-1
            attributes:
              discoverable: false
              urlRewriteSupported: false
            targetPort: 13131
            exposure: public
            protocol: http
          - name: theia-redirect-2
            attributes:
              discoverable: false
              urlRewriteSupported: false
            targetPort: 13132
            exposure: public
            protocol: http
          - name: theia-redirect-3
            attributes:
              discoverable: false
              urlRewriteSupported: false
            targetPort: 13133
            exposure: public
            protocol: http
          - name: terminal
            attributes:
              type: collocated-terminal
              discoverable: false
              cookiesAuthEnabled: true
              urlRewriteSupported: true
            targetPort: 3333
            exposure: public
            secure: false
            protocol: wss
      attributes: {}
    - name: plugins
      volume: {}
    - name: theia-local
      volume: {}
    - name: theia-ide-contributions
      container:
        image: registry.redhat.io/devspaces/udi-rhel8:3.5
        args:
          - sh
          - '-c'
          - ${PLUGIN_REMOTE_ENDPOINT_EXECUTABLE}
        env:
          - name: PLUGIN_REMOTE_ENDPOINT_EXECUTABLE
            value: /remote-endpoint/plugin-remote-endpoint
          - name: THEIA_PLUGINS
            value: local-dir:///plugins/sidecars/tools
        volumeMounts:
          - name: plugins
            path: /plugins
          - name: remote-endpoint
            path: /remote-endpoint
        memoryLimit: 512Mi
      attributes:
        controller.devfile.io/container-contribution: true
    - name: remote-endpoint
      volume:
        ephemeral: true
    - name: che-machine-exec
      container:
        image: registry.redhat.io/devspaces/machineexec-rhel8:3.5
        command:
          - /go/bin/che-machine-exec
          - '--url'
          - 127.0.0.1:3333
        memoryLimit: 128Mi
        memoryRequest: 32Mi
        cpuLimit: 500m
        cpuRequest: 30m
      attributes: {}
    - name: remote-runtime-injector
      container:
        image: registry.redhat.io/devspaces/theia-endpoint-rhel8:3.5
        env:
          - name: PLUGIN_REMOTE_ENDPOINT_EXECUTABLE
            value: /remote-endpoint/plugin-remote-endpoint
          - name: REMOTE_ENDPOINT_VOLUME_NAME
            value: remote-endpoint
        volumeMounts:
          - name: plugins
            path: /plugins
          - name: remote-endpoint
            path: /remote-endpoint
        memoryLimit: 128Mi
        memoryRequest: 32Mi
        cpuLimit: 500m
        cpuRequest: 30m
---
apiVersion: workspace.devfile.io/v1alpha2
kind: DevWorkspace
metadata:
  name: nodejs-web-app
spec:
  started: true
  template:
    components:
      - name: tools
        container:
          image: registry.redhat.io/devspaces/udi-rhel8:3.5
          endpoints:
            - exposure: public
              name: nodejs
              protocol: http
              targetPort: 3000
          memoryLimit: 1G
          mountSources: true
          volumeMounts:
            - name: npm
              path: /home/user/.npm
        attributes:
          controller.devfile.io/merge-contribution: true
      - name: npm
        volume:
          size: 1G
    commands:
      - id: 1-dependencies
        exec:
          label: Download dependencies
          component: tools
          workingDir: ${PROJECTS_ROOT}/web-nodejs-sample/app
          commandLine: npm install
          group:
            kind: build
      - id: 2-run
        exec:
          label: Run the web app
          component: tools
          workingDir: ${PROJECTS_ROOT}/web-nodejs-sample/app
          commandLine: nodemon app.js
          group:
            kind: run
      - id: 3-install-and-run
        exec:
          label: Run the web app (and download dependencies)
          component: tools
          workingDir: ${PROJECTS_ROOT}/web-nodejs-sample/app
          commandLine: npm install; nodemon app.js
          group:
            kind: run
      - id: 4-debug
        exec:
          label: Run the web app (debugging enabled)
          component: tools
          workingDir: ${PROJECTS_ROOT}/web-nodejs-sample/app
          commandLine: nodemon --inspect app.js
          group:
            kind: debug
            isDefault: true
      - id: 5-stopapp
        exec:
          label: Stop the web app
          component: tools
          commandLine: >-
            node_server_pids=$(pgrep -fx '.*nodemon (--inspect )?app.js' | tr
            "\\n" " ") && echo "Stopping node server with PIDs:
            ${node_server_pids}" &&  kill -15 ${node_server_pids} &>/dev/null &&
            echo 'Done.'
          group:
            kind: run
    projects:
      - name: web-nodejs-sample
        zip:
          location: '{{ INTERNAL_URL }}/resources/v2/web-nodejs-sample.zip'
  contributions:
    - name: editor
      kubernetes:
        name: che-theia-nodejs-web-app
