summaryrefslogtreecommitdiff
path: root/fabfile.py
blob: a7c20479964df943dcaff8f8149337e6f93a6fe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
# encoding: utf-8
import json
import os

from fabric.api import task, cd, env, require, run, put


@task
def status():
    """
    Display some status of the server.
    """
    require('tuf_path', 'hosts', 'port', 'user')

    run('whoami')

    run('ls {0}/linux-i386 --color=auto'.format(env.tuf_path))
    run('ls {0}/linux-x86_64 --color=auto'.format(env.tuf_path))


@task
def update():
    """
    Update the TUF repo using the specified file name.
    """
    require('tuf_path', 'tuf_arch', 'hosts', 'port', 'user', 'repo_file')

    if env.tuf_arch not in ['32', '64']:
        print "Error: invalid parameter, use 32 or 64."
        return

    if not os.path.isfile(env.repo_file):
        print "Error: the file does not exist."
        return

    if env.tuf_arch == '32':
        arch = 'linux-i386'
    else:
        arch = 'linux-x86_64'

    path = os.path.join(env.tuf_path, arch)
    print arch, env.repo_file, path

    put(env.repo_file, path)

    with cd(path):
        # we keep the targets folder until we finish so we can recover it in
        # case of error
        run('mv targets targets.old')
        run('tar xjf {0} --strip-components=1'.format(env.repo_file))
        # NOTE: Don't copy the root.json file
        # run('cp -a metadata.staged/root.json metadata/')
        run('cp -a metadata.staged/targets.json* metadata/')
        run('cp -a metadata.staged/snapshot.json* metadata/')
        # '|| true' is a hack to avoid permissions problems
        run('chmod g+w -f -R metadata.staged/ metadata/timestamp.json || true')
        run('rm -fr targets.old')
        run('rm {0}'.format(env.repo_file))
        # Note: the timestamp is updated by cron


@task(default=True)
def help():
    print 'This script is meant to be used to update a TUF remote remository.'
    print 'You need to provide a fabfile.json containing server details and '
    print 'files to update. As an example see the fabfile.json.sample file.'
    print
    print 'Note: this assumes that you authenticate using the ssh-agent.'
    print
    print 'You should use this as follows:'
    print '  fab update'


def load_json():
    """
    Load a fabfile.json file and add its data to the 'env' dict.
    """
    # NOTE hopefully this will be available soon on fabric,
    # see https://github.com/fabric/fabric/pull/1092
    try:
        jdata = None
        with open('fabfile.json', 'r') as f:
            jdata = json.load(f)

        env.update(jdata)
        print "ENV updated"
    except:
        print "ENV not updated"
        pass


# Do this always and as a first task
load_json()