Ambari Custom Service 1 - 步骤概述

自定义服务包含以下步骤

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目录,或是/var/lib/ambari-server/resources/stacks/HDP

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):
      
      # 在这里调用self.configure,便于在安装后修改参数时使用。

    print 'Start the Sample Srv Master';

  def status(self, env):

    print 'Status of the Sample Srv Master';

if __name__ == "__main__":

  Master().execute()
  • 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()
  • 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

  • 修改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>
  • 创建配置目录SAMPLESRV/configuration
mkdir -p configuration

cd configuration
  • 创建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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值