Statistics
| Branch: | Tag: | Revision:

root / image_creator / main.py @ 69aa33fa

History | View | Annotate | Download (5.5 kB)

1
#!/usr/bin/env python
2

    
3
# Copyright 2011 GRNET S.A. All rights reserved.
4
#
5
# Redistribution and use in source and binary forms, with or
6
# without modification, are permitted provided that the following
7
# conditions are met:
8
#
9
#   1. Redistributions of source code must retain the above
10
#      copyright notice, this list of conditions and the following
11
#      disclaimer.
12
#
13
#   2. Redistributions in binary form must reproduce the above
14
#      copyright notice, this list of conditions and the following
15
#      disclaimer in the documentation and/or other materials
16
#      provided with the distribution.
17
#
18
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
# POSSIBILITY OF SUCH DAMAGE.
30
#
31
# The views and conclusions contained in the software and
32
# documentation are those of the authors and should not be
33
# interpreted as representing official policies, either expressed
34
# or implied, of GRNET S.A.
35

    
36
from image_creator import get_os_class
37
from image_creator import __version__ as version
38
from image_creator import FatalError
39
from image_creator.disk import Disk
40
from image_creator.util import get_command, error, progress_generator, success
41
from clint.textui import puts
42

    
43
import sys
44
import os
45
import optparse
46

    
47
dd = get_command('dd')
48

    
49

    
50
def check_writable_dir(option, opt_str, value, parser):
51
    dirname = os.path.dirname(value)
52
    name = os.path.basename(value)
53
    if dirname and not os.path.isdir(dirname):
54
        parser.error("`%s' is not an existing directory" % dirname)
55

    
56
    if not name:
57
        parser.error("`%s' is not a valid file name" % dirname)
58

    
59
    setattr(parser.values, option.dest, value)
60

    
61

    
62
def parse_options(input_args):
63
    usage = "Usage: %prog [options] <input_media>"
64
    parser = optparse.OptionParser(version=version, usage=usage)
65

    
66
    parser.add_option("-f", "--force", dest="force", default=False,
67
        action="store_true", help="overwrite output files if they exist")
68

    
69
    parser.add_option("--no-cleanup", dest="cleanup", default=True,
70
        help="don't cleanup sensitive data",
71
        action="store_false")
72

    
73
    parser.add_option("--no-sysprep", dest="sysprep", default=True,
74
        help="don't perform system preperation",
75
        action="store_false")
76

    
77
    parser.add_option("--no-shrink", dest="shrink", default=True,
78
        help="don't shrink any partition",
79
        action="store_false")
80

    
81
    parser.add_option("-o", "--outfile", type="string", dest="outfile",
82
        default=None, action="callback", callback=check_writable_dir,
83
        help="dump image to FILE",
84
        metavar="FILE")
85

    
86
    parser.add_option("-u", "--upload", dest="upload", default=False,
87
        help="upload the image to pithos",
88
        action="store_true")
89

    
90
    parser.add_option("-r", "--register", dest="register", default=False,
91
        help="register the image to ~okeanos", action="store_true")
92

    
93
    options, args = parser.parse_args(input_args)
94

    
95
    if len(args) != 1:
96
        parser.error('Wrong number of arguments')
97
    options.source = args[0]
98
    if not os.path.exists(options.source):
99
        parser.error('input media is not accessible')
100

    
101
    if options.register:
102
        options.upload = True
103

    
104
    if options.outfile is None and not options.upload:
105
        parser.error('either outfile (-o) or upload (-u) must be set.')
106

    
107
    return options
108

    
109

    
110
def image_creator():
111
    puts('snf-image-creator %s\n' % version)
112
    options = parse_options(sys.argv[1:])
113

    
114
    if os.geteuid() != 0:
115
        raise FatalError("You must run %s as root" \
116
                        % os.path.basename(sys.argv[0]))
117

    
118
    if not options.force and options.outfile is not None:
119
        for extension in ('', '.meta'):
120
            filename = "%s%s" % (options.outfile, extension)
121
            if os.path.exists(filename):
122
                raise FatalError("Output file %s exists "
123
                    "(use --force to overwrite it)." % filename)
124

    
125
    disk = Disk(options.source)
126
    try:
127
        dev = disk.get_device()
128
        dev.mount()
129

    
130
        osclass = get_os_class(dev.distro, dev.ostype)
131
        image_os = osclass(dev.root, dev.g)
132
        metadata = image_os.get_metadata()
133

    
134
        puts()
135

    
136
        if options.sysprep:
137
            image_os.sysprep()
138

    
139
        if options.cleanup:
140
            image_os.data_cleanup()
141

    
142
        dev.umount()
143

    
144
        size = options.shrink and dev.shrink() or dev.size()
145
        metadata['size'] = str(size // 2 ** 20)
146

    
147
        if options.outfile is not None:
148
            f = open('%s.%s' % (options.outfile, 'meta'), 'w')
149
            try:
150
                for key in metadata.keys():
151
                    f.write("%s=%s\n" % (key, metadata[key]))
152
            finally:
153
                f.close()
154

    
155
            dev.dump(options.outfile)
156
    finally:
157
        puts('cleaning up...')
158
        disk.cleanup()
159

    
160
    return 0
161

    
162

    
163
def main():
164
    try:
165
        ret = image_creator()
166
        sys.exit(ret)
167
    except FatalError as e:
168
        error(e)
169
        sys.exit(1)
170

    
171

    
172
if __name__ == '__main__':
173
    main()
174

    
175
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :