Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import os
  3. import time
  4. import requests
  5.  
  6.  
  7. HOST = "localhost"
  8. PORT = "10000"
  9. CDAP_SDK_PATH = "/Users/christopherchoi/Downloads/cdap-sdk-2.5.2"
  10. HELLO_WORLD_APP = "{0}/examples/{1}/target/{1}-2.5.2.jar".format(CDAP_SDK_PATH, "HelloWorld")
  11. PURCHASE_APP = "{0}/examples/{1}/target/{1}-2.5.2.jar".format(CDAP_SDK_PATH, "Purchase")
  12.  
  13.  
  14. class ClientRestAPI(object):
  15.     def __init__(self):
  16.         self.base_url = "http://{0}:{1}/v2".format(HOST, PORT)
  17.  
  18.     def deploy_app(self, jar_path):
  19.         path = self.base_url + "/apps"
  20.         payload = open(jar_path, "rb").read()
  21.         jar_name = os.path.basename(jar_path)
  22.         headers = {"X-Archive-Name": jar_name}
  23.         return requests.post(path, headers=headers, data=payload)
  24.  
  25.     def undeploy_app(self, app_id):
  26.         p = "apps/" + app_id
  27.         return requests.delete(self.base_url + "/" + p)
  28.  
  29.     def list_apps(self):
  30.         p = "apps"
  31.         return requests.get(self.base_url + "/" + p)
  32.  
  33.     def unrecoverable_reset(self):
  34.         path = 'unrecoverable/reset'
  35.         return requests.post(self.base_url + '/' + path)
  36.  
  37.  
  38. class DatasetRestAPI(object):
  39.     def __init__(self):
  40.         self.base_url = "http://{0}:{1}/v2".format(HOST, PORT)
  41.  
  42.     def delete_all(self):
  43.         p = "data/unrecoverable/datasets"
  44.         return requests.delete(self.base_url + "/" + p)
  45.  
  46.  
  47. if __name__ == "__main__":
  48.     client = ClientRestAPI()
  49.     datasets = DatasetRestAPI()
  50.  
  51.     os.system(os.path.join(CDAP_SDK_PATH, "bin/cdap.sh") + " start")
  52.  
  53.     # deploy hello world app
  54.     resp = client.deploy_app(HELLO_WORLD_APP)
  55.     if resp.status_code != 200:
  56.         print "Error deploying [{0}]!".format(HELLO_WORLD_APP)
  57.         print resp.status_code, resp.content
  58.  
  59.     # deploy purchase app
  60.     resp = client.deploy_app(PURCHASE_APP)
  61.     if resp.status_code != 200:
  62.         print "Error deploying [{0}]!".format(PURCHASE_APP)
  63.         print resp.status_code, resp.content
  64.         time.sleep(2)
  65.  
  66.     datasets.delete_all()
  67.     client.unrecoverable_reset()
  68.  
  69.     # deploy hello world app
  70.     resp = client.deploy_app(HELLO_WORLD_APP)
  71.     if resp.status_code != 200:
  72.         print "Error deploying [{0}]!".format(HELLO_WORLD_APP)
  73.         print resp.status_code, resp.content
  74.  
  75.     # deploy purchase app
  76.     resp = client.deploy_app(PURCHASE_APP)
  77.     if resp.status_code != 200:
  78.         print "Error deploying [{0}]!".format(PURCHASE_APP)
  79.         print resp.status_code, resp.content
  80.         time.sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement