summaryrefslogtreecommitdiffstats
path: root/azure/aria/aria-extension-cloudify/src/aria/aria/cli/service_template_utils.py
blob: 2af72a06ba9c536d364507467c56b007a7a30c79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Loading mechanism for service templates.
"""

import os
from urlparse import urlparse

from . import csar
from . import utils
from .exceptions import AriaCliError
from ..utils import archive as archive_utils


def get(source, service_template_filename):
    """
    Get a source and return a path to the main service template file

    The behavior based on then source argument content is:

    * local ``.yaml`` file: return the file
    * local archive (``.csar``, ``.zip``, ``.tar``, ``.tar.gz``, and ``.tar.bz2``): extract it
      locally and return path service template file
    * URL: download and get service template from downloaded archive
    * GitHub repo: download and get service template from downloaded archive

    :param source: path/URL/GitHub repo to archive/service-template file
    :type source: basestring
    :param service_template_filename: path to service template if source is a non-CSAR archive
     with CSAR archives, this is read from the metadata file)
    :type service_template_filename: basestring
    :return: path to main service template file
    :rtype: basestring
    """
    if urlparse(source).scheme:
        downloaded_file = utils.download_file(source)
        return _get_service_template_file_from_archive(
            downloaded_file, service_template_filename)
    elif os.path.isfile(source):
        if _is_archive(source):
            return _get_service_template_file_from_archive(source, service_template_filename)
        else:
            # Maybe check if yaml.
            return os.path.abspath(source)
    elif len(source.split('/')) == 2:
        url = _map_to_github_url(source)
        downloaded_file = utils.download_file(url)
        return _get_service_template_file_from_archive(
            downloaded_file, service_template_filename)
    else:
        raise AriaCliError(
            'You must provide either a path to a local file, a remote URL '
            'or a GitHub `organization/repository[:tag/branch]`')


def _get_service_template_file_from_archive(archive, service_template_filename):
    """
    Extract archive to temporary location and get path to service template file.

    :param archive: path to archive file
    :type archive: basestring
    :param service_template_filename: path to service template file relative to archive
    :type service_template_filename: basestring
    :return: absolute path to service template file
    :rtype: basestring

    """
    if csar.is_csar_archive(archive):
        service_template_file = _extract_csar_archive(archive)
    else:
        extract_directory = archive_utils.extract_archive(archive)
        print extract_directory
        service_template_dir = os.path.join(
            extract_directory,
            os.listdir(extract_directory)[0],
        )
        print service_template_dir
        service_template_file = os.path.join(service_template_dir, service_template_filename)
        print service_template_file
        print service_template_filename

    if not os.path.isfile(service_template_file):
        raise AriaCliError(
            'Could not find `{0}`. Please provide the name of the main '
            'service template file by using the `-n/--service-template-filename` flag'
            .format(service_template_filename))
    return service_template_file


def _map_to_github_url(source):
    """
    Returns a path to a downloaded GitHub archive.

    :param source: GitHub repo: ``org/repo[:tag/branch]``
    :type source: basestring
    :return: URL to the archive file for the given repo in GitHub
    :rtype: basestring

    """
    source_parts = source.split(':', 1)
    repo = source_parts[0]
    tag = source_parts[1] if len(source_parts) == 2 else 'master'
    url = 'https://github.com/{0}/archive/{1}.tar.gz'.format(repo, tag)
    return url


def _is_archive(source):
    return archive_utils.is_archive(source) or csar.is_csar_archive(source)


def _extract_csar_archive(archive):
    reader = csar.read(source=archive)
    main_service_template_file_name = os.path.basename(reader.entry_definitions)
    return os.path.join(reader.destination,
                        main_service_template_file_name)