root / snf-image-helper / inject-files.py @ c03b6c45
History | View | Annotate | Download (3 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
|
3 |
"""Inject files into a directory
|
4 |
|
5 |
This program injects files into a target directory.
|
6 |
The files are passed to the program through a JSON string either read from a
|
7 |
file or from standard input.
|
8 |
|
9 |
"""
|
10 |
|
11 |
import sys |
12 |
import os |
13 |
import json |
14 |
import datetime |
15 |
import base64 |
16 |
import struct |
17 |
from optparse import OptionParser |
18 |
|
19 |
|
20 |
def timestamp(): |
21 |
now = datetime.datetime.now() |
22 |
current_time = now.strftime("%Y%m%d.%H%M%S")
|
23 |
return current_time
|
24 |
|
25 |
def parse_arguments(input_args): |
26 |
usage = "Usage: %prog [options] <target>"
|
27 |
parser = OptionParser(usage=usage) |
28 |
parser.add_option("-i", "--input", |
29 |
action="store",type='string', dest="input_file", |
30 |
help="get input from FILE instead of stdin",
|
31 |
metavar="FILE")
|
32 |
parser.add_option("-d", "--decode", |
33 |
action="store_true", dest="decode", default=False, |
34 |
help="decode files under target and create manifest")
|
35 |
|
36 |
opts, args = parser.parse_args(input_args) |
37 |
|
38 |
if len(args) != 1: |
39 |
parser.error('target is missing')
|
40 |
|
41 |
target = args[0]
|
42 |
if not os.path.isdir(target): |
43 |
parser.error('target is not a directory')
|
44 |
|
45 |
input_file = opts.input_file |
46 |
if input_file is None: |
47 |
input_file = sys.stdin |
48 |
else:
|
49 |
if not os.path.isfile(input_file): |
50 |
parser.error('input file does not exist')
|
51 |
input_file = open(input_file,'r') |
52 |
|
53 |
return (input_file, target, opts.decode)
|
54 |
|
55 |
|
56 |
def main(): |
57 |
(input_file, target, decode) = parse_arguments(sys.argv[1:])
|
58 |
|
59 |
files = json.load(input_file) |
60 |
|
61 |
if decode:
|
62 |
manifest = open(target + '/manifest', 'w') |
63 |
|
64 |
count = 0
|
65 |
for f in files: |
66 |
count += 1
|
67 |
owner = f['owner'] if 'owner' in f else "root" |
68 |
group = f['group'] if 'group' in f else "root" |
69 |
mode = f['mode'] if 'mode' in f else 288 # 440 in oct = 288 in dec |
70 |
|
71 |
filepath = f['path'] if not decode else str(count) |
72 |
filepath = target + "/" + filepath
|
73 |
|
74 |
if os.path.lexists(filepath):
|
75 |
backup_file = filepath + '.bak.' + timestamp()
|
76 |
os.rename(filepath, backup_file) |
77 |
|
78 |
parentdir = os.path.dirname(filepath) |
79 |
if not os.path.exists(parentdir): |
80 |
os.makedirs(parentdir) |
81 |
|
82 |
newfile = open(filepath, 'w') |
83 |
newfile.write(base64.b64decode(f['contents']))
|
84 |
newfile.close() |
85 |
|
86 |
if decode:
|
87 |
manifest.write(str(count))
|
88 |
manifest.write(struct.pack('B', 0)) |
89 |
manifest.write(owner) |
90 |
manifest.write(struct.pack('B', 0)) |
91 |
manifest.write(group) |
92 |
manifest.write(struct.pack('B', 0)) |
93 |
manifest.write("%o" % mode)
|
94 |
manifest.write(struct.pack('B', 0)) |
95 |
manifest.write(f['path'])
|
96 |
manifest.write(struct.pack('B', 0)) |
97 |
|
98 |
sys.stderr.write('Files were injected successfully\n')
|
99 |
|
100 |
if decode:
|
101 |
manifest.close() |
102 |
|
103 |
input_file.close() |
104 |
return 0 |
105 |
|
106 |
if __name__ == "__main__": |
107 |
sys.exit(main()) |
108 |
|
109 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|