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目錄

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()
  • 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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章