slaptest 14.5 KB
Newer Older
Cédric Le Ninivin's avatar
Cédric Le Ninivin 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 29 30 31 32 33 34 35
#!/usr/bin/python
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2012 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 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.
#
##############################################################################

import ConfigParser
import logging
from optparse import OptionParser, Option
import os
import sys
36 37
import tempfile
import urllib2
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
38 39 40 41


# create console handler and set level to debug
ch = logging.StreamHandler()
42
ch.setLevel(logging.WARNING)
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
43
# create formatter
44
formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
# add formatter to ch
ch.setFormatter(formatter)



class Parser(OptionParser):
  """
  Parse all arguments.
  """
  def __init__(self, usage=None, version=None):
    """
    Initialize all options possibles.
    """
    OptionParser.__init__(self, usage=usage, version=version,
                          option_list=[
      Option("--slapos-configuration",
             help="path to slapos configuration directory",
             default='/etc/opt/slapos/',
             type=str),
      Option("--slapos-cron",
             help="path to slapos cron file",
             default='/etc/cron.d/slapos-node',
             type=str),
68 69 70 71
      Option("--check-upload",
             help="Check if upload parameters are ok (do not check certificates)",
             default=False,
             action="store_true"),
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
72 73 74 75
      Option("--test-agent",
             help="Check if parameters are good for a test agent",
             default=False,
             action="store_true"),
76 77 78 79
      Option("-v","--verbose",
             default=False,
             action="store_true",
             help="Verbose output."),
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
80 81 82 83 84 85 86 87 88 89 90
      Option("-n", "--dry-run",
             help="Simulate the execution steps",
             default=False,
             action="store_true"),
   ])

  def check_args(self):
    """
    Check arguments
    """
    (options, args) = self.parse_args()
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
91 92
    if options.test_agent :
      options.check_upload = True
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
93 94 95
    return options


96
def get_slapos_conf_example():
97 98 99
  """
  Get slapos.cfg.example and return its path
  """
100 101
  register_server_url = "http://git.erp5.org/gitweb/slapos.core.git/blob_plain/HEAD:/slapos.cfg.example"
  request = urllib2.Request(register_server_url)
102
  url = urllib2.urlopen(request)
103 104 105 106 107 108
  page = url.read()
  info, path = tempfile.mkstemp()
  slapos_cfg_example = open(path,'w')
  slapos_cfg_example.write(page)
  slapos_cfg_example.close()
  return path
109

110

Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
111 112
def check_networkcache(config,logger,configuration_parser
                       ,configuration_example_parser):
113 114 115
  """
  Check network cache download
  """
116
  section = "networkcache"
117 118 119 120 121 122 123 124 125 126
  if configuration_parser.has_section(section) :
    configuration_example_dict = dict(configuration_example_parser.items(section))
    configuration_dict = dict(configuration_parser.items(section))
    for key in configuration_example_dict:
      try:
        if not configuration_dict[key] ==  configuration_example_dict[key] :
          logger.warn("%s parameter in %s section is out of date" % (key, section))
      except KeyError:
        logger.warn("No %s parameter in %s section" % (key,section))
        pass
127

128 129 130 131 132
    if config.test_agent:
      configuration_dict = dict(configuration_parser.items('slapformat'))
      if int(configuration_dict['partition_amount']) < 60 :
        logger.warn("Partition amount is to low for a test agent. Is %s but should be at least 60"
                    % configuration_dict['partition_amount'] )
133

134 135
    if config.check_upload == True :
      check_networkcache_upload(config,logger,configuration_dict)
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
136

137

138 139
class Upload:
  """
140
  Class used as a reference to check network cache upload
141 142 143 144 145 146 147
  """
  def __init__(self):
    self.data = {'download-binary-dir-url': 'http://www.shacache.org/shadir',
                 'signature_certificate_file': '/etc/slapos-cache/signature.cert',
                 'upload-dir-url': 'https://www.shacache.org/shadir',
                 'shadir-cert-file': '/etc/slapos-cache/shacache.cert',
                 'download-cache-url': 'https://www.shacache.org/shacache',
148 149 150 151
                 'upload-cache-url': 'https://www.shacache.org/shacache',
                 'shacache-cert-file': '/etc/slapos-cache/shacache.cert',
                 'upload-binary-cache-url': 'https://www.shacache.org/shacache',
                 'shacache-key-file': '/etc/slapos-cache/shacache.key',
152
                 'download-binary-cache-url': 'http://www.shacache.org/shacache',
153 154
                 'upload-binary-dir-url':'https://www.shacache.org/shadir',
                 'signature_private_key_file': '/etc/slapos-cache/signature.key',
155 156
                 'shadir-key-file': '/etc/slapos-cache/shacache.key'}

157 158

def check_networkcache_upload(config,logger,configuration_dict):
159 160 161
  """
  Check network cache upload
  """
162 163 164 165 166 167
  upload_parameters = Upload()
  for key in upload_parameters.data:
    try:
      if not key.find("file") == -1:
        file = configuration_dict[key]
        if not os.path.exists(file) :
168 169
          logger.critical ("%s file for %s parameters does not exist "
                         % (file,key))
170
        else :
171
          logger.info ("%s parameter:%s does exists" % (key,file))
172 173
      else :
        if not configuration_dict[key] == upload_parameters.data[key]:
174
          logger.warn("%s is %s sould be %s"
175 176 177
                      %(key,configuration_dict[key]
                        ,upload_parameters.data[key]))
    except KeyError:
178
      logger.critical("No %s parameter in networkcache section "
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
179
                        % (key))
180
      pass
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
181

182
def get_computer_name(certificate):
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
183 184 185
  """
  Extract computer_id for certificate
  """
186 187 188 189 190 191 192 193 194 195 196
  certificate = open(certificate,"r")
  for line in certificate:
    i=0
    if "Subject" in line:
      k=line.find("COMP-")
      i=line.find("/email")
      certificate.close()
      return line[k:i]
  return -1

def check_computer_id(logger,computer_id,cert_file):
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
197 198 199
  """
  Get computer id from cert_file and compare with computer_id
  """
200 201 202 203
  comp_cert = get_computer_name(cert_file)
  if comp_cert == "":
    logger.error("Certificate file indicated is corrupted (no computer id)")
  elif comp_cert == computer_id :
204
      logger.info("Certificate and slapos.cfg define same computer id: %s"
205 206 207 208
                   % computer_id)
  else :
    logger.critical("Computers id from cerificate (%s) is different from slapos.cfg (%s)"
                    % (comp_cert,computer_id))
209 210 211 212 213 214 215 216 217 218 219 220 221 222

def slapos_conf_check (config):
  """
  Check if slapos.cfg look good
  """
  # Define logger for slapos.cfg verification
  logger = logging.getLogger('Checking slapos.cfg file:')
  logger.setLevel(logging.INFO)
  logger.addHandler(ch)
  # Load configuration file
  configuration_file_path = os.path.join (config.slapos_configuration
                                          ,'slapos.cfg')
  configuration_parser = ConfigParser.SafeConfigParser()
  configuration_parser.read(configuration_file_path)
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
223 224 225
  # Get example configuration file
  slapos_cfg_example = get_slapos_conf_example()
  configuration_example_parser = ConfigParser.RawConfigParser()
226
  configuration_example_parser.read(slapos_cfg_example)
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
227
  os.remove(slapos_cfg_example)
228

229 230 231 232 233 234 235 236 237 238
  # Check sections
  mandatory_sections = ["slapos","slapformat","networkcache"]
  for section in mandatory_sections:
    if not configuration_parser.has_section(section):
      logger.critical("No %s section in slapos.cfg" % section)
      mandatory_sections.remove(section)

  if 'networkcache' in mandatory_sections:
    mandatory_sections.remove('networkcache')

Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
239
  # Check if parameters for slapos and slapformat exists
240
  for section in mandatory_sections :
241
    configuration_dict = dict(configuration_parser.items(section))
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
242 243 244
    configuration_example_dict = dict(configuration_example_parser.items(section))
    for key in configuration_example_dict:
      if not key in configuration_dict:
245
        logger.critical("No %s parameter in %s section "
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
246 247 248
                        % (key,section))
      # check if necessary files exist
      elif key in ("key_file","cert_file","certificate_repository_path"):
249 250
        files = configuration_dict[key]
        if not os.path.exists(files) :
251 252
          logger.critical ("%s file for %s parameters does not exist "
                           % (files,key))
253 254
        else :
          logger.info ("%s parameter:%s does exists" % (key,files))
255 256 257 258
      # check if computer id is the same in slapos.cfg and certificate
      if key == "cert_file":
        check_computer_id(logger,configuration_dict["computer_id"],
                          configuration_dict["cert_file"])
259
  # Check networkcache
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
260 261
  check_networkcache(config,logger,configuration_parser,
                     configuration_example_parser)
262 263


264
class CronFile:
265 266 267 268
  """
  Class to analyse each cron line individualy
  """
  def __init__(self):
269 270 271 272 273 274 275 276
    """ Init all values"""
    self.slapformat  = -1
    self.slapgrid_cp = -1
    self.slapgrid_ur = -1
    self.slapgrid_sr = -1
    # cron file from slapos documentation
    self.slapgrid_sr_base = """*/5 * * * * root /opt/slapos/bin/slapgrid-sr --logfile=/opt/slapos/slapgrid-sr.log --pidfile=/opt/slapos/slapgrid-sr.pid /etc/opt/slapos/slapos.cfg >> /opt/slapos/slapgrid-sr.log 2>&1"""
    self.slapgrid_cp_base = """*/5 * * * * root /opt/slapos/bin/slapgrid-cp --logfile=/opt/slapos/slapgrid-cp.log --pidfile=/opt/slapos/slapgrid-cp.pid /etc/opt/slapos/slapos.cfg >> /opt/slapos/slapgrid-cp.log 2>&1"""
277
    self.slapgrid_ur_base = """0 0 * * * root i=20; false; while [ $? != 0 ]; do /opt/slapos/bin/slapgrid-ur --verbose --logfile=/opt/slapos/slapgrid-ur.log --pidfile=/opt/slapos/slapgrid-ur.pid /etc/opt/slapos/slapos.cfg >> /opt/slapos/slapgrid-ur.log 2>&1; sleep $(($i*60)); if [ $i < 20 ]; then let i++; fi; done;"""
278
    self.slapformat_base = """0 0 * * * root /opt/slapos/bin/slapformat --log_file=/opt/slapos/slapformat.log -c /etc/opt/slapos/slapos.cfg >> /opt/slapos/slapformat.log 2>&1"""
279 280 281 282

  def parse(self,cron_line):
    """ Parse cron line and give value to attributes """
    line = cron_line.split()
283
    if "slapformat"  in cron_line :
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
284 285
      self.slapformat  = self.compare(self.slapformat,
                                      self.slapformat_base.split()  , line)
286
    if "slapgrid-ur" in cron_line :
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
287 288
      self.slapgrid_ur = self.compare(self.slapgrid_ur,
                                      self.slapgrid_ur_base.split() , line)
289
    if "slapgrid-cp" in cron_line :
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
290 291
      self.slapgrid_cp = self.compare(self.slapgrid_cp,
                                      self.slapgrid_cp_base.split() , line)
292
    if "slapgrid-sr" in cron_line :
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
293 294
      self.slapgrid_sr = self.compare(self.slapgrid_sr,
                                      self.slapgrid_sr_base.split() , line)
295

Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
296 297 298
  def compare(self,state,reference,cron_line):
    if not state == -1 :
      return 2
299 300 301
    for i in range(0,6):
      if not reference[i] == cron_line[i] :
        return 0
302
    ref = len(set(reference[6:]))
303 304 305
    if not len(set(reference[6:]) & set(cron_line[6:])) == ref :
      return 0
    else: return 1
306

307 308 309 310 311 312 313 314 315 316
  def check(self,logger):
    elements = {"slapformat":self.slapformat,"slapgrid-ur":self.slapgrid_ur,
                "slapgrid-sr":self.slapgrid_sr,"slapgrid-cp":self.slapgrid_cp}
    for key in elements :
      if elements[key] == 0 :
        logger.error("Your line for %s command does not seem right" % key)
      elif elements[key] == -1 :
        logger.error("No line found for %s command" % key)
      elif elements[key] == 1 :
        logger.info("Line for %s command is good" % key)
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
317 318
      elif elements[key] == 2 :
        logger.error("You have a duplicated line for %s command" % key)
319

320 321 322 323 324 325 326 327 328 329


def cron_check (config):
  """
  Check cron file
  """
  # Define logger for cron file verification
  logger = logging.getLogger('Checking slapos-node cron file:')
  logger.setLevel(logging.INFO)
  logger.addHandler(ch)
330 331
  cron = open(config.slapos_cron,"r")
  cron_file = CronFile()
332 333
  for line in cron :
    if "/opt/slapos" in line and not line[0]=="#":
334 335
      cron_file.parse(line)
  cron_file.check(logger)
336

Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
337
def slapos_global_check (config):
338 339 340 341
  """
  Check for main files
  """
  # Define logger for computer chek
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
342 343 344
  logger = logging.getLogger('Checking your computer for SlapOS:')
  logger.setLevel(logging.INFO)
  logger.addHandler(ch)
345
  # checking slapos.cfg
346
  if not os.path.exists(os.path.join(config.slapos_configuration,'slapos.cfg')) :
347
    logger.critical("No slapos.cfg found in slapos configuration directory: %s"
348
                    % config.slapos_configuration )
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
349 350 351
  else :
    logger.info("SlapOS configuration file found")
    slapos_conf_check(config)
352 353 354 355 356
  # checking cron file
  if not os.path.exists(config.slapos_cron) :
    logger.warn("No %s found for cron" % config.slapos_cron)
  else:
    logger.info("Cron file found at %s" %config.slapos_cron)
357
    cron_check(config)
Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

# Class containing all parameters needed for configuration
class Config:
  def setConfig(self, option_dict):
    """
    Set options given by parameters.
    """
    # Set options parameters
    for option, value in option_dict.__dict__.items():
      setattr(self, option, value)
    # Define logger for register
    self.logger = logging.getLogger('slaptest configuration')
    self.logger.setLevel(logging.DEBUG)
    # add ch to logger
    self.logger.addHandler(ch)

374 375 376 377
    if self.verbose :
      ch.setLevel(logging.DEBUG)


Cédric Le Ninivin's avatar
Cédric Le Ninivin committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
  def displayUserConfig(self):
    self.logger.debug ("Slapos.cfg : %s" % self.slapos_configuration)
    self.logger.debug ("slapos cron file: %s" % self.slapos_cron)


def main():
  """Checking computer state to run slapos"""
  usage = "usage: %s [options] " % sys.argv[0]
  # Parse arguments
  config = Config()
  config.setConfig(Parser(usage=usage).check_args())
  config.displayUserConfig()
  slapos_global_check(config)
  sys.exit()


if __name__ == "__main__":
  main()