-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrefresh.zsh
More file actions
executable file
·216 lines (178 loc) · 6.95 KB
/
refresh.zsh
File metadata and controls
executable file
·216 lines (178 loc) · 6.95 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env zsh
: '
OpenSecOps Foundation Component Script Distribution System
This script distributes core deployment scripts from the Installer to all Foundation components,
ensuring consistent deployment behavior and capabilities across the OpenSecOps ecosystem.
What it distributes:
- deploy.py: Main deployment orchestration script with parameter resolution
- setup.zsh: Git repository setup utilities for dual-repository workflow
- publish.zsh: Publication workflow for clean release management
- .gitignore: Standardized ignore patterns for Foundation components
The refresh mechanism maintains script consistency while allowing each component to have
its own repository and development workflow. This ensures all components have the latest
deployment capabilities and bug fixes.
Usage:
./refresh [--dev] [--push] [REPO_name]
Options:
--dev: Run the script in development mode. In this mode, the script creates symlinks
for the scripts in the REPO without the file extension, and copies the example
deployment and changelog files to the REPO if they do not already exist.
--push: Push the changes to the remote repository after all changes have been made to
a directory.
REPO_name: The name of a specific REPO to process. If not provided, the script will
process all directories in the outer REPO that start with the names in APP_DIRECTORIES.
Example:
./scriptname --dev --push Foundation-MyApp
'
OUTER_FOLDER=".."
APP_DIRECTORIES=("Foundation" "SOAR")
SCRIPTS=("setup.zsh" "deploy.py" "publish.zsh")
DEPLOY_EXAMPLE_FILE="config-deploy.toml.example"
CHANGELOG_EXAMPLE_FILE="CHANGELOG.md.example"
LICENSE_FILE="LICENSE.md"
APP_TYPE_MAP=("Foundation:foundation" "SOAR:soar")
# Define colors
YELLOW="\033[93m"
LIGHT_BLUE="\033[94m"
GREEN="\033[92m"
RED="\033[91m"
END="\033[0m"
BOLD="\033[1m"
# Define a function to push changes
push_changes() {
local repo=$1
echo -e "${YELLOW}Pushing changes to the remote repository for $repo${END}"
cd "$repo"
# # Check for uncommitted changes
# if [[ -n $(git status --porcelain) ]]; then
# echo -e "${RED}There are uncommitted changes in $repo. Please commit them before running this script.${END}"
# cd -
# return
# fi
git add .
git commit -m "Dev scripts and files updated by automation."
git push origin main
cd -
}
# Optional exact REPO name as parameter
FILTER=""
DEV_MODE=false
# Process parameters
for arg in "$@"
do
if [[ "$arg" == "--dev" ]]
then
DEV_MODE=true
elif [[ "$arg" == "--push" ]]
then
PUSH_CHANGES=true
elif [[ -z "$FILTER" ]]
then
FILTER="$arg"
fi
done
# If exact REPO name is provided, process only that REPO. Otherwise, process all directories.
if [[ -z "$FILTER" ]]
then
TARGET_DIRECTORIES=()
for REPO in "${OUTER_FOLDER}"/*
do
if [[ -d "$REPO" ]]
then
BASE_DIR_NAME=$(basename "$REPO")
for APP_PREFIX in "${APP_DIRECTORIES[@]}"
do
if [[ "$BASE_DIR_NAME" == "$APP_PREFIX"* ]]
then
TARGET_DIRECTORIES+=("$REPO")
break
fi
done
fi
done
else
TARGET_DIRECTORIES=("${OUTER_FOLDER}/${FILTER}")
fi
for REPO in "${TARGET_DIRECTORIES[@]}"
do
if [[ -d "$REPO" ]]
then
echo -e "${LIGHT_BLUE}-------------------------------------${END}"
echo -e "${LIGHT_BLUE}Processing $REPO${END}"
BASE_DIR_NAME=$(basename "$REPO")
APP_TYPE="unknown"
for app_mapping in "${APP_TYPE_MAP[@]}"
do
DIR_PREFIX="${app_mapping%%:*}"
if [[ "$BASE_DIR_NAME" == "$DIR_PREFIX"* ]]
then
APP_TYPE="${app_mapping##*:}"
break
fi
done
# Check if scripts folder exists in the given REPO and delete it
if [[ -d "$REPO/scripts" ]]
then
echo -e "${YELLOW}Removing existing scripts REPO in $REPO${END}"
rm -rf "$REPO/scripts"
fi
# Create a new scripts REPO
echo -e "${YELLOW}Creating new scripts REPO in $REPO${END}"
mkdir "$REPO/scripts"
# Copy the scripts and make them executable
for SCRIPT in "${SCRIPTS[@]}"
do
echo -e "${YELLOW}Copying $SCRIPT to the new scripts REPO and setting it as executable${END}"
cp "scripts/$SCRIPT" "$REPO/scripts/"
chmod +x "$REPO/scripts/$SCRIPT"
# If DEV_MODE is true, create symlink without extension in the REPO
if [[ "$DEV_MODE" == true ]]
then
echo -e "${YELLOW}Creating symlink for $SCRIPT in the REPO without file extension${END}"
SCRIPT_BASE_NAME=$(basename "$SCRIPT")
SCRIPT_BASE_NAME="${SCRIPT_BASE_NAME%.*}"
ln -s "scripts/${SCRIPT}" "${REPO}/${SCRIPT_BASE_NAME}"
fi
done
# Copy README
echo -e "${YELLOW}Copying README.md to the new scripts REPO${END}"
cp "scripts/README.md" "$REPO/scripts/README.md"
# Copy .gitignore to the new REPO
echo -e "${YELLOW}Copying .gitignore to the new REPO${END}"
cp ".gitignore" "$REPO/"
# If DEV_MODE is true, copy example deploy and changelog config file to the main REPO if it does not exist
if [[ "$DEV_MODE" == true ]]
then
if [[ ! -e "$REPO/config-deploy.toml" ]]
then
echo -e "${YELLOW}Copying $DEPLOY_EXAMPLE_FILE to $REPO/config-deploy.toml with substitutions${END}"
# Read the contents of the file into a variable
file_content=$(cat "$DEPLOY_EXAMPLE_FILE")
# Remove the prefix from $BASE_DIR_NAME
SHORTENED_BASE_DIR_NAME=${BASE_DIR_NAME#*-}
# Perform the string substitutions
file_content=$(perl -pe "s/([^-]+)-<repo>/\1-${SHORTENED_BASE_DIR_NAME}/g" <<< "$file_content")
file_content=${file_content//<repo>/$BASE_DIR_NAME}
file_content=${file_content//<app>/$APP_TYPE}
# Write the result to config-deploy.toml
echo "$file_content" > "$REPO/config-deploy.toml"
fi
if [[ ! -e "$REPO/CHANGELOG.md" ]]
then
echo -e "${YELLOW}Copying $CHANGELOG_EXAMPLE_FILE to $REPO/CHANGELOG.md${END}"
cp "$CHANGELOG_EXAMPLE_FILE" "$REPO/CHANGELOG.md"
fi
echo -e "${YELLOW}Copying $LICENSE to $REPO/$LICENSE_FILE${END}"
cp "$LICENSE_FILE" "$REPO/$LICENSE_FILE"
fi
# If PUSH_CHANGES is true, push the changes to the remote repository
if [[ "$PUSH_CHANGES" == true ]]
then
push_changes "$REPO"
fi
echo -e "${GREEN}Finished processing $REPO${END}"
echo -e "${LIGHT_BLUE}-------------------------------------${END}"
else
echo -e "${RED}REPO $REPO not found${END}"
fi
done