summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Admin/Init.py
blob: 7a18c9287225a434b36269a5f354b49e0f2d99c9 (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
import os, socket
import Bcfg2.Server.Admin
import Bcfg2.Options

config = '''
[server]
repository = %s
structures = %s
generators = %s

[statistics]
sendmailpath = %s
database_engine = sqlite3
# 'postgresql', 'mysql', 'mysql_old', 'sqlite3' or 'ado_mssql'.
database_name =
# Or path to database file if using sqlite3.
#<repository>/etc/brpt.sqlite is default path if left empty
database_user =
# Not used with sqlite3.
database_password =
# Not used with sqlite3.
database_host =
# Not used with sqlite3.
database_port =
# Set to empty string for default. Not used with sqlite3.
web_debug = True


[communication]
protocol = %s
password = %s
key = %s/bcfg2.key

[components]
bcfg2 = %s
'''

groups = '''
<Groups version='3.0'>
   <Group profile='true' public='false' default='true' name='basic'>
      <Group name='%s'/>
   </Group>
   <Group name='ubuntu'/>
   <Group name='debian'/>
   <Group name='freebsd'/>
   <Group name='gentoo'/>
   <Group name='redhat'/>
   <Group name='suse'/>
   <Group name='mandrake'/>
   <Group name='solaris'/>
</Groups>
'''
clients = '''
<Clients version="3.0">
   <Client profile="basic" pingable="Y" pingtime="0" name="%s"/>
</Clients>
'''

os_list = [('Redhat/Fedora/RHEL/RHAS/Centos', 'redhat'),
           ('SUSE/SLES', 'suse'),
           ('Mandrake', 'mandrake'),
           ('Debian', 'debian'),
           ('Ubuntu', 'ubuntu'),
           ('Gentoo', 'gentoo'),
           ('FreeBSD', 'freebsd')]


class Init(Bcfg2.Server.Admin.Mode):
    __shorthelp__ = 'bcfg2-admin init'
    __longhelp__ = __shorthelp__ + '\n\tCompare two client specifications or directories of specifications'
    options = {'repo': Bcfg2.Options.SERVER_REPOSITORY,
               'struct': Bcfg2.Options.SERVER_STRUCTURES,
               'gens': Bcfg2.Options.SERVER_GENERATORS,
               'proto': Bcfg2.Options.SERVER_PROTOCOL,
               'sendmail': Bcfg2.Options.SENDMAIL_PATH}
    
    def __call__(self, args):
        Bcfg2.Server.Admin.Mode.__call__(self, args)
        opts = Bcfg2.Options.OptionParser(options)
        opts.parse([])
        repopath = raw_input("location of bcfg2 repository [%s]: " % opts['repo'])
        if repopath == '':
            repopath = opts['repo']
        password = ''
        while ( password == '' ):
            password = raw_input(
                "Input password used for communication verification: " )
        server = "https://%s:6789" % socket.getfqdn()
        rs = raw_input( "Input the server location [%s]: " % server)
        if rs:
            server = rs
        #create the groups.xml file
        prompt = '''Input base Operating System for clients:\n'''
        for entry in os_list:
            prompt += "%d: \n" % (os_list.index(entry) + 1, entry[0])
        prompt += ': '
        os_sel = os_list[int(raw_input(prompt))-1][1]
        self.initializeRepo(repopath, server, password, os_sel, opts)
        print "Repository created successfuly in %s" % (repopath)

    def initializeRepo(self, repo, server_uri, password, os_selection, opts):
        '''Setup a new repo'''
        keypath = os.path.dirname(os.path.abspath(settings.CONFIG_FILE))
        confdata = config % ( 
                        repo, opts['struct'], opts['gens'], 
                        opts['sendmail'], opts['proto'],
                        password, keypath, server_uri 
                    )

        open(settings.CONFIG_FILE,"w").write(confdata)
        # FIXME automate ssl key generation
        os.popen('openssl req -x509 -nodes -days 1000 -newkey rsa:1024 -out %s/bcfg2.key -keyout %s/bcfg2.key' % (keypath, keypath))
        try:
            os.chmod('%s/bcfg2.key'% keypath,'0600')
        except:
            pass
    
        for subdir in ['SSHbase', 'Cfg', 'Pkgmgr', 'Rules', 'etc', 'Metadata',
                       'Base', 'Bundler']:
            path = "%s/%s" % (repo, subdir)
            newpath = ''
            for subdir in path.split('/'):
                newpath = newpath + subdir + '/'
                try:
                    os.mkdir(newpath)
                except:
                    continue
            
        open("%s/Metadata/groups.xml"%repo, "w").write(groups % os_selection)
        #now the clients file
        open("%s/Metadata/clients.xml"%repo, "w").write(clients % socket.getfqdn())