Feature #298
[mediagoblin.git] / mediagoblin / gmg_commands / import_export.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 from mediagoblin.gmg_commands import util as commands_util
18 from mediagoblin import mg_globals
19 from mediagoblin.db import util as db_util
20 from mediagoblin.db.open import setup_connection_and_db_from_config
21 from mediagoblin.init.config import read_mediagoblin_config
22
23 import shlex
24 import tarfile
25 import subprocess
26 import os.path
27
28 def import_export_parse_setup(subparser):
29 # TODO: Add default
30 subparser.add_argument(
31 'tar_file')
32 subparser.add_argument(
33 '-cf', '--conf_file', default='mediagoblin.ini',
34 help='Config file used to set up environment')
35 subparser.add_argument(
36 '--mongodump_cache', default='mongodump',
37 help='mongodump cache directory')
38 subparser.add_argument(
39 '--mongodump_path', default='mongodump',
40 help='mongodump binary')
41 subparser.add_argument(
42 '--mongorestore_path', default='mongorestore',
43 help='mongorestore binary')
44 subparser.add_argument(
45 '--extract_path', default='/tmp/mediagoblin-import',
46 help='the directory to which the tarball should be extracted temporarily')
47
48 def _export_database(db, args):
49 print "\n== Exporting database ==\n"
50
51 command = '{mongodump_path} -d {database} -o {mongodump_cache}'.format(
52 mongodump_path=args.mongodump_path,
53 database=db.name,
54 mongodump_cache=args.mongodump_cache)
55
56 p = subprocess.Popen(
57 shlex.split(command))
58
59 p.wait()
60
61 print "\n== Database exported ==\n"
62
63 def _import_database(db, args):
64 command = '{mongorestore_path} -d {database} -o {mongodump_cache}'.format(
65 mongorestore_path=args.mongorestore_path,
66 database=db.name,
67 mongodump_cache=args.mongodump_cache)
68
69 def env_import(args):
70 config, validation_result = read_mediagoblin_config(args.conf_file)
71 connection, db = setup_connection_and_db_from_config(
72 config['mediagoblin'], use_pymongo=True)
73
74 tf = tarfile.open(
75 args.tar_file,
76 mode='r|gz')
77
78 tf.extractall(args.extract_path)
79
80 def env_export(args):
81 config, validation_result = read_mediagoblin_config(args.conf_file)
82 connection, db = setup_connection_and_db_from_config(
83 config['mediagoblin'], use_pymongo=True)
84
85 if os.path.exists(args.tar_file):
86 overwrite = raw_input(
87 'The output file already exists. '
88 'Are you **SURE** you want to overwrite it? '
89 '(yes/no)> ')
90 if not overwrite == 'yes':
91 print "Aborting."
92 return
93
94 tf = tarfile.open(
95 args.tar_file,
96 mode='w|gz')
97
98
99 _export_database(db, args)
100
101 tf.add(args.mongodump_cache)
102