Statistics
| Branch: | Tag: | Revision:

root / snf-deploy / fabfile.py @ b249aba9

History | View | Annotate | Download (39.4 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
                        capture=True)
85
            if deb:
86
                debug(env.host, " * Package %s found in %s..." % (package, env.env.packages))
87
                put(deb, "/tmp/")
88
                try_run("dpkg -i /tmp/%s || " % os.path.basename(deb) + APT_GET + "-f")
89
                try_run("rm /tmp/%s" % os.path.basename(deb))
90
                return
91

    
92
    info = getattr(env.env, package)
93
    if info in ["squeeze-backports", "squeeze", "stable", "testing", "unstable", "wheezy"]:
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/g' /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
    pg_hba=$(ls /etc/postgresql/*/main/pg_hba.conf)
437
    echo host all {0} {1}/32 {2} >> $pg_hba
438
    """.format(user, ip, method)
439
    try_run(cmd)
440
    cmd = """
441
    pg_hba=$(ls /etc/postgresql/*/main/pg_hba.conf)
442
    sed -i 's/\(host.*127.0.0.1.*\)md5/\\1trust/' $pg_hba
443
    """
444
    try_run(cmd)
445
    try_run("/etc/init.d/postgresql restart")
446

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

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

    
467
    if env.env.testing_vm:
468
        cmd = """
469
        conf=$(ls /etc/postgresql/*/main/postgresql.conf)
470
        echo "fsync=off\nsynchronous_commit=off\nfull_page_writes=off" >> $conf
471
        """
472
        try_run(cmd)
473

    
474
    allow_access_in_db(env.host, "all", "trust")
475
    try_run("/etc/init.d/postgresql restart")
476

    
477

    
478
@roles("db")
479
def destroy_db():
480
    try_run("""su - postgres -c ' psql -w -c "drop database snf_apps" '""")
481
    try_run("""su - postgres -c ' psql -w -c "drop database snf_pithos" '""")
482

    
483

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

    
506

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

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

    
534

    
535
@roles("accounts")
536
def astakos_register_components():
537
    debug(env.host, " * Register services in astakos...")
538

    
539
    cyclades_base_url = "https://%s/cyclades/" % env.env.cyclades.fqdn
540
    pithos_base_url = "https://%s/pithos/" % env.env.pithos.fqdn
541
    astakos_base_url = "https://%s/astakos/" % env.env.accounts.fqdn
542

    
543
    cmd = """
544
    snf-manage component-add "home" https://{0} home-icon.png
545
    snf-manage component-add "cyclades" {1}ui/
546
    snf-manage component-add "pithos" {2}ui/
547
    snf-manage component-add "astakos" {3}ui/
548
    """.format(env.env.cms.fqdn, cyclades_base_url,
549
               pithos_base_url, astakos_base_url)
550
    try_run(cmd)
551

    
552

    
553
@roles("accounts")
554
def add_user():
555
    debug(env.host, " * adding user %s to astakos..." % env.env.user_email)
556
    email=env.env.user_email
557
    name=env.env.user_name
558
    lastname=env.env.user_lastname
559
    passwd=env.env.user_passwd
560
    cmd = """
561
    snf-manage user-add {0} {1} {2}
562
    """.format(email, name, lastname)
563
    try_run(cmd)
564
    with settings(host_string=env.env.db.ip):
565
        uid, user_auth_token, user_uuid = get_auth_token_from_db(email)
566
    cmd = """
567
    snf-manage user-modify --password {0} {1}
568
    """.format(passwd, uid)
569
    try_run(cmd)
570

    
571

    
572
@roles("accounts")
573
def activate_user(user_email=None):
574
    if not user_email:
575
      user_email = env.env.user_email
576
    debug(env.host, " * Activate user %s..." % user_email)
577
    with settings(host_string=env.env.db.ip):
578
        uid, user_auth_token, user_uuid = get_auth_token_from_db(user_email)
579

    
580
    cmd = """
581
    snf-manage user-modify --verify {0}
582
    snf-manage user-modify --accept {0}
583
    """.format(uid)
584
    try_run(cmd)
585

    
586
@roles("accounts")
587
def setup_astakos():
588
    debug(env.host, "Setting up snf-astakos-app...")
589
    setup_gunicorn()
590
    setup_apache()
591
    setup_webproject()
592
    install_package("python-django-south")
593
    install_package("snf-astakos-app")
594
    install_package("kamaki")
595

    
596
    tmpl = "/etc/synnefo/astakos.conf"
597
    replace = {
598
      "ACCOUNTS": env.env.accounts.fqdn,
599
      "domain": env.env.domain,
600
      "CYCLADES": env.env.cyclades.fqdn,
601
      "PITHOS": env.env.pithos.fqdn,
602
    }
603
    custom = customize_settings_from_tmpl(tmpl, replace)
604
    put(custom, tmpl, mode=0644)
605
    if env.csrf_disable:
606
      cmd = """
607
cat <<EOF >> /etc/synnefo/astakos.conf
608
try:
609
  MIDDLEWARE_CLASSES.remove('django.middleware.csrf.CsrfViewMiddleware')
610
except:
611
  pass
612
EOF
613
"""
614
      try_run(cmd)
615

    
616
    try_run("/etc/init.d/gunicorn restart")
617

    
618
    cmd = """
619
    snf-manage syncdb --noinput
620
    snf-manage migrate im --delete-ghost-migrations
621
    snf-manage migrate quotaholder_app
622
    """
623
    try_run(cmd)
624

    
625

    
626
@roles("accounts")
627
def get_service_details(service="pithos"):
628
    debug(env.host, " * Getting registered details for %s service..." % service)
629
    result = try_run("snf-manage component-list")
630
    r = re.compile(r".*%s.*" % service, re.M)
631
    service_id, _, _, service_token = r.search(result).group().split()
632
    # print("%s: %s %s" % (service, service_id, service_token))
633
    return (service_id, service_token)
634

    
635

    
636
@roles("db")
637
def get_auth_token_from_db(user_email=None):
638
    if not user_email:
639
        user_email=env.env.user_email
640
    debug(env.host, " * Getting authentication token and uuid for user %s..." % user_email)
641
    cmd = """
642
    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
643
    su - postgres -c  "psql -w -d snf_apps -f /tmp/psqlcmd"
644
    """.format(user_email)
645

    
646
    result = try_run(cmd)
647
    r = re.compile(r"(\d+)[ |]*(\S+)[ |]*(\S+)[ |]*" + user_email, re.M)
648
    match = r.search(result)
649
    uid, user_auth_token, user_uuid = match.groups()
650
    # print("%s: %s %s %s" % ( user_email, uid, user_auth_token, user_uuid))
651

    
652
    return (uid, user_auth_token, user_uuid)
653

    
654

    
655
@roles("cms")
656
def cms_loaddata():
657
    debug(env.host, " * Loading cms initial data...")
658
    if env.cms_pass:
659
      debug(env.host, "Aborting. Prerequisites not met.")
660
      return
661
    tmpl = "/tmp/sites.json"
662
    replace = {}
663
    custom = customize_settings_from_tmpl(tmpl, replace)
664
    put(custom, tmpl)
665

    
666
    tmpl = "/tmp/page.json"
667
    replace = {}
668
    custom = customize_settings_from_tmpl(tmpl, replace)
669
    put(custom, tmpl)
670

    
671
    cmd = """
672
    snf-manage loaddata /tmp/sites.json
673
    snf-manage loaddata /tmp/page.json
674
    snf-manage createsuperuser --username=admin --email=admin@{0} --noinput
675
    """.format(env.env.domain)
676
    try_run(cmd)
677

    
678

    
679
@roles("cms")
680
def setup_cms():
681
    debug(env.host, "Setting up cms...")
682
    if env.cms_pass:
683
      debug(env.host, "Aborting. Prerequisites not met.")
684
      return
685
    with settings(hide("everything")):
686
        try_run("ping -c1 accounts." + env.env.domain)
687
    setup_gunicorn()
688
    setup_apache()
689
    setup_webproject()
690
    install_package("snf-cloudcms")
691

    
692
    tmpl = "/etc/synnefo/cms.conf"
693
    replace = {
694
        "ACCOUNTS": env.env.accounts.fqdn,
695
        }
696
    custom = customize_settings_from_tmpl(tmpl, replace)
697
    put(custom, tmpl, mode=0644)
698
    try_run("/etc/init.d/gunicorn restart")
699

    
700

    
701
    cmd = """
702
    snf-manage syncdb
703
    snf-manage migrate --delete-ghost-migrations
704
    """.format(env.env.domain)
705
    try_run(cmd)
706

    
707

    
708
def setup_nfs_dirs():
709
    debug(env.host, " * Creating NFS mount point for pithos and ganeti...")
710
    cmd = """
711
    mkdir -p {0}
712
    cd {0}
713
    mkdir -p data
714
    chown www-data:www-data data
715
    chmod g+ws data
716
    mkdir -p /srv/okeanos
717
    """.format(env.env.pithos_dir)
718
    try_run(cmd)
719

    
720

    
721
@roles("nodes")
722
def setup_nfs_clients():
723
    if env.host == env.env.pithos.ip:
724
      return
725

    
726
    host_info = env.env.ips_info[env.host]
727
    debug(env.host, " * Mounting pithos NFS mount point...")
728
    with settings(hide("everything")):
729
        try_run("ping -c1 " + env.env.pithos.hostname)
730
    with settings(host_string=env.env.pithos.ip):
731
        update_nfs_exports(host_info.ip)
732

    
733
    install_package("nfs-common")
734
    for d in [env.env.pithos_dir, env.env.image_dir]:
735
      try_run("mkdir -p " + d)
736
      cmd = """
737
      echo "{0}:{1} {1}  nfs defaults,rw,noatime,rsize=131072,wsize=131072,timeo=14,intr,noacl" >> /etc/fstab
738
      """.format(env.env.pithos.ip, d)
739
      try_run(cmd)
740
      try_run("mount " + d)
741

    
742
@roles("pithos")
743
def update_nfs_exports(ip):
744
    tmpl = "/tmp/exports"
745
    replace = {
746
      "pithos_dir": env.env.pithos_dir,
747
      "image_dir": env.env.image_dir,
748
      "ip": ip,
749
      }
750
    custom = customize_settings_from_tmpl(tmpl, replace)
751
    put(custom, tmpl)
752
    try_run("cat %s >> /etc/exports" % tmpl)
753
    try_run("/etc/init.d/nfs-kernel-server restart")
754

    
755
@roles("pithos")
756
def setup_nfs_server():
757
    debug(env.host, " * Setting up NFS server for pithos...")
758
    setup_nfs_dirs()
759
    install_package("nfs-kernel-server")
760

    
761

    
762
@roles("pithos")
763
def setup_pithos():
764
    debug(env.host, "Setting up snf-pithos-app...")
765
    with settings(hide("everything")):
766
        try_run("ping -c1 accounts." + env.env.domain)
767
        try_run("ping -c1 " + env.env.db.ip)
768
    setup_gunicorn()
769
    setup_apache()
770
    setup_webproject()
771

    
772
    with settings(host_string=env.env.accounts.ip):
773
        service_id, service_token = get_service_details("pithos")
774

    
775
    install_package("kamaki")
776
    install_package("snf-pithos-backend")
777
    install_package("snf-pithos-app")
778
    tmpl = "/etc/synnefo/pithos.conf"
779
    replace = {
780
        "ACCOUNTS": env.env.accounts.fqdn,
781
        "PITHOS": env.env.pithos.fqdn,
782
        "db_node": env.env.db.ip,
783
        "synnefo_user": env.env.synnefo_user,
784
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
785
        "pithos_dir": env.env.pithos_dir,
786
        "PITHOS_SERVICE_TOKEN": service_token,
787
        "proxy": env.env.pithos.hostname == env.env.accounts.hostname
788
        }
789
    custom = customize_settings_from_tmpl(tmpl, replace)
790
    put(custom, tmpl, mode=0644)
791
    try_run("/etc/init.d/gunicorn restart")
792

    
793
    install_package("snf-pithos-webclient")
794
    tmpl = "/etc/synnefo/webclient.conf"
795
    replace = {
796
        "ACCOUNTS": env.env.accounts.fqdn,
797
        "PITHOS_UI_CLOUDBAR_ACTIVE_SERVICE": service_id,
798
        }
799
    custom = customize_settings_from_tmpl(tmpl, replace)
800
    put(custom, tmpl, mode=0644)
801

    
802
    try_run("/etc/init.d/gunicorn restart")
803
    #TOFIX: the previous command lets pithos-backend create blocks and maps
804
    #       with root owner
805
    try_run("chown -R www-data:www-data %s/data " % env.env.pithos_dir)
806
    #try_run("pithos-migrate stamp 4c8ccdc58192")
807
    #try_run("pithos-migrate upgrade head")
808

    
809

    
810
@roles("ganeti")
811
def setup_ganeti():
812
    debug(env.host, "Setting up snf-ganeti...")
813
    node_info = env.env.ips_info[env.host]
814
    with settings(hide("everything")):
815
        #if env.enable_lvm:
816
        #    try_run("vgs " + env.env.vg)
817
        try_run("getent hosts " + env.env.cluster.fqdn)
818
        try_run("getent hosts %s | grep -v ^127" % env.host)
819
        try_run("hostname -f | grep " + node_info.fqdn)
820
        #try_run("ip link show " + env.env.common_bridge)
821
        #try_run("ip link show " + env.env.common_bridge)
822
        #try_run("apt-get update")
823
    install_package("qemu-kvm")
824
    install_package("python-bitarray")
825
    install_package("ganeti-htools")
826
    install_package("snf-ganeti")
827
    try_run("mkdir -p /srv/ganeti/file-storage/")
828
    cmd = """
829
cat <<EOF > /etc/ganeti/file-storage-paths
830
/srv/ganeti/file-storage
831
/srv/ganeti/shared-file-storage
832
EOF
833
"""
834
    try_run(cmd)
835

    
836

    
837
@roles("master")
838
def add_rapi_user():
839
    debug(env.host, " * Adding RAPI user to Ganeti backend...")
840
    cmd = """
841
    echo -n "{0}:Ganeti Remote API:{1}" | openssl md5
842
    """.format(env.env.synnefo_user, env.env.synnefo_rapi_passwd)
843
    result = try_run(cmd)
844
    if result.startswith("(stdin)= "):
845
        result = result.split("(stdin)= ")[1]
846
    cmd = """
847
    echo "{0} {1}{2} write" >> /var/lib/ganeti/rapi/users
848
    """.format(env.env.synnefo_user, '{ha1}',result)
849
    try_run(cmd)
850
    try_run("/etc/init.d/ganeti restart")
851

    
852
@roles("master")
853
def add_nodes():
854
    nodes = env.env.cluster_nodes.split(",")
855
    nodes.remove(env.env.master_node)
856
    debug(env.host, " * Adding nodes to Ganeti backend...")
857
    for n in nodes:
858
        add_node(n)
859

    
860
@roles("master")
861
def add_node(node):
862
    node_info = env.env.nodes_info[node]
863
    debug(env.host, " * Adding node %s to Ganeti backend..." % node_info.fqdn)
864
    cmd = "gnt-node add --no-ssh-key-check --master-capable=yes --vm-capable=yes " + node_info.fqdn
865
    try_run(cmd)
866

    
867
@roles("ganeti")
868
def enable_drbd():
869
    if env.enable_drbd:
870
        debug(env.host, " * Enabling DRBD...")
871
        try_run("modprobe drbd minor_count=255 usermode_helper=/bin/true")
872
        try_run("echo drbd minor_count=255 usermode_helper=/bin/true >> /etc/modules")
873

    
874
@roles("master")
875
def setup_drbd_dparams():
876
    if env.enable_drbd:
877
        debug(env.host, " * Twicking drbd related disk parameters in Ganeti...")
878
        cmd = """
879
        gnt-cluster modify --disk-parameters=drbd:metavg={0}
880
        gnt-group modify --disk-parameters=drbd:metavg={0} default
881
        """.format(env.env.vg)
882
        try_run(cmd)
883

    
884
@roles("master")
885
def enable_lvm():
886
    if env.enable_lvm:
887
        debug(env.host, " * Enabling LVM...")
888
        cmd = """
889
        gnt-cluster modify --vg-name={0}
890
        """.format(env.env.vg)
891
        try_run(cmd)
892
    else:
893
        debug(env.host, " * Disabling LVM...")
894
        try_run("gnt-cluster modify --no-lvm-storage")
895

    
896
@roles("master")
897
def destroy_cluster():
898
    debug(env.host, " * Destroying Ganeti cluster...")
899
    #TODO: remove instances first
900
    allnodes = env.env.cluster_hostnames[:]
901
    allnodes.remove(env.host)
902
    for n in allnodes:
903
      host_info = env.env.ips_info[host]
904
      debug(env.host, " * Removing node %s..." % n)
905
      cmd = "gnt-node remove  " + host_info.fqdn
906
      try_run(cmd)
907
    try_run("gnt-cluster destroy --yes-do-it")
908

    
909

    
910
@roles("master")
911
def init_cluster():
912
    debug(env.host, " * Initializing Ganeti backend...")
913
    # extra = ""
914
    # if env.enable_lvm:
915
    #     extra += " --vg-name={0} ".format(env.env.vg)
916
    # else:
917
    #     extra += " --no-lvm-storage "
918
    # if not env.enable_drbd:
919
    #     extra += " --no-drbd-storage "
920
    extra = " --no-lvm-storage --no-drbd-storage "
921
    cmd = """
922
    gnt-cluster init --enabled-hypervisors=kvm \
923
                     {0} \
924
                     --nic-parameters link={1},mode=bridged \
925
                     --master-netdev {2} \
926
                     --default-iallocator hail \
927
                     --hypervisor-parameters kvm:kernel_path=,vnc_bind_address=0.0.0.0 \
928
                     --no-ssh-init --no-etc-hosts \
929
                    {3}
930

931
    """.format(extra, env.env.common_bridge,
932
               env.env.cluster_netdev, env.env.cluster.fqdn)
933
    try_run(cmd)
934

    
935

    
936
@roles("ganeti")
937
def debootstrap():
938
    install_package("ganeti-instance-debootstrap")
939

    
940

    
941
@roles("ganeti")
942
def setup_image_host():
943
    debug(env.host, "Setting up snf-image...")
944
    install_package("snf-pithos-backend")
945
    install_package("snf-image")
946
    try_run("mkdir -p %s" % env.env.image_dir)
947
    tmpl = "/etc/default/snf-image"
948
    replace = {
949
        "synnefo_user": env.env.synnefo_user,
950
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
951
        "pithos_dir": env.env.pithos_dir,
952
        "db_node": env.env.db.ip,
953
    }
954
    custom = customize_settings_from_tmpl(tmpl, replace)
955
    put(custom, tmpl)
956

    
957

    
958
@roles("ganeti")
959
def setup_image_helper():
960
    debug(env.host, " * Updating helper image...")
961
    cmd = """
962
    snf-image-update-helper -y
963
    """
964
    try_run(cmd)
965

    
966

    
967
@roles("ganeti")
968
def setup_gtools():
969
    debug(env.host, " * Setting up snf-cyclades-gtools...")
970
    with settings(hide("everything")):
971
        try_run("ping -c1 " + env.env.mq.ip)
972
    setup_common()
973
    install_package("snf-cyclades-gtools")
974
    tmpl = "/etc/synnefo/gtools.conf"
975
    replace = {
976
        "synnefo_user": env.env.synnefo_user,
977
        "synnefo_rabbitmq_passwd": env.env.synnefo_rabbitmq_passwd,
978
        "mq_node": env.env.mq.ip,
979
    }
980
    custom = customize_settings_from_tmpl(tmpl, replace)
981
    put(custom, tmpl)
982

    
983
    cmd = """
984
    sed -i 's/false/true/' /etc/default/snf-ganeti-eventd
985
    /etc/init.d/snf-ganeti-eventd start
986
    """
987
    try_run(cmd)
988

    
989

    
990
@roles("ganeti")
991
def setup_iptables():
992
    debug(env.host, " * Setting up iptables to mangle DHCP requests...")
993
    cmd = """
994
    iptables -t mangle -A PREROUTING -i br+ -p udp -m udp --dport 67 -j NFQUEUE --queue-num 42
995
    iptables -t mangle -A PREROUTING -i tap+ -p udp -m udp --dport 67 -j NFQUEUE --queue-num 42
996
    iptables -t mangle -A PREROUTING -i prv+ -p udp -m udp --dport 67 -j NFQUEUE --queue-num 42
997

998
    ip6tables -t mangle -A PREROUTING -i br+ -p ipv6-icmp -m icmp6 --icmpv6-type 133 -j NFQUEUE --queue-num 43
999
    ip6tables -t mangle -A PREROUTING -i br+ -p ipv6-icmp -m icmp6 --icmpv6-type 135 -j NFQUEUE --queue-num 44
1000
    """
1001
    try_run(cmd)
1002

    
1003
@roles("ganeti")
1004
def setup_network():
1005
    debug(env.host, "Setting up networking for Ganeti instances (nfdhcpd, etc.)...")
1006
    install_package("nfqueue-bindings-python")
1007
    install_package("nfdhcpd")
1008
    tmpl = "/etc/nfdhcpd/nfdhcpd.conf"
1009
    replace = {
1010
      "ns_node_ip": env.env.ns.ip
1011
      }
1012
    custom = customize_settings_from_tmpl(tmpl, replace)
1013
    put(custom, tmpl)
1014
    try_run("/etc/init.d/nfdhcpd restart")
1015

    
1016
    install_package("snf-network")
1017
    cmd = """
1018
    sed -i 's/MAC_MASK.*/MAC_MASK = ff:ff:f0:00:00:00/' /etc/default/snf-network
1019
    """
1020
    try_run(cmd)
1021

    
1022

    
1023
@roles("router")
1024
def setup_router():
1025
    debug(env.host, " * Setting up internal router for NAT...")
1026
    cmd = """
1027
    echo 1 > /proc/sys/net/ipv4/ip_forward
1028
    iptables -t nat -A POSTROUTING -s {0} -o {3} -j MASQUERADE
1029
    ip addr add {1} dev {2}
1030
    ip route add {0} dev {2} src {1}
1031
    """.format(env.env.synnefo_public_network_subnet,
1032
               env.env.synnefo_public_network_gateway,
1033
               env.env.common_bridge, env.env.public_iface)
1034
    try_run(cmd)
1035

    
1036

    
1037
@roles("cyclades")
1038
def cyclades_loaddata():
1039
    debug(env.host, " * Loading initial data for cyclades...")
1040
    try_run("snf-manage flavor-create %s %s %s %s" % (env.env.flavor_cpu,
1041
                                                      env.env.flavor_ram,
1042
                                                      env.env.flavor_disk,
1043
                                                      env.env.flavor_storage))
1044
    #run("snf-manage loaddata flavors")
1045

    
1046

    
1047
@roles("cyclades")
1048
def setup_cyclades():
1049
    debug(env.host, "Setting up snf-cyclades-app...")
1050
    with settings(hide("everything")):
1051
        try_run("ping -c1 accounts." + env.env.domain)
1052
        try_run("ping -c1 " + env.env.db.ip)
1053
        try_run("ping -c1 " + env.env.mq.ip)
1054
    setup_gunicorn()
1055
    setup_apache()
1056
    setup_webproject()
1057
    install_package("memcached")
1058
    install_package("python-memcache")
1059
    install_package("snf-pithos-backend")
1060
    install_package("kamaki")
1061
    install_package("snf-cyclades-app")
1062
    install_package("python-django-south")
1063
    tmpl = "/etc/synnefo/cyclades.conf"
1064

    
1065
    with settings(host_string=env.env.accounts.ip):
1066
        service_id, service_token = get_service_details("cyclades")
1067

    
1068
    replace = {
1069
        "ACCOUNTS": env.env.accounts.fqdn,
1070
        "CYCLADES": env.env.cyclades.fqdn,
1071
        "mq_node": env.env.mq.ip,
1072
        "db_node": env.env.db.ip,
1073
        "synnefo_user": env.env.synnefo_user,
1074
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
1075
        "synnefo_rabbitmq_passwd": env.env.synnefo_rabbitmq_passwd,
1076
        "pithos_dir": env.env.pithos_dir,
1077
        "common_bridge": env.env.common_bridge,
1078
        "HOST": env.env.cyclades.ip,
1079
        "domain": env.env.domain,
1080
        "CYCLADES_SERVICE_TOKEN": service_token,
1081
        "proxy": env.env.cyclades.hostname == env.env.accounts.hostname
1082
        }
1083
    custom = customize_settings_from_tmpl(tmpl, replace)
1084
    put(custom, tmpl, mode=0644)
1085
    try_run("/etc/init.d/gunicorn restart")
1086

    
1087
    cmd = """
1088
    sed -i 's/false/true/' /etc/default/snf-dispatcher
1089
    /etc/init.d/snf-dispatcher start
1090
    """
1091
    try_run(cmd)
1092

    
1093
    try_run("snf-manage syncdb")
1094
    try_run("snf-manage migrate --delete-ghost-migrations")
1095

    
1096

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

    
1102

    
1103
@roles("cyclades")
1104
def add_backend():
1105
    debug(env.host, "adding %s ganeti backend to cyclades..." % env.env.cluster.fqdn)
1106
    with settings(hide("everything")):
1107
        try_run("ping -c1 " + env.env.cluster.fqdn)
1108
    cmd = """
1109
    snf-manage backend-add --clustername={0} --user={1} --pass={2}
1110
    """.format(env.env.cluster.fqdn, env.env.synnefo_user,
1111
               env.env.synnefo_rapi_passwd)
1112
    try_run(cmd)
1113
    backend_id = get_backend_id(env.env.cluster.fqdn)
1114
    try_run("snf-manage backend-modify --drained=False " + backend_id)
1115

    
1116
@roles("cyclades")
1117
def pin_user_to_backend(user_email):
1118
    backend_id = get_backend_id(env.env.cluster.fqdn)
1119
    # pin user to backend
1120
    cmd = """
1121
cat <<EOF >> /etc/synnefo/cyclades.conf
1122

1123
BACKEND_PER_USER = {
1124
  '%s': %s,
1125
}
1126

1127
EOF
1128
/etc/init.d/gunicorn restart
1129
    """  % (user_email, backend_id)
1130
    try_run(cmd)
1131

    
1132
@roles("cyclades")
1133
def add_pools():
1134
    debug(env.host, " * Creating pools of resources (brigdes, mac prefixes) in cyclades...")
1135
    try_run("snf-manage pool-create --type=mac-prefix --base=aa:00:0 --size=65536")
1136
    try_run("snf-manage pool-create --type=bridge --base=prv --size=20")
1137

    
1138

    
1139
@roles("accounts", "cyclades", "pithos")
1140
def export_services():
1141
    debug(env.host, " * Exporting services...")
1142
    host = env.host
1143
    services = []
1144
    if host == env.env.cyclades.ip:
1145
        services.append("cyclades")
1146
    if host == env.env.pithos.ip:
1147
        services.append("pithos")
1148
    if host == env.env.accounts.ip:
1149
        services.append("astakos")
1150
    for service in services:
1151
        filename = "%s_services.json" % service
1152
        cmd = "snf-manage service-export-%s > %s" % (service, filename)
1153
        run(cmd)
1154
        get(filename, filename+".local")
1155

    
1156

    
1157
@roles("accounts")
1158
def import_services():
1159
    debug(env.host, " * Registering services to astakos...")
1160
    for service in ["cyclades", "pithos", "astakos"]:
1161
        filename = "%s_services.json" % service
1162
        put(filename +".local", filename)
1163
        cmd = "snf-manage service-import --json=%s" % filename
1164
        run(cmd)
1165

    
1166
    debug(env.host, " * Setting default quota...")
1167
    cmd = """
1168
    snf-manage resource-modify --limit 40G pithos.diskspace
1169
    snf-manage resource-modify --limit 2 astakos.pending_app
1170
    snf-manage resource-modify --limit 4 cyclades.vm
1171
    snf-manage resource-modify --limit 40G cyclades.disk
1172
    snf-manage resource-modify --limit 16G cyclades.ram
1173
    snf-manage resource-modify --limit 8G cyclades.active_ram
1174
    snf-manage resource-modify --limit 32 cyclades.cpu
1175
    snf-manage resource-modify --limit 16 cyclades.active_cpu
1176
    snf-manage resource-modify --limit 4 cyclades.network.private
1177
    """
1178
    try_run(cmd)
1179

    
1180

    
1181
@roles("cyclades")
1182
def add_network():
1183
    debug(env.host, " * Adding public network in cyclades...")
1184
    backend_id = get_backend_id(env.env.cluster.fqdn)
1185
    cmd = """
1186
    snf-manage network-create --subnet={0} --gateway={1} --public --dhcp --flavor={2} --mode=bridged --link={3} --name=Internet --backend-id={4}
1187
    """.format(env.env.synnefo_public_network_subnet,
1188
               env.env.synnefo_public_network_gateway,
1189
               env.env.synnefo_public_network_type,
1190
               env.env.common_bridge, backend_id)
1191
    try_run(cmd)
1192

    
1193

    
1194
@roles("cyclades")
1195
def setup_vncauthproxy():
1196
    debug(env.host, " * Setting up vncauthproxy...")
1197
    install_package("snf-vncauthproxy")
1198
    cmd = """
1199
    echo CHUID="www-data:nogroup" >> /etc/default/vncauthproxy
1200
    rm /var/log/vncauthproxy/vncauthproxy.log
1201
    """
1202
    try_run(cmd)
1203
    try_run("/etc/init.d/vncauthproxy restart")
1204

    
1205
@roles("client")
1206
def setup_kamaki():
1207
    debug(env.host, "Setting up kamaki client...")
1208
    with settings(hide("everything")):
1209
        try_run("ping -c1 accounts." + env.env.domain)
1210
        try_run("ping -c1 cyclades." + env.env.domain)
1211
        try_run("ping -c1 pithos." + env.env.domain)
1212

    
1213
    with settings(host_string=env.env.db.ip):
1214
        uid, user_auth_token, user_uuid = get_auth_token_from_db(env.env.user_email)
1215

    
1216
    install_package("python-progress")
1217
    install_package("kamaki")
1218
    cmd = """
1219
    kamaki config set cloud.default.url "https://{0}/astakos/identity/v2.0/"
1220
    kamaki config set cloud.default.token {1}
1221
    """.format(env.env.accounts.fqdn, user_auth_token)
1222
    try_run(cmd)
1223
    try_run("kamaki file create images")
1224

    
1225
@roles("client")
1226
def upload_image(image="debian_base.diskdump"):
1227
    debug(env.host, " * Uploading initial image to pithos...")
1228
    image = "debian_base.diskdump"
1229
    try_run("wget {0} -O /tmp/{1}".format(env.env.debian_base_url, image))
1230
    try_run("kamaki file upload --container images /tmp/{0} {0}".format(image))
1231

    
1232
@roles("client")
1233
def register_image(image="debian_base.diskdump"):
1234
    debug(env.host, " * Register image to plankton...")
1235
    # with settings(host_string=env.env.db.ip):
1236
    #     uid, user_auth_token, user_uuid = get_auth_token_from_db(env.env.user_email)
1237

    
1238
    image_location = "images:{0}".format(image)
1239
    cmd = """
1240
    sleep 5
1241
    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
1242
    """.format(image_location)
1243
    try_run(cmd)
1244

    
1245
@roles("client")
1246
def setup_burnin():
1247
    debug(env.host, "Setting up burnin testing tool...")
1248
    install_package("kamaki")
1249
    install_package("snf-tools")
1250

    
1251
@roles("pithos")
1252
def add_image_locally():
1253
    debug(env.host, " * Getting image locally in order snf-image to use it directly..")
1254
    image = "debian_base.diskdump"
1255
    try_run("wget {0} -O {1}/{2}".format(env.env.debian_base_url, env.env.image_dir, image))
1256

    
1257

    
1258
@roles("master")
1259
def gnt_instance_add(name="test"):
1260
    debug(env.host, " * Adding test instance to Ganeti...")
1261
    osp="""img_passwd=gamwtosecurity,img_format=diskdump,img_id=debian_base,img_properties='{"OSFAMILY":"linux"\,"ROOT_PARTITION":"1"}'"""
1262
    cmd = """
1263
    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}
1264
    """.format(osp, name)
1265
    try_run(cmd)
1266

    
1267
@roles("master")
1268
def gnt_network_add(name="test", subnet="10.0.0.0/26", gw="10.0.0.1", mode="bridged", link="br0"):
1269
    debug(env.host, " * Adding test network to Ganeti...")
1270
    cmd = """
1271
    gnt-network add --network={1} --gateway={2} {0}
1272
    gnt-network connect {0} {3} {4}
1273
    """.format(name, subnet, gw, mode, link)
1274
    try_run(cmd)
1275

    
1276
@roles("ips")
1277
def test():
1278
    debug(env.host, "Testing...")
1279
    try_run("hostname && date")