OracleCreate.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/usr/bin/env python3
  2. # -*- encoding: utf-8 -*-
  3. # Author: MoeClub.org
  4. # pip3 install rsa
  5. # python3 OracleCreate.py -c "config/defaults.json" -i "create/defaults.json"
  6. # config/defaults.json
  7. # {
  8. # "compartmentId": "ocid1.tenancy...",
  9. # "userId": "ocid1.user...",
  10. # "URL": "https://iaas.xxxxx.oraclecloud.com/20160918/",
  11. # "certFinger": "ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff",
  12. # "certKey": "-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----"
  13. # }
  14. # create/defaults.json
  15. # {
  16. # "shape": "VM.Standard.E2.1.Micro",
  17. # "availabilityDomain": "flzu:AP-TOKYO-1-AD-1",
  18. # "subnetId": "ocid1.subnet...",
  19. # "imageId": "BASE64...",
  20. # "ssh_authorized_keys": "ssh-rsa ...",
  21. # }
  22. import hashlib
  23. import datetime
  24. import base64
  25. import json
  26. import time
  27. import rsa
  28. from urllib import request, error, parse
  29. class oracle:
  30. @staticmethod
  31. def http(url, method, headers=None, data=None, coding='utf-8'):
  32. if not headers:
  33. headers = {}
  34. if data is not None:
  35. if isinstance(data, (dict, list)):
  36. data = json.dumps(data)
  37. if 'content-length' not in [str(item).lower() for item in list(headers.keys())]:
  38. headers['Content-Length'] = str(len(data))
  39. data = str(data).encode(coding)
  40. url_obj = request.Request(url, method=method, data=data, headers=headers)
  41. try:
  42. res_obj = request.urlopen(url_obj)
  43. except error.HTTPError as err:
  44. res_obj = err
  45. return res_obj
  46. @staticmethod
  47. def header(keyID, privateKey, reqURL, reqMethod, body=None, algorithm="rsa-sha256"):
  48. sign_list = []
  49. url_parse = parse.urlparse(reqURL)
  50. _header_field = ["(request-target)", "date", "host"]
  51. _header = {
  52. 'host': str(url_parse.netloc),
  53. 'user-agent': 'oracle-api/1.0',
  54. 'date': str(datetime.datetime.utcnow().strftime("%a, %d %h %Y %H:%M:%S GMT")),
  55. 'accept': '*/*',
  56. 'accept-encoding': '',
  57. }
  58. sign_list.append(str("(request-target): {} {}").format(str(reqMethod).lower(), parse.quote_plus(str("{}{}").format(url_parse.path, str("?{}").format(url_parse.query) if url_parse.query else ""), safe="/?=&~")))
  59. if body is not None:
  60. if isinstance(body, (dict, list)):
  61. _body = json.dumps(body)
  62. else:
  63. _body = body
  64. _header_field += ["content-length", "content-type", "x-content-sha256"]
  65. _header['content-type'] = 'application/json'
  66. _header['content-length'] = str(len(_body))
  67. _header['x-content-sha256'] = str(base64.b64encode(hashlib.sha256(_body.encode("utf-8")).digest()).decode("utf-8"))
  68. sign_list += [str("{}: {}").format(item, _header[item]) for item in _header_field if "target" not in item]
  69. _signature = base64.b64encode(rsa.sign(str(str("\n").join(sign_list)).encode("utf-8"), rsa.PrivateKey.load_pkcs1(privateKey if isinstance(privateKey, bytes) else str(privateKey).strip().encode("utf-8")), str(algorithm).split("-", 1)[-1].upper().replace("SHA", "SHA-"))).decode("utf-8")
  70. _header['authorization'] = str('Signature keyId="{}",algorithm="{}",signature="{}",headers="{}"').format(keyID, algorithm, _signature, str(" ").join(_header_field))
  71. return _header
  72. @staticmethod
  73. def load_Key(file, BIN=False, coding='utf-8'):
  74. fd = open(file, 'r', encoding=coding)
  75. data = fd.read()
  76. fd.close()
  77. return str(data).strip().encode(coding) if BIN else str(data).strip()
  78. @staticmethod
  79. def load_Config(file, coding='utf-8'):
  80. fd = open(file, 'r', encoding=coding)
  81. data = fd.read()
  82. fd.close()
  83. return json.loads(data, encoding=coding)
  84. @classmethod
  85. def api(cls, method, url, keyID, privateKey, data=None):
  86. method_allow = ["GET", "HEAD", "DELETE", "PUT", "POST"]
  87. method = str(method).strip().upper()
  88. if method not in method_allow:
  89. raise Exception(str("Method Not Allow [{}]").format(method))
  90. if len(str(keyID).split("/")) != 3:
  91. raise Exception(str('Invalid "keyID"'))
  92. if method in ["PUT", "POST"] and data is None:
  93. data = ""
  94. privateKey = privateKey if isinstance(privateKey, bytes) else str(privateKey).strip().encode("utf-8")
  95. headers = cls.header(keyID, privateKey, url, method, data)
  96. return cls.http(url, method, headers, data)
  97. class action:
  98. def __init__(self, apiDict, configDict):
  99. self.apiDict = apiDict
  100. self.configDict = configDict
  101. self.instancesDict = {
  102. 'displayName': str(str(self.configDict["availabilityDomain"]).split(":", 1)[-1].split("-")[1]),
  103. 'shape': self.configDict["shape"],
  104. 'compartmentId': self.configDict["compartmentId"],
  105. 'availabilityDomain': self.configDict["availabilityDomain"],
  106. 'sourceDetails': {
  107. 'sourceType': 'image',
  108. 'imageId': self.configDict['imageId'],
  109. },
  110. 'createVnicDetails': {
  111. 'subnetId': self.configDict['subnetId'],
  112. 'assignPublicIp': True
  113. },
  114. 'metadata': {
  115. 'user_data': self.configDict['user_data'],
  116. 'ssh_authorized_keys': self.configDict['ssh_authorized_keys'],
  117. },
  118. 'agentConfig': {
  119. 'isMonitoringDisabled': False,
  120. 'isManagementDisabled': False
  121. },
  122. }
  123. self.apiKey = "/".join([apiDict["compartmentId"], apiDict["userId"], apiDict["certFinger"]])
  124. self.url = self.apiDict["URL"] + "instances"
  125. self.body = json.dumps(self.instancesDict, ensure_ascii=False)
  126. def create(self, Full=True, WaitResource=True):
  127. while True:
  128. FLAG = False
  129. try:
  130. try:
  131. response = oracle.api("POST", self.url, keyID=self.apiKey, privateKey=self.apiDict["certKey"], data=self.body)
  132. response_json = json.loads(response.read().decode())
  133. response_json["status_code"] = str(response.code)
  134. except Exception as e:
  135. print(e)
  136. response_json = {"code": "InternalError", "message": "Timeout.", "status_code": "555"}
  137. if str(response_json["status_code"]).startswith("4"):
  138. FLAG = True
  139. if str(response_json["status_code"]) == "401":
  140. response_json["message"] = "Not Authenticated."
  141. elif str(response_json["status_code"]) == "400":
  142. if str(response_json["code"]) == "LimitExceeded":
  143. response_json["message"] = "Limit Exceeded."
  144. if str(response_json["code"]) == "QuotaExceeded":
  145. response_json["message"] = "Quota Exceeded."
  146. elif str(response_json["status_code"]) == "429":
  147. FLAG = False
  148. elif str(response_json["status_code"]) == "404":
  149. if WaitResource and str(response_json["code"]) == "NotAuthorizedOrNotFound":
  150. FLAG = False
  151. if int(response_json["status_code"]) < 300:
  152. vm_ocid = str(str(response_json["id"]).split(".")[-1])
  153. print(str("{} [{}] {}").format(time.strftime("[%Y/%m/%d %H:%M:%S]", time.localtime()), response_json["status_code"], str(vm_ocid[:5] + "..." + vm_ocid[-7:])))
  154. else:
  155. print(str("{} [{}] {}").format(time.strftime("[%Y/%m/%d %H:%M:%S]", time.localtime()), response_json["status_code"], response_json["message"]))
  156. if Full is False and str(response_json["status_code"]) == "200":
  157. FLAG = True
  158. if not FLAG:
  159. if str(response_json["status_code"]) == "429":
  160. time.sleep(60)
  161. elif str(response_json["status_code"]) == "404":
  162. time.sleep(30)
  163. else:
  164. time.sleep(5)
  165. except Exception as e:
  166. FLAG = True
  167. print(e)
  168. if FLAG:
  169. break
  170. if __name__ == "__main__":
  171. def Exit(code=0, msg=None):
  172. if msg:
  173. print(msg)
  174. exit(code)
  175. import argparse
  176. parser = argparse.ArgumentParser()
  177. parser.add_argument('-c', type=str, default="", help="Config Path")
  178. parser.add_argument('-i', type=str, default="", help="Instances Config Path")
  179. args = parser.parse_args()
  180. configPath = str(args.c).strip()
  181. configInstances = str(args.i).strip()
  182. if not configPath:
  183. Exit(1, "Require Config Path.")
  184. if not configInstances:
  185. Exit(1, "Require Instances Config Path.")
  186. Action = action(oracle.load_Config(configPath), oracle.load_Config(configInstances))
  187. Action.create()
  188. Exit(0)