Statistics
| Branch: | Tag: | Revision:

root / snf-deploy / snfdeploy / fabfile.py @ 68d6d24b

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
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 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-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
    nodes = env.env.cluster_nodes.split(",")
857
    nodes.remove(env.env.master_node)
858
    debug(env.host, " * Adding nodes to Ganeti backend...")
859
    for n in nodes:
860
        add_node(n)
861

    
862

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

    
871

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

    
881

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

    
893

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

    
906

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

    
920

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

    
948

    
949
@roles("ganeti")
950
def debootstrap():
951
    install_package("ganeti-instance-debootstrap")
952

    
953

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

    
971

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

    
980

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

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

    
1003

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

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

    
1022

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

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

    
1043

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

    
1057

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

    
1067

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

    
1076

    
1077
@roles("ganeti")
1078
def setup_ganeti_collectd():
1079
    setup_collectd()
1080

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

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

    
1093
    try_run("/etc/init.d/collectd restart")
1094

    
1095

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

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

    
1108

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

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

    
1131

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

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

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

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

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

    
1181

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

    
1188

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

    
1203

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

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

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

    
1220

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

    
1230

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

    
1248

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

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

    
1273

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

    
1291

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

    
1312

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

    
1327

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

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

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

    
1349

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

    
1357

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

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

    
1378

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

    
1385

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

    
1394

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

    
1409

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

    
1420

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