-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupenv
executable file
·96 lines (82 loc) · 2.84 KB
/
setupenv
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
#!/usr/bin/env sh
# Use ./dev/ for files for local development
CONFIG_DIR=${1-.dev}
# Set debugging if not $PROD is set to true
! [ "$(echo "${PROD}" | tr '[:upper:]' '[:lower:]')" = "true" ] && DEBUGGING=true
create_config_dir() {
if ! [ -d "${CONFIG_DIR}" ]; then
if ! [ -e "${CONFIG_DIR}" ]; then
echo "Creating configuration directory \"${CONFIG_DIR}\""
mkdir -p "${CONFIG_DIR}"
else
echo "${CONFIG_DIR} exists but is not a directory!"
echo 'Please change CONFIG_DIR to something else, or delete the file.'
echo 'Exiting...'
exit 1
fi
fi
}
generate_keypair() {
if ! [ -e "${CONFIG_DIR}/id_rsa.pub" ]; then
if [ -e "${CONFIG_DIR}/id_rsa" ]; then
echo "You seem to have a private key but no public key!"
echo "Exiting..."
exit 1
fi
echo "Generating ssh key in \"${CONFIG_DIR}/id_rsa{,.pub}\""
ssh-keygen -q -t rsa -b 1024 -N '' -f "${CONFIG_DIR}/id_rsa"
fi
SSH_KEY="$(cat "${CONFIG_DIR}/id_rsa.pub")"
}
generate_django_key() {
DJANGO_KEYFILE="${CONFIG_DIR}/django"
# Immediately return if key exists
if [ -e "${DJANGO_KEYFILE}" ]; then
SECRET_KEY="$(cat "${DJANGO_KEYFILE}")"
return
fi
if [ "${DEBUGGING}" = "true" ]; then
echo "(development environment) Creating \"insecure\" django password in \"${DJANGO_KEYFILE}\""
echo "insecure" | tr -d '\n' > "${DJANGO_KEYFILE}"
else
echo "(!!PRODUCTION!!) Generating django password, located in \"${DJANGO_KEYFILE}\""
strings /dev/random \
| grep -Eo '[a-zA-Z0-9@_-]' \
| head -64 \
| tr -d '\n' > "${DJANGO_KEYFILE}"
fi
SECRET_KEY="$(cat "${DJANGO_KEYFILE}")"
}
generate_postgres_key() {
POSTGRES_PASSWORDFILE="${CONFIG_DIR}/postgres"
# Immediately return if postgress password exists
if [ -e "${POSTGRES_PASSWORDFILE}" ]; then
POSTGRES_PASSWORD="$(cat "${CONFIG_DIR}/postgres")"
return
fi
if [ "${DEBUGGING}" = "true" ]; then
echo "(development environment) Creating \"insecure\" postgres password in \"${POSTGRES_PASSWORDFILE}\""
echo "insecure" | tr -d '\n' > "${POSTGRES_PASSWORDFILE}"
else
echo "(!!PRODUCTION!!) Generating postgres password, located in \"${POSTGRES_PASSWORDFILE}\""
strings /dev/random \
| grep -Eo '[a-zA-Z0-9@_-]' \
| head -64 \
| tr -d '\n' > "${POSTGRES_PASSWORDFILE}"
fi
POSTGRES_PASSWORD="$(cat "${CONFIG_DIR}/postgres")"
}
# Create credential files
create_config_dir
[ -z "${SSH_KEY}" ] && generate_keypair
[ -z "${SECRET_KEY}" ] && generate_django_key
[ -z "${POSTGRES_PASSWORD}" ] && generate_postgres_key
# Export environment
export SSH_KEY="$SSH_KEY"
export SECRET_KEY="$SECRET_KEY"
export POSTGRES_PASSWORD="$POSTGRES_PASSWORD"
export CONFIG_DIR="$CONFIG_DIR"
echo "SSH_KEY=\"$SSH_KEY\""
echo "SECRET_KEY=\"$SECRET_KEY\""
echo "POSTGRES_PASSWORD=\"$POSTGRES_PASSWORD\""
echo "CONFIG_DIR=\"$CONFIG_DIR\""