nfsboot.check 3.54 KB
Newer Older
1
#!/usr/bin/python2
2
# Copyright (C) 2014-2015  Codethink Limited
3 4 5 6 7 8 9 10 11 12 13
#
# 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
14
# with this program.  If not, see <http://www.gnu.org/licenses/>.
15 16 17

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

18
import os
19

20
import writeexts
21 22


23
class NFSBootCheckExtension(writeexts.WriteExtension):
24 25 26

    _nfsboot_root = '/srv/nfsboot'

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

32 33
        location = args[0]

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

39 40
        hostname = os.environ.get('HOSTNAME', None)
        if hostname is None:
41
            raise writeexts.ExtensionError('You must specify a HOSTNAME.')
42
        if hostname == 'baserock':
43 44 45
            raise writeexts.ExtensionError('It is forbidden to nfsboot a '
                                           'system with hostname "%s"'
                                           % hostname)
46 47 48 49 50 51 52

        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):
53
            raise writeexts.ExtensionError(
54 55 56 57 58
                '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):
59
        self.check_ssh_connectivity(server)
60 61 62

        # Is an NFS server
        try:
63
            writeexts.ssh_runcmd(
64
                '[email protected]%s' % server, ['test', '-e', '/etc/exports'])
65 66 67
        except writeexts.ExtensionError:
            raise writeexts.ExtensionError('server %s is not an nfs server'
                                           % server)
68
        try:
69
            writeexts.ssh_runcmd(
70 71 72
                '[email protected]%s' % server, ['systemctl', 'is-enabled',
                                     'nfs-server.service'])

73 74 75
        except writeexts.ExtensionError:
            raise writeexts.ExtensionError('server %s does not control its '
                                           'nfs server by systemd' % server)
76 77

        # TFTP server exports /srv/nfsboot/tftp
78
        tftp_root = os.path.join(self._nfsboot_root, 'tftp')
79
        try:
80
            writeexts.ssh_runcmd(
81
                '[email protected]%s' % server, ['test' , '-d', tftp_root])
82 83 84
        except writeexts.ExtensionError:
            raise writeexts.ExtensionError('server %s does not export %s' %
                                           (tftp_root, server))
85 86 87

    def version_exists(self, versioned_root, location):
        try:
88 89 90
            writeexts.ssh_runcmd('[email protected]%s' % location,
                                 ['test', '-d', versioned_root])
        except writeexts.ExtensionError:
91 92 93 94 95
            return False

        return True


96
NFSBootCheckExtension().run()