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
23 import rift
.package
.store
as store
24 import rift
.package
.package
25 import rift
.package
.icon
as icon
27 from .base
import AbstractPackageManagerProxy
30 class UnknownPackageType(Exception):
34 class FileSystemProxy(AbstractPackageManagerProxy
):
35 """Proxy for Filesystem based store.
37 PACKAGE_TYPE_MAP
= {"vnfd": store
.VnfdPackageFilesystemStore
,
38 "nsd": store
.NsdPackageFilesystemStore
}
40 # Refer: https://confluence.riftio.com/display/ATG/Launchpad+package+formats
42 "nsd": ["icons", "ns_config", "scripts", "vnf_config"],
43 "vnfd": ["charms", "cloud_init", "icons", "images", "scripts"]
46 SCHEMA_TO_PERMS
= {'scripts': 0o777}
48 def __init__(self
, loop
, log
):
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
))
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()
65 store
= self
._get
_store
(package_type
)
67 package
= store
._get
_package
_dir
(package_id
)
68 rel_path
= os
.path
.relpath(package
, start
=store
.root_dir
)
70 url
= "https://127.0.0.1:4567/api/package/{}/{}".format(package_type
, rel_path
)
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()
80 return self
.SCHEMA
[package_type
]
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
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
)
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
)
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
)
105 # Insert (by copy) the file in the package location. For icons,
106 # insert also in UI location for UI to pickup
108 package
.insert_file(new_file
, dest_file
, package_path
, mode
=mode
)
110 if package_file_type
== 'icons':
111 icon_extract
= icon
.PackageIconExtractor(self
.log
)
112 icon_extract
.extract_icons(package
)
114 except rift
.package
.package
.PackageAppendError
as e
:
115 self
.log
.exception(e
)
118 self
.log
.debug("File insertion complete at {}".format(dest_file
))
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
)
126 # for files other than README, create the relative package path from the asset type
127 package_path_rel
= package_file_type
+ "/" + package_path \
128 if package_file_type
!= "readme" else package_path
130 # package_path has to be relative, so strip off the starting slash if
131 # provided incorrectly.
132 if package_path_rel
[0] == "/":
133 package_path_rel
= package_path_rel
[1:]
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_rel
)
140 package
.delete_file(dest_file
, package_path_rel
)
142 if package_file_type
== 'icons':
143 ui_icon_path
= os
.path
.join(
144 icon
.PackageIconExtractor
.DEFAULT_INSTALL_DIR
,
147 if os
.path
.exists(ui_icon_path
):
148 icon_file
= os
.path
.join(ui_icon_path
, package_path
)
149 self
.log
.debug("Deleting UI icon file path {}".format(icon_file
))
152 except rift
.package
.package
.PackageAppendError
as e
:
153 self
.log
.exception(e
)