Last active
October 22, 2016 17:42
-
-
Save rdhyee/7011434 to your computer and use it in GitHub Desktop.
An IPython notebook that I use to create a digitalocean.com droplet to host a minecraft server. I've currently hardcoded a reference to the Ansible Playbook, which I've posted at https://gist.github.com/rdhyee/66f1aa40b6ea520c9e6e. There's code to use the Ansible Python API to play the playbook -- though clearly I can use the command line to do …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"An IPython notebook that I use to create a digitalocean.com droplet to host a minecraft server. I've currently hardcoded a reference to the Ansible Playbook, which I've posted at https://gist.github.com/rdhyee/66f1aa40b6ea520c9e6e. There's code to use the Ansible Python API to play the playbook -- though clearly I can use the command line to do so. I've also been using the digitalocean Python module to launch a droplet. Ansible has facilities to launch droplets, but I've not used those capabilities yet." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"https://github.com/koalalorenzo/python-digitalocean\n", | |
"\n", | |
"To install:\n", | |
"\n", | |
" pip install -U python-digitalocean" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import digitalocean\n", | |
"from digitalocean_key import CLIENT_ID, API_KEY\n", | |
"\n", | |
"manager = digitalocean.Manager(client_id=CLIENT_ID, api_key=API_KEY)\n", | |
"\n", | |
"global_images = dict([(image.name, image) for image in manager.get_global_images()])\n", | |
"all_regions = dict([(region.name, region) for region in manager.get_all_regions()])\n", | |
"all_sizes = dict([(size.name, size) for size in manager.get_all_sizes()])\n", | |
"all_ssh_keys = dict([(key.name, key) for key in manager.get_all_sshkeys()])\n", | |
"\n", | |
"\n", | |
"def make_standard_droplet(name, size='512MB'):\n", | |
" droplet = digitalocean.Droplet(client_id=CLIENT_ID,\n", | |
" api_key=API_KEY,\n", | |
" name = name,\n", | |
" region_id=all_regions['San Francisco 1'].id, #San Francisco 1\n", | |
" image_id=global_images['Ubuntu 12.04.3 x64'].id, #Ubuntu 12.04.3 x64 Server\n", | |
" size_id=all_sizes[size].id, #512MB\n", | |
" backup_active=False)\n", | |
"\n", | |
" droplet.create(ssh_key_ids=all_ssh_keys['RY laptop'].id)\n", | |
" \n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"[(size_label, size.id) for (size_label, size) in all_sizes.iteritems()]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"global_images" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# create a droplet\n", | |
"\n", | |
"droplets_to_create = ['minecraft1']\n", | |
"\n", | |
"for name in droplets_to_create:\n", | |
" make_standard_droplet(name)\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import digitalocean\n", | |
"from digitalocean_key import CLIENT_ID, API_KEY\n", | |
"\n", | |
"manager = digitalocean.Manager(client_id=CLIENT_ID, api_key=API_KEY)\n", | |
"\n", | |
"droplets = manager.get_all_droplets()\n", | |
"[(droplet.id, droplet.name, droplet.ip_address) for droplet in droplets]\n", | |
"\n", | |
"instance = droplets[0]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# copy a command to ssh into the instance\n", | |
"\n", | |
"cmdstring = \"ssh -oStrictHostKeyChecking=no root@{0}\".format(instance.ip_address)\n", | |
"# works on a mac\n", | |
"! echo \"$cmdstring\" | pbcopy\n", | |
"cmdstring" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from ansible.playbook import PlayBook\n", | |
"from ansible import callbacks\n", | |
"from ansible import utils\n", | |
"\n", | |
"import jinja2\n", | |
"from tempfile import NamedTemporaryFile\n", | |
"import os\n", | |
"\n", | |
"# this example fills in mandatory params: playbook, callbacks, runner_callbacks, stats \n", | |
"# drawing inspiration from https://github.com/ansible/ansible/blob/release1.3.3/bin/ansible-playbook#L138\n", | |
"\n", | |
"playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)\n", | |
"stats = callbacks.AggregateStats()\n", | |
"runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)\n", | |
"\n", | |
"# let's dynamically generate the host_list\n", | |
"\n", | |
"HOST_LIST_TEMPLATE = \"\"\"\n", | |
"[local]\n", | |
"localhost\n", | |
"\n", | |
"[{{group_name}}]\n", | |
"{{ip_address}}\n", | |
"\"\"\"\n", | |
"\n", | |
"template = jinja2.Template(HOST_LIST_TEMPLATE)\n", | |
"\n", | |
"from tempfile import NamedTemporaryFile\n", | |
"\n", | |
"temp = NamedTemporaryFile(delete=False)\n", | |
"temp.write(template.render({'group_name':'minecraft_do', 'ip_address':instance.ip_address}))\n", | |
"temp.close()\n", | |
"\n", | |
"playbook = PlayBook(remote_user='root',\n", | |
" playbook='/Users/raymondyee/D/Document/Working_with_Open_Data/working-open-data/notebooks/minecraft.yml',\n", | |
" callbacks=playbook_cb,\n", | |
" runner_callbacks=runner_cb,\n", | |
" stats=stats,\n", | |
" host_list=temp.name\n", | |
" )\n", | |
"\n", | |
"results = playbook.run()\n", | |
"\n", | |
"# clean up\n", | |
"os.remove(temp.name)\n", | |
"\n", | |
"results" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# run this cell if you want to destroy droplet\n", | |
"\n", | |
"for droplet in droplets:\n", | |
" droplet.destroy()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"Using dynamic inventory and command line invocation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import digitalocean\n", | |
"from digitalocean_key import CLIENT_ID, API_KEY\n", | |
"\n", | |
"manager = digitalocean.Manager(client_id=CLIENT_ID, api_key=API_KEY)\n", | |
"\n", | |
"global_images = dict([(image.name, image) for image in manager.get_global_images()])\n", | |
"all_regions = dict([(region.name, region) for region in manager.get_all_regions()])\n", | |
"all_sizes = dict([(size.name, size) for size in manager.get_all_sizes()])\n", | |
"all_ssh_keys = dict([(key.name, key) for key in manager.get_all_sshkeys()])\n", | |
"\n", | |
"\n", | |
"PLAYBOOK_PATH = '/Users/raymondyee/D/Document/Working_with_Open_Data/working-open-data/notebooks/minecraft_do.yml'\n", | |
"\n", | |
"EXTRA_VARS = {'ansible_client_id':CLIENT_ID,\n", | |
" 'ansible_api_key': API_KEY,\n", | |
" 'ssh_key_id': all_ssh_keys['RY laptop'].id,\n", | |
" 'image_id':global_images['Ubuntu 12.04 x64'].id,\n", | |
" 'region_id': all_regions['San Francisco 1'].id,\n", | |
" 'size_id': all_sizes['512MB'].id,\n", | |
" 'java_memory': '512M'\n", | |
" }\n", | |
"\n", | |
"extra_vars = \" \".join([\"{0}={1}\".format(k,v) for (k,v) in EXTRA_VARS.items()])\n", | |
"\n", | |
"!ansible-playbook --verbose -u root --extra-vars \"$extra_vars\" $PLAYBOOK_PATH" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"ansible_command = 'ansible-playbook --verbose -u root --extra-vars \"{0}\" {1}'.format(extra_vars, PLAYBOOK_PATH)\n", | |
"! echo '$ansible_command' | pbcopy\n", | |
"ansible_command" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"try:\n", | |
" import dopy\n", | |
" from dopy.manager import DoError, DoManager\n", | |
"except ImportError as e:\n", | |
" print \"failed=True msg='dopy >= 0.2.2 required for this module'\"\n", | |
" sys.exit(1)\n", | |
"\n", | |
"if dopy.__version__ < '0.2.2':\n", | |
" print \"failed=True msg='dopy >= 0.2.2 required for this module'\"\n", | |
" sys.exit(1)\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import dopy\n", | |
"from dopy.manager import DoError, DoManager\n", | |
"\n", | |
"dopy.__version__" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment