Prepare
Last time, installation method and a basic operation of fairy support run were introduced
A basic function is introduced using apache http server installation as an example this time
Please download a sample from here
Starting the execution environment
If you left vagrant running last time, shut down with vagrant halt
Move to the vagrant folder in the downloaded サンプル
Enter vagrant up
cd vagrant vagrant up
When you start VirtualBox Manager, you can see that two virtual environments are running
Initializing the environment
The virtual environment that was started is not configured for DNS, nor is yum updated, so will do using fairy support run
Move to folder fairysupport_run in the downloaded sample
Enter java -jar com_fairysupport_run.jar dns
Enter java -jar com_fairysupport_run.jar yum_init
cd fairysupport_run java -jar com_fairysupport_run.jar dns
java -jar com_fairysupport_run.jar yum_init
DNS was set by java -jar com_fairysupport_run.jar dns
In java -jar com_fairysupport_run.jar yum_init, yum update and necessary software were installed
Creating a shell
Let's make an apache http server install shell
Move to folder fairysupport_run in the downloaded sample
Create a folder called apache_test
Create a file called main.sh in the created apache_test folder
Make sure that CR is not included in the line feed code. Save as LF instead of CR+LF
#!/bin/bash sudo -S yum -y install httpd sudo -S firewall-cmd --permanent --zone=public --add-service=http sudo -S firewall-cmd --permanent --zone=public --add-service=https sudo -S firewall-cmd --reload sudo -S systemctl start httpd.service sudo -S systemctl enable httpd.service
Contents of main.sh
httpd is installed using yum
firewalld permits http and https
Start httpd
Enable httpd autostart
folder tree
|-- fairysupport_run | |-- apache_test | | `-- main.sh | |-- com_fairysupport_run.jar | |-- env.txt | `-- server.properties.local |-- vagrant | `-- Vagrantfile
Switching server.properties
server.properties was made last time, but a sample is a file as server.properties.local
Let's take a look at the contents of env.txt. It is written as local. If you prepare a file named env.txt, you can write the suffix of server.properties
If you prepare the files server.properties.local, server.properties.dev, server.properties.stg, server.properties.prd, you can switch the server.properties to use by changing the contents of env.txt
Run of fairy support run
I'll try
Move to the folder where com_fairysupport_run.jar is put
Enter java -jar com_fairysupport_run.jar apache_test
cd fairysupport_run java -jar com_fairysupport_run.jar apache_test
When you run it, you will see that the apache http server is installed in the output contents
Let's access http://localhost:8080 in a browser
Let's access http://localhost:8181 in a browser
I think both are displaying pages correctly
I was able to install apache http server in two environments
I've entered java -jar so far, but it's cumbersome to enter java -jar every time
fairysupport_run.bat and fairysupport_run.sh are under fairysupport_run in the sample
If this is used, it is not necessary to enter java -jar every time. Choose whether to use bat or sh depending on the OS you are using. It's possible to execute by the following.
fairysupport_run.bat apache_test
./fairysupport_run.sh apache_test
Upload file
After apache http server installed, you usually have to customize the configuration file
Prepare a configuration file and set the same configuration file for all servers
Copy httpd.conf under fairysupport_run/apache in the downloaded sample and paste it into the apache_test folder you created earlier
|-- fairysupport_run | |-- apache_test | | |-- httpd.conf | | `-- main.sh | |-- com_fairysupport_run.jar | |-- env.txt | `-- server.properties.local |-- vagrant | `-- Vagrantfile
Let's modify the shell so that it can set the configuration file
main.sh#!/bin/bash sudo -S yum -y install httpd sudo -S mv /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bk sudo -S \cp -f ./httpd.conf /etc/httpd/conf/httpd.conf sudo -S chmod 644 /etc/httpd/conf/httpd.conf sudo -S semanage fcontext -a -t httpd_config_t /etc/httpd/conf/httpd.conf sudo -S restorecon -v /etc/httpd/conf/httpd.conf sudo -S firewall-cmd --permanent --zone=public --add-service=http sudo -S firewall-cmd --permanent --zone=public --add-service=https sudo -S firewall-cmd --reload sudo -S systemctl restart httpd.service sudo -S systemctl enable httpd.service
Contents of main.sh
Backing up configuration files using mv
The file in apache_test is uploaded by runing A, so replace the uploaded configuration file using cp. The \ that comes before cp is intentionally added to enable the f option
Setting permissions with chmod
semanage and restorecon is handling SELinux
When fairy support run is executed in this state, httpd.conf is backed up and httpd.conf is replaced
Common processing
The process of making backup files, replacing files, and labeling files is frequently performed
Let's put it together in common process
Create a folder called common_test in the same hierarchy as apache_test
Create a file called common.sh in the created common_test folder
Make sure that CR is not included in the line feed code. Save as LF instead of CR+LF
bk_cp_mode_label() { sudo -S mv "${2}" "${2}.bk" sudo -S \cp -f "${1}" "${2}" sudo -S chmod ${3} "${2}" sudo -S semanage fcontext -a -t "${4}" "${2}" sudo -S restorecon -v "${2}" }
Contents of common.sh
Created the function to perform from backup to SELinux handle
Create a file called include.txt in the apache_test folder
The contents of include.txt is ../common_test
If you exist a file called include.txt, fairy support run will upload the folder written in include.txt at runtime
../common_test
Let's modify the shell to use common processing
main.sh#!/bin/bash . ../common_test/common.sh sudo -S yum -y install httpd bk_cp_mode_label "./httpd.conf" "/etc/httpd/conf/httpd.conf" 644 httpd_config_t sudo -S firewall-cmd --permanent --zone=public --add-service=http sudo -S firewall-cmd --permanent --zone=public --add-service=https sudo -S firewall-cmd --reload sudo -S systemctl restart httpd.service sudo -S systemctl enable httpd.service
Contents of main.sh
Read a file which the common function was written. . ../common_test/common.sh is added. It is a the dot, half-width space, relative path to common.sh. If you didn't write the dot before the half-width space, shell cannot use the common function
The function bk_cp_mode_label created in common.sh is called
folder tree
|-- fairysupport_run | |-- apache_test | | |-- httpd.conf | | |-- include.txt | | `-- main.sh | |-- common_test | | `-- common.sh | |-- com_fairysupport_run.jar | |-- env.txt | `-- server.properties.local |-- vagrant | `-- Vagrantfile
If you run fairy support run in this state, you can see that the common_test folder is also uploaded
Execution file creation and arguments
Next, let's pass the contents of env.txt to the shell and add a process to change httpd.conf according to the contents of env.txt
So far we have passed the folder name in the argument of fairy support run, but you can give the file name
Let's create a file that can be given arguments
Create a file called run_apache_test.txt in the same hierarchy as com_fairysupport_run.jar
The contents of run_apache_test.txt are the following
run_apache_test.txt
apache_test ${ENV} -f server.properties
Contents of run_apache_test.txt
If you write ${ENV}, ${ENV} becomes the contents of env.txt when fairy support run is executed
In addition to ${ENV}, the following special variables can be used
${integer}:argument, ${DATE}: Execution date, ${HH}: hour part of the execution time, ${MM}: minute part of the execution time, ${SS}: second part of the execution time
Specify the file containing server information with f option. If the f option is omitted, server.properties is specified
Next, copy httpd.conf.dev and httpd.conf.local under fairysupport_run/apache in the downloaded sample and paste them into the apache_test folder
Next, let's modify the shell
main.sh#!/bin/bash . ../common_test/common.sh sudo -S yum -y install httpd bk_cp_mode_label "./httpd.conf.${1}" "/etc/httpd/conf/httpd.conf" 644 httpd_config_t sudo -S firewall-cmd --permanent --zone=public --add-service=http sudo -S firewall-cmd --permanent --zone=public --add-service=https sudo -S firewall-cmd --reload sudo -S systemctl restart httpd.service sudo -S systemctl enable httpd.service
Contents of main.sh
Changed the first argument of bk_cp_mode_label to ./httpd.conf.${1}. Because ${ENV} was given to the first argument of apache_test, the contents of env.txt are stored in ${1}
folder tree
|-- fairysupport_run | |-- apache_test | | |-- httpd.conf.dev | | |-- httpd.conf.local | | |-- include.txt | | `-- main.sh | |-- common_test | | `-- common.sh | |-- com_fairysupport_run.jar | |-- env.txt | |-- run_apache_test.txt | `-- server.properties.local |-- vagrant | `-- Vagrantfile
Let's run. It can be executed by giving a file name including the extension as shown below
fairysupport_run.bat run_apache_test.txt
./fairysupport_run.sh run_apache_test.txt
httpd.conf.local is placed as httpd.conf under /etc/httpd/conf/
Idempotence
If you execute the above apache_test many times, everything described in the shell will be executed every time
Some There is The tool which automatically controls so that the contents of a shell are not executed each time in tool-specific notation and some There is The tool to describe if statement to prevent execution using YAML or JSON
fairy support run will write the if statement by myself. But this won't be a if statement of big deal. This can be achieved with a very simple if statement
You don't need to research the conditions under which the tool will execute the command, nor do you need to learn YAML or JSON tool-specific notation to prevent the shell from running
Let's actually write
yum_installed_exit() { if sudo -S yum list installed | grep "${1}" > /dev/null 2>&1; then echo "${2}" exit 0 fi } bk_cp_mode_label() { sudo -S mv "${2}" "${2}.bk" sudo -S \cp -f "${1}" "${2}" sudo -S chmod ${3} "${2}" sudo -S semanage fcontext -a -t "${4}" "${2}" sudo -S restorecon -v "${2}" }
The correction contents of common.sh
Added a function called yum_installed_exit
Just check if it is already installed using the yum list installed command, and if it is installed, just output a message and exit the shell
Let's put the function added above into the shell
main.sh#!/bin/bash . ../common_test/common.sh yum_installed_exit "httpd" "Apache HTTP SERVER is already installed" sudo -S yum -y install httpd bk_cp_mode_label "./httpd.conf.${1}" "/etc/httpd/conf/httpd.conf" 644 httpd_config_t sudo -S firewall-cmd --permanent --zone=public --add-service=http sudo -S firewall-cmd --permanent --zone=public --add-service=https sudo -S firewall-cmd --reload sudo -S systemctl restart httpd.service sudo -S systemctl enable httpd.service
The correction contents of main.sh
Added a call to yum_installed_exit
If httpd is already installed, the line below yum_installed_exit will not be executed
I think this is enough, but let's create a function that will only run when other commands are needed
common.shyum_installed_exit() { if sudo -S yum list installed | grep "${1}" > /dev/null 2>&1; then echo "${2}" exit 0 fi } yum_install() { if ! sudo -S yum list installed | grep "${1}" > /dev/null 2>&1; then sudo -S yum -y install "${2}" fi } bk_cp_mode_label() { if ! sudo -S diff "${1}" "${2}" > /dev/null 2>&1; then sudo -S mv "${2}" "${2}.bk" sudo -S \cp -f "${1}" "${2}" sudo -S chmod ${3} "${2}" sudo -S semanage fcontext -a -t "${4}" "${2}" sudo -S restorecon -v "${2}" fi } systemctl_start() { if sudo -S systemctl status "${1}" | grep "Active: inactive" > /dev/null 2>&1; then sudo -S systemctl start "${1}.service" fi } systemctl_enable() { if sudo -S systemctl is-enabled "${1}" | grep disabled > /dev/null 2>&1; then sudo -S systemctl enable "${1}.service" fi } firewalld_add_service() { if ! echo " `sudo -S firewall-cmd --list-services --permanent --zone=public` " | grep " ${1} " > /dev/null 2>&1; then sudo -S firewall-cmd --permanent --zone=public --add-service="${1}" sudo -S firewall-cmd --reload fi }
The correction contents of common.sh
Added a function called yum_install. yum_install is a function that performs a yum install if the installation has not yet been done using yum
Modified a function called bk_cp_mode_label. Modified so that processing is executed only when the contents of the copy source file and the copy destination file are different
Added a function called systemctl_start. This is the function that start the service if it is stopped
Added a function called systemctl_enable. This function set the service to auto start if the service does not auto start
Added a function called firewalld_add_service. This function adds a service to the public zone if the service is not in the public zone
Let's put the function added above into the shell
main.sh#!/bin/bash . ../common_test/common.sh yum_installed_exit "httpd" "Apache HTTP SERVER is already installed" yum_install httpd httpd bk_cp_mode_label "./httpd.conf.${1}" "/etc/httpd/conf/httpd.conf" 644 httpd_config_t firewalld_add_service http firewalld_add_service https systemctl_start httpd systemctl_enable httpd
The correction contents of main.sh
Modified to call the function added in common.sh
Let's run
fairysupport_run.bat run_apache_test.txt
./fairysupport_run.sh run_apache_test.txt
If httpd is already installed, the message "Apache HTTP SERVER is already installed" will be output and the process will be terminated
Divide server.properties
Two server information is described in server.properties.local and processing has been executed for two environments
However, http server installation is job on the only web server
On the other hand, add user or add group is job on the web server and the DB server
Let's divide server.properties by considering server1 described in server.properties.local as web server and server2 as DB server
server1.user=vagrant server1.password=vagrant server1.address=127.0.0.1 server1.port=2230 server1.keyPath=C:\\fairy_support_run_sample\\.vagrant\\machines\\vm1\\virtualbox\\private_key server1.passphrase=server_db.properties.local
server1.user=vagrant server1.password=vagrant server1.address=127.0.0.1 server1.port=2240 server1.keyPath=C:\\fairy_support_run_sample\\.vagrant\\machines\\vm2\\virtualbox\\private_key server1.passphrase=server_all.txt
server_web.properties server_db.propertiesrun_all_apache_test.txt
apache_test ${ENV} -i server_all.txtrun_apache_test.txt
apache_test ${ENV} -f server_web.propertiesfolder tree
|-- fairysupport_run | |-- apache_test | | |-- httpd.conf.dev | | |-- httpd.conf.local | | |-- include.txt | | `-- main.sh | |-- common_test | | `-- common.sh | |-- com_fairysupport_run.jar | |-- env.txt | |-- run_apache_test.txt | |-- run_all_apache_test.txt | |-- server_all.txt | |-- server_web.properties.local | `-- server_db.properties.local |-- vagrant | `-- Vagrantfile
Created 4 files server_web.properties.local, server_db.properties.local, server_all.txt, run_all_apache_test.txt
Modified run_apache_test.txt
In run_all_apache_test.txt, server_all.txt is specified with the i option. server_all.txt contains two properties file names
As you can see, you can pass the file written the divided properties name to fairy support run using the i option and run the shell on multiple servers
Let's run
fairysupport_run.bat run_all_apache_test.txt
./fairysupport_run.sh run_all_apache_test.txt
Shell runs on the servers written in server_web.properties.local and server_db.properties.local
fairysupport_run.bat run_apache_test.txt
./fairysupport_run.sh run_apache_test.txt
Shell runs on the only server written in server_web.properties.local
On the next page, we will introduce the functions of fairy support run using mysql dump
table of contents