Statistics
| Branch: | Tag: | Revision:

root / snf-image-helper / inject-files.py @ 83d0c566

History | View | Annotate | Download (3.3 kB)

1 22a2e378 Nikos Skalkotos
#!/usr/bin/env python
2 d984217b Vangelis Koukis
#
3 d984217b Vangelis Koukis
# Copyright (C) 2011 GRNET S.A.
4 7e5d635b Nikos Skalkotos
#
5 7e5d635b Nikos Skalkotos
# This program is free software; you can redistribute it and/or modify
6 7e5d635b Nikos Skalkotos
# it under the terms of the GNU General Public License as published by
7 7e5d635b Nikos Skalkotos
# the Free Software Foundation; either version 2 of the License, or
8 7e5d635b Nikos Skalkotos
# (at your option) any later version.
9 7e5d635b Nikos Skalkotos
#
10 7e5d635b Nikos Skalkotos
# This program is distributed in the hope that it will be useful, but
11 7e5d635b Nikos Skalkotos
# WITHOUT ANY WARRANTY; without even the implied warranty of
12 7e5d635b Nikos Skalkotos
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 7e5d635b Nikos Skalkotos
# General Public License for more details.
14 7e5d635b Nikos Skalkotos
#
15 7e5d635b Nikos Skalkotos
# You should have received a copy of the GNU General Public License
16 7e5d635b Nikos Skalkotos
# along with this program; if not, write to the Free Software
17 7e5d635b Nikos Skalkotos
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 7e5d635b Nikos Skalkotos
# 02110-1301, USA.
19 7e5d635b Nikos Skalkotos
20 22a2e378 Nikos Skalkotos
"""Inject files into a directory
21 22a2e378 Nikos Skalkotos

22 22a2e378 Nikos Skalkotos
This program injects files into a target directory.
23 22a2e378 Nikos Skalkotos
The files are passed to the program through a JSON string either read from a
24 22a2e378 Nikos Skalkotos
file or from standard input.
25 22a2e378 Nikos Skalkotos

26 22a2e378 Nikos Skalkotos
"""
27 22a2e378 Nikos Skalkotos
28 22a2e378 Nikos Skalkotos
import sys
29 22a2e378 Nikos Skalkotos
import os
30 22a2e378 Nikos Skalkotos
import json
31 22a2e378 Nikos Skalkotos
import datetime
32 22a2e378 Nikos Skalkotos
import base64
33 22a2e378 Nikos Skalkotos
from optparse import OptionParser
34 22a2e378 Nikos Skalkotos
35 22a2e378 Nikos Skalkotos
36 22a2e378 Nikos Skalkotos
def timestamp():
37 22a2e378 Nikos Skalkotos
    now = datetime.datetime.now()
38 22a2e378 Nikos Skalkotos
    current_time = now.strftime("%Y%m%d.%H%M%S")
39 22a2e378 Nikos Skalkotos
    return current_time
40 22a2e378 Nikos Skalkotos
41 d984217b Vangelis Koukis
42 22a2e378 Nikos Skalkotos
def parse_arguments(input_args):
43 22a2e378 Nikos Skalkotos
    usage = "Usage: %prog [options] <target>"
44 22a2e378 Nikos Skalkotos
    parser = OptionParser(usage=usage)
45 22a2e378 Nikos Skalkotos
    parser.add_option("-i", "--input",
46 f58bbc57 Nikos Skalkotos
                      action="store", type='string', dest="input_file",
47 f58bbc57 Nikos Skalkotos
                      help="get input from FILE instead of stdin",
48 f58bbc57 Nikos Skalkotos
                      metavar="FILE")
49 f58bbc57 Nikos Skalkotos
    parser.add_option("-d", "--decode", action="store_true", dest="decode",
50 f58bbc57 Nikos Skalkotos
                      default=False,
51 f58bbc57 Nikos Skalkotos
                      help="decode files under target and create manifest")
52 22a2e378 Nikos Skalkotos
53 22a2e378 Nikos Skalkotos
    opts, args = parser.parse_args(input_args)
54 22a2e378 Nikos Skalkotos
55 22a2e378 Nikos Skalkotos
    if len(args) != 1:
56 22a2e378 Nikos Skalkotos
        parser.error('target is missing')
57 d984217b Vangelis Koukis
58 22a2e378 Nikos Skalkotos
    target = args[0]
59 22a2e378 Nikos Skalkotos
    if not os.path.isdir(target):
60 22a2e378 Nikos Skalkotos
        parser.error('target is not a directory')
61 22a2e378 Nikos Skalkotos
62 22a2e378 Nikos Skalkotos
    input_file = opts.input_file
63 22a2e378 Nikos Skalkotos
    if input_file is None:
64 22a2e378 Nikos Skalkotos
        input_file = sys.stdin
65 22a2e378 Nikos Skalkotos
    else:
66 22a2e378 Nikos Skalkotos
        if not os.path.isfile(input_file):
67 22a2e378 Nikos Skalkotos
            parser.error('input file does not exist')
68 d984217b Vangelis Koukis
        input_file = open(input_file, 'r')
69 d984217b Vangelis Koukis
70 c03b6c45 Nikos Skalkotos
    return (input_file, target, opts.decode)
71 22a2e378 Nikos Skalkotos
72 22a2e378 Nikos Skalkotos
73 22a2e378 Nikos Skalkotos
def main():
74 c03b6c45 Nikos Skalkotos
    (input_file, target, decode) = parse_arguments(sys.argv[1:])
75 22a2e378 Nikos Skalkotos
76 2545cdc5 Nikos Skalkotos
    files = json.load(input_file)
77 d984217b Vangelis Koukis
78 c03b6c45 Nikos Skalkotos
    if decode:
79 c03b6c45 Nikos Skalkotos
        manifest = open(target + '/manifest', 'w')
80 d984217b Vangelis Koukis
81 c03b6c45 Nikos Skalkotos
    count = 0
82 22a2e378 Nikos Skalkotos
    for f in files:
83 c03b6c45 Nikos Skalkotos
        count += 1
84 e0d2dc8f Nikos Skalkotos
        owner = f['owner'] if 'owner' in f else ""
85 e0d2dc8f Nikos Skalkotos
        group = f['group'] if 'group' in f else ""
86 d984217b Vangelis Koukis
        mode = f['mode'] if 'mode' in f else 0440
87 22a2e378 Nikos Skalkotos
88 c03b6c45 Nikos Skalkotos
        filepath = f['path'] if not decode else str(count)
89 c03b6c45 Nikos Skalkotos
        filepath = target + "/" + filepath
90 c03b6c45 Nikos Skalkotos
91 c03b6c45 Nikos Skalkotos
        if os.path.lexists(filepath):
92 c03b6c45 Nikos Skalkotos
            backup_file = filepath + '.bak.' + timestamp()
93 c03b6c45 Nikos Skalkotos
            os.rename(filepath, backup_file)
94 c03b6c45 Nikos Skalkotos
95 c03b6c45 Nikos Skalkotos
        parentdir = os.path.dirname(filepath)
96 22a2e378 Nikos Skalkotos
        if not os.path.exists(parentdir):
97 22a2e378 Nikos Skalkotos
            os.makedirs(parentdir)
98 22a2e378 Nikos Skalkotos
99 c03b6c45 Nikos Skalkotos
        newfile = open(filepath, 'w')
100 22a2e378 Nikos Skalkotos
        newfile.write(base64.b64decode(f['contents']))
101 22a2e378 Nikos Skalkotos
        newfile.close()
102 d984217b Vangelis Koukis
103 c03b6c45 Nikos Skalkotos
        if decode:
104 d984217b Vangelis Koukis
            manifest.write("%s\x00%s\x00%s\x00%o\x00%s\x00" %
105 d984217b Vangelis Koukis
                           (count, owner, group, mode, f['path']))
106 d984217b Vangelis Koukis
107 c03b6c45 Nikos Skalkotos
    if decode:
108 c03b6c45 Nikos Skalkotos
        manifest.close()
109 22a2e378 Nikos Skalkotos
110 22a2e378 Nikos Skalkotos
    input_file.close()
111 22a2e378 Nikos Skalkotos
    return 0
112 22a2e378 Nikos Skalkotos
113 22a2e378 Nikos Skalkotos
if __name__ == "__main__":
114 22a2e378 Nikos Skalkotos
    sys.exit(main())
115 22a2e378 Nikos Skalkotos
116 22a2e378 Nikos Skalkotos
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :