From f395d1d310e80ec3d45f0ee3dd7e4579f0df93ae Mon Sep 17 00:00:00 2001 From: Vishal Mishra Date: Mon, 12 Feb 2024 18:23:48 +0530 Subject: [PATCH] Initial Vagrant Setup --- .custom.zshrc | 12 ++++++ .gitignore | 1 + README.md | 30 ++++++++++++++ Vagrantfile | 25 ++++++++++++ Vagrantfile.backup | 77 ++++++++++++++++++++++++++++++++++++ auto_activate_venv_script.sh | 28 +++++++++++++ install.sh | 71 +++++++++++++++++++++++++++++++++ zsh_setup.sh | 55 ++++++++++++++++++++++++++ 8 files changed, 299 insertions(+) create mode 100644 .custom.zshrc create mode 100644 .gitignore create mode 100644 README.md create mode 100644 Vagrantfile create mode 100644 Vagrantfile.backup create mode 100755 auto_activate_venv_script.sh create mode 100644 install.sh create mode 100755 zsh_setup.sh diff --git a/.custom.zshrc b/.custom.zshrc new file mode 100644 index 0000000..a7f52c3 --- /dev/null +++ b/.custom.zshrc @@ -0,0 +1,12 @@ +ZSH_THEME=powerlevel10k/powerlevel10k + +plugins=( + git + zsh-autosuggestions + zsh-syntax-highlighting +) + +# This makes the oh-my-zsh script run twice when the shell is started. +# Not good but it works and we don't have to touch the default .zshrc file +# The idea being that you set some variables and then run the oh-my-zsh script. +source $ZSH/oh-my-zsh.sh \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..94695ea --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vagrant/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..879ea5b --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# Vagrant Box + +This Vagrant box is configured with the following features: + +## Operating System + +- Ubuntu 22.04 LTS + +## Python + +The box comes with multiple versions of Python pre-installed: + +- Python 3.10 +- Python 3.11 +- Python 3.12 + +## Shell + +The default shell is Zsh, with the powerlevel10k theme for a rich, informative prompt. + +Configure your theme in the first run. + +## Python Virtual Environments + +Python virtual environments are automatically activated when you navigate into a directory containing one. To take advantage of this feature, create your virtual environments in the `~/.virtualenvs` directory. + +## Getting Started + +To start using this Vagrant box, you need to have [Vagrant](https://www.vagrantup.com/) and [VirtualBox](https://www.virtualbox.org/) installed on your machine. Once you have those, you can clone this repository and run `vagrant up` in the project directory. + diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 0000000..ef908af --- /dev/null +++ b/Vagrantfile @@ -0,0 +1,25 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +Vagrant.configure("2") do |config| + config.vm.box = "ubuntu/jammy64" + + config.vm.network "forwarded_port", guest: 80, host: 8080 + config.vm.network "forwarded_port", guest: 8000, host: 8000 + + config.vm.synced_folder "~/workspace", "/workspace", type: "rsync", + rsync__exclude: ["node_modules/", "tmp/"] + + config.vm.provider "virtualbox" do |vb| + vb.gui = false + vb.memory = "8192" # 8GB + vb.cpus = 8 + end + + # SSH Setup + # config.ssh.private_key_path = "~/.ssh/vagrant_vm" + # config.ssh.insert_key = false + + # Run the install.sh script after the VM is up + config.vm.provision :shell, path: "install.sh", privileged: false +end \ No newline at end of file diff --git a/Vagrantfile.backup b/Vagrantfile.backup new file mode 100644 index 0000000..3a7aa5f --- /dev/null +++ b/Vagrantfile.backup @@ -0,0 +1,77 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +# All Vagrant configuration is done below. The "2" in Vagrant.configure +# configures the configuration version (we support older styles for +# backwards compatibility). Please don't change it unless you know what +# you're doing. +Vagrant.configure("2") do |config| + # The most common configuration options are documented and commented below. + # For a complete reference, please see the online documentation at + # https://docs.vagrantup.com. + + # Every Vagrant development environment requires a box. You can search for + # boxes at https://vagrantcloud.com/search. + config.vm.box = "ubuntu/jammy64" + + # Disable automatic box update checking. If you disable this, then + # boxes will only be checked for updates when the user runs + # `vagrant box outdated`. This is not recommended. + # config.vm.box_check_update = false + + # Create a forwarded port mapping which allows access to a specific port + # within the machine from a port on the host machine. In the example below, + # accessing "localhost:8080" will access port 80 on the guest machine. + # NOTE: This will enable public access to the opened port + # config.vm.network "forwarded_port", guest: 80, host: 8080 + + # Create a forwarded port mapping which allows access to a specific port + # within the machine from a port on the host machine and only allow access + # via 127.0.0.1 to disable public access + # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" + + # Create a private network, which allows host-only access to the machine + # using a specific IP. + # config.vm.network "private_network", ip: "192.168.33.10" + + # Create a public network, which generally matched to bridged network. + # Bridged networks make the machine appear as another physical device on + # your network. + # config.vm.network "public_network" + + # Share an additional folder to the guest VM. The first argument is + # the path on the host to the actual folder. The second argument is + # the path on the guest to mount the folder. And the optional third + # argument is a set of non-required options. + # config.vm.synced_folder "../data", "/vagrant_data" + + # Disable the default share of the current code directory. Doing this + # provides improved isolation between the vagrant box and your host + # by making sure your Vagrantfile isn't accessible to the vagrant box. + # If you use this you may want to enable additional shared subfolders as + # shown above. + # config.vm.synced_folder ".", "/vagrant", disabled: true + + # Provider-specific configuration so you can fine-tune various + # backing providers for Vagrant. These expose provider-specific options. + # Example for VirtualBox: + # + # config.vm.provider "virtualbox" do |vb| + # # Display the VirtualBox GUI when booting the machine + # vb.gui = true + # + # # Customize the amount of memory on the VM: + # vb.memory = "1024" + # end + # + # View the documentation for the provider you are using for more + # information on available options. + + # Enable provisioning with a shell script. Additional provisioners such as + # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the + # documentation for more information about their specific syntax and use. + # config.vm.provision "shell", inline: <<-SHELL + # apt-get update + # apt-get install -y apache2 + # SHELL +end diff --git a/auto_activate_venv_script.sh b/auto_activate_venv_script.sh new file mode 100755 index 0000000..1dbc976 --- /dev/null +++ b/auto_activate_venv_script.sh @@ -0,0 +1,28 @@ +function activate_venv() { + # Activate the virtual environment in the current directory + local env_name=${1:-$(basename $(pwd))} + + if [ -d "./venv" ]; then + # Check venv first + source "./venv/bin/activate" + elif [ -d "./.venv" ]; then + # Check .venv next + source "./.venv/bin/activate" + elif [ -d "$HOME/.virtualenvs/$env_name" ]; then + # Check $HOME/.virtualenvs/$env_name + source "$HOME/.virtualenvs/$env_name/bin/activate" + elif [ -n "$VIRTUALENV_DIR" ] && [ -d "$VIRTUALENV_DIR/$env_name" ]; then + # Check $VIRTUALENV_DIR/$env_name + source "$VIRTUALENV_DIR/$env_name/bin/activate" + elif [ -n "$VIRTUAL_ENV" ]; then + # Check $VIRTUAL_ENV + parentdir="$(dirname "$VIRTUAL_ENV")" + if [[ "$PWD"/ != "$parentdir"/* ]]; then + deactivate + fi + fi +} + +function cd() { + builtin cd "$@" && activate_venv +} \ No newline at end of file diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..2329dc1 --- /dev/null +++ b/install.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash + +sudo apt update +sudo apt upgrade +sudo apt dist-upgrade + +# Install required packages +echo "Installing required packages" +sudo apt install -y \ + zlib1g-dev build-essential \ + libncursesw5-dev libssl-dev \ + libsqlite3-dev tk-dev \ + libgdbm-dev libc6-dev libbz2-dev \ + python3-setuptools \ + python3-pip \ + python3-venv \ + openssl libffi-dev \ + libssl-dev curl \ + nginx \ + locate \ + git + +# Install Java +echo "Installing Java" +sudo apt update -y && sudo apt install -y openjdk-11-jdk + +# Install pip packages +echo "Upgrading pip" +python3 -m pip install --upgrade pip + +# Install Python +echo "Installing Python" +sudo add-apt-repository ppa:deadsnakes/ppa -y +sudo apt update -y && sudo apt install -y python3.12 python3.11 + +# Install docker using snap +echo "Installing Docker" +sudo apt update -y && sudo apt install snapd -y +sudo snap install docker + +# Install Postgres +echo "Installing Postgres" +sudo apt -y update && sudo apt -y install libpq-dev postgresql postgresql-contrib + +# Check the status of the postgres service +echo "Checking the status of the postgres service ... " +sleep 5 +sudo systemctl status postgresql + +# Install Redis +echo "Installing Redis" +sudo apt update -y && sudo apt install redis-server -y + +# Done message +echo "Done installing all the required packages" + +# ZSH Setup +# if zsh_setup.sh present, run it +if [ -f /vagrant/zsh_setup.sh ]; then + echo "Running zsh_setup.sh" + chmod +x /vagrant/zsh_setup.sh + /vagrant/zsh_setup.sh +fi + +# clean up +echo "Cleaning up" +sudo apt update -y && sudo apt upgrade -y && sudo apt autoremove -y && sudo apt autoclean -y + +# Add scripts to default shell rc file: .bashrc +echo "Adding script to .bashrc" +echo "source /vagrant/auto_activate_venv_script.sh" >> ~/.bashrc \ No newline at end of file diff --git a/zsh_setup.sh b/zsh_setup.sh new file mode 100755 index 0000000..2a839ff --- /dev/null +++ b/zsh_setup.sh @@ -0,0 +1,55 @@ +# Some fonts +sudo apt-get -y install \ + fonts-dejavu-core \ + fonts-freefont-ttf \ + fonts-inconsolata \ + fonts-liberation \ + fonts-font-awesome \ + fonts-powerline + +# Some icons +sudo apt-get -y install \ + fonts-noto-color-emoji \ + fonts-noto-color-emoji-core \ + adwaita-icon-theme \ + humanity-icon-theme \ + faenza-icon-theme + +# Install Oh My Zsh +# Remove existing zsh and oh-my-zsh +echo "Removing existing zsh and oh-my-zsh" + +rm -rf ~/.oh-my-zsh +rm -rf ~/.zshrc +rm -rf ~/.p10k.zsh +rm -rf ~/powerlevel10k + +# Zsh with Oh My Zsh +sudo apt update -y +sudo apt install zsh -y + +# Change default shell to zsh for the vagrant user +echo "Changing default shell to zsh" +sudo usermod -s $(which zsh) vagrant + +sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" +git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions +git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting + +# ZSH Theme + +git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k + +# https://github.com/romkatv/powerlevel10k/blob/master/README.md#weird-things-happen-after-typing-source-zshrc +# git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ~/powerlevel10k +# echo 'source ~/powerlevel10k/powerlevel10k.zsh-theme' >>~/.zshrc +# Manually run ~/.oh-my-zsh/oh-my-zsh.sh if this does not work + +# Add scripts to default shell rc file: .zshrc +echo "Adding scripts to .zshrc" +echo "source /vagrant/auto_activate_venv_script.sh" >> ~/.zshrc +echo "source /vagrant/.custom.zshrc" >> ~/.zshrc + +# Configure Powerlevel10k +echo "Configuring Powerlevel10k" +echo "Run p10k configure to configure Powerlevel10k" \ No newline at end of file