5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 from __future__ import print_function
24 class ProgressReporter(object):
25 CALLBACK_URL_PATH = '/etc/userconfig/callback_url'
26 DEFAULT_CLIENT_CERT = '/etc/userconfig/clientcert.pem'
27 DEFAULT_CLIENT_KEY = '/etc/userconfig/clientkey.pem'
30 self._prog = 'progress-reporter'
31 self._callback_url = None
34 self._percentage = None
35 self._description = None
36 self._client_cert_path = ProgressReporter.DEFAULT_CLIENT_CERT
37 self._client_key_path = ProgressReporter.DEFAULT_CLIENT_KEY
39 def __call__(self, args):
40 parser = argparse.ArgumentParser(description='Progress Reporter',
43 parser.add_argument('--callback-url',
45 metavar='CALLBACK-URL',
47 help='The URL for the report callback',
50 parser.add_argument('--client-cert-path',
51 dest='client_cert_path',
52 metavar='CLIENT-CERT-PATH',
54 help='The path to client cert file',
57 parser.add_argument('--client-key-path',
58 dest='client_key_path',
59 metavar='CLIENT-KEY-PATH',
61 help='The path to client key file',
64 parser.add_argument('--status',
68 help='The status of the progress, should be: ongoing or failed or success',
71 parser.add_argument('--description',
73 metavar='DESCRIPTION',
75 help='The description of the progress',
78 parser.add_argument('--percentage',
82 help='The percentage of the progress',
85 parser.add_argument('--verbose',
90 args = parser.parse_args(args)
94 def _set_callback_url(self):
96 with open(ProgressReporter.CALLBACK_URL_PATH) as cf:
97 self._callback_url = cf.readline().strip()
99 logging.debug('No callback URL found')
101 def _send_progress(self):
102 if self._callback_url:
103 request_data = {'status': self._status, 'description': self._description}
105 request_data['percentage'] = self._percentage
108 if self._client_cert_path:
109 cert_tuple = (self._client_cert_path, self._client_key_path)
113 response = requests.post(self._callback_url, json=request_data, cert=cert_tuple, verify=False)
114 if response.status_code != requests.codes.ok:
115 logging.debug('Failed to send progress: %s (%s)', str(response.reason), str(response.status_code))
116 raise Exception('Failed')
117 except Exception as ex:
118 logging.debug('Failed to send progress: %s', str(ex))
119 raise Exception('Failed')
123 def process(self, args):
124 log_level = logging.INFO
126 log_level = logging.DEBUG
128 logging.basicConfig(level=log_level, format='%(asctime)s %(message)s')
130 self._callback_url = args.callback_url
131 if not self._callback_url:
132 self._set_callback_url()
134 if args.client_cert_path:
135 self._client_cert_path = args.client_cert_path
137 if args.client_key_path:
138 self._client_key_path = args.client_key_path
140 self._verbose = args.verbose
141 self._status = args.status
142 self._percentage = args.percentage
143 self._description = args.description
145 logging.debug('callback_url = %s', self._callback_url)
146 logging.debug('client_cert_path = %s', self._client_cert_path)
147 logging.debug('client_key_path = %s', self._client_key_path)
148 logging.info('status=%s, percentage=%s, description=%s', self._status, self._percentage, self._description)
150 self._send_progress()
154 reporter = ProgressReporter()
157 except Exception as exp:
158 logging.error('Failed with error: %s', str(exp))
162 if __name__ == '__main__':