Sleds/buildvm/scat/sl_mongohelp.sh

186 lines
5.6 KiB
Bash

#!/bin/bash
# source sl_mongohelp.sh
if [ "$SEQUENCELOGICHOME" = "" ]; then
echo "[INFO] Using default SEQUENCELOGICHOME value"
export SEQUENCELOGICHOME=/sequencelogic
fi
if [ "${SLSYNC}" = "" ]; then
export SLSYNC="${SEQUENCELOGICHOME}/SLSync"
fi
# return 1 if user ($1)
function isUser(){
user=$1
me=`whoami`
if [ "$user" = "$me" ]; then
return 1
else
return 0
fi
}
function ownerMode(){
file=$1
chown ${CHMOD_FLAG} sequencelogic:sequencelogic $file
#chgrp ${CHMOD_FLAG} sequencelogic $file
chmod ${CHMOD_FLAG} $2 $file
echo "[OK] Changed: ${file} owner/group to 'sequencelogic' and set mode to: ${2}"
}
function createDirectory(){
dir=$1
if [ ! -d "${dir}" ]; then
mkdir -p "${dir}"
echo "[OK] Created directory: ${dir}"
fi
ownerMode "${dir}" $2
}
# create public/private keys in $SEQUENCELOGICHOME/cg
function createKeys(){
priv=${SEQUENCELOGICHOME}/cg/$1
pub=${SEQUENCELOGICHOME}/cg/$2
openssl genrsa -out ${priv} 2048
openssl rsa -in ${priv} -out ${pub} -outform PEM -pubout
ownerMode ${priv} 440
ownerMode ${pub} 440
echo "[OK] Generated public and private keys"
return 0
}
# create a uuid of length $1
function createUUID(){
LEN=$1
uuid=`uuidgen | sed 's/-//g'`
uuid=`echo $uuid | awk -v LEN=${LEN} '
{
frag=substr($0, 1, LEN);
print frag;
}
'`
echo $uuid
}
if [ "$MONGO_HOST" = "" ]; then
export MONGO_HOST="localhost"
fi
if [ "$MONGO_PORT" = "" ]; then
export MONGO_PORT="27017"
fi
MANAGED_POLICIES=`cat <<EOF
{policyName: "allowUserPasswordChange", type: 'boolean', value:true, list:[], prompts:{"en_US":"{logic-only}"}},
{policyName: "delayLoginFailure", type: 'duration', value:1500, list:[], prompts:{"en_US":"{logic-only 1.5secs}"}},
{policyName: "passwordFailLockout", type: 'int', value:5, list:[], prompts:{"en_US":"{logic-only}"}},
{policyName: "passwordLockoutAction", type: 'list', value:"delay", list:["delay","disable-account","re-enter-challenges"], prompts:{"en_US":"{logic-only}"}},
{policyName: "passwordLockoutDelay", type: 'duration', value:15000, list:[], prompts:{"en_US":"{logic-only 15secs}"}},
{policyName: "passwordResetAllowed", type: 'boolean', value:true, list:[], prompts:{"en_US":"{logic-only}"}},
{policyName: "maxDevices", type: 'int', value:10, list:[], prompts:{"en_US":"A maximum of 100 devices is supported per user"}},
{policyName: "pushFailureInactivateDuration", type: 'duration', value:1209600000, list:[], prompts:{"en_US":"Continued push notification failure device inactivation duration"}},
{policyName: "noLoginInactivateDuration", type: 'duration', value:2709400000, list:[], prompts:{"en_US":"Device not logged in inactivation duration"}},
{policyName: "noLoginRemoveDuration", type: 'duration', value:15732000000, list:[], prompts:{"en_US":"Device not logged in removal duration"}},
{policyName: "serverUserTracking", type: 'list', value:"on", list:["user-selectable","off","on"], prompts:{"en_US":"Allow user to have web service events tracked"}},
{policyName: "serverUserTrackingTTL", type: 'duration', value:"94608000000", prompts:{"en_US":"{logic-only length of time tracking data remains saved}"}},
{policyName: "smnViewMembersEnabled", type: 'boolean', value:true, list:[], prompts:{"en_US":"{logic-only may users see members in admin web page}"}},
{policyName: "trackBandwidth", type: 'boolean', value:false, list:[], prompts:{"en_US":"Track (and thus enforce) bandwidth consumption per product configurations"}},
{policyName: "browserProvisioning", type: 'list', value:"allow", list:["prohibit","allow","activation"], prompts:{"en_US":"Allow browsers to be provisioned as data devices"}}
EOF
`
# NONE!
MODERATED_POLICIES=""
# Create / update team
# Usage: urn name planId type status searchable tags
# admins default to cgadmin and testbot
function upsertTeam(){
ID=$1
NAME=$2
PLAN=$3
TYPE=$4
STATUS=$5
SEARCH=$6
TAGS=$7
if [ "${TYPE}" = "managed" ]; then
POLICIES="${MANAGED_POLICIES}"
else
POLICIES="${MODERATED_POLICIES}"
fi
echo "--- Creating team: ${NAME}"
json=`cat <<EOF
db.teampolicy.update({_id:"${ID}"}, {\\$set: {
team: "${NAME}",
type: "${TYPE}",
plan_id: "${PLAN}",
van: "${VANID}",
status: "${STATUS}",
searchable: ${SEARCH},
tags: [${TAGS}],
admin: [{office:'000000FF',roles:['teamadmin']},{office:'00000007',roles:['teamadmin']}],
policies: [
${POLICIES}
]}}, {upsert: true})
EOF
`
mongoExec "$json"
}
function mongoExec(){
if [ "${HAS_mongo}" = "false" ]; then
echo "Mongo not installed; not executing: $1"
return 2;
fi
MVERBOSE=--verbose
MECHO="/dev/null"
if [ "$MONGO_QUIET" = "true" ]; then
MVERBOSE="-quiet"
MECHO="/dev/null"
fi
echo "Exec $MONGO_HOST:$MONGO_PORT/sequencelogic $1" 2>&1 >${MECHO}
echo "$1" | mongo ${MVERBOSE} $MONGO_HOST:$MONGO_PORT/sequencelogic
if [ $? = 0 ]; then
echo "[OK] Exec $1" 2>&1 >${MECHO};
return 0;
else
echo "[ERROR] $? $1" 2>&1 >${MECHO};
return 1;
fi
}
# puts into dbCollection ($1) a key ($2) value ($3) into mongo
# requires MONGO_HOST and MONGO_PORT variables
function mongoPutRaw(){
db=$1
key=$2
val="$3"
case "$val" in
true|false|null) val=$val;;
[0-9.]+) val=$val;;
*) val="'${val}'";;
esac
json="db.${db}.update({_id:'${key}'},{_id:'${key}', __rAw__:${val}}, {upsert:true})"
mongoExec "$json"
# echo "[OK] $? set into DB $db key: ${key} value: ${val}"
}
# puts into dbCollection ($1) a key ($2) value ($3) into mongo
# requires MONGO_HOST and MONGO_PORT variables
function mongoPutDoc(){
db=$1
key=$2
val="$3"
json="db.${db}.update({_id:'${key}'},${val}, {upsert:true})"
mongoExec "$json"
}
echo "[INFO] Sourced sl_mongohelp.sh; SEQUENCELOGICHOME is $SEQUENCELOGICHOME"