summaryrefslogtreecommitdiff
path: root/extensions/nfsboot.check
blob: 0b2e6be7bf02a1818c19522d9029b08b827a1875 (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
#!/usr/bin/python2
# Copyright (C) 2014-2015  Codethink Limited
#
# 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 of the License.
#
# 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/>.

'''Preparatory checks for Morph 'nfsboot' write extension'''

import os

import writeexts


class NFSBootCheckExtension(writeexts.WriteExtension):

    _nfsboot_root = '/srv/nfsboot'

    def process_args(self, args):
        if len(args) != 1:
            raise writeexts.ExtensionError(
                'Wrong number of command line args')

        location = args[0]

        upgrade = self.get_environment_boolean('UPGRADE')
        if upgrade:
            raise writeexts.ExtensionError(
                'Upgrading is not currently supported for NFS deployments.')

        hostname = os.environ.get('HOSTNAME', None)
        if hostname is None:
            raise writeexts.ExtensionError('You must specify a HOSTNAME.')
        if hostname == 'baserock':
            raise writeexts.ExtensionError('It is forbidden to nfsboot a '
                                           'system with hostname "%s"'
                                           % hostname)

        self.test_good_server(location)

        version_label = os.getenv('VERSION_LABEL', 'factory')
        versioned_root = os.path.join(self._nfsboot_root, hostname, 'systems',
                                      version_label)
        if self.version_exists(versioned_root, location):
            raise writeexts.ExtensionError(
                'Root file system for host %s (version %s) already exists on '
                'the NFS server %s. Deployment aborted.' % (hostname,
                version_label, location))

    def test_good_server(self, server):
        self.check_ssh_connectivity(server)

        # Is an NFS server
        try:
            writeexts.ssh_runcmd(
                'root@%s' % server, ['test', '-e', '/etc/exports'])
        except writeexts.ExtensionError:
            raise writeexts.ExtensionError('server %s is not an nfs server'
                                           % server)
        try:
            writeexts.ssh_runcmd(
                'root@%s' % server, ['systemctl', 'is-enabled',
                                     'nfs-server.service'])

        except writeexts.ExtensionError:
            raise writeexts.ExtensionError('server %s does not control its '
                                           'nfs server by systemd' % server)

        # TFTP server exports /srv/nfsboot/tftp
        tftp_root = os.path.join(self._nfsboot_root, 'tftp')
        try:
            writeexts.ssh_runcmd(
                'root@%s' % server, ['test' , '-d', tftp_root])
        except writeexts.ExtensionError:
            raise writeexts.ExtensionError('server %s does not export %s' %
                                           (tftp_root, server))

    def version_exists(self, versioned_root, location):
        try:
            writeexts.ssh_runcmd('root@%s' % location,
                                 ['test', '-d', versioned_root])
        except writeexts.ExtensionError:
            return False

        return True


NFSBootCheckExtension().run()