summaryrefslogtreecommitdiff
path: root/source4/setup/upgrade_from_s3
blob: c7a4b97ef4e8a289ea98988921167e15d16b7471 (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
#!/usr/bin/env python
#
# Upgrade from Samba3
# Copyright Jelmer Vernooij 2005-2007
#
# 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/>.

import logging
import optparse
import os, sys

# Find right directory when running from source tree
sys.path.insert(0, "bin/python")

import samba
import samba.getopt as options
from samba.auth import system_session
from samba.upgrade import upgrade_from_samba3
from samba.samba3 import Samba3
from samba.samba3 import param as s3param

def get_testparm_var(testparm, varname):
    cmd = "%s -s -l --parameter-name='%s' 2>/dev/null" % (testparm, varname)
    output = os.popen(cmd, 'r').readline()
    return output.strip()

cmd_help = """upgrade_from_s3 [options] <configuration_file> <targetdir>

Input arguments are:
  <configuration_file> - path to existing smb.conf
  <targetdir>          - directory in which samba4 database will be created

In addition, specify either samba3 database directory (with --libdir) or
samba3 testparm utility (with --testparm).  """

parser = optparse.OptionParser(cmd_help)
parser.add_option_group(options.VersionOptions(parser))
parser.add_option("--quiet", help="Be quiet")
parser.add_option("--libdir", type="string", metavar="DIR",
                  help="samba3 database directory")
parser.add_option("--testparm", type="string", metavar="PATH",
                  help="samba3 testparm utility")

opts, args = parser.parse_args()

if len(args) < 2:
    parser.print_usage()
    sys.exit(1)

smbconf = args[0]
if not os.path.exists(smbconf):
    print("error: %s does not exist" % smbconf)
    sys.exit(1)

targetdir = args[1]
if not os.path.isdir(targetdir):
    print("error: %s is not a directory" % targetdir)
    sys.exit(1)

libdir = opts.libdir
testparm = opts.testparm

if not libdir and not testparm:
    print("error: please specify either libdir or testparm")
    sys.exit(1)

if libdir and testparm:
    print("warning: both libdir and testparm specified, libdir will be ignored.")
    libdir = None

logger = logging.getLogger("upgrade")
logger.addHandler(logging.StreamHandler(sys.stdout))
if opts.quiet:
	logger.setLevel(logging.WARNING)
else:
	logger.setLevel(logging.INFO)

s3conf = s3param.get_context()
#s3conf.load_default()

# Set correct default values from libdir or testparm
paths = {}
if libdir:
    paths["state directory"] = libdir
    paths["private dir"] = libdir
else:
    paths["state directory"] = get_testparm_var(testparm, "state directory")
    paths["private dir"] = get_testparm_var(testparm, "private dir")

for p in paths:
    s3conf.set(p, paths[p])

# load smb.conf parameters
logger.info("Reading smb.conf")
s3conf.load(smbconf)
samba3 = Samba3(smbconf, s3conf)

logger.info("Provisioning")
upgrade_from_samba3(samba3, logger, targetdir, session_info=system_session())