4404f6f832cc645932a0c76f19a2741e5fb75203
[oom.git] / cloudify-onap / plugins / onap-installation-plugin / k8s_installer / common / helm.py
1 ########
2 # Copyright (c) 2017 GigaSpaces Technologies Ltd. All rights reserved
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 import urllib
17 import tarfile
18 import os
19 import tempfile
20 from git import Repo
21
22 def get_helm_path(url):
23     tarball = _fetch_helm(url)
24     helm_dir = _get_tmp_file_name()
25     _untar_helm_archive(tarball, helm_dir)
26     helm_binary_path = _find_file('helm', helm_dir)
27     return helm_binary_path
28
29
30 def get_apps_root_path(git_url):
31     dst_repo_path = _get_tmp_file_name()
32     Repo.clone_from(git_url, dst_repo_path)
33     apps_root = format(dst_repo_path)
34     return apps_root
35
36 def _fetch_helm(url):
37     dst_tar_path = _get_tmp_file_name()
38
39     file = urllib.URLopener()
40     file.retrieve(url, dst_tar_path)
41
42     return dst_tar_path
43
44 def _untar_helm_archive(tar_path, helm_dir):
45     helm_tar = tarfile.open(tar_path)
46     helm_tar.extractall(helm_dir)
47     helm_tar.close()
48
49
50 def _find_file(filename, base_path):
51     for root, dirs, files in os.walk(base_path):
52         for name in files:
53             if name == filename:
54                 return os.path.abspath(os.path.join(root, name))
55
56     raise Exception('Cannot find helm binary')
57
58
59 def _get_tmp_file_name():
60     return '{}/{}'.format(tempfile._get_default_tempdir(), next(tempfile._get_candidate_names()))
61
62