Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. In Ambari Web, browse to Services and click the Actions button in the Service navigation area on the left.
  2. The "Add Services" wizard launches. You will see an option to include "My Test Service" (which is the <displayName> of the service as defined in the service metainfo.xml file).
  3. Select "My Test Service" and click Next.
  4. Select the hosts to install the Test Client and click Next.
  5. Once complete, the "My Test Service" will be available Service navigation area.
  6. If you want to add the Test Client to any hosts, you can browse to Hosts and navigate to a specific host and click "Add".

Example: Implementing a Custom Service

In this example, we will create a custom service called "SAMPLESRV", add it to an existing Stack definition and use the Ambari APIs to install/configure the service. This service includes a MASTER, SLAVE and CLIENT as well as a set of configurations.

Create and Add the Service
  1. On the Ambari Server, browse to the /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services directory. In this case, we will browse to the HDP 2.0 Stack definition.

    Code Block
    cd /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services
  2. Create a directory named /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/SAMPLESRV that will contain the service definition for SAMPLESRV.

    Code Block
    mkdir /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/SAMPLESRV
    cd /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/SAMPLESRV
  3. Browse to the newly created SAMPLESRV directory, create a metainfo.xml file that describes the new service. For example:

    Code Block
    <?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>
  4. In the above, my service name is "SAMPLESRV", and it contains:
    • one MASTER component "SAMPLESRV_MASTER"
    • one SLAVE component "SAMPLESRV_SLAVE"
    • one CLIENT component "SAMPLESRV_CLIENT"
  5. Next, let's create that command script. Create a directory for the command script /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/SAMPLESRV/package/scripts that we designated in the service metainfo.

    Code Block
    mkdir -p /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/SAMPLESRV/package/scripts
    cd /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/SAMPLESRV/package/scripts
  6. Browse to the scripts directory and create the .py command script files. For example:

    Code Block
    import sys
    from resource_management import *
    class Master(Script):
      def install(self, env):
        print 'Install 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';
      def configure(self, env):
        print 'Configure the Sample Srv Master';
    if __name__ == "__main__":
      Master().execute()
    Code Block
    import sys
    from resource_management import *
    class Slave(Script):
      def install(self, env):
        print 'Install 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';
      def configure(self, env):
        print 'Configure the Sample Srv Slave';
    if __name__ == "__main__":
      Slave().execute()
    Code Block
    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()
  7. Now, restart Ambari Server for this new service definition to be distributed to all the Agents in the cluster.

    Code Block
    ambari-server restart