...
Code Block |
---|
<metainfo> <versions> <active>true</active> </versions> <extends>2.0.6</extends> </metainfo> |
Example: Implementing a Custom
...
Service
In this example, we will create a custom service called "TESTSRVSAMPLESRV", add it to an existing Stack definition and use the Ambari APIs to install/configure the service. This service is includes a CLIENT so it has two commands: install and configureMASTER, SLAVE and CLIENT as well as a set of configurations.
Create and Add the Service
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
Create a directory named
/var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/TESTSRVSAMPLESRV
that will contain the service definition for TESTSRV SAMPLESRV.Code Block mkdir /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/TESTSRVSAMPLESRV cd /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/TESTSRVSAMPLESRV
Browse to the newly created
TESTSRV
SAMPLESRV
directory, create ametainfo.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>SAMPLESRV</name> <displayName>New TestSample Service</displayName> <comment>A <comment>A New TestSample Service</comment> <version>0<version>1.10.0</version> <components> <component> <name>TEST<name>SAMPLESRV_CLIENT<MASTER</name> <displayName>New<displayName>Sample TestSrv Client<Master</displayName> <category>CLIENT<<category>MASTER</category> <commandScript><cardinality>1</cardinality> <commandScript> <script>scripts/test_client <script>scripts/master.py</script> <scriptType>PYTHON</scriptType> <timeout>600</timeout> </commandScript> </component> <customCommands> <component> <customCommand> <name>SAMPLESRV_SLAVE</name> <name>SOMETHINGCUSTOM</name> <displayName>Sample Srv Slave</displayName> <commandScript> <category>SLAVE</category> <script>scripts/test_client.py</script><cardinality>1+</cardinality> <scriptType>PYTHON</scriptType><commandScript> <timeout>600</timeout><script>scripts/slave.py</script> <<scriptType>PYTHON</commandScript>scriptType> < <timeout>600</customCommand>timeout> </customCommands>commandScript> </component> </components> <component> <osSpecifics> <name>SAMPLESRV_CLIENT</name> <osSpecific> <displayName>Sample Srv Client</displayName> <osFamily>any</osFamily> <<category>CLIENT</osSpecific>category> </osSpecifics> <cardinality>1+</service>cardinality> </services> </metainfo>
- 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. Create a directory for the command script
/var/lib/ambari-server/resources/stacks/HDP/2.0.6/servi
ces/
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
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()
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 into existing Cluster (via the Ambari REST API)
...
Code Block |
---|
POST
/api/v1/clusters/MyCluster/services
{
"ServiceInfo": {
"service_name":"TESTSRV"
}
} |
...
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
|
...
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 |
...
<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>
- 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"
Next, let's create that command script. Create a directory for the command script
/var/lib/ambari-server/resources/stacks/HDP/2.0.6/servi
ces/
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
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()
Now, restart Ambari Server for this new service definition to be distributed to all the Agents in the cluster.
Code Block ambari-server restart
Example: Implementing a Custom Client-only
Code Block |
---|
PUT
/api/v1/clusters/MyCluster/services/TESTSRV
{
"RequestInfo": {
"context": "Install Test Srv Client"
},
"Body": {
"ServiceInfo": {
"state": "INSTALLED"
}
}
}
|
...
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"
}
}
} |
...
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"
}]
} |
...
If you want to see which hosts the component is installed.
Code Block |
---|
GET
/api/v1/clusters/MyCluster/components/TEST_CLIENT
|
Install the Service into existing Cluster (via Ambari Web "Add Services")
Note |
---|
This ability to add custom services via Ambari Web is new as of Ambari 1.7.0. |
...
Service
In this example, we will create a custom service called "SAMPLESRVTESTSRV", 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 configurationsis a CLIENT so it has two commands: install and configure.
Create and Add the Service
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
Create a directory named
/var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/SAMPLESRVTESTSRV
that will contain the service definition for SAMPLESRV TESTSRV.Code Block mkdir /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/SAMPLESRVTESTSRV cd /var/lib/ambari-server/resources/stacks/HDP/2.0.6/services/SAMPLESRVTESTSRV
Browse to the newly created
SAMPLESRV
TESTSRV
directory, create ametainfo.xml
file that describes the new service. For example:describes the new service. For example:Code Block <?xml version="1.0"?> <metainfo> <schemaVersion>2.0</schemaVersion>
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> <services> <components><service> <name>TESTSRV</name> <component> <displayName>New Test Service</displayName> <comment>A New Test <name>SAMPLESRV_MASTER<Service</name>comment> <version>0.1.0</version> <displayName>Sample Srv Master</displayName> <components> <category>MASTER</category><component> <cardinality>1<<name>TEST_CLIENT</cardinality>name> <commandScript> <displayName>New Test Client</displayName> <script>scripts/master.py</script><category>CLIENT</category> <commandScript> <scriptType>PYTHON</scriptType> <script>scripts/test_client.py</script> <timeout>600</timeout> <<scriptType>PYTHON</commandScript>scriptType> </component> <timeout>600</timeout> <component> </commandScript> <name>SAMPLESRV_SLAVE</name> <customCommands> <displayName>Sample Srv Slave</displayName> <customCommand> <category>SLAVE</category> <name>SOMETHINGCUSTOM</name> <cardinality>1+</cardinality> <commandScript> <script>scripts/slavetest_client.py</script> <scriptType>PYTHON</scriptType> <timeout>600</timeout> </commandScript> </component>customCommand> <component> </customCommands> <name>SAMPLESRV_CLIENT</name></component> </components> <displayName>Sample Srv Client</displayName><osSpecifics> <osSpecific> <category>CLIENT</category> <osFamily>any</osFamily> <cardinality>1+</cardinality> </osSpecific> <commandScript></osSpecifics> <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>
- 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"
Next, let's create that command script. Create a directory for the command script
/var/lib/ambari-server/resources/stacks/HDP/2.0.6/servi
ces/
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
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()
</service> </services> </metainfo>
- 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. Create a directory for the command script
/var/lib/ambari-server/resources/stacks/HDP/2.0.6/servi
ces/
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
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()
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 into existing Cluster (via the Ambari REST API)
Add the Service to the Cluster.
Code Block POST /api/v1/clusters/MyCluster/services { "ServiceInfo": { "service_name":"TESTSRV" } }
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
Install the component on all target hosts. For example, to install on
c6402.ambari.apache.org
andc6403.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
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" } } }
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" } } }
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" }] }
If you want to see which hosts the component is installed.
Code Block GET /api/v1/clusters/MyCluster/components/TEST_CLIENT
Install the Service into existing Cluster (via Ambari Web "Add Services")
Note |
---|
This ability to add custom services via Ambari Web is new as of Ambari 1.7.0. |
- In Ambari Web, browse to Services and click the Actions button in the Service navigation area on the left.
- 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 servicemetainfo.xml
file). - Select "My Test Service" and click Next.
- Select the hosts to install the Test Client and click Next.
- Once complete, the "My Test Service" will be available Service navigation area.
- 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".
Now, restart Ambari Server for this new service definition to be distributed to all the Agents in the cluster.
Code Block ambari-server restart