Guest User

Untitled

a guest
Jun 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Control Cluster Creation via API"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": null,
  13. "metadata": {
  14. "collapsed": true
  15. },
  16. "outputs": [],
  17. "source": [
  18. "import digitalocean\n",
  19. "import base64\n",
  20. "import paramiko\n",
  21. "from datetime import datetime"
  22. ]
  23. },
  24. {
  25. "cell_type": "markdown",
  26. "metadata": {},
  27. "source": [
  28. "### Init API\n",
  29. "* configure **token_secret**"
  30. ]
  31. },
  32. {
  33. "cell_type": "code",
  34. "execution_count": null,
  35. "metadata": {
  36. "collapsed": true
  37. },
  38. "outputs": [],
  39. "source": [
  40. "token_secret=\"REPLACE_WITH_YOUR_TOKEN\"\n",
  41. "manager = digitalocean.Manager(token=token_secret)"
  42. ]
  43. },
  44. {
  45. "cell_type": "markdown",
  46. "metadata": {},
  47. "source": [
  48. "### Get snapshot image\n",
  49. "* Prebuilt spark slave and take snapshot\n",
  50. "* Get the built image id into **image_id**"
  51. ]
  52. },
  53. {
  54. "cell_type": "code",
  55. "execution_count": null,
  56. "metadata": {},
  57. "outputs": [],
  58. "source": [
  59. "snapshots = manager.get_my_images()\n",
  60. "image_id = snapshots[0].id\n",
  61. "print(snapshots[0].id)"
  62. ]
  63. },
  64. {
  65. "cell_type": "markdown",
  66. "metadata": {},
  67. "source": [
  68. "### Create instances\n",
  69. "* Based on snapshot image retrieved from above\n",
  70. "* Specify the **total_instance** to be created\n",
  71. "* size and region can be configured too\n",
  72. "* Assumed here, you have ssh key created"
  73. ]
  74. },
  75. {
  76. "cell_type": "code",
  77. "execution_count": null,
  78. "metadata": {},
  79. "outputs": [],
  80. "source": [
  81. "##create instance\n",
  82. "keys = manager.get_all_sshkeys()\n",
  83. "total_instance = 5\n",
  84. "size_slug='s-1vcpu-2gb'\n",
  85. "region='sgp1' # Singapore\n",
  86. "names = []\n",
  87. "for i in range(total_instance):\n",
  88. " num = i\n",
  89. " if i < 9:\n",
  90. " num = \"0\"+str(num)\n",
  91. " else:\n",
  92. " num = str(num)\n",
  93. " names.append('hadoop-spark-slave-'+num)\n",
  94. "output = digitalocean.Droplet.create_multiple(\n",
  95. " token=token_secret,\n",
  96. " names=names,\n",
  97. " region=region, \n",
  98. " image=image_id, \n",
  99. " #size_slug='s-2vcpu-2gb', #2GB RAM, 2CPU\n",
  100. " size_slug=size_slug,\n",
  101. " backups=False,\n",
  102. " ssh_keys=keys,\n",
  103. " ipv6=True,\n",
  104. " private_networking=True,\n",
  105. " tags=['hadoop-slave']\n",
  106. ")\n",
  107. "print(output)\n",
  108. "print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))"
  109. ]
  110. },
  111. {
  112. "cell_type": "markdown",
  113. "metadata": {},
  114. "source": [
  115. "### Get IP addresses of the instances\n",
  116. "* Here, assumed all droplets are part of cluster\n",
  117. "* Master is tagged with **hadoop**\n",
  118. "* Slave is tagged with **hadoop-slave**"
  119. ]
  120. },
  121. {
  122. "cell_type": "code",
  123. "execution_count": null,
  124. "metadata": {
  125. "collapsed": true
  126. },
  127. "outputs": [],
  128. "source": [
  129. "my_droplets = manager.get_all_droplets()\n",
  130. "slaves = []\n",
  131. "master = {}\n",
  132. "slaves_str = ''\n",
  133. "for i in my_droplets:\n",
  134. " slaved = {}\n",
  135. " #print(i.tags)\n",
  136. " slaved = {'name':i.name,'id':i.id,'pure_slave':False}\n",
  137. " if 'hadoop-slave' in i.tags:\n",
  138. " slaved['pure_slave'] = True\n",
  139. " \n",
  140. " for j in i.networks['v4']:\n",
  141. " if j['type'] == 'private':\n",
  142. " #print(j['ip_address'] + \" #\" + i.name)\n",
  143. " slaves_str = slaves_str + j['ip_address'] + \" #\" + i.name + \"\\n\"\n",
  144. " slaved['private_ip'] = j['ip_address']\n",
  145. " else:\n",
  146. " slaved['public_ip'] = j['ip_address']\n",
  147. " if slaved['pure_slave']:\n",
  148. " slaves.append(slaved)\n",
  149. " else:\n",
  150. " master = slaved"
  151. ]
  152. },
  153. {
  154. "cell_type": "markdown",
  155. "metadata": {},
  156. "source": [
  157. "### Init SSH\n",
  158. "* Replace the path of private key file (the spark.pem below)\n",
  159. "* Replace the hadoop and spark base path"
  160. ]
  161. },
  162. {
  163. "cell_type": "code",
  164. "execution_count": null,
  165. "metadata": {
  166. "collapsed": true
  167. },
  168. "outputs": [],
  169. "source": [
  170. "k = paramiko.RSAKey.from_private_key_file(\"/Users/mahadir/.ssh/spark.pem\")\n",
  171. "user = 'root'\n",
  172. "hadoop_path = '/root/hadoop-3.1.0'\n",
  173. "spark_path = '/root/spark-2.3.0-bin-hadoop2.7'\n",
  174. "spark_master_port = 7077\n",
  175. "\n",
  176. "def send_command(private_ip,public_ip,user,k,cmds):\n",
  177. " print(\"connecting to \"+public_ip+\"..\")\n",
  178. " client = paramiko.SSHClient()\n",
  179. " client.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n",
  180. " client.connect(public_ip, username = user, pkey = k)\n",
  181. " for cmd in cmds:\n",
  182. " print(cmd)\n",
  183. " stdin, stdout, stderr = client.exec_command(cmd)\n",
  184. " for line in stdout:\n",
  185. " print('... ' + line.strip('\\n'))\n",
  186. " for line in stderr:\n",
  187. " print('... ' + line.strip('\\n'))\n",
  188. " client.close()\n",
  189. " print(\"closed connection to \"+public_ip+\"..\")"
  190. ]
  191. },
  192. {
  193. "cell_type": "markdown",
  194. "metadata": {},
  195. "source": [
  196. "### Start Master Node\n",
  197. "* You may skip this step if the master node already up and running with spark & hadoop\n",
  198. "* Note that I only have one hdfs instance and I don't start spark slave process here"
  199. ]
  200. },
  201. {
  202. "cell_type": "code",
  203. "execution_count": null,
  204. "metadata": {},
  205. "outputs": [],
  206. "source": [
  207. "#start master only\n",
  208. "cmds = [\n",
  209. " spark_path+'/sbin/start-master.sh',\n",
  210. " hadoop_path+'/sbin/start-dfs.sh',\n",
  211. " hadoop_path+'/sbin/start-yarn.sh'\n",
  212. " ]\n",
  213. "private_ip = master['private_ip']\n",
  214. "public_ip = master['public_ip']\n",
  215. "send_command(private_ip,public_ip,user,k,cmds)"
  216. ]
  217. },
  218. {
  219. "cell_type": "markdown",
  220. "metadata": {},
  221. "source": [
  222. "### Start Slave Nodes\n",
  223. "* Only start spark slaves"
  224. ]
  225. },
  226. {
  227. "cell_type": "code",
  228. "execution_count": null,
  229. "metadata": {},
  230. "outputs": [],
  231. "source": [
  232. "#start apache spark on slaves only\n",
  233. "for node in slaves:\n",
  234. " if node['pure_slave'] == False:\n",
  235. " continue\n",
  236. " private_ip = node['private_ip']\n",
  237. " public_ip = node['public_ip']\n",
  238. " cmds = [spark_path+'/sbin/start-slave.sh spark://'+master['private_ip']+':'+str(spark_master_port)]\n",
  239. " send_command(private_ip,public_ip,user,k,cmds)"
  240. ]
  241. },
  242. {
  243. "cell_type": "markdown",
  244. "metadata": {},
  245. "source": [
  246. "### Delete droplets\n",
  247. "* Delete only the droplets tagged with **hadoop-slave**"
  248. ]
  249. },
  250. {
  251. "cell_type": "code",
  252. "execution_count": null,
  253. "metadata": {
  254. "collapsed": true
  255. },
  256. "outputs": [],
  257. "source": [
  258. "###delete instance\n",
  259. "disposble_droplets = manager.get_all_droplets(tag_name='hadoop-slave')\n",
  260. "for i in disposble_droplets:\n",
  261. " i.destroy()\n",
  262. "print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))"
  263. ]
  264. }
  265. ],
  266. "metadata": {
  267. "kernelspec": {
  268. "display_name": "Python 3",
  269. "language": "python",
  270. "name": "python3"
  271. },
  272. "language_info": {
  273. "codemirror_mode": {
  274. "name": "ipython",
  275. "version": 3
  276. },
  277. "file_extension": ".py",
  278. "mimetype": "text/x-python",
  279. "name": "python",
  280. "nbconvert_exporter": "python",
  281. "pygments_lexer": "ipython3",
  282. "version": "3.5.3"
  283. }
  284. },
  285. "nbformat": 4,
  286. "nbformat_minor": 2
  287. }
Add Comment
Please, Sign In to add comment