927331c1eeff33cda37d49b6571be8cbb3c5fe5d
2 # Copyright 2017 RIFT.IO Inc
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # Author(s): Nandan Sinha
24 from concurrent
.futures
import Future
26 from gi
.repository
import (RwDts
as rwdts
)
27 import rift
.mano
.dts
as mano_dts
28 import rift
.downloader
as url_downloader
29 import rift
.tasklets
.rwlaunchpad
.onboard
as onboard
31 if sys
.version_info
< (3, 4, 4):
32 asyncio
.ensure_future
= asyncio
.async
35 class CopyStatusPublisher(mano_dts
.DtsHandler
, url_downloader
.DownloaderProtocol
):
37 def __init__(self
, log
, dts
, loop
, tasklet_info
):
38 super().__init
__(log
, dts
, loop
)
40 self
.tasklet_info
= tasklet_info
42 def xpath(self
, transaction_id
=None):
43 return ("D,/rw-pkg-mgmt:copy-jobs/rw-pkg-mgmt:job" +
44 ("[transaction-id='{}']".format(transaction_id
) if transaction_id
else ""))
49 self
.reg
= yield from self
.dts
.register(xpath
=self
.xpath(),
50 flags
=rwdts
.Flag
.PUBLISHER|rwdts
.Flag
.CACHE|rwdts
.Flag
.NO_PREP_READ
)
52 assert self
.reg
is not None
55 def register_copier(self
, copier
):
56 copier
.delegate
= self
57 future
= self
.loop
.run_in_executor(None, copier
.copy
)
58 self
.tasks
[copier
.transaction_id
] = (copier
, future
)
60 return (copier
.transaction_id
, copier
.dest_package_id
)
63 def _dts_publisher(self
, job_msg
):
64 # Publish the download state
65 self
.reg
.update_element(
66 self
.xpath(transaction_id
=job_msg
.transaction_id
), job_msg
)
69 def _async_add(func
, fut
):
73 except Exception as e
:
76 def _schedule_dts_work(self
, job_msg
):
77 f
= functools
.partial(
78 asyncio
.ensure_future
,
79 self
._dts
_publisher
(job_msg
),
82 self
.loop
.call_soon_threadsafe(CopyStatusPublisher
._async
_add
, f
, fut
)
84 if fut
.exception() is not None:
85 self
.log
.error("Caught future exception during download: %s type %s", str(fut
.exception()), type(fut
.exception()))
89 def on_download_progress(self
, job_msg
):
90 """callback that triggers update.
92 return self
._schedule
_dts
_work
(job_msg
)
94 def on_download_finished(self
, job_msg
):
95 """callback that triggers update.
97 # clean up the local cache
98 key
= job_msg
.transaction_id
102 return self
._schedule
_dts
_work
(job_msg
)
104 def on_download_succeeded(self
, job_msg
):
105 """Post the catalog descriptor object to the http endpoint.
106 Argument: job_msg (proto-gi descriptor_msg of the copied descriptor)
109 manifest
= self
.tasklet_info
.get_pb_manifest()
110 use_ssl
= manifest
.bootstrap_phase
.rwsecurity
.use_ssl
111 ssl_cert
, ssl_key
= None, None
113 ssl_cert
= manifest
.bootstrap_phase
.rwsecurity
.cert
114 ssl_key
= manifest
.bootstrap_phase
.rwsecurity
.key
116 onboarder
= onboard
.DescriptorOnboarder(self
.log
,
117 "127.0.0.1", 8008, use_ssl
, ssl_cert
, ssl_key
)
119 onboarder
.onboard(job_msg
)
120 except onboard
.OnboardError
as e
:
121 self
.log
.error("Onboard exception triggered while posting copied catalog descriptor %s", e
)