Statistics
| Branch: | Tag: | Revision:

root / snf-deploy / snfdeploy / fabfile.py @ d9667d93

History | View | Annotate | Download (42.2 kB)

1
# Too many lines in module pylint: disable-msg=C0302
2
# Too many arguments (7/5) pylint: disable-msg=R0913
3
"""
4
Fabric file for snf-deploy
5

6
"""
7

    
8
from __future__ import with_statement
9
from fabric.api import hide, env, settings, local, roles, execute
10
from fabric.operations import run, put, get
11
import fabric
12
import re
13
import os
14
import shutil
15
import tempfile
16
import ast
17
from snfdeploy.lib import debug, Conf, Env, disable_color
18
from snfdeploy.utils import *
19
from snfdeploy import massedit
20

    
21

    
22
def setup_env(args):
23
    """Setup environment"""
24
    print("Loading configuration for synnefo...")
25

    
26
    conf = Conf(args)
27
    env.env = Env(conf)
28

    
29
    env.local = args.autoconf
30
    env.key_inject = args.key_inject
31
    env.password = env.env.password
32
    env.user = env.env.user
33
    env.shell = "/bin/bash -c"
34
    env.key_filename = args.ssh_key
35

    
36
    if args.disable_colors:
37
        disable_color()
38

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

    
46
    if env.env.accounts.hostname in \
47
            [env.env.cyclades.hostname, env.env.pithos.hostname]:
48
        env.csrf_disable = True
49
    else:
50
        env.csrf_disable = False
51

    
52
    env.roledefs = {
53
        "nodes": env.env.ips,
54
        "ips": env.env.ips,
55
        "accounts": [env.env.accounts.ip],
56
        "cyclades": [env.env.cyclades.ip],
57
        "pithos": [env.env.pithos.ip],
58
        "cms": [env.env.cms.ip],
59
        "mq": [env.env.mq.ip],
60
        "db": [env.env.db.ip],
61
        "mq": [env.env.mq.ip],
62
        "db": [env.env.db.ip],
63
        "ns": [env.env.ns.ip],
64
        "client": [env.env.client.ip],
65
        "router": [env.env.router.ip],
66
        "stats": [env.env.stats.ip],
67
    }
68

    
69
    env.enable_lvm = False
70
    env.enable_drbd = False
71
    if ast.literal_eval(env.env.create_extra_disk) and env.env.extra_disk:
72
        env.enable_lvm = True
73
        env.enable_drbd = True
74

    
75
    env.roledefs.update({
76
        "ganeti": env.env.cluster_ips,
77
        "master": [env.env.master.ip],
78
    })
79

    
80

    
81
@roles("ns")
82
def update_ns_for_ganeti():
83
    debug(env.host,
84
          "Updating name server entries for backend %s..."
85
          % env.env.cluster.fqdn)
86
    update_arecord(env.env.cluster)
87
    update_ptrrecord(env.env.cluster)
88
    try_run("/etc/init.d/bind9 restart")
89

    
90

    
91
@roles("ns")
92
def update_ns_for_node(node):
93
    info = env.env.nodes_info.get(node)
94
    update_arecord(info)
95
    update_ptrrecord(info)
96
    try_run("/etc/init.d/bind9 restart")
97

    
98

    
99
@roles("ns")
100
def update_arecord(host):
101
    filename = "/etc/bind/zones/" + env.env.domain
102
    cmd = """
103
    echo '{0}' >> {1}
104
    """.format(host.arecord, filename)
105
    try_run(cmd)
106

    
107

    
108
@roles("ns")
109
def update_cnamerecord(host):
110
    filename = "/etc/bind/zones/" + env.env.domain
111
    cmd = """
112
    echo '{0}' >> {1}
113
    """.format(host.cnamerecord, filename)
114
    try_run(cmd)
115

    
116

    
117
@roles("ns")
118
def update_ptrrecord(host):
119
    filename = "/etc/bind/rev/synnefo.in-addr.arpa.zone"
120
    cmd = """
121
    echo '{0}' >> {1}
122
    """.format(host.ptrrecord, filename)
123
    try_run(cmd)
124

    
125

    
126
@roles("nodes")
127
def apt_get_update():
128
    debug(env.host, "apt-get update....")
129
    try_run("apt-get update")
130

    
131

    
132
@roles("ns")
133
def setup_ns():
134
    debug(env.host, "Setting up name server..")
135
    #WARNING: this should be remove after we are done
136
    # because gevent does pick randomly nameservers and google does
137
    # not know our setup!!!!!
138
    apt_get_update()
139
    install_package("bind9")
140
    tmpl = "/etc/bind/named.conf.local"
141
    replace = {
142
        "domain": env.env.domain,
143
    }
144
    custom = customize_settings_from_tmpl(tmpl, replace)
145
    try_put(custom, tmpl)
146

    
147
    try_run("mkdir -p /etc/bind/zones")
148
    tmpl = "/etc/bind/zones/example.com"
149
    replace = {
150
        "domain": env.env.domain,
151
        "ns_node_ip": env.env.ns.ip,
152
    }
153
    custom = customize_settings_from_tmpl(tmpl, replace)
154
    remote = "/etc/bind/zones/" + env.env.domain
155
    try_put(custom, remote)
156

    
157
    try_run("mkdir -p /etc/bind/rev")
158
    tmpl = "/etc/bind/rev/synnefo.in-addr.arpa.zone"
159
    replace = {
160
        "domain": env.env.domain,
161
    }
162
    custom = customize_settings_from_tmpl(tmpl, replace)
163
    try_put(custom, tmpl)
164

    
165
    tmpl = "/etc/bind/named.conf.options"
166
    replace = {
167
        "NODE_IPS": ";".join(env.env.ips),
168
    }
169
    custom = customize_settings_from_tmpl(tmpl, replace)
170
    try_put(custom, tmpl, mode=0644)
171

    
172
    for role, info in env.env.roles.iteritems():
173
        if role == "ns":
174
            continue
175
        update_cnamerecord(info)
176
    for node, info in env.env.nodes_info.iteritems():
177
        update_arecord(info)
178
        update_ptrrecord(info)
179

    
180
    try_run("/etc/init.d/bind9 restart")
181

    
182

    
183
@roles("nodes")
184
def check_dhcp():
185
    debug(env.host, "Checking IPs for synnefo..")
186
    for n, info in env.env.nodes_info.iteritems():
187
        try_run("ping -c 1 " + info.ip)
188

    
189

    
190
@roles("nodes")
191
def check_dns():
192
    debug(env.host, "Checking fqdns for synnefo..")
193
    for n, info in env.env.nodes_info.iteritems():
194
        try_run("ping -c 1 " + info.fqdn)
195

    
196
    for n, info in env.env.roles.iteritems():
197
        try_run("ping -c 1 " + info.fqdn)
198

    
199

    
200
@roles("nodes")
201
def check_connectivity():
202
    debug(env.host, "Checking internet connectivity..")
203
    try_run("ping -c 1 www.google.com")
204

    
205

    
206
@roles("nodes")
207
def check_ssh():
208
    debug(env.host, "Checking password-less ssh..")
209
    for n, info in env.env.nodes_info.iteritems():
210
        try_run("ssh " + info.fqdn + "  date")
211

    
212

    
213
@roles("ips")
214
def add_keys():
215
    if not env.key_inject:
216
        debug(env.host, "Skipping ssh keys injection..")
217
        return
218
    else:
219
        debug(env.host, "Adding rsa/dsa keys..")
220
    try_run("mkdir -p /root/.ssh")
221
    cmd = """
222
for f in $(ls /root/.ssh/*); do
223
  cp $f $f.bak
224
done
225
    """
226
    try_run(cmd)
227
    files = ["authorized_keys", "id_dsa", "id_dsa.pub",
228
             "id_rsa", "id_rsa.pub"]
229
    for f in files:
230
        tmpl = "/root/.ssh/" + f
231
        replace = {}
232
        custom = customize_settings_from_tmpl(tmpl, replace)
233
        try_put(custom, tmpl, mode=0600)
234

    
235
    cmd = """
236
if [ -e /root/.ssh/authorized_keys.bak ]; then
237
  cat /root/.ssh/authorized_keys.bak >> /root/.ssh/authorized_keys
238
fi
239
    """
240
    debug(env.host, "Updating exising authorized keys..")
241
    try_run(cmd)
242

    
243

    
244
@roles("ips")
245
def setup_resolv_conf():
246
    debug(env.host, "Tweak /etc/resolv.conf...")
247
    try_run("/etc/init.d/network-manager stop", abort=False)
248
    tmpl = "/etc/dhcp/dhclient-enter-hooks.d/nodnsupdate"
249
    replace = {}
250
    custom = customize_settings_from_tmpl(tmpl, replace)
251
    try_put(custom, tmpl, mode=0644)
252
    try_run("cp /etc/resolv.conf /etc/resolv.conf.bak")
253
    tmpl = "/etc/resolv.conf"
254
    replace = {
255
        "domain": env.env.domain,
256
        "ns_node_ip": env.env.ns.ip,
257
    }
258
    custom = customize_settings_from_tmpl(tmpl, replace)
259
    try:
260
        try_put(custom, tmpl)
261
        cmd = """
262
        echo "\
263
# This has been generated automatically by snf-deploy, at
264
# $(date).
265
# The immutable bit (+i attribute) has been used to avoid it being
266
# overwritten by software such as NetworkManager or resolvconf.
267
# Use lsattr/chattr to view or modify its file attributes.
268

269

270
$(cat {0})" > {0}
271
""".format(tmpl)
272
        try_run(cmd)
273
    except:
274
        pass
275
    try_run("chattr +i /etc/resolv.conf")
276

    
277

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

    
290

    
291
def create_bridges():
292
    debug(env.host, " * Creating bridges...")
293
    install_package("bridge-utils")
294
    cmd = """
295
    brctl addbr {0} ; ip link set {0} up
296
    """.format(env.env.common_bridge)
297
    try_run(cmd)
298

    
299

    
300
def connect_bridges():
301
    debug(env.host, " * Connecting bridges...")
302
    #cmd = """
303
    #brctl addif {0} {1}
304
    #""".format(env.env.common_bridge, env.env.public_iface)
305
    #try_run(cmd)
306

    
307

    
308
@roles("ganeti")
309
def setup_net_infra():
310
    debug(env.host, "Setup networking infrastracture..")
311
    create_bridges()
312
    connect_bridges()
313

    
314

    
315
@roles("ganeti")
316
def setup_lvm():
317
    debug(env.host, "create volume group %s for ganeti.." % env.env.vg)
318
    if env.enable_lvm:
319
        install_package("lvm2")
320
        cmd = """
321
        pvcreate {0}
322
        vgcreate {1} {0}
323
        """.format(env.env.extra_disk, env.env.vg)
324
        try_run(cmd)
325

    
326

    
327
@roles("nodes")
328
def setup_apt():
329
    debug(env.host, "Setting up apt sources...")
330
    install_package("curl")
331
    cmd = """
332
    echo 'APT::Install-Suggests "false";' >> /etc/apt/apt.conf
333
    curl -k https://dev.grnet.gr/files/apt-grnetdev.pub | apt-key add -
334
    """
335
    try_run(cmd)
336
    host_info = env.env.ips_info[env.host]
337
    if host_info.os == "squeeze":
338
        tmpl = "/etc/apt/sources.list.d/synnefo.squeeze.list"
339
    else:
340
        tmpl = "/etc/apt/sources.list.d/synnefo.wheezy.list"
341
    replace = {}
342
    custom = customize_settings_from_tmpl(tmpl, replace)
343
    try_put(custom, tmpl)
344
    apt_get_update()
345

    
346

    
347
@roles("cyclades", "cms", "pithos", "accounts")
348
def restart_services():
349
    debug(env.host, " * Restarting apache2 and gunicorn...")
350
    try_run("/etc/init.d/gunicorn restart")
351
    try_run("/etc/init.d/apache2 restart")
352

    
353

    
354
def setup_gunicorn():
355
    debug(env.host, " * Setting up gunicorn...")
356
    install_package("gunicorn")
357
    try_run("chown root.www-data /var/log/gunicorn")
358
    tmpl = "/etc/gunicorn.d/synnefo"
359
    replace = {}
360
    custom = customize_settings_from_tmpl(tmpl, replace)
361
    try_put(custom, tmpl, mode=0644)
362
    try_run("/etc/init.d/gunicorn restart")
363

    
364

    
365
def setup_apache():
366
    debug(env.host, " * Setting up apache2...")
367
    host_info = env.env.ips_info[env.host]
368
    install_package("apache2")
369
    tmpl = "/etc/apache2/sites-available/synnefo"
370
    replace = {
371
        "HOST": host_info.fqdn,
372
    }
373
    custom = customize_settings_from_tmpl(tmpl, replace)
374
    try_put(custom, tmpl)
375
    tmpl = "/etc/apache2/sites-available/synnefo-ssl"
376
    custom = customize_settings_from_tmpl(tmpl, replace)
377
    try_put(custom, tmpl)
378
    cmd = """
379
    a2enmod ssl
380
    a2enmod rewrite
381
    a2dissite default
382
    a2ensite synnefo
383
    a2ensite synnefo-ssl
384
    a2enmod headers
385
    a2enmod proxy_http
386
    a2dismod autoindex
387
    """
388
    try_run(cmd)
389
    try_run("/etc/init.d/apache2 restart")
390

    
391

    
392
@roles("mq")
393
def setup_mq():
394
    debug(env.host, "Setting up RabbitMQ...")
395
    install_package("rabbitmq-server")
396
    cmd = """
397
    rabbitmqctl add_user {0} {1}
398
    rabbitmqctl set_permissions {0} ".*" ".*" ".*"
399
    rabbitmqctl delete_user guest
400
    rabbitmqctl set_user_tags {0} administrator
401
    """.format(env.env.synnefo_user, env.env.synnefo_rabbitmq_passwd)
402
    try_run(cmd)
403
    try_run("/etc/init.d/rabbitmq-server restart")
404

    
405

    
406
@roles("db")
407
def allow_access_in_db(ip, user="all", method="md5"):
408
    cmd = """
409
    pg_hba=$(ls /etc/postgresql/*/main/pg_hba.conf)
410
    echo host all {0} {1}/32 {2} >> $pg_hba
411
    """.format(user, ip, method)
412
    try_run(cmd)
413
    cmd = """
414
    pg_hba=$(ls /etc/postgresql/*/main/pg_hba.conf)
415
    sed -i 's/\(host.*127.0.0.1.*\)md5/\\1trust/' $pg_hba
416
    """
417
    try_run(cmd)
418
    try_run("/etc/init.d/postgresql restart")
419

    
420

    
421
@roles("db")
422
def setup_db():
423
    debug(env.host, "Setting up DataBase server...")
424
    install_package("postgresql")
425

    
426
    tmpl = "/tmp/db-init.psql"
427
    replace = {
428
        "synnefo_user": env.env.synnefo_user,
429
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
430
        }
431
    custom = customize_settings_from_tmpl(tmpl, replace)
432
    try_put(custom, tmpl)
433
    cmd = 'su - postgres -c "psql -w -f %s" ' % tmpl
434
    try_run(cmd)
435
    cmd = """
436
    conf=$(ls /etc/postgresql/*/main/postgresql.conf)
437
    echo "listen_addresses = '*'" >> $conf
438
    """
439
    try_run(cmd)
440

    
441
    if env.env.testing_vm:
442
        cmd = """
443
        conf=$(ls /etc/postgresql/*/main/postgresql.conf)
444
        echo "fsync=off\nsynchronous_commit=off\nfull_page_writes=off" >> $conf
445
        """
446
        try_run(cmd)
447

    
448
    allow_access_in_db(env.host, "all", "trust")
449
    try_run("/etc/init.d/postgresql restart")
450

    
451

    
452
@roles("db")
453
def destroy_db():
454
    try_run("""su - postgres -c ' psql -w -c "drop database snf_apps" '""")
455
    try_run("""su - postgres -c ' psql -w -c "drop database snf_pithos" '""")
456

    
457

    
458
def setup_webproject():
459
    debug(env.host, " * Setting up snf-webproject...")
460
    with settings(hide("everything")):
461
        try_run("ping -c1 " + env.env.db.ip)
462
    setup_common()
463
    install_package("snf-webproject")
464
    install_package("python-psycopg2")
465
    install_package("python-gevent")
466
    install_package("python-django")
467
    tmpl = "/etc/synnefo/webproject.conf"
468
    replace = {
469
        "synnefo_user": env.env.synnefo_user,
470
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
471
        "db_node": env.env.db.ip,
472
        "domain": env.env.domain,
473
    }
474
    custom = customize_settings_from_tmpl(tmpl, replace)
475
    try_put(custom, tmpl, mode=0644)
476
    with settings(host_string=env.env.db.ip):
477
        host_info = env.env.ips_info[env.host]
478
        allow_access_in_db(host_info.ip, "all", "trust")
479
    try_run("/etc/init.d/gunicorn restart")
480

    
481

    
482
def setup_common():
483
    debug(env.host, " * Setting up snf-common...")
484
    host_info = env.env.ips_info[env.host]
485
    install_package("python-objpool")
486
    install_package("snf-common")
487
    install_package("python-astakosclient")
488
    install_package("snf-django-lib")
489
    install_package("snf-branding")
490
    tmpl = "/etc/synnefo/common.conf"
491
    replace = {
492
        #FIXME:
493
        "EMAIL_SUBJECT_PREFIX": env.host,
494
        "domain": env.env.domain,
495
        "HOST": host_info.fqdn,
496
        "MAIL_DIR": env.env.mail_dir,
497
    }
498
    custom = customize_settings_from_tmpl(tmpl, replace)
499
    try_put(custom, tmpl, mode=0644)
500
    try_run("mkdir -p {0}; chmod 777 {0}".format(env.env.mail_dir))
501
    try_run("/etc/init.d/gunicorn restart")
502

    
503

    
504
@roles("accounts")
505
def astakos_loaddata():
506
    debug(env.host, " * Loading initial data to astakos...")
507
    cmd = """
508
    snf-manage loaddata groups
509
    """
510
    try_run(cmd)
511

    
512

    
513
@roles("accounts")
514
def astakos_register_components():
515
    debug(env.host, " * Register services in astakos...")
516

    
517
    cyclades_base_url = "https://%s/cyclades" % env.env.cyclades.fqdn
518
    pithos_base_url = "https://%s/pithos" % env.env.pithos.fqdn
519
    astakos_base_url = "https://%s/astakos" % env.env.accounts.fqdn
520

    
521
    cmd = """
522
    snf-manage component-add "home" --ui-url https://{0}
523
    snf-manage component-add "cyclades" --base-url {1} --ui-url {1}/ui
524
    snf-manage component-add "pithos" --base-url {2} --ui-url {2}/ui
525
    snf-manage component-add "astakos" --base-url {3} --ui-url {3}/ui
526
    """.format(env.env.cms.fqdn, cyclades_base_url,
527
               pithos_base_url, astakos_base_url)
528
    try_run(cmd)
529

    
530

    
531
@roles("accounts")
532
def astakos_register_pithos_view():
533
    debug(env.host, " * Register pithos view as oauth2 client...")
534

    
535
    pithos_base_url = "https://%s/pithos" % env.env.pithos.fqdn
536

    
537
    cmd = """
538
    snf-manage oauth2-client-add pithos-view --secret={0} --is-trusted \
539
    --url {1}
540
    """.format(env.env.oa2_secret, '%s/ui/view' % pithos_base_url)
541
    try_run(cmd)
542

    
543

    
544
@roles("accounts")
545
def add_user():
546
    debug(env.host, " * adding user %s to astakos..." % env.env.user_email)
547
    email = env.env.user_email
548
    name = env.env.user_name
549
    lastname = env.env.user_lastname
550
    passwd = env.env.user_passwd
551
    cmd = """
552
    snf-manage user-add {0} {1} {2}
553
    """.format(email, name, lastname)
554
    try_run(cmd)
555
    with settings(host_string=env.env.db.ip):
556
        uid, user_auth_token, user_uuid = get_auth_token_from_db(email)
557
    cmd = """
558
    snf-manage user-modify --password {0} {1}
559
    """.format(passwd, uid)
560
    try_run(cmd)
561

    
562

    
563
@roles("accounts")
564
def activate_user(user_email=None):
565
    if not user_email:
566
        user_email = env.env.user_email
567
    debug(env.host, " * Activate user %s..." % user_email)
568
    with settings(host_string=env.env.db.ip):
569
        uid, user_auth_token, user_uuid = get_auth_token_from_db(user_email)
570

    
571
    cmd = """
572
    snf-manage user-modify --verify {0}
573
    snf-manage user-modify --accept {0}
574
    """.format(uid)
575
    try_run(cmd)
576

    
577

    
578
@roles("accounts")
579
def setup_astakos():
580
    debug(env.host, "Setting up snf-astakos-app...")
581
    setup_gunicorn()
582
    setup_apache()
583
    setup_webproject()
584
    install_package("python-django-south")
585
    install_package("snf-astakos-app")
586
    install_package("kamaki")
587

    
588
    tmpl = "/etc/synnefo/astakos.conf"
589
    replace = {
590
        "ACCOUNTS": env.env.accounts.fqdn,
591
        "domain": env.env.domain,
592
        "CYCLADES": env.env.cyclades.fqdn,
593
        "PITHOS": env.env.pithos.fqdn,
594
    }
595
    custom = customize_settings_from_tmpl(tmpl, replace)
596
    try_put(custom, tmpl, mode=0644)
597
    if env.csrf_disable:
598
        cmd = """
599
cat <<EOF >> /etc/synnefo/astakos.conf
600
try:
601
  MIDDLEWARE_CLASSES.remove('django.middleware.csrf.CsrfViewMiddleware')
602
except:
603
  pass
604
EOF
605
"""
606
        try_run(cmd)
607

    
608
    try_run("/etc/init.d/gunicorn restart")
609

    
610
    cmd = """
611
    snf-manage syncdb --noinput
612
    snf-manage migrate im --delete-ghost-migrations
613
    snf-manage migrate quotaholder_app
614
    snf-manage migrate oa2
615
    """
616
    try_run(cmd)
617

    
618

    
619
@roles("accounts")
620
def get_service_details(service="pithos"):
621
    debug(env.host,
622
          " * Getting registered details for %s service..." % service)
623
    result = try_run("snf-manage component-list -o id,name,token")
624
    r = re.compile(r".*%s.*" % service, re.M)
625
    service_id, _, service_token = r.search(result).group().split()
626
    # print("%s: %s %s" % (service, service_id, service_token))
627
    return (service_id, service_token)
628

    
629

    
630
@roles("db")
631
def get_auth_token_from_db(user_email=None):
632
    if not user_email:
633
        user_email = env.env.user_email
634
    debug(env.host,
635
          " * Getting authentication token and uuid for user %s..."
636
          % user_email)
637
    cmd = """
638
echo "select id, auth_token, uuid, email from auth_user, im_astakosuser \
639
where auth_user.id = im_astakosuser.user_ptr_id and auth_user.email = '{0}';" \
640
> /tmp/psqlcmd
641
su - postgres -c  "psql -w -d snf_apps -f /tmp/psqlcmd"
642
""".format(user_email)
643

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

    
650
    return (uid, user_auth_token, user_uuid)
651

    
652

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

    
664
    tmpl = "/tmp/page.json"
665
    replace = {}
666
    custom = customize_settings_from_tmpl(tmpl, replace)
667
    try_put(custom, tmpl)
668

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

    
676

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

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

    
698
    cmd = """
699
    snf-manage syncdb
700
    snf-manage migrate --delete-ghost-migrations
701
    """.format(env.env.domain)
702
    try_run(cmd)
703

    
704

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

    
717

    
718
@roles("nodes")
719
def setup_nfs_clients():
720
    if env.host == env.env.pithos.ip:
721
        return
722

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

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

    
740

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

    
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
        "oa2_secret": env.env.oa2_secret,
788
        }
789
    custom = customize_settings_from_tmpl(tmpl, replace)
790
    try_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
    try_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 head")
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-haskell")
826
    install_package("ganeti-htools")
827
    install_package("snf-ganeti")
828
    try_run("mkdir -p /srv/ganeti/file-storage/")
829
    cmd = """
830
cat <<EOF > /etc/ganeti/file-storage-paths
831
/srv/ganeti/file-storage
832
/srv/ganeti/shared-file-storage
833
EOF
834
"""
835
    try_run(cmd)
836

    
837

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

    
853

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

    
860

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

    
869

    
870
@roles("ganeti")
871
def enable_drbd():
872
    if env.enable_drbd:
873
        debug(env.host, " * Enabling DRBD...")
874
        install_package("drbd8-utils")
875
        try_run("modprobe drbd minor_count=255 usermode_helper=/bin/true")
876
        try_run("echo drbd minor_count=255 usermode_helper=/bin/true " +
877
                ">> /etc/modules")
878

    
879

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

    
891

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

    
904

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

    
918

    
919
@roles("master")
920
def init_cluster():
921
    debug(env.host, " * Initializing Ganeti backend...")
922
    # extra = ""
923
    # if env.enable_lvm:
924
    #     extra += " --vg-name={0} ".format(env.env.vg)
925
    # else:
926
    #     extra += " --no-lvm-storage "
927
    # if not env.enable_drbd:
928
    #     extra += " --no-drbd-storage "
929
    extra = " --no-lvm-storage --no-drbd-storage "
930
    cmd = """
931
    gnt-cluster init --enabled-hypervisors=kvm \
932
        {0} \
933
        --nic-parameters link={1},mode=bridged \
934
        --master-netdev {2} \
935
        --default-iallocator hail \
936
        --specs-nic-count min=0,max=8 \
937
        --hypervisor-parameters kvm:kernel_path=,vnc_bind_address=0.0.0.0 \
938
        --no-ssh-init --no-etc-hosts \
939
        {3}
940
    """.format(extra, env.env.common_bridge,
941
               env.env.cluster_netdev, env.env.cluster.fqdn)
942
    try_run(cmd)
943
    cmd = """gnt-cluster modify --enabled-disk-templates file,plain,ext"""
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
        "image_dir": env.env.image_dir,
965
    }
966
    custom = customize_settings_from_tmpl(tmpl, replace)
967
    try_put(custom, tmpl)
968

    
969

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

    
978

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

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

    
1001

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

1013
    ip6tables -t mangle -A PREROUTING -i br+ -p ipv6-icmp -m icmp6 \
1014
            --icmpv6-type 133 -j NFQUEUE --queue-num 43
1015
    ip6tables -t mangle -A PREROUTING -i br+ -p ipv6-icmp -m icmp6 \
1016
            --icmpv6-type 135 -j NFQUEUE --queue-num 44
1017
    """
1018
    try_run(cmd)
1019

    
1020

    
1021
@roles("ganeti")
1022
def setup_network():
1023
    debug(env.host,
1024
          "Setting up networking for Ganeti instances (nfdhcpd, etc.)...")
1025
    install_package("python-nfqueue")
1026
    install_package("nfdhcpd")
1027
    tmpl = "/etc/nfdhcpd/nfdhcpd.conf"
1028
    replace = {
1029
        "ns_node_ip": env.env.ns.ip
1030
    }
1031
    custom = customize_settings_from_tmpl(tmpl, replace)
1032
    try_put(custom, tmpl)
1033
    try_run("/etc/init.d/nfdhcpd restart")
1034

    
1035
    install_package("snf-network")
1036
    cmd = """
1037
sed -i 's/MAC_MASK.*/MAC_MASK = ff:ff:f0:00:00:00/' /etc/default/snf-network
1038
    """
1039
    try_run(cmd)
1040

    
1041

    
1042
@roles("router")
1043
def setup_router():
1044
    debug(env.host, " * Setting up internal router for NAT...")
1045
    cmd = """
1046
    echo 1 > /proc/sys/net/ipv4/ip_forward
1047
    iptables -t nat -A POSTROUTING -s {0} -o {3} -j MASQUERADE
1048
    ip addr add {1} dev {2}
1049
    ip route add {0} dev {2} src {1}
1050
    """.format(env.env.synnefo_public_network_subnet,
1051
               env.env.synnefo_public_network_gateway,
1052
               env.env.common_bridge, env.env.public_iface)
1053
    try_run(cmd)
1054

    
1055

    
1056
@roles("cyclades")
1057
def cyclades_loaddata():
1058
    debug(env.host, " * Loading initial data for cyclades...")
1059
    try_run("snf-manage flavor-create %s %s %s %s" % (env.env.flavor_cpu,
1060
                                                      env.env.flavor_ram,
1061
                                                      env.env.flavor_disk,
1062
                                                      env.env.flavor_storage))
1063
    #run("snf-manage loaddata flavors")
1064

    
1065

    
1066
@roles("ganeti", "stats")
1067
def setup_collectd():
1068
    install_package("collectd")
1069
    tmpl = "/etc/collectd/collectd.conf"
1070
    replace = {}
1071
    custom = customize_settings_from_tmpl(tmpl, replace)
1072
    try_put(custom, tmpl, mode=0644)
1073

    
1074

    
1075
@roles("ganeti")
1076
def setup_ganeti_collectd():
1077
    setup_collectd()
1078

    
1079
    tmpl = "/etc/collectd/passwd"
1080
    replace = {}
1081
    custom = customize_settings_from_tmpl(tmpl, replace)
1082
    try_put(custom, tmpl, mode=0644)
1083

    
1084
    tmpl = "/etc/collectd/synnefo-ganeti.conf"
1085
    replace = {
1086
        "STATS": env.env.stats.fqdn,
1087
        }
1088
    custom = customize_settings_from_tmpl(tmpl, replace)
1089
    try_put(custom, tmpl, mode=0644)
1090

    
1091
    try_run("/etc/init.d/collectd restart")
1092

    
1093

    
1094
@roles("stats")
1095
def setup_stats_collectd():
1096
    setup_collectd()
1097
    tmpl = "/etc/collectd/synnefo-stats.conf"
1098

    
1099
    replace = {
1100
        "STATS": env.env.stats.fqdn,
1101
        }
1102
    custom = customize_settings_from_tmpl(tmpl, replace)
1103
    try_put(custom, tmpl, mode=0644)
1104
    try_run("/etc/init.d/collectd restart")
1105

    
1106

    
1107
@roles("stats")
1108
def setup_stats():
1109
    debug(env.host, "Setting up snf-stats-app...")
1110
    setup_stats_collectd()
1111
    setup_gunicorn()
1112
    setup_apache()
1113
    setup_webproject()
1114
    install_package("snf-stats-app")
1115
    cmd = """
1116
    mkdir /var/cache/snf-stats-app/
1117
    chown www-data:www-data /var/cache/snf-stats-app/
1118
    """
1119
    try_run(cmd)
1120
    tmpl = "/etc/synnefo/stats.conf"
1121

    
1122
    replace = {
1123
        "STATS": env.env.stats.fqdn,
1124
        }
1125
    custom = customize_settings_from_tmpl(tmpl, replace)
1126
    try_put(custom, tmpl, mode=0644)
1127
    try_run("/etc/init.d/gunicorn restart")
1128

    
1129

    
1130
@roles("cyclades")
1131
def setup_cyclades():
1132
    debug(env.host, "Setting up snf-cyclades-app...")
1133
    with settings(hide("everything")):
1134
        try_run("ping -c1 accounts." + env.env.domain)
1135
        try_run("ping -c1 " + env.env.db.ip)
1136
        try_run("ping -c1 " + env.env.mq.ip)
1137
    setup_gunicorn()
1138
    setup_apache()
1139
    setup_webproject()
1140
    install_package("memcached")
1141
    install_package("python-memcache")
1142
    install_package("snf-pithos-backend")
1143
    install_package("kamaki")
1144
    install_package("snf-cyclades-app")
1145
    install_package("python-django-south")
1146
    tmpl = "/etc/synnefo/cyclades.conf"
1147

    
1148
    with settings(host_string=env.env.accounts.ip):
1149
        service_id, service_token = get_service_details("cyclades")
1150

    
1151
    replace = {
1152
        "ACCOUNTS": env.env.accounts.fqdn,
1153
        "CYCLADES": env.env.cyclades.fqdn,
1154
        "mq_node": env.env.mq.ip,
1155
        "db_node": env.env.db.ip,
1156
        "synnefo_user": env.env.synnefo_user,
1157
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
1158
        "synnefo_rabbitmq_passwd": env.env.synnefo_rabbitmq_passwd,
1159
        "pithos_dir": env.env.pithos_dir,
1160
        "common_bridge": env.env.common_bridge,
1161
        "HOST": env.env.cyclades.ip,
1162
        "domain": env.env.domain,
1163
        "CYCLADES_SERVICE_TOKEN": service_token,
1164
        'STATS': env.env.stats.fqdn,
1165
        'CYCLADES_NODE_IP': env.env.cyclades.ip,
1166
        }
1167
    custom = customize_settings_from_tmpl(tmpl, replace)
1168
    try_put(custom, tmpl, mode=0644)
1169
    try_run("/etc/init.d/gunicorn restart")
1170

    
1171
    cmd = """
1172
    sed -i 's/false/true/' /etc/default/snf-dispatcher
1173
    /etc/init.d/snf-dispatcher start
1174
    """
1175
    try_run(cmd)
1176

    
1177
    try_run("snf-manage syncdb")
1178
    try_run("snf-manage migrate --delete-ghost-migrations")
1179

    
1180

    
1181
@roles("cyclades")
1182
def get_backend_id(cluster_name="ganeti1.synnefo.deploy.local"):
1183
    backend_id = try_run("snf-manage backend-list 2>/dev/null " +
1184
                         "| grep %s | awk '{print $1}'" % cluster_name)
1185
    return backend_id
1186

    
1187

    
1188
@roles("cyclades")
1189
def add_backend():
1190
    debug(env.host,
1191
          "adding %s ganeti backend to cyclades..." % env.env.cluster.fqdn)
1192
    with settings(hide("everything")):
1193
        try_run("ping -c1 " + env.env.cluster.fqdn)
1194
    cmd = """
1195
    snf-manage backend-add --clustername={0} --user={1} --pass={2}
1196
    """.format(env.env.cluster.fqdn, env.env.synnefo_user,
1197
               env.env.synnefo_rapi_passwd)
1198
    try_run(cmd)
1199
    backend_id = get_backend_id(env.env.cluster.fqdn)
1200
    try_run("snf-manage backend-modify --drained=False " + backend_id)
1201

    
1202

    
1203
@roles("cyclades")
1204
def pin_user_to_backend(user_email):
1205
    backend_id = get_backend_id(env.env.cluster.fqdn)
1206
    # pin user to backend
1207
    cmd = """
1208
cat <<EOF >> /etc/synnefo/cyclades.conf
1209

1210
BACKEND_PER_USER = {
1211
  '{0}': {1},
1212
}
1213

1214
EOF
1215
/etc/init.d/gunicorn restart
1216
""".format(user_email, backend_id)
1217
    try_run(cmd)
1218

    
1219

    
1220
@roles("cyclades")
1221
def add_pools():
1222
    debug(env.host,
1223
          " * Creating pools of resources (brigdes, mac prefixes) " +
1224
          "in cyclades...")
1225
    try_run("snf-manage pool-create --type=mac-prefix " +
1226
            "--base=aa:00:0 --size=65536")
1227
    try_run("snf-manage pool-create --type=bridge --base=prv --size=20")
1228

    
1229

    
1230
@roles("accounts", "cyclades", "pithos")
1231
def export_services():
1232
    debug(env.host, " * Exporting services...")
1233
    host = env.host
1234
    services = []
1235
    if host == env.env.cyclades.ip:
1236
        services.append("cyclades")
1237
    if host == env.env.pithos.ip:
1238
        services.append("pithos")
1239
    if host == env.env.accounts.ip:
1240
        services.append("astakos")
1241
    for service in services:
1242
        filename = "%s_services.json" % service
1243
        cmd = "snf-manage service-export-%s > %s" % (service, filename)
1244
        run(cmd)
1245
        try_get(filename, filename + ".local")
1246

    
1247

    
1248
@roles("accounts")
1249
def import_services():
1250
    debug(env.host, " * Registering services to astakos...")
1251
    for service in ["cyclades", "pithos", "astakos"]:
1252
        filename = "%s_services.json" % service
1253
        try_put(filename + ".local", filename)
1254
        cmd = "snf-manage service-import --json=%s" % filename
1255
        run(cmd)
1256

    
1257
    debug(env.host, " * Setting default quota...")
1258
    cmd = """
1259
    snf-manage resource-modify --default-quota 40G pithos.diskspace
1260
    snf-manage resource-modify --default-quota 2 astakos.pending_app
1261
    snf-manage resource-modify --default-quota 4 cyclades.vm
1262
    snf-manage resource-modify --default-quota 40G cyclades.disk
1263
    snf-manage resource-modify --default-quota 16G cyclades.total_ram
1264
    snf-manage resource-modify --default-quota 8G cyclades.ram
1265
    snf-manage resource-modify --default-quota 32 cyclades.total_cpu
1266
    snf-manage resource-modify --default-quota 16 cyclades.cpu
1267
    snf-manage resource-modify --default-quota 4 cyclades.network.private
1268
    snf-manage resource-modify --default-quota 4 cyclades.floating_ip
1269
    """
1270
    try_run(cmd)
1271

    
1272

    
1273
@roles("accounts")
1274
def set_user_quota():
1275
    debug(env.host, " * Setting user quota...")
1276
    cmd = """
1277
    snf-manage user-modify -f --all --base-quota pithos.diskspace 40G
1278
    snf-manage user-modify -f --all --base-quota astakos.pending_app 2
1279
    snf-manage user-modify -f --all --base-quota cyclades.vm 4
1280
    snf-manage user-modify -f --all --base-quota cyclades.disk 40G
1281
    snf-manage user-modify -f --all --base-quota cyclades.total_ram 16G
1282
    snf-manage user-modify -f --all --base-quota cyclades.ram 8G
1283
    snf-manage user-modify -f --all --base-quota cyclades.total_cpu 32
1284
    snf-manage user-modify -f --all --base-quota cyclades.cpu 16
1285
    snf-manage user-modify -f --all --base-quota cyclades.network.private 4
1286
    snf-manage user-modify -f --all --base-quota cyclades.floating_ip 4
1287
    """
1288
    try_run(cmd)
1289

    
1290

    
1291
@roles("cyclades")
1292
def add_network():
1293
    debug(env.host, " * Adding public network in cyclades...")
1294
    cmd = """
1295
    snf-manage network-create --subnet={0} --gateway={1} --public \
1296
        --dhcp=True --flavor={2} --mode=bridged --link={3} --name=Internet \
1297
        --floating-ip-pool=True
1298
    """.format(env.env.synnefo_public_network_subnet,
1299
               env.env.synnefo_public_network_gateway,
1300
               env.env.synnefo_public_network_type,
1301
               env.env.common_bridge)
1302
    try_run(cmd)
1303
    if env.env.testing_vm:
1304
        cmd = ("snf-manage network-create --subnet6=babe::/64"
1305
               " --gateway6=babe::1 --public --flavor={0} --mode=bridged"
1306
               " --link={1} --name=IPv6PublicNetwork"
1307
               .format(env.env.synnefo_public_network_type,
1308
                       env.env.common_bridge))
1309
        try_run(cmd)
1310

    
1311

    
1312
@roles("cyclades")
1313
def setup_vncauthproxy():
1314
    debug(env.host, " * Setting up vncauthproxy...")
1315
    user = "synnefo"
1316
    salt = "$6$7FUdSvFcWAs3hfVj$"
1317
    passhash = "ZwvnvpQclTrDYWEwBvZDMRJZNgb6ZUKT1vNsh9NzUIxMpzBuGgMqYxCDTYF"\
1318
               "6OZcbunDZb88pjL2EIBnzrGMQW1"
1319
    cmd = """
1320
    mkdir /var/lib/vncauthproxy
1321
    echo '%s:%s%s' > /var/lib/vncauthproxy/users
1322
    """ % (user, salt, passhash)
1323
    try_run(cmd)
1324
    install_package("snf-vncauthproxy")
1325

    
1326

    
1327
@roles("client")
1328
def setup_kamaki():
1329
    debug(env.host, "Setting up kamaki client...")
1330
    with settings(hide("everything")):
1331
        try_run("ping -c1 accounts." + env.env.domain)
1332
        try_run("ping -c1 cyclades." + env.env.domain)
1333
        try_run("ping -c1 pithos." + env.env.domain)
1334

    
1335
    with settings(host_string=env.env.db.ip):
1336
        uid, user_auth_token, user_uuid = \
1337
            get_auth_token_from_db(env.env.user_email)
1338

    
1339
    install_package("python-progress")
1340
    install_package("kamaki")
1341
    cmd = """
1342
    kamaki config set cloud.default.url "https://{0}/astakos/identity/v2.0"
1343
    kamaki config set cloud.default.token {1}
1344
    """.format(env.env.accounts.fqdn, user_auth_token)
1345
    try_run(cmd)
1346
    try_run("kamaki container create images")
1347

    
1348

    
1349
@roles("client")
1350
def upload_image(image="debian_base.diskdump"):
1351
    debug(env.host, " * Uploading initial image to pithos...")
1352
    image = "debian_base.diskdump"
1353
    try_run("wget {0} -O /tmp/{1}".format(env.env.debian_base_url, image))
1354
    try_run("kamaki file upload --container images /tmp/{0} {0}".format(image))
1355

    
1356

    
1357
@roles("client")
1358
def register_image(image="debian_base.diskdump"):
1359
    debug(env.host, " * Register image to plankton...")
1360
    # with settings(host_string=env.env.db.ip):
1361
    #     uid, user_auth_token, user_uuid = \
1362
    #        get_auth_token_from_db(env.env.user_email)
1363

    
1364
    image_location = "/images/{0}".format(image)
1365
    cmd = """
1366
    sleep 5
1367
    kamaki image register --name="Debian Base" --location={0} --public \
1368
            --disk-format=diskdump \
1369
            --property OSFAMILY=linux --property ROOT_PARTITION=1 \
1370
            --property description="Debian Squeeze Base System" \
1371
            --property size=450M --property kernel=2.6.32 \
1372
            --property GUI="No GUI" --property sortorder=1 \
1373
            --property USERS=root --property OS=debian
1374
    """.format(image_location)
1375
    try_run(cmd)
1376

    
1377

    
1378
@roles("client")
1379
def setup_burnin():
1380
    debug(env.host, "Setting up burnin testing tool...")
1381
    install_package("kamaki")
1382
    install_package("snf-tools")
1383

    
1384

    
1385
@roles("pithos")
1386
def add_image_locally():
1387
    debug(env.host,
1388
          " * Getting image locally in order snf-image to use it directly..")
1389
    image = "debian_base.diskdump"
1390
    try_run("wget {0} -O {1}/{2}".format(
1391
            env.env.debian_base_url, env.env.image_dir, image))
1392

    
1393

    
1394
@roles("master")
1395
def gnt_instance_add(name="test"):
1396
    debug(env.host, " * Adding test instance to Ganeti...")
1397
    osp = """img_passwd=gamwtosecurity,\
1398
img_format=diskdump,img_id=debian_base,\
1399
img_properties='{"OSFAMILY":"linux"\,"ROOT_PARTITION":"1"}'"""
1400
    cmd = """
1401
    gnt-instance add  -o snf-image+default --os-parameters {0} \
1402
            -t plain --disk 0:size=1G --no-name-check --no-ip-check \
1403
            --net 0:ip=pool,network=test --no-install \
1404
            --hypervisor-parameters kvm:machine_version=pc-1.0 {1}
1405
    """.format(osp, name)
1406
    try_run(cmd)
1407

    
1408

    
1409
@roles("master")
1410
def gnt_network_add(name="test", subnet="10.0.0.0/26", gw="10.0.0.1",
1411
                    mode="bridged", link="br0"):
1412
    debug(env.host, " * Adding test network to Ganeti...")
1413
    cmd = """
1414
    gnt-network add --network={1} --gateway={2} {0}
1415
    gnt-network connect {0} {3} {4}
1416
    """.format(name, subnet, gw, mode, link)
1417
    try_run(cmd)
1418

    
1419

    
1420
@roles("ips")
1421
def test():
1422
    debug(env.host, "Testing...")
1423
    try_run("hostname && date")