├── .gitignore ├── .idea └── vcs.xml ├── LICENSE ├── README.md ├── aws ├── main.py └── requirements.txt ├── create_swap.sh ├── init_env_setup.sh └── project.service /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | 106 | # idea files 107 | .idea/* -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Apon Shahriar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # misc-scripts 2 | **Miscellaneous scripts** 3 | 4 | This scripts will help to make your life easy by automating some essential installation and environment setup. 5 | This scripts are meant to setup the environment and your supplied django project in an UBUNTU machine. 6 | 7 | # Django project setup in ubuntu mechine 8 | 9 | `init_env_setup.sh` is a script to initialize and setup the environment needed to run a django project. 10 | 11 | To run `init_env_setup.sh` 12 | 13 | `./init_env_setup.sh -p [PROJ_GIT_REPO] -u [PROJ_NAME]` 14 | 15 | It will install nginx, git, pip, gunicorn, virtualenv, Python3.6.4, pyenv 16 | 17 | Then clone the project ( you passed as parameter ) , Create a virtual environment for that project. 18 | Then install the requirements needed to run your project from `requirements.txt` inside the virtual environment. 19 | Your Project will be inside projects/ directory and virtual environment for your project will inside the venv 20 | directory. your virtual environment will be named as `venv_your_project_name` . 21 | 22 | After that a service and nginx configuation for you Application will be generated in the associated directory. 23 | 24 | # Create AWS server instances with boto3 25 | 26 | **Install requirements** 27 | 28 | `pip install -r requirements.txt` 29 | 30 | **Config AWS credintials** 31 | 32 | ``` 33 | sudo apt install awscli 34 | aws configure 35 | ``` 36 | Config with relevant info 37 | ``` 38 | AWS Access Key ID 39 | AWS Secret Access Key 40 | Default region name // In our case eu-central-1 41 | Default output format json 42 | ``` 43 | -------------------------------------------------------------------------------- /aws/main.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | client = boto3.client('lightsail') 3 | print(client.get_active_names()) -------------------------------------------------------------------------------- /aws/requirements.txt: -------------------------------------------------------------------------------- 1 | boto3==1.11.15 2 | -------------------------------------------------------------------------------- /create_swap.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | echo "This script creates a swap file for your machine." 3 | echo "swap_size = block_size X number_of_blocks" 4 | read -p "Enter Block size : (i.e. 256M,512M,1G,2G )" bs 5 | read -p "Enter Number of blocks : " cnt 6 | sudo swapoff -a 7 | sudo dd if=/dev/zero of=/swapfile bs=$bs count=$cnt 8 | sudo mkswap /swapfile 9 | sudo chmod 600 /swapfile 10 | sudo swapoff -a 11 | sudo swapon /swapfile 12 | echo "Your swap size : " 13 | grep SwapTotal /proc/meminfo 14 | echo "Making swapfile permanent : " 15 | sudo cp /etc/fstab /etc/fstab.back 16 | echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab 17 | -------------------------------------------------------------------------------- /init_env_setup.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | #........................Helper Functions......................# 4 | function find_wsgi_application() { 5 | val=$(find "$proj_loc" \( -name 'settings.py' -or -name 'local.py' -or -name 'development.py' -or -name 'production.py' \)) 6 | val1=$(cat $val | grep WSGI_APPLICATION | cut -d "=" -f 2) 7 | val2=$(echo $val1 | cut -b 1-) 8 | val3=("$val2") 9 | val4=$(echo $val3 | cut -c 2- | sed -e "s/.application'//g")":application" 10 | echo "$val4" 11 | } 12 | 13 | user_name=$(whoami) 14 | read -p "Project Name : " proj_name 15 | read -p "Project repository ( git@github.com:Shrek53/misc-scripts.git ): " proj_git 16 | read -p "Domain/IP (example.com): " domain 17 | 18 | echo "=========================Initiating environment setup=============================" 19 | sudo apt-get update 20 | sudo apt-get install nginx 21 | sudo apt-get install git 22 | sudo apt-get install python-pip 23 | sudo pip install --upgrade pip 24 | sudo pip install gunicorn 25 | sudo pip install virtualenv 26 | 27 | mkdir -p ~/projects 28 | cd ~/projects || exit 29 | 30 | echo "========= Did you add your ssh public key to the git repository ? (Y/N) ==========" 31 | read user_response 32 | if [[ $user_response == 'Y' || $user_response == 'y' ]]; then 33 | git clone "$proj_git" "$proj_name" 34 | cd ~/projects/"$proj_name" || exit 35 | proj_loc=$(pwd) 36 | cd .. 37 | else 38 | echo "========== Please add your ssh public key to the git repository ============" 39 | sleep 1 40 | exit 41 | fi 42 | 43 | cd ~ || exit 44 | sudo apt-get install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev 45 | curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash 46 | export PATH="/home/$user_name/.pyenv/bin:$PATH" 47 | eval "$(pyenv init -)" 48 | eval "$(pyenv virtualenv-init -)" 49 | pyenv install 3.6.9 50 | 51 | if [ ! -d "venv" ]; then 52 | mkdir -p ~/venv 53 | fi 54 | 55 | cd ~/venv || exit 56 | venv_name=venv_$proj_name 57 | pyenv local 3.6.9 58 | if [ ! -d $venv_name ]; then 59 | virtualenv -p python3.6 ~/venv/$venv_name 60 | fi 61 | 62 | cd ~/venv/"$venv_name" || exit 63 | venv_loc=$(pwd) 64 | echo $venv_loc 65 | source "$venv_loc"/bin/activate 66 | pip install gunicorn 67 | pip install -r "$proj_loc"/requirements.txt 68 | deactivate 69 | cd ~ || exit 70 | 71 | echo "==================== Create a service for your project ===================" 72 | echo "Enter service name" 73 | read service_name 74 | echo "Enter number of workers" 75 | read number_of_workers 76 | 77 | ##-----------------------------creating service file-----------------------------## 78 | wsgi_application=$(find_wsgi_application "$proj_loc") 79 | service_file_name=/etc/systemd/system/$service_name'.service' 80 | sudo touch $service_file_name 81 | 82 | sudo echo " 83 | [Unit] 84 | Description = $service_name 85 | After = network.target 86 | 87 | [Service] 88 | Restart=on-failure 89 | User = $user_name 90 | Group = www-data 91 | WorkingDirectory = $proj_loc 92 | Environment=\"PATH=$venv_loc/bin\" 93 | ExecStart = $venv_loc/bin/gunicorn --workers $number_of_workers --bind unix:$proj_loc/server.sock $wsgi_application --timeout 180 94 | 95 | [Install] 96 | WantedBy = multi-user.target 97 | " | sudo tee -a $service_file_name 98 | 99 | ##---------------------------creating nginx config file-------------------------## 100 | 101 | nginx_config_file_name=/etc/nginx/conf.d/$service_name'.conf' 102 | sudo touch $nginx_config_file_name 103 | 104 | sudo echo " 105 | server { 106 | listen 80; 107 | server_name $domain; 108 | 109 | location ^~ /static/ { 110 | root $proj_loc; 111 | } 112 | 113 | location = /favico.ico { 114 | root $proj_loc/favico.ico; 115 | } 116 | location / { 117 | include proxy_params; 118 | proxy_pass http://unix:$proj_loc/server.sock; 119 | } 120 | } 121 | " | sudo tee -a $nginx_config_file_name 122 | 123 | echo "==========================ALL SET========================" 124 | -------------------------------------------------------------------------------- /project.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description = proj_name 3 | After = network.target 4 | 5 | [Service] 6 | Restart=on-failure 7 | User = user_name 8 | Group = www-data 9 | WorkingDirectory = proj_loc 10 | Environment="PATH=venv_loc/bin" 11 | ExecStart = venv_loc/bin/gunicorn --workers 8 --bind unix:proj_loc/server.sock proj.wsgi:application --timeout 180 12 | 13 | [Install] 14 | WantedBy = multi-user.target --------------------------------------------------------------------------------