Commit 51d80e52 authored by Florian Kempenich's avatar Florian Kempenich
Browse files

Move location of the generated inventory

parent 513ca043
# This is a generated ansible inventory of all droplet created with the `ansible-droplet` tool
#
# You can link this inventory in your own playbook, and run tasks/roles out of the box.
#
...@@ -54,9 +54,9 @@ ...@@ -54,9 +54,9 @@
Hostname {{ created.droplet.ip_address }} Hostname {{ created.droplet.ip_address }}
User root User root
- name: "Add Droplet as Ansible Host in `inventory/ansible-machines`" - name: "Add Droplet as Ansible Host in `~/.ansible-droplet-inventory`"
blockinfile: blockinfile:
path: "{{ ansible_env.PWD }}/inventory/ansible-machines" path: "{{ ansible_env.HOME }}/.ansible-droplet-inventory"
state: present state: present
create: yes create: yes
marker: "# {mark} Ansible-managed DigitalOcean Droplet: '{{ droplet_name }}'" marker: "# {mark} Ansible-managed DigitalOcean Droplet: '{{ droplet_name }}'"
......
...@@ -35,9 +35,8 @@ ...@@ -35,9 +35,8 @@
state: absent state: absent
marker: "# {mark} Ansible-managed DigitalOcean Droplet: '{{ droplet_name }}'" marker: "# {mark} Ansible-managed DigitalOcean Droplet: '{{ droplet_name }}'"
- name: "Remove Droplet from Ansible Host in `inventory/ansible-machines`" - name: "Remove Droplet from Ansible Host in `~/.ansible-droplet-inventory`"
blockinfile: blockinfile:
path: "{{ ansible_env.PWD }}/inventory/ansible-machines" path: "{{ ansible_env.HOME }}/.ansible-droplet-inventory"
state: absent state: absent
marker: "# {mark} Ansible-managed DigitalOcean Droplet: '{{ droplet_name }}'" marker: "# {mark} Ansible-managed DigitalOcean Droplet: '{{ droplet_name }}'"
import click import click
import os.path import os.path
from os import symlink
import subprocess import subprocess
SCRIPT_DIR = os.path.dirname(__file__) SCRIPT_DIR = os.path.dirname(__file__)
...@@ -8,6 +9,9 @@ ANSIBLE = os.path.join(SCRIPT_DIR, 'ansible') ...@@ -8,6 +9,9 @@ ANSIBLE = os.path.join(SCRIPT_DIR, 'ansible')
REQUIREMENTS = 'roles/roles-on-droplet/roles-from-ansible-galaxy' REQUIREMENTS = 'roles/roles-on-droplet/roles-from-ansible-galaxy'
CONFIGURATION = 'group_vars' CONFIGURATION = 'group_vars'
CONFIGURATION_FILE = 'all' CONFIGURATION_FILE = 'all'
INVENTORY = 'inventory'
INVENTORY_DROPLETS_LINK = 'ansible-droplets'
INVENTORY_DROPLETS_FILE = '.ansible-droplet-inventory'
CREATE_PLAYBOOK = 'create-droplet-playbook.yml' CREATE_PLAYBOOK = 'create-droplet-playbook.yml'
DESTROY_PLAYBOOK = 'delete-droplet-playbook.yml' DESTROY_PLAYBOOK = 'delete-droplet-playbook.yml'
...@@ -34,6 +38,9 @@ def _set_configuration_if_needed(): ...@@ -34,6 +38,9 @@ def _set_configuration_if_needed():
ssh_key, do_token = _ask_for_configuration() ssh_key, do_token = _ask_for_configuration()
_create_configuration_file(ssh_key, do_token) _create_configuration_file(ssh_key, do_token)
if not _has_droplet_inventory_symlink():
_create_droplet_inventory_symlink()
def _is_configured(): def _is_configured():
return os.path.isfile(os.path.join(ANSIBLE, CONFIGURATION, CONFIGURATION_FILE)) return os.path.isfile(os.path.join(ANSIBLE, CONFIGURATION, CONFIGURATION_FILE))
...@@ -51,6 +58,21 @@ def _create_configuration_file(ssh_key, do_token): ...@@ -51,6 +58,21 @@ def _create_configuration_file(ssh_key, do_token):
file.write('ssh_pub_key_to_load_on_droplet_location: "{0}"\n'.format(ssh_key)) file.write('ssh_pub_key_to_load_on_droplet_location: "{0}"\n'.format(ssh_key))
file.write('digitalocean_token_location: "{0}"\n'.format(do_token)) file.write('digitalocean_token_location: "{0}"\n'.format(do_token))
def _has_droplet_inventory_symlink():
return os.path.islink(os.path.join(ANSIBLE, INVENTORY, INVENTORY_DROPLETS_LINK))
def _create_droplet_inventory_symlink():
inventory_link = os.path.join(ANSIBLE, INVENTORY, INVENTORY_DROPLETS_LINK)
inventory_file = os.path.join(os.path.expanduser("~"), INVENTORY_DROPLETS_FILE)
if not os.path.isfile(inventory_file):
raise AssertionError(
"Please ensure you didn't delete the generated inventory in the HOME dir\n" +
"In case you did, just create a new empty file at '{0}'".format(inventory_file)
)
symlink(inventory_file, inventory_link)
def _delete_current_configuration(): def _delete_current_configuration():
try: try:
os.remove(os.path.join(ANSIBLE, CONFIGURATION, CONFIGURATION_FILE)) os.remove(os.path.join(ANSIBLE, CONFIGURATION, CONFIGURATION_FILE))
......
from setuptools import setup from setuptools import setup
from os.path import expanduser
HOME = expanduser("~")
def readme(): def readme():
with open('README.md') as f: with open('README.md') as f:
...@@ -6,7 +9,7 @@ def readme(): ...@@ -6,7 +9,7 @@ def readme():
setup( setup(
name='ansible-droplet', name='ansible-droplet',
version='0.1.3', version='0.2.0',
description='A cli to Create / Destroy DigitalOcean Droplets', description='A cli to Create / Destroy DigitalOcean Droplets',
long_description=readme(), long_description=readme(),
keywords='digitalocean digital ocean droplet ansible ssh provision', keywords='digitalocean digital ocean droplet ansible ssh provision',
...@@ -29,7 +32,7 @@ setup( ...@@ -29,7 +32,7 @@ setup(
'dopy==0.3.5', 'dopy==0.3.5',
'click' 'click'
], ],
data_files=[(HOME, ['.ansible-droplet-inventory'])],
include_package_data=True, include_package_data=True,
zip_safe=False zip_safe=False
) )
# TODO: Investigate the `zip_safe` option
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment