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
, tasklet
):
108 proxy: Any impl of .proxy.AbstractPackageManagerProxy
109 publisher: Instance of tasklet to find the DownloadStatusPublisher
110 for a specific project
112 super().__init
__(log
, dts
, loop
)
114 self
.tasklet
= tasklet
118 return "/rw-pkg-mgmt:package-file-add"
120 def get_publisher(self
, msg
):
122 proj
= self
.tasklet
.projects
[msg
.project_name
]
123 except Exception as e
:
124 err
= "Project or project name not found {}: {}". \
125 format(msg
.as_dict(), e
)
127 raise Exception (err
)
129 return proj
.job_handler
132 def callback(self
, ks_path
, msg
):
133 publisher
= self
.get_publisher(msg
)
135 if not msg
.external_url
:
136 # For now we will only support External URL download
137 raise Exception ("No download URL provided")
139 # Create a tmp file to download the url
140 # We first store the data in temp and post download finish
141 # we move the file to actual location.
142 _
, filename
= tempfile
.mkstemp()
145 if msg
.username
is not None:
146 auth
= (msg
.username
, msg
.password
)
148 url_downloader
= pkg_downloader
.PackageFileDownloader
.from_rpc_input(
155 download_id
= yield from publisher
.register_downloader(url_downloader
)
157 rpc_op
= RPC_PACKAGE_ADD_ENDPOINT
.from_dict({"task_id": download_id
})
162 class PackageDeleteOperationsRpcHandler(mano_dts
.AbstractRpcHandler
):
163 def __init__(self
, log
, dts
, loop
, proxy
):
166 proxy: Any impl of .proxy.AbstractPackageManagerProxy
168 super().__init
__(log
, dts
, loop
)
173 return "/rw-pkg-mgmt:package-file-delete"
176 def callback(self
, ks_path
, msg
):
178 rpc_op
= RPC_PACKAGE_DELETE_ENDPOINT
.from_dict({"status": str(True)})
181 self
.proxy
.package_file_delete(
185 except Exception as e
:
186 self
.log
.exception(e
)
187 rpc_op
.status
= str(False)
188 rpc_op
.error_trace
= str(e
)