__init__.py 2.37 KB
Newer Older
1 2
# -*- coding: utf-8 -*-

3
from __future__ import print_function
4
from six.moves import configparser
5
import argparse
6
from six.moves import dbm_gnu as gdbm
7 8 9

import sys
import os
10
import logging
11

12
from . import process
13 14

def main():
15 16 17
    parser = argparse.ArgumentParser(description="Slapcontainer binary")
    parser.add_argument('configuration_file', type=str,
                        help="SlapOS configuration file.")
18 19
    parser.add_argument('database', type=str,
                        help='slapcontainer database')
20 21 22 23
    log_lvls = [lvl for lvl in logging._levelNames.keys()
                if isinstance(lvl, basestring)]
    parser.add_argument('--log', nargs=1, default=['INFO'],
                        choices=log_lvls,
24 25
                        metavar='lvl', help='Log level')
    parser.add_argument('--pid', nargs=1, help='pid file path')
26 27
    args = parser.parse_args()

28 29 30
    if args.pid is not None:
        pid_filename = args.pid[0]
        if os.path.exists(pid_filename):
31
            print("Already running", file=sys.stderr)
32 33 34 35 36 37 38 39 40 41 42 43 44
            return 127
        with open(pid_filename, 'w') as pid_file:
            pid_file.write(str(os.getpid()))

        try:
            run(args)
        finally:
            os.remove(pid_filename)

    else:
        run(args)

def run(args):
45
    slapos_conf = configparser.ConfigParser()
46
    slapos_conf.read(args.configuration_file)
47 48 49 50 51 52 53

    current_binary = os.path.join(os.getcwd(), sys.argv[0])
    binary_directory = os.path.dirname(current_binary)
    sr_directory = os.path.realpath(os.path.join(binary_directory, '..'))

    partition_amount = slapos_conf.getint('slapformat', 'partition_amount')
    partition_base_name = slapos_conf.get('slapformat', 'partition_base_name')
54 55
    try:
        bridge_name = slapos_conf.get('slapformat', 'interface_name')
56
    except configparser.NoOptionError:
57
        bridge_name = slapos_conf.get('slapformat', 'bridge_name')
58 59 60 61 62
    instance_root = slapos_conf.get('slapos', 'instance_root')
    partition_base_path = os.path.join(instance_root, partition_base_name)
    partition_list = ['%s%d' % (partition_base_path, i)
                      for i in range(partition_amount)]

63 64
    logging.basicConfig(level=logging.getLevelName(args.log[0]))

65
    database = gdbm.open(args.database, 'c', 0o600)
66
    try:
67
        process.main(sr_directory, partition_list, database, bridge_name)
68 69 70
    finally:
        database.sync()
        database.close()