RIFT 16526 Copy the icon image also to the UI logos location for UI update.
[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 import rift.package.icon as icon
26
27 from .base import AbstractPackageManagerProxy
28
29
30 class UnknownPackageType(Exception):
31 pass
32
33
34 class FileSystemProxy(AbstractPackageManagerProxy):
35 """Proxy for Filesystem based store.
36 """
37 PACKAGE_TYPE_MAP = {"vnfd": store.VnfdPackageFilesystemStore,
38 "nsd": store.NsdPackageFilesystemStore}
39
40 # Refer: https://confluence.riftio.com/display/ATG/Launchpad+package+formats
41 SCHEMA = {
42 "nsd": ["icons", "ns_config", "scripts", "vnf_config"],
43 "vnfd": ["charms", "cloud_init", "icons", "images", "scripts"]
44 }
45
46 SCHEMA_TO_PERMS = {'scripts': 0o777}
47
48 def __init__(self, loop, log):
49 self.loop = loop
50 self.log = log
51 self.store_cache = {}
52
53 def _get_store(self, package_type):
54 store_cls = self.PACKAGE_TYPE_MAP[package_type]
55 store = self.store_cache.setdefault(package_type, store_cls(self.log))
56
57 return store
58
59 @asyncio.coroutine
60 def endpoint(self, package_type, package_id):
61 package_type = package_type.lower()
62 if package_type not in self.PACKAGE_TYPE_MAP:
63 raise UnknownPackageType()
64
65 store = self._get_store(package_type)
66
67 package = store._get_package_dir(package_id)
68 rel_path = os.path.relpath(package, start=store.root_dir)
69
70 url = "https://127.0.0.1:4567/api/package/{}/{}".format(package_type, rel_path)
71
72 return url
73
74 @asyncio.coroutine
75 def schema(self, package_type):
76 package_type = package_type.lower()
77 if package_type not in self.PACKAGE_TYPE_MAP:
78 raise UnknownPackageType()
79
80 return self.SCHEMA[package_type]
81
82 def package_file_add(self, new_file, package_type, package_id, package_path, package_file_type):
83 # Get the schema from thr package path
84 # the first part will always be the vnfd/nsd name
85 mode = 0o664
86
87 # for files other than README, create the package path from the asset type, e.g. icons/icon1.png
88 # for README files, strip off any leading '/'
89 package_path = package_file_type + "/" + package_path \
90 if package_file_type != "readme" else package_path.strip('/')
91 components = package_path.split("/")
92 if len(components) > 2:
93 schema = components[1]
94 mode = self.SCHEMA_TO_PERMS.get(schema, mode)
95
96 # Fetch the package object
97 package_type = package_type.lower()
98 store = self._get_store(package_type)
99 package = store.get_package(package_id)
100
101 # Construct abs path of the destination obj
102 path = store._get_package_dir(package_id)
103 dest_file = os.path.join(path, package.prefix, package_path)
104
105 # Insert (by copy) the file in the package location. For icons,
106 # insert also in UI location for UI to pickup
107 try:
108 package.insert_file(new_file, dest_file, package_path, mode=mode)
109
110 if package_file_type == 'icons':
111 icon_extract = icon.PackageIconExtractor(self.log)
112 icon_extract.extract_icons(package)
113
114 except rift.package.package.PackageAppendError as e:
115 self.log.exception(e)
116 return False
117
118 self.log.debug("File insertion complete at {}".format(dest_file))
119 return True
120
121 def package_file_delete(self, package_type, package_id, package_path, package_file_type):
122 package_type = package_type.lower()
123 store = self._get_store(package_type)
124 package = store.get_package(package_id)
125
126 # for files other than README, create the package path from the asset type
127 package_path = package_file_type + "/" + package_path \
128 if package_file_type != "readme" else package_path
129
130 # package_path has to be relative, so strip off the starting slash if
131 # provided incorrectly.
132 if package_path[0] == "/":
133 package_path = package_path[1:]
134
135 # Construct abs path of the destination obj
136 path = store._get_package_dir(package_id)
137 dest_file = os.path.join(path, package.prefix, package_path)
138
139 try:
140 package.delete_file(dest_file, package_path)
141 except rift.package.package.PackageAppendError as e:
142 self.log.exception(e)
143 return False
144
145 return True
146