blob: f2ada07b5316371358e3b69e7fd524bfc40dd08a [file] [log] [blame]
garciadeblas8d8cd992024-05-21 16:04:14 +02001#!/bin/bash
2#######################################################################################
3# Copyright ETSI Contributors and Others.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14# implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#######################################################################################
18
garciadeblas7c473cb2024-08-22 10:13:00 +020019set -e -o pipefail
garciadeblas8d8cd992024-05-21 16:04:14 +020020
21export HERE=$(dirname "$(readlink --canonicalize "$BASH_SOURCE")")
22source "${HERE}/library/functions.sh"
23source "${HERE}/library/trap.sh"
24
25
26#####################################################################
27# Step 1: Create regular user and obtain tokens
28#####################################################################
29
30# Creates an aditional regular user
31# ---------------------------------
32m "Creating new user \"${GITEA_STD_USERNAME}\"..."
33"${HERE}/admin/create-user.sh" "${GITEA_STD_USERNAME}" "${GITEA_STD_USER_PASS}" "${GITEA_STD_USERNAME}@gitea"
34# "${HERE}/admin/create-user.sh" "${GITEA_STD_USERNAME}" "${GITEA_STD_USER_PASS}" "${GITEA_STD_USERNAME}@gitea" --admin
35
36m "New username: ${GITEA_STD_USERNAME}" "$CYAN"
37#m "New user's password: ${GITEA_STD_USER_PASS}" "$CYAN"
38
39# Creates access token for the admin
40# ----------------------------------
41export GITEA_ADMINISTRATOR_TOKEN=$( \
42 "${HERE}/admin/create-cmd-access-token.sh" "${GITEA_ADMINISTRATOR_USERNAME}" "${GITEA_ADMINISTRATOR_TOKEN_NAME}" | \
43 grep 'Access token was successfully created' | \
44 cut -d ' ' -f 6 \
45)
46m "Admin token name: ${GITEA_ADMINISTRATOR_TOKEN_NAME}"
47m "Admin token: ${GITEA_ADMINISTRATOR_TOKEN}"
48
49# Creates access token for the user
50# ---------------------------------
51export GITEA_STD_TOKEN=$( \
52 "${HERE}/admin/create-cmd-access-token.sh" "${GITEA_STD_USERNAME}" "${GITEA_STD_TOKEN_NAME}" | \
53 grep 'Access token was successfully created' | \
54 cut -d ' ' -f 6 \
55)
56m "Standard user token name: ${GITEA_STD_TOKEN_NAME}"
57m "Standard user token: ${GITEA_STD_TOKEN}"
58
59# # Alternative method, via API
60# # ---------------------------
61# export GITEA_STD_TOKEN=$( \
62# "${HERE}/admin/create-api-access-token.sh" "${GITEA_HTTP_URL}" "${GITEA_STD_USERNAME}" "${GITEA_STD_USER_PASS}" "${GITEA_STD_TOKEN_NAME}" \
63# )
64
65# Save tokens
66# ---------------------------------
67m "Saving tokens to credentials folder..."
68
69cat << EOF > "${CREDENTIALS_DIR}/gitea_tokens.rc"
70export GITEA_ADMINISTRATOR_TOKEN_NAME=${GITEA_ADMINISTRATOR_TOKEN_NAME}
71export GITEA_ADMINISTRATOR_TOKEN=${GITEA_ADMINISTRATOR_TOKEN}
72export GITEA_STD_TOKEN_NAME=${GITEA_STD_TOKEN_NAME}
73export GITEA_STD_TOKEN=${GITEA_STD_TOKEN}
74EOF
75
76m "Done."
77
78# Saves into K8s cluster as a secret
79m "Saving tokens to secret into K8s cluster..."
80
81kubectl delete secret gitea-tokens -n gitea 2> /dev/null || true
82kubectl create secret generic gitea-tokens -n gitea \
83 --from-literal=GITEA_ADMINISTRATOR_TOKEN_NAME=${GITEA_ADMINISTRATOR_TOKEN_NAME} \
84 --from-literal=GITEA_ADMINISTRATOR_TOKEN=${GITEA_ADMINISTRATOR_TOKEN} \
85 --from-literal=GITEA_STD_TOKEN_NAME=${GITEA_STD_TOKEN_NAME} \
86 --from-literal=GITEA_STD_TOKEN=${GITEA_STD_TOKEN}
87
88m "Done."
89echo
90m "Example: To retrieve token for standard user:"
91m "kubectl get secret gitea-tokens -n gitea -o jsonpath='{.data.GITEA_STD_TOKEN}' | base64 -d" ${CYAN}
92echo
93
94
95#####################################################################
96# Step 2: Create repositories
97#####################################################################
98
99# Loads tokens
100# ---------------------------------
101# m "Reloading tokens..."
102# source "${CREDENTIALS_DIR}/gitea_tokens.rc"
103# echo
104
105# Creates `fleet-osm` and `sw-catalogs-osm` repos in the space of the standard user
106# ----------------------------------------------------
107export REPO=fleet-osm
108m "Creating ${REPO} repo..."
109"${HERE}/admin/create-user-repository.sh" "${GITEA_HTTP_URL}" "${GITEA_STD_USERNAME}" "${GITEA_STD_TOKEN}" "${REPO}" false
110m "Done."
111echo
112
113export REPO=sw-catalogs-osm
114m "Creating ${REPO} repo..."
115"${HERE}/admin/create-user-repository.sh" "${GITEA_HTTP_URL}" "${GITEA_STD_USERNAME}" "${GITEA_STD_TOKEN}" "${REPO}" false
116m "Done."
117echo
118
119# Deletes a repo in the space of the standard user
120# ------------------------------------------------------
121# export REPO=name-of-repo-to-delete
122# "${HERE}/admin/delete-user-repository.sh" "${GITEA_HTTP_URL}" "${GITEA_STD_USERNAME}" "${GITEA_STD_TOKEN}" "${REPO}"
123
124
125
126# #####################################################################
127# # Other provisioning actions, for reference:
128# #####################################################################
129
130# # Creates new organization
131# # ------------------------
132# # "${HERE}/admin/create-org.sh" "${GITEA_HTTP_URL}" "${GITEA_ADMINISTRATOR_USERNAME}" "${GITEA_ADMINISTRATOR_TOKEN}" "${GITEA_EXTRA_ORGANIZATION}" private
133
134# # Creates a new repo in the organization
135# # --------------------------------------
136# # export REPO=test-repo
137# # "${HERE}/admin/create-org-repository.sh" "${GITEA_HTTP_URL}" "${GITEA_STD_USERNAME}" "${GITEA_ADMINISTRATOR_TOKEN}" "${GITEA_EXTRA_ORGANIZATION}" "${REPO}" true
138
139# # Deletes the repo in the space of the standard user
140# # --------------------------------------------------
141# # "${HERE}/admin/delete-org-repository.sh" "${GITEA_HTTP_URL}" "${GITEA_STD_USERNAME}" "${GITEA_ADMINISTRATOR_TOKEN}" "${GITEA_EXTRA_ORGANIZATION}" "${REPO}"
142
143# # Deletes organization
144# # --------------------
145# # "${HERE}/admin/delete-org.sh" "${GITEA_HTTP_URL}" "${GITEA_ADMINISTRATOR_USERNAME}" "${GITEA_ADMINISTRATOR_TOKEN}" "${GITEA_EXTRA_ORGANIZATION}"
146
147# # Creates a new repo in the space of the standard user
148# # ----------------------------------------------------
149# # export REPO=test-user-repo
150# # "${HERE}/admin/create-user-repository.sh" "${GITEA_HTTP_URL}" "${GITEA_STD_USERNAME}" "${GITEA_STD_TOKEN}" "${REPO}" false
151
152# # Deletes the new repo in the space of the standard user
153# # ------------------------------------------------------
154# # "${HERE}/admin/delete-user-repository.sh" "${GITEA_HTTP_URL}" "${GITEA_STD_USERNAME}" "${GITEA_STD_TOKEN}" "${REPO}"