~ubuntu-branches/debian/stretch/waagent/stretch

« back to all changes in this revision

Viewing changes to azurelinuxagent/common/protocol/hostplugin.py

  • Committer: Package Import Robot
  • Author(s): Bastian Blank
  • Date: 2016-08-24 16:48:22 UTC
  • mfrom: (1.2.5)
  • Revision ID: package-import@ubuntu.com-20160824164822-vdf8m5xy5gycm1cz
Tags: 2.1.6-1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Microsoft Azure Linux Agent
 
2
#
 
3
# Copyright 2014 Microsoft Corporation
 
4
#
 
5
# Licensed under the Apache License, Version 2.0 (the "License");
 
6
# you may not use this file except in compliance with the License.
 
7
# You may obtain a copy of the License at
 
8
#
 
9
#     http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
11
# Unless required by applicable law or agreed to in writing, software
 
12
# distributed under the License is distributed on an "AS IS" BASIS,
 
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
# See the License for the specific language governing permissions and
 
15
# limitations under the License.
 
16
#
 
17
# Requires Python 2.4+ and Openssl 1.0+
 
18
#
 
19
 
 
20
from azurelinuxagent.common.protocol.wire import *
 
21
from azurelinuxagent.common.utils import textutil
 
22
 
 
23
HOST_PLUGIN_PORT = 32526
 
24
URI_FORMAT_GET_API_VERSIONS = "http://{0}:{1}/versions"
 
25
URI_FORMAT_PUT_VM_STATUS = "http://{0}:{1}/status"
 
26
URI_FORMAT_PUT_LOG = "http://{0}:{1}/vmAgentLog"
 
27
API_VERSION = "2015-09-01"
 
28
 
 
29
 
 
30
class HostPluginProtocol(object):
 
31
    def __init__(self, endpoint):
 
32
        if endpoint is None:
 
33
            raise ProtocolError("Host plugin endpoint not provided")
 
34
        self.is_initialized = False
 
35
        self.is_available = False
 
36
        self.api_versions = None
 
37
        self.endpoint = endpoint
 
38
 
 
39
    def ensure_initialized(self):
 
40
        if not self.is_initialized:
 
41
            self.api_versions = self.get_api_versions()
 
42
            self.is_available = API_VERSION in self.api_versions
 
43
            self.is_initialized = True
 
44
        return self.is_available
 
45
 
 
46
    def get_api_versions(self):
 
47
        url = URI_FORMAT_GET_API_VERSIONS.format(self.endpoint,
 
48
                                                 HOST_PLUGIN_PORT)
 
49
        logger.info("getting API versions at [{0}]".format(url))
 
50
        try:
 
51
            response = restutil.http_get(url)
 
52
            if response.status != httpclient.OK:
 
53
                logger.error(
 
54
                    "get API versions returned status code [{0}]".format(
 
55
                        response.status))
 
56
                return []
 
57
            return response.read()
 
58
        except HttpError as e:
 
59
            logger.error("get API versions failed with [{0}]".format(e))
 
60
            return []
 
61
 
 
62
    def put_vm_status(self, status_blob, sas_url):
 
63
        """
 
64
        Try to upload the VM status via the host plugin /status channel
 
65
        :param sas_url: the blob SAS url to pass to the host plugin
 
66
        :type status_blob: StatusBlob
 
67
        """
 
68
        if not self.ensure_initialized():
 
69
            logger.error("host plugin channel is not available")
 
70
            return
 
71
        if status_blob is None or status_blob.vm_status is None:
 
72
            logger.error("no status data was provided")
 
73
            return
 
74
        url = URI_FORMAT_PUT_VM_STATUS.format(self.endpoint, HOST_PLUGIN_PORT)
 
75
        status = textutil.b64encode(status_blob.vm_status)
 
76
        headers = {"x-ms-version": API_VERSION}
 
77
        blob_headers = [{'headerName': 'x-ms-version',
 
78
                         'headerValue': status_blob.__storage_version__},
 
79
                        {'headerName': 'x-ms-blob-type',
 
80
                         'headerValue': status_blob.type}]
 
81
        data = json.dumps({'requestUri': sas_url, 'headers': blob_headers,
 
82
                           'content': status}, sort_keys=True)
 
83
        logger.info("put VM status at [{0}]".format(url))
 
84
        try:
 
85
            response = restutil.http_put(url, data, headers)
 
86
            if response.status != httpclient.OK:
 
87
                logger.error("put VM status returned status code [{0}]".format(
 
88
                    response.status))
 
89
        except HttpError as e:
 
90
            logger.error("put VM status failed with [{0}]".format(e))
 
91
 
 
92
    def put_vm_log(self, content, container_id, deployment_id):
 
93
        """
 
94
        Try to upload the given content to the host plugin
 
95
        :param deployment_id: the deployment id, which is obtained from the
 
96
        goal state (tenant name)
 
97
        :param container_id: the container id, which is obtained from the
 
98
        goal state
 
99
        :param content: the binary content of the zip file to upload
 
100
        :return:
 
101
        """
 
102
        if not self.ensure_initialized():
 
103
            logger.error("host plugin channel is not available")
 
104
            return
 
105
        if content is None or container_id is None or deployment_id is None:
 
106
            logger.error(
 
107
                "invalid arguments passed: "
 
108
                "[{0}], [{1}], [{2}]".format(
 
109
                    content,
 
110
                    container_id,
 
111
                    deployment_id))
 
112
            return
 
113
        url = URI_FORMAT_PUT_LOG.format(self.endpoint, HOST_PLUGIN_PORT)
 
114
 
 
115
        headers = {"x-ms-vmagentlog-deploymentid": deployment_id,
 
116
                   "x-ms-vmagentlog-containerid": container_id}
 
117
        logger.info("put VM log at [{0}]".format(url))
 
118
        try:
 
119
            response = restutil.http_put(url, content, headers)
 
120
            if response.status != httpclient.OK:
 
121
                logger.error("put log returned status code [{0}]".format(
 
122
                    response.status))
 
123
        except HttpError as e:
 
124
            logger.error("put log failed with [{0}]".format(e))