summaryrefslogtreecommitdiffstats
path: root/ipa-server/ipa-install/ipa-replica-install
blob: d94c47ea6b8542073a6951243976dee998db8dc4 (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
#! /usr/bin/python -E
# Authors: Karl MacMillan <kmacmillan@mentalrootkit.com>
#
# Copyright (C) 2007  Red Hat
# see file 'COPYING' for use and warranty information
#
# 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 or later
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

import sys

import tempfile, os, pwd, traceback, logging
from ConfigParser import SafeConfigParser

from ipa import ipautil

from ipaserver import dsinstance, replication, installutils, krbinstance, service
from ipaserver import httpinstance, webguiinstance, radiusinstance, ntpinstance, certs

class ReplicaConfig:
    def __init__(self):
        self.realm_name = ""
        self.master_host_name = ""
        self.dirman_password = ""
        self.ds_user = ""
        self.host_name = ""
        self.repl_password = ""
        self.dir = ""

def parse_options():
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-d", "--debug", dest="debug", action="store_true",
                      default=False, help="gather extra debugging information")

    options, args = parser.parse_args()

    if len(args) != 1:
        parser.error("you must provide a file generated by ipa-replica-prepare")

    return options, args[0]

def get_dirman_password():
    return installutils.read_password("Directory Manager (existing master)")

def expand_info(filename):
    top_dir = tempfile.mkdtemp("ipa")
    dir = top_dir + "/realm_info"
    ipautil.run(["tar", "xfz", filename, "-C", top_dir])

    return top_dir, dir

def read_info(dir, rconfig):
    filename = dir + "/realm_info"
    fd = open(filename)
    config = SafeConfigParser()
    config.readfp(fd)

    rconfig.realm_name = config.get("realm", "realm_name")
    rconfig.master_host_name = config.get("realm", "master_host_name")
    rconfig.ds_user = config.get("realm", "ds_user")

def get_host_name():
    hostname = installutils.get_fqdn()
    try:
        installutils.verify_fqdn(hostname)
    except RuntimeError, e:
        logging.error(str(e))
        sys.exit(1)

    return hostname

def set_owner(config, dir):
    pw = pwd.getpwnam(config.ds_user)
    os.chown(dir, pw.pw_uid, pw.pw_gid)

def install_ds(config):
    dsinstance.check_existing_installation()
    dsinstance.check_ports()

    # if we have a pkcs12 file, create the cert db from
    # that. Otherwise the ds setup will create the CA
    # cert
    pkcs12_info = None
    if ipautil.file_exists(config.dir + "/cacert.p12"):
        pkcs12_info = (config.dir + "/cacert.p12",
                       config.dir + "/pwdfile.txt")

    ds = dsinstance.DsInstance()
    ds.create_instance(config.ds_user, config.realm_name, config.host_name, config.dirman_password,
                       pkcs12_info)

def install_krb(config):
    krb = krbinstance.KrbInstance()
    ldappwd_filename = config.dir + "/ldappwd"
    krb.create_replica(config.ds_user, config.realm_name, config.host_name,
                       config.dirman_password, ldappwd_filename)

def install_http(config):
    http = httpinstance.HTTPInstance()
    http.create_instance(config.realm_name, config.host_name)

def main():
    options, filename = parse_options()
    installutils.standard_logging_setup("ipareplica-install.log", options.debug)

    top_dir, dir = expand_info(filename)
    
    config = ReplicaConfig()
    read_info(dir, config)
    config.host_name = get_host_name()
    config.repl_password = ipautil.ipa_generate_password()
    config.dir = dir

    # get the directory manager password
    config.dirman_password = get_dirman_password()

    install_ds(config)
    
    repl = replication.ReplicationManager(config.host_name, config.dirman_password)
    ret = repl.setup_replication(config.master_host_name, config.realm_name)
    if ret != 0:
        raise RuntimeError("failed to start replication")

    install_krb(config)
    install_http(config)
    
    # Create a Web Gui instance
    webgui = webguiinstance.WebGuiInstance()
    webgui.create_instance()

    # Configure ntpd
    ntp = ntpinstance.NTPInstance()
    ntp.create_instance()


    service.restart("dirsrv")
    service.restart("krb5kdc")
    
try:
    main()
except Exception, e:
    print "creation of replica failed: %s" % str(e)
    message = str(e)
    for str in traceback.format_tb(sys.exc_info()[2]):
        message = message + "\n" + str
    logging.debug(message)
    sys.exit(1)