Statistics
| Branch: | Tag: | Revision:

root / snf-deploy / snfdeploy / components.py @ d4f28d8f

History | View | Annotate | Download (31.2 kB)

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

    
34
import datetime
35
from snfdeploy.utils import debug
36

    
37

    
38
class SynnefoComponent(object):
39

    
40
    REQUIRED_PACKAGES = []
41

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

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

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

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

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

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

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

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

    
74
    def restart(self):
75
        return []
76

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

    
81

    
82
class HW(SynnefoComponent):
83
    def test(self):
84
        return [
85
            "ping -c 1 %s" % self.node_info.ip,
86
            "ping -c 1 www.google.com",
87
            "apt-get update",
88
            ]
89

    
90

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

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

    
106
    def initialize(self):
107
        f = "/root/.ssh/authorized_keys"
108
        return [
109
            "test -e {0}.bak && cat {0}.bak >> {0}".format(f)
110
            ]
111

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

    
115

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

    
123
    def configure(self):
124
        r1 = {
125
            "date": str(datetime.datetime.today()),
126
            "domain": self.env.env.domain,
127
            "ns_node_ip": self.env.env.ns.ip,
128
            }
129
        resolv = [
130
            ("/etc/resolv.conf", r1, {})
131
            ]
132
        return resolv
133

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

    
137

    
138
class DDNS(SynnefoComponent):
139
    REQUIRED_PACKAGES = [
140
        "dnsutils",
141
        ]
142

    
143
    def prepare(self):
144
        return [
145
            "mkdir -p /root/ddns/"
146
            ]
147

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

    
153

    
154
class NS(SynnefoComponent):
155
    REQUIRED_PACKAGES = [
156
        "bind9",
157
        ]
158

    
159
    def nsupdate(self, cmd):
160
        ret = """
161
nsupdate -k {0} > /dev/null <<EOF || true
162
server {1}
163
{2}
164
send
165
EOF
166
""".format(self.env.env.ddns_private_key, self.node_info.ip, cmd)
167
        return ret
168

    
169
    def prepare(self):
170
        return [
171
            "mkdir -p /etc/bind/zones",
172
            "chmod g+w /etc/bind/zones",
173
            "mkdir -p /etc/bind/rev",
174
            "chmod g+w /etc/bind/rev",
175
            ]
176

    
177
    def configure(self):
178
        d = self.env.env.domain
179
        ip = self.node_info.ip
180
        return [
181
            ("/etc/bind/named.conf.local", {"domain": d}, {}),
182
            ("/etc/bind/zones/example.com",
183
             {"domain": d, "ns_node_ip": ip},
184
             {"remote": "/etc/bind/zones/%s" % d}),
185
            ("/etc/bind/zones/vm.example.com",
186
             {"domain": d, "ns_node_ip": ip},
187
             {"remote": "/etc/bind/zones/vm.%s" % d}),
188
            ("/etc/bind/rev/synnefo.in-addr.arpa.zone", {"domain": d}, {}),
189
            ("/etc/bind/rev/synnefo.ip6.arpa.zone", {"domain": d}, {}),
190
            ("/etc/bind/named.conf.options",
191
             {"node_ips": ";".join(self.env.env.ips)}, {}),
192
            ("/root/ddns/ddns.key", {}, {"remote": "/etc/bind/ddns.key"}),
193
            ]
194

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

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

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

    
204
    def update_ns_for_node(self, node_info):
205
        return [
206
            self.update_arecord(node_info),
207
            self.update_cnamerecord(node_info),
208
            self.update_ptrrecord(node_info)
209
            ]
210

    
211
    def initialize(self):
212
        a = [self.update_arecord(n)
213
             for n in self.env.env.nodes_info.values()]
214
        ptr = [self.update_ptrrecord(n)
215
               for n in self.env.env.nodes_info.values()]
216
        cnames = [self.update_cnamerecord(n)
217
                  for n in self.env.env.roles_info.values()]
218

    
219
        return a + ptr + cnames
220

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

    
224
    def test(self):
225
        n = ["host %s localhost" % i.fqdn
226
             for i in self.env.env.nodes_info.values()]
227
        a = ["host %s localhost" % i.fqdn
228
             for i in self.env.env.roles_info.values()]
229
        return n + a
230

    
231

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

    
236
    def prepare(self):
237
        return [
238
            "echo 'APT::Install-Suggests \"false\";' >> /etc/apt/apt.conf",
239
            "curl -k https://dev.grnet.gr/files/apt-grnetdev.pub | \
240
                apt-key add -",
241
            ]
242

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

    
248
    def initialize(self):
249
        return [
250
            "apt-get update",
251
            ]
252

    
253

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

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

    
260
    def initialize(self):
261
        u = self.env.env.synnefo_user
262
        p = self.env.env.synnefo_rabbitmq_passwd
263
        return [
264
            "rabbitmqctl add_user %s %s" % (u, p),
265
            "rabbitmqctl set_permissions %s \".*\" \".*\" \".*\"" % u,
266
            "rabbitmqctl delete_user guest",
267
            "rabbitmqctl set_user_tags %s administrator" % u,
268
            ]
269

    
270

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

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

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

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

    
287
        return [cmd]
288

    
289
    def allow_access_in_db(self, node_info, user="all", method="md5"):
290
        f = "/etc/postgresql/*/main/pg_hba.conf"
291
        cmd1 = "echo host all %s %s/32 %s >> %s" % \
292
                (user, node_info.ip, method, f)
293
        cmd2 = "sed -i 's/\(host.*127.0.0.1.*\)md5/\\1trust/' %s" % f
294
        return [cmd1, cmd2] + self.restart()
295

    
296
    def configure(self):
297
        u = self.env.env.synnefo_user
298
        p = self.env.env.synnefo_db_passwd
299
        replace = {"synnefo_user": u, "synnefo_db_passwd": p}
300
        return [
301
            ("/tmp/db-init.psql", replace, {}),
302
            ]
303

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

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

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

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

    
323
    def destroy_db(self):
324
        return [
325
            """su - postgres -c ' psql -w -c "drop database snf_apps" '""",
326
            """su - postgres -c ' psql -w -c "drop database snf_pithos" '"""
327
            ]
328

    
329

    
330
class Ganeti(SynnefoComponent):
331

    
332
    REQUIRED_PACKAGES = [
333
        "qemu-kvm",
334
        "python-bitarray",
335
        "ganeti-htools",
336
        "ganeti-haskell",
337
        "snf-ganeti",
338
        "ganeti2",
339
        "bridge-utils",
340
        "lvm2",
341
        "drbd8-utils",
342
        ]
343

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

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

    
356
    def prepare_lvm(self):
357
        return [
358
            "pvcreate %s" % self.env.env.extra_disk,
359
            "vgcreate %s %s" % (self.env.env.extra_disk, self.env.env.vg)
360
            ]
361

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

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

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

    
377

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

    
383
        commands = [
384
          "echo %s {HA1}$(echo -n %s | openssl md5 | sed 's/^.* //') write > \
385
              /var/lib/ganeti/rapi/users" % (self.env.env.synnefo_user, x),
386
          ]
387

    
388
        return commands + self.restart()
389

    
390
    def add_node(self, node_info):
391
        commands = [
392
            "gnt-node add --no-ssh-key-check --master-capable=yes " +
393
              "--vm-capable=yes " + node_info.fqdn,
394
            ]
395
        return commands
396

    
397
    def try_use_vg(self):
398
        vg = self.env.env.vg
399
        return [
400
            "gnt-cluster modify --vg-name=%s || true" % vg,
401
            "gnt-cluster modify --disk-parameters=drbd:metavg=%s" % vg,
402
            "gnt-group modify --disk-parameters=drbd:metavg=%s default" % vg,
403
            ]
404

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

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

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

    
425

    
426
class Image(SynnefoComponent):
427
    REQUIRED_PACKAGES = [
428
        "snf-pithos-backend",
429
        "snf-image",
430
        ]
431

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

    
435
    def configure(self):
436
        tmpl = "/etc/default/snf-image"
437
        replace = {
438
            "synnefo_user": self.env.env.synnefo_user,
439
            "synnefo_db_passwd": self.env.env.synnefo_db_passwd,
440
            "pithos_dir": self.env.env.pithos_dir,
441
            "db_node": self.env.env.db.ip,
442
            "image_dir": self.env.env.image_dir,
443
            }
444
        return [(tmpl, replace, {})]
445

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

    
449

    
450
class GTools(SynnefoComponent):
451
    REQUIRED_PACKAGES = [
452
        "snf-cyclades-gtools",
453
        ]
454

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

    
458
    def configure(self):
459
        tmpl = "/etc/synnefo/gtools.conf"
460
        replace = {
461
            "synnefo_user": self.env.env.synnefo_user,
462
            "synnefo_rabbitmq_passwd": self.env.env.synnefo_rabbitmq_passwd,
463
            "mq_node": self.env.env.mq.ip,
464
            }
465
        return [(tmpl, replace, {})]
466

    
467
    def initialize(self):
468
        return [
469
            "sed -i 's/false/true/' /etc/default/snf-ganeti-eventd",
470
            "/etc/init.d/snf-ganeti-eventd start",
471
            ]
472

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

    
476

    
477
class Network(SynnefoComponent):
478
    REQUIRED_PACKAGES = [
479
        "python-nfqueue",
480
        "snf-network",
481
        "nfdhcpd",
482
        ]
483

    
484
    def configure(self):
485
        r1 = {
486
            "ns_node_ip": self.env.env.ns.ip
487
            }
488
        r2 = {
489
            "common_bridge": self.env.env.common_bridge,
490
            "public_iface": self.env.env.public_iface,
491
            "subnet": self.env.env.synnefo_public_network_subnet,
492
            "gateway": self.env.env.synnefo_public_network_gateway,
493
            "router_ip": self.env.env.router.ip,
494
            "node_ip": self.node_info.ip,
495
            }
496
        r3 = {
497
            "domain": self.env.env.domain,
498
            "server": self.env.env.ns.ip,
499
            "keyfile": self.env.env.ddns_private_key,
500
            }
501

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

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

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

    
514

    
515
class Apache(SynnefoComponent):
516
    REQUIRED_PACKAGES = [
517
        "apache2",
518
        ]
519

    
520
    def prepare(self):
521
        return [
522
            "a2enmod ssl", "a2enmod rewrite", "a2dissite default",
523
            "a2enmod headers",
524
            "a2enmod proxy_http", "a2dismod autoindex",
525
            ]
526

    
527
    def configure(self):
528
        r1 = {"HOST": self.node_info.fqdn}
529
        return [
530
            ("/etc/apache2/sites-available/synnefo", r1, {}),
531
            ("/etc/apache2/sites-available/synnefo-ssl", r1, {}),
532
            ]
533

    
534
    def initialize(self):
535
        return [
536
            "a2ensite synnefo", "a2ensite synnefo-ssl",
537
            ]
538

    
539
    def restart(self):
540
        return [
541
          "/etc/init.d/apache2 restart",
542
          ]
543

    
544

    
545
class Gunicorn(SynnefoComponent):
546
    REQUIRED_PACKAGES = [
547
        "gunicorn",
548
        ]
549

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

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

    
561
    def restart(self):
562
        return [
563
          "/etc/init.d/gunicorn restart",
564
          ]
565

    
566

    
567
class Common(SynnefoComponent):
568
    REQUIRED_PACKAGES = [
569
        # snf-common
570
        "python-objpool",
571
        "snf-common",
572
        "python-astakosclient",
573
        "snf-django-lib",
574
        "snf-branding",
575
        ]
576

    
577
    def configure(self):
578
        r1 = {
579
            "EMAIL_SUBJECT_PREFIX": self.node_info.hostname,
580
            "domain": self.env.env.domain,
581
            "HOST": self.node_info.fqdn,
582
            "MAIL_DIR": self.env.env.mail_dir,
583
            }
584
        return [
585
            ("/etc/synnefo/common.conf", r1, {}),
586
            ]
587

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

    
591
    def restart(self):
592
        return [
593
          "/etc/init.d/gunicorn restart",
594
          ]
595

    
596

    
597
class WEB(SynnefoComponent):
598
    REQUIRED_PACKAGES = [
599
        "snf-webproject",
600
        "python-psycopg2",
601
        "python-gevent",
602
        "python-django",
603
        ]
604

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

    
608
    def configure(self):
609
        r1 = {
610
            "synnefo_user": self.env.env.synnefo_user,
611
            "synnefo_db_passwd": self.env.env.synnefo_db_passwd,
612
            "db_node": self.env.env.db.fqdn,
613
            "domain": self.env.env.domain,
614
            }
615
        return [
616
            ("/etc/synnefo/webproject.conf", r1, {}),
617
            ]
618

    
619
    def restart(self):
620
        return [
621
          "/etc/init.d/gunicorn restart",
622
          ]
623

    
624

    
625
class Astakos(SynnefoComponent):
626
    REQUIRED_PACKAGES = [
627
        "python-django-south",
628
        "snf-astakos-app",
629
        "kamaki",
630
        ]
631

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

    
638
    def import_service(self):
639
        f = self.env.jsonfile
640
        return [
641
            "snf-manage service-import --json=%s" % f
642
            ]
643

    
644
    def set_default_quota(self):
645
        cmd = "snf-manage resource-modify --default-quota"
646
        return [
647
            "%s 40G pithos.diskspace" % cmd,
648
            "%s 2 astakos.pending_app" % cmd,
649
            "%s 4 cyclades.vm" % cmd,
650
            "%s 40G cyclades.disk" % cmd,
651
            "%s 16G cyclades.total_ram" % cmd,
652
            "%s 8G cyclades.ram" % cmd,
653
            "%s 32 cyclades.total_cpu" % cmd,
654
            "%s 16 cyclades.cpu" % cmd,
655
            "%s 4 cyclades.network.private" % cmd,
656
            "%s 4 cyclades.floating_ip" % cmd,
657
            ]
658

    
659
    def modify_all_quota(self):
660
        cmd = "snf-manage user-modify -f --all --base-quota"
661
        return [
662
            "%s pithos.diskspace 40G" % cmd,
663
            "%s astakos.pending_app 2" % cmd,
664
            "%s cyclades.vm 4" % cmd,
665
            "%s cyclades.disk 40G" % cmd,
666
            "%s cyclades.total_ram 16G" % cmd,
667
            "%s cyclades.ram 8G" % cmd,
668
            "%s cyclades.total_cpu 32" % cmd,
669
            "%s cyclades.cpu 16" % cmd,
670
            "%s cyclades.network.private 4" % cmd,
671
            "%s cyclades.floating_ip 4" % cmd,
672
            ]
673

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

    
679
    def configure(self):
680
        r1 = {
681
            "ACCOUNTS": self.env.env.accounts.fqdn,
682
            "domain": self.env.env.domain,
683
            "CYCLADES": self.env.env.cyclades.fqdn,
684
            "PITHOS": self.env.env.pithos.fqdn,
685
            }
686
        return [
687
            ("/etc/synnefo/astakos.conf", r1, {})
688
            ]
689

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

    
696
        return [
697
            "snf-manage syncdb --noinput",
698
            "snf-manage migrate im --delete-ghost-migrations",
699
            "snf-manage migrate quotaholder_app",
700
            "snf-manage migrate oa2",
701
            "snf-manage loaddata groups",
702
            oa2
703
            ] + self.astakos_register_components()
704

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

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

    
718
        return [h, c, p, a]
719

    
720
    def add_user(self):
721
        info = (
722
            self.env.env.user_passwd,
723
            self.env.env.user_email,
724
            self.env.env.user_name,
725
            self.env.env.user_lastname,
726
            )
727
        cmd = "snf-manage user-add --password %s %s %s %s" % info
728
        return [cmd]
729

    
730
    def activate_user(self):
731
        user_id = self.env.user_id
732
        return [
733
            "snf-manage user-modify --verify %s" % user_id,
734
            "snf-manage user-modify --accept %s" % user_id,
735
            ]
736

    
737

    
738
class CMS(SynnefoComponent):
739
    REQUIRED_PACKAGES = [
740
        "snf-cloudcms"
741
        ]
742

    
743
    def configure(self):
744
        r1 = {
745
            "ACCOUNTS": self.env.env.accounts.fqdn
746
            }
747
        r2 = {
748
            "DOMAIN": self.env.env.domain
749
            }
750
        return [
751
            ("/etc/synnefo/cms.conf", r1, {}),
752
            ("/tmp/sites.json", r2, {}),
753
            ("/tmp/page.json", {}, {}),
754
            ]
755

    
756
    def initialize(self):
757
        return [
758
            "snf-manage syncdb",
759
            "snf-manage migrate --delete-ghost-migrations",
760
            "snf-manage loaddata /tmp/sites.json",
761
            "snf-manage loaddata /tmp/page.json",
762
            "snf-manage createsuperuser --username=admin \
763
                  --email=admin@%s --noinput" % self.env.env.domain,
764
            ]
765

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

    
769

    
770
class Mount(SynnefoComponent):
771
    REQUIRED_PACKAGES = [
772
        "nfs-common"
773
        ]
774

    
775
    def prepare(self):
776
        ret = []
777
        for d in [self.env.env.pithos_dir, self.env.env.image_dir]:
778
            ret.append("mkdir -p %s" % d)
779
            cmd = """
780
cat >> /etc/fstab <<EOF
781
{0}:{1} {1}  nfs defaults,rw,noatime,rsize=131072,wsize=131072 0 0
782
EOF
783
""".format(self.env.env.pithos.ip, d)
784
            ret.append(cmd)
785

    
786
        return ret
787

    
788
    def initialize(self):
789
        ret = []
790
        for d in [self.env.env.pithos_dir, self.env.env.image_dir]:
791
            ret.append("mount %s" % d)
792
        return ret
793

    
794

    
795
class NFS(SynnefoComponent):
796
    REQUIRED_PACKAGES = [
797
        "nfs-kernel-server"
798
        ]
799

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

    
806
    def prepare(self):
807
        p = self.env.env.pithos_dir
808
        return [
809
            "mkdir -p %s" % self.env.env.image_dir,
810
            "mkdir -p %s/data" % p,
811
            "chown www-data.www-data %s/data" % p,
812
            "chmod g+ws %s/data" % p,
813
            ] + self.prepare_image()
814

    
815
    def update_exports(self, node_info):
816
        cmd = """
817
cat >> /etc/exports <<EOF
818
{0} {2}(rw,async,no_subtree_check,no_root_squash)
819
{1} {2}(rw,async,no_subtree_check,no_root_squash)
820
EOF
821
""".format(self.env.env.pithos_dir, self.env.env.image_dir, node_info.ip)
822
        return [cmd] + self.restart()
823

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

    
827

    
828
class Pithos(SynnefoComponent):
829
    REQUIRED_PACKAGES = [
830
        "kamaki",
831
        "snf-pithos-backend",
832
        "snf-pithos-app",
833
        "snf-pithos-webclient",
834
        ]
835

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

    
842
    def configure(self):
843
        r1 = {
844
            "ACCOUNTS": self.env.env.accounts.fqdn,
845
            "PITHOS": self.env.env.pithos.fqdn,
846
            "db_node": self.env.env.db.ip,
847
            "synnefo_user": self.env.env.synnefo_user,
848
            "synnefo_db_passwd": self.env.env.synnefo_db_passwd,
849
            "pithos_dir": self.env.env.pithos_dir,
850
            "PITHOS_SERVICE_TOKEN": self.env.service_token,
851
            "oa2_secret": self.env.env.oa2_secret,
852
            }
853
        r2 = {
854
            "ACCOUNTS": self.env.env.accounts.fqdn,
855
            "PITHOS_UI_CLOUDBAR_ACTIVE_SERVICE": self.env.service_id,
856
            }
857

    
858
        return [
859
            ("/etc/synnefo/pithos.conf", r1, {}),
860
            ("/etc/synnefo/webclient.conf", r2, {}),
861
            ]
862

    
863

    
864
class Cyclades(SynnefoComponent):
865
    REQUIRED_PACKAGES = [
866
        "memcached",
867
        "python-memcache",
868
        "snf-pithos-backend",
869
        "kamaki",
870
        "snf-cyclades-app",
871
        "python-django-south",
872
        ]
873

    
874
    def add_network(self):
875
        subnet = self.env.env.synnefo_public_network_subnet
876
        gw = self.env.env.synnefo_public_network_gateway
877
        ntype = self.env.env.synnefo_public_network_type
878
        link = self.env.env.common_bridge
879

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

    
886
        return [cmd]
887

    
888
    def add_network6(self):
889
        subnet = "babe::/64"
890
        gw = "babe::1"
891
        ntype = self.env.env.synnefo_public_network_type
892
        link = self.env.env.common_bridge
893

    
894
        cmd = """
895
snf-manage network-create --subnet6={0} \
896
      --gateway6={1} --public --dhcp=True --flavor={2} --mode=bridged \
897
       --link={3} --name=IPv6PublicNetwork
898
""".format(subnet, gw, ntype, link)
899

    
900
        return [cmd]
901

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

    
908
    def list_backends(self):
909
        return [
910
            "snf-manage backend-list"
911
            ]
912

    
913
    def add_backend(self):
914
        cluster = self.env.env.cluster
915
        user = self.env.env.synnefo_user
916
        passwd = self.env.env.synnefo_rapi_passwd
917
        return [
918
            "snf-manage backend-add --clustername=%s --user=%s --pass=%s" %
919
              (cluster.fqdn, user, passwd)
920
            ]
921

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

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

    
931
    def configure(self):
932
        r1 = {
933
            "ACCOUNTS": self.env.env.accounts.fqdn,
934
            "CYCLADES": self.env.env.cyclades.fqdn,
935
            "mq_node": self.env.env.mq.ip,
936
            "db_node": self.env.env.db.ip,
937
            "synnefo_user": self.env.env.synnefo_user,
938
            "synnefo_db_passwd": self.env.env.synnefo_db_passwd,
939
            "synnefo_rabbitmq_passwd": self.env.env.synnefo_rabbitmq_passwd,
940
            "pithos_dir": self.env.env.pithos_dir,
941
            "common_bridge": self.env.env.common_bridge,
942
            "HOST": self.env.env.cyclades.ip,
943
            "domain": self.env.env.domain,
944
            "CYCLADES_SERVICE_TOKEN": self.env.service_token,
945
            "STATS": self.env.env.stats.fqdn
946
            }
947
        return [
948
            ("/etc/synnefo/cyclades.conf", r1, {})
949
            ]
950

    
951
    def initialize(self):
952
        cpu = self.env.env.flavor_cpu
953
        ram = self.env.env.flavor_ram
954
        disk = self.env.env.flavor_disk
955
        storage = self.env.env.flavor_storage
956
        return [
957
            "snf-manage syncdb",
958
            "snf-manage migrate --delete-ghost-migrations",
959
            "snf-manage pool-create --type=mac-prefix \
960
              --base=aa:00:0 --size=65536",
961
            "snf-manage pool-create --type=bridge --base=prv --size=20",
962
            "snf-manage flavor-create %s %s %s %s" % (cpu, ram, disk, storage),
963
            ]
964

    
965
    def restart(self):
966
        return [
967
            "/etc/init.d/gunicorn restart",
968
            "/etc/init.d/snf-dispatcher restart",
969
            ]
970

    
971

    
972
class VNC(SynnefoComponent):
973
    REQUIRED_PACKAGES = [
974
        "snf-vncauthproxy"
975
        ]
976

    
977
    def restart(self):
978
        return [
979
            "/etc/init.d/vncauthproxy restart"
980
            ]
981

    
982

    
983
class Kamaki(SynnefoComponent):
984
    REQUIRED_PACKAGES = [
985
        "python-progress",
986
        "kamaki",
987
        ]
988

    
989
    def initialize(self):
990
        url = "https://%s/astakos/identity/v2.0" % self.env.env.accounts.fqdn
991
        return [
992
            "kamaki config set cloud.default.url %s" % url,
993
            "kamaki config set cloud.default.token %s" %
994
              self.env.user_auth_token,
995
            "kamaki container create images",
996
            ]
997

    
998
    def fetch_image(self):
999
        url = self.env.env.debian_base_url
1000
        image = "debian_base.diskdump"
1001
        return [
1002
            "wget %s -O /tmp/%s" % (url, image)
1003
            ]
1004

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

    
1011
    def register_image(self):
1012
        image = "debian_base.diskdump"
1013
        image_location = "/images/%s" % image
1014
        cmd = """
1015
        kamaki image register --name "Debian Base" --location {0} \
1016
              --public --disk-format=diskdump \
1017
              --property OSFAMILY=linux --property ROOT_PARTITION=1 \
1018
              --property description="Debian Squeeze Base System" \
1019
              --property size=450M --property kernel=2.6.32 \
1020
              --property GUI="No GUI" --property sortorder=1 \
1021
              --property USERS=root --property OS=debian
1022
        """.format(image_location)
1023
        return [
1024
            "sleep 5",
1025
            cmd
1026
            ]
1027

    
1028

    
1029
class Burnin(SynnefoComponent):
1030
    REQUIRED_PACKAGES = [
1031
        "kamaki",
1032
        "snf-tools",
1033
        ]
1034

    
1035

    
1036
class Collectd(SynnefoComponent):
1037
    REQUIRED_PACKAGES = [
1038
        "collectd",
1039
        ]
1040

    
1041
    def configure(self):
1042
        return [
1043
            ("/etc/collectd/collectd.conf", {}, {}),
1044
            ]
1045

    
1046
    def restart(self):
1047
        return [
1048
            "/etc/init.d/collectd restart",
1049
            ]
1050

    
1051

    
1052
class Stats(SynnefoComponent):
1053
    REQUIRED_PACKAGES = [
1054
        "snf-stats-app",
1055
        ]
1056

    
1057
    def prepare(self):
1058
        return [
1059
            "mkdir -p /var/cache/snf-stats-app/",
1060
            "chown www-data:www-data /var/cache/snf-stats-app/",
1061
            ]
1062

    
1063
    def configure(self):
1064
        r1 = {
1065
            "STATS": self.env.env.stats.fqdn,
1066
            }
1067
        return [
1068
            ("/etc/synnefo/stats.conf", r1, {}),
1069
            ("/etc/collectd/synnefo-stats.conf", r1, {}),
1070
            ]
1071

    
1072
    def restart(self):
1073
        return [
1074
            "/etc/init.d/gunicorn restart",
1075
            "/etc/init.d/apache2 restart",
1076
            ]
1077

    
1078

    
1079
class GanetiCollectd(SynnefoComponent):
1080
    def configure(self):
1081
        r1 = {
1082
            "STATS": self.env.env.stats.fqdn,
1083
            }
1084
        return [
1085
            ("/etc/collectd/passwd", {}, {}),
1086
            ("/etc/collectd/synnefo-ganeti.conf", r1, {}),
1087
            ]