~joetalbott/+junk/fix_tmpdir

« back to all changes in this revision

Viewing changes to core_image_builder/__init__.py

  • Committer: Joe Talbott
  • Date: 2015-05-19 21:37:40 UTC
  • Revision ID: joe.talbott@canonical.com-20150519213740-48l0u6bmecbyg9mt
Add core-image-publisher code with name changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# core-image-builder
 
2
# Copyright (C) 2015 Canonical
 
3
#
 
4
# This program is free software: you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation, either version 3 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
#
 
17
 
 
18
import argparse
 
19
import configparser
 
20
import logging
 
21
import os
 
22
 
 
23
from uservice_utils.logging import configure_service_logging
 
24
from uservice_utils.queue import (
 
25
    SimpleRabbitQueueWorker,
 
26
    DefaultRetryPolicy,
 
27
)
 
28
 
 
29
from core_image_builder.queue import (
 
30
    create_connection_from_config,
 
31
    CoreImageResultBuilder,
 
32
)
 
33
from core_image_builder import (
 
34
    constants,
 
35
    worker,
 
36
)
 
37
 
 
38
 
 
39
def read_config():
 
40
    parser = argparse.ArgumentParser(description='Core image builder.')
 
41
    parser.add_argument(
 
42
        '-c',
 
43
        '--conf',
 
44
        default='core-service.conf',
 
45
        help='Configuration file path'
 
46
    )
 
47
    args = parser.parse_args()
 
48
 
 
49
    # Load configuration options.
 
50
    config = configparser.ConfigParser()
 
51
    config.read(args.conf)
 
52
    return config
 
53
 
 
54
 
 
55
def main():
 
56
    config = read_config()
 
57
    log_path = os.path.abspath(
 
58
        os.path.join(__file__, '../../logs/core-image-builder.log')
 
59
    )
 
60
    configure_service_logging(
 
61
        log_path,
 
62
        config['logstash'] if 'logstash' in config else None
 
63
    )
 
64
    logger = logging.getLogger(__name__)
 
65
    logger.info('Started!', extra=constants.LOGGING_EXTRA)
 
66
 
 
67
    connection = create_connection_from_config(config)
 
68
    try:
 
69
        with connection:
 
70
            builder = CoreImageResultBuilder(connection)
 
71
            retry_policy = DefaultRetryPolicy(
 
72
                max_retries=3,
 
73
                dead_queue='core.deadletters.{}'.format(constants.API_VERSION)
 
74
            )
 
75
            monitor = SimpleRabbitQueueWorker(
 
76
                connection,
 
77
                'core.image.{}'.format(constants.API_VERSION),
 
78
                worker.ImageBuilderWorker(config, builder),
 
79
                retry_policy,
 
80
            )
 
81
            monitor.run()
 
82
    except KeyboardInterrupt:
 
83
        print("Bye!")