Revision c03b6c45

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

  
b/snf-image-helper/tasks/50EnforcePersonality.in
16 16
    log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
17 17
fi
18 18

  
19
if [ -n "$SNF_IMAGE_PERSONALITY" ]; then
19
if [ -z "$SNF_IMAGE_PERSONALITY" ]; then
20
    warn "This image has no personality (0 files to inject)"
21
fi
22

  
23
if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
20 24
    echo "$SNF_IMAGE_PERSONALITY" |
21 25
        @scriptsdir@/inject-files.py "$SNF_IMAGE_TARGET"
22
else
23
    warn "This image has no personality (0 files to inject)\n"
26
    exit 0
27
elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then
28
    tmpdir=$(chroot "$SNF_IMAGE_TARGET" mktemp -d)
29
    add_cleanup rm -rf "$SNF_IMAGE_TARGET/$tmpdir"
30
    echo "$SNF_IMAGE_PERSONALITY" |
31
        @scriptsdir@/inject-files.py -d "$SNF_IMAGE_TARGET/$tmpdir"
32
    {
33
        while read -d $'\0' src; do
34
            read -d $'\0' owner;
35
            read -d $'\0' group;
36
            read -d $'\0' mode;
37
            read -d $'\0' dest;
38
            chroot "$SNF_IMAGE_TARGET" chown "$owner:$group" "$tmpdir/$src"
39
            chroot "$SNF_IMAGE_TARGET" su -l "$owner" -c \
40
                "umask 0007; install -D -m $mode $(printf "%q" "$tmpdir")/$src \
41
                $(printf "%q" "$dest")"
42
        done
43
    } < "$SNF_IMAGE_TARGET/$tmpdir/manifest"
24 44
fi
25 45

  
26 46
exit 0

Also available in: Unified diff