Add an init_site.sh script

This adds a script that can be used to create a new site definitions
based on reference manifests in treasuremap.  It does the following:

1. clone treasuremap at the appropriate tag/branch
2. create a project side-by-side with airshipctl/treasuremap
3. copy in reference site manifests
4. update the site's metadata.yaml appropriately
5. create & update an airship config file for the site

Change-Id: Ied0403cc9df461a1a1ee75fca0caf17fb56055bc
(cherry picked from commit 9f51669d61)
This commit is contained in:
Matt McEuen 2021-04-07 14:21:00 -05:00 committed by Sirajudeen
parent ab7bbfce89
commit 0d935d8200
1 changed files with 105 additions and 0 deletions

105
tools/init_site.sh Executable file
View File

@ -0,0 +1,105 @@
#!/bin/bash
# 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.
# This interactive script makes a copy of an Airship reference site and turns it
# into a standalone project.
set -e
# We can override this to "../treasuremap/manifests/site/test-site" till stl3 is merged
: ${REFERENCE_SITE:="../treasuremap/manifests/site/reference-airship-core"}
REFERENCE_SITE_SHORT=$(echo ${REFERENCE_SITE} | sed "s|.*/||")
: ${REFERENCE_TYPE:="airship-core"}
# The branch/tag foo below is only needed till we work some kinks out of `airshipctl document pull`
# Note: for airshipctl and treasuremap, specify a tag OR a branch, with "" for the other
: ${AIRSHIPCTL_REF:="v2.0.0"} # We can override this to "v2.0" till v2.0.0 is tagged
: ${AIRSHIPCTL_REF_TYPE:="tag"} # We can override this to "branch" till v2.0.0 is tagged
: ${TREASUREMAP_REF:="v2.0.0"} # We can override this to "v2.0" till v2.0.0 is tagged
: ${TREASUREMAP_REF_TYPE:="tag"} # We can override this to "branch" till v2.0.0 is tagged
# Args expected by `airshipctl config set-manifest`
TREASUREMAP_CONF_REF="--${TREASUREMAP_REF_TYPE} ${TREASUREMAP_REF}"
AIRSHIPCTL_CONF_REF="--${AIRSHIPCTL_REF_TYPE} ${AIRSHIPCTL_REF}"
echo "This script will initialize a new Airship site definition, based on the"
echo "treasuremap reference manifests, in a new project that lives side-by-side"
echo "with airshipctl and treasuremap. Please run this script from the airshipctl directory."
echo
if [[ -z "$SITE" || -z "$PROJECT" ]]; then
read -p "Choose a name for your project: " PROJECT
read -p "Choose a name for your site: " SITE
SITE_LOCATION="../${PROJECT}/manifests/site/${SITE}"
read -p "Creating '${SITE_LOCATION}', do you want to continue? (Y/N): " OK
if ! [[ $OK == [yY] ]]; then
echo "Site initialization cancelled"
exit 1
fi
else
SITE_LOCATION="../${PROJECT}/manifests/site/${SITE}"
fi
if [[ -e "${SITE_LOCATION}" ]]; then
echo "A site definition ${SITE_LOCATION} already exists, aborting"
exit 2
fi
set -x
# TODO: replace with `airshipctl document pull` once tag/branch based pulls work
if [[ ! -e ../treasuremap ]]; then
git clone https://opendev.org/airship/treasuremap ../treasuremap
fi
pushd .; cd ../treasuremap; git checkout ${TREASUREMAP_REF}; popd
# Initialize a new site from the treasuremap reference
mkdir -p "${SITE_LOCATION}"
cp -r ${REFERENCE_SITE}/* "${SITE_LOCATION}"
# Update kustomize references
find "${SITE_LOCATION}" -type f -exec sed -i \
"s|type/${REFERENCE_TYPE}|../../treasuremap/manifests/type/${REFERENCE_TYPE}|g" {} +
find "${SITE_LOCATION}" -type f -exec sed -i \
"s|\.\./function|../../../treasuremap/manifests/function|g" {} +
# Update metadata.yaml paths with the new site name
sed -i "s|${REFERENCE_SITE_SHORT}|${SITE}|g" "${SITE_LOCATION}/metadata.yaml"
# Set up airshipctl config file
# Note: set-context doesn't have an option to set managementConfiguration. A problem?
if [[ ! -e ~/.airship/config ]]; then
airshipctl config init
fi
airshipctl config set-manifest "${SITE}" \
--repo airshipctl \
--url "https://opendev.org/airship/airshipctl" \
${AIRSHIPCTL_CONF_REF}
airshipctl config set-manifest "${SITE}" \
--repo treasuremap \
--url "https://opendev.org/airship/treasuremap" \
${TREASUREMAP_CONF_REF}
airshipctl config set-manifest "${SITE}" \
--repo primary \
--url "https://github.com/my-organization/${PROJECT}" \
--branch master
airshipctl config set-manifest "${SITE}" \
--metadata-path "manifests/site/${SITE}/metadata.yaml" \
--target-path "$(realpath ..)"
airshipctl config set-context ephemeral-cluster --manifest "${SITE}"
airshipctl config set-context target-cluster --manifest "${SITE}"
airshipctl config use-context ephemeral-cluster