Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (44.7 kB)

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

6 f1800130 Ilias Tsitsimpis
"""
7 f1800130 Ilias Tsitsimpis
8 0ac84a9a Dimitris Aragiorgis
from __future__ import with_statement
9 f1800130 Ilias Tsitsimpis
from fabric.api import hide, env, settings, local, roles
10 f1800130 Ilias Tsitsimpis
from fabric.operations import run, put, get
11 0e6e73d3 Ilias Tsitsimpis
import fabric
12 0ac84a9a Dimitris Aragiorgis
import re
13 f1800130 Ilias Tsitsimpis
import os
14 f1800130 Ilias Tsitsimpis
import shutil
15 0ac84a9a Dimitris Aragiorgis
import tempfile
16 0ac84a9a Dimitris Aragiorgis
import ast
17 f1800130 Ilias Tsitsimpis
from snfdeploy.lib import debug, Conf, Env, disable_color
18 0ac84a9a Dimitris Aragiorgis
from snfdeploy import massedit
19 0ac84a9a Dimitris Aragiorgis
20 0ac84a9a Dimitris Aragiorgis
21 3bae85da Dimitris Aragiorgis
def setup_env(args):
22 f1800130 Ilias Tsitsimpis
    """Setup environment"""
23 0ac84a9a Dimitris Aragiorgis
    print("Loading configuration for synnefo...")
24 3bae85da Dimitris Aragiorgis
25 3bae85da Dimitris Aragiorgis
    conf = Conf(args)
26 0ac84a9a Dimitris Aragiorgis
    env.env = Env(conf)
27 0ac84a9a Dimitris Aragiorgis
28 3bae85da Dimitris Aragiorgis
    env.local = args.autoconf
29 3bae85da Dimitris Aragiorgis
    env.key_inject = args.key_inject
30 0ac84a9a Dimitris Aragiorgis
    env.password = env.env.password
31 0ac84a9a Dimitris Aragiorgis
    env.user = env.env.user
32 0ac84a9a Dimitris Aragiorgis
    env.shell = "/bin/bash -c"
33 3bae85da Dimitris Aragiorgis
    env.key_filename = args.ssh_key
34 0ac84a9a Dimitris Aragiorgis
35 3bae85da Dimitris Aragiorgis
    if args.disable_colors:
36 1bc6d467 Dionysis Grigoropoulos
        disable_color()
37 1bc6d467 Dionysis Grigoropoulos
38 f1800130 Ilias Tsitsimpis
    if env.env.cms.hostname in \
39 f1800130 Ilias Tsitsimpis
            [env.env.accounts.hostname, env.env.cyclades.hostname,
40 f1800130 Ilias Tsitsimpis
             env.env.pithos.hostname]:
41 f1800130 Ilias Tsitsimpis
        env.cms_pass = True
42 0ac84a9a Dimitris Aragiorgis
    else:
43 f1800130 Ilias Tsitsimpis
        env.cms_pass = False
44 0ac84a9a Dimitris Aragiorgis
45 f1800130 Ilias Tsitsimpis
    if env.env.accounts.hostname in \
46 f1800130 Ilias Tsitsimpis
            [env.env.cyclades.hostname, env.env.pithos.hostname]:
47 f1800130 Ilias Tsitsimpis
        env.csrf_disable = True
48 0ac84a9a Dimitris Aragiorgis
    else:
49 f1800130 Ilias Tsitsimpis
        env.csrf_disable = False
50 0ac84a9a Dimitris Aragiorgis
51 0ac84a9a Dimitris Aragiorgis
    env.roledefs = {
52 0ac84a9a Dimitris Aragiorgis
        "nodes": env.env.ips,
53 0ac84a9a Dimitris Aragiorgis
        "ips": env.env.ips,
54 0ac84a9a Dimitris Aragiorgis
        "accounts": [env.env.accounts.ip],
55 0ac84a9a Dimitris Aragiorgis
        "cyclades": [env.env.cyclades.ip],
56 0ac84a9a Dimitris Aragiorgis
        "pithos": [env.env.pithos.ip],
57 0ac84a9a Dimitris Aragiorgis
        "cms": [env.env.cms.ip],
58 0ac84a9a Dimitris Aragiorgis
        "mq": [env.env.mq.ip],
59 0ac84a9a Dimitris Aragiorgis
        "db": [env.env.db.ip],
60 f1800130 Ilias Tsitsimpis
        "mq": [env.env.mq.ip],
61 f1800130 Ilias Tsitsimpis
        "db": [env.env.db.ip],
62 0ac84a9a Dimitris Aragiorgis
        "ns": [env.env.ns.ip],
63 0ac84a9a Dimitris Aragiorgis
        "client": [env.env.client.ip],
64 0ac84a9a Dimitris Aragiorgis
        "router": [env.env.router.ip],
65 0cfa5e03 Stratos Psomadakis
        "stats": [env.env.stats.ip],
66 0ac84a9a Dimitris Aragiorgis
    }
67 0ac84a9a Dimitris Aragiorgis
68 0ac84a9a Dimitris Aragiorgis
    env.enable_lvm = False
69 0ac84a9a Dimitris Aragiorgis
    env.enable_drbd = False
70 0ac84a9a Dimitris Aragiorgis
    if ast.literal_eval(env.env.create_extra_disk) and env.env.extra_disk:
71 0ac84a9a Dimitris Aragiorgis
        env.enable_lvm = True
72 0ac84a9a Dimitris Aragiorgis
        env.enable_drbd = True
73 0ac84a9a Dimitris Aragiorgis
74 0ac84a9a Dimitris Aragiorgis
    env.roledefs.update({
75 0ac84a9a Dimitris Aragiorgis
        "ganeti": env.env.cluster_ips,
76 0ac84a9a Dimitris Aragiorgis
        "master": [env.env.master.ip],
77 0ac84a9a Dimitris Aragiorgis
    })
78 0ac84a9a Dimitris Aragiorgis
79 0ac84a9a Dimitris Aragiorgis
80 0ac84a9a Dimitris Aragiorgis
def install_package(package):
81 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Installing package %s..." % package)
82 f1800130 Ilias Tsitsimpis
    apt_get = "export DEBIAN_FRONTEND=noninteractive ;" + \
83 f1800130 Ilias Tsitsimpis
              "apt-get install -y --force-yes "
84 0ac84a9a Dimitris Aragiorgis
85 f1b4fdfc Dimitris Aragiorgis
    host_info = env.env.ips_info[env.host]
86 24dc98e2 Dimitris Aragiorgis
    env.env.update_packages(host_info.os)
87 0ac84a9a Dimitris Aragiorgis
    if ast.literal_eval(env.env.use_local_packages):
88 0ac84a9a Dimitris Aragiorgis
        with settings(warn_only=True):
89 b2334d13 Dimitris Aragiorgis
            deb = local("ls %s/%s*%s_*.deb"
90 f1800130 Ilias Tsitsimpis
                        % (env.env.packages, package, host_info.os),
91 c2a749b4 Ilias Tsitsimpis
                        capture=True)
92 0ac84a9a Dimitris Aragiorgis
            if deb:
93 f1800130 Ilias Tsitsimpis
                debug(env.host,
94 f1800130 Ilias Tsitsimpis
                      " * Package %s found in %s..."
95 f1800130 Ilias Tsitsimpis
                      % (package, env.env.packages))
96 0e6e73d3 Ilias Tsitsimpis
                try_put(deb, "/tmp/")
97 f1800130 Ilias Tsitsimpis
                try_run("dpkg -i /tmp/%s || "
98 f1800130 Ilias Tsitsimpis
                        % os.path.basename(deb) + apt_get + "-f")
99 24dc98e2 Dimitris Aragiorgis
                try_run("rm /tmp/%s" % os.path.basename(deb))
100 0ac84a9a Dimitris Aragiorgis
                return
101 0ac84a9a Dimitris Aragiorgis
102 0ac84a9a Dimitris Aragiorgis
    info = getattr(env.env, package)
103 f1800130 Ilias Tsitsimpis
    if info in \
104 f1800130 Ilias Tsitsimpis
            ["squeeze-backports", "squeeze", "stable",
105 f1800130 Ilias Tsitsimpis
             "testing", "unstable", "wheezy"]:
106 f1800130 Ilias Tsitsimpis
        apt_get += " -t %s %s " % (info, package)
107 0ac84a9a Dimitris Aragiorgis
    elif info:
108 f1800130 Ilias Tsitsimpis
        apt_get += " %s=%s " % (package, info)
109 0ac84a9a Dimitris Aragiorgis
    else:
110 f1800130 Ilias Tsitsimpis
        apt_get += package
111 0ac84a9a Dimitris Aragiorgis
112 f1800130 Ilias Tsitsimpis
    try_run(apt_get)
113 0ac84a9a Dimitris Aragiorgis
114 0ac84a9a Dimitris Aragiorgis
    return
115 0ac84a9a Dimitris Aragiorgis
116 0ac84a9a Dimitris Aragiorgis
117 0ac84a9a Dimitris Aragiorgis
@roles("ns")
118 0ac84a9a Dimitris Aragiorgis
def update_ns_for_ganeti():
119 f1800130 Ilias Tsitsimpis
    debug(env.host,
120 f1800130 Ilias Tsitsimpis
          "Updating name server entries for backend %s..."
121 f1800130 Ilias Tsitsimpis
          % env.env.cluster.fqdn)
122 0ac84a9a Dimitris Aragiorgis
    update_arecord(env.env.cluster)
123 0ac84a9a Dimitris Aragiorgis
    update_ptrrecord(env.env.cluster)
124 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/bind9 restart")
125 0ac84a9a Dimitris Aragiorgis
126 0ac84a9a Dimitris Aragiorgis
127 0ac84a9a Dimitris Aragiorgis
@roles("ns")
128 0ac84a9a Dimitris Aragiorgis
def update_ns_for_node(node):
129 0ac84a9a Dimitris Aragiorgis
    info = env.env.nodes_info.get(node)
130 0ac84a9a Dimitris Aragiorgis
    update_arecord(info)
131 0ac84a9a Dimitris Aragiorgis
    update_ptrrecord(info)
132 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/bind9 restart")
133 0ac84a9a Dimitris Aragiorgis
134 0ac84a9a Dimitris Aragiorgis
135 0ac84a9a Dimitris Aragiorgis
@roles("ns")
136 0ac84a9a Dimitris Aragiorgis
def update_arecord(host):
137 0ac84a9a Dimitris Aragiorgis
    filename = "/etc/bind/zones/" + env.env.domain
138 0ac84a9a Dimitris Aragiorgis
    cmd = """
139 0ac84a9a Dimitris Aragiorgis
    echo '{0}' >> {1}
140 0ac84a9a Dimitris Aragiorgis
    """.format(host.arecord, filename)
141 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
142 0ac84a9a Dimitris Aragiorgis
143 0ac84a9a Dimitris Aragiorgis
144 0ac84a9a Dimitris Aragiorgis
@roles("ns")
145 0ac84a9a Dimitris Aragiorgis
def update_cnamerecord(host):
146 0ac84a9a Dimitris Aragiorgis
    filename = "/etc/bind/zones/" + env.env.domain
147 0ac84a9a Dimitris Aragiorgis
    cmd = """
148 0ac84a9a Dimitris Aragiorgis
    echo '{0}' >> {1}
149 0ac84a9a Dimitris Aragiorgis
    """.format(host.cnamerecord, filename)
150 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
151 0ac84a9a Dimitris Aragiorgis
152 0ac84a9a Dimitris Aragiorgis
153 0ac84a9a Dimitris Aragiorgis
@roles("ns")
154 0ac84a9a Dimitris Aragiorgis
def update_ptrrecord(host):
155 0ac84a9a Dimitris Aragiorgis
    filename = "/etc/bind/rev/synnefo.in-addr.arpa.zone"
156 0ac84a9a Dimitris Aragiorgis
    cmd = """
157 0ac84a9a Dimitris Aragiorgis
    echo '{0}' >> {1}
158 0ac84a9a Dimitris Aragiorgis
    """.format(host.ptrrecord, filename)
159 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
160 0ac84a9a Dimitris Aragiorgis
161 f1800130 Ilias Tsitsimpis
162 0ac84a9a Dimitris Aragiorgis
@roles("nodes")
163 0ac84a9a Dimitris Aragiorgis
def apt_get_update():
164 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "apt-get update....")
165 0ac84a9a Dimitris Aragiorgis
    try_run("apt-get update")
166 0ac84a9a Dimitris Aragiorgis
167 f1800130 Ilias Tsitsimpis
168 0ac84a9a Dimitris Aragiorgis
@roles("ns")
169 0ac84a9a Dimitris Aragiorgis
def setup_ns():
170 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up name server..")
171 0ac84a9a Dimitris Aragiorgis
    #WARNING: this should be remove after we are done
172 0ac84a9a Dimitris Aragiorgis
    # because gevent does pick randomly nameservers and google does
173 0ac84a9a Dimitris Aragiorgis
    # not know our setup!!!!!
174 0ac84a9a Dimitris Aragiorgis
    apt_get_update()
175 0ac84a9a Dimitris Aragiorgis
    install_package("bind9")
176 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/bind/named.conf.local"
177 0ac84a9a Dimitris Aragiorgis
    replace = {
178 f1800130 Ilias Tsitsimpis
        "domain": env.env.domain,
179 f1800130 Ilias Tsitsimpis
    }
180 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
181 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl)
182 0ac84a9a Dimitris Aragiorgis
183 0ac84a9a Dimitris Aragiorgis
    try_run("mkdir -p /etc/bind/zones")
184 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/bind/zones/example.com"
185 0ac84a9a Dimitris Aragiorgis
    replace = {
186 f1800130 Ilias Tsitsimpis
        "domain": env.env.domain,
187 f1800130 Ilias Tsitsimpis
        "ns_node_ip": env.env.ns.ip,
188 f1800130 Ilias Tsitsimpis
    }
189 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
190 0ac84a9a Dimitris Aragiorgis
    remote = "/etc/bind/zones/" + env.env.domain
191 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, remote)
192 0ac84a9a Dimitris Aragiorgis
193 0ac84a9a Dimitris Aragiorgis
    try_run("mkdir -p /etc/bind/rev")
194 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/bind/rev/synnefo.in-addr.arpa.zone"
195 0ac84a9a Dimitris Aragiorgis
    replace = {
196 f1800130 Ilias Tsitsimpis
        "domain": env.env.domain,
197 f1800130 Ilias Tsitsimpis
    }
198 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
199 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl)
200 0ac84a9a Dimitris Aragiorgis
201 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/bind/named.conf.options"
202 0ac84a9a Dimitris Aragiorgis
    replace = {
203 f1800130 Ilias Tsitsimpis
        "NODE_IPS": ";".join(env.env.ips),
204 f1800130 Ilias Tsitsimpis
    }
205 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
206 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl, mode=0644)
207 0ac84a9a Dimitris Aragiorgis
208 0ac84a9a Dimitris Aragiorgis
    for role, info in env.env.roles.iteritems():
209 0ac84a9a Dimitris Aragiorgis
        if role == "ns":
210 0ac84a9a Dimitris Aragiorgis
            continue
211 0ac84a9a Dimitris Aragiorgis
        update_cnamerecord(info)
212 0ac84a9a Dimitris Aragiorgis
    for node, info in env.env.nodes_info.iteritems():
213 0ac84a9a Dimitris Aragiorgis
        update_arecord(info)
214 0ac84a9a Dimitris Aragiorgis
        update_ptrrecord(info)
215 0ac84a9a Dimitris Aragiorgis
216 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/bind9 restart")
217 0ac84a9a Dimitris Aragiorgis
218 0ac84a9a Dimitris Aragiorgis
219 0ac84a9a Dimitris Aragiorgis
@roles("nodes")
220 0ac84a9a Dimitris Aragiorgis
def check_dhcp():
221 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Checking IPs for synnefo..")
222 0ac84a9a Dimitris Aragiorgis
    for n, info in env.env.nodes_info.iteritems():
223 0e6e73d3 Ilias Tsitsimpis
        try_run("ping -c 1 " + info.ip)
224 0ac84a9a Dimitris Aragiorgis
225 f1800130 Ilias Tsitsimpis
226 0ac84a9a Dimitris Aragiorgis
@roles("nodes")
227 0ac84a9a Dimitris Aragiorgis
def check_dns():
228 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Checking fqdns for synnefo..")
229 0ac84a9a Dimitris Aragiorgis
    for n, info in env.env.nodes_info.iteritems():
230 0e6e73d3 Ilias Tsitsimpis
        try_run("ping -c 1 " + info.fqdn)
231 0ac84a9a Dimitris Aragiorgis
232 0ac84a9a Dimitris Aragiorgis
    for n, info in env.env.roles.iteritems():
233 0e6e73d3 Ilias Tsitsimpis
        try_run("ping -c 1 " + info.fqdn)
234 0ac84a9a Dimitris Aragiorgis
235 f1800130 Ilias Tsitsimpis
236 0ac84a9a Dimitris Aragiorgis
@roles("nodes")
237 0ac84a9a Dimitris Aragiorgis
def check_connectivity():
238 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Checking internet connectivity..")
239 0e6e73d3 Ilias Tsitsimpis
    try_run("ping -c 1 www.google.com")
240 0ac84a9a Dimitris Aragiorgis
241 0ac84a9a Dimitris Aragiorgis
242 0ac84a9a Dimitris Aragiorgis
@roles("nodes")
243 0ac84a9a Dimitris Aragiorgis
def check_ssh():
244 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Checking password-less ssh..")
245 0ac84a9a Dimitris Aragiorgis
    for n, info in env.env.nodes_info.iteritems():
246 0e6e73d3 Ilias Tsitsimpis
        try_run("ssh " + info.fqdn + "  date")
247 0ac84a9a Dimitris Aragiorgis
248 0ac84a9a Dimitris Aragiorgis
249 0ac84a9a Dimitris Aragiorgis
@roles("ips")
250 0ac84a9a Dimitris Aragiorgis
def add_keys():
251 8780d2fa Dimitris Aragiorgis
    if not env.key_inject:
252 f1800130 Ilias Tsitsimpis
        debug(env.host, "Skipping ssh keys injection..")
253 f1800130 Ilias Tsitsimpis
        return
254 8780d2fa Dimitris Aragiorgis
    else:
255 f1800130 Ilias Tsitsimpis
        debug(env.host, "Adding rsa/dsa keys..")
256 0ac84a9a Dimitris Aragiorgis
    try_run("mkdir -p /root/.ssh")
257 0ac84a9a Dimitris Aragiorgis
    cmd = """
258 0ac84a9a Dimitris Aragiorgis
for f in $(ls /root/.ssh/*); do
259 0ac84a9a Dimitris Aragiorgis
  cp $f $f.bak
260 0ac84a9a Dimitris Aragiorgis
done
261 0ac84a9a Dimitris Aragiorgis
    """
262 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
263 0ac84a9a Dimitris Aragiorgis
    files = ["authorized_keys", "id_dsa", "id_dsa.pub",
264 0ac84a9a Dimitris Aragiorgis
             "id_rsa", "id_rsa.pub"]
265 0ac84a9a Dimitris Aragiorgis
    for f in files:
266 f1800130 Ilias Tsitsimpis
        tmpl = "/root/.ssh/" + f
267 f1800130 Ilias Tsitsimpis
        replace = {}
268 f1800130 Ilias Tsitsimpis
        custom = customize_settings_from_tmpl(tmpl, replace)
269 0e6e73d3 Ilias Tsitsimpis
        try_put(custom, tmpl, mode=0600)
270 0ac84a9a Dimitris Aragiorgis
271 0ac84a9a Dimitris Aragiorgis
    cmd = """
272 0ac84a9a Dimitris Aragiorgis
if [ -e /root/.ssh/authorized_keys.bak ]; then
273 0ac84a9a Dimitris Aragiorgis
  cat /root/.ssh/authorized_keys.bak >> /root/.ssh/authorized_keys
274 0ac84a9a Dimitris Aragiorgis
fi
275 0ac84a9a Dimitris Aragiorgis
    """
276 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Updating exising authorized keys..")
277 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
278 0ac84a9a Dimitris Aragiorgis
279 f1800130 Ilias Tsitsimpis
280 0ac84a9a Dimitris Aragiorgis
@roles("ips")
281 0ac84a9a Dimitris Aragiorgis
def setup_resolv_conf():
282 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Tweak /etc/resolv.conf...")
283 0e6e73d3 Ilias Tsitsimpis
    try_run("/etc/init.d/network-manager stop", abort=False)
284 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/dhcp/dhclient-enter-hooks.d/nodnsupdate"
285 0ac84a9a Dimitris Aragiorgis
    replace = {}
286 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
287 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl, mode=0644)
288 0ac84a9a Dimitris Aragiorgis
    try_run("cp /etc/resolv.conf /etc/resolv.conf.bak")
289 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/resolv.conf"
290 0ac84a9a Dimitris Aragiorgis
    replace = {
291 f1800130 Ilias Tsitsimpis
        "domain": env.env.domain,
292 f1800130 Ilias Tsitsimpis
        "ns_node_ip": env.env.ns.ip,
293 f1800130 Ilias Tsitsimpis
    }
294 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
295 703d78d6 Dimitris Aragiorgis
    try:
296 0e6e73d3 Ilias Tsitsimpis
        try_put(custom, tmpl)
297 28991d59 Ilias Tsitsimpis
        cmd = """
298 28991d59 Ilias Tsitsimpis
        echo "\
299 28991d59 Ilias Tsitsimpis
# This has been generated automatically by snf-deploy, at
300 28991d59 Ilias Tsitsimpis
# $(date).
301 28991d59 Ilias Tsitsimpis
# The immutable bit (+i attribute) has been used to avoid it being
302 28991d59 Ilias Tsitsimpis
# overwritten by software such as NetworkManager or resolvconf.
303 28991d59 Ilias Tsitsimpis
# Use lsattr/chattr to view or modify its file attributes.
304 28991d59 Ilias Tsitsimpis

305 28991d59 Ilias Tsitsimpis

306 28991d59 Ilias Tsitsimpis
$(cat {0})" > {0}
307 28991d59 Ilias Tsitsimpis
""".format(tmpl)
308 28991d59 Ilias Tsitsimpis
        try_run(cmd)
309 703d78d6 Dimitris Aragiorgis
    except:
310 f1800130 Ilias Tsitsimpis
        pass
311 0ac84a9a Dimitris Aragiorgis
    try_run("chattr +i /etc/resolv.conf")
312 0ac84a9a Dimitris Aragiorgis
313 0ac84a9a Dimitris Aragiorgis
314 0ac84a9a Dimitris Aragiorgis
@roles("ips")
315 0ac84a9a Dimitris Aragiorgis
def setup_hosts():
316 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Tweaking /etc/hosts and ssh_config files...")
317 0ac84a9a Dimitris Aragiorgis
    try_run("echo StrictHostKeyChecking no >> /etc/ssh/ssh_config")
318 fea067c8 Dimitris Aragiorgis
    cmd = "sed -i 's/^127.*$/127.0.0.1 localhost/g' /etc/hosts "
319 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
320 6e0e55ba Dimitris Aragiorgis
    host_info = env.env.ips_info[env.host]
321 6e0e55ba Dimitris Aragiorgis
    cmd = "hostname %s" % host_info.hostname
322 6e0e55ba Dimitris Aragiorgis
    try_run(cmd)
323 6e0e55ba Dimitris Aragiorgis
    cmd = "echo %s > /etc/hostname" % host_info.hostname
324 6e0e55ba Dimitris Aragiorgis
    try_run(cmd)
325 0ac84a9a Dimitris Aragiorgis
326 0ac84a9a Dimitris Aragiorgis
327 0e6e73d3 Ilias Tsitsimpis
def try_run(cmd, abort=True):
328 0ac84a9a Dimitris Aragiorgis
    try:
329 f1800130 Ilias Tsitsimpis
        if env.local:
330 f1800130 Ilias Tsitsimpis
            return local(cmd, capture=True)
331 f1800130 Ilias Tsitsimpis
        else:
332 f1800130 Ilias Tsitsimpis
            return run(cmd)
333 0e6e73d3 Ilias Tsitsimpis
    except BaseException as e:
334 0e6e73d3 Ilias Tsitsimpis
        if abort:
335 0e6e73d3 Ilias Tsitsimpis
            fabric.utils.abort(e)
336 0e6e73d3 Ilias Tsitsimpis
        else:
337 0e6e73d3 Ilias Tsitsimpis
            debug(env.host, "WARNING: command failed. Continuing anyway...")
338 0e6e73d3 Ilias Tsitsimpis
339 0e6e73d3 Ilias Tsitsimpis
340 0e6e73d3 Ilias Tsitsimpis
def try_put(local_path=None, remote_path=None, abort=True, **kwargs):
341 0e6e73d3 Ilias Tsitsimpis
    try:
342 0e6e73d3 Ilias Tsitsimpis
        put(local_path=local_path, remote_path=remote_path, **kwargs)
343 0e6e73d3 Ilias Tsitsimpis
    except BaseException as e:
344 f1800130 Ilias Tsitsimpis
        if abort:
345 0e6e73d3 Ilias Tsitsimpis
            fabric.utils.abort(e)
346 0e6e73d3 Ilias Tsitsimpis
        else:
347 0e6e73d3 Ilias Tsitsimpis
            debug(env.host, "WARNING: command failed. Continuing anyway...")
348 0e6e73d3 Ilias Tsitsimpis
349 0e6e73d3 Ilias Tsitsimpis
350 0e6e73d3 Ilias Tsitsimpis
def try_get(remote_path, local_path=None, abort=True, **kwargs):
351 0e6e73d3 Ilias Tsitsimpis
    try:
352 0e6e73d3 Ilias Tsitsimpis
        get(remote_path, local_path=local_path, **kwargs)
353 0e6e73d3 Ilias Tsitsimpis
    except BaseException as e:
354 0e6e73d3 Ilias Tsitsimpis
        if abort:
355 0e6e73d3 Ilias Tsitsimpis
            fabric.utils.abort(e)
356 0e6e73d3 Ilias Tsitsimpis
        else:
357 0e6e73d3 Ilias Tsitsimpis
            debug(env.host, "WARNING: command failed. Continuing anyway...")
358 f1800130 Ilias Tsitsimpis
359 0ac84a9a Dimitris Aragiorgis
360 0ac84a9a Dimitris Aragiorgis
def create_bridges():
361 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Creating bridges...")
362 0ac84a9a Dimitris Aragiorgis
    install_package("bridge-utils")
363 0ac84a9a Dimitris Aragiorgis
    cmd = """
364 0ac84a9a Dimitris Aragiorgis
    brctl addbr {0} ; ip link set {0} up
365 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.common_bridge)
366 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
367 0ac84a9a Dimitris Aragiorgis
368 0ac84a9a Dimitris Aragiorgis
369 0ac84a9a Dimitris Aragiorgis
def connect_bridges():
370 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Connecting bridges...")
371 f1800130 Ilias Tsitsimpis
    #cmd = """
372 f1800130 Ilias Tsitsimpis
    #brctl addif {0} {1}
373 f1800130 Ilias Tsitsimpis
    #""".format(env.env.common_bridge, env.env.public_iface)
374 0ac84a9a Dimitris Aragiorgis
    #try_run(cmd)
375 0ac84a9a Dimitris Aragiorgis
376 0ac84a9a Dimitris Aragiorgis
377 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
378 0ac84a9a Dimitris Aragiorgis
def setup_net_infra():
379 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setup networking infrastracture..")
380 0ac84a9a Dimitris Aragiorgis
    create_bridges()
381 0ac84a9a Dimitris Aragiorgis
    connect_bridges()
382 0ac84a9a Dimitris Aragiorgis
383 0ac84a9a Dimitris Aragiorgis
384 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
385 0ac84a9a Dimitris Aragiorgis
def setup_lvm():
386 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "create volume group %s for ganeti.." % env.env.vg)
387 0ac84a9a Dimitris Aragiorgis
    if env.enable_lvm:
388 0ac84a9a Dimitris Aragiorgis
        install_package("lvm2")
389 0ac84a9a Dimitris Aragiorgis
        cmd = """
390 0ac84a9a Dimitris Aragiorgis
        pvcreate {0}
391 0ac84a9a Dimitris Aragiorgis
        vgcreate {1} {0}
392 0ac84a9a Dimitris Aragiorgis
        """.format(env.env.extra_disk, env.env.vg)
393 0ac84a9a Dimitris Aragiorgis
        try_run(cmd)
394 0ac84a9a Dimitris Aragiorgis
395 0ac84a9a Dimitris Aragiorgis
396 0ac84a9a Dimitris Aragiorgis
def customize_settings_from_tmpl(tmpl, replace):
397 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Customizing template %s..." % tmpl)
398 0ac84a9a Dimitris Aragiorgis
    local = env.env.templates + tmpl
399 0ac84a9a Dimitris Aragiorgis
    _, custom = tempfile.mkstemp()
400 0ac84a9a Dimitris Aragiorgis
    shutil.copyfile(local, custom)
401 0ac84a9a Dimitris Aragiorgis
    for k, v in replace.iteritems():
402 0ac84a9a Dimitris Aragiorgis
        regex = "re.sub('%{0}%', '{1}', line)".format(k.upper(), v)
403 0ac84a9a Dimitris Aragiorgis
        massedit.edit_files([custom], [regex], dry_run=False)
404 0ac84a9a Dimitris Aragiorgis
405 0ac84a9a Dimitris Aragiorgis
    return custom
406 0ac84a9a Dimitris Aragiorgis
407 0ac84a9a Dimitris Aragiorgis
408 0ac84a9a Dimitris Aragiorgis
@roles("nodes")
409 0ac84a9a Dimitris Aragiorgis
def setup_apt():
410 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up apt sources...")
411 0ac84a9a Dimitris Aragiorgis
    install_package("curl")
412 0ac84a9a Dimitris Aragiorgis
    cmd = """
413 0ac84a9a Dimitris Aragiorgis
    echo 'APT::Install-Suggests "false";' >> /etc/apt/apt.conf
414 0ac84a9a Dimitris Aragiorgis
    curl -k https://dev.grnet.gr/files/apt-grnetdev.pub | apt-key add -
415 0ac84a9a Dimitris Aragiorgis
    """
416 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
417 f1b4fdfc Dimitris Aragiorgis
    host_info = env.env.ips_info[env.host]
418 f1b4fdfc Dimitris Aragiorgis
    if host_info.os == "squeeze":
419 f1800130 Ilias Tsitsimpis
        tmpl = "/etc/apt/sources.list.d/synnefo.squeeze.list"
420 f1b4fdfc Dimitris Aragiorgis
    else:
421 f1800130 Ilias Tsitsimpis
        tmpl = "/etc/apt/sources.list.d/synnefo.wheezy.list"
422 0ac84a9a Dimitris Aragiorgis
    replace = {}
423 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
424 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl)
425 0ac84a9a Dimitris Aragiorgis
    apt_get_update()
426 0ac84a9a Dimitris Aragiorgis
427 0ac84a9a Dimitris Aragiorgis
428 0ac84a9a Dimitris Aragiorgis
@roles("cyclades", "cms", "pithos", "accounts")
429 0ac84a9a Dimitris Aragiorgis
def restart_services():
430 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Restarting apache2 and gunicorn...")
431 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
432 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/apache2 restart")
433 0ac84a9a Dimitris Aragiorgis
434 0ac84a9a Dimitris Aragiorgis
435 0ac84a9a Dimitris Aragiorgis
def setup_gunicorn():
436 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up gunicorn...")
437 0ac84a9a Dimitris Aragiorgis
    install_package("gunicorn")
438 5a390037 Dimitris Aragiorgis
    try_run("chown root.www-data /var/log/gunicorn")
439 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/gunicorn.d/synnefo"
440 0ac84a9a Dimitris Aragiorgis
    replace = {}
441 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
442 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl, mode=0644)
443 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
444 0ac84a9a Dimitris Aragiorgis
445 0ac84a9a Dimitris Aragiorgis
446 0ac84a9a Dimitris Aragiorgis
def setup_apache():
447 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up apache2...")
448 0ac84a9a Dimitris Aragiorgis
    host_info = env.env.ips_info[env.host]
449 0ac84a9a Dimitris Aragiorgis
    install_package("apache2")
450 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/apache2/sites-available/synnefo"
451 0ac84a9a Dimitris Aragiorgis
    replace = {
452 0ac84a9a Dimitris Aragiorgis
        "HOST": host_info.fqdn,
453 0ac84a9a Dimitris Aragiorgis
    }
454 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
455 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl)
456 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/apache2/sites-available/synnefo-ssl"
457 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
458 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl)
459 0ac84a9a Dimitris Aragiorgis
    cmd = """
460 0ac84a9a Dimitris Aragiorgis
    a2enmod ssl
461 0ac84a9a Dimitris Aragiorgis
    a2enmod rewrite
462 0ac84a9a Dimitris Aragiorgis
    a2dissite default
463 0ac84a9a Dimitris Aragiorgis
    a2ensite synnefo
464 0ac84a9a Dimitris Aragiorgis
    a2ensite synnefo-ssl
465 0ac84a9a Dimitris Aragiorgis
    a2enmod headers
466 0ac84a9a Dimitris Aragiorgis
    a2enmod proxy_http
467 0ac84a9a Dimitris Aragiorgis
    a2dismod autoindex
468 0ac84a9a Dimitris Aragiorgis
    """
469 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
470 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/apache2 restart")
471 0ac84a9a Dimitris Aragiorgis
472 0ac84a9a Dimitris Aragiorgis
473 0ac84a9a Dimitris Aragiorgis
@roles("mq")
474 0ac84a9a Dimitris Aragiorgis
def setup_mq():
475 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up RabbitMQ...")
476 0ac84a9a Dimitris Aragiorgis
    install_package("rabbitmq-server")
477 0ac84a9a Dimitris Aragiorgis
    cmd = """
478 0ac84a9a Dimitris Aragiorgis
    rabbitmqctl add_user {0} {1}
479 0ac84a9a Dimitris Aragiorgis
    rabbitmqctl set_permissions {0} ".*" ".*" ".*"
480 0ac84a9a Dimitris Aragiorgis
    rabbitmqctl delete_user guest
481 0ac84a9a Dimitris Aragiorgis
    rabbitmqctl set_user_tags {0} administrator
482 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.synnefo_user, env.env.synnefo_rabbitmq_passwd)
483 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
484 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/rabbitmq-server restart")
485 0ac84a9a Dimitris Aragiorgis
486 0ac84a9a Dimitris Aragiorgis
487 0ac84a9a Dimitris Aragiorgis
@roles("db")
488 108fbde6 Dimitris Aragiorgis
def allow_access_in_db(ip, user="all", method="md5"):
489 0ac84a9a Dimitris Aragiorgis
    cmd = """
490 ccd1fef8 Ilias Tsitsimpis
    pg_hba=$(ls /etc/postgresql/*/main/pg_hba.conf)
491 ccd1fef8 Ilias Tsitsimpis
    echo host all {0} {1}/32 {2} >> $pg_hba
492 108fbde6 Dimitris Aragiorgis
    """.format(user, ip, method)
493 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
494 61aedf5a Christos Stavrakakis
    cmd = """
495 ccd1fef8 Ilias Tsitsimpis
    pg_hba=$(ls /etc/postgresql/*/main/pg_hba.conf)
496 ccd1fef8 Ilias Tsitsimpis
    sed -i 's/\(host.*127.0.0.1.*\)md5/\\1trust/' $pg_hba
497 61aedf5a Christos Stavrakakis
    """
498 61aedf5a Christos Stavrakakis
    try_run(cmd)
499 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/postgresql restart")
500 0ac84a9a Dimitris Aragiorgis
501 f1800130 Ilias Tsitsimpis
502 0ac84a9a Dimitris Aragiorgis
@roles("db")
503 0ac84a9a Dimitris Aragiorgis
def setup_db():
504 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up DataBase server...")
505 0ac84a9a Dimitris Aragiorgis
    install_package("postgresql")
506 0ac84a9a Dimitris Aragiorgis
507 0ac84a9a Dimitris Aragiorgis
    tmpl = "/tmp/db-init.psql"
508 0ac84a9a Dimitris Aragiorgis
    replace = {
509 0ac84a9a Dimitris Aragiorgis
        "synnefo_user": env.env.synnefo_user,
510 0ac84a9a Dimitris Aragiorgis
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
511 0ac84a9a Dimitris Aragiorgis
        }
512 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
513 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl)
514 0ac84a9a Dimitris Aragiorgis
    cmd = 'su - postgres -c "psql -w -f %s" ' % tmpl
515 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
516 0ac84a9a Dimitris Aragiorgis
    cmd = """
517 ccd1fef8 Ilias Tsitsimpis
    conf=$(ls /etc/postgresql/*/main/postgresql.conf)
518 ccd1fef8 Ilias Tsitsimpis
    echo "listen_addresses = '*'" >> $conf
519 0ac84a9a Dimitris Aragiorgis
    """
520 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
521 0ac84a9a Dimitris Aragiorgis
522 bd9b6f21 Christos Stavrakakis
    if env.env.testing_vm:
523 bd9b6f21 Christos Stavrakakis
        cmd = """
524 ccd1fef8 Ilias Tsitsimpis
        conf=$(ls /etc/postgresql/*/main/postgresql.conf)
525 ccd1fef8 Ilias Tsitsimpis
        echo "fsync=off\nsynchronous_commit=off\nfull_page_writes=off" >> $conf
526 bd9b6f21 Christos Stavrakakis
        """
527 bd9b6f21 Christos Stavrakakis
        try_run(cmd)
528 bd9b6f21 Christos Stavrakakis
529 108fbde6 Dimitris Aragiorgis
    allow_access_in_db(env.host, "all", "trust")
530 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/postgresql restart")
531 0ac84a9a Dimitris Aragiorgis
532 0ac84a9a Dimitris Aragiorgis
533 0ac84a9a Dimitris Aragiorgis
@roles("db")
534 0ac84a9a Dimitris Aragiorgis
def destroy_db():
535 0ac84a9a Dimitris Aragiorgis
    try_run("""su - postgres -c ' psql -w -c "drop database snf_apps" '""")
536 0ac84a9a Dimitris Aragiorgis
    try_run("""su - postgres -c ' psql -w -c "drop database snf_pithos" '""")
537 0ac84a9a Dimitris Aragiorgis
538 0ac84a9a Dimitris Aragiorgis
539 0ac84a9a Dimitris Aragiorgis
def setup_webproject():
540 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up snf-webproject...")
541 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
542 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 " + env.env.db.ip)
543 0ac84a9a Dimitris Aragiorgis
    setup_common()
544 0ac84a9a Dimitris Aragiorgis
    install_package("snf-webproject")
545 0ac84a9a Dimitris Aragiorgis
    install_package("python-psycopg2")
546 0ac84a9a Dimitris Aragiorgis
    install_package("python-gevent")
547 0500dae6 Christos Stavrakakis
    install_package("python-django")
548 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/synnefo/webproject.conf"
549 0ac84a9a Dimitris Aragiorgis
    replace = {
550 0ac84a9a Dimitris Aragiorgis
        "synnefo_user": env.env.synnefo_user,
551 0ac84a9a Dimitris Aragiorgis
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
552 0ac84a9a Dimitris Aragiorgis
        "db_node": env.env.db.ip,
553 0ac84a9a Dimitris Aragiorgis
        "domain": env.env.domain,
554 0ac84a9a Dimitris Aragiorgis
    }
555 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
556 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl, mode=0644)
557 1d8cf97d Dimitris Aragiorgis
    with settings(host_string=env.env.db.ip):
558 0ac84a9a Dimitris Aragiorgis
        host_info = env.env.ips_info[env.host]
559 108fbde6 Dimitris Aragiorgis
        allow_access_in_db(host_info.ip, "all", "trust")
560 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
561 0ac84a9a Dimitris Aragiorgis
562 0ac84a9a Dimitris Aragiorgis
563 0ac84a9a Dimitris Aragiorgis
def setup_common():
564 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up snf-common...")
565 0ac84a9a Dimitris Aragiorgis
    host_info = env.env.ips_info[env.host]
566 0ac84a9a Dimitris Aragiorgis
    install_package("python-objpool")
567 0ac84a9a Dimitris Aragiorgis
    install_package("snf-common")
568 0ac84a9a Dimitris Aragiorgis
    install_package("python-astakosclient")
569 0ac84a9a Dimitris Aragiorgis
    install_package("snf-django-lib")
570 0ac84a9a Dimitris Aragiorgis
    install_package("snf-branding")
571 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/synnefo/common.conf"
572 0ac84a9a Dimitris Aragiorgis
    replace = {
573 0ac84a9a Dimitris Aragiorgis
        #FIXME:
574 0ac84a9a Dimitris Aragiorgis
        "EMAIL_SUBJECT_PREFIX": env.host,
575 0ac84a9a Dimitris Aragiorgis
        "domain": env.env.domain,
576 0ac84a9a Dimitris Aragiorgis
        "HOST": host_info.fqdn,
577 f4cb5d92 Ilias Tsitsimpis
        "MAIL_DIR": env.env.mail_dir,
578 0ac84a9a Dimitris Aragiorgis
    }
579 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
580 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl, mode=0644)
581 ed0dd751 Ilias Tsitsimpis
    try_run("mkdir -p {0}; chmod 777 {0}".format(env.env.mail_dir))
582 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
583 0ac84a9a Dimitris Aragiorgis
584 f1800130 Ilias Tsitsimpis
585 0ac84a9a Dimitris Aragiorgis
@roles("accounts")
586 0ac84a9a Dimitris Aragiorgis
def astakos_loaddata():
587 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Loading initial data to astakos...")
588 0ac84a9a Dimitris Aragiorgis
    cmd = """
589 0ac84a9a Dimitris Aragiorgis
    snf-manage loaddata groups
590 0ac84a9a Dimitris Aragiorgis
    """
591 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
592 0ac84a9a Dimitris Aragiorgis
593 0ac84a9a Dimitris Aragiorgis
594 0ac84a9a Dimitris Aragiorgis
@roles("accounts")
595 fea067c8 Dimitris Aragiorgis
def astakos_register_components():
596 65c656c1 Giorgos Korfiatis
    debug(env.host, " * Register services in astakos...")
597 65c656c1 Giorgos Korfiatis
598 eb765213 Giorgos Korfiatis
    cyclades_base_url = "https://%s/cyclades" % env.env.cyclades.fqdn
599 eb765213 Giorgos Korfiatis
    pithos_base_url = "https://%s/pithos" % env.env.pithos.fqdn
600 eb765213 Giorgos Korfiatis
    astakos_base_url = "https://%s/astakos" % env.env.accounts.fqdn
601 65c656c1 Giorgos Korfiatis
602 0ac84a9a Dimitris Aragiorgis
    cmd = """
603 eb765213 Giorgos Korfiatis
    snf-manage component-add "home" --ui-url https://{0}
604 eb765213 Giorgos Korfiatis
    snf-manage component-add "cyclades" --base-url {1} --ui-url {1}/ui
605 eb765213 Giorgos Korfiatis
    snf-manage component-add "pithos" --base-url {2} --ui-url {2}/ui
606 eb765213 Giorgos Korfiatis
    snf-manage component-add "astakos" --base-url {3} --ui-url {3}/ui
607 65c656c1 Giorgos Korfiatis
    """.format(env.env.cms.fqdn, cyclades_base_url,
608 5fcd489e Dimitris Aragiorgis
               pithos_base_url, astakos_base_url)
609 65c656c1 Giorgos Korfiatis
    try_run(cmd)
610 0ac84a9a Dimitris Aragiorgis
611 0ac84a9a Dimitris Aragiorgis
612 0ac84a9a Dimitris Aragiorgis
@roles("accounts")
613 952a28c0 Sofia Papagiannaki
def astakos_register_pithos_view():
614 952a28c0 Sofia Papagiannaki
    debug(env.host, " * Register pithos view as oauth2 client...")
615 952a28c0 Sofia Papagiannaki
616 952a28c0 Sofia Papagiannaki
    pithos_base_url = "https://%s/pithos" % env.env.pithos.fqdn
617 952a28c0 Sofia Papagiannaki
618 952a28c0 Sofia Papagiannaki
    cmd = """
619 952a28c0 Sofia Papagiannaki
    snf-manage oauth2-client-add pithos-view --secret=12345 --is-trusted \
620 952a28c0 Sofia Papagiannaki
    --url {0}
621 952a28c0 Sofia Papagiannaki
    """.format('%s/ui/view' % pithos_base_url)
622 952a28c0 Sofia Papagiannaki
    try_run(cmd)
623 952a28c0 Sofia Papagiannaki
624 952a28c0 Sofia Papagiannaki
625 952a28c0 Sofia Papagiannaki
@roles("accounts")
626 0ac84a9a Dimitris Aragiorgis
def add_user():
627 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * adding user %s to astakos..." % env.env.user_email)
628 f1800130 Ilias Tsitsimpis
    email = env.env.user_email
629 f1800130 Ilias Tsitsimpis
    name = env.env.user_name
630 f1800130 Ilias Tsitsimpis
    lastname = env.env.user_lastname
631 f1800130 Ilias Tsitsimpis
    passwd = env.env.user_passwd
632 0ac84a9a Dimitris Aragiorgis
    cmd = """
633 0ac84a9a Dimitris Aragiorgis
    snf-manage user-add {0} {1} {2}
634 0ac84a9a Dimitris Aragiorgis
    """.format(email, name, lastname)
635 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
636 1d8cf97d Dimitris Aragiorgis
    with settings(host_string=env.env.db.ip):
637 0ac84a9a Dimitris Aragiorgis
        uid, user_auth_token, user_uuid = get_auth_token_from_db(email)
638 0ac84a9a Dimitris Aragiorgis
    cmd = """
639 0ac84a9a Dimitris Aragiorgis
    snf-manage user-modify --password {0} {1}
640 0ac84a9a Dimitris Aragiorgis
    """.format(passwd, uid)
641 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
642 0ac84a9a Dimitris Aragiorgis
643 0ac84a9a Dimitris Aragiorgis
644 0ac84a9a Dimitris Aragiorgis
@roles("accounts")
645 0ac84a9a Dimitris Aragiorgis
def activate_user(user_email=None):
646 0ac84a9a Dimitris Aragiorgis
    if not user_email:
647 f1800130 Ilias Tsitsimpis
        user_email = env.env.user_email
648 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Activate user %s..." % user_email)
649 1d8cf97d Dimitris Aragiorgis
    with settings(host_string=env.env.db.ip):
650 0ac84a9a Dimitris Aragiorgis
        uid, user_auth_token, user_uuid = get_auth_token_from_db(user_email)
651 0ac84a9a Dimitris Aragiorgis
652 0ac84a9a Dimitris Aragiorgis
    cmd = """
653 0ac84a9a Dimitris Aragiorgis
    snf-manage user-modify --verify {0}
654 0ac84a9a Dimitris Aragiorgis
    snf-manage user-modify --accept {0}
655 0ac84a9a Dimitris Aragiorgis
    """.format(uid)
656 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
657 0ac84a9a Dimitris Aragiorgis
658 f1800130 Ilias Tsitsimpis
659 0ac84a9a Dimitris Aragiorgis
@roles("accounts")
660 0ac84a9a Dimitris Aragiorgis
def setup_astakos():
661 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up snf-astakos-app...")
662 0ac84a9a Dimitris Aragiorgis
    setup_gunicorn()
663 0ac84a9a Dimitris Aragiorgis
    setup_apache()
664 0ac84a9a Dimitris Aragiorgis
    setup_webproject()
665 0ac84a9a Dimitris Aragiorgis
    install_package("python-django-south")
666 0ac84a9a Dimitris Aragiorgis
    install_package("snf-astakos-app")
667 0ac84a9a Dimitris Aragiorgis
    install_package("kamaki")
668 0ac84a9a Dimitris Aragiorgis
669 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/synnefo/astakos.conf"
670 0ac84a9a Dimitris Aragiorgis
    replace = {
671 f1800130 Ilias Tsitsimpis
        "ACCOUNTS": env.env.accounts.fqdn,
672 f1800130 Ilias Tsitsimpis
        "domain": env.env.domain,
673 f1800130 Ilias Tsitsimpis
        "CYCLADES": env.env.cyclades.fqdn,
674 f1800130 Ilias Tsitsimpis
        "PITHOS": env.env.pithos.fqdn,
675 0ac84a9a Dimitris Aragiorgis
    }
676 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
677 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl, mode=0644)
678 0ac84a9a Dimitris Aragiorgis
    if env.csrf_disable:
679 f1800130 Ilias Tsitsimpis
        cmd = """
680 0ac84a9a Dimitris Aragiorgis
cat <<EOF >> /etc/synnefo/astakos.conf
681 0ac84a9a Dimitris Aragiorgis
try:
682 0ac84a9a Dimitris Aragiorgis
  MIDDLEWARE_CLASSES.remove('django.middleware.csrf.CsrfViewMiddleware')
683 0ac84a9a Dimitris Aragiorgis
except:
684 0ac84a9a Dimitris Aragiorgis
  pass
685 0ac84a9a Dimitris Aragiorgis
EOF
686 0ac84a9a Dimitris Aragiorgis
"""
687 f1800130 Ilias Tsitsimpis
        try_run(cmd)
688 0ac84a9a Dimitris Aragiorgis
689 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
690 0ac84a9a Dimitris Aragiorgis
691 0ac84a9a Dimitris Aragiorgis
    cmd = """
692 0ac84a9a Dimitris Aragiorgis
    snf-manage syncdb --noinput
693 0ac84a9a Dimitris Aragiorgis
    snf-manage migrate im --delete-ghost-migrations
694 0ac84a9a Dimitris Aragiorgis
    snf-manage migrate quotaholder_app
695 952a28c0 Sofia Papagiannaki
    snf-manage migrate oa2
696 0ac84a9a Dimitris Aragiorgis
    """
697 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
698 0ac84a9a Dimitris Aragiorgis
699 65c656c1 Giorgos Korfiatis
700 0ac84a9a Dimitris Aragiorgis
@roles("accounts")
701 0ac84a9a Dimitris Aragiorgis
def get_service_details(service="pithos"):
702 f1800130 Ilias Tsitsimpis
    debug(env.host,
703 f1800130 Ilias Tsitsimpis
          " * Getting registered details for %s service..." % service)
704 b3adfb20 Christos Stavrakakis
    result = try_run("snf-manage component-list -o id,name,token")
705 0ac84a9a Dimitris Aragiorgis
    r = re.compile(r".*%s.*" % service, re.M)
706 b3adfb20 Christos Stavrakakis
    service_id, _, service_token = r.search(result).group().split()
707 0ac84a9a Dimitris Aragiorgis
    # print("%s: %s %s" % (service, service_id, service_token))
708 0ac84a9a Dimitris Aragiorgis
    return (service_id, service_token)
709 0ac84a9a Dimitris Aragiorgis
710 0ac84a9a Dimitris Aragiorgis
711 0ac84a9a Dimitris Aragiorgis
@roles("db")
712 0ac84a9a Dimitris Aragiorgis
def get_auth_token_from_db(user_email=None):
713 0ac84a9a Dimitris Aragiorgis
    if not user_email:
714 f1800130 Ilias Tsitsimpis
        user_email = env.env.user_email
715 f1800130 Ilias Tsitsimpis
    debug(env.host,
716 f1800130 Ilias Tsitsimpis
          " * Getting authentication token and uuid for user %s..."
717 f1800130 Ilias Tsitsimpis
          % user_email)
718 0ac84a9a Dimitris Aragiorgis
    cmd = """
719 f1800130 Ilias Tsitsimpis
echo "select id, auth_token, uuid, email from auth_user, im_astakosuser \
720 f1800130 Ilias Tsitsimpis
where auth_user.id = im_astakosuser.user_ptr_id and auth_user.email = '{0}';" \
721 f1800130 Ilias Tsitsimpis
> /tmp/psqlcmd
722 f1800130 Ilias Tsitsimpis
su - postgres -c  "psql -w -d snf_apps -f /tmp/psqlcmd"
723 f1800130 Ilias Tsitsimpis
""".format(user_email)
724 0ac84a9a Dimitris Aragiorgis
725 0ac84a9a Dimitris Aragiorgis
    result = try_run(cmd)
726 0ac84a9a Dimitris Aragiorgis
    r = re.compile(r"(\d+)[ |]*(\S+)[ |]*(\S+)[ |]*" + user_email, re.M)
727 0ac84a9a Dimitris Aragiorgis
    match = r.search(result)
728 0ac84a9a Dimitris Aragiorgis
    uid, user_auth_token, user_uuid = match.groups()
729 0ac84a9a Dimitris Aragiorgis
    # print("%s: %s %s %s" % ( user_email, uid, user_auth_token, user_uuid))
730 0ac84a9a Dimitris Aragiorgis
731 0ac84a9a Dimitris Aragiorgis
    return (uid, user_auth_token, user_uuid)
732 0ac84a9a Dimitris Aragiorgis
733 0ac84a9a Dimitris Aragiorgis
734 0ac84a9a Dimitris Aragiorgis
@roles("cms")
735 0ac84a9a Dimitris Aragiorgis
def cms_loaddata():
736 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Loading cms initial data...")
737 0ac84a9a Dimitris Aragiorgis
    if env.cms_pass:
738 f1800130 Ilias Tsitsimpis
        debug(env.host, "Aborting. Prerequisites not met.")
739 f1800130 Ilias Tsitsimpis
        return
740 0ac84a9a Dimitris Aragiorgis
    tmpl = "/tmp/sites.json"
741 0ac84a9a Dimitris Aragiorgis
    replace = {}
742 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
743 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl)
744 0ac84a9a Dimitris Aragiorgis
745 0ac84a9a Dimitris Aragiorgis
    tmpl = "/tmp/page.json"
746 0ac84a9a Dimitris Aragiorgis
    replace = {}
747 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
748 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl)
749 0ac84a9a Dimitris Aragiorgis
750 0ac84a9a Dimitris Aragiorgis
    cmd = """
751 0ac84a9a Dimitris Aragiorgis
    snf-manage loaddata /tmp/sites.json
752 0ac84a9a Dimitris Aragiorgis
    snf-manage loaddata /tmp/page.json
753 0ac84a9a Dimitris Aragiorgis
    snf-manage createsuperuser --username=admin --email=admin@{0} --noinput
754 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.domain)
755 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
756 0ac84a9a Dimitris Aragiorgis
757 0ac84a9a Dimitris Aragiorgis
758 0ac84a9a Dimitris Aragiorgis
@roles("cms")
759 0ac84a9a Dimitris Aragiorgis
def setup_cms():
760 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up cms...")
761 0ac84a9a Dimitris Aragiorgis
    if env.cms_pass:
762 f1800130 Ilias Tsitsimpis
        debug(env.host, "Aborting. Prerequisites not met.")
763 f1800130 Ilias Tsitsimpis
        return
764 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
765 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 accounts." + env.env.domain)
766 0ac84a9a Dimitris Aragiorgis
    setup_gunicorn()
767 0ac84a9a Dimitris Aragiorgis
    setup_apache()
768 0ac84a9a Dimitris Aragiorgis
    setup_webproject()
769 0ac84a9a Dimitris Aragiorgis
    install_package("snf-cloudcms")
770 0ac84a9a Dimitris Aragiorgis
771 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/synnefo/cms.conf"
772 0ac84a9a Dimitris Aragiorgis
    replace = {
773 0ac84a9a Dimitris Aragiorgis
        "ACCOUNTS": env.env.accounts.fqdn,
774 0ac84a9a Dimitris Aragiorgis
        }
775 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
776 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl, mode=0644)
777 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
778 0ac84a9a Dimitris Aragiorgis
779 0ac84a9a Dimitris Aragiorgis
    cmd = """
780 0ac84a9a Dimitris Aragiorgis
    snf-manage syncdb
781 0ac84a9a Dimitris Aragiorgis
    snf-manage migrate --delete-ghost-migrations
782 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.domain)
783 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
784 0ac84a9a Dimitris Aragiorgis
785 0ac84a9a Dimitris Aragiorgis
786 0ac84a9a Dimitris Aragiorgis
def setup_nfs_dirs():
787 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Creating NFS mount point for pithos and ganeti...")
788 0ac84a9a Dimitris Aragiorgis
    cmd = """
789 0ac84a9a Dimitris Aragiorgis
    mkdir -p {0}
790 0ac84a9a Dimitris Aragiorgis
    cd {0}
791 0ac84a9a Dimitris Aragiorgis
    mkdir -p data
792 0ac84a9a Dimitris Aragiorgis
    chown www-data:www-data data
793 0ac84a9a Dimitris Aragiorgis
    chmod g+ws data
794 093d25d8 Dimitris Aragiorgis
    mkdir -p {1}
795 093d25d8 Dimitris Aragiorgis
    """.format(env.env.pithos_dir, env.env.image_dir)
796 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
797 0ac84a9a Dimitris Aragiorgis
798 0ac84a9a Dimitris Aragiorgis
799 0ac84a9a Dimitris Aragiorgis
@roles("nodes")
800 0ac84a9a Dimitris Aragiorgis
def setup_nfs_clients():
801 1d8cf97d Dimitris Aragiorgis
    if env.host == env.env.pithos.ip:
802 f1800130 Ilias Tsitsimpis
        return
803 0ac84a9a Dimitris Aragiorgis
804 9f93a6fb Dimitris Aragiorgis
    host_info = env.env.ips_info[env.host]
805 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Mounting pithos NFS mount point...")
806 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
807 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 " + env.env.pithos.hostname)
808 9f93a6fb Dimitris Aragiorgis
    with settings(host_string=env.env.pithos.ip):
809 9f93a6fb Dimitris Aragiorgis
        update_nfs_exports(host_info.ip)
810 9f93a6fb Dimitris Aragiorgis
811 0ac84a9a Dimitris Aragiorgis
    install_package("nfs-common")
812 9f93a6fb Dimitris Aragiorgis
    for d in [env.env.pithos_dir, env.env.image_dir]:
813 f1800130 Ilias Tsitsimpis
        try_run("mkdir -p " + d)
814 f1800130 Ilias Tsitsimpis
        cmd = """
815 f1800130 Ilias Tsitsimpis
echo "{0}:{1} {1}  nfs defaults,rw,noatime,rsize=131072,\
816 f1800130 Ilias Tsitsimpis
wsize=131072,timeo=14,intr,noacl" >> /etc/fstab
817 f1800130 Ilias Tsitsimpis
""".format(env.env.pithos.ip, d)
818 f1800130 Ilias Tsitsimpis
        try_run(cmd)
819 f1800130 Ilias Tsitsimpis
        try_run("mount " + d)
820 f1800130 Ilias Tsitsimpis
821 0ac84a9a Dimitris Aragiorgis
822 0ac84a9a Dimitris Aragiorgis
@roles("pithos")
823 9f93a6fb Dimitris Aragiorgis
def update_nfs_exports(ip):
824 9f93a6fb Dimitris Aragiorgis
    tmpl = "/tmp/exports"
825 0ac84a9a Dimitris Aragiorgis
    replace = {
826 f1800130 Ilias Tsitsimpis
        "pithos_dir": env.env.pithos_dir,
827 f1800130 Ilias Tsitsimpis
        "image_dir": env.env.image_dir,
828 f1800130 Ilias Tsitsimpis
        "ip": ip,
829 f1800130 Ilias Tsitsimpis
    }
830 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
831 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl)
832 9f93a6fb Dimitris Aragiorgis
    try_run("cat %s >> /etc/exports" % tmpl)
833 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/nfs-kernel-server restart")
834 0ac84a9a Dimitris Aragiorgis
835 f1800130 Ilias Tsitsimpis
836 9f93a6fb Dimitris Aragiorgis
@roles("pithos")
837 9f93a6fb Dimitris Aragiorgis
def setup_nfs_server():
838 9f93a6fb Dimitris Aragiorgis
    debug(env.host, " * Setting up NFS server for pithos...")
839 9f93a6fb Dimitris Aragiorgis
    setup_nfs_dirs()
840 9f93a6fb Dimitris Aragiorgis
    install_package("nfs-kernel-server")
841 9f93a6fb Dimitris Aragiorgis
842 0ac84a9a Dimitris Aragiorgis
843 0ac84a9a Dimitris Aragiorgis
@roles("pithos")
844 0ac84a9a Dimitris Aragiorgis
def setup_pithos():
845 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up snf-pithos-app...")
846 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
847 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 accounts." + env.env.domain)
848 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 " + env.env.db.ip)
849 0ac84a9a Dimitris Aragiorgis
    setup_gunicorn()
850 0ac84a9a Dimitris Aragiorgis
    setup_apache()
851 0ac84a9a Dimitris Aragiorgis
    setup_webproject()
852 0ac84a9a Dimitris Aragiorgis
853 1d8cf97d Dimitris Aragiorgis
    with settings(host_string=env.env.accounts.ip):
854 0ac84a9a Dimitris Aragiorgis
        service_id, service_token = get_service_details("pithos")
855 0ac84a9a Dimitris Aragiorgis
856 0ac84a9a Dimitris Aragiorgis
    install_package("kamaki")
857 0ac84a9a Dimitris Aragiorgis
    install_package("snf-pithos-backend")
858 0ac84a9a Dimitris Aragiorgis
    install_package("snf-pithos-app")
859 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/synnefo/pithos.conf"
860 0ac84a9a Dimitris Aragiorgis
    replace = {
861 0ac84a9a Dimitris Aragiorgis
        "ACCOUNTS": env.env.accounts.fqdn,
862 0ac84a9a Dimitris Aragiorgis
        "PITHOS": env.env.pithos.fqdn,
863 0ac84a9a Dimitris Aragiorgis
        "db_node": env.env.db.ip,
864 0ac84a9a Dimitris Aragiorgis
        "synnefo_user": env.env.synnefo_user,
865 0ac84a9a Dimitris Aragiorgis
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
866 0ac84a9a Dimitris Aragiorgis
        "pithos_dir": env.env.pithos_dir,
867 0ac84a9a Dimitris Aragiorgis
        "PITHOS_SERVICE_TOKEN": service_token,
868 0ac84a9a Dimitris Aragiorgis
        }
869 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
870 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl, mode=0644)
871 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
872 0ac84a9a Dimitris Aragiorgis
873 0ac84a9a Dimitris Aragiorgis
    install_package("snf-pithos-webclient")
874 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/synnefo/webclient.conf"
875 0ac84a9a Dimitris Aragiorgis
    replace = {
876 0ac84a9a Dimitris Aragiorgis
        "ACCOUNTS": env.env.accounts.fqdn,
877 0ac84a9a Dimitris Aragiorgis
        "PITHOS_UI_CLOUDBAR_ACTIVE_SERVICE": service_id,
878 0ac84a9a Dimitris Aragiorgis
        }
879 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
880 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl, mode=0644)
881 0ac84a9a Dimitris Aragiorgis
882 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
883 0ac84a9a Dimitris Aragiorgis
    #TOFIX: the previous command lets pithos-backend create blocks and maps
884 0ac84a9a Dimitris Aragiorgis
    #       with root owner
885 0ac84a9a Dimitris Aragiorgis
    try_run("chown -R www-data:www-data %s/data " % env.env.pithos_dir)
886 0ac84a9a Dimitris Aragiorgis
    #try_run("pithos-migrate stamp 4c8ccdc58192")
887 0ac84a9a Dimitris Aragiorgis
    #try_run("pithos-migrate upgrade head")
888 0ac84a9a Dimitris Aragiorgis
889 0ac84a9a Dimitris Aragiorgis
890 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
891 0ac84a9a Dimitris Aragiorgis
def setup_ganeti():
892 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up snf-ganeti...")
893 0ac84a9a Dimitris Aragiorgis
    node_info = env.env.ips_info[env.host]
894 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
895 0ac84a9a Dimitris Aragiorgis
        #if env.enable_lvm:
896 0ac84a9a Dimitris Aragiorgis
        #    try_run("vgs " + env.env.vg)
897 0ac84a9a Dimitris Aragiorgis
        try_run("getent hosts " + env.env.cluster.fqdn)
898 0ac84a9a Dimitris Aragiorgis
        try_run("getent hosts %s | grep -v ^127" % env.host)
899 0ac84a9a Dimitris Aragiorgis
        try_run("hostname -f | grep " + node_info.fqdn)
900 0ac84a9a Dimitris Aragiorgis
        #try_run("ip link show " + env.env.common_bridge)
901 0ac84a9a Dimitris Aragiorgis
        #try_run("ip link show " + env.env.common_bridge)
902 0ac84a9a Dimitris Aragiorgis
        #try_run("apt-get update")
903 0ac84a9a Dimitris Aragiorgis
    install_package("qemu-kvm")
904 0ac84a9a Dimitris Aragiorgis
    install_package("python-bitarray")
905 03f01e0d Dimitris Aragiorgis
    install_package("ganeti-haskell")
906 0ac84a9a Dimitris Aragiorgis
    install_package("ganeti-htools")
907 0ac84a9a Dimitris Aragiorgis
    install_package("snf-ganeti")
908 0ac84a9a Dimitris Aragiorgis
    try_run("mkdir -p /srv/ganeti/file-storage/")
909 0ac84a9a Dimitris Aragiorgis
    cmd = """
910 0ac84a9a Dimitris Aragiorgis
cat <<EOF > /etc/ganeti/file-storage-paths
911 0ac84a9a Dimitris Aragiorgis
/srv/ganeti/file-storage
912 0ac84a9a Dimitris Aragiorgis
/srv/ganeti/shared-file-storage
913 0ac84a9a Dimitris Aragiorgis
EOF
914 0ac84a9a Dimitris Aragiorgis
"""
915 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
916 0ac84a9a Dimitris Aragiorgis
917 0ac84a9a Dimitris Aragiorgis
918 0ac84a9a Dimitris Aragiorgis
@roles("master")
919 0ac84a9a Dimitris Aragiorgis
def add_rapi_user():
920 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Adding RAPI user to Ganeti backend...")
921 0ac84a9a Dimitris Aragiorgis
    cmd = """
922 2242394d Dimitris Aragiorgis
    echo -n "{0}:Ganeti Remote API:{1}" | openssl md5 | sed 's/^.* //'
923 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.synnefo_user, env.env.synnefo_rapi_passwd)
924 0ac84a9a Dimitris Aragiorgis
    result = try_run(cmd)
925 b249aba9 Christos Stavrakakis
    if result.startswith("(stdin)= "):
926 b249aba9 Christos Stavrakakis
        result = result.split("(stdin)= ")[1]
927 0ac84a9a Dimitris Aragiorgis
    cmd = """
928 0ac84a9a Dimitris Aragiorgis
    echo "{0} {1}{2} write" >> /var/lib/ganeti/rapi/users
929 f1800130 Ilias Tsitsimpis
    """.format(env.env.synnefo_user, '{ha1}', result)
930 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
931 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/ganeti restart")
932 0ac84a9a Dimitris Aragiorgis
933 f1800130 Ilias Tsitsimpis
934 0ac84a9a Dimitris Aragiorgis
@roles("master")
935 0ac84a9a Dimitris Aragiorgis
def add_nodes():
936 0ac84a9a Dimitris Aragiorgis
    nodes = env.env.cluster_nodes.split(",")
937 0ac84a9a Dimitris Aragiorgis
    nodes.remove(env.env.master_node)
938 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Adding nodes to Ganeti backend...")
939 0ac84a9a Dimitris Aragiorgis
    for n in nodes:
940 0ac84a9a Dimitris Aragiorgis
        add_node(n)
941 0ac84a9a Dimitris Aragiorgis
942 f1800130 Ilias Tsitsimpis
943 0ac84a9a Dimitris Aragiorgis
@roles("master")
944 0ac84a9a Dimitris Aragiorgis
def add_node(node):
945 0ac84a9a Dimitris Aragiorgis
    node_info = env.env.nodes_info[node]
946 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Adding node %s to Ganeti backend..." % node_info.fqdn)
947 f1800130 Ilias Tsitsimpis
    cmd = "gnt-node add --no-ssh-key-check --master-capable=yes " + \
948 f1800130 Ilias Tsitsimpis
          "--vm-capable=yes " + node_info.fqdn
949 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
950 0ac84a9a Dimitris Aragiorgis
951 f1800130 Ilias Tsitsimpis
952 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
953 0ac84a9a Dimitris Aragiorgis
def enable_drbd():
954 0ac84a9a Dimitris Aragiorgis
    if env.enable_drbd:
955 0ac84a9a Dimitris Aragiorgis
        debug(env.host, " * Enabling DRBD...")
956 04660f63 Dimitris Aragiorgis
        install_package("drbd8-utils")
957 0ac84a9a Dimitris Aragiorgis
        try_run("modprobe drbd minor_count=255 usermode_helper=/bin/true")
958 f1800130 Ilias Tsitsimpis
        try_run("echo drbd minor_count=255 usermode_helper=/bin/true " +
959 f1800130 Ilias Tsitsimpis
                ">> /etc/modules")
960 f1800130 Ilias Tsitsimpis
961 0ac84a9a Dimitris Aragiorgis
962 0ac84a9a Dimitris Aragiorgis
@roles("master")
963 0ac84a9a Dimitris Aragiorgis
def setup_drbd_dparams():
964 0ac84a9a Dimitris Aragiorgis
    if env.enable_drbd:
965 f1800130 Ilias Tsitsimpis
        debug(env.host,
966 f1800130 Ilias Tsitsimpis
              " * Twicking drbd related disk parameters in Ganeti...")
967 0ac84a9a Dimitris Aragiorgis
        cmd = """
968 0ac84a9a Dimitris Aragiorgis
        gnt-cluster modify --disk-parameters=drbd:metavg={0}
969 0ac84a9a Dimitris Aragiorgis
        gnt-group modify --disk-parameters=drbd:metavg={0} default
970 0ac84a9a Dimitris Aragiorgis
        """.format(env.env.vg)
971 0ac84a9a Dimitris Aragiorgis
        try_run(cmd)
972 0ac84a9a Dimitris Aragiorgis
973 f1800130 Ilias Tsitsimpis
974 0ac84a9a Dimitris Aragiorgis
@roles("master")
975 0ac84a9a Dimitris Aragiorgis
def enable_lvm():
976 0ac84a9a Dimitris Aragiorgis
    if env.enable_lvm:
977 0ac84a9a Dimitris Aragiorgis
        debug(env.host, " * Enabling LVM...")
978 0ac84a9a Dimitris Aragiorgis
        cmd = """
979 0ac84a9a Dimitris Aragiorgis
        gnt-cluster modify --vg-name={0}
980 0ac84a9a Dimitris Aragiorgis
        """.format(env.env.vg)
981 0ac84a9a Dimitris Aragiorgis
        try_run(cmd)
982 0ac84a9a Dimitris Aragiorgis
    else:
983 0ac84a9a Dimitris Aragiorgis
        debug(env.host, " * Disabling LVM...")
984 0ac84a9a Dimitris Aragiorgis
        try_run("gnt-cluster modify --no-lvm-storage")
985 0ac84a9a Dimitris Aragiorgis
986 f1800130 Ilias Tsitsimpis
987 0ac84a9a Dimitris Aragiorgis
@roles("master")
988 0ac84a9a Dimitris Aragiorgis
def destroy_cluster():
989 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Destroying Ganeti cluster...")
990 0ac84a9a Dimitris Aragiorgis
    #TODO: remove instances first
991 0ac84a9a Dimitris Aragiorgis
    allnodes = env.env.cluster_hostnames[:]
992 0ac84a9a Dimitris Aragiorgis
    allnodes.remove(env.host)
993 0ac84a9a Dimitris Aragiorgis
    for n in allnodes:
994 f1800130 Ilias Tsitsimpis
        host_info = env.env.ips_info[env.host]
995 f1800130 Ilias Tsitsimpis
        debug(env.host, " * Removing node %s..." % n)
996 f1800130 Ilias Tsitsimpis
        cmd = "gnt-node remove  " + host_info.fqdn
997 f1800130 Ilias Tsitsimpis
        try_run(cmd)
998 0ac84a9a Dimitris Aragiorgis
    try_run("gnt-cluster destroy --yes-do-it")
999 0ac84a9a Dimitris Aragiorgis
1000 0ac84a9a Dimitris Aragiorgis
1001 0ac84a9a Dimitris Aragiorgis
@roles("master")
1002 0ac84a9a Dimitris Aragiorgis
def init_cluster():
1003 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Initializing Ganeti backend...")
1004 0ac84a9a Dimitris Aragiorgis
    # extra = ""
1005 0ac84a9a Dimitris Aragiorgis
    # if env.enable_lvm:
1006 0ac84a9a Dimitris Aragiorgis
    #     extra += " --vg-name={0} ".format(env.env.vg)
1007 0ac84a9a Dimitris Aragiorgis
    # else:
1008 0ac84a9a Dimitris Aragiorgis
    #     extra += " --no-lvm-storage "
1009 0ac84a9a Dimitris Aragiorgis
    # if not env.enable_drbd:
1010 0ac84a9a Dimitris Aragiorgis
    #     extra += " --no-drbd-storage "
1011 0ac84a9a Dimitris Aragiorgis
    extra = " --no-lvm-storage --no-drbd-storage "
1012 0ac84a9a Dimitris Aragiorgis
    cmd = """
1013 0ac84a9a Dimitris Aragiorgis
    gnt-cluster init --enabled-hypervisors=kvm \
1014 f1800130 Ilias Tsitsimpis
        {0} \
1015 f1800130 Ilias Tsitsimpis
        --nic-parameters link={1},mode=bridged \
1016 f1800130 Ilias Tsitsimpis
        --master-netdev {2} \
1017 f1800130 Ilias Tsitsimpis
        --default-iallocator hail \
1018 c6e90126 Christos Stavrakakis
        --specs-nic-count min=0,max=8 \
1019 f1800130 Ilias Tsitsimpis
        --hypervisor-parameters kvm:kernel_path=,vnc_bind_address=0.0.0.0 \
1020 f1800130 Ilias Tsitsimpis
        --no-ssh-init --no-etc-hosts \
1021 f1800130 Ilias Tsitsimpis
        {3}
1022 0ac84a9a Dimitris Aragiorgis
    """.format(extra, env.env.common_bridge,
1023 0ac84a9a Dimitris Aragiorgis
               env.env.cluster_netdev, env.env.cluster.fqdn)
1024 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1025 d88c9628 Christos Stavrakakis
    cmd = """gnt-cluster modify --enabled-disk-templates file,plain,ext"""
1026 d88c9628 Christos Stavrakakis
    try_run(cmd)
1027 0ac84a9a Dimitris Aragiorgis
1028 0ac84a9a Dimitris Aragiorgis
1029 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
1030 0ac84a9a Dimitris Aragiorgis
def debootstrap():
1031 0ac84a9a Dimitris Aragiorgis
    install_package("ganeti-instance-debootstrap")
1032 0ac84a9a Dimitris Aragiorgis
1033 0ac84a9a Dimitris Aragiorgis
1034 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
1035 0ac84a9a Dimitris Aragiorgis
def setup_image_host():
1036 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up snf-image...")
1037 0ac84a9a Dimitris Aragiorgis
    install_package("snf-pithos-backend")
1038 0ac84a9a Dimitris Aragiorgis
    install_package("snf-image")
1039 9f93a6fb Dimitris Aragiorgis
    try_run("mkdir -p %s" % env.env.image_dir)
1040 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/default/snf-image"
1041 0ac84a9a Dimitris Aragiorgis
    replace = {
1042 0ac84a9a Dimitris Aragiorgis
        "synnefo_user": env.env.synnefo_user,
1043 0ac84a9a Dimitris Aragiorgis
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
1044 0ac84a9a Dimitris Aragiorgis
        "pithos_dir": env.env.pithos_dir,
1045 0ac84a9a Dimitris Aragiorgis
        "db_node": env.env.db.ip,
1046 093d25d8 Dimitris Aragiorgis
        "image_dir": env.env.image_dir,
1047 0ac84a9a Dimitris Aragiorgis
    }
1048 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
1049 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl)
1050 0ac84a9a Dimitris Aragiorgis
1051 0ac84a9a Dimitris Aragiorgis
1052 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
1053 0ac84a9a Dimitris Aragiorgis
def setup_image_helper():
1054 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Updating helper image...")
1055 0ac84a9a Dimitris Aragiorgis
    cmd = """
1056 0ac84a9a Dimitris Aragiorgis
    snf-image-update-helper -y
1057 0ac84a9a Dimitris Aragiorgis
    """
1058 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1059 0ac84a9a Dimitris Aragiorgis
1060 0ac84a9a Dimitris Aragiorgis
1061 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
1062 0ac84a9a Dimitris Aragiorgis
def setup_gtools():
1063 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up snf-cyclades-gtools...")
1064 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
1065 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 " + env.env.mq.ip)
1066 0ac84a9a Dimitris Aragiorgis
    setup_common()
1067 0ac84a9a Dimitris Aragiorgis
    install_package("snf-cyclades-gtools")
1068 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/synnefo/gtools.conf"
1069 0ac84a9a Dimitris Aragiorgis
    replace = {
1070 0ac84a9a Dimitris Aragiorgis
        "synnefo_user": env.env.synnefo_user,
1071 0ac84a9a Dimitris Aragiorgis
        "synnefo_rabbitmq_passwd": env.env.synnefo_rabbitmq_passwd,
1072 0ac84a9a Dimitris Aragiorgis
        "mq_node": env.env.mq.ip,
1073 0ac84a9a Dimitris Aragiorgis
    }
1074 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
1075 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl)
1076 0ac84a9a Dimitris Aragiorgis
1077 0ac84a9a Dimitris Aragiorgis
    cmd = """
1078 0ac84a9a Dimitris Aragiorgis
    sed -i 's/false/true/' /etc/default/snf-ganeti-eventd
1079 0ac84a9a Dimitris Aragiorgis
    /etc/init.d/snf-ganeti-eventd start
1080 0ac84a9a Dimitris Aragiorgis
    """
1081 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1082 0ac84a9a Dimitris Aragiorgis
1083 0ac84a9a Dimitris Aragiorgis
1084 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
1085 0ac84a9a Dimitris Aragiorgis
def setup_iptables():
1086 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up iptables to mangle DHCP requests...")
1087 0ac84a9a Dimitris Aragiorgis
    cmd = """
1088 f1800130 Ilias Tsitsimpis
    iptables -t mangle -A PREROUTING -i br+ -p udp -m udp --dport 67 \
1089 f1800130 Ilias Tsitsimpis
            -j NFQUEUE --queue-num 42
1090 f1800130 Ilias Tsitsimpis
    iptables -t mangle -A PREROUTING -i tap+ -p udp -m udp --dport 67 \
1091 f1800130 Ilias Tsitsimpis
            -j NFQUEUE --queue-num 42
1092 f1800130 Ilias Tsitsimpis
    iptables -t mangle -A PREROUTING -i prv+ -p udp -m udp --dport 67 \
1093 f1800130 Ilias Tsitsimpis
            -j NFQUEUE --queue-num 42
1094 f1800130 Ilias Tsitsimpis

1095 f1800130 Ilias Tsitsimpis
    ip6tables -t mangle -A PREROUTING -i br+ -p ipv6-icmp -m icmp6 \
1096 f1800130 Ilias Tsitsimpis
            --icmpv6-type 133 -j NFQUEUE --queue-num 43
1097 f1800130 Ilias Tsitsimpis
    ip6tables -t mangle -A PREROUTING -i br+ -p ipv6-icmp -m icmp6 \
1098 f1800130 Ilias Tsitsimpis
            --icmpv6-type 135 -j NFQUEUE --queue-num 44
1099 0ac84a9a Dimitris Aragiorgis
    """
1100 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1101 0ac84a9a Dimitris Aragiorgis
1102 f1800130 Ilias Tsitsimpis
1103 0ac84a9a Dimitris Aragiorgis
@roles("ganeti")
1104 0ac84a9a Dimitris Aragiorgis
def setup_network():
1105 f1800130 Ilias Tsitsimpis
    debug(env.host,
1106 f1800130 Ilias Tsitsimpis
          "Setting up networking for Ganeti instances (nfdhcpd, etc.)...")
1107 03f01e0d Dimitris Aragiorgis
    install_package("python-nfqueue")
1108 0ac84a9a Dimitris Aragiorgis
    install_package("nfdhcpd")
1109 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/nfdhcpd/nfdhcpd.conf"
1110 0ac84a9a Dimitris Aragiorgis
    replace = {
1111 f1800130 Ilias Tsitsimpis
        "ns_node_ip": env.env.ns.ip
1112 f1800130 Ilias Tsitsimpis
    }
1113 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
1114 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl)
1115 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/nfdhcpd restart")
1116 0ac84a9a Dimitris Aragiorgis
1117 0ac84a9a Dimitris Aragiorgis
    install_package("snf-network")
1118 0ac84a9a Dimitris Aragiorgis
    cmd = """
1119 f1800130 Ilias Tsitsimpis
sed -i 's/MAC_MASK.*/MAC_MASK = ff:ff:f0:00:00:00/' /etc/default/snf-network
1120 0ac84a9a Dimitris Aragiorgis
    """
1121 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1122 0ac84a9a Dimitris Aragiorgis
1123 0ac84a9a Dimitris Aragiorgis
1124 0ac84a9a Dimitris Aragiorgis
@roles("router")
1125 0ac84a9a Dimitris Aragiorgis
def setup_router():
1126 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up internal router for NAT...")
1127 0ac84a9a Dimitris Aragiorgis
    cmd = """
1128 0ac84a9a Dimitris Aragiorgis
    echo 1 > /proc/sys/net/ipv4/ip_forward
1129 0ac84a9a Dimitris Aragiorgis
    iptables -t nat -A POSTROUTING -s {0} -o {3} -j MASQUERADE
1130 0ac84a9a Dimitris Aragiorgis
    ip addr add {1} dev {2}
1131 0ac84a9a Dimitris Aragiorgis
    ip route add {0} dev {2} src {1}
1132 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.synnefo_public_network_subnet,
1133 0ac84a9a Dimitris Aragiorgis
               env.env.synnefo_public_network_gateway,
1134 0ac84a9a Dimitris Aragiorgis
               env.env.common_bridge, env.env.public_iface)
1135 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1136 0ac84a9a Dimitris Aragiorgis
1137 6c51153a Dionysis Grigoropoulos
1138 0ac84a9a Dimitris Aragiorgis
@roles("cyclades")
1139 0ac84a9a Dimitris Aragiorgis
def cyclades_loaddata():
1140 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Loading initial data for cyclades...")
1141 6c51153a Dionysis Grigoropoulos
    try_run("snf-manage flavor-create %s %s %s %s" % (env.env.flavor_cpu,
1142 6c51153a Dionysis Grigoropoulos
                                                      env.env.flavor_ram,
1143 6c51153a Dionysis Grigoropoulos
                                                      env.env.flavor_disk,
1144 6c51153a Dionysis Grigoropoulos
                                                      env.env.flavor_storage))
1145 0ac84a9a Dimitris Aragiorgis
    #run("snf-manage loaddata flavors")
1146 0ac84a9a Dimitris Aragiorgis
1147 0ac84a9a Dimitris Aragiorgis
1148 0cfa5e03 Stratos Psomadakis
@roles("ganeti", "stats")
1149 0cfa5e03 Stratos Psomadakis
def setup_collectd():
1150 0cfa5e03 Stratos Psomadakis
    install_package("collectd")
1151 0cfa5e03 Stratos Psomadakis
    tmpl = "/etc/collectd/collectd.conf"
1152 0cfa5e03 Stratos Psomadakis
    replace = {}
1153 0cfa5e03 Stratos Psomadakis
    custom = customize_settings_from_tmpl(tmpl, replace)
1154 0cfa5e03 Stratos Psomadakis
    try_put(custom, tmpl, mode=0644)
1155 0cfa5e03 Stratos Psomadakis
1156 0cfa5e03 Stratos Psomadakis
1157 0cfa5e03 Stratos Psomadakis
@roles("ganeti")
1158 0cfa5e03 Stratos Psomadakis
def setup_ganeti_collectd():
1159 0cfa5e03 Stratos Psomadakis
    setup_collectd()
1160 0cfa5e03 Stratos Psomadakis
1161 0cfa5e03 Stratos Psomadakis
    tmpl = "/etc/collectd/passwd"
1162 0cfa5e03 Stratos Psomadakis
    replace = {}
1163 0cfa5e03 Stratos Psomadakis
    custom = customize_settings_from_tmpl(tmpl, replace)
1164 0cfa5e03 Stratos Psomadakis
    try_put(custom, tmpl, mode=0644)
1165 0cfa5e03 Stratos Psomadakis
1166 0cfa5e03 Stratos Psomadakis
    tmpl = "/etc/collectd/synnefo-ganeti.conf"
1167 0cfa5e03 Stratos Psomadakis
    replace = {
1168 0cfa5e03 Stratos Psomadakis
        "STATS": env.env.stats.fqdn,
1169 0cfa5e03 Stratos Psomadakis
        }
1170 0cfa5e03 Stratos Psomadakis
    custom = customize_settings_from_tmpl(tmpl, replace)
1171 0cfa5e03 Stratos Psomadakis
    try_put(custom, tmpl, mode=0644)
1172 0cfa5e03 Stratos Psomadakis
1173 0cfa5e03 Stratos Psomadakis
    try_run("/etc/init.d/collectd restart")
1174 0cfa5e03 Stratos Psomadakis
1175 0cfa5e03 Stratos Psomadakis
1176 0cfa5e03 Stratos Psomadakis
@roles("stats")
1177 0cfa5e03 Stratos Psomadakis
def setup_stats_collectd():
1178 0cfa5e03 Stratos Psomadakis
    setup_collectd()
1179 0cfa5e03 Stratos Psomadakis
    tmpl = "/etc/collectd/synnefo-stats.conf"
1180 0cfa5e03 Stratos Psomadakis
1181 0cfa5e03 Stratos Psomadakis
    replace = {
1182 0cfa5e03 Stratos Psomadakis
        "STATS": env.env.stats.fqdn,
1183 0cfa5e03 Stratos Psomadakis
        }
1184 0cfa5e03 Stratos Psomadakis
    custom = customize_settings_from_tmpl(tmpl, replace)
1185 0cfa5e03 Stratos Psomadakis
    try_put(custom, tmpl, mode=0644)
1186 0cfa5e03 Stratos Psomadakis
    try_run("/etc/init.d/collectd restart")
1187 0cfa5e03 Stratos Psomadakis
1188 0cfa5e03 Stratos Psomadakis
1189 0cfa5e03 Stratos Psomadakis
@roles("stats")
1190 0cfa5e03 Stratos Psomadakis
def setup_stats():
1191 0cfa5e03 Stratos Psomadakis
    debug(env.host, "Setting up snf-stats-app...")
1192 0cfa5e03 Stratos Psomadakis
    setup_stats_collectd()
1193 0cfa5e03 Stratos Psomadakis
    setup_gunicorn()
1194 0cfa5e03 Stratos Psomadakis
    setup_apache()
1195 0cfa5e03 Stratos Psomadakis
    setup_webproject()
1196 0cfa5e03 Stratos Psomadakis
    install_package("snf-stats-app")
1197 0cfa5e03 Stratos Psomadakis
    cmd = """
1198 0cfa5e03 Stratos Psomadakis
    mkdir /var/cache/snf-stats-app/
1199 0cfa5e03 Stratos Psomadakis
    chown www-data:www-data /var/cache/snf-stats-app/
1200 0cfa5e03 Stratos Psomadakis
    """
1201 0cfa5e03 Stratos Psomadakis
    try_run(cmd)
1202 0cfa5e03 Stratos Psomadakis
    tmpl = "/etc/synnefo/stats.conf"
1203 0cfa5e03 Stratos Psomadakis
1204 0cfa5e03 Stratos Psomadakis
    replace = {
1205 0cfa5e03 Stratos Psomadakis
        "STATS": env.env.stats.fqdn,
1206 0cfa5e03 Stratos Psomadakis
        }
1207 0cfa5e03 Stratos Psomadakis
    custom = customize_settings_from_tmpl(tmpl, replace)
1208 0cfa5e03 Stratos Psomadakis
    try_put(custom, tmpl, mode=0644)
1209 0cfa5e03 Stratos Psomadakis
    try_run("/etc/init.d/gunicorn restart")
1210 0cfa5e03 Stratos Psomadakis
1211 0cfa5e03 Stratos Psomadakis
1212 0ac84a9a Dimitris Aragiorgis
@roles("cyclades")
1213 0ac84a9a Dimitris Aragiorgis
def setup_cyclades():
1214 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up snf-cyclades-app...")
1215 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
1216 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 accounts." + env.env.domain)
1217 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 " + env.env.db.ip)
1218 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 " + env.env.mq.ip)
1219 0ac84a9a Dimitris Aragiorgis
    setup_gunicorn()
1220 0ac84a9a Dimitris Aragiorgis
    setup_apache()
1221 0ac84a9a Dimitris Aragiorgis
    setup_webproject()
1222 0ac84a9a Dimitris Aragiorgis
    install_package("memcached")
1223 0ac84a9a Dimitris Aragiorgis
    install_package("python-memcache")
1224 0ac84a9a Dimitris Aragiorgis
    install_package("snf-pithos-backend")
1225 0ac84a9a Dimitris Aragiorgis
    install_package("kamaki")
1226 0ac84a9a Dimitris Aragiorgis
    install_package("snf-cyclades-app")
1227 0ac84a9a Dimitris Aragiorgis
    install_package("python-django-south")
1228 0ac84a9a Dimitris Aragiorgis
    tmpl = "/etc/synnefo/cyclades.conf"
1229 0ac84a9a Dimitris Aragiorgis
1230 1d8cf97d Dimitris Aragiorgis
    with settings(host_string=env.env.accounts.ip):
1231 0ac84a9a Dimitris Aragiorgis
        service_id, service_token = get_service_details("cyclades")
1232 0ac84a9a Dimitris Aragiorgis
1233 0ac84a9a Dimitris Aragiorgis
    replace = {
1234 0ac84a9a Dimitris Aragiorgis
        "ACCOUNTS": env.env.accounts.fqdn,
1235 0ac84a9a Dimitris Aragiorgis
        "CYCLADES": env.env.cyclades.fqdn,
1236 0ac84a9a Dimitris Aragiorgis
        "mq_node": env.env.mq.ip,
1237 0ac84a9a Dimitris Aragiorgis
        "db_node": env.env.db.ip,
1238 0ac84a9a Dimitris Aragiorgis
        "synnefo_user": env.env.synnefo_user,
1239 0ac84a9a Dimitris Aragiorgis
        "synnefo_db_passwd": env.env.synnefo_db_passwd,
1240 0ac84a9a Dimitris Aragiorgis
        "synnefo_rabbitmq_passwd": env.env.synnefo_rabbitmq_passwd,
1241 0ac84a9a Dimitris Aragiorgis
        "pithos_dir": env.env.pithos_dir,
1242 0ac84a9a Dimitris Aragiorgis
        "common_bridge": env.env.common_bridge,
1243 0ac84a9a Dimitris Aragiorgis
        "HOST": env.env.cyclades.ip,
1244 0ac84a9a Dimitris Aragiorgis
        "domain": env.env.domain,
1245 0ac84a9a Dimitris Aragiorgis
        "CYCLADES_SERVICE_TOKEN": service_token,
1246 0cfa5e03 Stratos Psomadakis
        'STATS': env.env.stats.fqdn,
1247 0ac84a9a Dimitris Aragiorgis
        }
1248 0ac84a9a Dimitris Aragiorgis
    custom = customize_settings_from_tmpl(tmpl, replace)
1249 0e6e73d3 Ilias Tsitsimpis
    try_put(custom, tmpl, mode=0644)
1250 0ac84a9a Dimitris Aragiorgis
    try_run("/etc/init.d/gunicorn restart")
1251 0ac84a9a Dimitris Aragiorgis
1252 0ac84a9a Dimitris Aragiorgis
    cmd = """
1253 0ac84a9a Dimitris Aragiorgis
    sed -i 's/false/true/' /etc/default/snf-dispatcher
1254 0ac84a9a Dimitris Aragiorgis
    /etc/init.d/snf-dispatcher start
1255 0ac84a9a Dimitris Aragiorgis
    """
1256 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1257 0ac84a9a Dimitris Aragiorgis
1258 0ac84a9a Dimitris Aragiorgis
    try_run("snf-manage syncdb")
1259 0ac84a9a Dimitris Aragiorgis
    try_run("snf-manage migrate --delete-ghost-migrations")
1260 0ac84a9a Dimitris Aragiorgis
1261 0ac84a9a Dimitris Aragiorgis
1262 0ac84a9a Dimitris Aragiorgis
@roles("cyclades")
1263 0ac84a9a Dimitris Aragiorgis
def get_backend_id(cluster_name="ganeti1.synnefo.deploy.local"):
1264 f1800130 Ilias Tsitsimpis
    backend_id = try_run("snf-manage backend-list 2>/dev/null " +
1265 f1800130 Ilias Tsitsimpis
                         "| grep %s | awk '{print $1}'" % cluster_name)
1266 0ac84a9a Dimitris Aragiorgis
    return backend_id
1267 0ac84a9a Dimitris Aragiorgis
1268 0ac84a9a Dimitris Aragiorgis
1269 0ac84a9a Dimitris Aragiorgis
@roles("cyclades")
1270 0ac84a9a Dimitris Aragiorgis
def add_backend():
1271 f1800130 Ilias Tsitsimpis
    debug(env.host,
1272 f1800130 Ilias Tsitsimpis
          "adding %s ganeti backend to cyclades..." % env.env.cluster.fqdn)
1273 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
1274 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 " + env.env.cluster.fqdn)
1275 0ac84a9a Dimitris Aragiorgis
    cmd = """
1276 0ac84a9a Dimitris Aragiorgis
    snf-manage backend-add --clustername={0} --user={1} --pass={2}
1277 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.cluster.fqdn, env.env.synnefo_user,
1278 0ac84a9a Dimitris Aragiorgis
               env.env.synnefo_rapi_passwd)
1279 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1280 0ac84a9a Dimitris Aragiorgis
    backend_id = get_backend_id(env.env.cluster.fqdn)
1281 0ac84a9a Dimitris Aragiorgis
    try_run("snf-manage backend-modify --drained=False " + backend_id)
1282 0ac84a9a Dimitris Aragiorgis
1283 f1800130 Ilias Tsitsimpis
1284 0ac84a9a Dimitris Aragiorgis
@roles("cyclades")
1285 0ac84a9a Dimitris Aragiorgis
def pin_user_to_backend(user_email):
1286 0ac84a9a Dimitris Aragiorgis
    backend_id = get_backend_id(env.env.cluster.fqdn)
1287 0ac84a9a Dimitris Aragiorgis
    # pin user to backend
1288 0ac84a9a Dimitris Aragiorgis
    cmd = """
1289 0ac84a9a Dimitris Aragiorgis
cat <<EOF >> /etc/synnefo/cyclades.conf
1290 0ac84a9a Dimitris Aragiorgis

1291 0ac84a9a Dimitris Aragiorgis
BACKEND_PER_USER = {
1292 f1800130 Ilias Tsitsimpis
  '{0}': {1},
1293 0ac84a9a Dimitris Aragiorgis
}
1294 0ac84a9a Dimitris Aragiorgis

1295 0ac84a9a Dimitris Aragiorgis
EOF
1296 0ac84a9a Dimitris Aragiorgis
/etc/init.d/gunicorn restart
1297 f1800130 Ilias Tsitsimpis
""".format(user_email, backend_id)
1298 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1299 0ac84a9a Dimitris Aragiorgis
1300 f1800130 Ilias Tsitsimpis
1301 0ac84a9a Dimitris Aragiorgis
@roles("cyclades")
1302 0ac84a9a Dimitris Aragiorgis
def add_pools():
1303 f1800130 Ilias Tsitsimpis
    debug(env.host,
1304 f1800130 Ilias Tsitsimpis
          " * Creating pools of resources (brigdes, mac prefixes) " +
1305 f1800130 Ilias Tsitsimpis
          "in cyclades...")
1306 f1800130 Ilias Tsitsimpis
    try_run("snf-manage pool-create --type=mac-prefix " +
1307 f1800130 Ilias Tsitsimpis
            "--base=aa:00:0 --size=65536")
1308 0ac84a9a Dimitris Aragiorgis
    try_run("snf-manage pool-create --type=bridge --base=prv --size=20")
1309 0ac84a9a Dimitris Aragiorgis
1310 0ac84a9a Dimitris Aragiorgis
1311 3c626269 Christos Stavrakakis
@roles("accounts", "cyclades", "pithos")
1312 3c626269 Christos Stavrakakis
def export_services():
1313 3c626269 Christos Stavrakakis
    debug(env.host, " * Exporting services...")
1314 3c626269 Christos Stavrakakis
    host = env.host
1315 3c626269 Christos Stavrakakis
    services = []
1316 3c626269 Christos Stavrakakis
    if host == env.env.cyclades.ip:
1317 3c626269 Christos Stavrakakis
        services.append("cyclades")
1318 3c626269 Christos Stavrakakis
    if host == env.env.pithos.ip:
1319 3c626269 Christos Stavrakakis
        services.append("pithos")
1320 3c626269 Christos Stavrakakis
    if host == env.env.accounts.ip:
1321 3c626269 Christos Stavrakakis
        services.append("astakos")
1322 3c626269 Christos Stavrakakis
    for service in services:
1323 3c626269 Christos Stavrakakis
        filename = "%s_services.json" % service
1324 3c626269 Christos Stavrakakis
        cmd = "snf-manage service-export-%s > %s" % (service, filename)
1325 3c626269 Christos Stavrakakis
        run(cmd)
1326 0e6e73d3 Ilias Tsitsimpis
        try_get(filename, filename+".local")
1327 3c626269 Christos Stavrakakis
1328 3c626269 Christos Stavrakakis
1329 3c626269 Christos Stavrakakis
@roles("accounts")
1330 3c626269 Christos Stavrakakis
def import_services():
1331 3c626269 Christos Stavrakakis
    debug(env.host, " * Registering services to astakos...")
1332 3c626269 Christos Stavrakakis
    for service in ["cyclades", "pithos", "astakos"]:
1333 3c626269 Christos Stavrakakis
        filename = "%s_services.json" % service
1334 0e6e73d3 Ilias Tsitsimpis
        try_put(filename + ".local", filename)
1335 3c626269 Christos Stavrakakis
        cmd = "snf-manage service-import --json=%s" % filename
1336 3c626269 Christos Stavrakakis
        run(cmd)
1337 3c626269 Christos Stavrakakis
1338 3c626269 Christos Stavrakakis
    debug(env.host, " * Setting default quota...")
1339 3c626269 Christos Stavrakakis
    cmd = """
1340 d61d6007 Giorgos Korfiatis
    snf-manage resource-modify --default-quota 40G pithos.diskspace
1341 d61d6007 Giorgos Korfiatis
    snf-manage resource-modify --default-quota 2 astakos.pending_app
1342 d61d6007 Giorgos Korfiatis
    snf-manage resource-modify --default-quota 4 cyclades.vm
1343 d61d6007 Giorgos Korfiatis
    snf-manage resource-modify --default-quota 40G cyclades.disk
1344 d61d6007 Giorgos Korfiatis
    snf-manage resource-modify --default-quota 16G cyclades.total_ram
1345 d61d6007 Giorgos Korfiatis
    snf-manage resource-modify --default-quota 8G cyclades.ram
1346 d61d6007 Giorgos Korfiatis
    snf-manage resource-modify --default-quota 32 cyclades.total_cpu
1347 d61d6007 Giorgos Korfiatis
    snf-manage resource-modify --default-quota 16 cyclades.cpu
1348 d61d6007 Giorgos Korfiatis
    snf-manage resource-modify --default-quota 4 cyclades.network.private
1349 d61d6007 Giorgos Korfiatis
    snf-manage resource-modify --default-quota 4 cyclades.floating_ip
1350 3c626269 Christos Stavrakakis
    """
1351 3c626269 Christos Stavrakakis
    try_run(cmd)
1352 3c626269 Christos Stavrakakis
1353 3c626269 Christos Stavrakakis
1354 38fd3090 Christos Stavrakakis
@roles("accounts")
1355 38fd3090 Christos Stavrakakis
def set_user_quota():
1356 38fd3090 Christos Stavrakakis
    debug(env.host, " * Setting user quota...")
1357 38fd3090 Christos Stavrakakis
    cmd = """
1358 38fd3090 Christos Stavrakakis
    snf-manage user-modify -f --all --base-quota pithos.diskspace 40G
1359 38fd3090 Christos Stavrakakis
    snf-manage user-modify -f --all --base-quota astakos.pending_app 2
1360 38fd3090 Christos Stavrakakis
    snf-manage user-modify -f --all --base-quota cyclades.vm 4
1361 38fd3090 Christos Stavrakakis
    snf-manage user-modify -f --all --base-quota cyclades.disk 40G
1362 38fd3090 Christos Stavrakakis
    snf-manage user-modify -f --all --base-quota cyclades.total_ram 16G
1363 38fd3090 Christos Stavrakakis
    snf-manage user-modify -f --all --base-quota cyclades.ram 8G
1364 38fd3090 Christos Stavrakakis
    snf-manage user-modify -f --all --base-quota cyclades.total_cpu 32
1365 38fd3090 Christos Stavrakakis
    snf-manage user-modify -f --all --base-quota cyclades.cpu 16
1366 38fd3090 Christos Stavrakakis
    snf-manage user-modify -f --all --base-quota cyclades.network.private 4
1367 38fd3090 Christos Stavrakakis
    snf-manage user-modify -f --all --base-quota cyclades.floating_ip 4
1368 38fd3090 Christos Stavrakakis
    """
1369 38fd3090 Christos Stavrakakis
    try_run(cmd)
1370 38fd3090 Christos Stavrakakis
1371 38fd3090 Christos Stavrakakis
1372 0ac84a9a Dimitris Aragiorgis
@roles("cyclades")
1373 0ac84a9a Dimitris Aragiorgis
def add_network():
1374 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Adding public network in cyclades...")
1375 0ac84a9a Dimitris Aragiorgis
    cmd = """
1376 28991d59 Ilias Tsitsimpis
    snf-manage network-create --subnet={0} --gateway={1} --public \
1377 28991d59 Ilias Tsitsimpis
        --dhcp=True --flavor={2} --mode=bridged --link={3} --name=Internet \
1378 3672c171 Christos Stavrakakis
        --floating-ip-pool=True
1379 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.synnefo_public_network_subnet,
1380 0ac84a9a Dimitris Aragiorgis
               env.env.synnefo_public_network_gateway,
1381 0ac84a9a Dimitris Aragiorgis
               env.env.synnefo_public_network_type,
1382 23f46a1b Christos Stavrakakis
               env.env.common_bridge)
1383 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1384 23f46a1b Christos Stavrakakis
    if env.env.testing_vm:
1385 23f46a1b Christos Stavrakakis
        cmd = ("snf-manage network-create --subnet6=babe::/64"
1386 23f46a1b Christos Stavrakakis
               " --gateway6=babe::1 --public --flavor={0} --mode=bridged"
1387 23f46a1b Christos Stavrakakis
               " --link={1} --name=IPv6PublicNetwork"
1388 23f46a1b Christos Stavrakakis
               .format(env.env.synnefo_public_network_type,
1389 23f46a1b Christos Stavrakakis
                       env.env.common_bridge))
1390 23f46a1b Christos Stavrakakis
        try_run(cmd)
1391 0ac84a9a Dimitris Aragiorgis
1392 0ac84a9a Dimitris Aragiorgis
1393 0ac84a9a Dimitris Aragiorgis
@roles("cyclades")
1394 0ac84a9a Dimitris Aragiorgis
def setup_vncauthproxy():
1395 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Setting up vncauthproxy...")
1396 fba7aaf4 Stratos Psomadakis
    user = "synnefo"
1397 fba7aaf4 Stratos Psomadakis
    salt = "$6$7FUdSvFcWAs3hfVj$"
1398 fba7aaf4 Stratos Psomadakis
    passhash = "ZwvnvpQclTrDYWEwBvZDMRJZNgb6ZUKT1vNsh9NzUIxMpzBuGgMqYxCDTYF"\
1399 fba7aaf4 Stratos Psomadakis
               "6OZcbunDZb88pjL2EIBnzrGMQW1"
1400 0ac84a9a Dimitris Aragiorgis
    cmd = """
1401 fba7aaf4 Stratos Psomadakis
    mkdir /var/lib/vncauthproxy
1402 fba7aaf4 Stratos Psomadakis
    echo '%s:%s%s' > /var/lib/vncauthproxy/users
1403 fba7aaf4 Stratos Psomadakis
    """ % (user, salt, passhash)
1404 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1405 fba7aaf4 Stratos Psomadakis
    install_package("snf-vncauthproxy")
1406 0ac84a9a Dimitris Aragiorgis
1407 f1800130 Ilias Tsitsimpis
1408 0ac84a9a Dimitris Aragiorgis
@roles("client")
1409 0ac84a9a Dimitris Aragiorgis
def setup_kamaki():
1410 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up kamaki client...")
1411 0ac84a9a Dimitris Aragiorgis
    with settings(hide("everything")):
1412 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 accounts." + env.env.domain)
1413 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 cyclades." + env.env.domain)
1414 0ac84a9a Dimitris Aragiorgis
        try_run("ping -c1 pithos." + env.env.domain)
1415 0ac84a9a Dimitris Aragiorgis
1416 1d8cf97d Dimitris Aragiorgis
    with settings(host_string=env.env.db.ip):
1417 f1800130 Ilias Tsitsimpis
        uid, user_auth_token, user_uuid = \
1418 f1800130 Ilias Tsitsimpis
            get_auth_token_from_db(env.env.user_email)
1419 0ac84a9a Dimitris Aragiorgis
1420 0ac84a9a Dimitris Aragiorgis
    install_package("python-progress")
1421 0ac84a9a Dimitris Aragiorgis
    install_package("kamaki")
1422 0ac84a9a Dimitris Aragiorgis
    cmd = """
1423 6ce03057 Giorgos Korfiatis
    kamaki config set cloud.default.url "https://{0}/astakos/identity/v2.0"
1424 0ac84a9a Dimitris Aragiorgis
    kamaki config set cloud.default.token {1}
1425 0ac84a9a Dimitris Aragiorgis
    """.format(env.env.accounts.fqdn, user_auth_token)
1426 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1427 9f41ab1e Ilias Tsitsimpis
    try_run("kamaki container create images")
1428 0ac84a9a Dimitris Aragiorgis
1429 f1800130 Ilias Tsitsimpis
1430 0ac84a9a Dimitris Aragiorgis
@roles("client")
1431 0ac84a9a Dimitris Aragiorgis
def upload_image(image="debian_base.diskdump"):
1432 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Uploading initial image to pithos...")
1433 0ac84a9a Dimitris Aragiorgis
    image = "debian_base.diskdump"
1434 0ac84a9a Dimitris Aragiorgis
    try_run("wget {0} -O /tmp/{1}".format(env.env.debian_base_url, image))
1435 0ac84a9a Dimitris Aragiorgis
    try_run("kamaki file upload --container images /tmp/{0} {0}".format(image))
1436 0ac84a9a Dimitris Aragiorgis
1437 f1800130 Ilias Tsitsimpis
1438 0ac84a9a Dimitris Aragiorgis
@roles("client")
1439 0ac84a9a Dimitris Aragiorgis
def register_image(image="debian_base.diskdump"):
1440 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Register image to plankton...")
1441 fea067c8 Dimitris Aragiorgis
    # with settings(host_string=env.env.db.ip):
1442 f1800130 Ilias Tsitsimpis
    #     uid, user_auth_token, user_uuid = \
1443 f1800130 Ilias Tsitsimpis
    #        get_auth_token_from_db(env.env.user_email)
1444 0ac84a9a Dimitris Aragiorgis
1445 6d28ce6a Ilias Tsitsimpis
    image_location = "/images/{0}".format(image)
1446 0ac84a9a Dimitris Aragiorgis
    cmd = """
1447 0ac84a9a Dimitris Aragiorgis
    sleep 5
1448 f92dffed Christos Stavrakakis
    kamaki image register --name="Debian Base" --location={0} --public \
1449 f92dffed Christos Stavrakakis
            --disk-format=diskdump \
1450 f1800130 Ilias Tsitsimpis
            --property OSFAMILY=linux --property ROOT_PARTITION=1 \
1451 f1800130 Ilias Tsitsimpis
            --property description="Debian Squeeze Base System" \
1452 f1800130 Ilias Tsitsimpis
            --property size=450M --property kernel=2.6.32 \
1453 f1800130 Ilias Tsitsimpis
            --property GUI="No GUI" --property sortorder=1 \
1454 f1800130 Ilias Tsitsimpis
            --property USERS=root --property OS=debian
1455 83aa2ea5 Ilias Tsitsimpis
    """.format(image_location)
1456 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1457 0ac84a9a Dimitris Aragiorgis
1458 f1800130 Ilias Tsitsimpis
1459 0ac84a9a Dimitris Aragiorgis
@roles("client")
1460 0ac84a9a Dimitris Aragiorgis
def setup_burnin():
1461 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Setting up burnin testing tool...")
1462 0ac84a9a Dimitris Aragiorgis
    install_package("kamaki")
1463 0ac84a9a Dimitris Aragiorgis
    install_package("snf-tools")
1464 0ac84a9a Dimitris Aragiorgis
1465 f1800130 Ilias Tsitsimpis
1466 0ac84a9a Dimitris Aragiorgis
@roles("pithos")
1467 0ac84a9a Dimitris Aragiorgis
def add_image_locally():
1468 f1800130 Ilias Tsitsimpis
    debug(env.host,
1469 f1800130 Ilias Tsitsimpis
          " * Getting image locally in order snf-image to use it directly..")
1470 0ac84a9a Dimitris Aragiorgis
    image = "debian_base.diskdump"
1471 f1800130 Ilias Tsitsimpis
    try_run("wget {0} -O {1}/{2}".format(
1472 f1800130 Ilias Tsitsimpis
            env.env.debian_base_url, env.env.image_dir, image))
1473 0ac84a9a Dimitris Aragiorgis
1474 0ac84a9a Dimitris Aragiorgis
1475 0ac84a9a Dimitris Aragiorgis
@roles("master")
1476 0ac84a9a Dimitris Aragiorgis
def gnt_instance_add(name="test"):
1477 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Adding test instance to Ganeti...")
1478 f1800130 Ilias Tsitsimpis
    osp = """img_passwd=gamwtosecurity,\
1479 f1800130 Ilias Tsitsimpis
img_format=diskdump,img_id=debian_base,\
1480 f1800130 Ilias Tsitsimpis
img_properties='{"OSFAMILY":"linux"\,"ROOT_PARTITION":"1"}'"""
1481 0ac84a9a Dimitris Aragiorgis
    cmd = """
1482 f1800130 Ilias Tsitsimpis
    gnt-instance add  -o snf-image+default --os-parameters {0} \
1483 f1800130 Ilias Tsitsimpis
            -t plain --disk 0:size=1G --no-name-check --no-ip-check \
1484 f1800130 Ilias Tsitsimpis
            --net 0:ip=pool,network=test --no-install \
1485 f1800130 Ilias Tsitsimpis
            --hypervisor-parameters kvm:machine_version=pc-1.0 {1}
1486 0ac84a9a Dimitris Aragiorgis
    """.format(osp, name)
1487 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1488 0ac84a9a Dimitris Aragiorgis
1489 f1800130 Ilias Tsitsimpis
1490 0ac84a9a Dimitris Aragiorgis
@roles("master")
1491 f1800130 Ilias Tsitsimpis
def gnt_network_add(name="test", subnet="10.0.0.0/26", gw="10.0.0.1",
1492 f1800130 Ilias Tsitsimpis
                    mode="bridged", link="br0"):
1493 0ac84a9a Dimitris Aragiorgis
    debug(env.host, " * Adding test network to Ganeti...")
1494 0ac84a9a Dimitris Aragiorgis
    cmd = """
1495 0ac84a9a Dimitris Aragiorgis
    gnt-network add --network={1} --gateway={2} {0}
1496 0ac84a9a Dimitris Aragiorgis
    gnt-network connect {0} {3} {4}
1497 0ac84a9a Dimitris Aragiorgis
    """.format(name, subnet, gw, mode, link)
1498 0ac84a9a Dimitris Aragiorgis
    try_run(cmd)
1499 0ac84a9a Dimitris Aragiorgis
1500 f1800130 Ilias Tsitsimpis
1501 0ac84a9a Dimitris Aragiorgis
@roles("ips")
1502 0ac84a9a Dimitris Aragiorgis
def test():
1503 0ac84a9a Dimitris Aragiorgis
    debug(env.host, "Testing...")
1504 0ac84a9a Dimitris Aragiorgis
    try_run("hostname && date")