Original: HTTPS://GITHUB.COM/X113773/TESTALL/ISSUES/4
Problem:
Weblogic/tomcat Direct Remote Call via Java or call local shell or ansible to deploy new program + start stop service How to manage? What configuration information is required?
Adoption Scenario:
Deploy new program via [Ansible] (http://www.ansible.com.cn/) + start stop service;
Introduction to Java Code usage:
1.[ansiblehelper.java] (https://github.com/x113773/testall/blob/master/src/main/java/com/ansel/testall/ansible/ Ansiblehelper.java) is a tool class that receives externally transmitted parameters, replaces the hosts configuration, executes playbook scripts, and part of the code is as follows:
```
/**
* Complete Project automatic deployment of Tomcat or WebLogic (Tomcat supports multiple projects at the same time, WebLogic can only deploy one project at a time)
*
* @param ansiblesettingsmap
* This parameter needs to provide several key value pairs as follows:
* Ansiblesettingsmap.put ("Ansible_ssh_hosts_path"
*, "Native Hosts file path, with hosts filename");
* Ansiblesettingsmap.put ("Ansible_ssh_host", "Target host IP address");
* Ansiblesettingsmap.put ("Ansible_ssh_user", "Target host user name");
* Ansiblesettingsmap.put ("Ansible_ssh_pass", "Target host user password");
* @param playbookparammap
* For tomcat deployment, this parameter needs to provide several key-value pairs as follows:
* Playbookparammap.put ("Local_war_path", "The path of the war package deployed by this confidential, without file name");
* Playbookparammap.put ("Project_name_arr",
* String array format item (War package) name, without. War suffix});
* Playbookparammap.put ("tomcat", "target host Tomcat path, to tomcat/master directory");
* -----------------------------------------------------------------
* For WebLogic deployment, this parameter needs to provide several key-value pairs as follows:
* Playbookparammap.put ("user_name", "target host WebLogic user name");
* Playbookparammap.put ("password", "Target host WebLogic password");
* Playbookparammap.put ("Weblogic_path",
* "Target host WebLogic path, to oracle_home/");
* Playbookparammap.put ("Local_war_file", "War package path with the confidential deployment, with file name");
* Playbookparammap.put ("domain_name", "WebLogic Domain name");
* Playbookparammap.put ("SERVER_NAME", "WebLogic Service name");
* Playbookparammap.put ("Admin_url", "WebLogic service address, need to use IIOP agreement");
* Playbookparammap.put ("War_file_path", "Storage path of the target host War package");
* Playbookparammap.put ("project_name",
* "project name (i.e., war package name, without. War suffix)");
* @param Playbookpath
* Ansible Tomcat or WebLogic playbook path with file name
*/
public static void Dodeploy (Map<string, object> ansiblesettingsmap, map<string, object> Playbookparammap, String Playbookpath) {
Setanisblehosts (ANSIBLESETTINGSMAP);
String res = Doanisbleplaybook (Playbookparammap, Playbookpath);
System.out.println ("========== output =============");
System.out.println (RES);
}
```
2.[ansiblecontroller.java] (https://github.com/x113773/testall/blob/master/src/main/java/com/ansel/testall/ Ansible/ansiblecontroller.java) is a test example that invokes the above tool class, which contains the parameters required to execute playbook (some parameters are defined in [Application-development.properties] ( Https://github.com/x113773/testall/blob/master/src/main/resources/application-development.properties))
3.[filecontentreplacehelper.java] (https://github.com/x113773/testall/blob/master/src/main/java/com/ansel/ Testall/helper/filecontentreplacehelper.java) and [Execlinuxcmd.java] (https://github.com/x113773/testall/blob/ Master/src/main/java/com/ansel/testall/helper/execlinuxcmd.java) are the two tool classes that replace the text content of a file and know what you give to Linux commands.
Specific implementation steps:
1. Preparatory work:
Two Linux servers:
The primary server (this system needs to be deployed on this server), the required software environment:
Python, Ansible, JDK
From the server (target server), the required software environment:
Python, Tomcat, Weblogic, JDK
2. Configuration information:
master server ansible directory [hosts] (https://github.com/x113773/testall/blob/master/src/main/resources/public/ansible/hosts) file, [Tomcat.yaml] (HTTPS://GITHUB.COM/X113773/TESTALL/BLOB/MASTER/SRC/MAIN/RESOURCES/PUBLIC/ANSIBLE/TOMCAT.YAML) Files and [Weblogic.yaml] (Https://github.com/x113773/testall/blob/master/src/main/resources/public/ansible/weblogic.yaml) files.
1) The Hosts file is configured from the server's IP, port, user name, password and other information, such as:
' Host1 ansible_ssh_host=192.168.10.129 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass= ' 123456 ' #host_to _replace
`
2) The Tomcat.yaml file is a [playbook] (http://www.ansible.com.cn/docs/playbooks.html) (script) that defines the entire deployment process for Tomcat:
```
---
-Hosts:all
Tasks
-Name:stop Tomcat service #调用脚本shutdown_tomcat. SH, stop Tomcat services
Script: "/etc/ansible/shutdown_tomcat.sh"
-Name:remove old war file #删除旧的war包及文件
SHELL:CD {{"Tomcat}}WEBAPPS&&RM-RF {{item}}} {{Item}}.war
With_items: "{{Project_name_arr}}"
-Name:copy new War file #从主服务器拷贝新war包到从服务器
copy:src={{local_war_path}}{{Item}}.war dest={{Tomcat}}webapps/
With_items: "{{Project_name_arr}}"
-Name:start Tomcat Service #启动Tomcat
shell:chdir={{Tomcat}}bin/nohup./startup.sh &
```
On the command line, pass the parameter in {{parameter name}} to run the playbook:
' Ansible-playbook/etc/ansible/tomcat.yaml--extra-vars ' {"Tomcat": "/usr/local/tomcat8/", "Local_war_path": "/opt/ Store/"," Project_name_arr ": [" gr "," Hw2 "]}"
3) Weblogic.yaml defines the entire deployment process for WebLogic:
```
---
-Hosts:all
Tasks
-Name:remove old war file #删除从服务器上旧的war包
SHELL:CD {{War_file_path}}&&rm-rf {{project_name}}.war
-Name:copy new War file #拷贝主服务器上的war包到从服务器指定目录
copy:src={{Local_war_file}} dest={{War_file_path}}
-Name:start WebLogic Service #调用shell脚本, pass in the specified parameters, complete the start and stop services, deploy the project
Script: "/etc/ansible/start_weblogic.sh {{user_name}} {{password}}
{{Weblogic_path}} {{domain_name}} {{server_name}} {{Admin_url}}
{{War_file_path}} {{project_name}} '
```
[Shutdown_tomcat.sh] (https://github.com/x113773/testall/blob/master/src/main/resources/public/ansible/shutdown_tomcat.sh)
[Start_weblogic.sh] (https://github.com/x113773/testall/blob/master/src/main/resources/public/ansible/start_weblogic.sh)
Automatic deployment of projects using Ansible (Tomcat, Weblogic)