summaryrefslogtreecommitdiffstats
path: root/azure/aria/aria-extension-cloudify/src/aria/aria/cli/csar.py
blob: 40b169921b8fd7c79f8989e03d27269d95736735 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# 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.

"""
Support for the CSAR (Cloud Service ARchive) packaging specification.

See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca
/TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html#_Toc461787381>`__
"""

import os
import logging
import pprint
import tempfile
import zipfile

import requests
from ruamel import yaml

CSAR_FILE_EXTENSION = '.csar'
META_FILE = 'TOSCA-Metadata/TOSCA.meta'
META_FILE_VERSION_KEY = 'TOSCA-Meta-File-Version'
META_FILE_VERSION_VALUE = '1.0'
META_CSAR_VERSION_KEY = 'CSAR-Version'
META_CSAR_VERSION_VALUE = '1.1'
META_CREATED_BY_KEY = 'Created-By'
META_CREATED_BY_VALUE = 'ARIA'
META_ENTRY_DEFINITIONS_KEY = 'Entry-Definitions'
BASE_METADATA = {
    META_FILE_VERSION_KEY: META_FILE_VERSION_VALUE,
    META_CSAR_VERSION_KEY: META_CSAR_VERSION_VALUE,
    META_CREATED_BY_KEY: META_CREATED_BY_VALUE,
}


def write(service_template_path, destination, logger):

    service_template_path = os.path.abspath(os.path.expanduser(service_template_path))
    source = os.path.dirname(service_template_path)
    entry = os.path.basename(service_template_path)

    meta_file = os.path.join(source, META_FILE)
    if not os.path.isdir(source):
        raise ValueError('{0} is not a directory. Please specify the service template '
                         'directory.'.format(source))
    if not os.path.isfile(service_template_path):
        raise ValueError('{0} does not exists. Please specify a valid entry point.'
                         .format(service_template_path))
    if os.path.exists(destination):
        raise ValueError('{0} already exists. Please provide a path to where the CSAR should be '
                         'created.'.format(destination))
    if os.path.exists(meta_file):
        raise ValueError('{0} already exists. This commands generates a meta file for you. Please '
                         'remove the existing metafile.'.format(meta_file))
    metadata = BASE_METADATA.copy()
    metadata[META_ENTRY_DEFINITIONS_KEY] = entry
    logger.debug('Compressing root directory to ZIP')
    with zipfile.ZipFile(destination, 'w', zipfile.ZIP_DEFLATED) as f:
        for root, _, files in os.walk(source):
            for file in files:
                file_full_path = os.path.join(root, file)
                file_relative_path = os.path.relpath(file_full_path, source)
                logger.debug('Writing to archive: {0}'.format(file_relative_path))
                f.write(file_full_path, file_relative_path)
        logger.debug('Writing new metadata file to {0}'.format(META_FILE))
        f.writestr(META_FILE, yaml.dump(metadata, default_flow_style=False))


class _CSARReader(object):

    def __init__(self, source, destination, logger):
        self.logger = logger
        if os.path.isdir(destination) and os.listdir(destination):
            raise ValueError('{0} already exists and is not empty. '
                             'Please specify the location where the CSAR '
                             'should be extracted.'.format(destination))
        downloaded_csar = '://' in source
        if downloaded_csar:
            file_descriptor, download_target = tempfile.mkstemp()
            os.close(file_descriptor)
            self._download(source, download_target)
            source = download_target
        self.source = os.path.expanduser(source)
        self.destination = os.path.expanduser(destination)
        self.metadata = {}
        try:
            if not os.path.exists(self.source):
                raise ValueError('{0} does not exists. Please specify a valid CSAR path.'
                                 .format(self.source))
            if not zipfile.is_zipfile(self.source):
                raise ValueError('{0} is not a valid CSAR.'.format(self.source))
            self._extract()
            self._read_metadata()
            self._validate()
        finally:
            if downloaded_csar:
                os.remove(self.source)

    @property
    def created_by(self):
        return self.metadata.get(META_CREATED_BY_KEY)

    @property
    def csar_version(self):
        return self.metadata.get(META_CSAR_VERSION_KEY)

    @property
    def meta_file_version(self):
        return self.metadata.get(META_FILE_VERSION_KEY)

    @property
    def entry_definitions(self):
        return self.metadata.get(META_ENTRY_DEFINITIONS_KEY)

    @property
    def entry_definitions_yaml(self):
        with open(os.path.join(self.destination, self.entry_definitions)) as f:
            return yaml.load(f)

    def _extract(self):
        self.logger.debug('Extracting CSAR contents')
        if not os.path.exists(self.destination):
            os.mkdir(self.destination)
        with zipfile.ZipFile(self.source) as f:
            f.extractall(self.destination)
        self.logger.debug('CSAR contents successfully extracted')

    def _read_metadata(self):
        csar_metafile = os.path.join(self.destination, META_FILE)
        if not os.path.exists(csar_metafile):
            raise ValueError('Metadata file {0} is missing from the CSAR'.format(csar_metafile))
        self.logger.debug('CSAR metadata file: {0}'.format(csar_metafile))
        self.logger.debug('Attempting to parse CSAR metadata YAML')
        with open(csar_metafile) as f:
            self.metadata.update(yaml.load(f))
        self.logger.debug('CSAR metadata:{0}{1}'.format(os.linesep, pprint.pformat(self.metadata)))

    def _validate(self):
        def validate_key(key, expected=None):
            if not self.metadata.get(key):
                raise ValueError('{0} is missing from the metadata file.'.format(key))
            actual = str(self.metadata[key])
            if expected and actual != expected:
                raise ValueError('{0} is expected to be {1} in the metadata file while it is in '
                                 'fact {2}.'.format(key, expected, actual))
        validate_key(META_FILE_VERSION_KEY, expected=META_FILE_VERSION_VALUE)
        validate_key(META_CSAR_VERSION_KEY, expected=META_CSAR_VERSION_VALUE)
        validate_key(META_CREATED_BY_KEY)
        validate_key(META_ENTRY_DEFINITIONS_KEY)
        self.logger.debug('CSAR entry definitions: {0}'.format(self.entry_definitions))
        entry_definitions_path = os.path.join(self.destination, self.entry_definitions)
        if not os.path.isfile(entry_definitions_path):
            raise ValueError('The entry definitions {0} referenced by the metadata file does not '
                             'exist.'.format(entry_definitions_path))

    def _download(self, url, target):
        response = requests.get(url, stream=True)
        if response.status_code != 200:
            raise ValueError('Server at {0} returned a {1} status code'
                             .format(url, response.status_code))
        self.logger.info('Downloading {0} to {1}'.format(url, target))
        with open(target, 'wb') as f:
            for chunk in response.iter_content(chunk_size=8192):
                if chunk:
                    f.write(chunk)


def read(source, destination=None, logger=None):
    destination = destination or tempfile.mkdtemp()
    logger = logger or logging.getLogger('dummy')
    return _CSARReader(source=source, destination=destination, logger=logger)


def is_csar_archive(source):
    return source.endswith(CSAR_FILE_EXTENSION)