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
26 from .base
import AbstractPackageManagerProxy
29 class UnknownPackageType(Exception):
33 class FileSystemProxy(AbstractPackageManagerProxy
):
34 """Proxy for Filesystem based store.
36 PACKAGE_TYPE_MAP
= {"vnfd": store
.VnfdPackageFilesystemStore
,
37 "nsd": store
.NsdPackageFilesystemStore
}
39 # Refer: https://confluence.riftio.com/display/ATG/Launchpad+package+formats
41 "nsd": ["icons", "ns_config", "scripts", "vnf_config"],
42 "vnfd": ["charms", "cloud_init", "icons", "images", "scripts"]
45 SCHEMA_TO_PERMS
= {'scripts': 0o777}
47 def __init__(self
, loop
, log
):
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
))
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()
64 store
= self
._get
_store
(package_type
)
66 package
= store
._get
_package
_dir
(package_id
)
67 rel_path
= os
.path
.relpath(package
, start
=store
.root_dir
)
69 url
= "https://127.0.0.1:4567/api/package/{}/{}".format(package_type
, rel_path
)
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()
79 return self
.SCHEMA
[package_type
]
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
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
)
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
)
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
)
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
)
110 self
.log
.debug("File insertion complete at {}".format(dest_file
))
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
)
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
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:]
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
)
132 package
.delete_file(dest_file
, package_path
)
133 except rift
.package
.package
.PackageAppendError
as e
:
134 self
.log
.exception(e
)