[RIFT 16087] Backend changes to decouple storage semantics from user interface. Chang...
[osm/SO.git] / rwlaunchpad / plugins / rwpkgmgr / rift / tasklets / rwpkgmgr / proxy / filesystem.py
1 #
2 # Copyright 2016 RIFT.IO Inc
3 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 #
16 # Author(s): Varun Prasad
17 # Creation Date: 09/25/2016
18 #
19
20 import asyncio
21 import os
22
23 import rift.package.store as store
24 import rift.package.package
25
26 from .base import AbstractPackageManagerProxy
27
28
29 class UnknownPackageType(Exception):
30 pass
31
32
33 class FileSystemProxy(AbstractPackageManagerProxy):
34 """Proxy for Filesystem based store.
35 """
36 PACKAGE_TYPE_MAP = {"vnfd": store.VnfdPackageFilesystemStore,
37 "nsd": store.NsdPackageFilesystemStore}
38
39 # Refer: https://confluence.riftio.com/display/ATG/Launchpad+package+formats
40 SCHEMA = {
41 "nsd": ["icons", "ns_config", "scripts", "vnf_config"],
42 "vnfd": ["charms", "cloud_init", "icons", "images", "scripts"]
43 }
44
45 SCHEMA_TO_PERMS = {'scripts': 0o777}
46
47 def __init__(self, loop, log):
48 self.loop = loop
49 self.log = log
50 self.store_cache = {}
51
52 def _get_store(self, package_type):
53 store_cls = self.PACKAGE_TYPE_MAP[package_type]
54 store = self.store_cache.setdefault(package_type, store_cls(self.log))
55
56 return store
57
58 @asyncio.coroutine
59 def endpoint(self, package_type, package_id):
60 package_type = package_type.lower()
61 if package_type not in self.PACKAGE_TYPE_MAP:
62 raise UnknownPackageType()
63
64 store = self._get_store(package_type)
65
66 package = store._get_package_dir(package_id)
67 rel_path = os.path.relpath(package, start=store.root_dir)
68
69 url = "https://127.0.0.1:4567/api/package/{}/{}".format(package_type, rel_path)
70
71 return url
72
73 @asyncio.coroutine
74 def schema(self, package_type):
75 package_type = package_type.lower()
76 if package_type not in self.PACKAGE_TYPE_MAP:
77 raise UnknownPackageType()
78
79 return self.SCHEMA[package_type]
80
81 def package_file_add(self, new_file, package_type, package_id, package_path, package_file_type):
82 # Get the schema from thr package path
83 # the first part will always be the vnfd/nsd name
84 mode = 0o664
85
86 # for files other than README, create the package path from the asset type
87 package_path = package_file_type + "/" + package_path \
88 if package_file_type != "readme" else package_path
89 components = package_path.split("/")
90 if len(components) > 2:
91 schema = components[1]
92 mode = self.SCHEMA_TO_PERMS.get(schema, mode)
93
94 # Fetch the package object
95 package_type = package_type.lower()
96 store = self._get_store(package_type)
97 package = store.get_package(package_id)
98
99 # Construct abs path of the destination obj
100 path = store._get_package_dir(package_id)
101 dest_file = os.path.join(path, package.prefix, package_path)
102
103 try:
104 package.insert_file(new_file, dest_file, package_path, mode=mode)
105 except rift.package.package.PackageAppendError as e:
106 self.log.exception(e)
107 return False
108
109 return True
110
111 def package_file_delete(self, package_type, package_id, package_path, package_file_type):
112 package_type = package_type.lower()
113 store = self._get_store(package_type)
114 package = store.get_package(package_id)
115
116 # for files other than README, create the package path from the asset type
117 package_path = package_file_type + "/" + package_path \
118 if package_file_type != "readme" else package_path
119
120 # package_path has to be relative, so strip off the starting slash if
121 # provided incorrectly.
122 if package_path[0] == "/":
123 package_path = package_path[1:]
124
125 # Construct abs path of the destination obj
126 path = store._get_package_dir(package_id)
127 dest_file = os.path.join(path, package.prefix, package_path)
128
129 try:
130 package.delete_file(dest_file, package_path)
131 except rift.package.package.PackageAppendError as e:
132 self.log.exception(e)
133 return False
134
135 return True
136