翻译:https://cwiki.apache.org/confluence/display/AMBARI/Defining+a+Custom+Service
自定义服务包含以下步骤:
1 创建服务文件夹
2 创建描述服务信息的Metainfo.xml
3 创建关于安装、配置、启动、停止等命令的脚本
4 给自定义服务添加配置
自定义服务定义完成以后重启server就能在添加服务中找到自定义服务
命令:ambari-server restart
实现自定义服务例子
在本例子中创建一个服务SAMPLESRV,包括三个组件MASTER, SLAVE 和CLIENT。
1 创建服务
在/ambari/ambari-server/src/main/resources/stacks/HDP/2.5/services/目录下创建SAMPLESRV目录
mkdir SAMPLESRV
cd SAMPLESRV
注意:文件夹名字不能和Ambari预定义的相同,比如HUE
2 在SAMPLESRV目录中创建metainfo.xml文件
内容如下
<?xml version="1.0"?>
<metainfo>
<schemaVersion>2.0</schemaVersion>
<services>
<service>
<name>SAMPLESRV</name>
<displayName>New Sample Service</displayName>
<comment>A New Sample Service</comment>
<version>1.0.0</version>
<components>
<component>
<name>SAMPLESRV_MASTER</name>
<displayName>Sample Srv Master</displayName>
<category>MASTER</category>
<cardinality>1</cardinality>
<commandScript>
<script>scripts/master.py</script>
<scriptType>PYTHON</scriptType>
<timeout>600</timeout>
</commandScript>
</component>
<component>
<name>SAMPLESRV_SLAVE</name>
<displayName>Sample Srv Slave</displayName>
<category>SLAVE</category>
<cardinality>1+</cardinality>
<commandScript>
<script>scripts/slave.py</script>
<scriptType>PYTHON</scriptType>
<timeout>600</timeout>
</commandScript>
</component>
<component>
<name>SAMPLESRV_CLIENT</name>
<displayName>Sample Srv Client</displayName>
<category>CLIENT</category>
<cardinality>1+</cardinality>
<commandScript>
<script>scripts/sample_client.py</script>
<scriptType>PYTHON</scriptType>
<timeout>600</timeout>
</commandScript>
</component>
</components>
<osSpecifics>
<osSpecific>
<osFamily>any</osFamily>
</osSpecific>
</osSpecifics>
</service>
</services>
</metainfo>
服务的metainfo.xml文件描述了服务信息,组件信息和执行命令的脚本。服务的组件有三种类型:MASTER,SLAVE和CLIENT。下表展示了每种类型必须支持的命令,也可以自己拓展命令。
组件类型 | 默认的生命周期命令 |
MASTER | install, start, stop, configure, status |
SLAVE | install, start, stop, configure, status |
CLIENT | install, configure, status |
SAMPLESRV_MASTER是MASTER类型的组件,命令的实现方法所在脚本是services/SAMPLESRV/package
/scripts/master.py。
脚本是PYTHON编写的,实现了默认的生命周期命令。
在metainfo中有一个选项设置configuration-dir,缺省默认值是configuration
也可以自己制定配置文件的存放位置
<configuration-dir>configuration</configuration-dir>
在
configuration-dependencies
部分设置配置依赖关系,可以针对服务全体或者组件部分设置添加。依赖的含义在于当
config-type更新后ambari自动重启相关的组件和服务。
<configuration-dependencies>
<config-type>core-site</config-type>
<config-type>hdfs-site</config-type>
</configuration-dependencies>
3 下一步创建命令脚本
1)创建目录
SAMPLESRV/package/scripts
mkdir -p package/scripts
cd package/scripts
2)在scripts目录下创建命令脚本
- master.py内容:
import sys
from resource_management import *
class Master(Script):
def install(self, env):
print 'Install the Sample Srv Master';
def configure(self, env):
print 'Configure the Sample Srv Master';
def stop(self, env):
print 'Stop the Sample Srv Master';
def start(self, env):
print 'Start the Sample Srv Master';
def status(self, env):
print 'Status of the Sample Srv Master';
if __name__ == "__main__":
Master().execute()
2 .slave.py内容
import sys
from resource_management import *
class Slave(Script):
def install(self, env):
print 'Install the Sample Srv Slave';
def configure(self, env):
print 'Configure the Sample Srv Slave';
def stop(self, env):
print 'Stop the Sample Srv Slave';
def start(self, env):
print 'Start the Sample Srv Slave';
def status(self, env):
print 'Status of the Sample Srv Slave';
if __name__ == "__main__":
Slave().execute()
3.sample_client.py
内容
import sys
from resource_management import *
class SampleClient(Script):
def install(self, env):
print 'Install the Sample Srv Client';
def configure(self, env):
print 'Configure the Sample Srv Client';
if __name__ == "__main__":
SampleClient().execute()
在metainfo.xml的client组件中添加自定义命令SOMETHINGCUSTOM
<component>
<name>SAMPLESRV_CLIENT</name>
<displayName>Sample Srv Client</displayName>
<category>CLIENT</category>
<cardinality>1+</cardinality>
<commandScript>
<script>scripts/sample_client.py</script>
<scriptType>PYTHON</scriptType>
<timeout>600</timeout>
</commandScript>
<customCommands>
<customCommand>
<name>SOMETHINGCUSTOM</name>
<commandScript>
<script>scripts/sample_client.py</script>
<scriptType>PYTHON</scriptType>
<timeout>600</timeout>
</commandScript>
</customCommand>
</customCommands>
</component>
在package/scripts/sample_client.py中添加命令对应的处理方法
import sys
from resource_management import *
class SampleClient(Script):
def install(self, env):
print 'Install the Sample Srv Client';
def configure(self, env):
print 'Configure the Sample Srv Client';
def somethingcustom(self, env):
print 'Something custom';
if __name__ == "__main__":
SampleClient().execute()
4 给自定义服务添加配置
在下面的例子中添加配置类型test-config
1)修改metainfo.xml
<component>
<name>SAMPLESRV_CLIENT</name>
<displayName>Sample Srv Client</displayName>
<category>CLIENT</category>
<cardinality>1+</cardinality>
<commandScript>
<script>scripts/sample_client.py</script>
<scriptType>PYTHON</scriptType>
<timeout>600</timeout>
</commandScript>
<configFiles>
<configFile>
<type>xml</type>
<fileName>test-config.xml</fileName>
<dictionaryName>test-config</dictionaryName>
</configFile>
</configFiles>
</component>
2 )创建配置目录SAMPLESRV/configuration
mkdir -p configuration
cd configuration
3) 创建test-config.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>some.test.property</name>
<value>this.is.the.default.value</value>
<description>This is a test description.</description>
</property>
<property>
<name>another.test.property</name>
<value>5</value>
<description>This is a second test description.</description>
</property>
</configuration>