This is the article on the 9th day of Engineer Advent Calendar 2019.
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.
This script automates a series of tasks to build a virtual environment using vagrant and virtualbox. As a flow
Two arguments are required to run the script. One is the name of the directory The other is the type of virtual machine.
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.
DIR_NAME=$1 cd $ROOT_DIR if [ ! -d "$DIR_NAME" ]; then mkdir $ROOT_DIR/$DIR_NAME fi
$ HOME / VM is assigned to the variable
$ HOME / VM stands for
C: \ Users \ username \ VM.
$ 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.
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.)
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
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
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.
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
\ $ 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.
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.
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.