OracleAction.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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": "ap-seoul-1",
  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. # "shapeConfig": {"ocpus": 4, "memoryInGBs": 24},
  19. # "availabilityDomain": "xx:XX",
  20. # "subnetId": "ocid1.subnet...",
  21. # "imageId": "ocid1.image...",
  22. # "user_data": "BASE64...",
  23. # "ssh_authorized_keys": "ssh-rsa ...",
  24. # }
  25. import hashlib
  26. import datetime
  27. import base64
  28. import json
  29. import time
  30. import rsa
  31. from urllib import request, error, parse
  32. class oracle:
  33. @staticmethod
  34. def http(url, method, headers=None, data=None, coding='utf-8'):
  35. if not headers:
  36. headers = {}
  37. if data is not None:
  38. if isinstance(data, (dict, list)):
  39. data = json.dumps(data)
  40. if 'content-length' not in [str(item).lower() for item in list(headers.keys())]:
  41. headers['Content-Length'] = str(len(data))
  42. data = str(data).encode(coding)
  43. url_obj = request.Request(url, method=method, data=data, headers=headers)
  44. try:
  45. res_obj = request.urlopen(url_obj)
  46. except error.HTTPError as err:
  47. res_obj = err
  48. return res_obj
  49. @staticmethod
  50. def header(keyID, privateKey, reqURL, reqMethod, body=None, algorithm="rsa-sha256"):
  51. sign_list = []
  52. url_parse = parse.urlparse(reqURL)
  53. _header_field = ["(request-target)", "date", "host"]
  54. _header = {
  55. 'host': str(url_parse.netloc),
  56. 'user-agent': 'oracle-api/1.0',
  57. 'date': str(datetime.datetime.utcnow().strftime("%a, %d %h %Y %H:%M:%S GMT")),
  58. 'accept': '*/*',
  59. 'accept-encoding': '',
  60. }
  61. 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="/?=&~")))
  62. if body is not None:
  63. if isinstance(body, (dict, list)):
  64. _body = json.dumps(body)
  65. else:
  66. _body = body
  67. _header_field += ["content-length", "content-type", "x-content-sha256"]
  68. _header['content-type'] = 'application/json'
  69. _header['content-length'] = str(len(_body))
  70. _header['x-content-sha256'] = str(base64.b64encode(hashlib.sha256(_body.encode("utf-8")).digest()).decode("utf-8"))
  71. sign_list += [str("{}: {}").format(item, _header[item]) for item in _header_field if "target" not in item]
  72. _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")
  73. _header['authorization'] = str('Signature keyId="{}",algorithm="{}",signature="{}",headers="{}"').format(keyID, algorithm, _signature, str(" ").join(_header_field))
  74. return _header
  75. @staticmethod
  76. def load_Key(file, BIN=False, coding='utf-8'):
  77. fd = open(file, 'r', encoding=coding)
  78. data = fd.read()
  79. fd.close()
  80. return str(data).strip().encode(coding) if BIN else str(data).strip()
  81. @staticmethod
  82. def load_Config(file, coding='utf-8'):
  83. fd = open(file, 'r', encoding=coding)
  84. data = fd.read()
  85. fd.close()
  86. return json.loads(data)
  87. @classmethod
  88. def api(cls, method, url, keyID, privateKey, data=None):
  89. method_allow = ["GET", "HEAD", "DELETE", "PUT", "POST"]
  90. method = str(method).strip().upper()
  91. if method not in method_allow:
  92. raise Exception(str("Method Not Allow [{}]").format(method))
  93. if len(str(keyID).split("/")) != 3:
  94. raise Exception(str('Invalid "keyID"'))
  95. if method in ["PUT", "POST"] and data is None:
  96. data = ""
  97. privateKey = privateKey if isinstance(privateKey, bytes) else str(privateKey).strip().encode("utf-8")
  98. headers = cls.header(keyID, privateKey, url, method, data)
  99. return cls.http(url, method, headers, data)
  100. class Action:
  101. def __init__(self, apiDict, instancesId=None, configDict=None):
  102. self.apiDict = apiDict
  103. self.apiDict["URL"] = self.apiDict["URL"] if str(self.apiDict["URL"]).startswith("https://iaas.") else str("https://iaas.{}.oraclecloud.com/20160918/").format(self.apiDict["URL"])
  104. self.privateKey = self.apiDict["certKey"]
  105. self.apiKey = "/".join([self.apiDict["compartmentId"], self.apiDict["userId"], self.apiDict["certFinger"]])
  106. self.configDict = configDict
  107. self.instancesId = instancesId
  108. self.instancesDict = None
  109. self.VNIC = None
  110. self.PRIVATE = None
  111. def getPrivateIP(self):
  112. if not self.instancesId:
  113. print("Require instancesId.")
  114. exit(1)
  115. url = self.apiDict["URL"] + "vnicAttachments?instanceId=" + self.instancesId + "&compartmentId=" + self.apiDict["compartmentId"]
  116. response = oracle.api("GET", url, keyID=self.apiKey, privateKey=self.privateKey)
  117. response_vnic = json.loads(response.read().decode())
  118. if response_vnic:
  119. self.VNIC = response_vnic[0]
  120. if not self.VNIC:
  121. print("Not Found VNIC.")
  122. exit(1)
  123. url = self.apiDict["URL"] + "privateIps?vnicId=" + self.VNIC["vnicId"]
  124. response = oracle.api("GET", url, keyID=self.apiKey, privateKey=self.privateKey)
  125. response_private = json.loads(response.read().decode())
  126. if response_private:
  127. for privateIp in response_private:
  128. if privateIp["isPrimary"]:
  129. self.PRIVATE = response_private[0]
  130. break
  131. if not self.PRIVATE:
  132. print("Not Found Private IP Address.")
  133. exit(1)
  134. def getPublicIP(self):
  135. url = self.apiDict["URL"] + "publicIps/actions/getByPrivateIpId"
  136. BodyOne = json.dumps({"privateIpId": self.PRIVATE["id"]}, ensure_ascii=False)
  137. response = oracle.api("POST", url, keyID=self.apiKey, privateKey=self.privateKey, data=BodyOne)
  138. if response.code >= 200 and response.code < 400:
  139. response_public = json.loads(response.read().decode())
  140. return response_public
  141. elif response.code >= 400 and response.code < 500:
  142. return None
  143. else:
  144. print("Server Error. [%s]" % response.code)
  145. exit(1)
  146. def delPublicIPbyAddress(self):
  147. if not self.instancesId:
  148. print("Require Address.")
  149. exit(1)
  150. url = self.apiDict["URL"] + "publicIps/actions/getByIpAddress"
  151. BodyOne = json.dumps({"ipAddress": self.instancesId}, ensure_ascii=False)
  152. response = oracle.api("POST", url, keyID=self.apiKey, privateKey=self.privateKey, data=BodyOne)
  153. response_json = json.loads(response.read().decode())
  154. if "id" in response_json:
  155. self.delPublicIP(publicIp=response_json["id"])
  156. else:
  157. print(response_json)
  158. def delPublicIP(self, publicIp):
  159. url = self.apiDict["URL"] + "publicIps/" + publicIp
  160. oracle.api("DELETE", url, keyID=self.apiKey, privateKey=self.privateKey)
  161. def newPublicIP(self):
  162. bodyTwo = {
  163. "lifetime": "EPHEMERAL",
  164. "compartmentId": self.apiDict["compartmentId"],
  165. "privateIpId": self.PRIVATE["id"],
  166. }
  167. url = self.apiDict["URL"] + "publicIps"
  168. BodyTwo = json.dumps(bodyTwo, ensure_ascii=False)
  169. response = oracle.api("POST", url, keyID=self.apiKey, privateKey=self.privateKey, data=BodyTwo)
  170. NewPublic = json.loads(response.read().decode())
  171. print("PublicIP:", NewPublic["ipAddress"] if "ipAddress" in NewPublic else "NULL")
  172. return NewPublic if "ipAddress" in NewPublic else "NULL"
  173. def showPublicIP(self, showLog=True):
  174. self.getPrivateIP()
  175. PUBLIC = self.getPublicIP()
  176. publicIp = "NULL"
  177. if PUBLIC:
  178. publicIp = PUBLIC["ipAddress"]
  179. if showLog:
  180. print("PublicIP: %s" % publicIp)
  181. return PUBLIC
  182. def changePubilcIP(self):
  183. self.getPrivateIP()
  184. PUBLIC = self.getPublicIP()
  185. publicIp = "NULL"
  186. if PUBLIC:
  187. publicIp = PUBLIC["ipAddress"]
  188. self.delPublicIP(PUBLIC["id"])
  189. print("PublicIP[*]: %s" % publicIp)
  190. PUBLIC = self.newPublicIP()
  191. return PUBLIC
  192. def delete(self):
  193. if not self.instancesId:
  194. print("Require instancesId.")
  195. exit(1)
  196. url = self.apiDict["URL"] + "instances/" + self.instancesId + "?preserveBootVolume=false"
  197. response = oracle.api("DELETE", url, keyID=self.apiKey, privateKey=self.privateKey, data=None)
  198. response_json = {}
  199. response_json["status_code"] = str(response.code)
  200. response_json["data"] = response.read().decode()
  201. if response_json["status_code"] == "204":
  202. print("Delete success! ")
  203. else:
  204. print(json.dumps(response_json, indent=4))
  205. def reboot(self):
  206. if not self.instancesId:
  207. print("Require instancesId.")
  208. exit(1)
  209. url = self.apiDict["URL"] + "instances/" + self.instancesId + "?action=RESET"
  210. response = oracle.api("POST", url, keyID=self.apiKey, privateKey=self.privateKey, data=None)
  211. response_json = json.loads(response.read().decode())
  212. response_json["status_code"] = str(response.code)
  213. if response_json["status_code"] == "200":
  214. itemItem = {}
  215. itemItem["displayName"] = response_json["displayName"]
  216. itemItem["shape"] = response_json["shape"]
  217. itemItem["lifecycleState"] = response_json["lifecycleState"]
  218. itemItem["id"] = response_json["id"]
  219. itemItem["timeCreated"] = response_json["timeCreated"]
  220. itemItem["actionStatus"] = "SUCCESS"
  221. else:
  222. itemItem = response_json
  223. print(json.dumps(itemItem, indent=4))
  224. def rename(self, newName, DisableMonitoring=True):
  225. if not self.instancesId:
  226. print("Require instancesId.")
  227. exit(1)
  228. setName = str(newName).strip()
  229. if not setName:
  230. print("Name Invalid.")
  231. exit(1)
  232. body = {"displayName": setName, "agentConfig": {"isMonitoringDisabled": DisableMonitoring}}
  233. url = self.apiDict["URL"] + "instances/" + self.instancesId
  234. Body = json.dumps(body, ensure_ascii=False)
  235. response = oracle.api("PUT", url, keyID=self.apiKey, privateKey=self.privateKey, data=Body)
  236. response_json = json.loads(response.read().decode())
  237. response_json["status_code"] = str(response.code)
  238. print(json.dumps(response_json, indent=4))
  239. def getInstances(self):
  240. url = self.apiDict["URL"] + "instances?compartmentId=" + self.apiDict["compartmentId"]
  241. response = oracle.api("GET", url, keyID=self.apiKey, privateKey=self.privateKey)
  242. response_json = json.loads(response.read().decode())
  243. InstancesItem = []
  244. for item in response_json:
  245. if "id" not in item:
  246. continue
  247. itemItem = {}
  248. itemItem["displayName"] = item["displayName"]
  249. itemItem["shape"] = item["shape"]
  250. itemItem["lifecycleState"] = item["lifecycleState"]
  251. itemItem["id"] = item["id"]
  252. itemItem["timeCreated"] = item["timeCreated"]
  253. InstancesItem.append(itemItem)
  254. return InstancesItem
  255. def createInstancesConfig(self, Name=None):
  256. self.instancesDict = {
  257. 'displayName': str(str(self.configDict["availabilityDomain"]).split(":", 1)[-1].split("-")[1]),
  258. 'shape': self.configDict["shape"],
  259. 'compartmentId': self.apiDict["compartmentId"],
  260. 'availabilityDomain': self.configDict["availabilityDomain"],
  261. 'sourceDetails': {
  262. 'sourceType': 'image',
  263. 'imageId': self.configDict['imageId'],
  264. },
  265. 'createVnicDetails': {
  266. 'subnetId': self.configDict['subnetId'],
  267. 'assignPublicIp': True
  268. },
  269. 'metadata': {
  270. 'user_data': self.configDict['user_data'],
  271. 'ssh_authorized_keys': self.configDict['ssh_authorized_keys'],
  272. },
  273. 'agentConfig': {
  274. 'isMonitoringDisabled': False,
  275. 'isManagementDisabled': False
  276. },
  277. }
  278. if self.configDict["shape"] == "VM.Standard.A1.Flex":
  279. self.instancesDict["shapeConfig"] = self.configDict["shapeConfig"]
  280. if Name is not None and str(Name).strip() != "":
  281. self.instancesDict['displayName'] = str(Name).strip()
  282. def createInstance(self, Name=None):
  283. url = self.apiDict["URL"] + "instances"
  284. if not self.instancesDict or Name is not None:
  285. self.createInstancesConfig(Name=Name)
  286. body = json.dumps(self.instancesDict, ensure_ascii=False)
  287. isBreak = False
  288. while not isBreak:
  289. sleepTime = 0
  290. try:
  291. response = oracle.api("POST", url, keyID=self.apiKey, privateKey=self.privateKey, data=body)
  292. response_json = json.loads(response.read().decode())
  293. response_json["status_code"] = str(response.code)
  294. except Exception as e:
  295. print(e)
  296. response_json = {"code": "InternalError", "message": "Timeout.", "status_code": "555"}
  297. if str(response_json["status_code"]).startswith("4"):
  298. if str(response_json["status_code"]) == "401":
  299. response_json["message"] = "Not Authenticated."
  300. isBreak = True
  301. elif str(response_json["status_code"]) == "400":
  302. if str(response_json["code"]) == "LimitExceeded":
  303. response_json["message"] = "Limit Exceeded."
  304. elif str(response_json["code"]) == "QuotaExceeded":
  305. response_json["message"] = "Quota Exceeded."
  306. isBreak = True
  307. elif str(response_json["status_code"]) == "404":
  308. isBreak = True
  309. elif str(response_json["status_code"]) == "429":
  310. sleepTime = 60
  311. if int(response_json["status_code"]) < 300:
  312. vm_ocid = str(str(response_json["id"]).split(".")[-1])
  313. print(str("{} [{}] {}").format(time.strftime("[%Y/%m/%d %H:%M:%S]", time.localtime()), response_json["status_code"], str(vm_ocid[:5] + "..." + vm_ocid[-7:])))
  314. isBreak = True
  315. continue
  316. else:
  317. print(str("{} [{}] {}").format(time.strftime("[%Y/%m/%d %H:%M:%S]", time.localtime()), response_json["status_code"], response_json["message"]))
  318. if sleepTime > 0:
  319. time.sleep(sleepTime)
  320. else:
  321. time.sleep(30)
  322. if __name__ == "__main__":
  323. def Exit(code=0, msg=None):
  324. if msg:
  325. print(msg)
  326. exit(code)
  327. import argparse
  328. parser = argparse.ArgumentParser()
  329. parser.add_argument('-c', type=str, default="", help="Config Path")
  330. parser.add_argument('-i', type=str, default="", help="Instances Id or Instances Config Path")
  331. parser.add_argument('-n', type=str, default="", help="New Instances Name")
  332. parser.add_argument('-p', type=str, default="", help="IP Address Prefix")
  333. parser.add_argument('-a', type=str, default="", help="Action [show, change, rename, create, reboot, delete, deladdr, target, list, listaddr]")
  334. args = parser.parse_args()
  335. configPath = str(args.c).strip()
  336. configAction = str(args.a).strip().lower()
  337. configInstancesId = str(args.i).strip()
  338. configInstancesName = str(args.n).strip()
  339. configAddress = str(args.p).strip()
  340. configActionList = ["show", "change", "rename", "create", "reboot", "delete", "deladdr", "target", "list", "listaddr"]
  341. if not configPath:
  342. Exit(1, "Require Config Path.")
  343. if not configAction or configAction not in configActionList:
  344. Exit(1, "Invalid Action.")
  345. if not configAction.startswith("list") and not configInstancesId:
  346. Exit(1, "Require Instances Id or Instances Config Path.")
  347. if configAction == "loop" and not configAddress:
  348. configAction = "change"
  349. if configAction == "show":
  350. Action = Action(apiDict=oracle.load_Config(configPath), instancesId=configInstancesId)
  351. Action.showPublicIP()
  352. elif configAction == "change":
  353. Action = Action(apiDict=oracle.load_Config(configPath), instancesId=configInstancesId)
  354. Action.changePubilcIP()
  355. elif configAction == "rename":
  356. if not configInstancesName:
  357. Exit(1, "Require Instances Name.")
  358. Action = Action(apiDict=oracle.load_Config(configPath), instancesId=configInstancesId)
  359. Action.rename(configInstancesName)
  360. elif configAction == "reboot":
  361. Action = Action(apiDict=oracle.load_Config(configPath), instancesId=configInstancesId)
  362. Action.reboot()
  363. elif configAction == "delete":
  364. Action = Action(apiDict=oracle.load_Config(configPath), instancesId=configInstancesId)
  365. Action.delete()
  366. elif configAction == "deladdr":
  367. Action = Action(apiDict=oracle.load_Config(configPath), instancesId=configInstancesId)
  368. Action.delPublicIPbyAddress()
  369. elif configAction == "create":
  370. if not configInstancesName:
  371. configInstancesName = None
  372. else:
  373. configInstancesName = str(configInstancesName).strip()
  374. Action = Action(apiDict=oracle.load_Config(configPath), configDict=oracle.load_Config(configInstancesId))
  375. Action.createInstance(configInstancesName)
  376. elif configAction == "target":
  377. Action = Action(apiDict=oracle.load_Config(configPath), instancesId=configInstancesId)
  378. while True:
  379. NewPublic = Action.changePubilcIP()
  380. if isinstance(NewPublic, str):
  381. time.sleep(2)
  382. elif isinstance(NewPublic, dict) and str(NewPublic["ipAddress"]).startswith(configAddress):
  383. break
  384. else:
  385. del NewPublic
  386. time.sleep(5)
  387. elif configAction == "list":
  388. Action = Action(apiDict=oracle.load_Config(configPath))
  389. Item = Action.getInstances()
  390. print(json.dumps(Item, indent=4))
  391. elif configAction == "listaddr":
  392. Action = Action(apiDict=oracle.load_Config(configPath))
  393. Item = Action.getInstances()
  394. ItemWithAddress = []
  395. try:
  396. for item in Item.copy():
  397. if item["lifecycleState"] == "TERMINATED":
  398. continue
  399. Action.instancesId = item["id"]
  400. Action.PRIVATE = None
  401. Action.VNIC = None
  402. Action.getPrivateIP()
  403. PUBLIC = Action.getPublicIP()
  404. item["ipAddress"] = "NULL"
  405. if PUBLIC:
  406. item["ipAddress"] = PUBLIC["ipAddress"]
  407. ItemWithAddress.append(item)
  408. except:
  409. print(json.dumps(Item, indent=4))
  410. Exit(0)
  411. print(json.dumps(ItemWithAddress, indent=4))
  412. Exit(0)