Last active
November 25, 2022 23:58
-
-
Save guinunez/4f3006aeaef673eed7a0ff6acbeede79 to your computer and use it in GitHub Desktop.
nest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Tool to the initial setup of a django project on dreamhost: | |
# Latest version: wget -O - https://gist.githubusercontent.com/guinunez/4f3006aeaef673eed7a0ff6acbeede79/raw/nest.sh | bash | |
# This is what the script does | |
# - Create the SSH key | |
# - install python 3 | |
# - Clone repo | |
# - create virtualenv: python3 -m venv .env | |
# - install requeriments.txt | |
# - create settings-local | |
# - migrate | |
# - create passenger file | |
# - create superuser | |
# - create public folder with static and media | |
# - create initial data | |
# - restart server | |
function pause(){ | |
read -p "$*" </dev/tty | |
} | |
function version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; } | |
function vercomp () { | |
if [[ $1 == $2 ]] | |
then | |
return 0 | |
fi | |
local IFS=. | |
local i ver1=($1) ver2=($2) | |
# fill empty fields in ver1 with zeros | |
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)) | |
do | |
ver1[i]=0 | |
done | |
for ((i=0; i<${#ver1[@]}; i++)) | |
do | |
if [[ -z ${ver2[i]} ]] | |
then | |
# fill empty fields in ver2 with zeros | |
ver2[i]=0 | |
fi | |
if ((10#${ver1[i]} > 10#${ver2[i]})) | |
then | |
return 1 | |
fi | |
if ((10#${ver1[i]} < 10#${ver2[i]})) | |
then | |
return 2 | |
fi | |
done | |
return 0 | |
} | |
# TODO: Deberiamos ver si hay un archivo de cracion sin terminar de este sitio | |
# Config section | |
echo "Site's base URL? (sin http://)" | |
read sitiourl </dev/tty | |
echo "Repo URL? (git-ssh)" | |
read repourl </dev/tty | |
echo "Database host:" | |
read databasehost </dev/tty | |
echo "Database name:" | |
read databasename </dev/tty | |
echo "Database username:" | |
read databaseuser </dev/tty | |
echo "Database password:" | |
read databasepassword </dev/tty | |
# TODO: Deberiamos crear un archivo de creacion con estas configuraciones | |
pause 'All set. Press [Enter] key to continue...' | |
# - Crear la llave SSH | |
if [ ! -f ~/.ssh/id_rsa.pub ]; then | |
echo "id_rsa.pub not found, creating it" | |
ssh-keygen | |
fi | |
echo "This is the public ssh key, copy it on bitbucket's or github's settings" | |
echo | |
cat ~/.ssh/id_rsa.pub | |
pause 'Press [Enter] key to continue...' | |
# - instalar python 3 | |
# como dreamhost no tiene instalado venv, vamos a forzar instalacion siempre | |
python_version="3.10.1" | |
version=$(python3 -V 2>&1 | grep -Po '(?<=Python )(.+)') | |
# Testear esto, y mucho | |
vercomp $version $python_version | |
# if [ "$version" -gt "$python_version" ]; then | |
if [ "$?" eq "1" ]; then | |
echo "Downloading and installing python $python_version" | |
url_python="https://www.python.org/ftp/python/$python_version/Python-$python_version.tar.xz" | |
wget $url_python | |
tar xvf Python-$python_version.tar.xz | |
cd Python-$python_version/ | |
# TODO: esto hay que actualizarlo instalando openssl 1.1.1 y compilando con esa instalacion: https://help.dreamhost.com/hc/en-us/articles/360001435926-Installing-OpenSSL-locally-under-your-username y ./configure --prefix=$HOME/opt/python-$python_version --enable-optimizations --with-openssl=$HOME/openssl | |
./configure --prefix=$HOME/opt/python-$python_version --enable-optimizations | |
make | |
make install | |
cd .. | |
# TODO: Acá se podra mejorar el shell | |
echo "export PATH=\$HOME/opt/python-$python_version/bin:\$PATH" >> ~/.bash_profile | |
. ~/.bash_profile | |
else | |
echo "Python 3 already installed. Version: $version" | |
python_version=$version | |
fi | |
# - Clonar repo | |
mv $sitiourl $sitiourl".delete" | |
echo "Cloning the repository" | |
git clone $repourl $sitiourl | |
# - crear virtualenv | |
echo "Creating virtualenv" | |
python3 -m venv ~/$sitiourl/env/ | |
# - activar virtualenv | |
echo "Activating virtualenv" | |
source ~/$sitiourl/env/bin/activate | |
# - instalar requeriments.txt | |
echo "installing requeriments.txt" | |
pip3 install -r ~/$sitiourl/sitio/requirements.txt | |
# - crear settings-local | |
echo "ADMINS = (" > ~/$sitiourl/sitio/sitio/settings_local.py | |
echo " (u'Contacto $sitiourl', 'contacto@$sitiourl')," >> ~/$sitiourl/sitio/sitio/settings_local.py | |
echo ")" >> ~/$sitiourl/sitio/sitio/settings_local.py | |
echo "" >> ~/$sitiourl/sitio/sitio/settings_local.py | |
echo "DATABASES = {" >> ~/$sitiourl/sitio/sitio/settings_local.py | |
echo " 'default': {" >> ~/$sitiourl/sitio/sitio/settings_local.py | |
echo " 'ENGINE': 'django.db.backends.mysql'," >> ~/$sitiourl/sitio/sitio/settings_local.py | |
echo " 'NAME': '$databasename'," >> ~/$sitiourl/sitio/sitio/settings_local.py | |
echo " 'USER': '$databaseuser'," >> ~/$sitiourl/sitio/sitio/settings_local.py | |
echo " 'PASSWORD': '$databasepassword'," >> ~/$sitiourl/sitio/sitio/settings_local.py | |
echo " 'HOST': '$databasehost'," >> ~/$sitiourl/sitio/sitio/settings_local.py | |
echo " 'OPTIONS': {" >> ~/$sitiourl/sitio/sitio/settings_local.py | |
echo " 'sql_mode': 'STRICT_ALL_TABLES'," >> ~/$sitiourl/sitio/sitio/settings_local.py | |
echo " }," >> ~/$sitiourl/sitio/sitio/settings_local.py | |
echo " }" >> ~/$sitiourl/sitio/sitio/settings_local.py | |
echo "}" >> ~/$sitiourl/sitio/sitio/settings_local.py | |
# - migrar | |
python3 ~/$sitiourl/sitio/manage.py migrate | |
# - crear archivo passenger | |
echo "import sys, os" > ~/$sitiourl/passenger_wsgi.py | |
echo "INTERP = os.path.join(os.environ['HOME'], '$sitiourl', 'env', 'bin', 'python3')" >> ~/$sitiourl/passenger_wsgi.py | |
echo "" >> ~/$sitiourl/passenger_wsgi.py | |
echo "if sys.executable != INTERP:" >> ~/$sitiourl/passenger_wsgi.py | |
echo " os.execl(INTERP, INTERP, *sys.argv)" >> ~/$sitiourl/passenger_wsgi.py | |
echo "sys.path.append(os.getcwd())" >> ~/$sitiourl/passenger_wsgi.py | |
echo "" >> ~/$sitiourl/passenger_wsgi.py | |
echo "# add your project directory to the sys.path" >> ~/$sitiourl/passenger_wsgi.py | |
echo "" >> ~/$sitiourl/passenger_wsgi.py | |
echo "project_home = os.path.join(os.environ['HOME'], '$sitiourl', 'sitio')" >> ~/$sitiourl/passenger_wsgi.py | |
echo "" >> ~/$sitiourl/passenger_wsgi.py | |
echo "if project_home not in sys.path:" >> ~/$sitiourl/passenger_wsgi.py | |
echo "" >> ~/$sitiourl/passenger_wsgi.py | |
echo " sys.path.append(project_home)" >> ~/$sitiourl/passenger_wsgi.py | |
echo "" >> ~/$sitiourl/passenger_wsgi.py | |
echo "# set environment variable to tell django where your settings.py is" >> ~/$sitiourl/passenger_wsgi.py | |
echo "" >> ~/$sitiourl/passenger_wsgi.py | |
echo "os.environ['DJANGO_SETTINGS_MODULE'] = 'sitio.settings'" >> ~/$sitiourl/passenger_wsgi.py | |
echo "" >> ~/$sitiourl/passenger_wsgi.py | |
echo "# serve django via WSGI" >> ~/$sitiourl/passenger_wsgi.py | |
echo "" >> ~/$sitiourl/passenger_wsgi.py | |
echo "# Django >= 1.7" >> ~/$sitiourl/passenger_wsgi.py | |
echo "from django.core.wsgi import get_wsgi_application" >> ~/$sitiourl/passenger_wsgi.py | |
echo "application = get_wsgi_application()" >> ~/$sitiourl/passenger_wsgi.py | |
# - crear superusuario | |
python3 ~/$sitiourl/sitio/manage.py createsuperuser </dev/tty | |
# - crear carpeta public con static y media | |
# no lo hacemos, que lo haga el programador del sitio | |
# - crear datos iniciales | |
# a futuro | |
# - reiniciar server | |
mkdir ~/$sitiourl/tmp | |
touch ~/$sitiourl/tmp/restart.txt | |
echo "Done" | |
# TODO: Deberiamos borrar el archivo de creacion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment