Statistics
| Branch: | Tag: | Revision:

root / snf-deploy / fabfile.py @ 9f93a6fb

History | View | Annotate | Download (40.2 kB)

1
from __future__ import with_statement
2
from fabric.api import *
3
from fabric.contrib.console import confirm
4
from random import choice
5
from fabric.operations import run, put
6
import re
7
import shutil, os
8
from functools import wraps
9
import imp
10
import ConfigParser
11
import sys
12
import tempfile
13
import ast
14
from snfdeploy.lib import *
15
from snfdeploy import massedit
16

    
17

    
18
def setup_env(confdir="conf", packages="packages",
19
              templates="files", cluster_name="ganeti1", autoconf=False, disable_colors=False, key_inject=False):
20
    print("Loading configuration for synnefo...")
21
    print(" * Using config files under %s..." % confdir)
22
    print(" * Using %s and %s for packages and templates accordingly..." % (packages, templates))
23

    
24
    autoconf = ast.literal_eval(autoconf)
25
    disable_colors = ast.literal_eval(disable_colors)
26
    env.key_inject = ast.literal_eval(key_inject)
27
    conf = Conf.configure(confdir=confdir, cluster_name=cluster_name, autoconf=autoconf)
28
    env.env = Env(conf)
29

    
30
    env.local = autoconf
31
    env.password = env.env.password
32
    env.user = env.env.user
33
    env.shell = "/bin/bash -c"
34

    
35
    if disable_colors:
36
        disable_color()
37

    
38
    if env.env.cms.hostname in [env.env.accounts.hostname, env.env.cyclades.hostname, env.env.pithos.hostname]:
39
      env.cms_pass = True
40
    else:
41
      env.cms_pass = False
42

    
43
    if env.env.accounts.hostname in [env.env.cyclades.hostname, env.env.pithos.hostname]:
44
      env.csrf_disable = True
45
    else:
46
      env.csrf_disable = False
47

    
48

    
49
    env.roledefs = {
50
        "nodes": env.env.ips,
51
        "ips": env.env.ips,
52
        "accounts": [env.env.accounts.ip],
53
        "cyclades": [env.env.cyclades.ip],
54
        "pithos": [env.env.pithos.ip],
55
        "cms": [env.env.cms.ip],
56
        "mq": [env.env.mq.ip],
57
        "db": [env.env.db.ip],
58
        "ns": [env.env.ns.ip],
59
        "client": [env.env.client.ip],
60
        "router": [env.env.router.ip],
61
    }
62

    
63
    env.enable_lvm = False
64
    env.enable_drbd = False
65
    if ast.literal_eval(env.env.create_extra_disk) and env.env.extra_disk:
66
        env.enable_lvm = True
67
        env.enable_drbd = True
68

    
69
    env.roledefs.update({
70
        "ganeti": env.env.cluster_ips,
71
        "master": [env.env.master.ip],
72
    })
73

    
74

    
75
def install_package(package):
76
    debug(env.host, " * Installing package %s..." % package)
77
    APT_GET = "export DEBIAN_FRONTEND=noninteractive ;apt-get install -y --force-yes "
78

    
79
    host_info = env.env.ips_info[env.host]
80
    if ast.literal_eval(env.env.use_local_packages):
81
        with settings(warn_only=True):
82
            deb = local("ls %s/%s*%s_all.deb" % (env.env.packages, package, host_info[os]))
83
            if deb:
84
                debug(env.host, " * Package %s found in %s..." % (package, env.env.packages))
85
                put(deb, "/tmp/")
86
                try_run("dpkg -i /tmp/%s*deb || " % package + APT_GET + "-f")
87
                try_run("rm /tmp/%s*deb" % package)
88
                return
89

    
90
    info = getattr(env.env, package)
91
    if info in ["squeeze-backports", "stable", "testing", "unstable"]:
92
        if  info == "squeeze-backports" and host_infa.os == "wheezy":
93
          info = host_info.os
94
        APT_GET += " -t %s %s " % (info, package)
95
    elif info:
96
        APT_GET += " %s=%s " % (package, info)
97
    else:
98
        APT_GET += package
99

    
100
    try_run(APT_GET)
101

    
102
    return
103

    
104

    
105
@roles("ns")
106
def update_ns_for_ganeti():
107
    debug(env.host, "Updating name server entries for backend %s..." % env.env.cluster.fqdn)
108
    update_arecord(env.env.cluster)
109
    update_ptrrecord(env.env.cluster)
110
    try_run("/etc/init.d/bind9 restart")
111

    
112

    
113
@roles("ns")
114
def update_ns_for_node(node):
115
    info = env.env.nodes_info.get(node)
116
    update_arecord(info)
117
    update_ptrrecord(info)
118
    try_run("/etc/init.d/bind9 restart")
119

    
120

    
121
@roles("ns")
122
def update_arecord(host):
123
    filename = "/etc/bind/zones/" + env.env.domain
124
    cmd = """
125
    echo '{0}' >> {1}
126
    """.format(host.arecord, filename)
127
    try_run(cmd)
128

    
129

    
130
@roles("ns")
131
def update_cnamerecord(host):
132
    filename = "/etc/bind/zones/" + env.env.domain
133
    cmd = """
134
    echo '{0}' >> {1}
135
    """.format(host.cnamerecord, filename)
136
    try_run(cmd)
137

    
138

    
139
@roles("ns")
140
def update_ptrrecord(host):
141
    filename = "/etc/bind/rev/synnefo.in-addr.arpa.zone"
142
    cmd = """
143
    echo '{0}' >> {1}
144
    """.format(host.ptrrecord, filename)
145
    try_run(cmd)
146

    
147
@roles("nodes")
148
def apt_get_update():
149
    debug(env.host, "apt-get update....")
150
    try_run("apt-get update")
151

    
152
@roles("ns")
153
def setup_ns():
154
    debug(env.host, "Setting up name server..")
155
    #WARNING: this should be remove after we are done
156
    # because gevent does pick randomly nameservers and google does
157
    # not know our setup!!!!!
158
    apt_get_update()
159
    install_package("bind9")
160
    tmpl = "/etc/bind/named.conf.local"
161
    replace = {
162
      "domain": env.env.domain,
163
      }
164
    custom = customize_settings_from_tmpl(tmpl, replace)
165
    put(custom, tmpl)
166

    
167
    try_run("mkdir -p /etc/bind/zones")
168
    tmpl = "/etc/bind/zones/example.com"
169
    replace = {
170
      "domain": env.env.domain,
171
      "ns_node_ip": env.env.ns.ip,
172
      }
173
    custom = customize_settings_from_tmpl(tmpl, replace)
174
    remote = "/etc/bind/zones/" + env.env.domain
175
    put(custom, remote)
176

    
177
    try_run("mkdir -p /etc/bind/rev")
178
    tmpl = "/etc/bind/rev/synnefo.in-addr.arpa.zone"
179
    replace = {
180
      "domain": env.env.domain,
181
      }
182
    custom = customize_settings_from_tmpl(tmpl, replace)
183
    put(custom, tmpl)
184

    
185
    tmpl = "/etc/bind/named.conf.options"
186
    replace = {
187
      "NODE_IPS": ";".join(env.env.ips),
188
      }
189
    custom = customize_settings_from_tmpl(tmpl, replace)
190
    put(custom, tmpl, mode=0644)
191

    
192
    for role, info in env.env.roles.iteritems():
193
        if role == "ns":
194
            continue
195
        update_cnamerecord(info)
196
    for node, info in env.env.nodes_info.iteritems():
197
        update_arecord(info)
198
        update_ptrrecord(info)
199

    
200
    try_run("/etc/init.d/bind9 restart")
201

    
202

    
203
@roles("nodes")
204
def check_dhcp():
205
    debug(env.host, "Checking IPs for synnefo..")
206
    for n, info in env.env.nodes_info.iteritems():
207
        try_run("ping -c 1 " + info.ip, True)
208

    
209
@roles("nodes")
210
def check_dns():
211
    debug(env.host, "Checking fqdns for synnefo..")
212
    for n, info in env.env.nodes_info.iteritems():
213
        try_run("ping -c 1 " + info.fqdn, True)
214

    
215
    for n, info in env.env.roles.iteritems():
216
        try_run("ping -c 1 " + info.fqdn, True)
217

    
218
@roles("nodes")
219
def check_connectivity():
220
    debug(env.host, "Checking internet connectivity..")
221
    try_run("ping -c 1 www.google.com", True)
222

    
223

    
224
@roles("nodes")
225
def check_ssh():
226
    debug(env.host, "Checking password-less ssh..")
227
    for n, info in env.env.nodes_info.iteritems():
228
        try_run("ssh " + info.fqdn + "  date", True)
229

    
230

    
231
@roles("ips")
232
def add_keys():
233
    if not env.key_inject:
234
      debug(env.host, "Skipping ssh keys injection..")
235
      return
236
    else:
237
      debug(env.host, "Adding rsa/dsa keys..")
238
    try_run("mkdir -p /root/.ssh")
239
    cmd = """
240
for f in $(ls /root/.ssh/*); do
241
  cp $f $f.bak
242
done
243
    """
244
    try_run(cmd)
245
    files = ["authorized_keys", "id_dsa", "id_dsa.pub",
246
             "id_rsa", "id_rsa.pub"]
247
    for f in files:
248
      tmpl = "/root/.ssh/" + f
249
      replace = {}
250
      custom = customize_settings_from_tmpl(tmpl, replace)
251
      put(custom, tmpl, mode=0600)
252

    
253
    cmd = """
254
if [ -e /root/.ssh/authorized_keys.bak ]; then
255
  cat /root/.ssh/authorized_keys.bak >> /root/.ssh/authorized_keys
256
fi
257
    """
258
    debug(env.host, "Updating exising authorized keys..")
259
    try_run(cmd)
260

    
261
@roles("ips")
262
def setup_resolv_conf():
263
    debug(env.host, "Tweak /etc/resolv.conf...")
264
    try_run("/etc/init.d/network-manager stop")
265
    tmpl = "/etc/dhcp/dhclient-enter-hooks.d/nodnsupdate"
266
    replace = {}
267
    custom = customize_settings_from_tmpl(tmpl, replace)
268
    put(custom, tmpl, mode=0644)
269
    try_run("cp /etc/resolv.conf /etc/resolv.conf.bak")
270
    tmpl = "/etc/resolv.conf"
271
    replace = {
272
      "domain": env.env.domain,
273
      "ns_node_ip": env.env.ns.ip,
274
      }
275
    custom = customize_settings_from_tmpl(tmpl, replace)
276
    try:
277
      put(custom, tmpl)
278
    except:
279
      pass
280
    try_run("chattr +i /etc/resolv.conf")
281

    
282

    
283
@roles("ips")
284
def setup_hosts():
285
    debug(env.host, "Tweaking /etc/hosts and ssh_config files...")
286
    try_run("echo StrictHostKeyChecking no >> /etc/ssh/ssh_config")
287
    cmd = " sed -i 's/^127.*/127.0.0.1 localhost/' /etc/hosts "
288
    try_run(cmd)
289
    host_info = env.env.ips_info[env.host]
290
    cmd = "hostname %s" % host_info.hostname
291
    try_run(cmd)
292
    cmd = "echo %s > /etc/hostname" % host_info.hostname
293
    try_run(cmd)
294

    
295

    
296
def try_run(cmd, abort=False):
297
    try:
298
      if env.local:
299
        return local(cmd, capture=True)
300
      else:
301
        return run(cmd)
302
    except:
303
      debug(env.host, "WARNING: command failed. Continuing anyway...")
304
      if abort:
305
        raise
306

    
307
def create_bridges():
308
    debug(env.host, " * Creating bridges...")
309
    install_package("bridge-utils")
310
    cmd = """
311
    brctl addbr {0} ; ip link set {0} up
312
    """.format(env.env.common_bridge)
313
    try_run(cmd)
314

    
315

    
316
def connect_bridges():
317
    debug(env.host, " * Connecting bridges...")
318
    cmd = """
319
    brctl addif {0} {1}
320
    """.format(env.env.common_bridge, env.env.public_iface)
321
    #try_run(cmd)
322

    
323

    
324
@roles("ganeti")
325
def setup_net_infra():
326
    debug(env.host, "Setup networking infrastracture..")
327
    create_bridges()
328
    connect_bridges()
329

    
330

    
331
@roles("ganeti")
332
def setup_lvm():
333
    debug(env.host, "create volume group %s for ganeti.." % env.env.vg)
334
    if env.enable_lvm:
335
        install_package("lvm2")
336
        cmd = """
337
        pvcreate {0}
338
        vgcreate {1} {0}
339
        """.format(env.env.extra_disk, env.env.vg)
340
        try_run(cmd)
341

    
342

    
343
def customize_settings_from_tmpl(tmpl, replace):
344
    debug(env.host, " * Customizing template %s..." % tmpl)
345
    local = env.env.templates + tmpl
346
    _, custom = tempfile.mkstemp()
347
    shutil.copyfile(local, custom)
348
    for k, v in replace.iteritems():
349
        regex = "re.sub('%{0}%', '{1}', line)".format(k.upper(), v)
350
        massedit.edit_files([custom], [regex], dry_run=False)
351

    
352
    return custom
353

    
354

    
355
@roles("nodes")
356
def setup_apt():
357
    debug(env.host, "Setting up apt sources...")
358
    install_package("curl")
359
    cmd = """
360
    echo 'APT::Install-Suggests "false";' >> /etc/apt/apt.conf
361
    curl -k https://dev.grnet.gr/files/apt-grnetdev.pub | apt-key add -
362
    """
363
    try_run(cmd)
364
    host_info = env.env.ips_info[env.host]
365
    if host_info.os == "squeeze":
366
      tmpl = "/etc/apt/sources.list.d/synnefo.squeeze.list"
367
    else:
368
      tmpl = "/etc/apt/sources.list.d/synnefo.wheezy.list"
369
    replace = {}
370
    custom = customize_settings_from_tmpl(tmpl, replace)
371
    put(custom, tmpl)
372
    apt_get_update()
373

    
374

    
375
@roles("cyclades", "cms", "pithos", "accounts")
376
def restart_services():
377
    debug(env.host, " * Restarting apache2 and gunicorn...")
378
    try_run("/etc/init.d/gunicorn restart")
379
    try_run("/etc/init.d/apache2 restart")
380

    
381

    
382
def setup_gunicorn():
383
    debug(env.host, " * Setting up gunicorn...")
384
    install_package("gunicorn")
385
    tmpl = "/etc/gunicorn.d/synnefo"
386
    replace = {}
387
    custom = customize_settings_from_tmpl(tmpl, replace)
388
    put(custom, tmpl, mode=0644)
389
    try_run("/etc/init.d/gunicorn restart")
390

    
391

    
392
def setup_apache():
393
    debug(env.host, " * Setting up apache2...")
394
    host_info = env.env.ips_info[env.host]
395
    install_package("apache2")
396
    tmpl = "/etc/apache2/sites-available/synnefo"
397
    replace = {
398
        "HOST": host_info.fqdn,
399
    }
400
    custom = customize_settings_from_tmpl(tmpl, replace)
401
    put(custom, tmpl)
402
    tmpl = "/etc/apache2/sites-available/synnefo-ssl"
403
    custom = customize_settings_from_tmpl(tmpl, replace)
404
    put(custom, tmpl)
405
    cmd = """
406
    a2enmod ssl
407
    a2enmod rewrite
408
    a2dissite default
409
    a2ensite synnefo
410
    a2ensite synnefo-ssl
411
    a2enmod headers
412
    a2enmod proxy_http
413
    a2dismod autoindex
414
    """
415
    try_run(cmd)
416
    try_run("/etc/init.d/apache2 restart")
417

    
418

    
419
@roles("mq")
420
def setup_mq():
421
    debug(env.host, "Setting up RabbitMQ...")
422
    install_package("rabbitmq-server")
423
    cmd = """
424
    rabbitmqctl add_user {0} {1}
425
    rabbitmqctl set_permissions {0} ".*" ".*" ".*"
426
    rabbitmqctl delete_user guest
427
    rabbitmqctl set_user_tags {0} administrator
428
    """.format(env.env.synnefo_user, env.env.synnefo_rabbitmq_passwd)
429
    try_run(cmd)
430
    try_run("/etc/init.d/rabbitmq-server restart")
431

    
432

    
433
@roles("db")
434
def allow_access_in_db(ip, user="all", method="md5"):
435
    cmd = """
436
    echo host all {0} {1}/32 {2} >> /etc/postgresql/8.4/main/pg_hba.conf
437
    """.format(user, ip, method)
438
    try_run(cmd)
439
    cmd = """
440
    sed -i 's/\(host.*127.0.0.1.*\)md5/\\1trust/' /etc/postgresql/8.4/main/pg_hba.conf
441
    """
442
    try_run(cmd)
443
    try_run("/etc/init.d/postgresql restart")
444

    
445
@roles("db")
446
def setup_db():
447
    debug(env.host, "Setting up DataBase server...")
448
    install_package("postgresql")
449

    
450
    tmpl = "/tmp/db-init.psql"
451
    replace = {
452
        "synnefo_user": env.env.synnefo_user,
453
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
454
        }
455
    custom = customize_settings_from_tmpl(tmpl, replace)
456
    put(custom, tmpl)
457
    cmd = 'su - postgres -c "psql -w -f %s" ' % tmpl
458
    try_run(cmd)
459
    cmd = """
460
    echo "listen_addresses = '*'" >> /etc/postgresql/8.4/main/postgresql.conf
461
    """
462
    try_run(cmd)
463

    
464
    if env.env.testing_vm:
465
        cmd = """
466
        echo "fsync=off\nsynchronous_commit=off\nfull_page_writes=off" >> /etc/postgresql/8.4/main/postgresql.conf
467
        """
468
        try_run(cmd)
469

    
470
    allow_access_in_db(env.host, "all", "trust")
471
    try_run("/etc/init.d/postgresql restart")
472

    
473

    
474
@roles("db")
475
def destroy_db():
476
    try_run("""su - postgres -c ' psql -w -c "drop database snf_apps" '""")
477
    try_run("""su - postgres -c ' psql -w -c "drop database snf_pithos" '""")
478

    
479

    
480
def setup_webproject():
481
    debug(env.host, " * Setting up snf-webproject...")
482
    with settings(hide("everything")):
483
        try_run("ping -c1 " + env.env.db.ip)
484
    setup_common()
485
    install_package("snf-webproject")
486
    install_package("python-psycopg2")
487
    install_package("python-gevent")
488
    tmpl = "/etc/synnefo/webproject.conf"
489
    replace = {
490
        "synnefo_user": env.env.synnefo_user,
491
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
492
        "db_node": env.env.db.ip,
493
        "domain": env.env.domain,
494
    }
495
    custom = customize_settings_from_tmpl(tmpl, replace)
496
    put(custom, tmpl, mode=0644)
497
    with settings(host_string=env.env.db.ip):
498
        host_info = env.env.ips_info[env.host]
499
        allow_access_in_db(host_info.ip, "all", "trust")
500
    try_run("/etc/init.d/gunicorn restart")
501

    
502

    
503
def setup_common():
504
    debug(env.host, " * Setting up snf-common...")
505
    host_info = env.env.ips_info[env.host]
506
    install_package("python-objpool")
507
    install_package("snf-common")
508
    install_package("python-astakosclient")
509
    install_package("snf-django-lib")
510
    install_package("snf-branding")
511
    tmpl = "/etc/synnefo/common.conf"
512
    replace = {
513
        #FIXME:
514
        "EMAIL_SUBJECT_PREFIX": env.host,
515
        "domain": env.env.domain,
516
        "HOST": host_info.fqdn,
517
    }
518
    custom = customize_settings_from_tmpl(tmpl, replace)
519
    put(custom, tmpl, mode=0644)
520
    try_run("/etc/init.d/gunicorn restart")
521

    
522
@roles("accounts")
523
def astakos_loaddata():
524
    debug(env.host, " * Loading initial data to astakos...")
525
    cmd = """
526
    snf-manage loaddata groups
527
    """
528
    try_run(cmd)
529

    
530

    
531
@roles("accounts")
532
def astakos_register_services():
533
    debug(env.host, " * Register services in astakos...")
534

    
535
    cyclades_base_url = "https://%s/cyclades/" % env.env.cyclades.fqdn
536
    pithos_base_url = "https://%s/pithos/" % env.env.pithos.fqdn
537
    astakos_base_url = "https://%s/astakos/" % env.env.accounts.fqdn
538

    
539
    cmd = """
540
    snf-manage component-add "home" https://{0} home-icon.png
541
    snf-manage component-add "cyclades" {1}ui/
542
    snf-manage component-add "pithos" {2}ui/
543
    snf-manage component-add "astakos" {3}ui/
544
    """.format(env.env.cms.fqdn, cyclades_base_url,
545
               pithos.base_url, astakos_base_url)
546
    try_run(cmd)
547
    import_service("astakos", astakos_base_url)
548
    import_service("pithos", pithos_base_url)
549
    import_service("cyclades", cyclades_base_url)
550
    cmd = """
551
    snf-manage resource-modify --limit 40G pithos.diskspace
552
    snf-manage resource-modify --limit 2 astakos.pending_app
553
    snf-manage resource-modify --limit 4 cyclades.vm
554
    snf-manage resource-modify --limit 40G cyclades.disk
555
    snf-manage resource-modify --limit 8G cyclades.ram
556
    snf-manage resource-modify --limit 16 cyclades.cpu
557
    snf-manage resource-modify --limit 4 cyclades.network.private
558
    """
559
    try_run(cmd)
560

    
561

    
562
@roles("accounts")
563
def add_user():
564
    debug(env.host, " * adding user %s to astakos..." % env.env.user_email)
565
    email=env.env.user_email
566
    name=env.env.user_name
567
    lastname=env.env.user_lastname
568
    passwd=env.env.user_passwd
569
    cmd = """
570
    snf-manage user-add {0} {1} {2}
571
    """.format(email, name, lastname)
572
    try_run(cmd)
573
    with settings(host_string=env.env.db.ip):
574
        uid, user_auth_token, user_uuid = get_auth_token_from_db(email)
575
    cmd = """
576
    snf-manage user-modify --password {0} {1}
577
    """.format(passwd, uid)
578
    try_run(cmd)
579

    
580

    
581
@roles("accounts")
582
def activate_user(user_email=None):
583
    if not user_email:
584
      user_email = env.env.user_email
585
    debug(env.host, " * Activate user %s..." % user_email)
586
    with settings(host_string=env.env.db.ip):
587
        uid, user_auth_token, user_uuid = get_auth_token_from_db(user_email)
588

    
589
    cmd = """
590
    snf-manage user-modify --verify {0}
591
    snf-manage user-modify --accept {0}
592
    """.format(uid)
593
    try_run(cmd)
594

    
595
@roles("accounts")
596
def setup_astakos():
597
    debug(env.host, "Setting up snf-astakos-app...")
598
    setup_gunicorn()
599
    setup_apache()
600
    setup_webproject()
601
    install_package("python-django-south")
602
    install_package("snf-astakos-app")
603
    install_package("kamaki")
604

    
605
    tmpl = "/etc/synnefo/astakos.conf"
606
    replace = {
607
      "ACCOUNTS": env.env.accounts.fqdn,
608
      "domain": env.env.domain,
609
      "CYCLADES": env.env.cyclades.fqdn,
610
      "PITHOS": env.env.pithos.fqdn,
611
    }
612
    custom = customize_settings_from_tmpl(tmpl, replace)
613
    put(custom, tmpl, mode=0644)
614
    if env.csrf_disable:
615
      cmd = """
616
cat <<EOF >> /etc/synnefo/astakos.conf
617
try:
618
  MIDDLEWARE_CLASSES.remove('django.middleware.csrf.CsrfViewMiddleware')
619
except:
620
  pass
621
EOF
622
"""
623
      try_run(cmd)
624

    
625
    try_run("/etc/init.d/gunicorn restart")
626

    
627
    cmd = """
628
    snf-manage syncdb --noinput
629
    snf-manage migrate im --delete-ghost-migrations
630
    snf-manage migrate quotaholder_app
631
    """
632
    try_run(cmd)
633

    
634

    
635
def import_service(service, base_url):
636
    try_run("snf-service-export %s %s | snf-manage service-import -" %
637
            (service, base_url))
638

    
639

    
640
@roles("accounts")
641
def get_service_details(service="pithos"):
642
    debug(env.host, " * Getting registered details for %s service..." % service)
643
    result = try_run("snf-manage component-list")
644
    r = re.compile(r".*%s.*" % service, re.M)
645
    service_id, _, _, service_token = r.search(result).group().split()
646
    # print("%s: %s %s" % (service, service_id, service_token))
647
    return (service_id, service_token)
648

    
649

    
650
@roles("db")
651
def get_auth_token_from_db(user_email=None):
652
    if not user_email:
653
        user_email=env.env.user_email
654
    debug(env.host, " * Getting authentication token and uuid for user %s..." % user_email)
655
    cmd = """
656
    echo "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}';" > /tmp/psqlcmd
657
    su - postgres -c  "psql -w -d snf_apps -f /tmp/psqlcmd"
658
    """.format(user_email)
659

    
660
    result = try_run(cmd)
661
    r = re.compile(r"(\d+)[ |]*(\S+)[ |]*(\S+)[ |]*" + user_email, re.M)
662
    match = r.search(result)
663
    uid, user_auth_token, user_uuid = match.groups()
664
    # print("%s: %s %s %s" % ( user_email, uid, user_auth_token, user_uuid))
665

    
666
    return (uid, user_auth_token, user_uuid)
667

    
668

    
669
@roles("cms")
670
def cms_loaddata():
671
    debug(env.host, " * Loading cms initial data...")
672
    if env.cms_pass:
673
      debug(env.host, "Aborting. Prerequisites not met.")
674
      return
675
    tmpl = "/tmp/sites.json"
676
    replace = {}
677
    custom = customize_settings_from_tmpl(tmpl, replace)
678
    put(custom, tmpl)
679

    
680
    tmpl = "/tmp/page.json"
681
    replace = {}
682
    custom = customize_settings_from_tmpl(tmpl, replace)
683
    put(custom, tmpl)
684

    
685
    cmd = """
686
    snf-manage loaddata /tmp/sites.json
687
    snf-manage loaddata /tmp/page.json
688
    snf-manage createsuperuser --username=admin --email=admin@{0} --noinput
689
    """.format(env.env.domain)
690
    try_run(cmd)
691

    
692

    
693
@roles("cms")
694
def setup_cms():
695
    debug(env.host, "Setting up cms...")
696
    if env.cms_pass:
697
      debug(env.host, "Aborting. Prerequisites not met.")
698
      return
699
    with settings(hide("everything")):
700
        try_run("ping -c1 accounts." + env.env.domain)
701
    setup_gunicorn()
702
    setup_apache()
703
    setup_webproject()
704
    install_package("snf-cloudcms")
705

    
706
    tmpl = "/etc/synnefo/cms.conf"
707
    replace = {
708
        "ACCOUNTS": env.env.accounts.fqdn,
709
        }
710
    custom = customize_settings_from_tmpl(tmpl, replace)
711
    put(custom, tmpl, mode=0644)
712
    try_run("/etc/init.d/gunicorn restart")
713

    
714

    
715
    cmd = """
716
    snf-manage syncdb
717
    snf-manage migrate --delete-ghost-migrations
718
    """.format(env.env.domain)
719
    try_run(cmd)
720

    
721

    
722
def setup_nfs_dirs():
723
    debug(env.host, " * Creating NFS mount point for pithos and ganeti...")
724
    cmd = """
725
    mkdir -p {0}
726
    cd {0}
727
    mkdir -p data
728
    chown www-data:www-data data
729
    chmod g+ws data
730
    mkdir -p /srv/okeanos
731
    """.format(env.env.pithos_dir)
732
    try_run(cmd)
733

    
734

    
735
@roles("nodes")
736
def setup_nfs_clients():
737
    if env.host == env.env.pithos.ip:
738
      return
739

    
740
    host_info = env.env.ips_info[env.host]
741
    debug(env.host, " * Mounting pithos NFS mount point...")
742
    with settings(hide("everything")):
743
        try_run("ping -c1 " + env.env.pithos.hostname)
744
    with settings(host_string=env.env.pithos.ip):
745
        update_nfs_exports(host_info.ip)
746

    
747
    install_package("nfs-common")
748
    for d in [env.env.pithos_dir, env.env.image_dir]:
749
      try_run("mkdir -p " + d)
750
      cmd = """
751
      echo "{0}:{1} {1}  nfs defaults,rw,noatime,rsize=131072,wsize=131072,timeo=14,intr,noacl" >> /etc/fstab
752
      """.format(env.env.pithos.ip, d)
753
      try_run(cmd)
754
      try_run("mount " + d)
755

    
756
@roles("pithos")
757
def update_nfs_exports(ip):
758
    tmpl = "/tmp/exports"
759
    replace = {
760
      "pithos_dir": env.env.pithos_dir,
761
      "image_dir": env.env.image_dir,
762
      "ip": ip,
763
      }
764
    custom = customize_settings_from_tmpl(tmpl, replace)
765
    put(custom, tmpl)
766
    try_run("cat %s >> /etc/exports" % tmpl)
767
    try_run("/etc/init.d/nfs-kernel-server restart")
768

    
769
@roles("pithos")
770
def setup_nfs_server():
771
    debug(env.host, " * Setting up NFS server for pithos...")
772
    setup_nfs_dirs()
773
    install_package("nfs-kernel-server")
774

    
775

    
776
@roles("pithos")
777
def setup_pithos():
778
    debug(env.host, "Setting up snf-pithos-app...")
779
    with settings(hide("everything")):
780
        try_run("ping -c1 accounts." + env.env.domain)
781
        try_run("ping -c1 " + env.env.db.ip)
782
    setup_gunicorn()
783
    setup_apache()
784
    setup_webproject()
785

    
786
    with settings(host_string=env.env.accounts.ip):
787
        service_id, service_token = get_service_details("pithos")
788

    
789
    install_package("kamaki")
790
    install_package("snf-pithos-backend")
791
    install_package("snf-pithos-app")
792
    tmpl = "/etc/synnefo/pithos.conf"
793
    replace = {
794
        "ACCOUNTS": env.env.accounts.fqdn,
795
        "PITHOS": env.env.pithos.fqdn,
796
        "db_node": env.env.db.ip,
797
        "synnefo_user": env.env.synnefo_user,
798
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
799
        "pithos_dir": env.env.pithos_dir,
800
        "PITHOS_SERVICE_TOKEN": service_token,
801
        "proxy": env.env.pithos.hostname == env.env.accounts.hostname
802
        }
803
    custom = customize_settings_from_tmpl(tmpl, replace)
804
    put(custom, tmpl, mode=0644)
805
    try_run("/etc/init.d/gunicorn restart")
806

    
807
    install_package("snf-pithos-webclient")
808
    tmpl = "/etc/synnefo/webclient.conf"
809
    replace = {
810
        "ACCOUNTS": env.env.accounts.fqdn,
811
        "PITHOS_UI_CLOUDBAR_ACTIVE_SERVICE": service_id,
812
        }
813
    custom = customize_settings_from_tmpl(tmpl, replace)
814
    put(custom, tmpl, mode=0644)
815

    
816
    try_run("/etc/init.d/gunicorn restart")
817
    #TOFIX: the previous command lets pithos-backend create blocks and maps
818
    #       with root owner
819
    try_run("chown -R www-data:www-data %s/data " % env.env.pithos_dir)
820
    #try_run("pithos-migrate stamp 4c8ccdc58192")
821
    #try_run("pithos-migrate upgrade head")
822

    
823

    
824
def add_wheezy():
825
    tmpl = "/etc/apt/sources.list.d/wheezy.list"
826
    replace = {}
827
    custom = customize_settings_from_tmpl(tmpl, replace)
828
    put(custom, tmpl)
829
    apt_get_update()
830

    
831

    
832
def remove_wheezy():
833
    try_run("rm -f /etc/apt/sources.list.d/wheezy.list")
834
    apt_get_update()
835

    
836

    
837
@roles("ganeti")
838
def setup_ganeti():
839
    debug(env.host, "Setting up snf-ganeti...")
840
    node_info = env.env.ips_info[env.host]
841
    with settings(hide("everything")):
842
        #if env.enable_lvm:
843
        #    try_run("vgs " + env.env.vg)
844
        try_run("getent hosts " + env.env.cluster.fqdn)
845
        try_run("getent hosts %s | grep -v ^127" % env.host)
846
        try_run("hostname -f | grep " + node_info.fqdn)
847
        #try_run("ip link show " + env.env.common_bridge)
848
        #try_run("ip link show " + env.env.common_bridge)
849
        #try_run("apt-get update")
850
    install_package("qemu-kvm")
851
    install_package("python-bitarray")
852
    add_wheezy()
853
    install_package("ganeti-htools")
854
    remove_wheezy()
855
    install_package("snf-ganeti")
856
    try_run("mkdir -p /srv/ganeti/file-storage/")
857
    cmd = """
858
cat <<EOF > /etc/ganeti/file-storage-paths
859
/srv/ganeti/file-storage
860
/srv/ganeti/shared-file-storage
861
EOF
862
"""
863
    try_run(cmd)
864

    
865

    
866
@roles("master")
867
def add_rapi_user():
868
    debug(env.host, " * Adding RAPI user to Ganeti backend...")
869
    cmd = """
870
    echo -n "{0}:Ganeti Remote API:{1}" | openssl md5
871
    """.format(env.env.synnefo_user, env.env.synnefo_rapi_passwd)
872
    result = try_run(cmd)
873
    cmd = """
874
    echo "{0} {1}{2} write" >> /var/lib/ganeti/rapi/users
875
    """.format(env.env.synnefo_user, '{ha1}',result)
876
    try_run(cmd)
877
    try_run("/etc/init.d/ganeti restart")
878

    
879
@roles("master")
880
def add_nodes():
881
    nodes = env.env.cluster_nodes.split(",")
882
    nodes.remove(env.env.master_node)
883
    debug(env.host, " * Adding nodes to Ganeti backend...")
884
    for n in nodes:
885
        add_node(n)
886

    
887
@roles("master")
888
def add_node(node):
889
    node_info = env.env.nodes_info[node]
890
    debug(env.host, " * Adding node %s to Ganeti backend..." % node_info.fqdn)
891
    cmd = "gnt-node add --no-ssh-key-check --master-capable=yes --vm-capable=yes " + node_info.fqdn
892
    try_run(cmd)
893

    
894
@roles("ganeti")
895
def enable_drbd():
896
    if env.enable_drbd:
897
        debug(env.host, " * Enabling DRBD...")
898
        try_run("modprobe drbd minor_count=255 usermode_helper=/bin/true")
899
        try_run("echo drbd minor_count=255 usermode_helper=/bin/true >> /etc/modules")
900

    
901
@roles("master")
902
def setup_drbd_dparams():
903
    if env.enable_drbd:
904
        debug(env.host, " * Twicking drbd related disk parameters in Ganeti...")
905
        cmd = """
906
        gnt-cluster modify --disk-parameters=drbd:metavg={0}
907
        gnt-group modify --disk-parameters=drbd:metavg={0} default
908
        """.format(env.env.vg)
909
        try_run(cmd)
910

    
911
@roles("master")
912
def enable_lvm():
913
    if env.enable_lvm:
914
        debug(env.host, " * Enabling LVM...")
915
        cmd = """
916
        gnt-cluster modify --vg-name={0}
917
        """.format(env.env.vg)
918
        try_run(cmd)
919
    else:
920
        debug(env.host, " * Disabling LVM...")
921
        try_run("gnt-cluster modify --no-lvm-storage")
922

    
923
@roles("master")
924
def destroy_cluster():
925
    debug(env.host, " * Destroying Ganeti cluster...")
926
    #TODO: remove instances first
927
    allnodes = env.env.cluster_hostnames[:]
928
    allnodes.remove(env.host)
929
    for n in allnodes:
930
      host_info = env.env.ips_info[host]
931
      debug(env.host, " * Removing node %s..." % n)
932
      cmd = "gnt-node remove  " + host_info.fqdn
933
      try_run(cmd)
934
    try_run("gnt-cluster destroy --yes-do-it")
935

    
936

    
937
@roles("master")
938
def init_cluster():
939
    debug(env.host, " * Initializing Ganeti backend...")
940
    # extra = ""
941
    # if env.enable_lvm:
942
    #     extra += " --vg-name={0} ".format(env.env.vg)
943
    # else:
944
    #     extra += " --no-lvm-storage "
945
    # if not env.enable_drbd:
946
    #     extra += " --no-drbd-storage "
947
    extra = " --no-lvm-storage --no-drbd-storage "
948
    cmd = """
949
    gnt-cluster init --enabled-hypervisors=kvm \
950
                     {0} \
951
                     --nic-parameters link={1},mode=bridged \
952
                     --master-netdev {2} \
953
                     --default-iallocator hail \
954
                     --hypervisor-parameters kvm:kernel_path=,vnc_bind_address=0.0.0.0 \
955
                     --no-ssh-init --no-etc-hosts \
956
                    {3}
957

958
    """.format(extra, env.env.common_bridge,
959
               env.env.cluster_netdev, env.env.cluster.fqdn)
960
    try_run(cmd)
961

    
962

    
963
@roles("ganeti")
964
def debootstrap():
965
    install_package("ganeti-instance-debootstrap")
966

    
967

    
968
@roles("ganeti")
969
def setup_image_host():
970
    debug(env.host, "Setting up snf-image...")
971
    install_package("snf-pithos-backend")
972
    install_package("snf-image")
973
    try_run("mkdir -p %s" % env.env.image_dir)
974
    tmpl = "/etc/default/snf-image"
975
    replace = {
976
        "synnefo_user": env.env.synnefo_user,
977
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
978
        "pithos_dir": env.env.pithos_dir,
979
        "db_node": env.env.db.ip,
980
    }
981
    custom = customize_settings_from_tmpl(tmpl, replace)
982
    put(custom, tmpl)
983

    
984

    
985
@roles("ganeti")
986
def setup_image_helper():
987
    debug(env.host, " * Updating helper image...")
988
    cmd = """
989
    snf-image-update-helper -y
990
    """
991
    try_run(cmd)
992

    
993

    
994
@roles("ganeti")
995
def setup_gtools():
996
    debug(env.host, " * Setting up snf-cyclades-gtools...")
997
    with settings(hide("everything")):
998
        try_run("ping -c1 " + env.env.mq.ip)
999
    setup_common()
1000
    install_package("snf-cyclades-gtools")
1001
    tmpl = "/etc/synnefo/gtools.conf"
1002
    replace = {
1003
        "synnefo_user": env.env.synnefo_user,
1004
        "synnefo_rabbitmq_passwd": env.env.synnefo_rabbitmq_passwd,
1005
        "mq_node": env.env.mq.ip,
1006
    }
1007
    custom = customize_settings_from_tmpl(tmpl, replace)
1008
    put(custom, tmpl)
1009

    
1010
    cmd = """
1011
    sed -i 's/false/true/' /etc/default/snf-ganeti-eventd
1012
    /etc/init.d/snf-ganeti-eventd start
1013
    """
1014
    try_run(cmd)
1015

    
1016

    
1017
@roles("ganeti")
1018
def setup_iptables():
1019
    debug(env.host, " * Setting up iptables to mangle DHCP requests...")
1020
    cmd = """
1021
    iptables -t mangle -A PREROUTING -i br+ -p udp -m udp --dport 67 -j NFQUEUE --queue-num 42
1022
    iptables -t mangle -A PREROUTING -i tap+ -p udp -m udp --dport 67 -j NFQUEUE --queue-num 42
1023
    iptables -t mangle -A PREROUTING -i prv+ -p udp -m udp --dport 67 -j NFQUEUE --queue-num 42
1024

1025
    ip6tables -t mangle -A PREROUTING -i br+ -p ipv6-icmp -m icmp6 --icmpv6-type 133 -j NFQUEUE --queue-num 43
1026
    ip6tables -t mangle -A PREROUTING -i br+ -p ipv6-icmp -m icmp6 --icmpv6-type 135 -j NFQUEUE --queue-num 44
1027
    """
1028
    try_run(cmd)
1029

    
1030
@roles("ganeti")
1031
def setup_network():
1032
    debug(env.host, "Setting up networking for Ganeti instances (nfdhcpd, etc.)...")
1033
    install_package("nfqueue-bindings-python")
1034
    install_package("nfdhcpd")
1035
    tmpl = "/etc/nfdhcpd/nfdhcpd.conf"
1036
    replace = {
1037
      "ns_node_ip": env.env.ns.ip
1038
      }
1039
    custom = customize_settings_from_tmpl(tmpl, replace)
1040
    put(custom, tmpl)
1041
    try_run("/etc/init.d/nfdhcpd restart")
1042

    
1043
    install_package("snf-network")
1044
    cmd = """
1045
    sed -i 's/MAC_MASK.*/MAC_MASK = ff:ff:f0:00:00:00/' /etc/default/snf-network
1046
    """
1047
    try_run(cmd)
1048

    
1049

    
1050
@roles("router")
1051
def setup_router():
1052
    debug(env.host, " * Setting up internal router for NAT...")
1053
    cmd = """
1054
    echo 1 > /proc/sys/net/ipv4/ip_forward
1055
    iptables -t nat -A POSTROUTING -s {0} -o {3} -j MASQUERADE
1056
    ip addr add {1} dev {2}
1057
    ip route add {0} dev {2} src {1}
1058
    """.format(env.env.synnefo_public_network_subnet,
1059
               env.env.synnefo_public_network_gateway,
1060
               env.env.common_bridge, env.env.public_iface)
1061
    try_run(cmd)
1062

    
1063

    
1064
@roles("cyclades")
1065
def cyclades_loaddata():
1066
    debug(env.host, " * Loading initial data for cyclades...")
1067
    try_run("snf-manage flavor-create %s %s %s %s" % (env.env.flavor_cpu,
1068
                                                      env.env.flavor_ram,
1069
                                                      env.env.flavor_disk,
1070
                                                      env.env.flavor_storage))
1071
    #run("snf-manage loaddata flavors")
1072

    
1073

    
1074
@roles("cyclades")
1075
def setup_cyclades():
1076
    debug(env.host, "Setting up snf-cyclades-app...")
1077
    with settings(hide("everything")):
1078
        try_run("ping -c1 accounts." + env.env.domain)
1079
        try_run("ping -c1 " + env.env.db.ip)
1080
        try_run("ping -c1 " + env.env.mq.ip)
1081
    setup_gunicorn()
1082
    setup_apache()
1083
    setup_webproject()
1084
    install_package("memcached")
1085
    install_package("python-memcache")
1086
    install_package("snf-pithos-backend")
1087
    install_package("kamaki")
1088
    install_package("snf-cyclades-app")
1089
    install_package("python-django-south")
1090
    tmpl = "/etc/synnefo/cyclades.conf"
1091

    
1092
    with settings(host_string=env.env.accounts.ip):
1093
        service_id, service_token = get_service_details("cyclades")
1094

    
1095
    replace = {
1096
        "ACCOUNTS": env.env.accounts.fqdn,
1097
        "CYCLADES": env.env.cyclades.fqdn,
1098
        "mq_node": env.env.mq.ip,
1099
        "db_node": env.env.db.ip,
1100
        "synnefo_user": env.env.synnefo_user,
1101
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
1102
        "synnefo_rabbitmq_passwd": env.env.synnefo_rabbitmq_passwd,
1103
        "pithos_dir": env.env.pithos_dir,
1104
        "common_bridge": env.env.common_bridge,
1105
        "HOST": env.env.cyclades.ip,
1106
        "domain": env.env.domain,
1107
        "CYCLADES_SERVICE_TOKEN": service_token,
1108
        "proxy": env.env.cyclades.hostname == env.env.accounts.hostname
1109
        }
1110
    custom = customize_settings_from_tmpl(tmpl, replace)
1111
    put(custom, tmpl, mode=0644)
1112
    try_run("/etc/init.d/gunicorn restart")
1113

    
1114
    cmd = """
1115
    sed -i 's/false/true/' /etc/default/snf-dispatcher
1116
    /etc/init.d/snf-dispatcher start
1117
    """
1118
    try_run(cmd)
1119

    
1120
    try_run("snf-manage syncdb")
1121
    try_run("snf-manage migrate --delete-ghost-migrations")
1122

    
1123

    
1124
@roles("cyclades")
1125
def get_backend_id(cluster_name="ganeti1.synnefo.deploy.local"):
1126
    backend_id = try_run("snf-manage backend-list 2>/dev/null | grep %s | awk '{print $1}'" % cluster_name)
1127
    return backend_id
1128

    
1129

    
1130
@roles("cyclades")
1131
def add_backend():
1132
    debug(env.host, "adding %s ganeti backend to cyclades..." % env.env.cluster.fqdn)
1133
    with settings(hide("everything")):
1134
        try_run("ping -c1 " + env.env.cluster.fqdn)
1135
    cmd = """
1136
    snf-manage backend-add --clustername={0} --user={1} --pass={2}
1137
    """.format(env.env.cluster.fqdn, env.env.synnefo_user,
1138
               env.env.synnefo_rapi_passwd)
1139
    try_run(cmd)
1140
    backend_id = get_backend_id(env.env.cluster.fqdn)
1141
    try_run("snf-manage backend-modify --drained=False " + backend_id)
1142

    
1143
@roles("cyclades")
1144
def pin_user_to_backend(user_email):
1145
    backend_id = get_backend_id(env.env.cluster.fqdn)
1146
    # pin user to backend
1147
    cmd = """
1148
cat <<EOF >> /etc/synnefo/cyclades.conf
1149

1150
BACKEND_PER_USER = {
1151
  '%s': %s,
1152
}
1153

1154
EOF
1155
/etc/init.d/gunicorn restart
1156
    """  % (user_email, backend_id)
1157
    try_run(cmd)
1158

    
1159
@roles("cyclades")
1160
def add_pools():
1161
    debug(env.host, " * Creating pools of resources (brigdes, mac prefixes) in cyclades...")
1162
    try_run("snf-manage pool-create --type=mac-prefix --base=aa:00:0 --size=65536")
1163
    try_run("snf-manage pool-create --type=bridge --base=prv --size=20")
1164

    
1165

    
1166
@roles("accounts", "cyclades", "pithos")
1167
def export_services():
1168
    debug(env.host, " * Exporting services...")
1169
    host = env.host
1170
    services = []
1171
    if host == env.env.cyclades.ip:
1172
        services.append("cyclades")
1173
    if host == env.env.pithos.ip:
1174
        services.append("pithos")
1175
    if host == env.env.accounts.ip:
1176
        services.append("astakos")
1177
    for service in services:
1178
        filename = "%s_services.json" % service
1179
        cmd = "snf-manage service-export-%s > %s" % (service, filename)
1180
        run(cmd)
1181
        get(filename, filename+".local")
1182

    
1183

    
1184
@roles("accounts")
1185
def import_services():
1186
    debug(env.host, " * Registering services to astakos...")
1187
    for service in ["cyclades", "pithos", "astakos"]:
1188
        filename = "%s_services.json" % service
1189
        put(filename +".local", filename)
1190
        cmd = "snf-manage service-import --json=%s" % filename
1191
        run(cmd)
1192

    
1193
    debug(env.host, " * Setting default quota...")
1194
    cmd = """
1195
    snf-manage resource-modify --limit 40G pithos.diskspace
1196
    snf-manage resource-modify --limit 2 astakos.pending_app
1197
    snf-manage resource-modify --limit 4 cyclades.vm
1198
    snf-manage resource-modify --limit 40G cyclades.disk
1199
    snf-manage resource-modify --limit 16G cyclades.ram
1200
    snf-manage resource-modify --limit 8G cyclades.active_ram
1201
    snf-manage resource-modify --limit 32 cyclades.cpu
1202
    snf-manage resource-modify --limit 16 cyclades.active_cpu
1203
    snf-manage resource-modify --limit 4 cyclades.network.private
1204
    """
1205
    try_run(cmd)
1206

    
1207

    
1208
@roles("cyclades")
1209
def add_network():
1210
    debug(env.host, " * Adding public network in cyclades...")
1211
    backend_id = get_backend_id(env.env.cluster.fqdn)
1212
    cmd = """
1213
    snf-manage network-create --subnet={0} --gateway={1} --public --dhcp --flavor={2} --mode=bridged --link={3} --name=Internet --backend-id={4}
1214
    """.format(env.env.synnefo_public_network_subnet,
1215
               env.env.synnefo_public_network_gateway,
1216
               env.env.synnefo_public_network_type,
1217
               env.env.common_bridge, backend_id)
1218
    try_run(cmd)
1219

    
1220

    
1221
@roles("cyclades")
1222
def setup_vncauthproxy():
1223
    debug(env.host, " * Setting up vncauthproxy...")
1224
    install_package("snf-vncauthproxy")
1225
    cmd = """
1226
    echo CHUID="www-data:nogroup" >> /etc/default/vncauthproxy
1227
    rm /var/log/vncauthproxy/vncauthproxy.log
1228
    """
1229
    try_run(cmd)
1230
    try_run("/etc/init.d/vncauthproxy restart")
1231

    
1232
@roles("client")
1233
def setup_kamaki():
1234
    debug(env.host, "Setting up kamaki client...")
1235
    with settings(hide("everything")):
1236
        try_run("ping -c1 accounts." + env.env.domain)
1237
        try_run("ping -c1 cyclades." + env.env.domain)
1238
        try_run("ping -c1 pithos." + env.env.domain)
1239

    
1240
    with settings(host_string=env.env.db.ip):
1241
        uid, user_auth_token, user_uuid = get_auth_token_from_db(env.env.user_email)
1242

    
1243
    install_package("python-progress")
1244
    install_package("kamaki")
1245
    cmd = """
1246
    kamaki config set cloud.default.url "https://{0}/astakos/identity/v2.0/"
1247
    kamaki config set cloud.default.token {1}
1248
    """.format(env.env.accounts.fqdn, user_auth_token)
1249
    try_run(cmd)
1250
    try_run("kamaki file create images")
1251

    
1252
@roles("client")
1253
def upload_image(image="debian_base.diskdump"):
1254
    debug(env.host, " * Uploading initial image to pithos...")
1255
    image = "debian_base.diskdump"
1256
    try_run("wget {0} -O /tmp/{1}".format(env.env.debian_base_url, image))
1257
    try_run("kamaki file upload --container images /tmp/{0} {0}".format(image))
1258

    
1259
@roles("client")
1260
def register_image(image="debian_base.diskdump"):
1261
    debug(env.host, " * Register image to plankton...")
1262
    with settings(host_string=env.env.db.ip):
1263
        uid, user_auth_token, user_uuid = get_auth_token_from_db(env.env.user_email)
1264

    
1265
    image_location = "images:{0}".format(image)
1266
    cmd = """
1267
    sleep 5
1268
    kamaki image register "Debian Base" {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
1269
    """.format(image_location)
1270
    try_run(cmd)
1271

    
1272
@roles("client")
1273
def setup_burnin():
1274
    debug(env.host, "Setting up burnin testing tool...")
1275
    install_package("kamaki")
1276
    install_package("snf-tools")
1277

    
1278
@roles("pithos")
1279
def add_image_locally():
1280
    debug(env.host, " * Getting image locally in order snf-image to use it directly..")
1281
    image = "debian_base.diskdump"
1282
    try_run("wget {0} -O {1}/{2}".format(env.env.debian_base_url, env.env.image_dir, image))
1283

    
1284

    
1285
@roles("master")
1286
def gnt_instance_add(name="test"):
1287
    debug(env.host, " * Adding test instance to Ganeti...")
1288
    osp="""img_passwd=gamwtosecurity,img_format=diskdump,img_id=debian_base,img_properties='{"OSFAMILY":"linux"\,"ROOT_PARTITION":"1"}'"""
1289
    cmd = """
1290
    gnt-instance add  -o snf-image+default --os-parameters {0} -t plain --disk 0:size=1G --no-name-check --no-ip-check --net 0:ip=pool,network=test --no-install --hypervisor-parameters kvm:machine_version=pc-1.0 {1}
1291
    """.format(osp, name)
1292
    try_run(cmd)
1293

    
1294
@roles("master")
1295
def gnt_network_add(name="test", subnet="10.0.0.0/26", gw="10.0.0.1", mode="bridged", link="br0"):
1296
    debug(env.host, " * Adding test network to Ganeti...")
1297
    cmd = """
1298
    gnt-network add --network={1} --gateway={2} {0}
1299
    gnt-network connect {0} {3} {4}
1300
    """.format(name, subnet, gw, mode, link)
1301
    try_run(cmd)
1302

    
1303
@roles("ips")
1304
def test():
1305
    debug(env.host, "Testing...")
1306
    try_run("hostname && date")