Advertisement
Guest User

Untitled

a guest
Jan 5th, 2012
9,376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: iso-8859-15 -*-
  3.  
  4. import json
  5. import os
  6. import pycurl
  7. import sys
  8. import cStringIO
  9.  
  10. if __name__ == "__main__":
  11.  
  12. name_server_standart = "Server created by script %d"
  13. json_file_standart = "{ \"server\" : { \"name\" : \"%s\", \"imageRef\" : \"%s\", \"flavorRef\" : \"%s\" } }"
  14.  
  15. curl_auth_token = pycurl.Curl()
  16.  
  17. gettoken = cStringIO.StringIO()
  18.  
  19. curl_auth_token.setopt(pycurl.URL, "http://192.168.100.241:8774/v1.1")
  20. curl_auth_token.setopt(pycurl.POST, 1)
  21. curl_auth_token.setopt(pycurl.HTTPHEADER, ["X-Auth-User: cpca",
  22. "X-Auth-Key: 438ac2d9-689f-4c50-9d00-c2883cfd38d0"])
  23.  
  24. curl_auth_token.setopt(pycurl.HEADERFUNCTION, gettoken.write)
  25. curl_auth_token.perform()
  26. chg = gettoken.getvalue()
  27.  
  28. auth_token = chg[chg.find("X-Auth-Token: ")+len("X-Auth-Token: ") : chg.find("X-Server-Management-Url:")-1]
  29.  
  30. token = "X-Auth-Token: {0}".format(auth_token)
  31. curl_auth_token.close()
  32.  
  33.  
  34. #----------------------------
  35.  
  36. getter = cStringIO.StringIO()
  37. curl_hab_image = pycurl.Curl()
  38. curl_hab_image.setopt(pycurl.URL, "http://192.168.100.241:8774/v1.1/nuvemcpca/images/7")
  39. curl_hab_image.setopt(pycurl.HTTPGET, 1) #tirei essa linha e funcionou, nao sei porque
  40. curl_hab_image.setopt(pycurl.HTTPHEADER, [token])
  41.  
  42.  
  43. curl_hab_image.setopt(pycurl.WRITEFUNCTION, getter.write)
  44. #curl_list.setopt(pycurl.VERBOSE, 1)
  45. curl_hab_image.perform()
  46. curl_hab_image.close()
  47.  
  48.  
  49.  
  50. getter = cStringIO.StringIO()
  51.  
  52. curl_list = pycurl.Curl()
  53. curl_list.setopt(pycurl.URL, "http://192.168.100.241:8774/v1.1/nuvemcpca/servers/detail")
  54. curl_list.setopt(pycurl.HTTPGET, 1) #tirei essa linha e funcionou, nao sei porque
  55. curl_list.setopt(pycurl.HTTPHEADER, [token])
  56.  
  57.  
  58. curl_list.setopt(pycurl.WRITEFUNCTION, getter.write)
  59. #curl_list.setopt(pycurl.VERBOSE, 1)
  60. curl_list.perform()
  61. curl_list.close()
  62.  
  63. resp = getter.getvalue()
  64.  
  65.  
  66.  
  67. #print resp
  68. con = int(resp.count("status"))
  69.  
  70. s = json.loads(resp)
  71.  
  72. lst = []
  73.  
  74. #print s
  75.  
  76. for i in range(con):
  77. lst.append(s['servers'][i]['status'])
  78.  
  79. #print len(lst)
  80.  
  81. for j in range(len(lst)):
  82. actual = lst.pop()
  83. print actual
  84.  
  85. if actual != "ACTIVE" and actual != "BUILD" and actual != "REBOOT" and actual != "RESIZE":
  86.  
  87. print "Entra no If"
  88.  
  89.  
  90. f = file('counter', 'r+w')
  91.  
  92. num = 0
  93. for line in f:
  94. num = line
  95.  
  96. content = int(num)+1
  97.  
  98. ins = str(content)
  99.  
  100. f.seek(0)
  101. f.write(ins)
  102. f.truncate()
  103. f.close()
  104.  
  105.  
  106. print "Contador"
  107.  
  108. json_file = file('json_file_create_server.json','r+w')
  109.  
  110. name_server_final = name_server_standart % content
  111. path_to_image = "http://192.168.100.241:8774/v1.1/nuvemcpca/images/7"
  112. path_to_flavor = "http://192.168.100.241:8774/v1.1/nuvemcpca/flavors/1"
  113.  
  114. new_json_file_content = json_file_standart % (name_server_final, path_to_image, path_to_flavor)
  115.  
  116. json_file.seek(0)
  117. json_file.write(new_json_file_content)
  118. json_file.truncate()
  119. json_file.close()
  120.  
  121.  
  122. print "Json File"
  123.  
  124.  
  125. fil = file("json_file_create_server.json")
  126. siz = os.path.getsize("json_file_create_server.json")
  127.  
  128. cont_size = "Content-Length: %d" % siz
  129. cont_type = "Content-Type: application/json"
  130. accept = "Accept: application/json"
  131.  
  132. c_create_servers = pycurl.Curl()
  133.  
  134. logger = cStringIO.StringIO()
  135.  
  136. c_create_servers.setopt(pycurl.URL, "http://192.168.100.241:8774/v1.1/nuvemcpca/servers")
  137.  
  138. c_create_servers.setopt(pycurl.HTTPHEADER, [token, cont_type, accept, cont_size])
  139.  
  140. c_create_servers.setopt(pycurl.POST, 1)
  141.  
  142. c_create_servers.setopt(pycurl.INFILE, fil)
  143.  
  144. c_create_servers.setopt(pycurl.INFILESIZE, siz)
  145.  
  146. c_create_servers.setopt(pycurl.WRITEFUNCTION, logger.write)
  147.  
  148.  
  149. print "Teste perform"
  150.  
  151. c_create_servers.perform()
  152.  
  153. print logger.getvalue()
  154.  
  155. c_create_servers.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement