better messages and update readme

This commit is contained in:
88lex 2020-05-10 15:17:57 +08:00
parent 3c6a52b660
commit 4eab83ca32
2 changed files with 32 additions and 3 deletions

View file

@ -1,7 +1,18 @@
**sa-gen**
**sagen**
This script is not enormously difficult, but does require reading carefully and installing/setting up gcloud sdk.
Usage: `./sa-gen` will run the script using the variables that you insert/edit below.
CHANGELOG: The updated sagen does exactly what the old version did, except
- Google's back end sometimes lags for a few seconds between creating projects or service accounts and giving the sdk access to see them.
- As a result, sagen now has variable delays for each cycle of creating service accounts and between each 'function'. The functions are:
- Creating a project. If a project exists an ERROR message will tell you. It is non-fatal, ignore it.
- Enabling apis that give the SAs permission to access Google Drive and gsheets. Others can be added manually if you like.
- Creating service accounts (SAs) in each project that has been created.
- Downloading json keys that include a token that allows you to access and act upon Drive and gsheet resources. Guard the keys with your life.
- Creating a members.csv (current) and allmembers.csv (cumulative) list of all SA emails, which can be added to My Drive, a Shared Drive (Team Drive) and/or a gsheet. These emails can be added individualy or in bulk (see Bulk Add to Group).
For sa-gen to run correctly you MUST first edit sa-gen itself, inserting your own information in the fields described below.
Be sure to run `chmod +x sa-gen` to all the script to execute.

View file

@ -12,7 +12,8 @@ SA_EMAIL_BASE_NAME="insertuniquename"
FIRST_SA_NUM=1
NUM_SAS_PER_PROJECT=100
CYCLE_DELAY=0.1s # If issues with Google back end not recognizing SAs increase this number. Set = 0 for no delay
SECTION_DELAY=0.1s # If issues with Google back end not recognizing SAs increase this number. Set = 0 for no delay
SECTION_DELAY=5s # If issues with Google back end not recognizing SAs increase this number. Set = 0 for no delay
create_projects() {
PROJECT="$PROJECT_BASE_NAME$project_num"
@ -49,6 +50,9 @@ create_sas() {
set +x
sleep $CYCLE_DELAY
done
sleep $SECTION_DELAY
SA_COUNT=`gcloud iam service-accounts list | grep gservice | wc -l`
echo -e "Total number of service accounts (SAs) in project $PROJECT = $SA_COUNT"
let COUNT=$COUNT+$NUM_SAS_PER_PROJECT
}
@ -58,6 +62,7 @@ create_keys() {
gcloud config set project $PROJECT
set +x
echo -e "create json keys for $PROJECT"
TOTAL_JSONS_BEF=`ls $KEYS_DIR | grep ".json" | wc -l`
let LAST_SA_NUM=$COUNT+$NUM_SAS_PER_PROJECT-1
for name in $(seq $COUNT $LAST_SA_NUM); do
saname="$SA_EMAIL_BASE_NAME""$name"
@ -70,13 +75,21 @@ create_keys() {
echo "$GROUP_NAME,$saname@$PROJECT.iam.gserviceaccount.com,USER,MEMBER" | tee -a $KEYS_DIR/members.csv $KEYS_DIR/allmembers.csv
sleep $CYCLE_DELAY
done
MEMBER_COUNT=`cat /opt/sa/members.csv | grep "gservice" | wc -l`
echo -e "\nNumber of service accounts in members.csv = $MEMBER_COUNT"
TOTAL_JSONS_NOW=`ls $KEYS_DIR | grep ".json" | wc -l`
let TOTAL_JSONS_MADE=$TOTAL_JSONS_NOW-$TOTAL_JSONS_BEF
echo -e "Total SA json keys created for project $PROJECT = $TOTAL_JSONS_MADE"
let COUNT=$COUNT+$NUM_SAS_PER_PROJECT
}
main() {
mkdir -p $KEYS_DIR
[ -f $KEYS_DIR/members.csv ] && cat $KEYS_DIR/members.csv >> $KEYS_DIR/allmembers.csv && sort -uo $KEYS_DIR/allmembers.csv $KEYS_DIR/allmembers.csv
[ -f $KEYS_DIR/members.csv ] && cat $KEYS_DIR/members.csv >> $KEYS_DIR/allmembers.csv && \
sort -uo $KEYS_DIR/allmembers.csv $KEYS_DIR/allmembers.csv
echo "Group Email [Required],Member Email,Member Type,Member Role" >$KEYS_DIR/members.csv
TOTAL_JSONS_START=`ls $KEYS_DIR | grep ".json" | wc -l`
echo -e "\nTotal SA json keys before running sa-gen = $TOTAL_JSONS_START"
for function in create_projects enable_apis create_sas create_keys ; do
COUNT=$FIRST_SA_NUM
for project_num in $(seq $FIRST_PROJECT_NUM $LAST_PROJECT_NUM); do
@ -84,6 +97,11 @@ main() {
sleep $SECTION_DELAY
done
done
TOTAL_JSONS_END=`ls $KEYS_DIR | grep ".json" | wc -l`
echo -e "\n\nTotal SA json keys BEFORE running sa-gen = $TOTAL_JSONS_START"
echo -e "Total SA json keys AFTER running sa-gen = $TOTAL_JSONS_END"
let TOTAL_JSONS_MADE=$TOTAL_JSONS_END-$TOTAL_JSONS_START
echo -e "Total SA jsons CREATED = $TOTAL_JSONS_MADE"
}
main