1
# Microsoft Azure Linux Agent
3
# Copyright 2014 Microsoft Corporation
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
9
# http://www.apache.org/licenses/LICENSE-2.0
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.
17
# Requires Python 2.4+ and Openssl 1.0+
20
from azurelinuxagent.common.protocol.wire import *
21
from azurelinuxagent.common.utils import textutil
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"
30
class HostPluginProtocol(object):
31
def __init__(self, endpoint):
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
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
46
def get_api_versions(self):
47
url = URI_FORMAT_GET_API_VERSIONS.format(self.endpoint,
49
logger.info("getting API versions at [{0}]".format(url))
51
response = restutil.http_get(url)
52
if response.status != httpclient.OK:
54
"get API versions returned status code [{0}]".format(
57
return response.read()
58
except HttpError as e:
59
logger.error("get API versions failed with [{0}]".format(e))
62
def put_vm_status(self, status_blob, sas_url):
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
68
if not self.ensure_initialized():
69
logger.error("host plugin channel is not available")
71
if status_blob is None or status_blob.vm_status is None:
72
logger.error("no status data was provided")
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))
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(
89
except HttpError as e:
90
logger.error("put VM status failed with [{0}]".format(e))
92
def put_vm_log(self, content, container_id, deployment_id):
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
99
:param content: the binary content of the zip file to upload
102
if not self.ensure_initialized():
103
logger.error("host plugin channel is not available")
105
if content is None or container_id is None or deployment_id is None:
107
"invalid arguments passed: "
108
"[{0}], [{1}], [{2}]".format(
113
url = URI_FORMAT_PUT_LOG.format(self.endpoint, HOST_PLUGIN_PORT)
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))
119
response = restutil.http_put(url, content, headers)
120
if response.status != httpclient.OK:
121
logger.error("put log returned status code [{0}]".format(
123
except HttpError as e:
124
logger.error("put log failed with [{0}]".format(e))