summaryrefslogtreecommitdiffstats
path: root/src/virtBootstrap/virt_bootstrap.py
blob: ddc54568b74546209d666792c21c15b2a43dbc97 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python
# Authors: Cedric Bosdonnat <cbosdonnat@suse.com>
#
# Copyright (C) 2017 SUSE, 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, either version 3 of the License, or
# (at your option) any later version.

# 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 General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
Main executable file which process input arguments
and calls corresponding methods on appropriate object.
"""

import argparse
import gettext
import logging
import sys
import os
from textwrap import dedent
try:
    from urlparse import urlparse
except ImportError:
    from urllib.parse import urlparse

from virtBootstrap import sources
from virtBootstrap import progress
from virtBootstrap import utils


gettext.bindtextdomain("virt-bootstrap", "/usr/share/locale")
gettext.textdomain("virt-bootstrap")
try:
    gettext.install("virt-bootstrap",
                    localedir="/usr/share/locale",
                    codeset='utf-8')
except IOError:
    try:
        import __builtin__
        # pylint: disable=undefined-variable
        __builtin__.__dict__['_'] = unicode
    except ImportError:
        import builtin
        builtin.__dict__['_'] = str

# pylint: disable=invalid-name
# Create logger
logger = logging.getLogger('virtBootstrap.virt_bootstrap')


def get_source(source_type):
    """
    Get object which match the source type
    """
    try:
        class_name = "%sSource" % source_type.capitalize()
        clazz = getattr(sources, class_name)
        return clazz
    except Exception:
        raise Exception("Invalid image URL scheme: '%s'" % source_type)


# The implementation for remapping ownership of all files inside a
# container's rootfs is inspired by the tool uidmapshift:
#
# Original author: Serge Hallyn <serge.hallyn@ubuntu.com>
# Original license: GPLv2
# http://bazaar.launchpad.net/%7Eserge-hallyn/+junk/nsexec/view/head:/uidmapshift.c

def get_map_id(old_id, opts):
    """
    Calculate new map_id.
    """
    if old_id >= opts['first'] and old_id < opts['last']:
        return old_id + opts['offset']
    return -1


def parse_idmap(idmap):
    """
    Parse user input to 'start', 'target' and 'count' values.

    Example:
        ['0:1000:10', '500:500:10']
    is converted to:
        [[0, 1000, 10], [500, 500, 10]]
    """
    if not isinstance(idmap, list) or idmap == []:
        return None
    try:
        idmap_list = []
        for x in idmap:
            start, target, count = x.split(':')
            idmap_list.append([int(start), int(target), int(count)])

        return idmap_list
    except Exception:
        raise ValueError("Invalid UID/GID mapping value: %s" % idmap)


def get_mapping_opts(mapping):
    """
    Get range options from UID/GID mapping
    """
    start = mapping[0] if mapping[0] > -1 else 0
    target = mapping[1] if mapping[1] > -1 else 0
    count = mapping[2] if mapping[2] > -1 else 1

    opts = {
        'first': start,
        'last': start + count,
        'offset': target - start
    }
    return opts


def map_id(path, map_uid, map_gid):
    """
    Remapping ownership of all files inside a container's rootfs.

    map_gid and map_uid: Contain integers in a list with format:
        [<start>, <target>, <count>]
    """
    if map_uid:
        uid_opts = get_mapping_opts(map_uid)
    if map_gid:
        gid_opts = get_mapping_opts(map_gid)

    for root, _ignore, files in os.walk(os.path.realpath(path)):
        for name in [root] + files:
            file_path = os.path.join(root, name)

            stat_info = os.lstat(file_path)
            old_uid = stat_info.st_uid
            old_gid = stat_info.st_gid

            new_uid = get_map_id(old_uid, uid_opts) if map_uid else -1
            new_gid = get_map_id(old_gid, gid_opts) if map_gid else -1
            os.lchown(file_path, new_uid, new_gid)


def mapping_uid_gid(dest, uid_map, gid_map):
    """
    Mapping ownership for each uid_map and gid_map.
    """
    len_diff = len(uid_map) - len(gid_map)

    if len_diff < 0:
        uid_map += [None] * abs(len_diff)
    elif len_diff > 0:
        gid_map += [None] * len_diff

    for uid, gid in zip(uid_map, gid_map):
        map_id(dest, uid, gid)


# pylint: disable=too-many-arguments
def bootstrap(uri, dest,
              fmt=utils.DEFAULT_OUTPUT_FORMAT,
              username=None,
              password=None,
              root_password=None,
              uid_map=None,
              gid_map=None,
              not_secure=False,
              no_cache=False,
              progress_cb=None):
    """
    Get source object and call unpack method
    """
    # Get instance of progress storing module
    prog = progress.Progress(progress_cb)

    uri = urlparse(uri)
    source = get_source(uri.scheme or 'file')

    if not os.path.exists(dest):
        os.makedirs(dest)
    elif not os.path.isdir(dest):  # Show error if not directory
        logger.error("Destination path '%s' is not directory.", dest)
        sys.exit(1)
    elif not os.access(dest, os.W_OK):  # Check write permissions
        logger.error("No write permissions on destination path '%s'", dest)
        sys.exit(1)

    source(uri=uri,
           fmt=fmt,
           username=username,
           password=password,
           not_secure=not_secure,
           no_cache=no_cache,
           progress=prog).unpack(dest)

    if root_password is not None:
        logger.info("Setting password of the root account")
        utils.set_root_password(fmt, dest, root_password)

    if fmt == "dir" and uid_map or gid_map:
        logger.info("Mapping UID/GID")
        mapping_uid_gid(dest, uid_map, gid_map)


def set_logging_conf(loglevel=None):
    """
    Set format and logging level
    """
    # Get logger
    module_logger = logging.getLogger('virtBootstrap')

    # Create console handler
    console_handler = logging.StreamHandler()

    # Set logging format
    log_format = ('%(levelname)-8s: %(message)s')
    console_handler.setFormatter(logging.Formatter(log_format))

    # Add the handlers to logger
    module_logger.addHandler(console_handler)

    # Set logging level
    module_logger.setLevel(loglevel or logging.INFO)


def main():
    parser = argparse.ArgumentParser(
        description=_("Container bootstrapping tool"),
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog=dedent(_('''
                            Example supported URI formats:
                            ----------------------------------------
                              docker://ubuntu:latest
                              docker://docker.io/fedora
                              docker://privateregistry:5000/image
                              file:///path/to/local/rootfs.tar.xz
                            ----------------------------------------

                        ''')))
    parser.add_argument("uri",
                        help=_("URI of container image"))
    parser.add_argument("dest",
                        help=_("Destination folder"))
    parser.add_argument("--not-secure", action='store_true',
                        help=_("Ignore HTTPS errors"))
    parser.add_argument("-u", "--username", default=None,
                        help=_("Username for accessing the source registry"))
    parser.add_argument("-p", "--password", default=None,
                        help=_("Password for accessing the source registry"))
    parser.add_argument("--root-password", default=None,
                        help=_("Set root password"))
    parser.add_argument("--uidmap", default=None, action='append',
                        metavar="<start>:<target>:<count>",
                        help=_("Map UIDs"))
    parser.add_argument("--gidmap", default=None, action='append',
                        metavar="<start>:<target>:<count>",
                        help=_("Map GIDs"))
    parser.add_argument("--idmap", default=None, action='append',
                        metavar="<start>:<target>:<count>",
                        help=_("Map both UIDs/GIDs"))
    parser.add_argument("--no-cache", action="store_true",
                        help=_("Do not store downloaded Docker images"))
    parser.add_argument("-f", "--format", default=utils.DEFAULT_OUTPUT_FORMAT,
                        choices=['dir', 'qcow2'],
                        help=_("Format to be used for the root filesystem"))
    parser.add_argument("-d", "--debug", action="store_const", dest="loglevel",
                        const=logging.DEBUG, help=_("Show debug messages"))
    parser.add_argument("-q", "--quiet", action="store_const", dest="loglevel",
                        const=logging.WARNING,
                        help=_("Don't print progress messages"))
    parser.add_argument("--status-only", action="store_const",
                        const=utils.write_progress,
                        help=_("Show only progress information"))

    try:
        args = parser.parse_args()

        if not args.status_only:
            # Configure logging lovel/format
            set_logging_conf(args.loglevel)

        uid_map = parse_idmap(args.idmap) if args.idmap else []
        gid_map = list(uid_map) if args.idmap else []
        if args.uidmap:
            uid_map += parse_idmap(args.uidmap)
        if args.gidmap:
            gid_map += parse_idmap(args.gidmap)

        # do the job here!
        bootstrap(uri=args.uri,
                  dest=args.dest,
                  fmt=args.format,
                  username=args.username,
                  password=args.password,
                  root_password=args.root_password,
                  uid_map=uid_map,
                  gid_map=gid_map,
                  not_secure=args.not_secure,
                  no_cache=args.no_cache,
                  progress_cb=args.status_only)

        sys.exit(0)
    except KeyboardInterrupt:
        sys.exit(0)
    except ValueError as err:
        sys.stderr.write("%s: %s\n" % (sys.argv[0], err))
        sys.stderr.flush()
        sys.exit(1)


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