root / snf-cyclades-gtools / synnefo / ganeti / progress_monitor.py @ a8073cf2
History | View | Annotate | Download (3.4 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
# -*- coding: utf-8 -*-
|
3 |
#
|
4 |
# Copyright 2011, 2012 GRNET S.A. All rights reserved.
|
5 |
#
|
6 |
# Redistribution and use in source and binary forms, with or
|
7 |
# without modification, are permitted provided that the following
|
8 |
# conditions are met:
|
9 |
#
|
10 |
# 1. Redistributions of source code must retain the above
|
11 |
# copyright notice, this list of conditions and the following
|
12 |
# disclaimer.
|
13 |
#
|
14 |
# 2. Redistributions in binary form must reproduce the above
|
15 |
# copyright notice, this list of conditions and the following
|
16 |
# disclaimer in the documentation and/or other materials
|
17 |
# provided with the distribution.
|
18 |
#
|
19 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
20 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
21 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
22 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
23 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
24 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
25 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
26 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
27 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
28 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
29 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30 |
# POSSIBILITY OF SUCH DAMAGE.
|
31 |
#
|
32 |
# The views and conclusions contained in the software and
|
33 |
# documentation are those of the authors and should not be
|
34 |
# interpreted as representing official policies, either expressed
|
35 |
# or implied, of GRNET S.A.
|
36 |
#
|
37 |
"""Utility to monitor the progress of image deployment
|
38 |
|
39 |
A small utility that collects various monitoring messages from snf-image and
|
40 |
forwards them to the rest of the Synnefo infrastructure over AMQP.
|
41 |
"""
|
42 |
|
43 |
import os |
44 |
import sys |
45 |
import time |
46 |
import json |
47 |
|
48 |
from synnefo import settings |
49 |
from synnefo.lib.amqp import AMQPClient |
50 |
from synnefo.lib.utils import split_time |
51 |
|
52 |
PROGNAME = os.path.basename(sys.argv[0])
|
53 |
|
54 |
|
55 |
def jsonstream(file): |
56 |
buf = ""
|
57 |
decoder = json.JSONDecoder() |
58 |
while True: |
59 |
new_data = os.read(file.fileno(), 512) |
60 |
if not len(new_data): |
61 |
break
|
62 |
|
63 |
buf += new_data.strip() |
64 |
while 1: |
65 |
try:
|
66 |
msg, idx = decoder.raw_decode(buf) |
67 |
except ValueError: |
68 |
break
|
69 |
yield msg
|
70 |
buf = buf[idx:].strip() |
71 |
|
72 |
|
73 |
def main(): |
74 |
|
75 |
usage = "Usage: %s <instance_name>\n" % PROGNAME
|
76 |
|
77 |
if len(sys.argv) != 2: |
78 |
sys.stderr.write(usage) |
79 |
return 1 |
80 |
|
81 |
instance_name = sys.argv[1]
|
82 |
|
83 |
# WARNING: This assumes that instance names
|
84 |
# are of the form prefix-id, and uses prefix to
|
85 |
# determine the routekey for AMPQ
|
86 |
prefix = instance_name.split('-')[0] |
87 |
routekey = "ganeti.%s.event.progress" % prefix
|
88 |
amqp_client = AMQPClient() |
89 |
amqp_client.connect(hosts=settings.AMQP_HOSTS, confirm_buffer=10)
|
90 |
amqp_client.exchange_declare(settings.EXCHANGE_GANETI, "topic")
|
91 |
|
92 |
for msg in jsonstream(sys.stdin): |
93 |
msg['event_time'] = split_time(time.time())
|
94 |
msg['instance'] = instance_name
|
95 |
|
96 |
# and send it over AMQP
|
97 |
amqp_client.basic_publish(exchange=settings.EXCHANGE_GANETI, |
98 |
routing_key=routekey, |
99 |
body=json.dumps(msg)) |
100 |
|
101 |
amqp_client.close() |
102 |
return 0 |
103 |
|
104 |
if __name__ == "__main__": |
105 |
sys.exit(main()) |
106 |
|
107 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|