provisioner.py 8.55 KB
Newer Older
1 2 3
from __future__ import (absolute_import, division,
                        print_function, unicode_literals)
import logging
4

5 6 7 8
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

from kamaki.clients import astakos, cyclades
9 10 11
from kamaki.clients import ClientError
from kamaki.clients.utils import https
from kamaki.cli.config import Config as KamakiConfig
12
from fokia.cluster_error_constants import *
13

14
# TODO: remove this and actually use ssl cert files
15 16 17 18
https.patch_ignore_ssl()

import argparse

19
storage_templates = ['drdb', 'ext_vlmc']
20 21 22 23


class Provisioner:
    """
24
        provisions virtual machines on ~okeanos
25 26
    """

27
    def __init__(self, cloud_name):
28 29

        # Load .kamakirc configuration
30
        logger.info("Retrieving .kamakirc configuration")
31
        self.config = KamakiConfig()
32 33 34 35 36
        cloud_section = self.config._sections['cloud'].get(cloud_name)
        if not cloud_section:
            message = "Cloud '%s' was not found in you .kamakirc configuration file. " \
                      "Currently you have availablie in your configuration these clouds: %s"
            raise KeyError(message % (cloud_name, self.config._sections['cloud'].keys()))
37 38 39 40

        # Get the authentication url and token
        auth_url, auth_token = cloud_section['url'], cloud_section['token']

41
        logger.info("Initiating Astakos Client")
42 43
        self.astakos = astakos.AstakosClient(auth_url, auth_token)

44 45
        logger.info("Retrieving cyclades endpoint url")
        compute_url = self.astakos.get_endpoint_url(
46
            cyclades.CycladesComputeClient.service_type)
47 48
        logger.info("Initiating Cyclades client")
        self.cyclades = cyclades.CycladesComputeClient(compute_url, auth_token)
49

50 51 52 53 54 55 56 57 58
        # Create the network client
        networkURL = self.astakos.get_endpoint_url(
            cyclades.CycladesNetworkClient.service_type)
        self.network_client = cyclades.CycladesNetworkClient(networkURL, auth_token)

        # Constants
        self.Bytes_to_GB = 1024*1024*1024
        self.Bytes_to_MB = 1024*1024

59
    def find_flavor(self, **kwargs):
60 61
        """

62
        :param kwargs: should contains the keys that specify the specs
63
        :return: first flavor objects that matches the specs criteria
64
        """
65 66 67 68 69

        # Set all the default parameters
        kwargs.setdefault("vcpus", 1)
        kwargs.setdefault("ram", 1024)
        kwargs.setdefault("disk", 40)
70
        logger.info("Retrieving flavor")
71
        for flavor in self.cyclades.list_flavors(detail=True):
72 73
            if all([kwargs[key] == flavor[key] \
                    for key in set(flavor.keys()).intersection(kwargs.keys())]):
74 75 76
                return flavor
        return None

77
    def find_image(self, **kwargs):
78 79 80 81 82
        """
        :param image_name: Name of the image to filter by
        :param kwargs:
        :return: first image object that matches the name criteria
        """
83
        image_name = kwargs['image_name']
84 85

        logger.info("Retrieving image")
86
        for image in self.cyclades.list_images(detail=True):
87
            if image_name in image['name']:
88 89 90 91
                return image
        return None

    def find_project_id(self, **kwargs):
92 93 94 95
        """
        :param kwargs: name, state, owner and mode to filter project by
        :return: first project_id that matches the project name
        """
96
        filter = {
97 98 99 100
            'name': kwargs.get("project_name"),
            'state': kwargs.get("project_state"),
            'owner': kwargs.get("project_owner"),
            'mode': kwargs.get("project_mode"),
101
        }
102
        logger.info("Retrieving project")
103 104 105
        return self.astakos.get_projects(**filter)[0]

    def create_vm(self, vm_name=None, **kwargs):
106 107 108 109 110
        """
        :param vm_name: Name of the virtual machine to create
        :param kwargs: passed to the functions called for detail options
        :return:
        """
111 112 113 114
        flavor_id = self.find_flavor(**kwargs)['id']
        image_id = self.find_image(**kwargs)['id']
        project_id = self.find_project_id(**kwargs)['id']
        try:
115
            okeanos_response = self.cyclades.create_server(name=vm_name, flavor_id=flavor_id,
116 117 118
                                                           image_id=image_id,
                                                           project_id=project_id,
                                                           networks=[], personality=[])
119
        except ClientError as ex:
120 121
            raise ex
        return okeanos_response
122

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    def get_quotas(self, **kwargs):
        """
        Get the user quotas for the defined project.
        :return: user quotas object
        """
        return self.astakos.get_quotas()

    def check_all_resources(self, quotas, **kwargs):
        """
        Checks user's quota for every requested resource.
        Returns True if everything available.
        :param **kwargs: arguments
        """
        project_id = self.find_project_id(**kwargs)['id']
        # quotas = self.get_quotas()

        # Check for VMs
        pending_vm = quotas[project_id]['cyclades.vm']['pending']
        limit_vm = quotas[project_id]['cyclades.vm']['limit']
        usage_vm = quotas[project_id]['cyclades.vm']['usage']
        available_vm = limit_vm - usage_vm - pending_vm
        if available_vm < kwargs['cluster_size']:
            msg = 'Cyclades VMs out of limit'
            raise ClientError(msg, error_quotas_cluster_size)
            return False
        # Check for CPUs
        pending_cpu = quotas[project_id]['cyclades.cpu']['pending']
        limit_cpu = quotas[project_id]['cyclades.cpu']['limit']
        usage_cpu = quotas[project_id]['cyclades.cpu']['usage']
        available_cpu = limit_cpu - usage_cpu - pending_cpu
        if available_cpu < kwargs['vcpus']:
            msg = 'Cyclades cpu out of limit'
            raise ClientError(msg, error_quotas_cpu)
            return False
        # Check for RAM
        pending_ram = quotas[project_id]['cyclades.ram']['pending']
        limit_ram = quotas[project_id]['cyclades.ram']['limit']
        usage_ram = quotas[project_id]['cyclades.ram']['usage']
        available_ram = (limit_ram - usage_ram - pending_ram) / self.Bytes_to_MB
        if available_ram < kwargs['ram']:
            msg = 'Cyclades ram out of limit'
            raise ClientError(msg, error_quotas_ram)
            return False
        # Check for Disk space
        pending_cd = quotas[project_id]['cyclades.ram']['pending']
        limit_cd = quotas[project_id]['cyclades.disk']['limit']
        usage_cd = quotas[project_id]['cyclades.disk']['usage']
        available_cyclades_disk_GB = (limit_cd - usage_cd - pending_cd) / self.Bytes_to_GB
        if available_cyclades_disk_GB < kwargs['disk']:
            msg = 'Cyclades disk out of limit'
            raise ClientError(msg, error_quotas_cyclades_disk)
            return False
        # Check for public IPs
        list_float_ips = self.network_client.list_floatingips()
        pending_ips = quotas[project_id]['cyclades.floating_ip']['pending']
        limit_ips = quotas[project_id]['cyclades.floating_ip']['limit']
        usage_ips = quotas[project_id]['cyclades.floating_ip']['usage']
        available_ips = limit_ips - usage_ips - pending_ips
        for d in list_float_ips:
            if d['instance_id'] is None and d['port_id'] is None:
                available_ips += 1
        if available_ips < kwargs['ip_request']:
            msg = 'Public IPs out of limit'
            raise ClientError(msg, error_quotas_cyclades_disk)
            return False
        # Check for networks
        pending_net = quotas[project_id]['cyclades.network.private']['project_pending']
        limit_net = quotas[project_id]['cyclades.network.private']['project_limit']
        usage_net = quotas[project_id]['cyclades.network.private']['project_usage']
        available_networks = limit_net - usage_net - pending_net
        if available_networks < kwargs['network_request']:
            msg = 'Private Network out of limit'
            raise ClientError(msg, error_quotas_cyclades_disk)
            return False
        return True

199 200 201 202

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Okeanos VM provisioning")
    parser.add_argument('--cloud', type=str, dest="cloud", default="lambda")
203 204 205
    parser.add_argument('--project-name', type=str, dest="project_name",
                        default="lambda.grnet.gr")
    parser.add_argument('--name', type=str, dest='name', default="to mikro debian sto livadi")
206 207

    args = parser.parse_args()
208 209

    provisioner = Provisioner(cloud_name=args.cloud)
Avraam Tsantekidis's avatar
Avraam Tsantekidis committed
210 211
    print(provisioner.create_vm(vm_name=args.name, project_name=args.project_name,
                            image_name="debian"))