s3_upgrade: Update commandline options and use updated samba3 python module
[Samba/gebeck_regimport.git] / source4 / setup / upgrade_from_s3
blobc7a4b97ef4e8a289ea98988921167e15d16b7471
1 #!/usr/bin/env python
3 # Upgrade from Samba3
4 # Copyright Jelmer Vernooij 2005-2007
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 import logging
20 import optparse
21 import os, sys
23 # Find right directory when running from source tree
24 sys.path.insert(0, "bin/python")
26 import samba
27 import samba.getopt as options
28 from samba.auth import system_session
29 from samba.upgrade import upgrade_from_samba3
30 from samba.samba3 import Samba3
31 from samba.samba3 import param as s3param
33 def get_testparm_var(testparm, varname):
34 cmd = "%s -s -l --parameter-name='%s' 2>/dev/null" % (testparm, varname)
35 output = os.popen(cmd, 'r').readline()
36 return output.strip()
38 cmd_help = """upgrade_from_s3 [options] <configuration_file> <targetdir>
40 Input arguments are:
41 <configuration_file> - path to existing smb.conf
42 <targetdir> - directory in which samba4 database will be created
44 In addition, specify either samba3 database directory (with --libdir) or
45 samba3 testparm utility (with --testparm). """
47 parser = optparse.OptionParser(cmd_help)
48 parser.add_option_group(options.VersionOptions(parser))
49 parser.add_option("--quiet", help="Be quiet")
50 parser.add_option("--libdir", type="string", metavar="DIR",
51 help="samba3 database directory")
52 parser.add_option("--testparm", type="string", metavar="PATH",
53 help="samba3 testparm utility")
55 opts, args = parser.parse_args()
57 if len(args) < 2:
58 parser.print_usage()
59 sys.exit(1)
61 smbconf = args[0]
62 if not os.path.exists(smbconf):
63 print("error: %s does not exist" % smbconf)
64 sys.exit(1)
66 targetdir = args[1]
67 if not os.path.isdir(targetdir):
68 print("error: %s is not a directory" % targetdir)
69 sys.exit(1)
71 libdir = opts.libdir
72 testparm = opts.testparm
74 if not libdir and not testparm:
75 print("error: please specify either libdir or testparm")
76 sys.exit(1)
78 if libdir and testparm:
79 print("warning: both libdir and testparm specified, libdir will be ignored.")
80 libdir = None
82 logger = logging.getLogger("upgrade")
83 logger.addHandler(logging.StreamHandler(sys.stdout))
84 if opts.quiet:
85 logger.setLevel(logging.WARNING)
86 else:
87 logger.setLevel(logging.INFO)
89 s3conf = s3param.get_context()
90 #s3conf.load_default()
92 # Set correct default values from libdir or testparm
93 paths = {}
94 if libdir:
95 paths["state directory"] = libdir
96 paths["private dir"] = libdir
97 else:
98 paths["state directory"] = get_testparm_var(testparm, "state directory")
99 paths["private dir"] = get_testparm_var(testparm, "private dir")
101 for p in paths:
102 s3conf.set(p, paths[p])
104 # load smb.conf parameters
105 logger.info("Reading smb.conf")
106 s3conf.load(smbconf)
107 samba3 = Samba3(smbconf, s3conf)
109 logger.info("Provisioning")
110 upgrade_from_samba3(samba3, logger, targetdir, session_info=system_session())