saleor

by rodolfojcj

Helm chartv0.1.1App 3.1.0-alphaApache-2.0

Saleor is a modular, high performance, headless e-commerce platform built with Python, GraphQL, Django, and React.

About this chart

Saleor Helm Chart

This directory contains a Helm chart to deploy Saleor on a Kubernetes cluster.

Requirements

  • A working Kubernetes cluster, to which you have access to install this chart.
  • A working Helm instance to install this chart.
  • To persist the PostgreSQL data, a persistent volume. Possible exceptions to this would be either a configured external database or a testing environment where you only want to explore how things work, in which case the database data is considered ephemeral.
  • To persist the Saleor media data, like products images, a persistent volume. A possible exception to this would be a testing environment to explore how things work, in which case the media data is considered ephemeral.
  • A working Docker registry, possibly a private one with the corresponding authentication credentials, from which the Kubernetes cluster will download the images of the Saleor main components.

Components

  • Reused via existing Helm charts that provide the needed Docker compatible images:
    • Optionally, a PostgreSQL database instance. Alternatively, an external database can be configured.
    • An standalone Redis instance.
    • A Jaeger all-in-one instance.
  • Saleor main components, which are not publicly available, then we need to somehow provide them ourselves for our usage:
    • API backend. Disabled by default, however it is supposed that you will enable it.
    • Dashboard UI. Disabled by default, however it is supposed that you will enable it.
    • Deprecated Storefront UI. Disabled by default, but you may enable it. Given that is deprecated, using it is not recommended.
    • New React Storefront UI. Disabled by default, however it is supposed that you will enable it.

Configuration

The available settings are specified in the values file of this chart.

To override them, follow the Helm values documentation. As an example of this, let's consider the following contents that can be passed to the helm binary:

# save this file as my-saleor-chart-values.yaml
postgresql:
  postgresqlPassword: VeryDifficultDatabasePassword
  persistence:
    enabled: true


saleorApi:
  enabled: true
  image:
    registry: mydockerregistry.example.com
    pullSecrets:
      - myprivateregistry-basic-auth-secret
    tag: 3.1.0-a.24
  ingress:
    enabled: true
    className: nginx
    annotations:
      cert-manager.io/issuer: letsencrypt-issuer
    hostname: mysaleorapi.example.com
    pathType: Prefix
    tls: false
  allowedHosts: mysaleorapi.example.com
  allowedClientHosts: mysaleordashboard.example.com,mysaleornewstorefront.example.com,mysaleordeprecatedstorefront.example.com
  populateDbWithSampleData: true
  url: 'http://mysaleorapi.example.com'


saleorDashboard:
  enabled: true
  image:
    registry: mydockerregistry.example.com
    pullSecrets:
      - myprivateregistry-basic-auth-secret
    tag: 3.1.0-a.17
  ingress:
    enabled: true
    className: nginx
    annotations:
      cert-manager.io/issuer: letsencrypt-issuer
    hostname: mysaleordashboard.example.com
    pathType: Prefix
    tls: false
  url: 'http://mysaleordashboard.example.com'


saleorDeprecatedStorefront:
  enabled: false
  image:
    registry: mydockerregistry.example.com
    pullSecrets:
      - myprivateregistry-basic-auth-secret
    tag: 3.0.0-b.2
  ingress:
    enabled: true
    className: nginx
    annotations:
      cert-manager.io/issuer: letsencrypt-issuer
    hostname: mysaleordeprecatedstorefront.example.com
    pathType: Prefix
    tls: false
  url: 'http://mysaleordeprecatedstorefront.example.com'


saleorReactStorefront:
  enabled: true
  image:
    registry: mydockerregistry.example.com
    pullSecrets:
      - myprivateregistry-basic-auth-secret
    tag: main
    pullPolicy: Always
  ingress:
    enabled: true
    className: nginx
    annotations:
      cert-manager.io/issuer: letsencrypt-issuer
    hostname: mysaleornewstorefront.example.com
    pathType: Prefix
    tls: false
  url: 'http://mysaleornewstorefront.example.com'
  hostAliases:
    - ip: "1.2.3.4"
      hostnames:
      - "mysaleorapi.example.com"


persistence:
  mediaData:
    enabled: true
    existingClaim: my-saleor-media-pvc


podAffinities:
  saleorWorker:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        - labelSelector:
            matchExpressions:
              - key: attached-to-media-data-volume
                operator: In
                values:
                  - "true"
          topologyKey: "kubernetes.io/hostname"

Installation

Using the helm binary, any of these commands may be executed to install this saleor chart in your cluster:

helm install my-saleor-helm-release saleor
helm install my-saleor-helm-release saleor --values ./my-saleor-chart-values.yaml
helm install my-saleor-helm-release saleor --namespace my-namespace --values ./my-saleor-chart-values.yaml

How to build and get the Docker compatible images for the Saleor components?

Given that Docker images for the Saleor main components (API, Dashboard and Frontends) are not officially published, you may need to build them by yourself and then publish them to a Docker registry to which you have read and write access.

In case you already have a Docker registry from which you will download the needed images, then skip this section.

In the commands given bellow, the podman binary is used, however you may use the traditional docker binary instead of it, adjusting the commands syntax as needed.

To run some of the listed commands, superuser privileges may be needed.

Saleor API

# SALEOR_API_VERSION environment variable will point to a valid tag or
# commit ID in the saleor/saleor repository
export SALEOR_API_VERSION=3.1.0-a.15

# this step probably needs superuser privileges
echo 'unqualified-search-registries = ["docker.io"]' > /etc/containers/registries.conf

# the git binary needs to be previously installed
git clone git://github.com/saleor/saleor
cd saleor
git checkout $SALEOR_API_VERSION

echo "Will build the Saleor API image."
podman build --build-arg STATIC_URL=/static/ --tag saleor:$SALEOR_API_VERSION --file Dockerfile

echo "Will push the Saleor API image to our public or private Docker registry site."
podman login --username myregistryusername --password myregistrysuperhardpassword mydockerregistry.example.com
podman push saleor:$SALEOR_API_VERSION mydockerregistry.example.com/saleor/saleor:$SALEOR_API_VERSION

echo "Just to verify, we will search that Saleor API image in our public or private Docker registry site."
podman search --log-level debug --list-tags mydockerregistry.example.com/saleor/saleor

Saleor Dashboard

# SALEOR_DASHBOARD_VERSION environment variable will point to a valid tag or
# commit ID in the saleor/saleor-dashboard repository
export SALEOR_DASHBOARD_VERSION=3.1.0-a.13

# this step probably needs superuser privileges
echo 'unqualified-search-registries = ["docker.io"]' > /etc/containers/registries.conf

# the git binary needs to be previously installed
git clone git://github.com/saleor/saleor-dashboard
cd saleor-dashboard
git checkout $SALEOR_DASHBOARD_VERSION

echo "Will build the Saleor Dashboard image."
podman build --build-arg API_URI=http://mysaleorapi.example.com/graphql/ --tag saleor-dashboard:$SALEOR_DASHBOARD_VERSION --file Dockerfile

echo "Will push the Saleor Dashboard image to our public or private Docker registry site."
podman login --username myregistryusername --password myregistrysuperhardpassword mydockerregistry.example.com
podman push saleor-dashboard:$SALEOR_DASHBOARD_VERSION mydockerregistry.example.com/saleor/dashboard:$SALEOR_DASHBOARD_VERSION

echo "Just to verify, we will search that Saleor Dashboard image in our public or private Docker registry site."
podman search --log-level debug --list-tags mydockerregistry.example.com/saleor/dashboard

Saleor Deprecated Storefront

#
# NOTES
# =====
# - Saleor API backend must be up and running, and the frontend builder machine
# must have connectivity with it.
# - Running Saleor API backend should not have its sample data loaded while the
# frontend image is built. This is to avoid NPM errors saying something like
# UnhandledPromiseRejectionWarning because of already existing data, which
# possibly happens while some kind of NPM test stage is performed as part of
# the NPM export stage.
#

# SALEOR_DEPRECATED_STOREFRONT_VERSION environment variable will point to a
# valid tag or commit ID in the saleor/saleor-storefront repository
export SALEOR_DEPRECATED_STOREFRONT_VERSION=3.0.0-b.2

# this step probably needs superuser privileges
echo 'unqualified-search-registries = ["docker.io"]' > /etc/containers/registries.conf

# if needed DNS records are not configured to resolve the Saleor API hostname, we
# can use the /etc/hosts file of the local machine to solve this part by commenting
# out the following line and adjusting the IP address and hostname
# echo '1.2.3.4  mysaleorapi.example.com' >> /etc/hosts

# the git binary needs to be previously installed
git clone git://github.com/saleor/saleor-storefront
cd saleor-storefront
git checkout $SALEOR_DEPRECATED_STOREFRONT_VERSION

# seems that NEXT_PUBLIC_API_URI replaced API_URI?
# If yes, then API_URI is no longer needed?
# See https://github.com/saleor/saleor-storefront#prerequisites which says
# """To run the storefront, you have to set the NEXT_PUBLIC_API_URI environment variable to point to the Saleor GraphQL API."""
# """To change it, either create a .env.local file and add it there or set an env variable using export command."""
echo "NEXT_PUBLIC_API_URI=http://mysaleorapi.example.com/graphql/" >> .env.local
## echo "NEXT_PUBLIC_DEMO_MODE=true" >> .env.local

# if the Saleor API backend is not up and running, it fails with an error that
# happens in this step of the Dockerfile:
#     npm run build:export
# then we need to have it running before trying to build the deprecated storefront image

#
echo "npm_config_verbose=true" >> .env.local
#

echo "Will build the Saleor Deprecated storefront image."
podman build --build-arg API_URI=http://mysaleorapi.example.com/graphql/ --build-arg DEMO_MODE=1 --tag saleor-deprecated-storefront:$SALEOR_DEPRECATED_STOREFRONT_VERSION --file Dockerfile

echo "Will push the Saleor API image to our public or private Docker registry site."
podman login --username myregistryusername --password myregistrysuperhardpassword mydockerregistry.example.com
podman push saleor-deprecated-storefront:$SALEOR_DEPRECATED_STOREFRONT_VERSION mydockerregistry.example.com/saleor/deprecated-storefront:$SALEOR_DEPRECATED_STOREFRONT_VERSION

echo "Just to verify, we will search that Saleor Deprecated storefront image in our public or private Docker registry site."
podman search --log-level debug --list-tags mydockerregistry.example.com/saleor/deprecated-storefront

Saleor New React Storefront

# SALEOR_NEW_STOREFRONT_VERSION environment variable will point to a valid tag
# or commit ID in the saleor/saleor-storefront repository
export SALEOR_NEW_STOREFRONT_VERSION=main

# this step probably needs superuser privileges
echo 'unqualified-search-registries = ["docker.io"]' > /etc/containers/registries.conf

# the git binary needs to be previously installed
git clone git://github.com/saleor/react-storefront
cd react-storefront
git checkout $SALEOR_NEW_STOREFRONT_VERSION

# Dockerfile step begins
# there is no Dockerfile provided by react-storefront sources, then we
# will need to create one by ourselves, as follows
cat > Dockerfile << EOF
FROM docker.io/node:16
WORKDIR /app
RUN npm install -g pnpm
COPY . .
RUN pnpm i

ARG NEXT_PUBLIC_API_URI
ARG NEXT_PUBLIC_DEFAULT_CHANNEL
ARG NEXT_PUBLIC_VERCEL_URL

ENV NEXT_PUBLIC_API_URI ${NEXT_PUBLIC_API_URI:-http://mysaleorapi.example.com/graphql/}
ENV NEXT_PUBLIC_DEFAULT_CHANNEL ${NEXT_PUBLIC_DEFAULT_CHANNEL:-default-channel}
ENV NEXT_PUBLIC_VERCEL_URL ${NEXT_PUBLIC_VERCEL_URL:-http://react-storefront.example.com}

# export failed with this:
# Error: i18n support is not compatible with next export. See here for more info on deploying: https://nextjs.org/docs/deployment
# RUN pnpm run export

# This is to prevent this error:
#   http://mynewreactstorefront.example.com/_next/image?url=http%3A%2F%2Fmysaleorapi.example.com%2Fmedia%2Fproducts%2Fsaleordemoproduct_cl_boot07_1.png&w=828&q=75
#   400 Bad Request
#   data:text/plain,"url" parameter is not allowed
# which seems to come from this Next.js source file:
#  https://github.com/vercel/next.js/blob/canary/packages/next/server/image-optimizer.ts
#    if (!domains.includes(hrefParsed.hostname)) {
#      res.statusCode = 400
#      res.end('"url" parameter is not allowed')
#      return { finished: true }
#    }
RUN sed -i 's/"img.youtube.com"/"img.youtube.com", "mysaleorapi.example.com"/g' next.config.js

# Then as an alternative to the failed "pnpm run export" attempt, we will directly
# run the Node application in production mode, exposing its default port 3000.
# see https://nextjs.org/docs/api-reference/cli#production
# see https://github.com/saleor/react-storefront/issues/84#issuecomment-966874104
expose 3000
RUN pnpm run build
CMD pnpm run start
EOF
# Dockerfile step ends

# The add-host argument allows us to resolve the hostname of the Saleor API instance inside
# the new React Storefront image, but you can remove it if not needed in your environment
# because there are properly configured DNS records for that
echo "Will build the Saleor New React Storefront image."
po

Deploy saleor with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has saleor on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartsaleor is pinned at v0.1.1 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply