Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helm install script #27

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
790122e
Added helm installation script.
Jun 18, 2024
60003cd
Copyright statement and information about the relevant Apache Licence.
Jun 18, 2024
62317f8
Refractored the function to detect cluster router base: Instead of us…
Jun 18, 2024
59965c6
Added clear instructions and a description of the script, the options…
Jun 18, 2024
6e1a1a1
Added --help option to display usage information.
Jun 19, 2024
733f5f8
.sh file name change as noted that the install-helm.sh file makes it …
Jun 19, 2024
45c1e1f
Added --cli <oc|kubectl> option to specify which CLI tool to use.
Jun 19, 2024
40aa740
Placed the checks for essential information at the top of the file en…
Jun 19, 2024
d4a5a5c
Support for --release-name and --generate-name options: Allows users …
Jun 19, 2024
aa66059
Merge branch 'redhat-developer:main' into helm-install-script
Fortune-Ndlovu Jun 24, 2024
8996e31
Using dirname to determine the base directory of the script ensuring …
Fortune-Ndlovu Jun 24, 2024
7673f4b
Removed login check because we already provide feedback if the user i…
Fortune-Ndlovu Jun 24, 2024
77042ae
Updated the detect_cluster_router_base function to use oc get ingress…
Fortune-Ndlovu Jun 24, 2024
a990059
Refactored the script to only handle the CLI Helm installation for OCP.
Fortune-Ndlovu Jun 25, 2024
bc57eed
Added install.md file.
Fortune-Ndlovu Jun 28, 2024
8ba23d9
Update install.md
Fortune-Ndlovu Jun 28, 2024
1b5f427
Update install.md
Fortune-Ndlovu Jun 28, 2024
ca84b36
Add suggestions
Fortune-Ndlovu Jul 1, 2024
8b01512
Update install.md
Fortune-Ndlovu Jul 1, 2024
cafbd47
Improve error handling and better practices in setting configuration …
Fortune-Ndlovu Jul 8, 2024
813fa8b
If detect_cluster_router_base fails, we could just exit with the righ…
Fortune-Ndlovu Jul 16, 2024
a67d111
since the ROUTER_BASE is always required and will either be provided …
Fortune-Ndlovu Jul 16, 2024
2317626
removing case
Fortune-Ndlovu Jul 16, 2024
905e1ae
adding space
Fortune-Ndlovu Jul 16, 2024
401589f
Not needed to specify the path to the Chart default values.yaml file.
Fortune-Ndlovu Jul 16, 2024
41bf45b
Trapping for each condition separately, and then providing a call to …
Fortune-Ndlovu Jul 16, 2024
dd8c73e
List all Helm releases in the specified name space
Fortune-Ndlovu Jul 18, 2024
cfa136f
Adding the -i flag to install the chart if it doesnt exists, or upgra…
Fortune-Ndlovu Jul 19, 2024
595db25
Testing script
Fortune-Ndlovu Jul 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions scripts/install.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
## **Documentation for the script to handle CLI helm installation for OCP**
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved

## **Overview**

The purpose of the [`install.sh` script](./install.sh) is to simplify and automate the installation process of this Helm Chart on OpenShift Container Platform (OCP) clusters. Instead of requiring users to follow a lengthy and potentially error-prone series of manual steps, this script consolidates the process into a single, reusable command that can:

* Detect if the OpenShift Client ([`oc`](https://docs.openshift.com/container-platform/4.16/cli_reference/openshift_cli/getting-started-cli.html)) is installed; fail if not found and report the error to the user with a call to action (installing it).

* If cluster router base can be detected, use that value to update the helm chart installation; if not, fail and request user pass in command line flag as cluster router base could not be detected (can test this failure case with dev sandbox; can test passing case with clusterbot).

## **Prerequisites**

1. Ensure that `oc` (for OpenShift) is installed. See https://docs.openshift.com/container-platform/4.16/cli_reference/openshift_cli/getting-started-cli.html#installing-openshift-cli for further instructions.
2. Ensure that you are logged into an OCP cluster and that it is running. See [Logging in to the OpenShift CLI](https://docs.redhat.com/en/documentation/openshift_container_platform/4.16/html/cli_tools/openshift-cli-oc#cli-logging-in_cli-developer-commands).
3. git
4. [Helm](https://helm.sh/docs/intro/install/)

## **Installation Steps**

1. **Download the Script** - Clone the repository and navigate to the `scripts` directory:

```shell
git clone --depth 1 https://github.com/redhat-developer/rhdh-chart.git
cd rhdh-charts/scripts
```

2. **Run the Script** - Execute the script to install the Helm chart. The script will automatically detect that you are using `oc` and ensure you are logged into your cluster. \
`./install.sh`

3. **Specify Router Base** - If the script cannot automatically detect the cluster router base, you can provide it manually using the `--router-base` flag, example:\
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved
`./install.sh --router-base <your-router-base>`

4. **Specify Release Name** - To install the Helm chart with a custom release name: \
`./install.sh --release-name myrelease`

5. **Generate a Release Name** - To generate a name for the Helm release: \
`./install.sh --generate-name`

6. **Specify Namespace** - To specify the namespace for the Helm release: \
`./install.sh --namespace mynamespace`

7. **Specify Custom Values File** - To use a custom values file for the Helm chart: \
`./install.sh --values /path/to/values.yaml`

## **Troubleshooting**

* Missing <code>oc</code> - </strong> If the script outputs an error indicating <code>oc</code> is not installed, please install the CLI tool for your cluster:
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved
* For OpenShift: Install <code>oc</code>

* <strong>Not Logged Into Cluster: </strong> If you are not logged into a cluster, follow these steps to log in:
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved
* For OpenShift:
`oc login <your-cluster-url>`
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved

* <strong>Router Base Detection Failed: - </strong> If the script cannot detect the cluster router base, manually provide it using the <code>--router-base</code> flag as shown in the example usage section.
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved

* <strong>Error: INSTALLATION FAILED:</strong>

An error occurred while checking for chart dependencies. You may need to run `helm dependency build` to fetch missing dependencies: found in Chart.yaml, but missing in charts/ directory: common, backstage:
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved
`helm dependency build`

An example of the installation output should be:

Using router base: example.com
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved

NAME: ...
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved

LAST DEPLOYED: ...

NAMESPACE: ...

STATUS: ...

REVISION: ...

Helm installation completed successfully.

You can run: `helm list --namespace $myProject` to confirm this installation.
nickboldt marked this conversation as resolved.
Show resolved Hide resolved
146 changes: 146 additions & 0 deletions scripts/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/bin/bash
#
# Copyright (c) 2024 Red Hat, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Script to handle CLI helm installation for OCP
#
# Requires: oc and an active login session to a cluster.
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved

set -e

usage() {
echo "
This script simplifies and automates the installation process of Helm charts on the OpenShift Container Platform (OCP) clusters.
It ensures that the user is logged into a cluster and attempts to detect the cluster router base, updating the Helm chart configuration accordingly.

Usage:
$0 [OPTIONS]

Options:
--router-base <router-base> : Manually provide the cluster router base if auto-detection fails.
--release-name <name> : Specify a custom release name for the Helm chart.
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved
--generate-name : Generate a name for the Helm release (overrides --release-name).
--namespace <namespace> : Specify the namespace for the Helm release (autodetects if not provided).
--values <file> : Specify your own values file for the Helm chart.
--help : Show this help message and exit.

Examples:
$0 # Auto-detects router base and installs the Helm chart
$0 --router-base example.com # Manually specifies the router base and installs the Helm chart
$0 --release-name myrelease # Installs the Helm chart with the specified release name
$0 --generate-name # Generates a name for the Helm release
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved
$0 --values /path/to/values.yaml # Installs the Helm chart using the specified values file
"
}

# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}

# Check for required commands
if ! command_exists helm || ! command_exists oc; then
echo "Error: Required commands 'helm' and 'oc' are not available."
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved
exit 1
fi

# Check if required files and directories exist
HELM_CHART_DIR="$(dirname "$0")/../charts/backstage"
DEFAULT_VALUES_FILE="$HELM_CHART_DIR/values.yaml"

if [ ! -d "$HELM_CHART_DIR" ]; then
echo "Error: Helm chart directory not found at $HELM_CHART_DIR"
exit 1
fi

# Parse command-line arguments
ROUTER_BASE=""
RELEASE_NAME=""
GENERATE_NAME=false
NAMESPACE=""
VALUES_FILE="$DEFAULT_VALUES_FILE"
EXTRA_HELM_ARGS=""

while [[ "$#" -gt 0 ]]; do
case $1 in
--router-base)
ROUTER_BASE="$2"
shift
;;
--release-name)
RELEASE_NAME="$2"
shift
;;
--generate-name)
GENERATE_NAME=true
;;
--namespace)
NAMESPACE="$2"
shift
;;
--help)
usage
exit 0
;;
*)
EXTRA_HELM_ARGS+=" $1"
;;
esac
shift
done

# Function to detect cluster router base
detect_cluster_router_base() {
ROUTER_BASE=$(oc get ingress.config.openshift.io/cluster -o=jsonpath='{.spec.domain}')
}

# Detect cluster router base if not provided
if [[ -z "$ROUTER_BASE" ]]; then
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved
detect_cluster_router_base || (echo "Error: Cluster router base could not be detected. Please provide it using the --router-base flag." && exit 1)
fi

# Detect namespace if not provided
if [[ -z "$NAMESPACE" ]]; then
NAMESPACE=$(oc config view --minify --output 'jsonpath={..namespace}')
if [[ -z $NAMESPACE ]]; then
echo "Error: Namespace could not be detected. Please provide it using the --namespace flag."
exit 1
fi
fi

# Always include the router base in Helm arguments
EXTRA_HELM_ARGS+=" --set global.clusterRouterBase=$ROUTER_BASE"

# Construct Helm install command
HELM_CMD="helm install"
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved
if [[ $GENERATE_NAME == true ]]; then
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved
HELM_CMD+=" --generate-name"
elif [[ -z "$RELEASE_NAME" ]]; then
echo "Error: Either --release-name must be specified or --generate-name must be used."
exit 1
else
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved
HELM_CMD+=" $RELEASE_NAME"
fi

HELM_CMD+=" $HELM_CHART_DIR --namespace $NAMESPACE $EXTRA_HELM_ARGS"

# Execute Helm install command
echo "Executing: $HELM_CMD"

if eval "$HELM_CMD"; then
echo "Helm installation completed successfully."
else
echo "Something went wrong with Helm installation!"
exit 1
Fortune-Ndlovu marked this conversation as resolved.
Show resolved Hide resolved
fi