-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigure
More file actions
executable file
·178 lines (142 loc) · 7.32 KB
/
configure
File metadata and controls
executable file
·178 lines (142 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
set -euo pipefail
# Colors
GREEN="\033[0;32m"
BLUE="\033[0;34m"
YELLOW="\033[1;33m"
RED="\033[0;31m"
NC="\033[0m"
# Spinner state
SPINNER_PID=""
SPINNER_MSG=""
start_spinner() {
SPINNER_MSG="$1"
echo -ne "${BLUE}==>${NC} $SPINNER_MSG..."
(
while true; do
for c in / - \\ \|; do
echo -ne "\r${BLUE}==>${NC} $SPINNER_MSG... $c"
sleep 0.1
done
done
) &
SPINNER_PID=$!
disown
}
stop_spinner_success() {
kill "$SPINNER_PID" >/dev/null 2>&1 || true
wait "$SPINNER_PID" 2>/dev/null || true
echo -ne "\r\033[K"
echo -e "${GREEN}✔${NC} $SPINNER_MSG"
}
stop_spinner_error() {
kill "$SPINNER_PID" >/dev/null 2>&1 || true
wait "$SPINNER_PID" 2>/dev/null || true
echo -ne "\r\033[K"
echo -e "${RED}✖${NC} $SPINNER_MSG"
exit 1
}
# --- Step 1: Environment Validation ---
start_spinner "Validating that the NRN has been loaded into the environment."
if [ -z "${NRN:-}" ]; then
stop_spinner_error "NRN is not set. Please export the NRN environment variable before running this script."
fi
stop_spinner_success "NRN found and loaded successfully."
start_spinner "Validating that the REPO_PATH has been loaded into the environment."
if [ -z "${REPO_PATH:-}" ]; then
stop_spinner_error "REPO_PATH is not set. Please export the REPO_PATH environment variable before running this script."
fi
stop_spinner_success "REPO_PATH found and loaded successfully."
start_spinner "Validating that the ENVIRONMENT has been loaded into the environment."
if [ -z "${ENVIRONMENT:-}" ]; then
stop_spinner_error "ENVIRONMENT is not set. Please export the ENVIRONMENT environment variable before running this script."
fi
stop_spinner_success "ENVIRONMENT found and loaded successfully."
start_spinner "Validating that the NP_API_KEY has been loaded into the environment."
if [ -z "${NP_API_KEY:-}" ]; then
stop_spinner_error "NP_API_KEY is not set. Please export the NP_API_KEY environment variable before running this script."
fi
stop_spinner_success "NP_API_KEY found and loaded successfully."
# SERVICE_PATH is the subdirectory within REPO_PATH that contains the implementation.
# Defaults to "lambda" which matches the structure of this repository.
export SERVICE_PATH="${SERVICE_PATH:-lambda}"
echo -e ""
echo -e "${YELLOW}Configuration:${NC}"
echo -e " NRN = $NRN"
echo -e " REPO_PATH = $REPO_PATH"
echo -e " SERVICE_PATH = $SERVICE_PATH"
echo -e " ENVIRONMENT = $ENVIRONMENT"
echo -e ""
# --- Step 2: Generate and Create Service Specification ---
SPECS_PATH="$SERVICE_PATH/specs"
SERVICE_SPEC_PATH="$SPECS_PATH/service-spec.json"
ACTION_DIR="$SPECS_PATH/actions"
SCOPE_TYPE_DEFINITION_PATH="$SPECS_PATH/scope-type-definition.json"
gomplate --file "$SERVICE_SPEC_PATH.tpl" --out "$SERVICE_SPEC_PATH"
start_spinner "Creating the service specification in the platform."
SERVICE_SPEC_BODY=$(cat "$SERVICE_SPEC_PATH")
SERVICE_SPEC=$(np service specification create --body "$SERVICE_SPEC_BODY" --format json) || stop_spinner_error "Failed to create the service specification."
SERVICE_SPECIFICATION_ID=$(echo "$SERVICE_SPEC" | jq -r .id)
SERVICE_SLUG=$(echo "$SERVICE_SPEC" | jq -r .slug)
[[ "$SERVICE_SPECIFICATION_ID" == "null" || -z "$SERVICE_SPECIFICATION_ID" ]] && stop_spinner_error "Failed to parse service specification response: $SERVICE_SPEC"
stop_spinner_success "Service specification created successfully."
echo -e " ${BLUE}→${NC} SERVICE_SPECIFICATION_ID = $SERVICE_SPECIFICATION_ID"
echo -e " ${BLUE}→${NC} SERVICE_SLUG = $SERVICE_SLUG"
rm "$SERVICE_SPEC_PATH"
export SERVICE_SPECIFICATION_ID
export SERVICE_SLUG
# --- Step 3: Create Action Specifications ---
find "$ACTION_DIR" -type f -name "*.tpl" | while read -r TEMPLATE_FILE; do
REL_PATH="${TEMPLATE_FILE#$ACTION_DIR/}"
OUTPUT_PATH="$ACTION_DIR/${REL_PATH%.tpl}"
gomplate --file "$TEMPLATE_FILE" --out "$OUTPUT_PATH"
ACTION_SPEC_BODY=$(cat "$OUTPUT_PATH")
start_spinner "Registering action specification: ${REL_PATH%.json.tpl}."
ACTION_SPEC=$(np service specification action specification create \
--serviceSpecificationId "$SERVICE_SPECIFICATION_ID" \
--body "$ACTION_SPEC_BODY" \
--format json) || stop_spinner_error "Failed to create action specification: $REL_PATH."
ACTION_SPEC_ID=$(echo "$ACTION_SPEC" | jq -r .id)
[[ "$ACTION_SPEC_ID" == "null" || -z "$ACTION_SPEC_ID" ]] && stop_spinner_error "Failed to parse action specification response: $ACTION_SPEC"
rm "$OUTPUT_PATH"
stop_spinner_success "Action specification registered."
echo -e " ${BLUE}→${NC} ACTION_SPEC_ID = $ACTION_SPEC_ID"
done
# --- Step 4: Register Scope Type ---
gomplate --file "$SCOPE_TYPE_DEFINITION_PATH.tpl" --out "$SCOPE_TYPE_DEFINITION_PATH"
SCOPE_TYPE_BODY=$(cat "$SCOPE_TYPE_DEFINITION_PATH")
SCOPE_TYPE_NAME=$(echo "$SCOPE_TYPE_BODY" | jq -r .name)
start_spinner "Creating the scope type: $SCOPE_TYPE_NAME."
SCOPE_TYPE=$(np scope type create --body "$SCOPE_TYPE_BODY" --format json) || stop_spinner_error "Failed to create the scope type: $SCOPE_TYPE_NAME."
SCOPE_TYPE_ID=$(echo "$SCOPE_TYPE" | jq -r .id)
SCOPE_TYPE_NRN=$(echo "$SCOPE_TYPE" | jq -r '.nrn // empty')
[[ "$SCOPE_TYPE_ID" == "null" || -z "$SCOPE_TYPE_ID" ]] && stop_spinner_error "Failed to parse scope type response: $SCOPE_TYPE"
stop_spinner_success "Scope type created successfully."
echo -e " ${BLUE}→${NC} SCOPE_TYPE_ID = $SCOPE_TYPE_ID"
[[ -n "$SCOPE_TYPE_NRN" ]] && echo -e " ${BLUE}→${NC} SCOPE_TYPE_NRN = $SCOPE_TYPE_NRN"
rm "$SCOPE_TYPE_DEFINITION_PATH"
# --- Step 5: Create Notification Channel ---
NOTIFICATION_CHANNEL_PATH="$SPECS_PATH/notification-channel.json"
gomplate --file "$NOTIFICATION_CHANNEL_PATH.tpl" --out "$NOTIFICATION_CHANNEL_PATH"
start_spinner "Creating the notification channel."
NOTIFICATION_CHANNEL_BODY=$(cat "$NOTIFICATION_CHANNEL_PATH")
NOTIFICATION_CHANNEL=$(np notification channel create --format json --body "$NOTIFICATION_CHANNEL_BODY") || stop_spinner_error "Failed to create the notification channel."
NOTIFICATION_CHANNEL_ID=$(echo "$NOTIFICATION_CHANNEL" | jq -r .id)
NOTIFICATION_CHANNEL_NRN=$(echo "$NOTIFICATION_CHANNEL" | jq -r '.nrn // empty')
[[ "$NOTIFICATION_CHANNEL_ID" == "null" || -z "$NOTIFICATION_CHANNEL_ID" ]] && stop_spinner_error "Failed to parse notification channel response: $NOTIFICATION_CHANNEL"
rm "$NOTIFICATION_CHANNEL_PATH"
stop_spinner_success "Notification channel created successfully."
echo -e " ${BLUE}→${NC} NOTIFICATION_CHANNEL_ID = $NOTIFICATION_CHANNEL_ID"
[[ -n "$NOTIFICATION_CHANNEL_NRN" ]] && echo -e " ${BLUE}→${NC} NOTIFICATION_CHANNEL_NRN = $NOTIFICATION_CHANNEL_NRN"
# --- Step 6: Patch NRN ---
start_spinner "Configuring the NRN to use the external provider for logs and metrics."
{
np nrn patch --nrn "$NRN" --body "{\"global.${SERVICE_SLUG}_metric_provider\": \"externalmetrics\", \"global.${SERVICE_SLUG}_log_provider\": \"external\"}" >/dev/null 2>&1
} || stop_spinner_error "Failed to patch the NRN with external provider configuration."
stop_spinner_success "NRN updated to use the external provider."
echo -e "${GREEN}✔${NC} The custom scope setup process completed successfully."
echo -e ""
echo -e " ${BLUE}SERVICE_SPECIFICATION_ID${NC} = $SERVICE_SPECIFICATION_ID"
echo -e " ${BLUE}SERVICE_SLUG${NC} = $SERVICE_SLUG"
echo -e " ${BLUE}SCOPE_TYPE_ID${NC} = $SCOPE_TYPE_ID"
echo -e " ${BLUE}NOTIFICATION_CHANNEL_ID${NC} = $NOTIFICATION_CHANNEL_ID"