Statistics
| Branch: | Tag: | Revision:

root / snf-deploy / fabfile.py @ eaae0a32

History | View | Annotate | Download (38.4 kB)

1 0ac84a9a Dimitris Aragiorgis
from __future__ import with_statement
2 0ac84a9a Dimitris Aragiorgis
from fabric.api import *
3 0ac84a9a Dimitris Aragiorgis
from fabric.contrib.console import confirm
4 0ac84a9a Dimitris Aragiorgis
from random import choice
5 0ac84a9a Dimitris Aragiorgis
from fabric.operations import run, put
6 0ac84a9a Dimitris Aragiorgis
import re
7 0ac84a9a Dimitris Aragiorgis
import shutil, os
8 0ac84a9a Dimitris Aragiorgis
from functools import wraps
9 0ac84a9a Dimitris Aragiorgis
import imp
10 0ac84a9a Dimitris Aragiorgis
import ConfigParser
11 0ac84a9a Dimitris Aragiorgis
import sys
12 0ac84a9a Dimitris Aragiorgis
import tempfile
13 0ac84a9a Dimitris Aragiorgis
import ast
14 0ac84a9a Dimitris Aragiorgis
from snfdeploy.lib import *
15 0ac84a9a Dimitris Aragiorgis
from snfdeploy import massedit
16 0ac84a9a Dimitris Aragiorgis
17 0ac84a9a Dimitris Aragiorgis
18 0ac84a9a Dimitris Aragiorgis
def setup_env(confdir="conf", packages="packages",
19 8780d2fa Dimitris Aragiorgis
              templates="files", cluster_name="ganeti1", autoconf=False, disable_colors=False, key_inject=False):
20 0ac84a9a Dimitris Aragiorgis
    print("Loading configuration for synnefo...")
21 0ac84a9a Dimitris Aragiorgis
    print(" * Using config files under %s..." % confdir)
22 0ac84a9a Dimitris Aragiorgis
    print(" * Using %s and %s for packages and templates accordingly..." % (packages, templates))
23 0ac84a9a Dimitris Aragiorgis
24 0ac84a9a Dimitris Aragiorgis
    autoconf = ast.literal_eval(autoconf)
25 1bc6d467 Dionysis Grigoropoulos
    disable_colors = ast.literal_eval(disable_colors)
26 8780d2fa Dimitris Aragiorgis
    env.key_inject = ast.literal_eval(key_inject)
27 0ac84a9a Dimitris Aragiorgis
    conf = Conf.configure(confdir=confdir, cluster_name=cluster_name, autoconf=autoconf)
28 0ac84a9a Dimitris Aragiorgis
    env.env = Env(conf)
29 0ac84a9a Dimitris Aragiorgis
30 0ac84a9a Dimitris Aragiorgis
    env.local = autoconf
31 0ac84a9a Dimitris Aragiorgis
    env.password = env.env.password
32 0ac84a9a Dimitris Aragiorgis
    env.user = env.env.user
33 0ac84a9a Dimitris Aragiorgis
    env.shell = "/bin/bash -c"
34 0ac84a9a Dimitris Aragiorgis
35 1bc6d467 Dionysis Grigoropoulos
    if disable_colors:
36 1bc6d467 Dionysis Grigoropoulos
        disable_color()
37 1bc6d467 Dionysis Grigoropoulos
38 0ac84a9a Dimitris Aragiorgis
    if env.env.cms.hostname in [env.env.accounts.hostname, env.env.cyclades.hostname, env.env.pithos.hostname]:
39 0ac84a9a Dimitris Aragiorgis
      env.cms_pass = True
40 0ac84a9a Dimitris Aragiorgis
    else:
41 0ac84a9a Dimitris Aragiorgis
      env.cms_pass = False
42 0ac84a9a Dimitris Aragiorgis
43 0ac84a9a Dimitris Aragiorgis
    if env.env.accounts.hostname in [env.env.cyclades.hostname, env.env.pithos.hostname]:
44 0ac84a9a Dimitris Aragiorgis
      env.csrf_disable = True
45 0ac84a9a Dimitris Aragiorgis
    else:
46 0ac84a9a Dimitris Aragiorgis
      env.csrf_disable = False
47 0ac84a9a Dimitris Aragiorgis
48 0ac84a9a Dimitris Aragiorgis
49 0ac84a9a Dimitris Aragiorgis
    env.roledefs = {
50 0ac84a9a Dimitris Aragiorgis
        "nodes": env.env.ips,
51 0ac84a9a Dimitris Aragiorgis
        "ips": env.env.ips,
52 0ac84a9a Dimitris Aragiorgis
        "accounts": [env.env.accounts.ip],
53 0ac84a9a Dimitris Aragiorgis
        "cyclades": [env.env.cyclades.ip],
54 0ac84a9a Dimitris Aragiorgis
        "pithos": [env.env.pithos.ip],
55 0ac84a9a Dimitris Aragiorgis
        "cms": [env.env.cms.ip],
56 0ac84a9a Dimitris Aragiorgis
        "mq": [env.env.mq.ip],
57 0ac84a9a Dimitris Aragiorgis
        "db": [env.env.db.ip],
58 0ac84a9a Dimitris Aragiorgis
        "ns": [env.env.ns.ip],
59 0ac84a9a Dimitris Aragiorgis
        "client": [env.env.client.ip],
60 0ac84a9a Dimitris Aragiorgis
        "router": [env.env.router.ip],
61 0ac84a9a Dimitris Aragiorgis
    }
62 0ac84a9a Dimitris Aragiorgis
63 0ac84a9a Dimitris Aragiorgis
    env.enable_lvm = False
64 0ac84a9a Dimitris Aragiorgis
    env.enable_drbd = False
65 0ac84a9a Dimitris Aragiorgis
    if ast.literal_eval(env.env.create_extra_disk) and env.env.extra_disk:
66 0ac84a9a Dimitris Aragiorgis
        env.enable_lvm = True
67 0ac84a9a Dimitris Aragiorgis
        env.enable_drbd = True
68 0ac84a9a Dimitris Aragiorgis
69 0ac84a9a Dimitris Aragiorgis
    env.roledefs.update({
70 0ac84a9a Dimitris Aragiorgis
        "ganeti": env.env.cluster_ips,
71 0ac84a9a Dimitris Aragiorgis
        "master": [env.env.master.ip],
72 0ac84a9a Dimitris Aragiorgis
    })
73 0ac84a9a Dimitris Aragiorgis
74 0ac84a9a Dimitris Aragiorgis
75 0ac84a9a Dimitris Aragiorgis
def install_package(package):
76 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Installing package %s..." % package)
77 0ac84a9a Dimitris Aragiorgis
    APT_GET = "export DEBIAN_FRONTEND=noninteractive ;apt-get install -y --force-yes "
78 0ac84a9a Dimitris Aragiorgis
79 0ac84a9a Dimitris Aragiorgis
    if ast.literal_eval(env.env.use_local_packages):
80 0ac84a9a Dimitris Aragiorgis
        with settings(warn_only=True):
81 0ac84a9a Dimitris Aragiorgis
            deb = local("ls %s/%s*deb" % (env.env.packages, package))
82 0ac84a9a Dimitris Aragiorgis
            if deb:
83 0ac84a9a Dimitris Aragiorgis
                debug(env.host, " * Package %s found in %s..." % (package, env.env.packages))
84 0ac84a9a Dimitris Aragiorgis
                put(deb, "/tmp/")
85 0ac84a9a Dimitris Aragiorgis
                try_run("dpkg -i /tmp/%s*deb || " % package + APT_GET + "-f")
86 0ac84a9a Dimitris Aragiorgis
                try_run("rm /tmp/%s*deb" % package)
87 0ac84a9a Dimitris Aragiorgis
                return
88 0ac84a9a Dimitris Aragiorgis
89 0ac84a9a Dimitris Aragiorgis
    info = getattr(env.env, package)
90 0ac84a9a Dimitris Aragiorgis
    if info in ["stable", "squeeze-backports", "testing", "unstable"]:
91 0ac84a9a Dimitris Aragiorgis
        APT_GET += " -t %s %s " % (info, package)
92 0ac84a9a Dimitris Aragiorgis
    elif info:
93 0ac84a9a Dimitris Aragiorgis
        APT_GET += " %s=%s " % (package, info)
94 0ac84a9a Dimitris Aragiorgis
    else:
95 0ac84a9a Dimitris Aragiorgis
        APT_GET += package
96 0ac84a9a Dimitris Aragiorgis
97 0ac84a9a Dimitris Aragiorgis
    try_run(APT_GET)
98 0ac84a9a Dimitris Aragiorgis
99 0ac84a9a Dimitris Aragiorgis
    return
100 0ac84a9a Dimitris Aragiorgis
101 0ac84a9a Dimitris Aragiorgis
102 0ac84a9a Dimitris Aragiorgis
@roles("ns")
103 0ac84a9a Dimitris Aragiorgis
def update_ns_for_ganeti():
104 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Updating name server entries for backend %s..." % env.env.cluster.fqdn)
105 0ac84a9a Dimitris Aragiorgis
    update_arecord(env.env.cluster)
106 0ac84a9a Dimitris Aragiorgis
    update_ptrrecord(env.env.cluster)
107 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/bind9 restart")
108 0ac84a9a Dimitris Aragiorgis
109 0ac84a9a Dimitris Aragiorgis
110 0ac84a9a Dimitris Aragiorgis
@roles("ns")
111 0ac84a9a Dimitris Aragiorgis
def update_ns_for_node(node):
112 0ac84a9a Dimitris Aragiorgis
    info = env.env.nodes_info.get(node)
113 0ac84a9a Dimitris Aragiorgis
    update_arecord(info)
114 0ac84a9a Dimitris Aragiorgis
    update_ptrrecord(info)
115 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/bind9 restart")
116 0ac84a9a Dimitris Aragiorgis
117 0ac84a9a Dimitris Aragiorgis
118 0ac84a9a Dimitris Aragiorgis
@roles("ns")
119 0ac84a9a Dimitris Aragiorgis
def update_arecord(host):
120 0ac84a9a Dimitris Aragiorgis
    filename = "/etc/bind/zones/" + env.env.domain
121 0ac84a9a Dimitris Aragiorgis
    cmd = """
122 0ac84a9a Dimitris Aragiorgis
    echo '{0}' >> {1}
123 0ac84a9a Dimitris Aragiorgis
    """.format(host.arecord, filename)
124 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
125 0ac84a9a Dimitris Aragiorgis
126 0ac84a9a Dimitris Aragiorgis
127 0ac84a9a Dimitris Aragiorgis
@roles("ns")
128 0ac84a9a Dimitris Aragiorgis
def update_cnamerecord(host):
129 0ac84a9a Dimitris Aragiorgis
    filename = "/etc/bind/zones/" + env.env.domain
130 0ac84a9a Dimitris Aragiorgis
    cmd = """
131 0ac84a9a Dimitris Aragiorgis
    echo '{0}' >> {1}
132 0ac84a9a Dimitris Aragiorgis
    """.format(host.cnamerecord, filename)
133 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
134 0ac84a9a Dimitris Aragiorgis
135 0ac84a9a Dimitris Aragiorgis
136 0ac84a9a Dimitris Aragiorgis
@roles("ns")
137 0ac84a9a Dimitris Aragiorgis
def update_ptrrecord(host):
138 0ac84a9a Dimitris Aragiorgis
    filename = "/etc/bind/rev/synnefo.in-addr.arpa.zone"
139 0ac84a9a Dimitris Aragiorgis
    cmd = """
140 0ac84a9a Dimitris Aragiorgis
    echo '{0}' >> {1}
141 0ac84a9a Dimitris Aragiorgis
    """.format(host.ptrrecord, filename)
142 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
143 0ac84a9a Dimitris Aragiorgis
144 0ac84a9a Dimitris Aragiorgis
@roles("nodes")
145 0ac84a9a Dimitris Aragiorgis
def apt_get_update():
146 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "apt-get update....")
147 0ac84a9a Dimitris Aragiorgis
    try_run("apt-get update")
148 0ac84a9a Dimitris Aragiorgis
149 0ac84a9a Dimitris Aragiorgis
@roles("ns")
150 0ac84a9a Dimitris Aragiorgis
def setup_ns():
151 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up name server..")
152 0ac84a9a Dimitris Aragiorgis
    #WARNING: this should be remove after we are done
153 0ac84a9a Dimitris Aragiorgis
    # because gevent does pick randomly nameservers and google does
154 0ac84a9a Dimitris Aragiorgis
    # not know our setup!!!!!
155 0ac84a9a Dimitris Aragiorgis
    apt_get_update()
156 0ac84a9a Dimitris Aragiorgis
    install_package("bind9")
157 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/bind/named.conf.local"
158 0ac84a9a Dimitris Aragiorgis
    replace = {
159 0ac84a9a Dimitris Aragiorgis
      "domain": env.env.domain,
160 0ac84a9a Dimitris Aragiorgis
      }
161 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
162 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl)
163 0ac84a9a Dimitris Aragiorgis
164 0ac84a9a Dimitris Aragiorgis
    try_run("mkdir -p /etc/bind/zones")
165 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/bind/zones/example.com"
166 0ac84a9a Dimitris Aragiorgis
    replace = {
167 0ac84a9a Dimitris Aragiorgis
      "domain": env.env.domain,
168 0ac84a9a Dimitris Aragiorgis
      "ns_node_ip": env.env.ns.ip,
169 0ac84a9a Dimitris Aragiorgis
      }
170 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
171 0ac84a9a Dimitris Aragiorgis
    remote = "/etc/bind/zones/" + env.env.domain
172 0ac84a9a Dimitris Aragiorgis
    put(custom, remote)
173 0ac84a9a Dimitris Aragiorgis
174 0ac84a9a Dimitris Aragiorgis
    try_run("mkdir -p /etc/bind/rev")
175 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/bind/rev/synnefo.in-addr.arpa.zone"
176 0ac84a9a Dimitris Aragiorgis
    replace = {
177 0ac84a9a Dimitris Aragiorgis
      "domain": env.env.domain,
178 0ac84a9a Dimitris Aragiorgis
      }
179 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
180 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl)
181 0ac84a9a Dimitris Aragiorgis
182 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/bind/named.conf.options"
183 0ac84a9a Dimitris Aragiorgis
    replace = {
184 0ac84a9a Dimitris Aragiorgis
      "NODE_IPS": ";".join(env.env.ips),
185 0ac84a9a Dimitris Aragiorgis
      }
186 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
187 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl, mode=0644)
188 0ac84a9a Dimitris Aragiorgis
189 0ac84a9a Dimitris Aragiorgis
    for role, info in env.env.roles.iteritems():
190 0ac84a9a Dimitris Aragiorgis
        if role == "ns":
191 0ac84a9a Dimitris Aragiorgis
            continue
192 0ac84a9a Dimitris Aragiorgis
        update_cnamerecord(info)
193 0ac84a9a Dimitris Aragiorgis
    for node, info in env.env.nodes_info.iteritems():
194 0ac84a9a Dimitris Aragiorgis
        update_arecord(info)
195 0ac84a9a Dimitris Aragiorgis
        update_ptrrecord(info)
196 0ac84a9a Dimitris Aragiorgis
197 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/bind9 restart")
198 0ac84a9a Dimitris Aragiorgis
199 0ac84a9a Dimitris Aragiorgis
200 0ac84a9a Dimitris Aragiorgis
@roles("nodes")
201 0ac84a9a Dimitris Aragiorgis
def check_dhcp():
202 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Checking IPs for synnefo..")
203 0ac84a9a Dimitris Aragiorgis
    for n, info in env.env.nodes_info.iteritems():
204 df1d01d4 Dimitris Aragiorgis
        try_run("ping -c 1 " + info.ip, True)
205 0ac84a9a Dimitris Aragiorgis
206 0ac84a9a Dimitris Aragiorgis
@roles("nodes")
207 0ac84a9a Dimitris Aragiorgis
def check_dns():
208 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Checking fqdns for synnefo..")
209 0ac84a9a Dimitris Aragiorgis
    for n, info in env.env.nodes_info.iteritems():
210 df1d01d4 Dimitris Aragiorgis
        try_run("ping -c 1 " + info.fqdn, True)
211 0ac84a9a Dimitris Aragiorgis
212 0ac84a9a Dimitris Aragiorgis
    for n, info in env.env.roles.iteritems():
213 df1d01d4 Dimitris Aragiorgis
        try_run("ping -c 1 " + info.fqdn, True)
214 0ac84a9a Dimitris Aragiorgis
215 0ac84a9a Dimitris Aragiorgis
@roles("nodes")
216 0ac84a9a Dimitris Aragiorgis
def check_connectivity():
217 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Checking internet connectivity..")
218 df1d01d4 Dimitris Aragiorgis
    try_run("ping -c 1 www.google.com", True)
219 0ac84a9a Dimitris Aragiorgis
220 0ac84a9a Dimitris Aragiorgis
221 0ac84a9a Dimitris Aragiorgis
@roles("nodes")
222 0ac84a9a Dimitris Aragiorgis
def check_ssh():
223 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Checking password-less ssh..")
224 0ac84a9a Dimitris Aragiorgis
    for n, info in env.env.nodes_info.iteritems():
225 df1d01d4 Dimitris Aragiorgis
        try_run("ssh " + info.fqdn + "  date", True)
226 0ac84a9a Dimitris Aragiorgis
227 0ac84a9a Dimitris Aragiorgis
228 0ac84a9a Dimitris Aragiorgis
@roles("ips")
229 0ac84a9a Dimitris Aragiorgis
def add_keys():
230 8780d2fa Dimitris Aragiorgis
    if not env.key_inject:
231 8f5ea521 Dimitris Aragiorgis
      debug(env.host, "Skipping ssh keys injection..")
232 8780d2fa Dimitris Aragiorgis
      return
233 8780d2fa Dimitris Aragiorgis
    else:
234 8780d2fa Dimitris Aragiorgis
      debug(env.host, "Adding rsa/dsa keys..")
235 0ac84a9a Dimitris Aragiorgis
    try_run("mkdir -p /root/.ssh")
236 0ac84a9a Dimitris Aragiorgis
    cmd = """
237 0ac84a9a Dimitris Aragiorgis
for f in $(ls /root/.ssh/*); do
238 0ac84a9a Dimitris Aragiorgis
  cp $f $f.bak
239 0ac84a9a Dimitris Aragiorgis
done
240 0ac84a9a Dimitris Aragiorgis
    """
241 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
242 0ac84a9a Dimitris Aragiorgis
    files = ["authorized_keys", "id_dsa", "id_dsa.pub",
243 0ac84a9a Dimitris Aragiorgis
             "id_rsa", "id_rsa.pub"]
244 0ac84a9a Dimitris Aragiorgis
    for f in files:
245 0ac84a9a Dimitris Aragiorgis
      tmpl = "/root/.ssh/" + f
246 0ac84a9a Dimitris Aragiorgis
      replace = {}
247 0ac84a9a Dimitris Aragiorgis
      custom = customize_settings_from_tmpl(tmpl, replace)
248 c61f4b8c Dimitris Aragiorgis
      put(custom, tmpl, mode=0600)
249 0ac84a9a Dimitris Aragiorgis
250 0ac84a9a Dimitris Aragiorgis
    cmd = """
251 0ac84a9a Dimitris Aragiorgis
if [ -e /root/.ssh/authorized_keys.bak ]; then
252 0ac84a9a Dimitris Aragiorgis
  cat /root/.ssh/authorized_keys.bak >> /root/.ssh/authorized_keys
253 0ac84a9a Dimitris Aragiorgis
fi
254 0ac84a9a Dimitris Aragiorgis
    """
255 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Updating exising authorized keys..")
256 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
257 0ac84a9a Dimitris Aragiorgis
258 0ac84a9a Dimitris Aragiorgis
@roles("ips")
259 0ac84a9a Dimitris Aragiorgis
def setup_resolv_conf():
260 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Tweak /etc/resolv.conf...")
261 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/network-manager stop")
262 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/dhcp/dhclient-enter-hooks.d/nodnsupdate"
263 0ac84a9a Dimitris Aragiorgis
    replace = {}
264 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
265 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl, mode=0644)
266 0ac84a9a Dimitris Aragiorgis
    try_run("cp /etc/resolv.conf /etc/resolv.conf.bak")
267 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/resolv.conf"
268 0ac84a9a Dimitris Aragiorgis
    replace = {
269 0ac84a9a Dimitris Aragiorgis
      "domain": env.env.domain,
270 0ac84a9a Dimitris Aragiorgis
      "ns_node_ip": env.env.ns.ip,
271 0ac84a9a Dimitris Aragiorgis
      }
272 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
273 703d78d6 Dimitris Aragiorgis
    try:
274 703d78d6 Dimitris Aragiorgis
      put(custom, tmpl)
275 703d78d6 Dimitris Aragiorgis
    except:
276 703d78d6 Dimitris Aragiorgis
      pass
277 0ac84a9a Dimitris Aragiorgis
    try_run("chattr +i /etc/resolv.conf")
278 0ac84a9a Dimitris Aragiorgis
279 0ac84a9a Dimitris Aragiorgis
280 0ac84a9a Dimitris Aragiorgis
@roles("ips")
281 0ac84a9a Dimitris Aragiorgis
def setup_hosts():
282 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Tweaking /etc/hosts and ssh_config files...")
283 0ac84a9a Dimitris Aragiorgis
    try_run("echo StrictHostKeyChecking no >> /etc/ssh/ssh_config")
284 8665d8b6 Dimitris Aragiorgis
    cmd = " sed -i 's/^127.*/127.0.0.1 localhost/' /etc/hosts "
285 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
286 6e0e55ba Dimitris Aragiorgis
    host_info = env.env.ips_info[env.host]
287 6e0e55ba Dimitris Aragiorgis
    cmd = "hostname %s" % host_info.hostname
288 6e0e55ba Dimitris Aragiorgis
    try_run(cmd)
289 6e0e55ba Dimitris Aragiorgis
    cmd = "echo %s > /etc/hostname" % host_info.hostname
290 6e0e55ba Dimitris Aragiorgis
    try_run(cmd)
291 0ac84a9a Dimitris Aragiorgis
292 0ac84a9a Dimitris Aragiorgis
293 df1d01d4 Dimitris Aragiorgis
def try_run(cmd, abort=False):
294 0ac84a9a Dimitris Aragiorgis
    try:
295 0ac84a9a Dimitris Aragiorgis
      if env.local:
296 0ac84a9a Dimitris Aragiorgis
        return local(cmd, capture=True)
297 0ac84a9a Dimitris Aragiorgis
      else:
298 0ac84a9a Dimitris Aragiorgis
        return run(cmd)
299 0ac84a9a Dimitris Aragiorgis
    except:
300 0ac84a9a Dimitris Aragiorgis
      debug(env.host, "WARNING: command failed. Continuing anyway...")
301 df1d01d4 Dimitris Aragiorgis
      if abort:
302 df1d01d4 Dimitris Aragiorgis
        raise
303 0ac84a9a Dimitris Aragiorgis
304 0ac84a9a Dimitris Aragiorgis
def create_bridges():
305 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Creating bridges...")
306 0ac84a9a Dimitris Aragiorgis
    install_package("bridge-utils")
307 0ac84a9a Dimitris Aragiorgis
    cmd = """
308 0ac84a9a Dimitris Aragiorgis
    brctl addbr {0} ; ip link set {0} up
309 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.common_bridge)
310 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
311 0ac84a9a Dimitris Aragiorgis
312 0ac84a9a Dimitris Aragiorgis
313 0ac84a9a Dimitris Aragiorgis
def connect_bridges():
314 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Connecting bridges...")
315 0ac84a9a Dimitris Aragiorgis
    cmd = """
316 0ac84a9a Dimitris Aragiorgis
    brctl addif {0} {1}
317 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.common_bridge, env.env.public_iface)
318 0ac84a9a Dimitris Aragiorgis
    #try_run(cmd)
319 0ac84a9a Dimitris Aragiorgis
320 0ac84a9a Dimitris Aragiorgis
321 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
322 0ac84a9a Dimitris Aragiorgis
def setup_net_infra():
323 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setup networking infrastracture..")
324 0ac84a9a Dimitris Aragiorgis
    create_bridges()
325 0ac84a9a Dimitris Aragiorgis
    connect_bridges()
326 0ac84a9a Dimitris Aragiorgis
327 0ac84a9a Dimitris Aragiorgis
328 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
329 0ac84a9a Dimitris Aragiorgis
def setup_lvm():
330 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "create volume group %s for ganeti.." % env.env.vg)
331 0ac84a9a Dimitris Aragiorgis
    if env.enable_lvm:
332 0ac84a9a Dimitris Aragiorgis
        install_package("lvm2")
333 0ac84a9a Dimitris Aragiorgis
        cmd = """
334 0ac84a9a Dimitris Aragiorgis
        pvcreate {0}
335 0ac84a9a Dimitris Aragiorgis
        vgcreate {1} {0}
336 0ac84a9a Dimitris Aragiorgis
        """.format(env.env.extra_disk, env.env.vg)
337 0ac84a9a Dimitris Aragiorgis
        try_run(cmd)
338 0ac84a9a Dimitris Aragiorgis
339 0ac84a9a Dimitris Aragiorgis
340 0ac84a9a Dimitris Aragiorgis
def customize_settings_from_tmpl(tmpl, replace):
341 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Customizing template %s..." % tmpl)
342 0ac84a9a Dimitris Aragiorgis
    local = env.env.templates + tmpl
343 0ac84a9a Dimitris Aragiorgis
    _, custom = tempfile.mkstemp()
344 0ac84a9a Dimitris Aragiorgis
    shutil.copyfile(local, custom)
345 0ac84a9a Dimitris Aragiorgis
    for k, v in replace.iteritems():
346 0ac84a9a Dimitris Aragiorgis
        regex = "re.sub('%{0}%', '{1}', line)".format(k.upper(), v)
347 0ac84a9a Dimitris Aragiorgis
        massedit.edit_files([custom], [regex], dry_run=False)
348 0ac84a9a Dimitris Aragiorgis
349 0ac84a9a Dimitris Aragiorgis
    return custom
350 0ac84a9a Dimitris Aragiorgis
351 0ac84a9a Dimitris Aragiorgis
352 0ac84a9a Dimitris Aragiorgis
@roles("nodes")
353 0ac84a9a Dimitris Aragiorgis
def setup_apt():
354 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up apt sources...")
355 0ac84a9a Dimitris Aragiorgis
    install_package("curl")
356 0ac84a9a Dimitris Aragiorgis
    cmd = """
357 0ac84a9a Dimitris Aragiorgis
    echo 'APT::Install-Suggests "false";' >> /etc/apt/apt.conf
358 0ac84a9a Dimitris Aragiorgis
    curl -k https://dev.grnet.gr/files/apt-grnetdev.pub | apt-key add -
359 0ac84a9a Dimitris Aragiorgis
    """
360 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
361 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/apt/sources.list.d/okeanos.list"
362 0ac84a9a Dimitris Aragiorgis
    replace = {}
363 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
364 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl)
365 0ac84a9a Dimitris Aragiorgis
    apt_get_update()
366 0ac84a9a Dimitris Aragiorgis
367 0ac84a9a Dimitris Aragiorgis
368 0ac84a9a Dimitris Aragiorgis
@roles("cyclades", "cms", "pithos", "accounts")
369 0ac84a9a Dimitris Aragiorgis
def restart_services():
370 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Restarting apache2 and gunicorn...")
371 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
372 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/apache2 restart")
373 0ac84a9a Dimitris Aragiorgis
374 0ac84a9a Dimitris Aragiorgis
375 0ac84a9a Dimitris Aragiorgis
def setup_gunicorn():
376 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up gunicorn...")
377 0ac84a9a Dimitris Aragiorgis
    install_package("gunicorn")
378 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/gunicorn.d/synnefo"
379 0ac84a9a Dimitris Aragiorgis
    replace = {}
380 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
381 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl, mode=0644)
382 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
383 0ac84a9a Dimitris Aragiorgis
384 0ac84a9a Dimitris Aragiorgis
385 0ac84a9a Dimitris Aragiorgis
def setup_apache():
386 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up apache2...")
387 0ac84a9a Dimitris Aragiorgis
    host_info = env.env.ips_info[env.host]
388 0ac84a9a Dimitris Aragiorgis
    install_package("apache2")
389 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/apache2/sites-available/synnefo"
390 0ac84a9a Dimitris Aragiorgis
    replace = {
391 0ac84a9a Dimitris Aragiorgis
        "HOST": host_info.fqdn,
392 0ac84a9a Dimitris Aragiorgis
    }
393 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
394 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl)
395 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/apache2/sites-available/synnefo-ssl"
396 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
397 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl)
398 0ac84a9a Dimitris Aragiorgis
    cmd = """
399 0ac84a9a Dimitris Aragiorgis
    a2enmod ssl
400 0ac84a9a Dimitris Aragiorgis
    a2enmod rewrite
401 0ac84a9a Dimitris Aragiorgis
    a2dissite default
402 0ac84a9a Dimitris Aragiorgis
    a2ensite synnefo
403 0ac84a9a Dimitris Aragiorgis
    a2ensite synnefo-ssl
404 0ac84a9a Dimitris Aragiorgis
    a2enmod headers
405 0ac84a9a Dimitris Aragiorgis
    a2enmod proxy_http
406 0ac84a9a Dimitris Aragiorgis
    a2dismod autoindex
407 0ac84a9a Dimitris Aragiorgis
    """
408 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
409 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/apache2 restart")
410 0ac84a9a Dimitris Aragiorgis
411 0ac84a9a Dimitris Aragiorgis
412 0ac84a9a Dimitris Aragiorgis
@roles("mq")
413 0ac84a9a Dimitris Aragiorgis
def setup_mq():
414 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up RabbitMQ...")
415 0ac84a9a Dimitris Aragiorgis
    install_package("rabbitmq-server")
416 0ac84a9a Dimitris Aragiorgis
    cmd = """
417 0ac84a9a Dimitris Aragiorgis
    rabbitmqctl add_user {0} {1}
418 0ac84a9a Dimitris Aragiorgis
    rabbitmqctl set_permissions {0} ".*" ".*" ".*"
419 0ac84a9a Dimitris Aragiorgis
    rabbitmqctl delete_user guest
420 0ac84a9a Dimitris Aragiorgis
    rabbitmqctl set_user_tags {0} administrator
421 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.synnefo_user, env.env.synnefo_rabbitmq_passwd)
422 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
423 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/rabbitmq-server restart")
424 0ac84a9a Dimitris Aragiorgis
425 0ac84a9a Dimitris Aragiorgis
426 0ac84a9a Dimitris Aragiorgis
@roles("db")
427 108fbde6 Dimitris Aragiorgis
def allow_access_in_db(ip, user="all", method="md5"):
428 0ac84a9a Dimitris Aragiorgis
    cmd = """
429 108fbde6 Dimitris Aragiorgis
    echo host all {0} {1}/32 {2} >> /etc/postgresql/8.4/main/pg_hba.conf
430 108fbde6 Dimitris Aragiorgis
    """.format(user, ip, method)
431 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
432 61aedf5a Christos Stavrakakis
    cmd = """
433 561d42ec Christos Stavrakakis
    sed -i 's/\(host.*127.0.0.1.*\)md5/\\1trust/' /etc/postgresql/8.4/main/pg_hba.conf
434 61aedf5a Christos Stavrakakis
    """
435 61aedf5a Christos Stavrakakis
    try_run(cmd)
436 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/postgresql restart")
437 0ac84a9a Dimitris Aragiorgis
438 0ac84a9a Dimitris Aragiorgis
@roles("db")
439 0ac84a9a Dimitris Aragiorgis
def setup_db():
440 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up DataBase server...")
441 0ac84a9a Dimitris Aragiorgis
    install_package("postgresql")
442 0ac84a9a Dimitris Aragiorgis
443 0ac84a9a Dimitris Aragiorgis
    tmpl = "/tmp/db-init.psql"
444 0ac84a9a Dimitris Aragiorgis
    replace = {
445 0ac84a9a Dimitris Aragiorgis
        "synnefo_user": env.env.synnefo_user,
446 0ac84a9a Dimitris Aragiorgis
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
447 0ac84a9a Dimitris Aragiorgis
        }
448 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
449 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl)
450 0ac84a9a Dimitris Aragiorgis
    cmd = 'su - postgres -c "psql -w -f %s" ' % tmpl
451 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
452 0ac84a9a Dimitris Aragiorgis
    cmd = """
453 0ac84a9a Dimitris Aragiorgis
    echo "listen_addresses = '*'" >> /etc/postgresql/8.4/main/postgresql.conf
454 0ac84a9a Dimitris Aragiorgis
    """
455 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
456 0ac84a9a Dimitris Aragiorgis
457 bd9b6f21 Christos Stavrakakis
    if env.env.testing_vm:
458 bd9b6f21 Christos Stavrakakis
        cmd = """
459 bd9b6f21 Christos Stavrakakis
        echo "fsync=off\nsynchronous_commit=off\nfull_page_writes=off" >> /etc/postgresql/8.4/main/postgresql.conf
460 bd9b6f21 Christos Stavrakakis
        """
461 bd9b6f21 Christos Stavrakakis
        try_run(cmd)
462 bd9b6f21 Christos Stavrakakis
463 108fbde6 Dimitris Aragiorgis
    allow_access_in_db(env.host, "all", "trust")
464 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/postgresql restart")
465 0ac84a9a Dimitris Aragiorgis
466 0ac84a9a Dimitris Aragiorgis
467 0ac84a9a Dimitris Aragiorgis
@roles("db")
468 0ac84a9a Dimitris Aragiorgis
def destroy_db():
469 0ac84a9a Dimitris Aragiorgis
    try_run("""su - postgres -c ' psql -w -c "drop database snf_apps" '""")
470 0ac84a9a Dimitris Aragiorgis
    try_run("""su - postgres -c ' psql -w -c "drop database snf_pithos" '""")
471 0ac84a9a Dimitris Aragiorgis
472 0ac84a9a Dimitris Aragiorgis
473 0ac84a9a Dimitris Aragiorgis
def setup_webproject():
474 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up snf-webproject...")
475 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
476 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 " + env.env.db.ip)
477 0ac84a9a Dimitris Aragiorgis
    setup_common()
478 0ac84a9a Dimitris Aragiorgis
    install_package("snf-webproject")
479 0ac84a9a Dimitris Aragiorgis
    install_package("python-psycopg2")
480 0ac84a9a Dimitris Aragiorgis
    install_package("python-gevent")
481 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/synnefo/webproject.conf"
482 0ac84a9a Dimitris Aragiorgis
    replace = {
483 0ac84a9a Dimitris Aragiorgis
        "synnefo_user": env.env.synnefo_user,
484 0ac84a9a Dimitris Aragiorgis
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
485 0ac84a9a Dimitris Aragiorgis
        "db_node": env.env.db.ip,
486 0ac84a9a Dimitris Aragiorgis
        "domain": env.env.domain,
487 0ac84a9a Dimitris Aragiorgis
    }
488 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
489 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl, mode=0644)
490 1d8cf97d Dimitris Aragiorgis
    with settings(host_string=env.env.db.ip):
491 0ac84a9a Dimitris Aragiorgis
        host_info = env.env.ips_info[env.host]
492 108fbde6 Dimitris Aragiorgis
        allow_access_in_db(host_info.ip, "all", "trust")
493 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
494 0ac84a9a Dimitris Aragiorgis
495 0ac84a9a Dimitris Aragiorgis
496 0ac84a9a Dimitris Aragiorgis
def setup_common():
497 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up snf-common...")
498 0ac84a9a Dimitris Aragiorgis
    host_info = env.env.ips_info[env.host]
499 0ac84a9a Dimitris Aragiorgis
    install_package("python-objpool")
500 0ac84a9a Dimitris Aragiorgis
    install_package("snf-common")
501 0ac84a9a Dimitris Aragiorgis
    install_package("python-astakosclient")
502 0ac84a9a Dimitris Aragiorgis
    install_package("snf-django-lib")
503 0ac84a9a Dimitris Aragiorgis
    install_package("snf-branding")
504 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/synnefo/common.conf"
505 0ac84a9a Dimitris Aragiorgis
    replace = {
506 0ac84a9a Dimitris Aragiorgis
        #FIXME:
507 0ac84a9a Dimitris Aragiorgis
        "EMAIL_SUBJECT_PREFIX": env.host,
508 0ac84a9a Dimitris Aragiorgis
        "domain": env.env.domain,
509 0ac84a9a Dimitris Aragiorgis
        "HOST": host_info.fqdn,
510 0ac84a9a Dimitris Aragiorgis
    }
511 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
512 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl, mode=0644)
513 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
514 0ac84a9a Dimitris Aragiorgis
515 0ac84a9a Dimitris Aragiorgis
@roles("accounts")
516 0ac84a9a Dimitris Aragiorgis
def astakos_loaddata():
517 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Loading initial data to astakos...")
518 0ac84a9a Dimitris Aragiorgis
    cmd = """
519 0ac84a9a Dimitris Aragiorgis
    snf-manage loaddata groups
520 0ac84a9a Dimitris Aragiorgis
    """
521 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
522 0ac84a9a Dimitris Aragiorgis
523 0ac84a9a Dimitris Aragiorgis
524 0ac84a9a Dimitris Aragiorgis
@roles("accounts")
525 0ac84a9a Dimitris Aragiorgis
def astakos_register_services():
526 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Register services in astakos...")
527 0ac84a9a Dimitris Aragiorgis
    cmd = """
528 0ac84a9a Dimitris Aragiorgis
    snf-manage component-add "home" https://{0} home-icon.png
529 0ac84a9a Dimitris Aragiorgis
    snf-manage component-add "cyclades" https://{1}/cyclades/ui/
530 0ac84a9a Dimitris Aragiorgis
    snf-manage component-add "pithos" https://{2}/pithos/ui/
531 0ac84a9a Dimitris Aragiorgis
    snf-manage component-add "astakos" https://{3}/astakos/ui/
532 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.cms.fqdn, env.env.cyclades.fqdn, env.env.pithos.fqdn, env.env.accounts.fqdn)
533 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
534 0ac84a9a Dimitris Aragiorgis
    import_service("astakos")
535 0ac84a9a Dimitris Aragiorgis
    import_service("pithos")
536 0ac84a9a Dimitris Aragiorgis
    import_service("cyclades")
537 0ac84a9a Dimitris Aragiorgis
    tmpl = "/tmp/resources.json"
538 0ac84a9a Dimitris Aragiorgis
    replace = {}
539 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
540 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl)
541 0ac84a9a Dimitris Aragiorgis
    try_run("snf-manage resource-import --json %s" % tmpl)
542 0ac84a9a Dimitris Aragiorgis
    cmd = """
543 0ac84a9a Dimitris Aragiorgis
    snf-manage resource-modify --limit 40G pithos.diskspace
544 0ac84a9a Dimitris Aragiorgis
    snf-manage resource-modify --limit 2 astakos.pending_app
545 0ac84a9a Dimitris Aragiorgis
    snf-manage resource-modify --limit 4 cyclades.vm
546 0ac84a9a Dimitris Aragiorgis
    snf-manage resource-modify --limit 40G cyclades.disk
547 0ac84a9a Dimitris Aragiorgis
    snf-manage resource-modify --limit 8G cyclades.ram
548 0ac84a9a Dimitris Aragiorgis
    snf-manage resource-modify --limit 16 cyclades.cpu
549 0ac84a9a Dimitris Aragiorgis
    snf-manage resource-modify --limit 4 cyclades.network.private
550 0ac84a9a Dimitris Aragiorgis
    """
551 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
552 0ac84a9a Dimitris Aragiorgis
553 0ac84a9a Dimitris Aragiorgis
554 0ac84a9a Dimitris Aragiorgis
@roles("accounts")
555 0ac84a9a Dimitris Aragiorgis
def add_user():
556 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * adding user %s to astakos..." % env.env.user_email)
557 0ac84a9a Dimitris Aragiorgis
    email=env.env.user_email
558 0ac84a9a Dimitris Aragiorgis
    name=env.env.user_name
559 0ac84a9a Dimitris Aragiorgis
    lastname=env.env.user_lastname
560 0ac84a9a Dimitris Aragiorgis
    passwd=env.env.user_passwd
561 0ac84a9a Dimitris Aragiorgis
    cmd = """
562 0ac84a9a Dimitris Aragiorgis
    snf-manage user-add {0} {1} {2}
563 0ac84a9a Dimitris Aragiorgis
    """.format(email, name, lastname)
564 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
565 1d8cf97d Dimitris Aragiorgis
    with settings(host_string=env.env.db.ip):
566 0ac84a9a Dimitris Aragiorgis
        uid, user_auth_token, user_uuid = get_auth_token_from_db(email)
567 0ac84a9a Dimitris Aragiorgis
    cmd = """
568 0ac84a9a Dimitris Aragiorgis
    snf-manage user-modify --password {0} {1}
569 0ac84a9a Dimitris Aragiorgis
    """.format(passwd, uid)
570 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
571 0ac84a9a Dimitris Aragiorgis
572 0ac84a9a Dimitris Aragiorgis
573 0ac84a9a Dimitris Aragiorgis
@roles("accounts")
574 0ac84a9a Dimitris Aragiorgis
def activate_user(user_email=None):
575 0ac84a9a Dimitris Aragiorgis
    if not user_email:
576 0ac84a9a Dimitris Aragiorgis
      user_email = env.env.user_email
577 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Activate user %s..." % user_email)
578 1d8cf97d Dimitris Aragiorgis
    with settings(host_string=env.env.db.ip):
579 0ac84a9a Dimitris Aragiorgis
        uid, user_auth_token, user_uuid = get_auth_token_from_db(user_email)
580 0ac84a9a Dimitris Aragiorgis
581 0ac84a9a Dimitris Aragiorgis
    cmd = """
582 0ac84a9a Dimitris Aragiorgis
    snf-manage user-modify --verify {0}
583 0ac84a9a Dimitris Aragiorgis
    snf-manage user-modify --accept {0}
584 0ac84a9a Dimitris Aragiorgis
    """.format(uid)
585 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
586 0ac84a9a Dimitris Aragiorgis
587 0ac84a9a Dimitris Aragiorgis
@roles("accounts")
588 0ac84a9a Dimitris Aragiorgis
def setup_astakos():
589 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up snf-astakos-app...")
590 0ac84a9a Dimitris Aragiorgis
    setup_gunicorn()
591 0ac84a9a Dimitris Aragiorgis
    setup_apache()
592 0ac84a9a Dimitris Aragiorgis
    setup_webproject()
593 0ac84a9a Dimitris Aragiorgis
    install_package("python-django-south")
594 0ac84a9a Dimitris Aragiorgis
    install_package("snf-astakos-app")
595 0ac84a9a Dimitris Aragiorgis
    install_package("kamaki")
596 0ac84a9a Dimitris Aragiorgis
597 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/synnefo/astakos.conf"
598 0ac84a9a Dimitris Aragiorgis
    replace = {
599 0ac84a9a Dimitris Aragiorgis
      "ACCOUNTS": env.env.accounts.fqdn,
600 0ac84a9a Dimitris Aragiorgis
      "domain": env.env.domain,
601 0ac84a9a Dimitris Aragiorgis
      "CYCLADES": env.env.cyclades.fqdn,
602 0ac84a9a Dimitris Aragiorgis
      "PITHOS": env.env.pithos.fqdn,
603 0ac84a9a Dimitris Aragiorgis
    }
604 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
605 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl, mode=0644)
606 0ac84a9a Dimitris Aragiorgis
    if env.csrf_disable:
607 0ac84a9a Dimitris Aragiorgis
      cmd = """
608 0ac84a9a Dimitris Aragiorgis
cat <<EOF >> /etc/synnefo/astakos.conf
609 0ac84a9a Dimitris Aragiorgis
try:
610 0ac84a9a Dimitris Aragiorgis
  MIDDLEWARE_CLASSES.remove('django.middleware.csrf.CsrfViewMiddleware')
611 0ac84a9a Dimitris Aragiorgis
except:
612 0ac84a9a Dimitris Aragiorgis
  pass
613 0ac84a9a Dimitris Aragiorgis
EOF
614 0ac84a9a Dimitris Aragiorgis
"""
615 0ac84a9a Dimitris Aragiorgis
      try_run(cmd)
616 0ac84a9a Dimitris Aragiorgis
617 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
618 0ac84a9a Dimitris Aragiorgis
619 0ac84a9a Dimitris Aragiorgis
    cmd = """
620 0ac84a9a Dimitris Aragiorgis
    snf-manage syncdb --noinput
621 0ac84a9a Dimitris Aragiorgis
    snf-manage migrate im --delete-ghost-migrations
622 0ac84a9a Dimitris Aragiorgis
    snf-manage migrate quotaholder_app
623 0ac84a9a Dimitris Aragiorgis
    """
624 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
625 0ac84a9a Dimitris Aragiorgis
626 0ac84a9a Dimitris Aragiorgis
def import_service(service):
627 0ac84a9a Dimitris Aragiorgis
    tmpl = "/tmp/%s.json" % service
628 0ac84a9a Dimitris Aragiorgis
    replace = {
629 0ac84a9a Dimitris Aragiorgis
      "DOMAIN": env.env.domain,
630 0ac84a9a Dimitris Aragiorgis
      }
631 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
632 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl)
633 0ac84a9a Dimitris Aragiorgis
    try_run("snf-manage service-import --json %s" % tmpl)
634 0ac84a9a Dimitris Aragiorgis
635 0ac84a9a Dimitris Aragiorgis
@roles("accounts")
636 0ac84a9a Dimitris Aragiorgis
def get_service_details(service="pithos"):
637 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Getting registered details for %s service..." % service)
638 0ac84a9a Dimitris Aragiorgis
    result = try_run("snf-manage component-list")
639 0ac84a9a Dimitris Aragiorgis
    r = re.compile(r".*%s.*" % service, re.M)
640 0ac84a9a Dimitris Aragiorgis
    service_id, _, _, service_token = r.search(result).group().split()
641 0ac84a9a Dimitris Aragiorgis
    # print("%s: %s %s" % (service, service_id, service_token))
642 0ac84a9a Dimitris Aragiorgis
    return (service_id, service_token)
643 0ac84a9a Dimitris Aragiorgis
644 0ac84a9a Dimitris Aragiorgis
645 0ac84a9a Dimitris Aragiorgis
@roles("db")
646 0ac84a9a Dimitris Aragiorgis
def get_auth_token_from_db(user_email=None):
647 0ac84a9a Dimitris Aragiorgis
    if not user_email:
648 0ac84a9a Dimitris Aragiorgis
        user_email=env.env.user_email
649 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Getting authentication token and uuid for user %s..." % user_email)
650 0ac84a9a Dimitris Aragiorgis
    cmd = """
651 0ac84a9a Dimitris Aragiorgis
    echo "select id, auth_token, uuid, email from auth_user, im_astakosuser where auth_user.id = im_astakosuser.user_ptr_id and auth_user.email = '{0}';" > /tmp/psqlcmd
652 0ac84a9a Dimitris Aragiorgis
    su - postgres -c  "psql -w -d snf_apps -f /tmp/psqlcmd"
653 0ac84a9a Dimitris Aragiorgis
    """.format(user_email)
654 0ac84a9a Dimitris Aragiorgis
655 0ac84a9a Dimitris Aragiorgis
    result = try_run(cmd)
656 0ac84a9a Dimitris Aragiorgis
    r = re.compile(r"(\d+)[ |]*(\S+)[ |]*(\S+)[ |]*" + user_email, re.M)
657 0ac84a9a Dimitris Aragiorgis
    match = r.search(result)
658 0ac84a9a Dimitris Aragiorgis
    uid, user_auth_token, user_uuid = match.groups()
659 0ac84a9a Dimitris Aragiorgis
    # print("%s: %s %s %s" % ( user_email, uid, user_auth_token, user_uuid))
660 0ac84a9a Dimitris Aragiorgis
661 0ac84a9a Dimitris Aragiorgis
    return (uid, user_auth_token, user_uuid)
662 0ac84a9a Dimitris Aragiorgis
663 0ac84a9a Dimitris Aragiorgis
664 0ac84a9a Dimitris Aragiorgis
@roles("cms")
665 0ac84a9a Dimitris Aragiorgis
def cms_loaddata():
666 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Loading cms initial data...")
667 0ac84a9a Dimitris Aragiorgis
    if env.cms_pass:
668 0ac84a9a Dimitris Aragiorgis
      debug(env.host, "Aborting. Prerequisites not met.")
669 0ac84a9a Dimitris Aragiorgis
      return
670 0ac84a9a Dimitris Aragiorgis
    tmpl = "/tmp/sites.json"
671 0ac84a9a Dimitris Aragiorgis
    replace = {}
672 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
673 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl)
674 0ac84a9a Dimitris Aragiorgis
675 0ac84a9a Dimitris Aragiorgis
    tmpl = "/tmp/page.json"
676 0ac84a9a Dimitris Aragiorgis
    replace = {}
677 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
678 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl)
679 0ac84a9a Dimitris Aragiorgis
680 0ac84a9a Dimitris Aragiorgis
    cmd = """
681 0ac84a9a Dimitris Aragiorgis
    snf-manage loaddata /tmp/sites.json
682 0ac84a9a Dimitris Aragiorgis
    snf-manage loaddata /tmp/page.json
683 0ac84a9a Dimitris Aragiorgis
    snf-manage createsuperuser --username=admin --email=admin@{0} --noinput
684 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.domain)
685 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
686 0ac84a9a Dimitris Aragiorgis
687 0ac84a9a Dimitris Aragiorgis
688 0ac84a9a Dimitris Aragiorgis
@roles("cms")
689 0ac84a9a Dimitris Aragiorgis
def setup_cms():
690 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up cms...")
691 0ac84a9a Dimitris Aragiorgis
    if env.cms_pass:
692 0ac84a9a Dimitris Aragiorgis
      debug(env.host, "Aborting. Prerequisites not met.")
693 0ac84a9a Dimitris Aragiorgis
      return
694 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
695 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 accounts." + env.env.domain)
696 0ac84a9a Dimitris Aragiorgis
    setup_gunicorn()
697 0ac84a9a Dimitris Aragiorgis
    setup_apache()
698 0ac84a9a Dimitris Aragiorgis
    setup_webproject()
699 0ac84a9a Dimitris Aragiorgis
    install_package("snf-cloudcms")
700 0ac84a9a Dimitris Aragiorgis
701 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/synnefo/cms.conf"
702 0ac84a9a Dimitris Aragiorgis
    replace = {
703 0ac84a9a Dimitris Aragiorgis
        "ACCOUNTS": env.env.accounts.fqdn,
704 0ac84a9a Dimitris Aragiorgis
        }
705 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
706 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl, mode=0644)
707 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
708 0ac84a9a Dimitris Aragiorgis
709 0ac84a9a Dimitris Aragiorgis
710 0ac84a9a Dimitris Aragiorgis
    cmd = """
711 0ac84a9a Dimitris Aragiorgis
    snf-manage syncdb
712 0ac84a9a Dimitris Aragiorgis
    snf-manage migrate --delete-ghost-migrations
713 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.domain)
714 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
715 0ac84a9a Dimitris Aragiorgis
716 0ac84a9a Dimitris Aragiorgis
717 0ac84a9a Dimitris Aragiorgis
def setup_nfs_dirs():
718 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Creating NFS mount point for pithos and ganeti...")
719 0ac84a9a Dimitris Aragiorgis
    cmd = """
720 0ac84a9a Dimitris Aragiorgis
    mkdir -p {0}
721 0ac84a9a Dimitris Aragiorgis
    cd {0}
722 0ac84a9a Dimitris Aragiorgis
    mkdir -p data
723 0ac84a9a Dimitris Aragiorgis
    chown www-data:www-data data
724 0ac84a9a Dimitris Aragiorgis
    chmod g+ws data
725 0ac84a9a Dimitris Aragiorgis
    mkdir -p /srv/okeanos
726 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.pithos_dir)
727 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
728 0ac84a9a Dimitris Aragiorgis
729 0ac84a9a Dimitris Aragiorgis
730 0ac84a9a Dimitris Aragiorgis
@roles("nodes")
731 0ac84a9a Dimitris Aragiorgis
def setup_nfs_clients():
732 1d8cf97d Dimitris Aragiorgis
    if env.host == env.env.pithos.ip:
733 0ac84a9a Dimitris Aragiorgis
      return
734 0ac84a9a Dimitris Aragiorgis
735 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Mounting pithos NFS mount point...")
736 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
737 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 " + env.env.pithos.hostname)
738 0ac84a9a Dimitris Aragiorgis
    install_package("nfs-common")
739 0ac84a9a Dimitris Aragiorgis
    for d in [env.env.pithos_dir, "/srv/okeanos"]:
740 0ac84a9a Dimitris Aragiorgis
      try_run("mkdir -p " + d)
741 0ac84a9a Dimitris Aragiorgis
      cmd = """
742 0ac84a9a Dimitris Aragiorgis
      echo "{0}:/{1} {2}  nfs4 defaults,rw,noatime,nodiratime,intr,rsize=1048576,wsize=1048576,noacl" >> /etc/fstab
743 0ac84a9a Dimitris Aragiorgis
      """.format(env.env.pithos.hostname, os.path.basename(d), d)
744 0ac84a9a Dimitris Aragiorgis
      try_run(cmd)
745 0ac84a9a Dimitris Aragiorgis
      try_run("mount " + d)
746 0ac84a9a Dimitris Aragiorgis
747 0ac84a9a Dimitris Aragiorgis
748 0ac84a9a Dimitris Aragiorgis
@roles("pithos")
749 0ac84a9a Dimitris Aragiorgis
def setup_nfs_server():
750 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up NFS server for pithos...")
751 0ac84a9a Dimitris Aragiorgis
    setup_nfs_dirs()
752 0ac84a9a Dimitris Aragiorgis
    install_package("nfs-kernel-server")
753 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/exports"
754 0ac84a9a Dimitris Aragiorgis
    replace = {
755 0ac84a9a Dimitris Aragiorgis
      "pithos_dir": env.env.pithos_dir,
756 0ac84a9a Dimitris Aragiorgis
      "srv": os.path.dirname(env.env.pithos_dir),
757 0ac84a9a Dimitris Aragiorgis
      "subnet": env.env.subnet
758 0ac84a9a Dimitris Aragiorgis
      }
759 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
760 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl)
761 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/nfs-kernel-server restart")
762 0ac84a9a Dimitris Aragiorgis
763 0ac84a9a Dimitris Aragiorgis
764 0ac84a9a Dimitris Aragiorgis
@roles("pithos")
765 0ac84a9a Dimitris Aragiorgis
def setup_pithos():
766 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up snf-pithos-app...")
767 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
768 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 accounts." + env.env.domain)
769 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 " + env.env.db.ip)
770 0ac84a9a Dimitris Aragiorgis
    setup_gunicorn()
771 0ac84a9a Dimitris Aragiorgis
    setup_apache()
772 0ac84a9a Dimitris Aragiorgis
    setup_webproject()
773 0ac84a9a Dimitris Aragiorgis
774 1d8cf97d Dimitris Aragiorgis
    with settings(host_string=env.env.accounts.ip):
775 0ac84a9a Dimitris Aragiorgis
        service_id, service_token = get_service_details("pithos")
776 0ac84a9a Dimitris Aragiorgis
777 0ac84a9a Dimitris Aragiorgis
    install_package("kamaki")
778 0ac84a9a Dimitris Aragiorgis
    install_package("snf-pithos-backend")
779 0ac84a9a Dimitris Aragiorgis
    install_package("snf-pithos-app")
780 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/synnefo/pithos.conf"
781 0ac84a9a Dimitris Aragiorgis
    replace = {
782 0ac84a9a Dimitris Aragiorgis
        "ACCOUNTS": env.env.accounts.fqdn,
783 0ac84a9a Dimitris Aragiorgis
        "PITHOS": env.env.pithos.fqdn,
784 0ac84a9a Dimitris Aragiorgis
        "db_node": env.env.db.ip,
785 0ac84a9a Dimitris Aragiorgis
        "synnefo_user": env.env.synnefo_user,
786 0ac84a9a Dimitris Aragiorgis
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
787 0ac84a9a Dimitris Aragiorgis
        "pithos_dir": env.env.pithos_dir,
788 0ac84a9a Dimitris Aragiorgis
        "PITHOS_SERVICE_TOKEN": service_token,
789 0ac84a9a Dimitris Aragiorgis
        "proxy": env.env.pithos.hostname == env.env.accounts.hostname
790 0ac84a9a Dimitris Aragiorgis
        }
791 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
792 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl, mode=0644)
793 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
794 0ac84a9a Dimitris Aragiorgis
795 0ac84a9a Dimitris Aragiorgis
    install_package("snf-pithos-webclient")
796 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/synnefo/webclient.conf"
797 0ac84a9a Dimitris Aragiorgis
    replace = {
798 0ac84a9a Dimitris Aragiorgis
        "ACCOUNTS": env.env.accounts.fqdn,
799 0ac84a9a Dimitris Aragiorgis
        "PITHOS_UI_CLOUDBAR_ACTIVE_SERVICE": service_id,
800 0ac84a9a Dimitris Aragiorgis
        }
801 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
802 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl, mode=0644)
803 0ac84a9a Dimitris Aragiorgis
804 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
805 0ac84a9a Dimitris Aragiorgis
    #TOFIX: the previous command lets pithos-backend create blocks and maps
806 0ac84a9a Dimitris Aragiorgis
    #       with root owner
807 0ac84a9a Dimitris Aragiorgis
    try_run("chown -R www-data:www-data %s/data " % env.env.pithos_dir)
808 0ac84a9a Dimitris Aragiorgis
    #try_run("pithos-migrate stamp 4c8ccdc58192")
809 0ac84a9a Dimitris Aragiorgis
    #try_run("pithos-migrate upgrade head")
810 0ac84a9a Dimitris Aragiorgis
811 0ac84a9a Dimitris Aragiorgis
812 0ac84a9a Dimitris Aragiorgis
def add_wheezy():
813 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/apt/sources.list.d/wheezy.list"
814 0ac84a9a Dimitris Aragiorgis
    replace = {}
815 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
816 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl)
817 0ac84a9a Dimitris Aragiorgis
    apt_get_update()
818 0ac84a9a Dimitris Aragiorgis
819 0ac84a9a Dimitris Aragiorgis
820 0ac84a9a Dimitris Aragiorgis
def remove_wheezy():
821 0ac84a9a Dimitris Aragiorgis
    try_run("rm -f /etc/apt/sources.list.d/wheezy.list")
822 0ac84a9a Dimitris Aragiorgis
    apt_get_update()
823 0ac84a9a Dimitris Aragiorgis
824 0ac84a9a Dimitris Aragiorgis
825 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
826 0ac84a9a Dimitris Aragiorgis
def setup_ganeti():
827 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up snf-ganeti...")
828 0ac84a9a Dimitris Aragiorgis
    node_info = env.env.ips_info[env.host]
829 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
830 0ac84a9a Dimitris Aragiorgis
        #if env.enable_lvm:
831 0ac84a9a Dimitris Aragiorgis
        #    try_run("vgs " + env.env.vg)
832 0ac84a9a Dimitris Aragiorgis
        try_run("getent hosts " + env.env.cluster.fqdn)
833 0ac84a9a Dimitris Aragiorgis
        try_run("getent hosts %s | grep -v ^127" % env.host)
834 0ac84a9a Dimitris Aragiorgis
        try_run("hostname -f | grep " + node_info.fqdn)
835 0ac84a9a Dimitris Aragiorgis
        #try_run("ip link show " + env.env.common_bridge)
836 0ac84a9a Dimitris Aragiorgis
        #try_run("ip link show " + env.env.common_bridge)
837 0ac84a9a Dimitris Aragiorgis
        #try_run("apt-get update")
838 0ac84a9a Dimitris Aragiorgis
    install_package("qemu-kvm")
839 0ac84a9a Dimitris Aragiorgis
    install_package("python-bitarray")
840 0ac84a9a Dimitris Aragiorgis
    add_wheezy()
841 0ac84a9a Dimitris Aragiorgis
    install_package("ganeti-htools")
842 0ac84a9a Dimitris Aragiorgis
    remove_wheezy()
843 0ac84a9a Dimitris Aragiorgis
    install_package("snf-ganeti")
844 0ac84a9a Dimitris Aragiorgis
    try_run("mkdir -p /srv/ganeti/file-storage/")
845 0ac84a9a Dimitris Aragiorgis
    cmd = """
846 0ac84a9a Dimitris Aragiorgis
cat <<EOF > /etc/ganeti/file-storage-paths
847 0ac84a9a Dimitris Aragiorgis
/srv/ganeti/file-storage
848 0ac84a9a Dimitris Aragiorgis
/srv/ganeti/shared-file-storage
849 0ac84a9a Dimitris Aragiorgis
EOF
850 0ac84a9a Dimitris Aragiorgis
"""
851 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
852 0ac84a9a Dimitris Aragiorgis
853 0ac84a9a Dimitris Aragiorgis
854 0ac84a9a Dimitris Aragiorgis
@roles("master")
855 0ac84a9a Dimitris Aragiorgis
def add_rapi_user():
856 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Adding RAPI user to Ganeti backend...")
857 0ac84a9a Dimitris Aragiorgis
    cmd = """
858 0ac84a9a Dimitris Aragiorgis
    echo -n "{0}:Ganeti Remote API:{1}" | openssl md5
859 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.synnefo_user, env.env.synnefo_rapi_passwd)
860 0ac84a9a Dimitris Aragiorgis
    result = try_run(cmd)
861 0ac84a9a Dimitris Aragiorgis
    cmd = """
862 0ac84a9a Dimitris Aragiorgis
    echo "{0} {1}{2} write" >> /var/lib/ganeti/rapi/users
863 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.synnefo_user, '{ha1}',result)
864 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
865 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/ganeti restart")
866 0ac84a9a Dimitris Aragiorgis
867 0ac84a9a Dimitris Aragiorgis
@roles("master")
868 0ac84a9a Dimitris Aragiorgis
def add_nodes():
869 0ac84a9a Dimitris Aragiorgis
    nodes = env.env.cluster_nodes.split(",")
870 0ac84a9a Dimitris Aragiorgis
    nodes.remove(env.env.master_node)
871 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Adding nodes to Ganeti backend...")
872 0ac84a9a Dimitris Aragiorgis
    for n in nodes:
873 0ac84a9a Dimitris Aragiorgis
        add_node(n)
874 0ac84a9a Dimitris Aragiorgis
875 0ac84a9a Dimitris Aragiorgis
@roles("master")
876 0ac84a9a Dimitris Aragiorgis
def add_node(node):
877 0ac84a9a Dimitris Aragiorgis
    node_info = env.env.nodes_info[node]
878 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Adding node %s to Ganeti backend..." % node_info.fqdn)
879 0ac84a9a Dimitris Aragiorgis
    cmd = "gnt-node add --no-ssh-key-check --master-capable=yes --vm-capable=yes " + node_info.fqdn
880 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
881 0ac84a9a Dimitris Aragiorgis
882 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
883 0ac84a9a Dimitris Aragiorgis
def enable_drbd():
884 0ac84a9a Dimitris Aragiorgis
    if env.enable_drbd:
885 0ac84a9a Dimitris Aragiorgis
        debug(env.host, " * Enabling DRBD...")
886 0ac84a9a Dimitris Aragiorgis
        try_run("modprobe drbd minor_count=255 usermode_helper=/bin/true")
887 0ac84a9a Dimitris Aragiorgis
        try_run("echo drbd minor_count=255 usermode_helper=/bin/true >> /etc/modules")
888 0ac84a9a Dimitris Aragiorgis
889 0ac84a9a Dimitris Aragiorgis
@roles("master")
890 0ac84a9a Dimitris Aragiorgis
def setup_drbd_dparams():
891 0ac84a9a Dimitris Aragiorgis
    if env.enable_drbd:
892 0ac84a9a Dimitris Aragiorgis
        debug(env.host, " * Twicking drbd related disk parameters in Ganeti...")
893 0ac84a9a Dimitris Aragiorgis
        cmd = """
894 0ac84a9a Dimitris Aragiorgis
        gnt-cluster modify --disk-parameters=drbd:metavg={0}
895 0ac84a9a Dimitris Aragiorgis
        gnt-group modify --disk-parameters=drbd:metavg={0} default
896 0ac84a9a Dimitris Aragiorgis
        """.format(env.env.vg)
897 0ac84a9a Dimitris Aragiorgis
        try_run(cmd)
898 0ac84a9a Dimitris Aragiorgis
899 0ac84a9a Dimitris Aragiorgis
@roles("master")
900 0ac84a9a Dimitris Aragiorgis
def enable_lvm():
901 0ac84a9a Dimitris Aragiorgis
    if env.enable_lvm:
902 0ac84a9a Dimitris Aragiorgis
        debug(env.host, " * Enabling LVM...")
903 0ac84a9a Dimitris Aragiorgis
        cmd = """
904 0ac84a9a Dimitris Aragiorgis
        gnt-cluster modify --vg-name={0}
905 0ac84a9a Dimitris Aragiorgis
        """.format(env.env.vg)
906 0ac84a9a Dimitris Aragiorgis
        try_run(cmd)
907 0ac84a9a Dimitris Aragiorgis
    else:
908 0ac84a9a Dimitris Aragiorgis
        debug(env.host, " * Disabling LVM...")
909 0ac84a9a Dimitris Aragiorgis
        try_run("gnt-cluster modify --no-lvm-storage")
910 0ac84a9a Dimitris Aragiorgis
911 0ac84a9a Dimitris Aragiorgis
@roles("master")
912 0ac84a9a Dimitris Aragiorgis
def destroy_cluster():
913 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Destroying Ganeti cluster...")
914 0ac84a9a Dimitris Aragiorgis
    #TODO: remove instances first
915 0ac84a9a Dimitris Aragiorgis
    allnodes = env.env.cluster_hostnames[:]
916 0ac84a9a Dimitris Aragiorgis
    allnodes.remove(env.host)
917 0ac84a9a Dimitris Aragiorgis
    for n in allnodes:
918 0ac84a9a Dimitris Aragiorgis
      host_info = env.env.ips_info[host]
919 0ac84a9a Dimitris Aragiorgis
      debug(env.host, " * Removing node %s..." % n)
920 0ac84a9a Dimitris Aragiorgis
      cmd = "gnt-node remove  " + host_info.fqdn
921 0ac84a9a Dimitris Aragiorgis
      try_run(cmd)
922 0ac84a9a Dimitris Aragiorgis
    try_run("gnt-cluster destroy --yes-do-it")
923 0ac84a9a Dimitris Aragiorgis
924 0ac84a9a Dimitris Aragiorgis
925 0ac84a9a Dimitris Aragiorgis
@roles("master")
926 0ac84a9a Dimitris Aragiorgis
def init_cluster():
927 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Initializing Ganeti backend...")
928 0ac84a9a Dimitris Aragiorgis
    # extra = ""
929 0ac84a9a Dimitris Aragiorgis
    # if env.enable_lvm:
930 0ac84a9a Dimitris Aragiorgis
    #     extra += " --vg-name={0} ".format(env.env.vg)
931 0ac84a9a Dimitris Aragiorgis
    # else:
932 0ac84a9a Dimitris Aragiorgis
    #     extra += " --no-lvm-storage "
933 0ac84a9a Dimitris Aragiorgis
    # if not env.enable_drbd:
934 0ac84a9a Dimitris Aragiorgis
    #     extra += " --no-drbd-storage "
935 0ac84a9a Dimitris Aragiorgis
    extra = " --no-lvm-storage --no-drbd-storage "
936 0ac84a9a Dimitris Aragiorgis
    cmd = """
937 0ac84a9a Dimitris Aragiorgis
    gnt-cluster init --enabled-hypervisors=kvm \
938 0ac84a9a Dimitris Aragiorgis
                     {0} \
939 0ac84a9a Dimitris Aragiorgis
                     --nic-parameters link={1},mode=bridged \
940 0ac84a9a Dimitris Aragiorgis
                     --master-netdev {2} \
941 0ac84a9a Dimitris Aragiorgis
                     --default-iallocator hail \
942 0ac84a9a Dimitris Aragiorgis
                     --hypervisor-parameters kvm:kernel_path=,vnc_bind_address=0.0.0.0 \
943 0ac84a9a Dimitris Aragiorgis
                     --no-ssh-init --no-etc-hosts \
944 0ac84a9a Dimitris Aragiorgis
                    {3}
945 0ac84a9a Dimitris Aragiorgis

946 0ac84a9a Dimitris Aragiorgis
    """.format(extra, env.env.common_bridge,
947 0ac84a9a Dimitris Aragiorgis
               env.env.cluster_netdev, env.env.cluster.fqdn)
948 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
949 0ac84a9a Dimitris Aragiorgis
950 0ac84a9a Dimitris Aragiorgis
951 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
952 0ac84a9a Dimitris Aragiorgis
def debootstrap():
953 0ac84a9a Dimitris Aragiorgis
    install_package("ganeti-instance-debootstrap")
954 0ac84a9a Dimitris Aragiorgis
955 0ac84a9a Dimitris Aragiorgis
956 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
957 0ac84a9a Dimitris Aragiorgis
def setup_image_host():
958 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up snf-image...")
959 0ac84a9a Dimitris Aragiorgis
    install_package("snf-pithos-backend")
960 0ac84a9a Dimitris Aragiorgis
    install_package("snf-image")
961 0ac84a9a Dimitris Aragiorgis
    try_run("mkdir -p /srv/okeanos")
962 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/default/snf-image"
963 0ac84a9a Dimitris Aragiorgis
    replace = {
964 0ac84a9a Dimitris Aragiorgis
        "synnefo_user": env.env.synnefo_user,
965 0ac84a9a Dimitris Aragiorgis
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
966 0ac84a9a Dimitris Aragiorgis
        "pithos_dir": env.env.pithos_dir,
967 0ac84a9a Dimitris Aragiorgis
        "db_node": env.env.db.ip,
968 0ac84a9a Dimitris Aragiorgis
    }
969 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
970 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl)
971 0ac84a9a Dimitris Aragiorgis
972 0ac84a9a Dimitris Aragiorgis
973 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
974 0ac84a9a Dimitris Aragiorgis
def setup_image_helper():
975 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Updating helper image...")
976 0ac84a9a Dimitris Aragiorgis
    cmd = """
977 0ac84a9a Dimitris Aragiorgis
    snf-image-update-helper -y
978 0ac84a9a Dimitris Aragiorgis
    """
979 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
980 0ac84a9a Dimitris Aragiorgis
981 0ac84a9a Dimitris Aragiorgis
982 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
983 0ac84a9a Dimitris Aragiorgis
def setup_gtools():
984 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up snf-cyclades-gtools...")
985 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
986 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 " + env.env.mq.ip)
987 0ac84a9a Dimitris Aragiorgis
    setup_common()
988 0ac84a9a Dimitris Aragiorgis
    install_package("snf-cyclades-gtools")
989 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/synnefo/gtools.conf"
990 0ac84a9a Dimitris Aragiorgis
    replace = {
991 0ac84a9a Dimitris Aragiorgis
        "synnefo_user": env.env.synnefo_user,
992 0ac84a9a Dimitris Aragiorgis
        "synnefo_rabbitmq_passwd": env.env.synnefo_rabbitmq_passwd,
993 0ac84a9a Dimitris Aragiorgis
        "mq_node": env.env.mq.ip,
994 0ac84a9a Dimitris Aragiorgis
    }
995 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
996 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl)
997 0ac84a9a Dimitris Aragiorgis
998 0ac84a9a Dimitris Aragiorgis
    cmd = """
999 0ac84a9a Dimitris Aragiorgis
    sed -i 's/false/true/' /etc/default/snf-ganeti-eventd
1000 0ac84a9a Dimitris Aragiorgis
    /etc/init.d/snf-ganeti-eventd start
1001 0ac84a9a Dimitris Aragiorgis
    """
1002 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1003 0ac84a9a Dimitris Aragiorgis
1004 0ac84a9a Dimitris Aragiorgis
1005 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
1006 0ac84a9a Dimitris Aragiorgis
def setup_iptables():
1007 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up iptables to mangle DHCP requests...")
1008 0ac84a9a Dimitris Aragiorgis
    cmd = """
1009 0ac84a9a Dimitris Aragiorgis
    iptables -t mangle -A PREROUTING -i br+ -p udp -m udp --dport 67 -j NFQUEUE --queue-num 42
1010 0ac84a9a Dimitris Aragiorgis
    iptables -t mangle -A PREROUTING -i tap+ -p udp -m udp --dport 67 -j NFQUEUE --queue-num 42
1011 0ac84a9a Dimitris Aragiorgis
    iptables -t mangle -A PREROUTING -i prv+ -p udp -m udp --dport 67 -j NFQUEUE --queue-num 42
1012 0ac84a9a Dimitris Aragiorgis

1013 0ac84a9a Dimitris Aragiorgis
    ip6tables -t mangle -A PREROUTING -i br+ -p ipv6-icmp -m icmp6 --icmpv6-type 133 -j NFQUEUE --queue-num 43
1014 0ac84a9a Dimitris Aragiorgis
    ip6tables -t mangle -A PREROUTING -i br+ -p ipv6-icmp -m icmp6 --icmpv6-type 135 -j NFQUEUE --queue-num 44
1015 0ac84a9a Dimitris Aragiorgis
    """
1016 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1017 0ac84a9a Dimitris Aragiorgis
1018 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
1019 0ac84a9a Dimitris Aragiorgis
def setup_network():
1020 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up networking for Ganeti instances (nfdhcpd, etc.)...")
1021 0ac84a9a Dimitris Aragiorgis
    install_package("nfqueue-bindings-python")
1022 0ac84a9a Dimitris Aragiorgis
    install_package("nfdhcpd")
1023 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/nfdhcpd/nfdhcpd.conf"
1024 0ac84a9a Dimitris Aragiorgis
    replace = {
1025 0ac84a9a Dimitris Aragiorgis
      "ns_node_ip": env.env.ns.ip
1026 0ac84a9a Dimitris Aragiorgis
      }
1027 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
1028 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl)
1029 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/nfdhcpd restart")
1030 0ac84a9a Dimitris Aragiorgis
1031 0ac84a9a Dimitris Aragiorgis
    install_package("snf-network")
1032 0ac84a9a Dimitris Aragiorgis
    cmd = """
1033 0ac84a9a Dimitris Aragiorgis
    sed -i 's/MAC_MASK.*/MAC_MASK = ff:ff:f0:00:00:00/' /etc/default/snf-network
1034 0ac84a9a Dimitris Aragiorgis
    """
1035 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1036 0ac84a9a Dimitris Aragiorgis
1037 0ac84a9a Dimitris Aragiorgis
1038 0ac84a9a Dimitris Aragiorgis
@roles("router")
1039 0ac84a9a Dimitris Aragiorgis
def setup_router():
1040 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up internal router for NAT...")
1041 0ac84a9a Dimitris Aragiorgis
    cmd = """
1042 0ac84a9a Dimitris Aragiorgis
    echo 1 > /proc/sys/net/ipv4/ip_forward
1043 0ac84a9a Dimitris Aragiorgis
    iptables -t nat -A POSTROUTING -s {0} -o {3} -j MASQUERADE
1044 0ac84a9a Dimitris Aragiorgis
    ip addr add {1} dev {2}
1045 0ac84a9a Dimitris Aragiorgis
    ip route add {0} dev {2} src {1}
1046 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.synnefo_public_network_subnet,
1047 0ac84a9a Dimitris Aragiorgis
               env.env.synnefo_public_network_gateway,
1048 0ac84a9a Dimitris Aragiorgis
               env.env.common_bridge, env.env.public_iface)
1049 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1050 0ac84a9a Dimitris Aragiorgis
1051 6c51153a Dionysis Grigoropoulos
1052 0ac84a9a Dimitris Aragiorgis
@roles("cyclades")
1053 0ac84a9a Dimitris Aragiorgis
def cyclades_loaddata():
1054 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Loading initial data for cyclades...")
1055 6c51153a Dionysis Grigoropoulos
    try_run("snf-manage flavor-create %s %s %s %s" % (env.env.flavor_cpu,
1056 6c51153a Dionysis Grigoropoulos
                                                      env.env.flavor_ram,
1057 6c51153a Dionysis Grigoropoulos
                                                      env.env.flavor_disk,
1058 6c51153a Dionysis Grigoropoulos
                                                      env.env.flavor_storage))
1059 0ac84a9a Dimitris Aragiorgis
    #run("snf-manage loaddata flavors")
1060 0ac84a9a Dimitris Aragiorgis
1061 0ac84a9a Dimitris Aragiorgis
1062 0ac84a9a Dimitris Aragiorgis
@roles("cyclades")
1063 0ac84a9a Dimitris Aragiorgis
def setup_cyclades():
1064 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up snf-cyclades-app...")
1065 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
1066 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 accounts." + env.env.domain)
1067 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 " + env.env.db.ip)
1068 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 " + env.env.mq.ip)
1069 0ac84a9a Dimitris Aragiorgis
    setup_gunicorn()
1070 0ac84a9a Dimitris Aragiorgis
    setup_apache()
1071 0ac84a9a Dimitris Aragiorgis
    setup_webproject()
1072 0ac84a9a Dimitris Aragiorgis
    install_package("memcached")
1073 0ac84a9a Dimitris Aragiorgis
    install_package("python-memcache")
1074 0ac84a9a Dimitris Aragiorgis
    install_package("snf-pithos-backend")
1075 0ac84a9a Dimitris Aragiorgis
    install_package("kamaki")
1076 0ac84a9a Dimitris Aragiorgis
    install_package("snf-cyclades-app")
1077 0ac84a9a Dimitris Aragiorgis
    install_package("python-django-south")
1078 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/synnefo/cyclades.conf"
1079 0ac84a9a Dimitris Aragiorgis
1080 1d8cf97d Dimitris Aragiorgis
    with settings(host_string=env.env.accounts.ip):
1081 0ac84a9a Dimitris Aragiorgis
        service_id, service_token = get_service_details("cyclades")
1082 0ac84a9a Dimitris Aragiorgis
1083 0ac84a9a Dimitris Aragiorgis
    replace = {
1084 0ac84a9a Dimitris Aragiorgis
        "ACCOUNTS": env.env.accounts.fqdn,
1085 0ac84a9a Dimitris Aragiorgis
        "CYCLADES": env.env.cyclades.fqdn,
1086 0ac84a9a Dimitris Aragiorgis
        "mq_node": env.env.mq.ip,
1087 0ac84a9a Dimitris Aragiorgis
        "db_node": env.env.db.ip,
1088 0ac84a9a Dimitris Aragiorgis
        "synnefo_user": env.env.synnefo_user,
1089 0ac84a9a Dimitris Aragiorgis
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
1090 0ac84a9a Dimitris Aragiorgis
        "synnefo_rabbitmq_passwd": env.env.synnefo_rabbitmq_passwd,
1091 0ac84a9a Dimitris Aragiorgis
        "pithos_dir": env.env.pithos_dir,
1092 0ac84a9a Dimitris Aragiorgis
        "common_bridge": env.env.common_bridge,
1093 0ac84a9a Dimitris Aragiorgis
        "HOST": env.env.cyclades.ip,
1094 0ac84a9a Dimitris Aragiorgis
        "domain": env.env.domain,
1095 0ac84a9a Dimitris Aragiorgis
        "CYCLADES_SERVICE_TOKEN": service_token,
1096 0ac84a9a Dimitris Aragiorgis
        "proxy": env.env.cyclades.hostname == env.env.accounts.hostname
1097 0ac84a9a Dimitris Aragiorgis
        }
1098 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
1099 0ac84a9a Dimitris Aragiorgis
    put(custom, tmpl, mode=0644)
1100 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
1101 0ac84a9a Dimitris Aragiorgis
1102 0ac84a9a Dimitris Aragiorgis
    cmd = """
1103 0ac84a9a Dimitris Aragiorgis
    sed -i 's/false/true/' /etc/default/snf-dispatcher
1104 0ac84a9a Dimitris Aragiorgis
    /etc/init.d/snf-dispatcher start
1105 0ac84a9a Dimitris Aragiorgis
    """
1106 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1107 0ac84a9a Dimitris Aragiorgis
1108 0ac84a9a Dimitris Aragiorgis
    try_run("snf-manage syncdb")
1109 0ac84a9a Dimitris Aragiorgis
    try_run("snf-manage migrate --delete-ghost-migrations")
1110 0ac84a9a Dimitris Aragiorgis
1111 0ac84a9a Dimitris Aragiorgis
1112 0ac84a9a Dimitris Aragiorgis
@roles("cyclades")
1113 0ac84a9a Dimitris Aragiorgis
def get_backend_id(cluster_name="ganeti1.synnefo.deploy.local"):
1114 0ac84a9a Dimitris Aragiorgis
    backend_id = try_run("snf-manage backend-list 2>/dev/null | grep %s | awk '{print $1}'" % cluster_name)
1115 0ac84a9a Dimitris Aragiorgis
    return backend_id
1116 0ac84a9a Dimitris Aragiorgis
1117 0ac84a9a Dimitris Aragiorgis
1118 0ac84a9a Dimitris Aragiorgis
@roles("cyclades")
1119 0ac84a9a Dimitris Aragiorgis
def add_backend():
1120 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "adding %s ganeti backend to cyclades..." % env.env.cluster.fqdn)
1121 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
1122 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 " + env.env.cluster.fqdn)
1123 0ac84a9a Dimitris Aragiorgis
    cmd = """
1124 0ac84a9a Dimitris Aragiorgis
    snf-manage backend-add --clustername={0} --user={1} --pass={2}
1125 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.cluster.fqdn, env.env.synnefo_user,
1126 0ac84a9a Dimitris Aragiorgis
               env.env.synnefo_rapi_passwd)
1127 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1128 0ac84a9a Dimitris Aragiorgis
    backend_id = get_backend_id(env.env.cluster.fqdn)
1129 0ac84a9a Dimitris Aragiorgis
    try_run("snf-manage backend-modify --drained=False " + backend_id)
1130 0ac84a9a Dimitris Aragiorgis
1131 0ac84a9a Dimitris Aragiorgis
@roles("cyclades")
1132 0ac84a9a Dimitris Aragiorgis
def pin_user_to_backend(user_email):
1133 0ac84a9a Dimitris Aragiorgis
    backend_id = get_backend_id(env.env.cluster.fqdn)
1134 0ac84a9a Dimitris Aragiorgis
    # pin user to backend
1135 0ac84a9a Dimitris Aragiorgis
    cmd = """
1136 0ac84a9a Dimitris Aragiorgis
cat <<EOF >> /etc/synnefo/cyclades.conf
1137 0ac84a9a Dimitris Aragiorgis

1138 0ac84a9a Dimitris Aragiorgis
BACKEND_PER_USER = {
1139 0ac84a9a Dimitris Aragiorgis
  '%s': %s,
1140 0ac84a9a Dimitris Aragiorgis
}
1141 0ac84a9a Dimitris Aragiorgis

1142 0ac84a9a Dimitris Aragiorgis
EOF
1143 0ac84a9a Dimitris Aragiorgis
/etc/init.d/gunicorn restart
1144 0ac84a9a Dimitris Aragiorgis
    """  % (user_email, backend_id)
1145 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1146 0ac84a9a Dimitris Aragiorgis
1147 0ac84a9a Dimitris Aragiorgis
@roles("cyclades")
1148 0ac84a9a Dimitris Aragiorgis
def add_pools():
1149 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Creating pools of resources (brigdes, mac prefixes) in cyclades...")
1150 0ac84a9a Dimitris Aragiorgis
    try_run("snf-manage pool-create --type=mac-prefix --base=aa:00:0 --size=65536")
1151 0ac84a9a Dimitris Aragiorgis
    try_run("snf-manage pool-create --type=bridge --base=prv --size=20")
1152 0ac84a9a Dimitris Aragiorgis
1153 0ac84a9a Dimitris Aragiorgis
1154 0ac84a9a Dimitris Aragiorgis
@roles("cyclades")
1155 0ac84a9a Dimitris Aragiorgis
def add_network():
1156 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Adding public network in cyclades...")
1157 0ac84a9a Dimitris Aragiorgis
    backend_id = get_backend_id(env.env.cluster.fqdn)
1158 0ac84a9a Dimitris Aragiorgis
    cmd = """
1159 d6719b00 Christos Stavrakakis
    snf-manage network-create --subnet={0} --gateway={1} --public --dhcp=True --flavor={2} --mode=bridged --link={3} --name=Internet --backend-id={4}
1160 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.synnefo_public_network_subnet,
1161 0ac84a9a Dimitris Aragiorgis
               env.env.synnefo_public_network_gateway,
1162 0ac84a9a Dimitris Aragiorgis
               env.env.synnefo_public_network_type,
1163 0ac84a9a Dimitris Aragiorgis
               env.env.common_bridge, backend_id)
1164 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1165 0ac84a9a Dimitris Aragiorgis
1166 0ac84a9a Dimitris Aragiorgis
1167 0ac84a9a Dimitris Aragiorgis
@roles("cyclades")
1168 0ac84a9a Dimitris Aragiorgis
def setup_vncauthproxy():
1169 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up vncauthproxy...")
1170 0ac84a9a Dimitris Aragiorgis
    install_package("snf-vncauthproxy")
1171 0ac84a9a Dimitris Aragiorgis
    cmd = """
1172 0ac84a9a Dimitris Aragiorgis
    echo CHUID="www-data:nogroup" >> /etc/default/vncauthproxy
1173 0ac84a9a Dimitris Aragiorgis
    rm /var/log/vncauthproxy/vncauthproxy.log
1174 0ac84a9a Dimitris Aragiorgis
    """
1175 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1176 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/vncauthproxy restart")
1177 0ac84a9a Dimitris Aragiorgis
1178 0ac84a9a Dimitris Aragiorgis
@roles("client")
1179 0ac84a9a Dimitris Aragiorgis
def setup_kamaki():
1180 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up kamaki client...")
1181 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
1182 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 accounts." + env.env.domain)
1183 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 cyclades." + env.env.domain)
1184 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 pithos." + env.env.domain)
1185 0ac84a9a Dimitris Aragiorgis
1186 1d8cf97d Dimitris Aragiorgis
    with settings(host_string=env.env.db.ip):
1187 0ac84a9a Dimitris Aragiorgis
        uid, user_auth_token, user_uuid = get_auth_token_from_db(env.env.user_email)
1188 0ac84a9a Dimitris Aragiorgis
1189 0ac84a9a Dimitris Aragiorgis
    install_package("python-progress")
1190 0ac84a9a Dimitris Aragiorgis
    install_package("kamaki")
1191 0ac84a9a Dimitris Aragiorgis
    cmd = """
1192 0ac84a9a Dimitris Aragiorgis
    kamaki config set cloud.default.url "https://{0}/astakos/identity/v2.0/"
1193 0ac84a9a Dimitris Aragiorgis
    kamaki config set cloud.default.token {1}
1194 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.accounts.fqdn, user_auth_token)
1195 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1196 0ac84a9a Dimitris Aragiorgis
    try_run("kamaki file create images")
1197 0ac84a9a Dimitris Aragiorgis
1198 0ac84a9a Dimitris Aragiorgis
@roles("client")
1199 0ac84a9a Dimitris Aragiorgis
def upload_image(image="debian_base.diskdump"):
1200 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Uploading initial image to pithos...")
1201 0ac84a9a Dimitris Aragiorgis
    image = "debian_base.diskdump"
1202 0ac84a9a Dimitris Aragiorgis
    try_run("wget {0} -O /tmp/{1}".format(env.env.debian_base_url, image))
1203 0ac84a9a Dimitris Aragiorgis
    try_run("kamaki file upload --container images /tmp/{0} {0}".format(image))
1204 0ac84a9a Dimitris Aragiorgis
1205 0ac84a9a Dimitris Aragiorgis
@roles("client")
1206 0ac84a9a Dimitris Aragiorgis
def register_image(image="debian_base.diskdump"):
1207 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Register image to plankton...")
1208 1d8cf97d Dimitris Aragiorgis
    with settings(host_string=env.env.db.ip):
1209 0ac84a9a Dimitris Aragiorgis
        uid, user_auth_token, user_uuid = get_auth_token_from_db(env.env.user_email)
1210 0ac84a9a Dimitris Aragiorgis
1211 0ac84a9a Dimitris Aragiorgis
    pithos_url = "pithos://{0}/images/{1}".format(user_uuid, image)
1212 0ac84a9a Dimitris Aragiorgis
    cmd = """
1213 0ac84a9a Dimitris Aragiorgis
    sleep 5
1214 0ac84a9a Dimitris Aragiorgis
    kamaki image register "Debian Base" {0} --public --disk-format=diskdump --property OSFAMILY=linux --property ROOT_PARTITION=1 --property description="Debian Squeeze Base System" --property size=450M --property kernel=2.6.32 --property GUI="No GUI" --property sortorder=1 --property USERS=root --property OS=debian
1215 0ac84a9a Dimitris Aragiorgis
    """.format(pithos_url)
1216 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1217 0ac84a9a Dimitris Aragiorgis
1218 0ac84a9a Dimitris Aragiorgis
@roles("client")
1219 0ac84a9a Dimitris Aragiorgis
def setup_burnin():
1220 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up burnin testing tool...")
1221 0ac84a9a Dimitris Aragiorgis
    install_package("kamaki")
1222 0ac84a9a Dimitris Aragiorgis
    install_package("snf-tools")
1223 0ac84a9a Dimitris Aragiorgis
1224 0ac84a9a Dimitris Aragiorgis
@roles("pithos")
1225 0ac84a9a Dimitris Aragiorgis
def add_image_locally():
1226 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Getting image locally in order snf-image to use it directly..")
1227 0ac84a9a Dimitris Aragiorgis
    image = "debian_base.diskdump"
1228 0ac84a9a Dimitris Aragiorgis
    try_run("wget {0} -O /srv/okeanos/{1}".format(env.env.debian_base_url, image))
1229 0ac84a9a Dimitris Aragiorgis
1230 0ac84a9a Dimitris Aragiorgis
1231 0ac84a9a Dimitris Aragiorgis
@roles("master")
1232 0ac84a9a Dimitris Aragiorgis
def gnt_instance_add(name="test"):
1233 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Adding test instance to Ganeti...")
1234 0ac84a9a Dimitris Aragiorgis
    osp="""img_passwd=gamwtosecurity,img_format=diskdump,img_id=debian_base,img_properties='{"OSFAMILY":"linux"\,"ROOT_PARTITION":"1"}'"""
1235 0ac84a9a Dimitris Aragiorgis
    cmd = """
1236 0ac84a9a Dimitris Aragiorgis
    gnt-instance add  -o snf-image+default --os-parameters {0} -t plain --disk 0:size=1G --no-name-check --no-ip-check --net 0:ip=pool,network=test --no-install --hypervisor-parameters kvm:machine_version=pc-1.0 {1}
1237 0ac84a9a Dimitris Aragiorgis
    """.format(osp, name)
1238 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1239 0ac84a9a Dimitris Aragiorgis
1240 0ac84a9a Dimitris Aragiorgis
@roles("master")
1241 0ac84a9a Dimitris Aragiorgis
def gnt_network_add(name="test", subnet="10.0.0.0/26", gw="10.0.0.1", mode="bridged", link="br0"):
1242 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Adding test network to Ganeti...")
1243 0ac84a9a Dimitris Aragiorgis
    cmd = """
1244 0ac84a9a Dimitris Aragiorgis
    gnt-network add --network={1} --gateway={2} {0}
1245 0ac84a9a Dimitris Aragiorgis
    gnt-network connect {0} {3} {4}
1246 0ac84a9a Dimitris Aragiorgis
    """.format(name, subnet, gw, mode, link)
1247 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1248 0ac84a9a Dimitris Aragiorgis
1249 0ac84a9a Dimitris Aragiorgis
@roles("ips")
1250 0ac84a9a Dimitris Aragiorgis
def test():
1251 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Testing...")
1252 0ac84a9a Dimitris Aragiorgis
    try_run("hostname && date")