container.py 4.18 KB
Newer Older
Antoine Catton's avatar
Antoine Catton committed
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
##############################################################################
#
# Copyright (c) 2010 Vifib SARL and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

import ConfigParser
29 30 31
import uuid
import os
import subprocess
Antoine Catton's avatar
Antoine Catton committed
32 33 34 35 36 37 38 39

# XXX : This is in order to get the computer_partition object
#       which exposes the state of the current partition.
#
# XXX : We could have modify slapgrid in order to put the
#       state of the current partition offline. But this is
#       written to have the most minimal impact.
from slapos.recipe.librecipe import GenericSlapRecipe
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
from slapos.recipe.librecipe import GenericBaseRecipe

def promise_func(args):

    output = subprocess.check_output(
        [args['lxc-info'], '-n', args['name']]
    )

    if 'RUNNING' in output:
        return 0
    else:
        return 127



class Promise(GenericBaseRecipe):

    def install(self):
        return [
            self.createPythonScript(
                self.options['promise'],
                'slapos.recipe.container.promise_func',
                {
                    'lxc-info': self.options['lxc-info'],
                    'name': self.options['slapcontainer-name']
                }
            )
        ]


Antoine Catton's avatar
Antoine Catton committed
70 71 72

class Recipe(GenericSlapRecipe):

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    def _options(self, options):
        config_filename = self.options['config']

        container_uuid = None

        if os.path.exists(config_filename):
            config = ConfigParser.ConfigParser()
            config.read(config_filename)
            if config.has_option('requested', 'name'):
                container_uuid = uuid.UUID(hex=config.get('requested', 'name'))

        if container_uuid is None:
            # uuid wasn't generated at first in order to avoid
            # wasting entropy
            container_uuid = uuid.uuid4()

        options['slapcontainer-name'] = container_uuid.hex

        return options



Antoine Catton's avatar
Antoine Catton committed
95 96 97 98 99 100 101 102 103
    def _install(self):
        path_list = []

        self.logger.info("Putting slapcontainer configuration file...")

        config = ConfigParser.ConfigParser()
        config.add_section('requested')
        config.set('requested', 'status',
                   self.computer_partition.getState())
104
        config.set('requested', 'name', self.options['slapcontainer-name'])
105 106 107 108 109 110 111 112 113 114 115 116
        config.add_section('network')
        config.set('network', 'ipv6', self.options['ipv6'])
        config.set('network', 'ipv4', self.options['ipv4'])
        config.set('network', 'interface', self.options['interface'])
        config.add_section('rootfs')
        config.set('rootfs', 'directory', self.options['rootfs'])
        config.set('rootfs', 'tmp', self.options['tmp-dir'])
        config.add_section('config')
        config.set('config', 'file', self.options['config-file'])
        config.add_section('tar')
        config.set('tar', 'binary', self.options['tar-binary'])
        config.set('tar', 'path', self.options['tar-path'])
Antoine Catton's avatar
Antoine Catton committed
117 118 119 120 121 122 123

        config_filename = self.options['config']
        with open(config_filename, 'w') as config_file:
            config.write(config_file)
        path_list.append(config_filename)

        return path_list