provisioner.py 22.7 KB
Newer Older
1
2
3
from __future__ import (absolute_import, division,
                        print_function, unicode_literals)
import logging
4
import re
efikalti's avatar
efikalti committed
5
import argparse
6

7
8
9
10
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

from kamaki.clients import astakos, cyclades
11
12
13
from kamaki.clients import ClientError
from kamaki.clients.utils import https
from kamaki.cli.config import Config as KamakiConfig
efikalti's avatar
efikalti committed
14
from kamaki import defaults
efikalti's avatar
efikalti committed
15
from fokia.cluster_error_constants import *
16
17
from Crypto.PublicKey import RSA
from base64 import b64encode
18

efikalti's avatar
efikalti committed
19
20
if not defaults.CACERTS_DEFAULT_PATH:
    https.patch_with_certs(CA_CERTS_PATH)
21

22
storage_templates = ['drdb', 'ext_vlmc']
23
24
25
26


class Provisioner:
    """
27
        provisions virtual machines on ~okeanos
28
29
    """

30
    def __init__(self, cloud_name):
31
32

        # Load .kamakirc configuration
33
        logger.info("Retrieving .kamakirc configuration")
34
        self.config = KamakiConfig()
35
36
37
38
39
        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()))
40
41
42
43

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

44
        logger.info("Initiating Astakos Client")
45
46
        self.astakos = astakos.AstakosClient(auth_url, auth_token)

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

53
54
55
56
57
58
59
60
61
        # 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

62
63
64
65
        self.master = None
        self.ips = None
        self.slaves = None
        self.vpn = None
efikalti's avatar
efikalti committed
66
        self.subnet = None
67
        self.private_key = None
68
        self.image_id = 'c6f5adce-21ad-4ce3-8591-acfe7eb73c02'
69

70
71
72
    """
    FIND RESOURCES
    """
73
    def find_flavor(self, **kwargs):
74
75
        """

76
        :param kwargs: should contains the keys that specify the specs
77
        :return: first flavor objects that matches the specs criteria
78
        """
79
80
81
82
83

        # Set all the default parameters
        kwargs.setdefault("vcpus", 1)
        kwargs.setdefault("ram", 1024)
        kwargs.setdefault("disk", 40)
84
        logger.info("Retrieving flavor")
85
        for flavor in self.cyclades.list_flavors(detail=True):
86
87
            if all([kwargs[key] == flavor[key] \
                    for key in set(flavor.keys()).intersection(kwargs.keys())]):
88
89
90
                return flavor
        return None

91
    def find_image(self, **kwargs):
92
93
94
95
96
        """
        :param image_name: Name of the image to filter by
        :param kwargs:
        :return: first image object that matches the name criteria
        """
97
        image_name = kwargs['image_name']
98
99

        logger.info("Retrieving image")
100
        for image in self.cyclades.list_images(detail=True):
101
            if image_name in image['name']:
102
103
104
105
                return image
        return None

    def find_project_id(self, **kwargs):
106
107
108
109
        """
        :param kwargs: name, state, owner and mode to filter project by
        :return: first project_id that matches the project name
        """
110
        filter = {
111
112
113
114
            'name': kwargs.get("project_name"),
            'state': kwargs.get("project_state"),
            'owner': kwargs.get("project_owner"),
            'mode': kwargs.get("project_mode"),
115
        }
116
        logger.info("Retrieving project")
117
118
        return self.astakos.get_projects(**filter)[0]

119
120
121
    """
    CREATE RESOURCES
    """
122

123
    def create_lambda_cluster(self, vm_name, **kwargs):
124
125
126
        """
        :param vm_name: hostname of the master
        :param kwargs: contains specifications of the vms.
127
        :return: dictionary object with the nodes of the cluster if it was successfully created
128
129
130
131
132
        """
        quotas = self.get_quotas()
        vcpus = kwargs['slaves'] * kwargs['vcpus_slave'] + kwargs['vcpus_master']
        ram = kwargs['slaves'] * kwargs['ram_slave'] + kwargs['ram_master']
        disk = kwargs['slaves'] * kwargs['disk_slave'] + kwargs['disk_master']
133
        project_id = self.find_project_id(**kwargs)['id']
134
135
136
137
138
139
140
141
        response = self.check_all_resources(quotas, cluster_size=kwargs['cluster_size'],
                                              vcpus=vcpus,
                                              ram=ram,
                                              disk=disk,
                                              ip_request=kwargs['ip_request'],
                                              network_request=kwargs['network_request'],
                                              project_name=kwargs['project_name'])
        if response:
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
            # Get ssh keys
            key = RSA.generate(2048)
            self.private_key = key.exportKey('PEM')
            pub_key = key.publickey().exportKey('OpenSSH') + ' root'
            public = dict(contents=b64encode(pub_key),
                          path='/root/.ssh/id_rsa.pub',
                          owner='root', group='root', mode=0600)
            authorized = dict(contents=b64encode(pub_key),
                          path='/root/.ssh/authorized_keys',
                          owner='root', group='root', mode=0600)
            private = dict(contents=b64encode(self.private_key),
                          path='/root/.ssh/id_rsa',
                          owner='root', group='root', mode=0600)

            master_personality = []
            master_personality.append(authorized)
            master_personality.append(public)
            master_personality.append(private)
            slave_personality = []
            slave_personality.append(authorized)

163
            # Create private network for cluster
164
165
            self.vpn = self.create_vpn('lambda-vpn', project_id=project_id)
            vpn_id = self.vpn['id']
166
167
168
169
            self.create_private_subnet(vpn_id)

            #reserve ip
            ip_request=kwargs['ip_request']
170
            self.ips = list()
efikalti's avatar
efikalti committed
171
            for i in range(ip_request):
172
                ip = self.reserve_ip(project_id=project_id)
173
                self.ips.append(ip)
174

175
176
            ip = None
            # Create master
177
178
            if len(self.ips) > 0:
                ip = self.ips[0]
179
180
181
182
183
184
185
            self.master = self.create_vm(vm_name=vm_name, ip=ip,
                                         net_id=vpn_id,
                                         vcpus=kwargs['vcpus_master'],
                                         ram=kwargs['ram_master'],
                                         disk=kwargs['disk_master'],
                                         personality=master_personality,
                                         **kwargs)
186
187

            # Create slaves
188
            self.slaves = list()
189
            for i in range(kwargs['slaves']):
190
                ip = None
efikalti's avatar
efikalti committed
191
192
                if len(self.ips) > i+1:
                    ip = self.ips[i+1]
193
                slave_name = 'lambda-node' + str(i+1)
194
195
196
197
198
199
200
201
                slave = self.create_vm(vm_name=slave_name,
                                       ip=ip,
                                       net_id=vpn_id,
                                       vcpus=kwargs['vcpus_slave'],
                                       ram=kwargs['ram_slave'],
                                       disk=kwargs['disk_slave'],
                                       personality=slave_personality,
                                       **kwargs)
202
                self.slaves.append(slave)
203

204
205
206
207
208
            # Wait for VMs to complete being built
            self.cyclades.wait_server(server_id=self.master['id'])
            for slave in self.slaves:
                self.cyclades.wait_server(slave['id'])

209
210
            # Create cluster dictionary object
            inventory = dict()
211
212
            inventory["master"] = self.master
            inventory["slaves"] = self.slaves
213
214
            return inventory

215
    def create_vm(self, vm_name=None, image_id=None, ip=None, personality=None, **kwargs):
216
        """
217
218
219
220
        :param vm_name: Name of the virtual machine to create
        :param image_id: image id if you want another image than the default
        :param kwargs: passed to the functions called for detail options
        :return:
221
        """
222
223
224
225
226
227
228
229
230
        flavor_id = self.find_flavor(**kwargs)['id']
        # Get image
        if image_id == None:
            image_id = self.image_id
        else:
            image_id = self.find_image(**kwargs)['id']
        project_id = self.find_project_id(**kwargs)['id']
        networks = list()
        if ip != None:
efikalti's avatar
efikalti committed
231
            ip_obj = dict()
232
233
234
235
236
237
238
239
240
241
242
243
244
245
            ip_obj['uuid'] = ip['floating_network_id']
            ip_obj['fixed_ip'] = ip['floating_ip_address']
            networks.append(ip_obj)
        networks.append({'uuid': kwargs['net_id']})
        if personality == None:
            personality = []
        try:
            okeanos_response = self.cyclades.create_server(name=vm_name, flavor_id=flavor_id,
                                                           image_id=image_id,
                                                           project_id=project_id,
                                                           networks=networks, personality=personality)
        except ClientError as ex:
            raise ex
        return okeanos_response
246

247
    def create_vpn(self, network_name, project_id):
248
249
250
        """
        Creates a virtual private network
        :param network_name: name of the network
251
        :return: the virtual network object
252
253
254
255
256
        """
        try:
            # Create vpn with custom type and the name given as argument
            vpn = self.network_client.create_network(
                        type=self.network_client.network_types[1],
257
258
                        name=network_name,
                        project_id=project_id)
259
            return vpn
260
261
262
263
        except ClientError as ex:
            raise ex
        return okeanos_response

264
    def reserve_ip(self,project_id):
265
266
267
268
        """
        Reserve ip
        :return: the ip object if successfull
        """
269
270
271
272
        list_float_ips = self.network_client.list_floatingips()
        for ip in list_float_ips:
            if ip['instance_id'] is None and ip['port_id'] is None:
                return ip
273
        try:
274
            ip = self.network_client.create_floatingip(project_id=project_id)
275
276
277
278
279
            return ip
        except ClientError as ex:
            raise ex
        return okeanos_response

280
    def create_private_subnet(self, net_id, cidr='192.168.0.0/24', gateway_ip='192.168.0.1'):
281
282
283
284
285
286
287
288
289
        """
        Creates a private subnets and connects it with this network
        :param net_id: id of the network
        :return: the id of the subnet if successfull
        """
        try:
            subnet = self.network_client.create_subnet(net_id, cidr,
                                                       gateway_ip=gateway_ip,
                                                       enable_dhcp=True)
efikalti's avatar
efikalti committed
290
            self.subnet = subnet
291
292
293
294
295
            return subnet['id']
        except ClientError as ex:
            raise ex
        return okeanos_response

296

297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
    def connect_vm(self, vm_id, net_id):
        """
        Connects the vm with this id to the network with the net_id
        :param vm_id: id of the vm
        :param net_id: id of the network
        :return: returns True if successfull
        """
        try:
            port = self.network_client.create_port(network_id=net_id,
                                                   device_id=vm_id)
            return True
        except ClientError as ex:
            raise ex
        return okeanos_response

312
    def attach_authorized_ip(self, ip, vm_id):
313
        """
314
        Attach the authorized ip with this id to the vm
315
316
317
318
319
320
321
322
323
324
325
326
327
        :param fnet_id: id of the floating network of the ip
        :param vm_id: id of the vm
        :return: returns True if successfull
        """
        try:
            port = self.network_client.create_port(network_id=ip['floating_network_id'],
                                                   device_id=vm_id,
                                                   fixed_ips=[dict(ip_address=ip['floating_ip_address']), ])
            return True
        except ClientError as ex:
            raise ex
        return okeanos_response

328
329
330
331
332
333
334
335
336
337
    """
    DELETE RESOURCES
    """

    def delete_lambda_cluster(self, details):
        """
        Delete a lambda cluster
        :param details: details of the cluster we want to delete
        :return: True if successfull
        """
efikalti's avatar
efikalti committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
        self.cyclades.get_server_details
        # Delete every node
        nodes = details['nodes']
        for node in nodes:
            if(not self.delete_vm(node)):
                msg = 'Error deleting node with id ', node
                raise ClientError(msg, error_fatal)

        # Wait to complete deleting VMs
        for node in nodes:
            self.cyclades.wait_server(server_id=node, current_status='ACTIVE')

        # Delete vpn
        vpn = details['vpn']
        if (not self.delete_vpn(vpn)):
            msg = 'Error deleting node with id ', node
            raise ClientError(msg, error_fatal)


357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377

    def delete_vm(self, vm_id):
        """
        Delete a vm
        :param vm_id: id of the vm we want to delete
        :return: True if successfull
        """
        try:
            self.cyclades.delete_server(vm_id)
            return True
        except ClientError as ex:
            raise ex
        return False

    def delete_vpn(self, net_id):
        """
        Delete a virtual private network
        :param net_id: id of the network we want to delete
        :return: True if successfull
        """
        try:
efikalti's avatar
efikalti committed
378
            self.network_client.delete_network(net_id)
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
            return True
        except ClientError as ex:
            raise ex
        return False

    """
    GET RESOURCES
    """

    def get_cluster_details(self):
        """
        :returns: dictionary of basic details for the cluster
        """
        details = dict()

        nodes = dict()
        master = dict()
        master['id'] = self.master['id']
        master['name'] = self.master['name']
        master['adminPass'] = self.master['adminPass']
        nodes['master'] = master

        slaves = list()
        for slave in self.slaves:
            slave_obj = dict()
            slave_obj['id'] = slave['id']
            slave_obj['name'] = slave['name']
            name = slave_obj['name']
            slaves.append(slave_obj)
        nodes['slaves'] = slaves

        details['nodes'] = nodes
        vpn = dict()
        vpn['id'] = self.vpn['id']
        vpn['type'] = self.vpn['type']
        details['vpn'] = vpn

        details['ips'] = self.ips
        ips_list = list()
        for ip in self.ips:
            ip_obj = dict()
            ip_obj['floating_network_id'] = ip['floating_network_id']
            ip_obj['floating_ip_address'] = ip['floating_ip_address']
            ip_obj['id'] = ip['id']
            ips_list.append(ip_obj)
        details['ips'] = ips_list

        subnet = dict()
        subnet['id'] = self.subnet['id']
        subnet['cidr'] = self.subnet['cidr']
        subnet['gateway_ip'] = self.subnet['gateway_ip']
        details['subnet'] = subnet
        return details

    def get_private_key(self):
        """
        :returns: Private key of master
        """
        return self.private_key

439
440
441
442
443
444
445
    def get_quotas(self, **kwargs):
        """
        Get the user quotas for the defined project.
        :return: user quotas object
        """
        return self.astakos.get_quotas()

446
447
448
449
450
    def get_server_info(self, server_id):
        """
        """
        return self.cyclades.get_server_details(server_id=server_id)

451
    def get_server_authorized_ip(self, server_id):
452
453
        """
        :param server_id: id of the server
454
        :returns: the authorized ip of the server if it has one,else None
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
        """
        addresses =  self.get_server_info(server_id=server_id)['addresses']
        for key in list(addresses.keys()):
            ip = addresses[key][0]['addr']
            if '192.168.0' not in ip and not re.search('[a-zA-Z]', ip):
                return ip
        return None

    def get_server_private_ip(self, server_id):
        """
        :param server_id: id of the server
        :returns: the private ip of the server if it has one,else None
        """
        addresses =  self.get_server_info(server_id=server_id)['addresses']
        for key in list(addresses.keys()):
            ip = addresses[key][0]['addr']
            if '192.168.0' in ip:
                return ip
        return None

475
476
477
    """
    CHECK RESOURCES
    """
478
479
480
481
482
483
484
    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']
efikalti's avatar
efikalti committed
485
486
        flavor = self.find_flavor(**kwargs)
        print(flavor)
487
488

        # Check for VMs
efikalti's avatar
efikalti committed
489
490
491
        pending_vm = quotas[project_id]['cyclades.vm']['project_pending']
        limit_vm = quotas[project_id]['cyclades.vm']['project_limit']
        usage_vm = quotas[project_id]['cyclades.vm']['project_usage']
492
493
494
495
496
497
        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
efikalti's avatar
efikalti committed
498
499
500
        pending_cpu = quotas[project_id]['cyclades.cpu']['project_pending']
        limit_cpu = quotas[project_id]['cyclades.cpu']['project_limit']
        usage_cpu = quotas[project_id]['cyclades.cpu']['project_usage']
501
502
503
504
505
506
        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
efikalti's avatar
efikalti committed
507
508
509
        pending_ram = quotas[project_id]['cyclades.ram']['project_pending']
        limit_ram = quotas[project_id]['cyclades.ram']['project_limit']
        usage_ram = quotas[project_id]['cyclades.ram']['project_usage']
510
511
512
513
514
515
        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
efikalti's avatar
efikalti committed
516
517
518
        pending_cd = quotas[project_id]['cyclades.ram']['project_pending']
        limit_cd = quotas[project_id]['cyclades.disk']['project_limit']
        usage_cd = quotas[project_id]['cyclades.disk']['project_usage']
519
520
521
522
523
        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
524
        # Check for authorized IPs
525
        list_float_ips = self.network_client.list_floatingips()
efikalti's avatar
efikalti committed
526
527
528
        pending_ips = quotas[project_id]['cyclades.floating_ip']['project_pending']
        limit_ips = quotas[project_id]['cyclades.floating_ip']['project_limit']
        usage_ips = quotas[project_id]['cyclades.floating_ip']['project_usage']
529
530
531
532
533
        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']:
534
            msg = 'authorized IPs out of limit'
efikalti's avatar
efikalti committed
535
            raise ClientError(msg, error_get_ip)
536
537
538
539
540
541
542
543
            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'
efikalti's avatar
efikalti committed
544
            raise ClientError(msg, error_get_network_quota)
545
546
547
            return False
        return True

548
549
550

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Okeanos VM provisioning")
551
    parser.add_argument('--cloud', type=str, dest="cloud", default="~okeanos")
552
553
554
    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")
555

556
557
558
559
560
561
562
563

    parser.add_argument('--slaves', type=int, dest='slaves', default=1)
    parser.add_argument('--vcpus_master', type=int, dest='vcpus_master', default=4)
    parser.add_argument('--vcpus_slave', type=int, dest='vcpus_slave', default=4)
    parser.add_argument('--ram_master', type=int, dest='ram_master', default=4096)  # in MB
    parser.add_argument('--ram_slave', type=int, dest='ram_slave', default=4096)  # in MB
    parser.add_argument('--disk_master', type=int, dest='disk_master', default=40)  # in GB
    parser.add_argument('--disk_slave', type=int, dest='disk_slave', default=40)  # in GB
564
    parser.add_argument('--ip_request', type=int, dest='ip_request', default=1)
565
566
567
568
    parser.add_argument('--network_request', type=int, dest='network_request', default=1)
    parser.add_argument('--image_name', type=str, dest='image_name', default="debian")
    parser.add_argument('--cluster_size', type=int, dest='cluster_size', default=2)

569
    args = parser.parse_args()
570
571

    provisioner = Provisioner(cloud_name=args.cloud)
572
573
    """

574

575
    response = provisioner.create_lambda_cluster(vm_name="lambda-master" , slaves=args.slaves,
576
577
578
579
580
581
582
583
584
585
                                          cluster_size=args.cluster_size,
                                          vcpus_master=args.vcpus_master,
                                          vcpus_slave=args.vcpus_slave,
                                          ram_master=args.ram_master,
                                          ram_slave=args.ram_slave,
                                          disk_master=args.disk_master,
                                          disk_slave=args.disk_slave,
                                          ip_request=args.ip_request,
                                          network_request=args.network_request,
                                          project_name=args.project_name)
586
    print(provisioner.get_cluster_details())
efikalti's avatar
efikalti committed
587
588
589
590

    """
    #details = {'nodes':[668403,668404],'vpn':144076}
    #provisioner.delete_lambda_cluster(details)