Commit 29b4264d authored by Ioannis Tsafaras's avatar Ioannis Tsafaras
Browse files

Add some path options, delete public ip on destroy

parent 888147fa
import logging
from fokia.vm_manager import VM_Manager
from fokia.ansible_manager_minimal import Manager
import os
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
check_folders = ['/var/www/okeanos-LoD/central_service/ansible',
'okeanos-LoD/central_service/ansible',
'central_service/ansible',
'../central_service/ansible',
'../../central_service/ansible']
ansible_path = os.environ.get('CENTRAL_ANSIBLE_PATH', None)
if not ansible_path:
for folder in check_folders:
if os.path.exists(folder):
ansible_path = folder
break
class CentralServiceManager:
"""
......@@ -13,34 +27,33 @@ class CentralServiceManager:
the ~okeanos infrastructure.
"""
def central_service_create(self, auth_token):
def central_service_create(self, auth_token, vcpus=4, ram=4096, disk=40,
project_name='lambda.grnet.gr',
private_key_path=None, public_key_path=None):
"""
Creates the central service vm and installs the relevant s/w.
:return:
"""
provisioner = VM_Manager(auth_token=auth_token)
vm_name = 'central_service'
vcpus = 4
ram = 4096
disk = 40
project_name = 'lambda.grnet.gr'
server_id = provisioner.create_single_vm(vm_name=vm_name,
vcpus=vcpus, ram=ram, disk=disk,
project_name=project_name)
project_name=project_name,
public_key_path=public_key_path)
hostname = 'snf-' + str(server_id) + '.vm.okeanos.grnet.gr'
group = 'central-vm'
ansible_manager = Manager(hostname, group)
ansible_manager = Manager(hostname, group, private_key_path)
ansible_result = ansible_manager.run_playbook(
playbook_file='../../central_service/ansible/playbooks/setup.yml')
playbook_file=os.path.join(ansible_path, 'playbooks', 'setup.yml'))
return ansible_result
def central_service_destroy(self, auth_token, vm_id, public_ip_id):
def central_service_destroy(self, auth_token, vm_id):
"""
Deletes the central service vm.
:return:
"""
vmmanager = VM_Manager(auth_token=auth_token)
vmmanager.destroy(vm_id=vm_id, public_ip_id=public_ip_id)
vmmanager.destroy(vm_id=vm_id)
def central_service_start(self, auth_token, vm_id):
"""
......
......@@ -6,10 +6,11 @@ from ansible import utils
class Manager:
def __init__(self, host, group):
private_key_file = os.path.expanduser('~/.ssh/id_rsa')
def __init__(self, host, group, private_key_path=None):
if private_key_path is None:
private_key_path = os.path.expanduser('~/.ssh/id_rsa')
ansible.constants.DEFAULT_TIMEOUT = 30
ansible.constants.DEFAULT_PRIVATE_KEY_FILE = private_key_file
ansible.constants.DEFAULT_PRIVATE_KEY_FILE = private_key_path
ansible.constants.HOST_KEY_CHECKING = False
self.ansible_inventory = ansible.inventory.Inventory(host_list=[host])
......
......@@ -12,7 +12,7 @@ class VM_Manager(ProvisionerBase):
def __init__(self, auth_token, cloud_name=None):
super(VM_Manager, self).__init__(auth_token=auth_token, cloud_name=cloud_name)
def create_single_vm(self, vm_name, wait=True, **kwargs):
def create_single_vm(self, vm_name, wait=True, public_key_path=None, **kwargs):
"""
Creates a single VM
:return: vm id
......@@ -37,7 +37,9 @@ class VM_Manager(ProvisionerBase):
public_ip = self.reserve_ip(project_id=project_id)
with open(os.path.expanduser('~/.ssh/id_rsa.pub'), 'r') as public_key_file:
if public_key_path is None:
public_key_path = os.path.expanduser('~/.ssh/id_rsa.pub')
with open(public_key_path, 'r') as public_key_file:
public_key = public_key_file.read()
authorized = {'contents': b64encode(public_key),
'path': '/root/.ssh/authorized_keys',
......@@ -112,13 +114,19 @@ class VM_Manager(ProvisionerBase):
raise ClientError(msg, error_get_ip)
return True
def destroy(self, vm_id, public_ip_id=None):
def destroy(self, vm_id):
"""
Destroys a single VM
:return:
"""
public_ip_id = None
# Get the current status of the VM
vm_status = self.cyclades.get_server_details(vm_id)["status"]
vm_status = self.cyclades.get_server_details(vm_id)['status']
floating_ips = self.network_client.list_floatingips()
for ip in floating_ips:
if int(ip['instance_id']) == vm_id:
public_ip_id = int(ip['id'])
# Destroy the VM
if vm_status != "DELETED":
......@@ -127,7 +135,7 @@ class VM_Manager(ProvisionerBase):
# Wait for the VM to be destroyed before destroying the public ip
self.cyclades.wait_server(vm_id, current_status=vm_status, max_wait=600)
# Destroy the public ip, if it exists
# Destroy the public ip that was attached to the VM
if public_ip_id is not None:
self.network_client.delete_floatingip(public_ip_id)
......
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