components.py 34.1 KB
Newer Older
1
# Copyright (C) 2010, 2011, 2012, 2013, 2014 GRNET S.A. All rights reserved.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A. OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

import datetime
from snfdeploy.utils import debug

37

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class SynnefoComponent(object):

    REQUIRED_PACKAGES = []

    def debug(self, msg, info=""):
        debug(self.__class__.__name__, msg, info)

    def __init__(self, node_info, env, *args, **kwargs):
        """ Take a node_info and env as argument and initialize local vars """
        self.node_info = node_info
        self.env = env

    def check(self):
        """ Returns a list of bash commands that check prerequisites """
        return []

    def install(self):
        """ Returns a list of debian packages to install """
        return self.REQUIRED_PACKAGES

    def prepare(self):
        """ Returs a list of bash commands that prepares the component """
        return []

    def configure(self):
        """ Must return a list of tuples (tmpl_path, replace_dict, mode) """
        return []

    def initialize(self):
        """ Returs a list of bash commands that initialize the component """
        return []

    def test(self):
        """ Returs a list of bash commands that test existing installation """
        return []

    def restart(self):
        return []

    #TODO: add cleanup method for each component
    def clean(self):
        return []


class HW(SynnefoComponent):
    def test(self):
        return [
            "ping -c 1 %s" % self.node_info.ip,
            "ping -c 1 www.google.com",
            "apt-get update",
            ]

90

91
92
93
94
95
96
97
98
99
100
101
102
class SSH(SynnefoComponent):
    def prepare(self):
        return [
            "mkdir -p /root/.ssh",
            "for f in $(ls /root/.ssh/*); do cp $f $f.bak ; done",
            "echo StrictHostKeyChecking no >> /etc/ssh/ssh_config",
            ]

    def configure(self):
        files = [
            "authorized_keys", "id_dsa", "id_dsa.pub", "id_rsa", "id_rsa.pub"
            ]
103
        ssh = [("/root/.ssh/%s" % f, {}, {"mode": 0600}) for f in files]
104
105
106
107
108
        return ssh

    def initialize(self):
        f = "/root/.ssh/authorized_keys"
        return [
109
            "test -e {0}.bak && cat {0}.bak >> {0} || true".format(f)
110
111
112
113
114
115
116
117
118
119
120
121
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
            ]

    def test(self):
        return ["ssh %s date" % self.node_info.ip]


class DNS(SynnefoComponent):
    def prepare(self):
        return [
            "chattr -i /etc/resolv.conf",
            "sed -i 's/^127.*$/127.0.0.1 localhost/g' /etc/hosts",
            ]

    def configure(self):
        r1 = {
            "date": str(datetime.datetime.today()),
            "domain": self.env.env.domain,
            "ns_node_ip": self.env.env.ns.ip,
            }
        resolv = [
            ("/etc/resolv.conf", r1, {})
            ]
        return resolv

    def initialize(self):
        return ["chattr +i /etc/resolv.conf"]


class DDNS(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "dnsutils",
        ]

    def prepare(self):
        return [
            "mkdir -p /root/ddns/"
            ]

    def configure(self):
        return [
            ("/root/ddns/" + k, {}, {}) for k in self.env.env.ddns_keys
            ]


class NS(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "bind9",
        ]

    def nsupdate(self, cmd):
        ret = """
nsupdate -k {0} > /dev/null <<EOF || true
server {1}
{2}
send
EOF
""".format(self.env.env.ddns_private_key, self.node_info.ip, cmd)
        return ret

    def prepare(self):
        return [
            "mkdir -p /etc/bind/zones",
            "chmod g+w /etc/bind/zones",
            "mkdir -p /etc/bind/rev",
            "chmod g+w /etc/bind/rev",
            ]

    def configure(self):
        d = self.env.env.domain
        ip = self.node_info.ip
        return [
            ("/etc/bind/named.conf.local", {"domain": d}, {}),
            ("/etc/bind/zones/example.com",
             {"domain": d, "ns_node_ip": ip},
             {"remote": "/etc/bind/zones/%s" % d}),
            ("/etc/bind/zones/vm.example.com",
             {"domain": d, "ns_node_ip": ip},
             {"remote": "/etc/bind/zones/vm.%s" % d}),
            ("/etc/bind/rev/synnefo.in-addr.arpa.zone", {"domain": d}, {}),
            ("/etc/bind/rev/synnefo.ip6.arpa.zone", {"domain": d}, {}),
            ("/etc/bind/named.conf.options",
             {"node_ips": ";".join(self.env.env.ips)}, {}),
            ("/root/ddns/ddns.key", {}, {"remote": "/etc/bind/ddns.key"}),
            ]

    def update_cnamerecord(self, node_info):
        return self.nsupdate("update add %s" % node_info.cnamerecord)

    def update_arecord(self, node_info):
        return self.nsupdate("update add %s" % node_info.arecord)

    def update_ptrrecord(self, node_info):
        return self.nsupdate("update add %s" % node_info.ptrrecord)

    def update_ns_for_node(self, node_info):
        return [
            self.update_arecord(node_info),
            self.update_cnamerecord(node_info),
            self.update_ptrrecord(node_info)
            ]

    def initialize(self):
        a = [self.update_arecord(n)
             for n in self.env.env.nodes_info.values()]
        ptr = [self.update_ptrrecord(n)
               for n in self.env.env.nodes_info.values()]
        cnames = [self.update_cnamerecord(n)
                  for n in self.env.env.roles_info.values()]

        return a + ptr + cnames

    def restart(self):
        return ["/etc/init.d/bind9 restart"]

    def test(self):
        n = ["host %s localhost" % i.fqdn
             for i in self.env.env.nodes_info.values()]
        a = ["host %s localhost" % i.fqdn
             for i in self.env.env.roles_info.values()]
        return n + a


class APT(SynnefoComponent):
    """ Setup apt repos and check fqdns """
    REQUIRED_PACKAGES = ["curl"]

    def prepare(self):
        return [
            "echo 'APT::Install-Suggests \"false\";' >> /etc/apt/apt.conf",
239
240
            "curl -k https://dev.grnet.gr/files/apt-grnetdev.pub | \
                apt-key add -",
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
            ]

    def configure(self):
        return [
            ("/etc/apt/sources.list.d/synnefo.wheezy.list", {}, {})
            ]

    def initialize(self):
        return [
            "apt-get update",
            ]


class MQ(SynnefoComponent):
    REQUIRED_PACKAGES = ["rabbitmq-server"]

    def check(self):
        return ["ping -c 1 mq.%s" % self.env.env.domain]

    def initialize(self):
        u = self.env.env.synnefo_user
        p = self.env.env.synnefo_rabbitmq_passwd
        return [
            "rabbitmqctl add_user %s %s" % (u, p),
            "rabbitmqctl set_permissions %s \".*\" \".*\" \".*\"" % u,
            "rabbitmqctl delete_user guest",
            "rabbitmqctl set_user_tags %s administrator" % u,
            ]


class DB(SynnefoComponent):
    REQUIRED_PACKAGES = ["postgresql"]

    def check(self):
        return ["ping -c 1 db.%s" % self.env.env.domain]

    def get_user_info_from_db(self):
        cmd = """
cat > /tmp/psqlcmd <<EOF
select id, auth_token, uuid, email from auth_user, im_astakosuser \
where auth_user.id = im_astakosuser.user_ptr_id and auth_user.email = '{0}';
EOF

su - postgres -c  "psql -w -d snf_apps -f /tmp/psqlcmd"
""".format(self.env.env.user_email)

        return [cmd]

    def allow_access_in_db(self, node_info, user="all", method="md5"):
        f = "/etc/postgresql/*/main/pg_hba.conf"
        cmd1 = "echo host all %s %s/32 %s >> %s" % \
292
            (user, node_info.ip, method, f)
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
        cmd2 = "sed -i 's/\(host.*127.0.0.1.*\)md5/\\1trust/' %s" % f
        return [cmd1, cmd2] + self.restart()

    def configure(self):
        u = self.env.env.synnefo_user
        p = self.env.env.synnefo_db_passwd
        replace = {"synnefo_user": u, "synnefo_db_passwd": p}
        return [
            ("/tmp/db-init.psql", replace, {}),
            ]

    def make_db_fast(self):
        f = "/etc/postgresql/*/main/postgresql.conf"
        opts = "fsync=off\nsynchronous_commit=off\nfull_page_writes=off\n"
        return ["""echo -e "%s" >> %s""" % (opts, f)]

    def prepare(self):
        f = "/etc/postgresql/*/main/postgresql.conf"
        return [
            """echo "listen_addresses = '*'" >> %s""" % f,
            ]

    def initialize(self):
        script = "/tmp/db-init.psql"
        cmd = "su - postgres -c \"psql -w -f %s\" " % script
        return [cmd]

    def restart(self):
        return ["/etc/init.d/postgresql restart"]

    def destroy_db(self):
        return [
            """su - postgres -c ' psql -w -c "drop database snf_apps" '""",
            """su - postgres -c ' psql -w -c "drop database snf_pithos" '"""
            ]


class Ganeti(SynnefoComponent):

    REQUIRED_PACKAGES = [
        "qemu-kvm",
        "python-bitarray",
        "ganeti-htools",
        "ganeti-haskell",
        "snf-ganeti",
        "ganeti2",
        "bridge-utils",
        "lvm2",
        "drbd8-utils",
        ]

    def check(self):
        commands = [
            "getent hosts %s | grep -v ^127" % self.node_info.hostname,
            "hostname -f | grep %s" % self.node_info.fqdn,
            ]
        return commands

    def configure(self):
        return [
            ("/etc/ganeti/file-storage-paths", {}, {}),
            ]

    def prepare_lvm(self):
        return [
            "pvcreate %s" % self.env.env.extra_disk,
            "vgcreate %s %s" % (self.env.env.extra_disk, self.env.env.vg)
            ]

    def prepare_net_infra(self):
        br = self.env.env.common_bridge
        return [
            "brctl addbr {0}; ip link set {0} up".format(br)
            ]

    def prepare(self):
        return [
            "mkdir -p /srv/ganeti/file-storage/",
            "sed -i 's/^127.*$/127.0.0.1 localhost/g' /etc/hosts"
            ] + self.prepare_net_infra()

    def restart(self):
375
        return ["/etc/init.d/ganeti restart"]
376
377
378
379


class Master(SynnefoComponent):
    def add_rapi_user(self):
380
381
382
        user = self.env.env.synnefo_user
        passwd = self.env.env.synnefo_rapi_passwd
        x = "%s:Ganeti Remote API:%s" % (user, passwd)
383

384
385
386
387
388
        cmd = """
cat >> /var/lib/ganeti/rapi/users <<EOF
%s {HA1}$(echo -n %s | openssl md5 | sed 's/^.* //') write
EOF
""" % (self.env.env.synnefo_user, x)
389

390
        return [cmd] + self.restart()
391
392
393

    def add_node(self, node_info):
        commands = [
394
            "gnt-node add --no-ssh-key-check --master-capable=yes " +
395
            "--vm-capable=yes " + node_info.fqdn,
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
            ]
        return commands

    def try_use_vg(self):
        vg = self.env.env.vg
        return [
            "gnt-cluster modify --vg-name=%s || true" % vg,
            "gnt-cluster modify --disk-parameters=drbd:metavg=%s" % vg,
            "gnt-group modify --disk-parameters=drbd:metavg=%s default" % vg,
            ]

    def initialize(self):
        cmd = """
        gnt-cluster init --enabled-hypervisors=kvm \
            --no-lvm-storage --no-drbd-storage \
            --nic-parameters link={0},mode=bridged \
            --master-netdev {1} \
            --specs-nic-count min=0,max=8 \
            --default-iallocator hail \
            --hypervisor-parameters kvm:kernel_path=,vnc_bind_address=0.0.0.0 \
            --no-ssh-init --no-etc-hosts \
            --enabled-disk-templates file,plain,ext,drbd \
            {2}
        """.format(self.env.env.common_bridge,
                   self.env.env.cluster_netdev, self.env.env.cluster.fqdn)

        return [cmd] + self.try_use_vg() + self.add_rapi_user()

    def restart(self):
        return ["/etc/init.d/ganeti restart"]


class Image(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "snf-image",
        ]

    def check(self):
        return ["mkdir -p %s" % self.env.env.image_dir]

    def configure(self):
        tmpl = "/etc/default/snf-image"
        replace = {
            "synnefo_user": self.env.env.synnefo_user,
            "synnefo_db_passwd": self.env.env.synnefo_db_passwd,
            "pithos_dir": self.env.env.pithos_dir,
            "db_node": self.env.env.db.ip,
            "image_dir": self.env.env.image_dir,
            }
        return [(tmpl, replace, {})]

    def initialize(self):
        return ["snf-image-update-helper -y"]


class GTools(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "snf-cyclades-gtools",
        ]

    def check(self):
        return ["ping -c1 %s" % self.env.env.mq.ip]

459
460
461
462
463
    def prepare(self):
        return [
            "sed -i 's/false/true/' /etc/default/snf-ganeti-eventd",
            ]

464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
    def configure(self):
        tmpl = "/etc/synnefo/gtools.conf"
        replace = {
            "synnefo_user": self.env.env.synnefo_user,
            "synnefo_rabbitmq_passwd": self.env.env.synnefo_rabbitmq_passwd,
            "mq_node": self.env.env.mq.ip,
            }
        return [(tmpl, replace, {})]

    def restart(self):
        return ["/etc/init.d/snf-ganeti-eventd restart"]


class Network(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "python-nfqueue",
        "snf-network",
        "nfdhcpd",
        ]

    def configure(self):
        r1 = {
            "ns_node_ip": self.env.env.ns.ip
            }
        r2 = {
            "common_bridge": self.env.env.common_bridge,
            "public_iface": self.env.env.public_iface,
            "subnet": self.env.env.synnefo_public_network_subnet,
            "gateway": self.env.env.synnefo_public_network_gateway,
            "router_ip": self.env.env.router.ip,
            "node_ip": self.node_info.ip,
            }
        r3 = {
            "domain": self.env.env.domain,
            "server": self.env.env.ns.ip,
            "keyfile": self.env.env.ddns_private_key,
            }

        return [
            ("/etc/nfdhcpd/nfdhcpd.conf", r1, {}),
            ("/etc/rc.local", r2, {"mode": 0755}),
            ("/etc/default/snf-network", r3, {}),
            ]

    def initialize(self):
        return ["/etc/init.d/rc.local start"]

    def restart(self):
        return ["/etc/init.d/nfdhcpd restart"]


class Apache(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "apache2",
        ]

    def prepare(self):
        return [
            "a2enmod ssl", "a2enmod rewrite", "a2dissite default",
            "a2enmod headers",
            "a2enmod proxy_http", "a2dismod autoindex",
            ]

    def configure(self):
        r1 = {"HOST": self.node_info.fqdn}
        return [
            ("/etc/apache2/sites-available/synnefo", r1, {}),
            ("/etc/apache2/sites-available/synnefo-ssl", r1, {}),
            ]

    def initialize(self):
        return [
            "a2ensite synnefo", "a2ensite synnefo-ssl",
            ]

    def restart(self):
        return [
541
542
            "/etc/init.d/apache2 restart",
            ]
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562


class Gunicorn(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "gunicorn",
        ]

    def prepare(self):
        return [
            "chown root.www-data /var/log/gunicorn",
            ]

    def configure(self):
        r1 = {"HOST": self.node_info.fqdn}
        return [
            ("/etc/gunicorn.d/synnefo", r1, {}),
            ]

    def restart(self):
        return [
563
564
            "/etc/init.d/gunicorn restart",
            ]
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592


class Common(SynnefoComponent):
    REQUIRED_PACKAGES = [
        # snf-common
        "python-objpool",
        "snf-common",
        "python-astakosclient",
        "snf-django-lib",
        "snf-branding",
        ]

    def configure(self):
        r1 = {
            "EMAIL_SUBJECT_PREFIX": self.node_info.hostname,
            "domain": self.env.env.domain,
            "HOST": self.node_info.fqdn,
            "MAIL_DIR": self.env.env.mail_dir,
            }
        return [
            ("/etc/synnefo/common.conf", r1, {}),
            ]

    def initialize(self):
        return ["mkdir -p {0}; chmod 777 {0}".format(self.env.env.mail_dir)]

    def restart(self):
        return [
593
594
            "/etc/init.d/gunicorn restart",
            ]
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620


class WEB(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "snf-webproject",
        "python-psycopg2",
        "python-gevent",
        "python-django",
        ]

    def check(self):
        return ["ping -c1 %s" % self.env.env.db.fqdn]

    def configure(self):
        r1 = {
            "synnefo_user": self.env.env.synnefo_user,
            "synnefo_db_passwd": self.env.env.synnefo_db_passwd,
            "db_node": self.env.env.db.fqdn,
            "domain": self.env.env.domain,
            }
        return [
            ("/etc/synnefo/webproject.conf", r1, {}),
            ]

    def restart(self):
        return [
621
622
            "/etc/init.d/gunicorn restart",
            ]
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644


class Astakos(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "python-django-south",
        "snf-astakos-app",
        "kamaki",
        ]

    def export_service(self):
        f = self.env.jsonfile
        return [
            "snf-manage service-export-astakos > %s" % f
            ]

    def import_service(self):
        f = self.env.jsonfile
        return [
            "snf-manage service-import --json=%s" % f
            ]

    def set_default_quota(self):
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
        cmd = "snf-manage resource-modify"
        return [
            "%s --base-default 40G pithos.diskspace" % cmd,
            "%s --base-default 2 astakos.pending_app" % cmd,
            "%s --base-default 4 cyclades.vm" % cmd,
            "%s --base-default 40G cyclades.disk" % cmd,
            "%s --base-default 16G cyclades.total_ram" % cmd,
            "%s --base-default 8G cyclades.ram" % cmd,
            "%s --base-default 32 cyclades.total_cpu" % cmd,
            "%s --base-default 16 cyclades.cpu" % cmd,
            "%s --base-default 4 cyclades.network.private" % cmd,
            "%s --base-default 4 cyclades.floating_ip" % cmd,
            "%s --project-default 0 pithos.diskspace" % cmd,
            "%s --project-default 0 astakos.pending_app" % cmd,
            "%s --project-default 0 cyclades.vm" % cmd,
            "%s --project-default 0 cyclades.disk" % cmd,
            "%s --project-default inf cyclades.total_ram" % cmd,
            "%s --project-default 0 cyclades.ram" % cmd,
            "%s --project-default inf cyclades.total_cpu" % cmd,
            "%s --project-default 0 cyclades.cpu" % cmd,
            "%s --project-default 0 cyclades.network.private" % cmd,
            "%s --project-default 0 cyclades.floating_ip" % cmd,
667
668
669
            ]

    def modify_all_quota(self):
670
671
672
673
674
675
676
677
678
679
680
681
        cmd = "snf-manage project-modify --all-base-projects --limit"
        return [
            "%s pithos.diskspace 40G 40G" % cmd,
            "%s astakos.pending_app 2 2" % cmd,
            "%s cyclades.vm 4 4" % cmd,
            "%s cyclades.disk 40G 40G" % cmd,
            "%s cyclades.total_ram 16G 16G" % cmd,
            "%s cyclades.ram 8G 8G" % cmd,
            "%s cyclades.total_cpu 32 32" % cmd,
            "%s cyclades.cpu 16 16" % cmd,
            "%s cyclades.network.private 4 4" % cmd,
            "%s cyclades.floating_ip 4 4" % cmd,
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
            ]

    def get_services(self):
        return [
            "snf-manage component-list -o id,name,token"
            ]

    def configure(self):
        r1 = {
            "ACCOUNTS": self.env.env.accounts.fqdn,
            "domain": self.env.env.domain,
            "CYCLADES": self.env.env.cyclades.fqdn,
            "PITHOS": self.env.env.pithos.fqdn,
            }
        return [
            ("/etc/synnefo/astakos.conf", r1, {})
            ]

    def initialize(self):
        secret = self.env.env.oa2_secret
        view = "https://%s/pithos/ui/view" % self.env.env.pithos.fqdn
        oa2 = "snf-manage oauth2-client-add pithos-view \
                  --secret=%s --is-trusted --url %s" % (secret, view)

        return [
            "snf-manage syncdb --noinput",
            "snf-manage migrate im --delete-ghost-migrations",
            "snf-manage migrate quotaholder_app",
            "snf-manage migrate oa2",
            "snf-manage loaddata groups",
            oa2
            ] + self.astakos_register_components()

    def astakos_register_components(self):
        # base urls
        cbu = "https://%s/cyclades" % self.env.env.cyclades.fqdn
        pbu = "https://%s/pithos" % self.env.env.pithos.fqdn
        abu = "https://%s/astakos" % self.env.env.accounts.fqdn
        cmsurl = "https://%s/home" % self.env.env.cms.fqdn

        cmd = "snf-manage component-add"
        h = "%s home --base-url %s --ui-url %s" % (cmd, cmsurl, cmsurl)
        c = "%s cyclades --base-url %s --ui-url %s/ui" % (cmd, cbu, cbu)
        p = "%s pithos --base-url %s --ui-url %s/ui" % (cmd, pbu, pbu)
        a = "%s astakos --base-url %s --ui-url %s/ui" % (cmd, abu, abu)

        return [h, c, p, a]

    def add_user(self):
        info = (
            self.env.env.user_passwd,
            self.env.env.user_email,
            self.env.env.user_name,
            self.env.env.user_lastname,
            )
        cmd = "snf-manage user-add --password %s %s %s %s" % info
        return [cmd]

    def activate_user(self):
        user_id = self.env.user_id
        return [
            "snf-manage user-modify --verify %s" % user_id,
            "snf-manage user-modify --accept %s" % user_id,
            ]


class CMS(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "snf-cloudcms"
        ]

    def configure(self):
        r1 = {
            "ACCOUNTS": self.env.env.accounts.fqdn
            }
        r2 = {
            "DOMAIN": self.env.env.domain
            }
        return [
            ("/etc/synnefo/cms.conf", r1, {}),
            ("/tmp/sites.json", r2, {}),
            ("/tmp/page.json", {}, {}),
            ]

    def initialize(self):
        return [
            "snf-manage syncdb",
            "snf-manage migrate --delete-ghost-migrations",
            "snf-manage loaddata /tmp/sites.json",
            "snf-manage loaddata /tmp/page.json",
            "snf-manage createsuperuser --username=admin \
                  --email=admin@%s --noinput" % self.env.env.domain,
            ]

    def restart(self):
        return ["/etc/init.d/gunicorn restart"]


class Mount(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "nfs-common"
        ]

    def prepare(self):
        ret = []
787
788
        dirs = [self.env.env.pithos_dir, self.env.env.image_dir, "/srv/archip"]
        for d in dirs:
789
790
791
792
793
794
795
796
797
798
799
800
            ret.append("mkdir -p %s" % d)
            cmd = """
cat >> /etc/fstab <<EOF
{0}:{1} {1}  nfs defaults,rw,noatime,rsize=131072,wsize=131072 0 0
EOF
""".format(self.env.env.pithos.ip, d)
            ret.append(cmd)

        return ret

    def initialize(self):
        ret = []
801
802
        dirs = [self.env.env.pithos_dir, self.env.env.image_dir, "/srv/archip"]
        for d in dirs:
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
            ret.append("mount %s" % d)
        return ret


class NFS(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "nfs-kernel-server"
        ]

    def prepare_image(self):
        url = self.env.env.debian_base_url
        d = self.env.env.image_dir
        image = "debian_base.diskdump"
        return ["wget %s -O %s/%s" % (url, d, image)]

    def prepare(self):
        p = self.env.env.pithos_dir
        return [
            "mkdir -p %s" % self.env.env.image_dir,
            "mkdir -p %s/data" % p,
823
824
            "mkdir -p /srv/archip/blocks",
            "mkdir -p /srv/archip/maps",
825
826
827
828
829
830
831
832
833
            "chown www-data.www-data %s/data" % p,
            "chmod g+ws %s/data" % p,
            ] + self.prepare_image()

    def update_exports(self, node_info):
        cmd = """
cat >> /etc/exports <<EOF
{0} {2}(rw,async,no_subtree_check,no_root_squash)
{1} {2}(rw,async,no_subtree_check,no_root_squash)
834
/srv/archip {2}(rw,async,no_subtree_check,no_root_squash)
835
836
837
838
839
840
841
842
843
844
845
EOF
""".format(self.env.env.pithos_dir, self.env.env.image_dir, node_info.ip)
        return [cmd] + self.restart()

    def restart(self):
        return ["exportfs -a"]


class Pithos(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "kamaki",
846
        "python-svipc",
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
        "snf-pithos-app",
        "snf-pithos-webclient",
        ]

    def export_service(self):
        f = self.env.jsonfile
        return [
            "snf-manage service-export-pithos > %s" % f
            ]

    def configure(self):
        r1 = {
            "ACCOUNTS": self.env.env.accounts.fqdn,
            "PITHOS": self.env.env.pithos.fqdn,
            "db_node": self.env.env.db.ip,
            "synnefo_user": self.env.env.synnefo_user,
            "synnefo_db_passwd": self.env.env.synnefo_db_passwd,
            "pithos_dir": self.env.env.pithos_dir,
            "PITHOS_SERVICE_TOKEN": self.env.service_token,
            "oa2_secret": self.env.env.oa2_secret,
            }
        r2 = {
            "ACCOUNTS": self.env.env.accounts.fqdn,
            "PITHOS_UI_CLOUDBAR_ACTIVE_SERVICE": self.env.service_id,
            }

        return [
            ("/etc/synnefo/pithos.conf", r1, {}),
            ("/etc/synnefo/webclient.conf", r2, {}),
            ]

878
879
880
    def initialize(self):
        return ["pithos-migrate stamp head"]

881

882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
class PithosBackend(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "snf-pithos-backend",
        ]

    def configure(self):
        r1 = {
            "db_node": self.env.env.db.ip,
            "synnefo_user": self.env.env.synnefo_user,
            "synnefo_db_passwd": self.env.env.synnefo_db_passwd,
            "pithos_dir": self.env.env.pithos_dir,
            }

        return [
            ("/etc/synnefo/backend.conf", r1, {}),
            ]


900
901
902
903
904
905
906
907
908
class Cyclades(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "memcached",
        "python-memcache",
        "kamaki",
        "snf-cyclades-app",
        "python-django-south",
        ]

909
910
911
912
913
    def add_network(self):
        subnet = self.env.env.synnefo_public_network_subnet
        gw = self.env.env.synnefo_public_network_gateway
        ntype = self.env.env.synnefo_public_network_type
        link = self.env.env.common_bridge
914
915
916
917
918
919
920
921
922

        cmd = """
snf-manage network-create --subnet={0} --gateway={1} --public \
  --dhcp=True --flavor={2} --mode=bridged --link={3} --name=Internet \
  --floating-ip-pool=True
""".format(subnet, gw, ntype, link)

        return [cmd]

923
924
925
926
927
    def add_network6(self):
        subnet = "babe::/64"
        gw = "babe::1"
        ntype = self.env.env.synnefo_public_network_type
        link = self.env.env.common_bridge
928

929
930
931
932
933
        cmd = """
snf-manage network-create --subnet6={0} \
      --gateway6={1} --public --dhcp=True --flavor={2} --mode=bridged \
       --link={3} --name=IPv6PublicNetwork
""".format(subnet, gw, ntype, link)
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952

        return [cmd]

    def export_service(self):
        f = self.env.jsonfile
        return [
            "snf-manage service-export-cyclades > %s" % f
            ]

    def list_backends(self):
        return [
            "snf-manage backend-list"
            ]

    def add_backend(self):
        cluster = self.env.env.cluster
        user = self.env.env.synnefo_user
        passwd = self.env.env.synnefo_rapi_passwd
        return [
953
            "snf-manage backend-add --clustername=%s --user=%s --pass=%s" %
954
            (cluster.fqdn, user, passwd)
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
            ]

    def undrain_backend(self):
        backend_id = self.env.backend_id
        return [
            "snf-manage backend-modify --drained=False %s" % str(backend_id)
            ]

    def prepare(self):
        return ["sed -i 's/false/true/' /etc/default/snf-dispatcher"]

    def configure(self):
        r1 = {
            "ACCOUNTS": self.env.env.accounts.fqdn,
            "CYCLADES": self.env.env.cyclades.fqdn,
            "mq_node": self.env.env.mq.ip,
            "db_node": self.env.env.db.ip,
            "synnefo_user": self.env.env.synnefo_user,
            "synnefo_db_passwd": self.env.env.synnefo_db_passwd,
            "synnefo_rabbitmq_passwd": self.env.env.synnefo_rabbitmq_passwd,
            "pithos_dir": self.env.env.pithos_dir,
            "common_bridge": self.env.env.common_bridge,
            "HOST": self.env.env.cyclades.ip,
            "domain": self.env.env.domain,
            "CYCLADES_SERVICE_TOKEN": self.env.service_token,
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
980
981
            "STATS": self.env.env.stats.fqdn,
            "SYNNEFO_VNC_PASSWD": self.env.env.synnefo_vnc_passwd,
982
            "CYCLADES_NODE_IP": self.env.env.cyclades.ip
983
984
985
986
987
988
989
990
991
992
993
994
995
            }
        return [
            ("/etc/synnefo/cyclades.conf", r1, {})
            ]

    def initialize(self):
        cpu = self.env.env.flavor_cpu
        ram = self.env.env.flavor_ram
        disk = self.env.env.flavor_disk
        storage = self.env.env.flavor_storage
        return [
            "snf-manage syncdb",
            "snf-manage migrate --delete-ghost-migrations",
996
997
            "snf-manage pool-create --type=mac-prefix \
              --base=aa:00:0 --size=65536",
998
999
1000
1001
1002
1003
1004
1005
1006
1007
            "snf-manage pool-create --type=bridge --base=prv --size=20",
            "snf-manage flavor-create %s %s %s %s" % (cpu, ram, disk, storage),
            ]

    def restart(self):
        return [
            "/etc/init.d/gunicorn restart",
            "/etc/init.d/snf-dispatcher restart",
            ]

1008

1009
1010
1011
1012
1013
class VNC(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "snf-vncauthproxy"
        ]

Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
1014
    def prepare(self):
1015
        return ["mkdir -p /var/lib/vncauthproxy"]
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027

    def configure(self):
        return [
            ("/var/lib/vncauthproxy/users", {}, {})
            ]

    def initialize(self):
        user = self.env.env.synnefo_user
        passwd = self.env.env.synnefo_vnc_passwd
        #TODO: run vncauthproxy-passwd
        return []

1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
    def restart(self):
        return [
            "/etc/init.d/vncauthproxy restart"
            ]


class Kamaki(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "python-progress",
        "kamaki",
        ]

    def initialize(self):
        url = "https://%s/astakos/identity/v2.0" % self.env.env.accounts.fqdn
1042
        token = self.env.user_auth_token
1043
        return [
1044
            "kamaki config set cloud.default.url %s" % url,
1045
            "kamaki config set cloud.default.token %s" % token,
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
            "kamaki container create images",
            ]

    def fetch_image(self):
        url = self.env.env.debian_base_url
        image = "debian_base.diskdump"
        return [
            "wget %s -O /tmp/%s" % (url, image)
            ]

    def upload_image(self):
        image = "debian_base.diskdump"
        return [
            "kamaki file upload --container images /tmp/%s %s" % (image, image)
            ]

    def register_image(self):
        image = "debian_base.diskdump"
        image_location = "/images/%s" % image
        cmd = """
        kamaki image register --name "Debian Base" --location {0} \
              --public --disk-format=diskdump \
              --property OSFAMILY=linux --property ROOT_PARTITION=1 \
              --property description="Debian Squeeze Base System" \
              --property size=450M --property kernel=2.6.32 \
              --property GUI="No GUI" --property sortorder=1 \
              --property USERS=root --property OS=debian
        """.format(image_location)
        return [
            "sleep 5",
            cmd
            ]

1079

1080
1081
1082
1083
1084
1085
class Burnin(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "kamaki",
        "snf-tools",
        ]

1086

1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
class Collectd(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "collectd",
        ]

    def configure(self):
        return [
            ("/etc/collectd/collectd.conf", {}, {}),
            ]

    def restart(self):
        return [
            "/etc/init.d/collectd restart",
            ]


class Stats(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "snf-stats-app",
        ]

    def prepare(self):
        return [
            "mkdir -p /var/cache/snf-stats-app/",
            "chown www-data:www-data /var/cache/snf-stats-app/",
            ]

    def configure(self):
        r1 = {
            "STATS": self.env.env.stats.fqdn,
            }
        return [
            ("/etc/synnefo/stats.conf", r1, {}),
            ("/etc/collectd/synnefo-stats.conf", r1, {}),
            ]

    def restart(self):
        return [
            "/etc/init.d/gunicorn restart",
            "/etc/init.d/apache2 restart",
            ]

1129

1130
1131
1132
1133
1134
1135
1136
1137
1138
class GanetiCollectd(SynnefoComponent):
    def configure(self):
        r1 = {
            "STATS": self.env.env.stats.fqdn,
            }
        return [
            ("/etc/collectd/passwd", {}, {}),
            ("/etc/collectd/synnefo-ganeti.conf", r1, {}),
            ]
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156


class Archip(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "librados2",
        "archipelago",
        "archipelago-dbg",
        "archipelago-modules-dkms",
        "archipelago-modules-source",
        "archipelago-rados",
        "archipelago-rados-dbg",
        "libxseg0",
        "libxseg0-dbg",
        "python-archipelago",
        "python-xseg",
        ]

    def prepare(self):
1157
        return ["mkdir -p /etc/archipelago"]
1158
1159

    def configure(self):
1160
        r1 = {"HOST": self.node_info.fqdn}
1161
        return [
1162
1163
1164
1165
            ("/etc/gunicorn.d/synnefo-archip", r1,
             {"remote": "/etc/gunicorn.d/synnefo"}),
            ("/etc/archipelago/pithos.conf.py", {}, {}),
            ("/etc/archipelago/archipelago.conf", {}, {})
1166
1167
1168
1169
            ]

    def restart(self):
        return [
1170
            "/etc/init.d/gunicorn restart",
1171
1172
1173
1174
1175
1176
1177
1178
            "archipelago restart"
            ]


class ArchipGaneti(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "archipelago-ganeti",
        ]