Revision 68d6d24b snf-deploy/snfdeploy/fabfile.py

b/snf-deploy/snfdeploy/fabfile.py
15 15
import tempfile
16 16
import ast
17 17
from snfdeploy.lib import debug, Conf, Env, disable_color
18
from snfdeploy.utils import *
18 19
from snfdeploy import massedit
19 20

  
20 21

  
......
77 78
    })
78 79

  
79 80

  
80
def install_package(package):
81
    debug(env.host, " * Installing package %s..." % package)
82
    apt_get = "export DEBIAN_FRONTEND=noninteractive ;" + \
83
              "apt-get install -y --force-yes "
84

  
85
    host_info = env.env.ips_info[env.host]
86
    env.env.update_packages(host_info.os)
87
    if ast.literal_eval(env.env.use_local_packages):
88
        with settings(warn_only=True):
89
            deb = local("ls %s/%s*%s_*.deb"
90
                        % (env.env.packages, package, host_info.os),
91
                        capture=True)
92
            if deb:
93
                debug(env.host,
94
                      " * Package %s found in %s..."
95
                      % (package, env.env.packages))
96
                try_put(deb, "/tmp/")
97
                try_run("dpkg -i /tmp/%s || "
98
                        % os.path.basename(deb) + apt_get + "-f")
99
                try_run("rm /tmp/%s" % os.path.basename(deb))
100
                return
101

  
102
    info = getattr(env.env, package)
103
    if info in \
104
            ["squeeze-backports", "squeeze", "stable",
105
             "testing", "unstable", "wheezy"]:
106
        apt_get += " -t %s %s " % (info, package)
107
    elif info:
108
        apt_get += " %s=%s " % (package, info)
109
    else:
110
        apt_get += package
111

  
112
    try_run(apt_get)
113

  
114
    return
115

  
116

  
117 81
@roles("ns")
118 82
def update_ns_for_ganeti():
119 83
    debug(env.host,
......
324 288
    try_run(cmd)
325 289

  
326 290

  
327
def try_run(cmd, abort=True):
328
    try:
329
        if env.local:
330
            return local(cmd, capture=True)
331
        else:
332
            return run(cmd)
333
    except BaseException as e:
334
        if abort:
335
            fabric.utils.abort(e)
336
        else:
337
            debug(env.host, "WARNING: command failed. Continuing anyway...")
338

  
339

  
340
def try_put(local_path=None, remote_path=None, abort=True, **kwargs):
341
    try:
342
        put(local_path=local_path, remote_path=remote_path, **kwargs)
343
    except BaseException as e:
344
        if abort:
345
            fabric.utils.abort(e)
346
        else:
347
            debug(env.host, "WARNING: command failed. Continuing anyway...")
348

  
349

  
350
def try_get(remote_path, local_path=None, abort=True, **kwargs):
351
    try:
352
        get(remote_path, local_path=local_path, **kwargs)
353
    except BaseException as e:
354
        if abort:
355
            fabric.utils.abort(e)
356
        else:
357
            debug(env.host, "WARNING: command failed. Continuing anyway...")
358

  
359

  
360 291
def create_bridges():
361 292
    debug(env.host, " * Creating bridges...")
362 293
    install_package("bridge-utils")
......
393 324
        try_run(cmd)
394 325

  
395 326

  
396
def customize_settings_from_tmpl(tmpl, replace):
397
    debug(env.host, " * Customizing template %s..." % tmpl)
398
    local = env.env.templates + tmpl
399
    _, custom = tempfile.mkstemp()
400
    shutil.copyfile(local, custom)
401
    for k, v in replace.iteritems():
402
        regex = "re.sub('%{0}%', '{1}', line)".format(k.upper(), v)
403
        massedit.edit_files([custom], [regex], dry_run=False)
404

  
405
    return custom
406

  
407

  
408 327
@roles("nodes")
409 328
def setup_apt():
410 329
    debug(env.host, "Setting up apt sources...")

Also available in: Unified diff