Detailed logs on Onboarding Failure
[osm/SO.git] / rwlaunchpad / plugins / rwlaunchpadtasklet / rift / tasklets / rwlaunchpad / image.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16 #
17
18 import asyncio
19 import itertools
20 import glanceclient
21
22 import gi
23 gi.require_version('RwcalYang', '1.0')
24
25 from rift.imagemgr import client
26
27
28 class ImageUploadError(Exception):
29 pass
30
31
32 class ImageUploader(object):
33 """ This class is responsible for uploading package images to cloud accounts """
34 def __init__(self, log, loop, dts):
35 """ Create an instance of ImageUploader
36
37 Arguments:
38 log - A logger
39 """
40 self._log = log
41 self._loop = loop
42 self._dts = dts
43
44 self._client = client.UploadJobClient(self._log, self._loop, self._dts)
45
46 def upload_image(self, image_name, image_checksum, image_hdl):
47 endpoint = "http://127.0.0.1:9292"
48 glance_client = glanceclient.Client('1', endpoint, token="asdf")
49
50 try:
51 for image in itertools.chain(
52 glance_client.images.list(is_public=False),
53 glance_client.images.list(is_public=True),
54 ):
55 if image.name == image_name and image_checksum == image_checksum:
56 self._log.debug("Found existing image in catalog, not re-uploading")
57 return
58
59 self._log.debug('Uploading image to catalog: {}'.format(image_name))
60
61 image = glance_client.images.create(name=image_name, data=image_hdl, is_public="False",
62 disk_format="qcow2", container_format="bare",
63 checksum=image_checksum)
64 self._log.debug('Image upload complete: %s', image)
65 except Exception as e:
66 raise ImageUploadError("Failed to upload image to catalog: %s" % str(e)) from e
67
68 def upload_image_to_cloud_accounts(self, image_name, image_checksum, cloud_accounts=None):
69 self._log.debug("uploading image %s to all cloud accounts", image_name)
70 upload_job = self._client.create_job_threadsafe(image_name, image_checksum, cloud_accounts)
71 try:
72 upload_job.wait_until_complete_threadsafe()
73 except client.UploadJobError as e:
74 raise ImageUploadError("Failed to upload image " + image_name + " to cloud accounts") from e
75