File: mic-create-bootstrap

package info (click to toggle)
mic2 0.19-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 932 kB
  • ctags: 864
  • sloc: python: 9,292; sh: 1,347; makefile: 62
file content (122 lines) | stat: -rwxr-xr-x 4,185 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python -t
#
# mic-create-bootstrap : create a bootstrap to run mic-image-creator
#
# Copyright 2009, Intel Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

import os
import os.path
import sys
import optparse
import shutil
import subprocess
import signal
import tempfile
from mic.imgcreate.fs import *
from mic.imgcreate.bootstrap import *

try:
    import mic.__version__
    version = mic.__version__.version
except:
    version = 'unknown'

class Usage(Exception):
    def __init__(self, msg = None, no_error = False):
        Exception.__init__(self, msg, no_error)

def parse_options(args):
    parser = optparse.OptionParser(
        usage="mic-create-bootstrap [options]"
        , version = version)

    parser.add_option("-k", "--cache", type="string", dest="cachedir",
                    help="Repo cache dir, it saves too much time to use repo cache.")
    parser.add_option("-n", "--reponame", type="string",
                      dest="reponame",
                      help="repo name, get repo cache according to this"
                     )
    parser.add_option("-r", "--repourl", type="string", dest="repourl",
                      help="Repo URL"
                     )
    parser.add_option("-p", "--proxy", type="string", dest="proxy",
                      help="Specify a http proxy if needed"
                     )
    parser.add_option("-o", "--outdir", type="string",
                      dest="outdir",
                      help="Output directory to use, it should be empty if existed "
                     )

    (options, args) = parser.parse_args()

    return options

def main():
    try:
        options = parse_options(sys.argv[1:])
        if not options:
            return 2
    except Usage, (msg, no_error):
        if no_error:
            out = sys.stdout
            ret = 0
        else:
            out = sys.stderr
            ret = 2
        if msg:
            print >> out, msg
        return ret

    if os.geteuid () != 0:
        print "You must run mic-create-bootstrap as root"
        return 1

    if not options.reponame:
        raise Usage("Please specify repo name, it can help reduce runtime if it has been cached.")

    if not options.outdir:
       raise Usage("Please use -o or --outdir option to specify target bootstrap directory.")
    elif not os.path.exists(options.outdir):
       try:
           os.makedirs(options.outdir)
       except OSError, (err, msg):
           raise Usage(msg)
    elif os.path.isdir(options.outdir) and len(os.listdir(options.outdir)) != 0:
       raise Usage("Output dir must be empty or non-existed directory.")
    elif not os.path.isdir(options.outdir):
       raise Usage("Output dir must be empty or non-existed directory.")

    proxies = {}
    if options.proxy:
        proxies = {"http":options.proxy,"https":options.proxy, "ftp":options.proxy, "ftps":options.proxy}

    rmcachedir = False
    if not options.cachedir:
        try:
            options.cachedir = tempfile.mkdtemp(dir = "/var/tmp", prefix = "bootstrapcache-")
            rmcachedir = True
        except OSError, (err, msg):
            raise Usage("Failed to create bootstrap cache directory on %s: %s" % ("/var/tmp", msg))

    get_repo_metadata(options.repourl, options.cachedir, options.reponame, proxies)
    build_bootstrap(options.repourl, options.cachedir, options.reponame, options.outdir, proxies)
    if rmcachedir:
        shutil.rmtree(options.cachedir, ignore_errors = True)
    print "Finished."
    return 0

if __name__ == "__main__":
    sys.exit(main())