Revision c03b6c45 snf-image-helper/inject-files.py

b/snf-image-helper/inject-files.py
13 13
import json
14 14
import datetime
15 15
import base64
16
import struct
16 17
from optparse import OptionParser
17 18

  
18 19

  
......
28 29
                        action="store",type='string', dest="input_file",
29 30
                        help="get input from FILE instead of stdin",
30 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")
31 35

  
32 36
    opts, args = parser.parse_args(input_args)
33 37

  
......
46 50
            parser.error('input file does not exist')
47 51
        input_file = open(input_file,'r')
48 52
        
49
    return (input_file, target)
53
    return (input_file, target, opts.decode)
50 54

  
51 55

  
52 56
def main():
53
    (input_file, target) = parse_arguments(sys.argv[1:])
57
    (input_file, target, decode) = parse_arguments(sys.argv[1:])
54 58

  
55 59
    files = json.load(input_file)
60
    
61
    if decode:
62
        manifest = open(target + '/manifest', 'w')
63
    
64
    count = 0
56 65
    for f in files:
57
        real_path = target + '/' + f['path']
58
        if os.path.lexists(real_path):
59
            backup_file = real_path + '.bak.' + timestamp()
60
            os.rename(real_path, backup_file)
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
61 70

  
62
        parentdir = os.path.dirname(real_path)
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)
63 79
        if not os.path.exists(parentdir):
64 80
            os.makedirs(parentdir)
65 81

  
66
        newfile = open(real_path, 'w')
82
        newfile = open(filepath, 'w')
67 83
        newfile.write(base64.b64decode(f['contents']))
68 84
        newfile.close()
69
        os.chmod(real_path, 0440)
70
    sys.stderr.write('Successful personalization of Image\n')
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()
71 102

  
72 103
    input_file.close()
73 104
    return 0
74 105

  
75

  
76 106
if __name__ == "__main__":
77 107
    sys.exit(main())
78 108

  

Also available in: Unified diff