add_service_api.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. '''
  17. # MUST be run on ambari-server host
  18. import json
  19. import time
  20. from resource_management.core.shell import checked_call, call
  21. # Change this to hostname of your ambari-server
  22. HOSTNAME = checked_call("hostname -f")[1].strip()
  23. ############# Configurations (feel free to change) #############
  24. SERVICE_NAME = "STORM"
  25. COMPONENTS = [
  26. "NIMBUS",
  27. "SUPERVISOR"
  28. ]
  29. COMPONENTS_TO_HOSTS = [
  30. {"NIMBUS": HOSTNAME},
  31. {"SUPERVISOR": HOSTNAME},
  32. #{"SUPERVISOR": "c6402.ambari.apache.org"},
  33. #{"SUPERVISOR": "c6403.ambari.apache.org"}
  34. ]
  35. PROTOCOL = "http"
  36. PORT = "8080"
  37. CLUSTER_NAME = "c1"
  38. STACK_VERSION = "2.0.8"
  39. CONFIGS_TO_CHANGE = {
  40. "storm-site":{
  41. #"storm.zookeeper.servers":"['c6401.amabri.apache.org','c6402.amabri.apache.org','c6403.amabri.apache.org']",
  42. #"nimbus.host": "c6401.ambari.apache.org"
  43. },
  44. #"global":{
  45. # "clientPort":"2182"
  46. #}
  47. }
  48. #################################################################
  49. SERVER_URL = "{protocol}://{hostname}:{port}".format(protocol=PROTOCOL, hostname=HOSTNAME, port=PORT)
  50. def main():
  51. # add service
  52. checked_call('curl -H \'X-Requested-By:anything\' -i -X POST -d \'[{{"ServiceInfo":{{"service_name":"{service_name}"}}}}]\' -u admin:admin {server_url}/api/v1/clusters/{cluster_name}/services'.
  53. format(service_name=SERVICE_NAME, server_url=SERVER_URL, cluster_name=CLUSTER_NAME))
  54. # add components
  55. for component in COMPONENTS:
  56. checked_call('curl -H \'X-Requested-By:anything\' -i -X POST -d \'{{"components":[{{"ServiceComponentInfo":{{"component_name":"{component}"}}}}]}}\' -u admin:admin {server_url}/api/v1/clusters/{cluster_name}/services?ServiceInfo/service_name={service_name}'.
  57. format(service_name=SERVICE_NAME, component=component, server_url=SERVER_URL, cluster_name=CLUSTER_NAME))
  58. # assign components to hosts
  59. for x in COMPONENTS_TO_HOSTS:
  60. for component, host in x.iteritems():
  61. checked_call('curl -H \'X-Requested-By:anything\' -i -X POST -d \'{{"host_components":[{{"HostRoles":{{"component_name":"{component}"}}}}]}}\' -u admin:admin {server_url}/api/v1/clusters/{cluster_name}/hosts?Hosts/host_name={host}'.
  62. format(host=host, component=component, server_url=SERVER_URL, cluster_name=CLUSTER_NAME))
  63. # update and create all the service-specific configurations
  64. checked_call('curl -H \'X-Requested-By:anything\'-X GET -u admin:admin {server_url}/api/v1/stacks2/HDP/versions/{stack_version}/stackServices/{service_name}/configurations?fields=* > /tmp/config.json'.
  65. format(server_url=SERVER_URL, stack_version=STACK_VERSION, service_name=SERVICE_NAME))
  66. with open('/tmp/config.json', "r") as f:
  67. d = json.load(f)
  68. configs = {}
  69. for x in d['items']:
  70. site_name = x['StackConfigurations']['type'][:-4]
  71. if not site_name in configs:
  72. configs[site_name] = {}
  73. config = configs[site_name]
  74. config[x['StackConfigurations']['property_name']] = x['StackConfigurations']['property_value']
  75. for site_name, site_content in configs.iteritems():
  76. code = call('/var/lib/ambari-server/resources/scripts/configs.sh get {hostname} {cluster_name} {site_name}'.format(hostname=HOSTNAME, cluster_name=CLUSTER_NAME, site_name=site_name))[0]
  77. if code:
  78. print "Adding new site: "+site_name
  79. checked_call('curl -i -H \'X-Requested-By:anything\' -X PUT -d \'{{"Clusters":{{"desired_configs":{{"type":"{site_name}","tag":"version1","properties":{site_content}}}}}}}\' -u admin:admin {server_url}/api/v1/clusters/{cluster_name}'.format(site_name=site_name, site_content=json.dumps(site_content), server_url=SERVER_URL, cluster_name=CLUSTER_NAME))
  80. else:
  81. timestamp = int(time.time())
  82. print "Modifiying site: "+site_name+" version"+str(timestamp)
  83. checked_call('/var/lib/ambari-server/resources/scripts/configs.sh get {hostname} {cluster_name} {site_name} /tmp/current_site.json'.format(hostname=HOSTNAME, cluster_name=CLUSTER_NAME, site_name=site_name))
  84. with open('/tmp/current_site.json', "r") as f:
  85. fcontent = f.read()
  86. d = json.loads("{"+fcontent+"}")
  87. for k,v in site_content.iteritems():
  88. d['properties'][k] = v
  89. checked_call('curl -i -H \'X-Requested-By:anything\' -X PUT -d \'{{"Clusters":{{"desired_configs":{{"type":"{site_name}","tag":"version{timestamp}","properties":{site_content}}}}}}}\' -u admin:admin {server_url}/api/v1/clusters/{cluster_name}'.format(site_name=site_name, timestamp=timestamp, site_content=json.dumps(d['properties']), server_url=SERVER_URL, cluster_name=CLUSTER_NAME))
  90. for site_name, site_configs in CONFIGS_TO_CHANGE.iteritems():
  91. for config_name, config_value in site_configs.iteritems():
  92. print "Adding config "+config_name+"="+config_value+" to "+site_name
  93. checked_call('/var/lib/ambari-server/resources/scripts/configs.sh set {hostname} {cluster_name} {site_name} {config_name} {config_value}'.format(config_name=config_name, config_value=config_value, hostname=HOSTNAME, cluster_name=CLUSTER_NAME, site_name=site_name))
  94. # install all new components
  95. checked_call('curl -H \'X-Requested-By:anything\' -i -X PUT -d \'{{"RequestInfo": {{"context" :"Installing Services"}}, "Body": {{"ServiceInfo": {{"state": "INSTALLED"}}}}}}\' -u admin:admin {server_url}/api/v1/clusters/{cluster_name}/services?ServiceInfo/state=INIT'.
  96. format(server_url=SERVER_URL, cluster_name=CLUSTER_NAME))
  97. if __name__ == '__main__':
  98. main()