Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (2.7 kB)

1
from __future__ import with_statement
2
from fabric.api import hide, env, settings, local, roles
3
from fabric.operations import run, put, get
4
import fabric
5
import re
6
import os
7
import shutil
8
import tempfile
9
import ast
10
from snfdeploy.lib import debug, Conf, Env, disable_color
11
from snfdeploy import massedit
12

    
13

    
14
def abort(action):
15
    def inner(*args, **kwargs):
16
        try:
17
            return action(*args, **kwargs)
18
        except BaseException as e:
19
            abort = kwargs.get("abort", True)
20
            if not abort:
21
                 debug(env.host, "WARNING: command failed. Continuing anyway...")
22
            else:
23
                 fabric.utils.abort(e)
24
    return inner
25

    
26

    
27
@abort
28
def try_get(remote_path, local_path=None, **kwargs):
29
    get(remote_path, local_path=local_path, **kwargs)
30

    
31

    
32
@abort
33
def try_put(local_path=None, remote_path=None, **kwargs):
34
    put(local_path=local_path, remote_path=remote_path, **kwargs)
35

    
36

    
37

    
38
@abort
39
def try_run(cmd, **kwargs):
40
    if env.local:
41
        return local(cmd, capture=True)
42
    else:
43
        return run(cmd)
44

    
45

    
46
def install_package(package):
47
    debug(env.host, " * Installing package %s..." % package)
48
    apt_get = "export DEBIAN_FRONTEND=noninteractive ;" + \
49
              "apt-get install -y --force-yes "
50

    
51
    host_info = env.env.ips_info[env.host]
52
    env.env.update_packages(host_info.os)
53
    if ast.literal_eval(env.env.use_local_packages):
54
        with settings(warn_only=True):
55
            deb = local("ls %s/%s*%s_*.deb"
56
                        % (env.env.packages, package, host_info.os),
57
                        capture=True)
58
            if deb:
59
                debug(env.host,
60
                      " * Package %s found in %s..."
61
                      % (package, env.env.packages))
62
                try_put(deb, "/tmp/")
63
                try_run("dpkg -i /tmp/%s || "
64
                        % os.path.basename(deb) + apt_get + "-f")
65
                try_run("rm /tmp/%s" % os.path.basename(deb))
66
                return
67

    
68
    info = getattr(env.env, package)
69
    if info in \
70
            ["squeeze-backports", "squeeze", "stable",
71
             "testing", "unstable", "wheezy"]:
72
        apt_get += " -t %s %s " % (info, package)
73
    elif info:
74
        apt_get += " %s=%s " % (package, info)
75
    else:
76
        apt_get += package
77

    
78
    try_run(apt_get)
79

    
80
    return
81

    
82

    
83
def customize_settings_from_tmpl(tmpl, replace):
84
    debug(env.host, " * Customizing template %s..." % tmpl)
85
    local = env.env.templates + tmpl
86
    _, custom = tempfile.mkstemp()
87
    shutil.copyfile(local, custom)
88
    for k, v in replace.iteritems():
89
        regex = "re.sub('%{0}%', '{1}', line)".format(k.upper(), v)
90
        massedit.edit_files([custom], [regex], dry_run=False)
91

    
92
    return custom
93

    
94