2acfed1bdb79b98b9f07caa043e4e53cc4fe0316
[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, e.g. icons/icon1.png
87 # for README files, strip off any leading '/'
88 package_path = package_file_type + "/" + package_path \
89 if package_file_type != "readme" else package_path.strip('/')
90 components = package_path.split("/")
91 if len(components) > 2:
92 schema = components[1]
93 mode = self.SCHEMA_TO_PERMS.get(schema, mode)
94
95 # Fetch the package object
96 package_type = package_type.lower()
97 store = self._get_store(package_type)
98 package = store.get_package(package_id)
99
100 # Construct abs path of the destination obj
101 path = store._get_package_dir(package_id)
102 dest_file = os.path.join(path, package.prefix, package_path)
103
104 try:
105 package.insert_file(new_file, dest_file, package_path, mode=mode)
106 except rift.package.package.PackageAppendError as e:
107 self.log.exception(e)
108 return False
109
110 self.log.debug("File insertion complete at {}".format(dest_file))
111 return True
112
113 def package_file_delete(self, package_type, package_id, package_path, package_file_type):
114 package_type = package_type.lower()
115 store = self._get_store(package_type)
116 package = store.get_package(package_id)
117
118 # for files other than README, create the package path from the asset type
119 package_path = package_file_type + "/" + package_path \
120 if package_file_type != "readme" else package_path
121
122 # package_path has to be relative, so strip off the starting slash if
123 # provided incorrectly.
124 if package_path[0] == "/":
125 package_path = package_path[1:]
126
127 # Construct abs path of the destination obj
128 path = store._get_package_dir(package_id)
129 dest_file = os.path.join(path, package.prefix, package_path)
130
131 try:
132 package.delete_file(dest_file, package_path)
133 except rift.package.package.PackageAppendError as e:
134 self.log.exception(e)
135 return False
136
137 return True
138