components.py 38.4 KB
Newer Older
Vangelis Koukis's avatar
Vangelis Koukis committed
1
# Copyright (C) 2010-2014 GRNET S.A.
2
#
Vangelis Koukis's avatar
Vangelis Koukis committed
3 4 5 6
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
7
#
Vangelis Koukis's avatar
Vangelis Koukis committed
8 9 10 11
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
12
#
Vangelis Koukis's avatar
Vangelis Koukis committed
13 14
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 16

import datetime
17 18
import ast
import simplejson
19
from snfdeploy.utils import debug
20
from snfdeploy.lib import Host
21

22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 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
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 []


67

68 69 70 71 72 73 74 75
class HW(SynnefoComponent):
    def test(self):
        return [
            "ping -c 1 %s" % self.node_info.ip,
            "ping -c 1 www.google.com",
            "apt-get update",
            ]

76

77 78 79 80 81 82 83 84 85 86 87 88
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"
            ]
89
        ssh = [("/root/.ssh/%s" % f, {}, {"mode": 0600}) for f in files]
90 91 92 93 94
        return ssh

    def initialize(self):
        f = "/root/.ssh/authorized_keys"
        return [
95
            "test -e {0}.bak && cat {0}.bak >> {0} || true".format(f)
96 97 98 99 100 101 102 103 104 105 106
            ]

    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",
107 108
            "echo %s > /etc/hostname" % self.node_info.hostname,
            "hostname %s" % self.node_info.hostname
109 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
            ]

    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)
            ]

199 200 201 202 203 204 205 206 207 208 209
    def add_qa_instances(self):
        instances = [
            ("xen-test-inst1", "1.2.3.4"),
            ("xen-test-inst2", "1.2.3.5"),
            ("xen-test-inst3", "1.2.3.6"),
            ("xen-test-inst4", "1.2.3.7"),
            ]
        for name, ip in instances:
            n = Host(name, ip, None, "vm." + self.env.env.domain, None, None)
            self.update_ns_for_node(n)

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
    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",
238 239
            "curl -k https://dev.grnet.gr/files/apt-grnetdev.pub | \
                apt-key add -",
240 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
            ]

    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" % \
291
            (user, node_info.ip, method, f)
292 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
        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",
341
        "ganeti-instance-debootstrap",
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
        ]

    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):
357 358 359 360 361 362 363 364 365 366
        ret = []
        disk = self.env.env.extra_disk
        vg = self.env.env.vg
        if disk:
            ret = [
                "test -e %s" % disk,
                "pvcreate %s" % disk,
                "vgcreate %s %s" % (vg, disk)
                ]
        return ret
367 368 369 370 371 372 373 374 375 376 377

    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"
378
            ] + self.prepare_net_infra() + self.prepare_lvm()
379 380

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


class Master(SynnefoComponent):
385 386 387 388 389 390
    def add_qa_rapi_user(self):
        cmd = """
echo ganeti-qa qa_example_passwd write >> /var/lib/ganeti/rapi/users
"""
        return [cmd] + self.restart()

391
    def add_rapi_user(self):
392 393 394
        user = self.env.env.synnefo_user
        passwd = self.env.env.synnefo_rapi_passwd
        x = "%s:Ganeti Remote API:%s" % (user, passwd)
395

396 397 398 399 400
        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)
401

402
        return [cmd] + self.restart()
403 404 405

    def add_node(self, node_info):
        commands = [
406
            "gnt-node add --no-ssh-key-check --master-capable=yes " +
407
            "--vm-capable=yes " + node_info.fqdn,
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
            ]
        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 \
430 431 432 433 434
            --ipolicy-std-specs \
cpu-count=1,disk-count=1,disk-size=1024,memory-size=128,nic-count=1,spindle-use=1 \
            --ipolicy-bounds-specs \
min:cpu-count=1,disk-count=1,disk-size=512,memory-size=128,nic-count=0,spindle-use=1/\
max:cpu-count=8,disk-count=16,disk-size=1048576,memory-size=32768,nic-count=8,spindle-use=12 \
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
            {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]

453 454 455 456 457 458 459 460
    def prepare(self):
        url = self.env.env.debian_base_url
        d = self.env.env.image_dir
        image = "debian_base.diskdump"
        return [
            "test -e %s/%s || wget %s -O %s/%s" % (d, image, url, d, image)
            ]

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    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]

484 485 486 487 488
    def prepare(self):
        return [
            "sed -i 's/false/true/' /etc/default/snf-ganeti-eventd",
            ]

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 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
    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 [
566 567
            "/etc/init.d/apache2 restart",
            ]
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587


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 [
588 589
            "/etc/init.d/gunicorn restart",
            ]
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617


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 [
618 619
            "/etc/init.d/gunicorn restart",
            ]
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645


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 [
646 647
            "/etc/init.d/gunicorn restart",
            ]
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669


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):
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
        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,
692 693 694
            ]

    def modify_all_quota(self):
695 696 697 698 699 700 701 702 703 704 705 706
        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,
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 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
            ]

    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 = []
812 813
        dirs = [self.env.env.pithos_dir, self.env.env.image_dir, "/srv/archip"]
        for d in dirs:
814 815 816 817 818
            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
819
""".format(self.env.env.nfs.ip, d)
820 821 822 823 824 825
            ret.append(cmd)

        return ret

    def initialize(self):
        ret = []
826 827
        dirs = [self.env.env.pithos_dir, self.env.env.image_dir, "/srv/archip"]
        for d in dirs:
828 829 830 831 832 833 834 835 836 837 838 839 840 841
            ret.append("mount %s" % d)
        return ret


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

    def prepare(self):
        p = self.env.env.pithos_dir
        return [
            "mkdir -p %s" % self.env.env.image_dir,
            "mkdir -p %s/data" % p,
842 843
            "mkdir -p /srv/archip/blocks",
            "mkdir -p /srv/archip/maps",
844 845
            "chown www-data.www-data %s/data" % p,
            "chmod g+ws %s/data" % p,
846
            ]
847 848 849 850 851 852

    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)
853
/srv/archip {2}(rw,async,no_subtree_check,no_root_squash)
854 855 856 857 858 859 860 861 862 863 864
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",
865
        "python-svipc",
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
        "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, {}),
            ]

897 898 899
    def initialize(self):
        return ["pithos-migrate stamp head"]

900

901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
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, {}),
            ]


919 920 921 922 923 924 925 926 927
class Cyclades(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "memcached",
        "python-memcache",
        "kamaki",
        "snf-cyclades-app",
        "python-django-south",
        ]

928 929 930 931 932
    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
933 934 935 936 937 938 939 940 941

        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]

942 943 944 945 946
    def add_network6(self):
        subnet = "babe::/64"
        gw = "babe::1"
        ntype = self.env.env.synnefo_public_network_type
        link = self.env.env.common_bridge
947

948 949 950 951 952
        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)
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971

        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 [
972
            "snf-manage backend-add --clustername=%s --user=%s --pass=%s" %
973
            (cluster.fqdn, user, passwd)
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
            ]

    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
999 1000
            "STATS": self.env.env.stats.fqdn,
            "SYNNEFO_VNC_PASSWD": self.env.env.synnefo_vnc_passwd,
1001
            "CYCLADES_NODE_IP": self.env.env.cyclades.ip
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
            }
        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",
1015 1016
            "snf-manage pool-create --type=mac-prefix \
              --base=aa:00:0 --size=65536",
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
            "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",
            ]

1027

1028 1029 1030 1031 1032
class VNC(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "snf-vncauthproxy"
        ]

Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
1033
    def prepare(self):
1034
        return ["mkdir -p /var/lib/vncauthproxy"]
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046

    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 []

1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
    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
1061
        token = self.env.user_auth_token
1062
        return [
1063
            "kamaki config set cloud.default.url %s" % url,
1064
            "kamaki config set cloud.default.token %s" % token,
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
            "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
            ]

1098

1099 1100 1101 1102 1103 1104
class Burnin(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "kamaki",
        "snf-tools",
        ]

1105

1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
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",
            ]

1148

1149 1150 1151 1152 1153 1154 1155 1156 1157
class GanetiCollectd(SynnefoComponent):
    def configure(self):
        r1 = {
            "STATS": self.env.env.stats.fqdn,
            }
        return [
            ("/etc/collectd/passwd", {}, {}),
            ("/etc/collectd/synnefo-ganeti.conf", r1, {}),
            ]
1158 1159


1160

1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
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):
1177
        return ["mkdir -p /etc/archipelago"]
1178 1179

    def configure(self):
1180
        r1 = {"HOST": self.node_info.fqdn}
1181
        r2 = {"SEGMENT_SIZE": self.env.env.segment_size}
1182
        return [
1183 1184 1185
            ("/etc/gunicorn.d/synnefo-archip", r1,
             {"remote": "/etc/gunicorn.d/synnefo"}),
            ("/etc/archipelago/pithos.conf.py", {}, {}),
1186
            ("/etc/archipelago/archipelago.conf", r2, {})
1187 1188 1189 1190
            ]

    def restart(self):
        return [
1191
            "/etc/init.d/gunicorn restart",
1192 1193 1194 1195 1196 1197 1198 1199
            "archipelago restart"
            ]


class ArchipGaneti(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "archipelago-ganeti",
        ]
1200 1201 1202 1203


class ExtStorage(SynnefoComponent):
    def prepare(self):
1204
        return ["mkdir -p /usr/local/lib/ganeti/"]
1205 1206 1207

    def initialize(self):
        url = "http://code.grnet.gr/git/extstorage"
1208
        extdir = "/usr/local/lib/ganeti/extstorage"
1209 1210 1211
        return [
            "git clone %s %s" % (url, extdir)
            ]
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341

class GanetiDev(SynnefoComponent):
    REQUIRED_PACKAGES = [
        "automake",
        "bridge-utils",
        "cabal-install",
        "fakeroot",
        "fping",
        "ghc",
        "ghc-haddock",
        "git",
        "graphviz",
        "hlint",
        "hscolour",
        "iproute",
        "iputils-arping",
        "libcurl4-openssl-dev",
        "libghc-attoparsec-dev",
        "libghc-crypto-dev",
        "libghc-curl-dev",
        "libghc-haddock-dev",
        "libghc-hinotify-dev",
        "libghc-hslogger-dev",
        "libghc-hunit-dev",
        "libghc-json-dev",
        "libghc-network-dev",
        "libghc-parallel-dev",
        "libghc-quickcheck2-dev",
        "libghc-regex-pcre-dev",
        "libghc-snap-server-dev",
        "libghc-temporary-dev",
        "libghc-test-framework-dev",
        "libghc-test-framework-hunit-dev",
        "libghc-test-framework-quickcheck2-dev",
        "libghc-text-dev",
        "libghc-utf8-string-dev",
        "libghc-vector-dev",
        "lvm2",
        "make",
        "ndisc6",
        "openssl",
        "pandoc",
        "pep8",
        "pylint",
        "python",
        "python-bitarray",
        "python-coverage",
        "python-epydoc",
        "python-ipaddr",
        "python-openssl",
        "python-pip",
        "python-pycurl",
        "python-pyinotify",
        "python-pyparsing",
        "python-setuptools",
        "python-simplejson",
        "python-sphinx",
        "python-yaml",
        "qemu-kvm",
        "socat",
        "ssh",
        "vim"
        ]

    CABAL = [
        "hinotify==0.3.2",
        "base64-bytestring",
        "lifted-base==0.2.0.3",
        "lens==3.10",
        ]

    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
        self.cluster = self.env.env.cluster
        self.qa_nodes = [self.env.env.master]

        for i, n in enumerate(self.env.env.cluster_nodes):
            self.qa_nodes.append(self.env.env.get_node_info(n))

        self.qa_nodes_list = []
        for n in self.qa_nodes:
            self.qa_nodes_list.append({
                "primary": n.fqdn,
                "secondary": n.ip,
                })

    def cabal(self):
        ret = ["cabal update"]
        for p in self.CABAL:
            ret.append("cabal install %s" % p)
        return ret

    def prepare(self):
        return self.cabal() + [
            "git clone git://git.ganeti.org/ganeti.git"
            ]

    def configure(self):
        """ Must return a list of tuples (tmpl_path, replace_dict, mode) """
        repl = {
            "CLUSTER_NAME": self.env.env.cluster_name,
            "VG": self.env.env.vg,
            "CLUSTER_NETDEV": self.env.env.cluster_netdev,
            "NODES": simplejson.dumps({"nodes": self.qa_nodes_list}),
            "DOMAIN": self.env.env.domain
            }
        return [
            ("/root/qa-sample.json", repl, {}),
            ]

    def initialize(self):
        """ Returs a list of bash commands that initialize the component """
        return [
            "cd ganeti; ./autogen.sh",
            "cd ganeti; ./configure --localstatedir=/var --sysconfdir=/etc",
            ]

    def test(self):
        """ Returs a list of bash commands that test existing installation """
        ret = []
        for n in self.qa_nodes:
            ret.append("ssh %s date" % n.hostname)
            ret.append("ssh %s date" % n.ip)
            ret.append("ssh %s date" % n.fqdn)
        return ret

    def restart(self):
        return []