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
):
82 # Get the schema from thr package path
83 # the first part will always be the vnfd/nsd name
85 components
= package_path
.split("/")
86 if len(components
) > 2:
87 schema
= components
[1]
88 mode
= self
.SCHEMA_TO_PERMS
.get(schema
, mode
)
90 # Fetch the package object
91 package_type
= package_type
.lower()
92 store
= self
._get
_store
(package_type
)
93 package
= store
.get_package(package_id
)
95 # Construct abs path of the destination obj
96 path
= store
._get
_package
_dir
(package_id
)
97 dest_file
= os
.path
.join(path
, package_path
)
100 package
.insert_file(new_file
, dest_file
, package_path
, mode
=mode
)
101 except rift
.package
.package
.PackageAppendError
as e
:
102 self
.log
.exception(e
)
107 def package_file_delete(self
, package_type
, package_id
, package_path
):
108 package_type
= package_type
.lower()
109 store
= self
._get
_store
(package_type
)
110 package
= store
.get_package(package_id
)
112 # package_path has to be relative, so strip off the starting slash if
113 # provided incorrectly.
114 if package_path
[0] == "/":
115 package_path
= package_path
[1:]
117 # Construct abs path of the destination obj
118 path
= store
._get
_package
_dir
(package_id
)
119 dest_file
= os
.path
.join(path
, package_path
)
122 package
.delete_file(dest_file
, package_path
)
123 except rift
.package
.package
.PackageAppendError
as e
:
124 self
.log
.exception(e
)