Statistics
| Branch: | Tag: | Revision:

root / snf-deploy / fabfile.py @ e083e94c

History | View | Annotate | Download (39.9 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
    env.env.update_packages(host_info.os)
81
    if ast.literal_eval(env.env.use_local_packages):
82
        with settings(warn_only=True):
83
            deb = local("ls %s/%s*%s_all.deb" % (env.env.packages, package, host_info.os))
84
            if deb:
85
                debug(env.host, " * Package %s found in %s..." % (package, env.env.packages))
86
                put(deb, "/tmp/")
87
                try_run("dpkg -i /tmp/%s || " % os.path.basename(deb) + APT_GET + "-f")
88
                try_run("rm /tmp/%s" % os.path.basename(deb))
89
                return
90

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

    
99
    try_run(APT_GET)
100

    
101
    return
102

    
103

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

    
111

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

    
119

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

    
128

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

    
137

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

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

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

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

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

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

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

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

    
201

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

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

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

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

    
222

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

    
229

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

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

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

    
281

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

    
294

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

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

    
314

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

    
322

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

    
329

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

    
341

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

    
351
    return custom
352

    
353

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

    
373

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

    
380

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

    
390

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

    
417

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

    
431

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

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

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

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

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

    
472

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

    
478

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

    
501

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

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

    
529

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

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

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

    
560

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

    
579

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

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

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

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

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

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

    
633

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

    
638

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

    
648

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

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

    
665
    return (uid, user_auth_token, user_uuid)
666

    
667

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

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

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

    
691

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

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

    
713

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

    
720

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

    
733

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

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

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

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

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

    
774

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

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

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

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

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

    
822

    
823
@roles("ganeti")
824
def setup_ganeti():
825
    debug(env.host, "Setting up snf-ganeti...")
826
    node_info = env.env.ips_info[env.host]
827
    with settings(hide("everything")):
828
        #if env.enable_lvm:
829
        #    try_run("vgs " + env.env.vg)
830
        try_run("getent hosts " + env.env.cluster.fqdn)
831
        try_run("getent hosts %s | grep -v ^127" % env.host)
832
        try_run("hostname -f | grep " + node_info.fqdn)
833
        #try_run("ip link show " + env.env.common_bridge)
834
        #try_run("ip link show " + env.env.common_bridge)
835
        #try_run("apt-get update")
836
    install_package("qemu-kvm")
837
    install_package("python-bitarray")
838
    install_package("ganeti-htools")
839
    install_package("snf-ganeti")
840
    try_run("mkdir -p /srv/ganeti/file-storage/")
841
    cmd = """
842
cat <<EOF > /etc/ganeti/file-storage-paths
843
/srv/ganeti/file-storage
844
/srv/ganeti/shared-file-storage
845
EOF
846
"""
847
    try_run(cmd)
848

    
849

    
850
@roles("master")
851
def add_rapi_user():
852
    debug(env.host, " * Adding RAPI user to Ganeti backend...")
853
    cmd = """
854
    echo -n "{0}:Ganeti Remote API:{1}" | openssl md5
855
    """.format(env.env.synnefo_user, env.env.synnefo_rapi_passwd)
856
    result = try_run(cmd)
857
    cmd = """
858
    echo "{0} {1}{2} write" >> /var/lib/ganeti/rapi/users
859
    """.format(env.env.synnefo_user, '{ha1}',result)
860
    try_run(cmd)
861
    try_run("/etc/init.d/ganeti restart")
862

    
863
@roles("master")
864
def add_nodes():
865
    nodes = env.env.cluster_nodes.split(",")
866
    nodes.remove(env.env.master_node)
867
    debug(env.host, " * Adding nodes to Ganeti backend...")
868
    for n in nodes:
869
        add_node(n)
870

    
871
@roles("master")
872
def add_node(node):
873
    node_info = env.env.nodes_info[node]
874
    debug(env.host, " * Adding node %s to Ganeti backend..." % node_info.fqdn)
875
    cmd = "gnt-node add --no-ssh-key-check --master-capable=yes --vm-capable=yes " + node_info.fqdn
876
    try_run(cmd)
877

    
878
@roles("ganeti")
879
def enable_drbd():
880
    if env.enable_drbd:
881
        debug(env.host, " * Enabling DRBD...")
882
        try_run("modprobe drbd minor_count=255 usermode_helper=/bin/true")
883
        try_run("echo drbd minor_count=255 usermode_helper=/bin/true >> /etc/modules")
884

    
885
@roles("master")
886
def setup_drbd_dparams():
887
    if env.enable_drbd:
888
        debug(env.host, " * Twicking drbd related disk parameters in Ganeti...")
889
        cmd = """
890
        gnt-cluster modify --disk-parameters=drbd:metavg={0}
891
        gnt-group modify --disk-parameters=drbd:metavg={0} default
892
        """.format(env.env.vg)
893
        try_run(cmd)
894

    
895
@roles("master")
896
def enable_lvm():
897
    if env.enable_lvm:
898
        debug(env.host, " * Enabling LVM...")
899
        cmd = """
900
        gnt-cluster modify --vg-name={0}
901
        """.format(env.env.vg)
902
        try_run(cmd)
903
    else:
904
        debug(env.host, " * Disabling LVM...")
905
        try_run("gnt-cluster modify --no-lvm-storage")
906

    
907
@roles("master")
908
def destroy_cluster():
909
    debug(env.host, " * Destroying Ganeti cluster...")
910
    #TODO: remove instances first
911
    allnodes = env.env.cluster_hostnames[:]
912
    allnodes.remove(env.host)
913
    for n in allnodes:
914
      host_info = env.env.ips_info[host]
915
      debug(env.host, " * Removing node %s..." % n)
916
      cmd = "gnt-node remove  " + host_info.fqdn
917
      try_run(cmd)
918
    try_run("gnt-cluster destroy --yes-do-it")
919

    
920

    
921
@roles("master")
922
def init_cluster():
923
    debug(env.host, " * Initializing Ganeti backend...")
924
    # extra = ""
925
    # if env.enable_lvm:
926
    #     extra += " --vg-name={0} ".format(env.env.vg)
927
    # else:
928
    #     extra += " --no-lvm-storage "
929
    # if not env.enable_drbd:
930
    #     extra += " --no-drbd-storage "
931
    extra = " --no-lvm-storage --no-drbd-storage "
932
    cmd = """
933
    gnt-cluster init --enabled-hypervisors=kvm \
934
                     {0} \
935
                     --nic-parameters link={1},mode=bridged \
936
                     --master-netdev {2} \
937
                     --default-iallocator hail \
938
                     --hypervisor-parameters kvm:kernel_path=,vnc_bind_address=0.0.0.0 \
939
                     --no-ssh-init --no-etc-hosts \
940
                    {3}
941

942
    """.format(extra, env.env.common_bridge,
943
               env.env.cluster_netdev, env.env.cluster.fqdn)
944
    try_run(cmd)
945

    
946

    
947
@roles("ganeti")
948
def debootstrap():
949
    install_package("ganeti-instance-debootstrap")
950

    
951

    
952
@roles("ganeti")
953
def setup_image_host():
954
    debug(env.host, "Setting up snf-image...")
955
    install_package("snf-pithos-backend")
956
    install_package("snf-image")
957
    try_run("mkdir -p %s" % env.env.image_dir)
958
    tmpl = "/etc/default/snf-image"
959
    replace = {
960
        "synnefo_user": env.env.synnefo_user,
961
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
962
        "pithos_dir": env.env.pithos_dir,
963
        "db_node": env.env.db.ip,
964
    }
965
    custom = customize_settings_from_tmpl(tmpl, replace)
966
    put(custom, tmpl)
967

    
968

    
969
@roles("ganeti")
970
def setup_image_helper():
971
    debug(env.host, " * Updating helper image...")
972
    cmd = """
973
    snf-image-update-helper -y
974
    """
975
    try_run(cmd)
976

    
977

    
978
@roles("ganeti")
979
def setup_gtools():
980
    debug(env.host, " * Setting up snf-cyclades-gtools...")
981
    with settings(hide("everything")):
982
        try_run("ping -c1 " + env.env.mq.ip)
983
    setup_common()
984
    install_package("snf-cyclades-gtools")
985
    tmpl = "/etc/synnefo/gtools.conf"
986
    replace = {
987
        "synnefo_user": env.env.synnefo_user,
988
        "synnefo_rabbitmq_passwd": env.env.synnefo_rabbitmq_passwd,
989
        "mq_node": env.env.mq.ip,
990
    }
991
    custom = customize_settings_from_tmpl(tmpl, replace)
992
    put(custom, tmpl)
993

    
994
    cmd = """
995
    sed -i 's/false/true/' /etc/default/snf-ganeti-eventd
996
    /etc/init.d/snf-ganeti-eventd start
997
    """
998
    try_run(cmd)
999

    
1000

    
1001
@roles("ganeti")
1002
def setup_iptables():
1003
    debug(env.host, " * Setting up iptables to mangle DHCP requests...")
1004
    cmd = """
1005
    iptables -t mangle -A PREROUTING -i br+ -p udp -m udp --dport 67 -j NFQUEUE --queue-num 42
1006
    iptables -t mangle -A PREROUTING -i tap+ -p udp -m udp --dport 67 -j NFQUEUE --queue-num 42
1007
    iptables -t mangle -A PREROUTING -i prv+ -p udp -m udp --dport 67 -j NFQUEUE --queue-num 42
1008

1009
    ip6tables -t mangle -A PREROUTING -i br+ -p ipv6-icmp -m icmp6 --icmpv6-type 133 -j NFQUEUE --queue-num 43
1010
    ip6tables -t mangle -A PREROUTING -i br+ -p ipv6-icmp -m icmp6 --icmpv6-type 135 -j NFQUEUE --queue-num 44
1011
    """
1012
    try_run(cmd)
1013

    
1014
@roles("ganeti")
1015
def setup_network():
1016
    debug(env.host, "Setting up networking for Ganeti instances (nfdhcpd, etc.)...")
1017
    install_package("nfqueue-bindings-python")
1018
    install_package("nfdhcpd")
1019
    tmpl = "/etc/nfdhcpd/nfdhcpd.conf"
1020
    replace = {
1021
      "ns_node_ip": env.env.ns.ip
1022
      }
1023
    custom = customize_settings_from_tmpl(tmpl, replace)
1024
    put(custom, tmpl)
1025
    try_run("/etc/init.d/nfdhcpd restart")
1026

    
1027
    install_package("snf-network")
1028
    cmd = """
1029
    sed -i 's/MAC_MASK.*/MAC_MASK = ff:ff:f0:00:00:00/' /etc/default/snf-network
1030
    """
1031
    try_run(cmd)
1032

    
1033

    
1034
@roles("router")
1035
def setup_router():
1036
    debug(env.host, " * Setting up internal router for NAT...")
1037
    cmd = """
1038
    echo 1 > /proc/sys/net/ipv4/ip_forward
1039
    iptables -t nat -A POSTROUTING -s {0} -o {3} -j MASQUERADE
1040
    ip addr add {1} dev {2}
1041
    ip route add {0} dev {2} src {1}
1042
    """.format(env.env.synnefo_public_network_subnet,
1043
               env.env.synnefo_public_network_gateway,
1044
               env.env.common_bridge, env.env.public_iface)
1045
    try_run(cmd)
1046

    
1047

    
1048
@roles("cyclades")
1049
def cyclades_loaddata():
1050
    debug(env.host, " * Loading initial data for cyclades...")
1051
    try_run("snf-manage flavor-create %s %s %s %s" % (env.env.flavor_cpu,
1052
                                                      env.env.flavor_ram,
1053
                                                      env.env.flavor_disk,
1054
                                                      env.env.flavor_storage))
1055
    #run("snf-manage loaddata flavors")
1056

    
1057

    
1058
@roles("cyclades")
1059
def setup_cyclades():
1060
    debug(env.host, "Setting up snf-cyclades-app...")
1061
    with settings(hide("everything")):
1062
        try_run("ping -c1 accounts." + env.env.domain)
1063
        try_run("ping -c1 " + env.env.db.ip)
1064
        try_run("ping -c1 " + env.env.mq.ip)
1065
    setup_gunicorn()
1066
    setup_apache()
1067
    setup_webproject()
1068
    install_package("memcached")
1069
    install_package("python-memcache")
1070
    install_package("snf-pithos-backend")
1071
    install_package("kamaki")
1072
    install_package("snf-cyclades-app")
1073
    install_package("python-django-south")
1074
    tmpl = "/etc/synnefo/cyclades.conf"
1075

    
1076
    with settings(host_string=env.env.accounts.ip):
1077
        service_id, service_token = get_service_details("cyclades")
1078

    
1079
    replace = {
1080
        "ACCOUNTS": env.env.accounts.fqdn,
1081
        "CYCLADES": env.env.cyclades.fqdn,
1082
        "mq_node": env.env.mq.ip,
1083
        "db_node": env.env.db.ip,
1084
        "synnefo_user": env.env.synnefo_user,
1085
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
1086
        "synnefo_rabbitmq_passwd": env.env.synnefo_rabbitmq_passwd,
1087
        "pithos_dir": env.env.pithos_dir,
1088
        "common_bridge": env.env.common_bridge,
1089
        "HOST": env.env.cyclades.ip,
1090
        "domain": env.env.domain,
1091
        "CYCLADES_SERVICE_TOKEN": service_token,
1092
        "proxy": env.env.cyclades.hostname == env.env.accounts.hostname
1093
        }
1094
    custom = customize_settings_from_tmpl(tmpl, replace)
1095
    put(custom, tmpl, mode=0644)
1096
    try_run("/etc/init.d/gunicorn restart")
1097

    
1098
    cmd = """
1099
    sed -i 's/false/true/' /etc/default/snf-dispatcher
1100
    /etc/init.d/snf-dispatcher start
1101
    """
1102
    try_run(cmd)
1103

    
1104
    try_run("snf-manage syncdb")
1105
    try_run("snf-manage migrate --delete-ghost-migrations")
1106

    
1107

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

    
1113

    
1114
@roles("cyclades")
1115
def add_backend():
1116
    debug(env.host, "adding %s ganeti backend to cyclades..." % env.env.cluster.fqdn)
1117
    with settings(hide("everything")):
1118
        try_run("ping -c1 " + env.env.cluster.fqdn)
1119
    cmd = """
1120
    snf-manage backend-add --clustername={0} --user={1} --pass={2}
1121
    """.format(env.env.cluster.fqdn, env.env.synnefo_user,
1122
               env.env.synnefo_rapi_passwd)
1123
    try_run(cmd)
1124
    backend_id = get_backend_id(env.env.cluster.fqdn)
1125
    try_run("snf-manage backend-modify --drained=False " + backend_id)
1126

    
1127
@roles("cyclades")
1128
def pin_user_to_backend(user_email):
1129
    backend_id = get_backend_id(env.env.cluster.fqdn)
1130
    # pin user to backend
1131
    cmd = """
1132
cat <<EOF >> /etc/synnefo/cyclades.conf
1133

1134
BACKEND_PER_USER = {
1135
  '%s': %s,
1136
}
1137

1138
EOF
1139
/etc/init.d/gunicorn restart
1140
    """  % (user_email, backend_id)
1141
    try_run(cmd)
1142

    
1143
@roles("cyclades")
1144
def add_pools():
1145
    debug(env.host, " * Creating pools of resources (brigdes, mac prefixes) in cyclades...")
1146
    try_run("snf-manage pool-create --type=mac-prefix --base=aa:00:0 --size=65536")
1147
    try_run("snf-manage pool-create --type=bridge --base=prv --size=20")
1148

    
1149

    
1150
@roles("accounts", "cyclades", "pithos")
1151
def export_services():
1152
    debug(env.host, " * Exporting services...")
1153
    host = env.host
1154
    services = []
1155
    if host == env.env.cyclades.ip:
1156
        services.append("cyclades")
1157
    if host == env.env.pithos.ip:
1158
        services.append("pithos")
1159
    if host == env.env.accounts.ip:
1160
        services.append("astakos")
1161
    for service in services:
1162
        filename = "%s_services.json" % service
1163
        cmd = "snf-manage service-export-%s > %s" % (service, filename)
1164
        run(cmd)
1165
        get(filename, filename+".local")
1166

    
1167

    
1168
@roles("accounts")
1169
def import_services():
1170
    debug(env.host, " * Registering services to astakos...")
1171
    for service in ["cyclades", "pithos", "astakos"]:
1172
        filename = "%s_services.json" % service
1173
        put(filename +".local", filename)
1174
        cmd = "snf-manage service-import --json=%s" % filename
1175
        run(cmd)
1176

    
1177
    debug(env.host, " * Setting default quota...")
1178
    cmd = """
1179
    snf-manage resource-modify --limit 40G pithos.diskspace
1180
    snf-manage resource-modify --limit 2 astakos.pending_app
1181
    snf-manage resource-modify --limit 4 cyclades.vm
1182
    snf-manage resource-modify --limit 40G cyclades.disk
1183
    snf-manage resource-modify --limit 16G cyclades.ram
1184
    snf-manage resource-modify --limit 8G cyclades.active_ram
1185
    snf-manage resource-modify --limit 32 cyclades.cpu
1186
    snf-manage resource-modify --limit 16 cyclades.active_cpu
1187
    snf-manage resource-modify --limit 4 cyclades.network.private
1188
    """
1189
    try_run(cmd)
1190

    
1191

    
1192
@roles("cyclades")
1193
def add_network():
1194
    debug(env.host, " * Adding public network in cyclades...")
1195
    backend_id = get_backend_id(env.env.cluster.fqdn)
1196
    cmd = """
1197
    snf-manage network-create --subnet={0} --gateway={1} --public --dhcp --flavor={2} --mode=bridged --link={3} --name=Internet --backend-id={4}
1198
    """.format(env.env.synnefo_public_network_subnet,
1199
               env.env.synnefo_public_network_gateway,
1200
               env.env.synnefo_public_network_type,
1201
               env.env.common_bridge, backend_id)
1202
    try_run(cmd)
1203

    
1204

    
1205
@roles("cyclades")
1206
def setup_vncauthproxy():
1207
    debug(env.host, " * Setting up vncauthproxy...")
1208
    install_package("snf-vncauthproxy")
1209
    cmd = """
1210
    echo CHUID="www-data:nogroup" >> /etc/default/vncauthproxy
1211
    rm /var/log/vncauthproxy/vncauthproxy.log
1212
    """
1213
    try_run(cmd)
1214
    try_run("/etc/init.d/vncauthproxy restart")
1215

    
1216
@roles("client")
1217
def setup_kamaki():
1218
    debug(env.host, "Setting up kamaki client...")
1219
    with settings(hide("everything")):
1220
        try_run("ping -c1 accounts." + env.env.domain)
1221
        try_run("ping -c1 cyclades." + env.env.domain)
1222
        try_run("ping -c1 pithos." + env.env.domain)
1223

    
1224
    with settings(host_string=env.env.db.ip):
1225
        uid, user_auth_token, user_uuid = get_auth_token_from_db(env.env.user_email)
1226

    
1227
    install_package("python-progress")
1228
    install_package("kamaki")
1229
    cmd = """
1230
    kamaki config set cloud.default.url "https://{0}/astakos/identity/v2.0/"
1231
    kamaki config set cloud.default.token {1}
1232
    """.format(env.env.accounts.fqdn, user_auth_token)
1233
    try_run(cmd)
1234
    try_run("kamaki file create images")
1235

    
1236
@roles("client")
1237
def upload_image(image="debian_base.diskdump"):
1238
    debug(env.host, " * Uploading initial image to pithos...")
1239
    image = "debian_base.diskdump"
1240
    try_run("wget {0} -O /tmp/{1}".format(env.env.debian_base_url, image))
1241
    try_run("kamaki file upload --container images /tmp/{0} {0}".format(image))
1242

    
1243
@roles("client")
1244
def register_image(image="debian_base.diskdump"):
1245
    debug(env.host, " * Register image to plankton...")
1246
    with settings(host_string=env.env.db.ip):
1247
        uid, user_auth_token, user_uuid = get_auth_token_from_db(env.env.user_email)
1248

    
1249
    image_location = "images:{0}".format(image)
1250
    cmd = """
1251
    sleep 5
1252
    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
1253
    """.format(image_location)
1254
    try_run(cmd)
1255

    
1256
@roles("client")
1257
def setup_burnin():
1258
    debug(env.host, "Setting up burnin testing tool...")
1259
    install_package("kamaki")
1260
    install_package("snf-tools")
1261

    
1262
@roles("pithos")
1263
def add_image_locally():
1264
    debug(env.host, " * Getting image locally in order snf-image to use it directly..")
1265
    image = "debian_base.diskdump"
1266
    try_run("wget {0} -O {1}/{2}".format(env.env.debian_base_url, env.env.image_dir, image))
1267

    
1268

    
1269
@roles("master")
1270
def gnt_instance_add(name="test"):
1271
    debug(env.host, " * Adding test instance to Ganeti...")
1272
    osp="""img_passwd=gamwtosecurity,img_format=diskdump,img_id=debian_base,img_properties='{"OSFAMILY":"linux"\,"ROOT_PARTITION":"1"}'"""
1273
    cmd = """
1274
    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}
1275
    """.format(osp, name)
1276
    try_run(cmd)
1277

    
1278
@roles("master")
1279
def gnt_network_add(name="test", subnet="10.0.0.0/26", gw="10.0.0.1", mode="bridged", link="br0"):
1280
    debug(env.host, " * Adding test network to Ganeti...")
1281
    cmd = """
1282
    gnt-network add --network={1} --gateway={2} {0}
1283
    gnt-network connect {0} {3} {4}
1284
    """.format(name, subnet, gw, mode, link)
1285
    try_run(cmd)
1286

    
1287
@roles("ips")
1288
def test():
1289
    debug(env.host, "Testing...")
1290
    try_run("hostname && date")