2 # Copyright 2016 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): Varun Prasad
17 # Creation Date: 09/25/2016
24 from gi
.repository
import (
28 import rift
.mano
.dts
as mano_dts
30 from . import downloader
as pkg_downloader
33 RPC_PKG_ENDPOINT
= RwPkgMgmtYang
.YangOutput_RwPkgMgmt_GetPackageEndpoint
34 RPC_SCHEMA_ENDPOINT
= RwPkgMgmtYang
.YangOutput_RwPkgMgmt_GetPackageSchema
35 RPC_PACKAGE_ADD_ENDPOINT
= RwPkgMgmtYang
.YangOutput_RwPkgMgmt_PackageFileAdd
36 RPC_PACKAGE_DELETE_ENDPOINT
= RwPkgMgmtYang
.YangOutput_RwPkgMgmt_PackageFileDelete
39 class EndpointDiscoveryRpcHandler(mano_dts
.AbstractRpcHandler
):
40 """RPC handler to generate the endpoint for the Package manager."""
42 def __init__(self
, log
, dts
, loop
, proxy
):
45 proxy: Any impl of .proxy.AbstractPackageManagerProxy
47 super().__init
__(log
, dts
, loop
)
52 return "/rw-pkg-mgmt:get-package-endpoint"
55 def callback(self
, ks_path
, msg
):
56 """Forwards the request to proxy.
58 url
= yield from self
.proxy
.endpoint(
62 rpc_op
= RPC_PKG_ENDPOINT
.from_dict({"endpoint": url
})
67 class SchemaRpcHandler(mano_dts
.AbstractRpcHandler
):
68 """RPC handler to generate the schema for the packages.
70 def __init__(self
, log
, dts
, loop
, proxy
):
73 proxy: Any impl of .proxy.AbstractPackageManagerProxy
75 super().__init
__(log
, dts
, loop
)
80 return "/rw-pkg-mgmt:get-package-schema"
83 def callback(self
, ks_path
, msg
):
85 package_type
= msg
.package_type
.lower()
86 schema
= yield from self
.proxy
.schema(msg
.package_type
)
88 rpc_op
= RPC_SCHEMA_ENDPOINT()
89 for dirname
in schema
:
90 rpc_op
.schema
.append(dirname
)
95 class PackageOperationsRpcHandler(mano_dts
.AbstractRpcHandler
):
99 1. For a request, we schedule a download in the background
100 2. We register the downloader to a publisher to push out the download status
101 Note: The publisher starts the download automatically.
102 3. Return a tracking ID for the client to monitor the entire status
105 def __init__(self
, log
, dts
, loop
, proxy
, publisher
):
108 proxy: Any impl of .proxy.AbstractPackageManagerProxy
109 publisher: Instance of DownloadStatusPublisher
111 super().__init
__(log
, dts
, loop
)
113 self
.publisher
= publisher
117 return "/rw-pkg-mgmt:package-file-add"
120 def callback(self
, ks_path
, msg
):
121 if not msg
.external_url
:
122 # For now we will only support External URL download
123 raise Exception ("No download URL provided")
125 # Create a tmp file to download the url
126 # We first store the data in temp and post download finish
127 # we move the file to actual location.
128 _
, filename
= tempfile
.mkstemp()
131 if msg
.username
is not None:
132 auth
= (msg
.username
, msg
.password
)
134 url_downloader
= pkg_downloader
.PackageFileDownloader
.from_rpc_input(
141 download_id
= yield from self
.publisher
.register_downloader(url_downloader
)
143 rpc_op
= RPC_PACKAGE_ADD_ENDPOINT
.from_dict({"task_id": download_id
})
148 class PackageDeleteOperationsRpcHandler(mano_dts
.AbstractRpcHandler
):
149 def __init__(self
, log
, dts
, loop
, proxy
):
152 proxy: Any impl of .proxy.AbstractPackageManagerProxy
154 super().__init
__(log
, dts
, loop
)
159 return "/rw-pkg-mgmt:package-file-delete"
162 def callback(self
, ks_path
, msg
):
164 rpc_op
= RPC_PACKAGE_DELETE_ENDPOINT
.from_dict({"status": str(True)})
167 self
.proxy
.package_file_delete(
171 except Exception as e
:
172 self
.log
.exception(e
)
173 rpc_op
.status
= str(False)
174 rpc_op
.error_trace
= str(e
)