ambari集成工具

 

ambari配置插件,支持新组件的接入

1 组件集成说明

1.1 插件目录:

/var/lib/ambari-server/resources/stacks/HDP//services
我的版本是HDP2.5,所以插件可以在 /var/lib/ambari-server/resources/stacks/HDP/2.5/services 中定义。

1.2 代码结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|_ stacks
|_ <stack_name>
|_ <stack_version>
metainfo.xml
|_ hooks
|_ repos
repoinfo.xml
|_ services #插件目录
|_ <service_name> #组件名
metainfo.xml #组件基本信息的定义,包括名称、版本、使用的脚本等
metrics.json #
alerts.json #监控
|_ configuration #配置文件目录
{configuration files}
|_ package #脚本、模板等
{files, scripts, templates}

2 创建自己的组件

2.1 进入ambari server的组件目录,创建以下目录结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@master services]# tree /var/lib/ambari-server/resources/stacks/HDP/2.5/services/TOMCAT/
/var/lib/ambari-server/resources/stacks/HDP/2.5/services/TOMCAT/
├── alerts.json
├── configuration
│ ├── tomcat-ambari-config.xml
│ └── tomcat-bootstrap-env.xml
├── metainfo.xml
├── package
│ ├── scripts
│ │ ├── master.py
│ │ ├── params.py
│ │ ├── status_params.py
│ │ └── tomcat.py
│ └── templates
│ ├── server.xml.j2
│ └── tomcat.conf.j2
└── README.md

2.2 文件及其说明

1) metainfo.xml

说明:metainfo.xml主要定义了组件名称,组件类型,安装的服务信息以及执行的脚本信息等。
一般组件有以下几种类型,组件有很多类型,例如zookeeper,有Master和Client,而且都是多台,因此category里面需要两中都定义,cardinality属性是多少个,一般zk需要多个,那就是1+。
本处已tomcat为例,只设置了一个样例,而且属于master类型。
commandScript 是指明安装的脚本路径,因为ambari目前只支持python,因此类型都是python。
osSpecifics 里面设置操作系统和需要安装的软件名称,这里设置any是指支持任何操作系统,同时我们只安装tomcat软件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?xml version="1.0"?>
<metainfo>
<schemaVersion>2.0</schemaVersion>
<services>
<service>
<!-- Internal name for service (must be unique) -->
<name>TOMCAT</name>
<!-- display name in Ambari UI -->
<displayName>TOMCAT</displayName>
<!-- Description of service - will be displayed when user clicks add service -->
<comment>Apache Tomcat™ is an open source software implementation of the Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket technologies</comment>
<!-- Version of component-->
<version>7.0.69</version>
<components>
<!-- In this case, there is only one master component -->
<component>
<name>TOMCAT</name>
<displayName>Tomcat</displayName>
<category>MASTER</category>
<!-- how many of these components are allowed in a cluster -->
<cardinality>1</cardinality>
<!-- reference to (and details of) what script is to be used to install/stop/start/config the service -->
<commandScript>
<script>scripts/master.py</script>
<scriptType>PYTHON</scriptType>
<timeout>10000</timeout>
</commandScript>
</component>
</components>
<!-- what yum packages need to be installed -->
<osSpecifics>
<osSpecific>
<osFamily>any</osFamily>
<packages>
<package><name>tomcat</name></package>
</packages>
</osSpecific>
</osSpecifics>
<!-- names for config files (under configuration dir) -->
<configuration-dependencies>
<config-type>tomcat-config</config-type>
</configuration-dependencies>
<restartRequiredAfterChange>true</restartRequiredAfterChange>
</service>
</services>
</metainfo>

2) alerts.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
"TOMCAT": {
"service": [],
"TOMCAT": [
{
"name": "TOMCAT",
"label": "TOMCAT Process",
"description": "This host-level alert is triggered if the TOMCAT Master cannot be determined to be up.",
"interval": 1,
"scope": "HOST",
"source": {
"type": "PORT",
"uri": "{{tomcat-ambari-config/tomcat.port}}",
"default_port": 8088,
"reporting": {
"ok": {
"text": "TCP OK - {0:.3f}s response on port {1}"
},
"warning": {
"text": "TCP OK - {0:.3f}s response on port {1}",
"value": 1.5
},
"critical": {
"text": "Connection failed: {0} to {1}:{2}",
"value": 5.0
}
}
}
}
]
}
}

3) configuration/tomcat-ambari-config.xml

说明:configuration里面两个xml文件定义的是一些配置的信息,都是以name、value、description等形式的出现,比较好理解。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- Define configuration paramaters for service: property name, default value and description (shown as help text) -->
<!-- service log file -->
<property>
<name>tomcat.install_dir</name>
<value>/opt</value>
<description>Local dir where to install component. tomcat folder will be created as a subdir of this dir e.g. /opt/tomcat-3.0.0</description>
</property>
<property>
<name>tomcat.port</name>
<value>8088</value>
<description>Port tomcat runs on</description>
</property>
<property>
<name>tomcat.webapp.address</name>
<value>localhost:8088</value>
</property>
<property>
<name>tomcat.JAVA_OPTS</name>
<value>-Xms2048m -Xmx2048m -Xmn422m</value>
</property>
</configuration>

4) configuration/tomcat-bootstrap-env.xml

说明:configuration里面两个xml文件定义的是一些配置的信息,都是以name、value、description等形式的出现,比较好理解。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>tomcat_log_dir</name>
<value>/var/log/tomcat</value>
<description>Tomcat Log dir</description>
</property>
<property>
<name>tomcat_pid_dir</name>
<value>/var/run/tomcat</value>
<description>Dir containing process ID file</description>
</property>
<property>
<name>tomcat_user</name>
<value>tomcat</value>
<description>User tomcat daemon runs as</description>
</property>
<property>
<name>tomcat_group</name>
<value>tomcat</value>
<description>Tomcat group</description>
</property>
</configuration>

5) package/scripts/params.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env python
from resource_management import *
from resource_management.libraries.script.script import Script
import sys, os, glob
from resource_management.libraries.functions.default import default
# config object that holds the configurations declared in the config xml file
config = Script.get_config()
# params from tomcat-ambari-config
tomcat_port = config['configurations']['tomcat-ambari-config']['tomcat.port']
tomcat_JAVA_OPTS = config['configurations']['tomcat-ambari-config']['tomcat.JAVA_OPTS']
conf_dir = '/etc/tomcat'
# params from tomcat-bootstrap
tomcat_user = config['configurations']['tomcat-bootstrap-env']['tomcat_user']
tomcat_group = config['configurations']['tomcat-bootstrap-env']['tomcat_group']

6) package/scripts/status_params.py

1
2
3
4
5
6
7
#!/usr/bin/env/ python
from resource_management import *
import sys, os
config = Script.get_config()
tomcat_pid_dir = config['configurations']['tomcat-bootstrap-env']['tomcat_pid_dir']
tomcat_pid_file = tomcat_pid_dir + '/tomcat.pid'
说明:两个params文件都是获取前面的xml文件的属性,这里使用python变量的形式重新定义。

7) package/scripts/tomcat.py

说明:该文件是定义的tomcat函数,主要是把templates里面的jinjia2模板转换成配置文件,templates里面有变量,都是以这样的形式出来的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from resource_management import *
#from yaml_config import yaml_config
import sys

def tomcat():
import params
File(format("{conf_dir}/tomcat.conf"),
content=Template("tomcat.conf.j2"),
owner=params.tomcat_user,
group=params.tomcat_group
)
File(format("{conf_dir}/server.xml"),
content=Template("server.xml.j2"),
owner=params.tomcat_user,
group=params.tomcat_group
)

8) package/scripts/master.py

说明:该文件是meta.xml定义的指定运行脚本,主要作用是安装、配置、启动、停止等操作,因为大类都被定义过了,我们这里需要重构下就可以了。
因为我们使用的是yum的安装方式,因此这里的写法还是比较简单易懂的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python
from resource_management import *
from tomcat import tomcat
class Master(Script):
def install(self, env):
# Install packages listed in metainfo.xml
self.install_packages(env)
#if any other install steps were needed they can be added here
self.configure(env)
#if any other install steps were needed they can be added here
def configure(self, env):
import params
env.set_params(params)
tomcat()
#To stop the service, use the linux service stop command and pipe output to log file
def stop(self, env):
import params
env.set_params(params)
Execute('systemctl stop tomcat')
#To start the service, use the linux service start command and pipe output to log file
def start(self, env):
import params
env.set_params(params)
#这里需要configure,可以让配置生效
self.configure(env)
Execute('systemctl start tomcat')
#To get status of the, use the linux service status command
def status(self, env):
import params
Execute('systemctl status tomcat')
if __name__ == "__main__":
Master().execute()

完成以上代码的编写后,需要重启ambari-server生效
ambari-server restart


 评论