ambari 添加自定義服務

1 告訴ambari添加什麼服務

/var/lib/ambari-server/resources/stacks/HDP/2.5/services

目錄下添加 服務名稱 比如TEST

然後在目錄下新建metainfo.xml 

cat /var/lib/ambari-server/resources/stacks/HDP/2.5/services/TEST/metainfo.xml

包括服務名稱 版本號 加載資源的路徑

<metainfo>

       <schemaVersion>2.0</schemaVersion>

       <services>

               <service>

                       <name>TEST</name>

                       <version>4.0</version>

                       <extends>common-services/TEST/4.0</extends>

               </service>

        </services>

</metainfo>

 

2 告訴ambari服務在哪怎麼加載

在/var/lib/ambari-server/resources/common-services下

新建 TEST/4.0 要和上面對應起來

.
└── TEST
    └── 4.0
        ├── configuration
        │   └── test-env.xml
        ├── metainfo.xml
        └── package
            └── scripts
                ├── params.py
                └── test-ccc.py

目錄結果如下 必須按照這個模板來 

gu裏面的內容也必須按照這個模板來

 

configuation/test-env.xml   配置引用參數

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
  <property>
    <name>test_user</name>
    <value>test</value>
    <property-type>USER</property-type>
    <description>user</description>
  </property>
 
  <property>
    <name>test_group</name>
    <value>test</value>
    <property-type>GROUP</property-type>
    <description>group</description>
  </property>
 
  <property>
    <name>test_base_dir</name>
    <value>/opt/test</value>
    <description>test base directory</description>
  </property>
 
  <property>
    <name>test_pid_dir</name>
    <value>/var/run/test</value>
    <description>test PID directory</description>
  </property>
</configuration>
 

 

metainfo.xml 多個組件多個component 

<?xml version="1.0"?>
<metainfo>
  <schemaVersion>2.0</schemaVersion>
  <services>
    <service>
      <name>test</name>
      <displayName>test</displayName>
      <comment>custom service</comment>
      <version>3.0</version>
      <components>
        <component>
          <name>test-ccc</name>
          <displayName>test-ccc</displayName>
          <category>MASTER</category>
          <cardinality>1</cardinality>
          <commandScript>
            <script>scripts/test-ccc.py</script>
            <scriptType>PYTHON</scriptType>
            <timeout>1200</timeout>
          </commandScript>
        </component>

      </components>
      <osSpecifics>
        <osSpecific>
          <osFamily>any</osFamily>
        </osSpecific>
      </osSpecifics>
    </service>
  </services>
</metainfo>
 

test-ccc.py 組件安裝啓動等

#! /usr/bin/env python
import sys
import os
import glob
import pwd
import grp
import signal
import time
from resource_management import *
 
class Master(Script):
 
    # Install testsearch
    def install(self, env):
        # Import properties defined in -config.xml file from the params class
        import params
 
        # This allows us to access the params.test_pid_file property as
        # format('{test_pid_file}')
        env.set_params(params)
 
        # Install dependent packages
        self.install_packages(env)
 
        # Create user and group for test if they don't exist
        try: grp.getgrnam(params.test_group)
        except KeyError: Group(group_name=params.test_group)
 
        try: pwd.getpwnam(params.test_user)
        except KeyError: User(username=params.test_user,
                              gid=params.test_group,
                              groups=[params.test_group],
                              ignore_failures=True
                              )
 
        # Create test directories
        Directory([params.test_base_dir, params.test_pid_dir],
                  mode=0755,
                  cd_access='a',
                  owner=params.test_user,
                  group=params.test_group,
                  create_parents=True
                  )
 
 
        # Download testsearch
        cmd = format("cd {test_base_dir};wget {test_actualTimeMonitor_download}")
        Execute(cmd, user="root")
 
        cmd1 = format("cd {test_base_dir}; unzip test-actualTimeMonitor.zip")
        Execute(cmd1, user="root")
 
        cmd2 = format("cd {test_base_dir}; rm test-actualTimeMonitor.zip")
        Execute(cmd2, user=params.test_user)
 
        Execute('echo "Install complete"')
 
 
    def configure(self, env):
        # Import properties defined in -config.xml file from the params class
        import params
 
        # This allows us to access the params.test_pid_file property as
        # format('{test_pid_file}')
        env.set_params(params)
 
        Execute('echo "Configuration complete"')
 
    def stop(self, env):
        # Import properties defined in -config.xml file from the params class
        import params
 
        # Import properties defined in -env.xml file from the status_params class
        import status_params
 
        # This allows us to access the params.test_pid_file property as
        #  format('{test_pid_file}')
        env.set_params(params)
 
        # Stop testsearch
        cmd = format("kill  -9 `cat {test_actualTimeMonitor_pid_file}`")
        Execute(cmd, user="root")
 
 
    def start(self, env):
        # Import properties defined in -config.xml file from the params class
        import params
 
        # This allows us to access the params.test_pid_file property as
        #  format('{test_pid_file}')
        env.set_params(params)
 
        # Configure testsearch
        self.configure(env)
 
        # Start testsearch
        cmd = format("cd {test_base_dir};sh test-actualTimeMonitor/start.sh {test_actualTimeMonitor_pid_file}")
        Execute(cmd, user="root")
 
 
    def status(self, env):
        # Import properties defined in -env.xml file from the status_params class
        import status_params
 
        # This allows us to access the params.test_pid_file property as
        #  format('{test_pid_file}')
        env.set_params(status_params)
 
        #try:
        #    pid_file = glob.glob(status_params.test_pid_file)[0]
        #except IndexError:
        #    pid_file = ''
 
        # Use built-in method to check status using pidfile
        check_process_status(status_params.test_actualTimeMonitor_pid_file)
 
if __name__ == "__main__":
    Master().execute()

 

param.py  參數引用

#! /usr/bin/env python
from resource_management import *
import os
 
# config object that holds the configurations declared in the -config.xml file
config = Script.get_config()
 
java64_home = config['hostLevelParams']['java_home']
 
hostname = config['hostname']
 
test_user = config['configurations']['test-env']['test_user']
test_group = config['configurations']['test-env']['test_group']
 
test_base_dir = config['configurations']['test-env']['test_base_dir']
 
test_pid_dir = config['configurations']['test-env']['test_pid_dir']

test_ccc_pid_file = format("{test_pid_dir}/ccc.pid")
 
test_ccc_download = 'http://127.0.0.1/test-ccc.zip'

 

注意全部的配置項對大小寫以及下劃線特別敏感,會無法執行

所以建議  小寫 和_ 下劃線
 

 

 

 

發佈了338 篇原創文章 · 獲贊 27 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章