vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / cli / service_template_utils.py
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License.  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 """
17 Loading mechanism for service templates.
18 """
19
20 import os
21 from urlparse import urlparse
22
23 from . import csar
24 from . import utils
25 from .exceptions import AriaCliError
26 from ..utils import archive as archive_utils
27
28
29 def get(source, service_template_filename):
30     """
31     Get a source and return a path to the main service template file
32
33     The behavior based on then source argument content is:
34
35     * local ``.yaml`` file: return the file
36     * local archive (``.csar``, ``.zip``, ``.tar``, ``.tar.gz``, and ``.tar.bz2``): extract it
37       locally and return path service template file
38     * URL: download and get service template from downloaded archive
39     * GitHub repo: download and get service template from downloaded archive
40
41     :param source: path/URL/GitHub repo to archive/service-template file
42     :type source: basestring
43     :param service_template_filename: path to service template if source is a non-CSAR archive
44      with CSAR archives, this is read from the metadata file)
45     :type service_template_filename: basestring
46     :return: path to main service template file
47     :rtype: basestring
48     """
49     if urlparse(source).scheme:
50         downloaded_file = utils.download_file(source)
51         return _get_service_template_file_from_archive(
52             downloaded_file, service_template_filename)
53     elif os.path.isfile(source):
54         if _is_archive(source):
55             return _get_service_template_file_from_archive(source, service_template_filename)
56         else:
57             # Maybe check if yaml.
58             return os.path.abspath(source)
59     elif len(source.split('/')) == 2:
60         url = _map_to_github_url(source)
61         downloaded_file = utils.download_file(url)
62         return _get_service_template_file_from_archive(
63             downloaded_file, service_template_filename)
64     else:
65         raise AriaCliError(
66             'You must provide either a path to a local file, a remote URL '
67             'or a GitHub `organization/repository[:tag/branch]`')
68
69
70 def _get_service_template_file_from_archive(archive, service_template_filename):
71     """
72     Extract archive to temporary location and get path to service template file.
73
74     :param archive: path to archive file
75     :type archive: basestring
76     :param service_template_filename: path to service template file relative to archive
77     :type service_template_filename: basestring
78     :return: absolute path to service template file
79     :rtype: basestring
80
81     """
82     if csar.is_csar_archive(archive):
83         service_template_file = _extract_csar_archive(archive)
84     else:
85         extract_directory = archive_utils.extract_archive(archive)
86         print extract_directory
87         service_template_dir = os.path.join(
88             extract_directory,
89             os.listdir(extract_directory)[0],
90         )
91         print service_template_dir
92         service_template_file = os.path.join(service_template_dir, service_template_filename)
93         print service_template_file
94         print service_template_filename
95
96     if not os.path.isfile(service_template_file):
97         raise AriaCliError(
98             'Could not find `{0}`. Please provide the name of the main '
99             'service template file by using the `-n/--service-template-filename` flag'
100             .format(service_template_filename))
101     return service_template_file
102
103
104 def _map_to_github_url(source):
105     """
106     Returns a path to a downloaded GitHub archive.
107
108     :param source: GitHub repo: ``org/repo[:tag/branch]``
109     :type source: basestring
110     :return: URL to the archive file for the given repo in GitHub
111     :rtype: basestring
112
113     """
114     source_parts = source.split(':', 1)
115     repo = source_parts[0]
116     tag = source_parts[1] if len(source_parts) == 2 else 'master'
117     url = 'https://github.com/{0}/archive/{1}.tar.gz'.format(repo, tag)
118     return url
119
120
121 def _is_archive(source):
122     return archive_utils.is_archive(source) or csar.is_csar_archive(source)
123
124
125 def _extract_csar_archive(archive):
126     reader = csar.read(source=archive)
127     main_service_template_file_name = os.path.basename(reader.entry_definitions)
128     return os.path.join(reader.destination,
129                         main_service_template_file_name)