[LINUX] Automate environment construction with Shell Script

This is the article on the 9th day of Engineer Advent Calendar 2019.

Introduction

I touched the script that backs up the company server and wanted to make something myself. Also, I think it's a waste for beginners to stumble when building an environment, so I'd like you to use this if you like. Source

I hope you will understand better if you read this article along with the source.

environment

OS: Windows10 I use Vagrant, VirtualBox, and Cmder so I can download it. Please install it.

Script description

This script automates a series of tasks to build a virtual environment using vagrant and virtualbox. As a flow

  1. Creating a directory
  2. Run vagrant init
  3. Rewriting Vagranfile
  4. Create the script required for vagrant provision
  5. Run vagrant up is.

Two arguments are required to run the script. One is the name of the directory The other is the type of virtual machine.

1. Creating a directory

First of all, create a file to put the Vagrantfile. In my case, I created a directory called C: \ Users \ username \ VM as the ROOT directory, and created the directory received as an argument in it.

setup.sh


DIR_NAME=$1

cd $ROOT_DIR
if [ ! -d "$DIR_NAME" ]; then
  mkdir $ROOT_DIR/$DIR_NAME
fi

Commentary

$ HOME / VM is assigned to the variable ROOT_DIR. This $ HOME / VM stands for C: \ Users \ username \ VM.

DIR_NAME=$1

This $ 1 is the first argument. If you execute the command sh setup.sh aaa, $ 1 will contain ʻaaa. Also, as we will see later, there are also $ 2 and $ 3`, which represent the second and third arguments, respectively.

if [ ! -d "$DIR_NAME" ]; then
	//processing
fi

The if statement here is to create the directory if there is no directory name received as an argument. -d checks for the existence of a directory.

2. Execute vagrant init and rewrite Vagrantfile

Run vagrant init in the directory you just created to create a Vagrantfile. Box is also specified at the time of init. The box I am using is bento / CentOS 7.5. (Please change here as appropriate.)

setup.sh


count_dir

cd $ROOT_DIR/$DIR_NAME

vagrant init bento/centos-7.5

if [ -f Vagrantfile ]; then
  sed -i '31a \  config.vm.network "forwarded_port" ,guest: 80, host: 8080, host_ip: "127.0.0.1"' Vagrantfile
  sed -i '35a \  config.vm.network "private_network", ip: "192.168.33.10"\n  config.vm.network :public_network, ip: "192.168.11.'$((100+$COUNT))'", bridged: "en1: Wi-Fi(AirPort)"' Vagrantfile
  sed -i '69a \  config.vm.provision "shell", :path => "provision.sh", :privileged => false' Vagrantfile
fi

Commentary

The Vagranfile is rewritten in the if statement. The top specifies the forward port. The IP address for the public network is specified in the middle. The IP address changes according to the number of directories obtained by the count_dir function. (I thought this feature was necessary at the beginning, but I didn't think about it.) Finally, I've added a line that uses vagrant provision.

3. Preparation of provision.sh

Finally, create provision.sh. In provision.sh, describe what you want to execute when vagrant up. In the script, there is a common.sh that describes the common command to be executed on any machine and a script that describes the command to be executed in each environment. Create provision.sh by combining this common.sh with the script to be executed in each environment.

setup.sh


case "$ENV_TYPE" in
    "dev") cp -p $DEV/development.sh provision.sh.org ;;
    *) ERROR_NUM=5
        get_errormessages
        exit ;;
esac

if [ -f provision.sh.org ]; then
	cp -p $COMMON/common.sh common.sh
	cp -p $COMMON/end.sh end.sh
	cat common.sh provision.sh.org end.sh > provision.sh
	rm common.sh provision.sh.org end.sh
fi

Commentary

\ $ ENV_TYPE contains the virtual machine type specified by the second argument. (Currently dev only) If \ $ ENV_TYPE is dev, the script will be copied from the specified folder.

5. Run vagrant up

All you have to do is run vagrant up. When vagrant up, execute the command described in provision.sh created earlier to set up the virtual machine.

Finally

The above is the general flow of the environment construction automation script created this time. I created this script for fun and study, but I hope that beginners who are stumbling on environment construction can enjoy programming with it.

I think it's difficult to understand with a childish sentence. If you have any questions, please leave a comment.

Recommended Posts

Automate environment construction with Shell Script
ML environment construction with Miniconda
Get started with Python! ~ ① Environment construction ~
ruby environment construction with aws EC2
Easy Jupyter environment construction with Cloud9
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
Using Chainer with CentOS7 [Environment construction]
pytorch @ python3.8 environment construction with pipenv
Data science environment construction with Docker
Environment construction with pyenv and pyenv-virtualenv
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
Vue.js + Flask environment construction memorandum ~ with Anaconda3 ~
Manage logrotate generations with a shell script.
A memo packed with RADEX environment construction
Let's get along with Python # 0 (Environment construction)
Django environment construction
DeepIE3D environment construction
Emacs-based environment construction
Python environment construction
Environment construction (python)
django environment construction
CodeIgniter environment construction
python environment construction
Python --Environment construction
Shell variables, environment variables
Python environment construction
Golang environment construction
python environment construction
Word2vec environment construction
Collecting information from Twitter with Python (Environment construction)
Environment construction with VSCode + Remote Container (Go / Application)
Analytical environment construction with Docker (jupyter notebook + PostgreSQL)
MacOS 10.11 environment construction: Powerline with Anaconda and Dein.vim
View today's weather forecast with a shell script
Erase & generate serial number files with shell script
[0] TensorFlow-GPU environment construction built with Anaconda on Ubuntu
Poetry-virtualenv environment construction with python of centos-sclo-rh ~ Notes
First python ① Environment construction with pythonbrew & Hello World !!
From Python environment construction to virtual environment construction with anaconda
Virtual environment construction with Docker + Flask (Python) + Jupyter notebook
Periodically execute python script with cron in venv environment
Environment construction: GCP + Docker
Django project environment construction
python windows environment construction
Go language environment construction
ConoHa environment construction memo
[Python] OpenCV environment construction with Docker (cv2.imshow () also works)
homebrew python environment construction
From Kafka to KSQL --Easy environment construction with docker
PyData related environment construction
Anaconda-4.2.0-python3 environment construction (Mac)
Python development environment construction
Data analysis environment construction with Python (IPython notebook + Pandas)
Shell script command replacement
Scraping with scrapy shell
YOLO v4 environment construction ①
Pepper-kun remote control environment construction with Docker + IPython Notebook
Python environment with docker-compose
pyenv + fish environment construction
Shell script special variables
python2.7 development environment construction