SlapOSControler.py 5.4 KB
Newer Older
Łukasz Nowak's avatar
Łukasz Nowak committed
1 2
##############################################################################
#
3
# Copyright (c) 2011 Nexedi SA and Contributors. All Rights Reserved.
Łukasz Nowak's avatar
Łukasz Nowak committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#
# 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 advised 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.
#
##############################################################################
27 28 29 30 31
import os
import slapos.slap
import subprocess
import time
import xml_marshaller
32 33 34

class SlapOSControler(object):

35 36 37 38 39 40
  def log(self, message):
    print message

  def __init__(self, config, process_group_pid_set=None, log=None):
    if log is not None:
      self.log = log
41 42 43 44 45 46 47
    self.config = config
    # By erasing everything, we make sure that we are able to "update"
    # existing profiles. This is quite dirty way to do updates...
    if os.path.exists(config['proxy_database']):
      os.unlink(config['proxy_database'])
    proxy = subprocess.Popen([config['slapproxy_binary'],
      config['slapos_config']], close_fds=True, preexec_fn=os.setsid)
48
    process_group_pid_set.add(proxy.pid)
49 50
    # XXX: dirty, giving some time for proxy to being able to accept
    # connections
51
    time.sleep(10)
52 53 54 55 56 57 58 59 60 61 62 63 64
    slap = slapos.slap.slap()
    slap.initializeConnection(config['master_url'])
    # register software profile
    self.software_profile = config['custom_profile_path']
    slap.registerSupply().supply(
        self.software_profile,
        computer_guid=config['computer_id'])
    computer = slap.registerComputer(config['computer_id'])
    # create partition and configure computer
    partition_reference = config['partition_reference']
    partition_path = os.path.join(config['instance_root'], partition_reference)
    if not os.path.exists(partition_path):
      os.mkdir(partition_path)
65
    os.chmod(partition_path, 0750)
66
    computer.updateConfiguration(xml_marshaller.xml_marshaller.dumps({
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
 'address': config['ipv4_address'],
 'instance_root': config['instance_root'],
 'netmask': '255.255.255.255',
 'partition_list': [{'address_list': [{'addr': config['ipv4_address'],
                                       'netmask': '255.255.255.255'},
                                      {'addr': config['ipv6_address'],
                                       'netmask': 'ffff:ffff:ffff::'},
                      ],
                     'path': partition_path,
                     'reference': partition_reference,
                     'tap': {'name': partition_reference},
                     }
                    ],
 'reference': config['computer_id'],
 'software_root': config['software_root']}))

83 84
  def runSoftwareRelease(self, config, environment, process_group_pid_set=None,
                         stdout=None, stderr=None):
85
    self.log("SlapOSControler.runSoftwareRelease")
86 87 88
    cpu_count = os.sysconf("SC_NPROCESSORS_ONLN")
    os.putenv('MAKEFLAGS', '-j%s' % cpu_count)
    os.environ['PATH'] = environment['PATH']
89
    command = [config['slapgrid_software_binary'], '-v', '-c',
90
      #'--buildout-parameter',"'-U -N' -o",
91 92
      config['slapos_config']]
    slapgrid = subprocess.Popen(command,
93 94 95 96 97 98 99 100
      stdout=stdout, stderr=stderr,
      close_fds=True, preexec_fn=os.setsid)
    process_group_pid_set.add(slapgrid.pid)
    slapgrid.wait()
    stdout.seek(0)
    stderr.seek(0)
    process_group_pid_set.remove(slapgrid.pid)
    status_dict = {'status_code':slapgrid.returncode,
101
                    'command': repr(command),
102 103 104 105 106
                    'stdout':stdout.read(),
                    'stderr':stderr.read()}
    stdout.close()
    stderr.close()
    return status_dict
107

108 109 110
  def runComputerPartition(self, config, environment,
                           process_group_pid_set=None,
                           stdout=None, stderr=None):
111
    self.log("SlapOSControler.runComputerPartition")
112 113 114 115
    slap = slapos.slap.slap()
    slap.registerOpenOrder().request(self.software_profile,
        partition_reference='testing partition',
        partition_parameter_kw=config['instance_dict'])
116 117 118
    command = [config['slapgrid_partition_binary'],
      config['slapos_config'], '-c', '-v']
    slapgrid = subprocess.Popen(command,
119 120
      stdout=stdout, stderr=stderr,
      close_fds=True, preexec_fn=os.setsid)
121
    process_group_pid_set.add(slapgrid.pid)
122
    slapgrid.wait()
123 124
    stdout.seek(0)
    stderr.seek(0)
125
    process_group_pid_set.remove(slapgrid.pid)
126
    status_dict = {'status_code':slapgrid.returncode,
127
                    'command': repr(command),
128 129 130 131
                    'stdout':stdout.read(),
                    'stderr':stderr.read()}
    stdout.close()
    stderr.close()
132
    return status_dict