OracleAction.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #!/usr/bin/env python3
  2. # -*- encoding: utf-8 -*-
  3. # Author: MoeClub.org
  4. # pip3 install rsa
  5. # python3 OracleAction.py -c "config/defaults.json" -i "ocid1.instance...|create/defaults.json" -a "<action>" -n "name"
  6. # https://docs.cloud.oracle.com/en-us/iaas/tools/public_ip_ranges.json
  7. # config/defaults.json
  8. # {
  9. # "compartmentId": "ocid1.tenancy...",
  10. # "userId": "ocid1.user...",
  11. # "URL": "https://iaas.xxx.oraclecloud.com/20160918/",
  12. # "certFinger": "ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff",
  13. # "certKey": "-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----"
  14. # }
  15. # create/defaults.json
  16. # {
  17. # "shape": "VM.Standard.E2.1.Micro",
  18. # "availabilityDomain": "xx:XX",
  19. # "subnetId": "ocid1.subnet...",
  20. # "imageId": "BASE64...",
  21. # "ssh_authorized_keys": "ssh-rsa ...",
  22. # }
  23. import hashlib
  24. import datetime
  25. import base64
  26. import json
  27. import time
  28. import rsa
  29. from urllib import request, error, parse
  30. class oracle:
  31. @staticmethod
  32. def http(url, method, headers=None, data=None, coding='utf-8'):
  33. if not headers:
  34. headers = {}
  35. if data is not None:
  36. if isinstance(data, (dict, list)):
  37. data = json.dumps(data)
  38. if 'content-length' not in [str(item).lower() for item in list(headers.keys())]:
  39. headers['Content-Length'] = str(len(data))
  40. data = str(data).encode(coding)
  41. url_obj = request.Request(url, method=method, data=data, headers=headers)
  42. try:
  43. res_obj = request.urlopen(url_obj)
  44. except error.HTTPError as err:
  45. res_obj = err
  46. return res_obj
  47. @staticmethod
  48. def header(keyID, privateKey, reqURL, reqMethod, body=None, algorithm="rsa-sha256"):
  49. sign_list = []
  50. url_parse = parse.urlparse(reqURL)
  51. _header_field = ["(request-target)", "date", "host"]
  52. _header = {
  53. 'host': str(url_parse.netloc),
  54. 'user-agent': 'oracle-api/1.0',
  55. 'date': str(datetime.datetime.utcnow().strftime("%a, %d %h %Y %H:%M:%S GMT")),
  56. 'accept': '*/*',
  57. 'accept-encoding': '',
  58. }
  59. 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="/?=&~")))
  60. if body is not None:
  61. if isinstance(body, (dict, list)):
  62. _body = json.dumps(body)
  63. else:
  64. _body = body
  65. _header_field += ["content-length", "content-type", "x-content-sha256"]
  66. _header['content-type'] = 'application/json'
  67. _header['content-length'] = str(len(_body))
  68. _header['x-content-sha256'] = str(base64.b64encode(hashlib.sha256(_body.encode("utf-8")).digest()).decode("utf-8"))
  69. sign_list += [str("{}: {}").format(item, _header[item]) for item in _header_field if "target" not in item]
  70. _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")
  71. _header['authorization'] = str('Signature keyId="{}",algorithm="{}",signature="{}",headers="{}"').format(keyID, algorithm, _signature, str(" ").join(_header_field))
  72. return _header
  73. @staticmethod
  74. def load_Key(file, BIN=False, coding='utf-8'):
  75. fd = open(file, 'r', encoding=coding)
  76. data = fd.read()
  77. fd.close()
  78. return str(data).strip().encode(coding) if BIN else str(data).strip()
  79. @staticmethod
  80. def load_Config(file, coding='utf-8'):
  81. fd = open(file, 'r', encoding=coding)
  82. data = fd.read()
  83. fd.close()
  84. return json.loads(data, encoding=coding)
  85. @classmethod
  86. def api(cls, method, url, keyID, privateKey, data=None):
  87. method_allow = ["GET", "HEAD", "DELETE", "PUT", "POST"]
  88. method = str(method).strip().upper()
  89. if method not in method_allow:
  90. raise Exception(str("Method Not Allow [{}]").format(method))
  91. if len(str(keyID).split("/")) != 3:
  92. raise Exception(str('Invalid "keyID"'))
  93. if method in ["PUT", "POST"] and data is None:
  94. data = ""
  95. privateKey = privateKey if isinstance(privateKey, bytes) else str(privateKey).strip().encode("utf-8")
  96. headers = cls.header(keyID, privateKey, url, method, data)
  97. return cls.http(url, method, headers, data)
  98. class action:
  99. def __init__(self, apiDict, instancesId=None, configDict=None):
  100. self.apiDict = apiDict
  101. self.privateKey = self.apiDict["certKey"]
  102. self.apiKey = "/".join([self.apiDict["compartmentId"], self.apiDict["userId"], self.apiDict["certFinger"]])
  103. self.configDict = configDict
  104. self.instancesId = instancesId
  105. self.instancesDict = None
  106. self.VNIC = None
  107. self.PRIVATE = None
  108. def getPrivateIP(self):
  109. if not self.instancesId:
  110. print("Require instancesId.")
  111. exit(1)
  112. url = self.apiDict["URL"] + "vnicAttachments?instanceId=" + self.instancesId + "&compartmentId=" + self.apiDict["compartmentId"]
  113. response = oracle.api("GET", url, keyID=self.apiKey, privateKey=self.privateKey)
  114. response_vnic = json.loads(response.read().decode())
  115. if response_vnic:
  116. self.VNIC = response_vnic[0]
  117. if not self.VNIC:
  118. print("Not Found VNIC.")
  119. exit(1)
  120. url = self.apiDict["URL"] + "privateIps?vnicId=" + self.VNIC["vnicId"]
  121. response = oracle.api("GET", url, keyID=self.apiKey, privateKey=self.privateKey)
  122. response_private = json.loads(response.read().decode())
  123. if response_private:
  124. for privateIp in response_private:
  125. if privateIp["isPrimary"]:
  126. self.PRIVATE = response_private[0]
  127. break
  128. if not self.PRIVATE:
  129. print("Not Found Private IP Address.")
  130. exit(1)
  131. def getPublicIP(self):
  132. url = self.apiDict["URL"] + "publicIps/actions/getByPrivateIpId"
  133. BodyOne = json.dumps({"privateIpId": self.PRIVATE["id"]}, ensure_ascii=False)
  134. response = oracle.api("POST", url, keyID=self.apiKey, privateKey=self.privateKey, data=BodyOne)
  135. if response.code >= 200 and response.code < 400:
  136. response_public = json.loads(response.read().decode())
  137. return response_public
  138. elif response.code >= 400 and response.code < 500:
  139. return None
  140. else:
  141. print("Server Error. [%s]" % response.code)
  142. exit(1)
  143. def delPublicIP(self, publicIp):
  144. url = self.apiDict["URL"] + "publicIps/" + publicIp
  145. oracle.api("DELETE", url, keyID=self.apiKey, privateKey=self.privateKey)
  146. def newPublicIP(self):
  147. bodyTwo = {
  148. "lifetime": "EPHEMERAL",
  149. "compartmentId": self.apiDict["compartmentId"],
  150. "privateIpId": self.PRIVATE["id"],
  151. }
  152. url = self.apiDict["URL"] + "publicIps"
  153. BodyTwo = json.dumps(bodyTwo, ensure_ascii=False)
  154. response = oracle.api("POST", url, keyID=self.apiKey, privateKey=self.privateKey, data=BodyTwo)
  155. NewPublic = json.loads(response.read().decode())
  156. print("PublicIP:", NewPublic["ipAddress"])
  157. return NewPublic
  158. def showPublicIP(self):
  159. self.getPrivateIP()
  160. PUBLIC = self.getPublicIP()
  161. publicIp = "NULL"
  162. if PUBLIC:
  163. publicIp = PUBLIC["ipAddress"]
  164. print("PublicIP: %s" % publicIp)
  165. return PUBLIC
  166. def changePubilcIP(self):
  167. self.getPrivateIP()
  168. PUBLIC = self.getPublicIP()
  169. publicIp = "NULL"
  170. if PUBLIC:
  171. publicIp = PUBLIC["ipAddress"]
  172. self.delPublicIP(PUBLIC["id"])
  173. print("PublicIP[*]: %s" % publicIp)
  174. PUBLIC = self.newPublicIP()
  175. return PUBLIC
  176. def rename(self, newName, DisableMonitoring=True):
  177. if not self.instancesId:
  178. print("Require instancesId.")
  179. exit(1)
  180. setName = str(newName).strip()
  181. if not setName:
  182. print("Name Invalid.")
  183. exit(1)
  184. body = {"displayName": setName, "agentConfig": {"isMonitoringDisabled": DisableMonitoring}}
  185. url = self.apiDict["URL"] + "instances/" + self.instancesId
  186. Body = json.dumps(body, ensure_ascii=False)
  187. response = oracle.api("PUT", url, keyID=self.apiKey, privateKey=self.privateKey, data=Body)
  188. response_json = json.loads(response.read().decode())
  189. response_json["status_code"] = str(response.code)
  190. print(json.dumps(response_json, indent=4))
  191. def createInstancesPre(self, Name=None):
  192. self.instancesDict = {
  193. 'displayName': str(str(self.configDict["availabilityDomain"]).split(":", 1)[-1].split("-")[1]),
  194. 'shape': self.configDict["shape"],
  195. 'compartmentId': self.apiDict["compartmentId"],
  196. 'availabilityDomain': self.configDict["availabilityDomain"],
  197. 'sourceDetails': {
  198. 'sourceType': 'image',
  199. 'imageId': self.configDict['imageId'],
  200. },
  201. 'createVnicDetails': {
  202. 'subnetId': self.configDict['subnetId'],
  203. 'assignPublicIp': True
  204. },
  205. 'metadata': {
  206. 'user_data': self.configDict['user_data'],
  207. 'ssh_authorized_keys': self.configDict['ssh_authorized_keys'],
  208. },
  209. 'agentConfig': {
  210. 'isMonitoringDisabled': False,
  211. 'isManagementDisabled': False
  212. },
  213. }
  214. if Name and str(Name).strip():
  215. self.instancesDict['displayName'] = str(Name).strip()
  216. def createInstances(self, Name=None, Full=True, WaitResource=True):
  217. url = self.apiDict["URL"] + "instances"
  218. if not self.instancesDict or Name is not None:
  219. self.createInstancesPre(Name=Name)
  220. body = json.dumps(self.instancesDict, ensure_ascii=False)
  221. while True:
  222. FLAG = False
  223. try:
  224. try:
  225. response = oracle.api("POST", url, keyID=self.apiKey, privateKey=self.privateKey, data=body)
  226. response_json = json.loads(response.read().decode())
  227. response_json["status_code"] = str(response.code)
  228. except Exception as e:
  229. print(e)
  230. response_json = {"code": "InternalError", "message": "Timeout.", "status_code": "555"}
  231. if str(response_json["status_code"]).startswith("4"):
  232. FLAG = True
  233. if str(response_json["status_code"]) == "401":
  234. response_json["message"] = "Not Authenticated."
  235. elif str(response_json["status_code"]) == "400":
  236. if str(response_json["code"]) == "LimitExceeded":
  237. response_json["message"] = "Limit Exceeded."
  238. if str(response_json["code"]) == "QuotaExceeded":
  239. response_json["message"] = "Quota Exceeded."
  240. elif str(response_json["status_code"]) == "429":
  241. FLAG = False
  242. elif str(response_json["status_code"]) == "404":
  243. if WaitResource and str(response_json["code"]) == "NotAuthorizedOrNotFound":
  244. FLAG = False
  245. if int(response_json["status_code"]) < 300:
  246. vm_ocid = str(str(response_json["id"]).split(".")[-1])
  247. print(str("{} [{}] {}").format(time.strftime("[%Y/%m/%d %H:%M:%S]", time.localtime()), response_json["status_code"], str(vm_ocid[:5] + "..." + vm_ocid[-7:])))
  248. else:
  249. print(str("{} [{}] {}").format(time.strftime("[%Y/%m/%d %H:%M:%S]", time.localtime()), response_json["status_code"], response_json["message"]))
  250. if Full is False and str(response_json["status_code"]) == "200":
  251. FLAG = True
  252. if not FLAG:
  253. if str(response_json["status_code"]) == "429":
  254. time.sleep(60)
  255. elif str(response_json["status_code"]) == "404":
  256. time.sleep(30)
  257. else:
  258. time.sleep(5)
  259. except Exception as e:
  260. FLAG = True
  261. print(e)
  262. if FLAG:
  263. break
  264. if __name__ == "__main__":
  265. def Exit(code=0, msg=None):
  266. if msg:
  267. print(msg)
  268. exit(code)
  269. import argparse
  270. parser = argparse.ArgumentParser()
  271. parser.add_argument('-c', type=str, default="", help="Config Path")
  272. parser.add_argument('-i', type=str, default="", help="Instances Id or Instances Config Path")
  273. parser.add_argument('-n', type=str, default="", help="New Instances Name")
  274. parser.add_argument('-p', type=str, default="", help="IP Address Prefix")
  275. parser.add_argument('-a', type=str, default="", help="Action [show, change, rename, create, target]")
  276. args = parser.parse_args()
  277. configPath = str(args.c).strip()
  278. configAction = str(args.a).strip().lower()
  279. configInstancesId = str(args.i).strip()
  280. configInstancesName = str(args.n).strip()
  281. configAddress = str(args.p).strip()
  282. configActionList = ["show", "change", "rename", "create", "target"]
  283. if not configPath:
  284. Exit(1, "Require Config Path.")
  285. if not configAction or configAction not in configActionList:
  286. Exit(1, "Invalid Action.")
  287. if not configInstancesId:
  288. Exit(1, "Require Instances Id or Instances Config Path.")
  289. if configAction == "loop" and not configAddress:
  290. configAction = "change"
  291. if configAction == "show":
  292. Action = action(apiDict=oracle.load_Config(configPath), instancesId=configInstancesId)
  293. Action.showPublicIP()
  294. elif configAction == "change":
  295. Action = action(apiDict=oracle.load_Config(configPath), instancesId=configInstancesId)
  296. Action.changePubilcIP()
  297. elif configAction == "rename":
  298. if not configInstancesName:
  299. Exit(1, "Require Instances Name.")
  300. Action = action(apiDict=oracle.load_Config(configPath), instancesId=configInstancesId)
  301. Action.rename(configInstancesName)
  302. elif configAction == "create":
  303. if not configInstancesName:
  304. configInstancesName = None
  305. else:
  306. configInstancesName = str(configInstancesName).strip()
  307. Action = action(apiDict=oracle.load_Config(configPath), configDict=oracle.load_Config(configInstancesId))
  308. Action.createInstances(configInstancesName)
  309. elif configAction == "target":
  310. Action = action(apiDict=oracle.load_Config(configPath), instancesId=configInstancesId)
  311. while True:
  312. NewPublic = Action.changePubilcIP()
  313. if str(NewPublic["ipAddress"]).startswith(configAddress):
  314. break
  315. else:
  316. del NewPublic
  317. time.sleep(3)
  318. Exit(0)