Versions Compared

Key

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

...

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 configurationscomponents.

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/TESTSRV that will contain the service definition for TESTSRV.

    Code Block
    mkdir /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/TESTSRV
    cd /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/TESTSRV
  3. Browse to the newly created TESTSRV 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>TESTSRV</name>
                <displayName>New Test Service</displayName>
                <comment>A New Test Service</comment>
                <version>0.1.0</version>
                <components>
                    <component>
                        <name>TEST_CLIENT</name>
                        <displayName>New Test Client</displayName>
                        <category>CLIENT</category>
                        <commandScript>
                            <script>scripts/test_client.py</script>
                            <scriptType>PYTHON</scriptType>
                            <timeout>600</timeout>
                        </commandScript>
                        <customCommands>
                          <customCommand>
                            <name>SOMETHINGCUSTOM</name>
                            <commandScript>
                              <script>scripts/test_client.py</script>
                              <scriptType>PYTHON</scriptType>
                              <timeout>600</timeout>
                            </commandScript>
                          </customCommand>
                        </customCommands>
                    </component>
                </components>
                <osSpecifics>
                    <osSpecific>
                        <osFamily>any</osFamily>
                    </osSpecific>
                </osSpecifics>
            </service>
        </services>
    </metainfo>
  4. In the above, my service name is "TESTSRV", and it contains one component "TEST_CLIENT" that is of component category "CLIENT". That client is managed via the command script scripts/test_client.py. Next, let's create that command script.
  5. Create a directory for the command script /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/TESTSRV/package/scripts that we designated in the service metainfo.

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

    Code Block
    import sys
    from resource_management import *
    
    class TestClient(Script):
      def install(self, env):
        print 'Install the client';
      def configure(self, env):
        print 'Configure the client';
      def somethingcustom(self, env):
        print 'Something custom';
    
    if __name__ == "__main__":
      TestClient().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
Install the Service

...

(via the Ambari REST API)
  1. Add the Service to the Cluster.

    Code Block
    POST
    /api/v1/clusters/MyCluster/services
    
    {
    "ServiceInfo": {
      "service_name":"TESTSRV"
      }
    }
  2. Add the Components to the Service. In this case, add TEST_CLIENT to TESTSRV.

    Code Block
    POST
    /api/v1/clusters/MyCluster/services/TESTSRV/components/TEST_CLIENT
    
  3. Install the component on all target hosts. For example, to install on c6402.ambari.apache.org and c6403.ambari.apache.org, first create the host_component resource on the hosts using POST.

    Code Block
    POST
    /api/v1/clusters/MyCluster/hosts/c6402.ambari.apache.org/host_components/TEST_CLIENT
    
    POST
    /api/v1/clusters/MyCluster/hosts/c6403.ambari.apache.org/host_components/TEST_CLIENT
  4. Now have Ambari install the components on all hosts. In this single command, you are instructing Ambari to install all components related to the service. This call the install() method in the command script on each host.

    Code Block
    PUT
    /api/v1/clusters/MyCluster/services/TESTSRV
    
    {
      "RequestInfo": {
        "context": "Install Test Srv Client"
      },
      "Body": {
        "ServiceInfo": {
          "state": "INSTALLED"
        }    
      }
    }    
    
  5. Alternatively, instead of installing all components at the same time, you can explicitly install each host component. In this example, we will explicitly install the TEST_CLIENT on c6402.ambari.apache.org:

    Code Block
    PUT
    /api/v1/clusters/MyCluster/hosts/c6402.ambari.apache.org/host_components/TEST_CLIENT
    
    {
      "RequestInfo": {
        "context":"Install Test Srv Client"
      },
      "Body": {
        "HostRoles": {
          "state":"INSTALLED"
        }
      }
    }
  6. Use the following to configure the client on the host. This will end up calling the configure() method in the command script.

    Code Block
    POST
    /api/v1/clusters/MyCluster/requests
     
    {
      "RequestInfo" : {
        "command" : "CONFIGURE",
        "context" : "Config Test Srv Client"
      },
      "Requests/resource_filters": [{
        "service_name" : "TESTSRV",
        "component_name" : "TEST_CLIENT",
        "hosts" : "c6403.ambari.apache.org"
      }]
    }
  7. If you want to see which hosts the component is installed.

    Code Block
    GET
    /api/v1/clusters/MyCluster/components/TEST_CLIENT
    
Install the Service

...

(via Ambari Web "Add Services")
Note

This ability to add custom services via Ambari Web is new as of Ambari 1.7.0.

  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 "My 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 "My Test Client" to any hosts, you can browse to Hosts and navigate to a specific host and click "+ Add".