3 # Copyright 2016 RIFT.IO Inc
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.
23 gi
.require_version('RwcalYang', '1.0')
25 from rift
.imagemgr
import client
28 class ImageUploadError(Exception):
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
44 self
._client
= client
.UploadJobClient(self
._log
, self
._loop
, self
._dts
)
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")
51 for image
in itertools
.chain(
52 glance_client
.images
.list(is_public
=False),
53 glance_client
.images
.list(is_public
=True),
55 if image
.name
== image_name
and image_checksum
== image_checksum
:
56 self
._log
.debug("Found existing image in catalog, not re-uploading")
59 self
._log
.debug('Uploading image to catalog: {}'.format(image_name
))
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
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
)
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