Merge "Add support to specify userdata when creating a server"
This commit is contained in:
commit
4fd4953b80
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
"""Mogan v1 Baremetal server action implementations"""
|
"""Mogan v1 Baremetal server action implementations"""
|
||||||
|
|
||||||
|
import io
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
@ -107,6 +108,11 @@ class CreateServer(command.ShowOne):
|
|||||||
metavar="<zone-name>",
|
metavar="<zone-name>",
|
||||||
help=_("The availability zone for the baremetal server placement"),
|
help=_("The availability zone for the baremetal server placement"),
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--user-data',
|
||||||
|
metavar='<user-data>',
|
||||||
|
help=_('User data file to inject into the instance'),
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--extra",
|
"--extra",
|
||||||
metavar="<extra>",
|
metavar="<extra>",
|
||||||
@ -144,17 +150,36 @@ class CreateServer(command.ShowOne):
|
|||||||
nic['net_id'] = nic['net-id']
|
nic['net_id'] = nic['net-id']
|
||||||
del nic['net-id']
|
del nic['net-id']
|
||||||
|
|
||||||
data = bc_client.server.create(
|
userdata = None
|
||||||
|
if parsed_args.user_data:
|
||||||
|
try:
|
||||||
|
userdata = io.open(parsed_args.user_data)
|
||||||
|
except IOError as e:
|
||||||
|
msg = _("Can't open '%(data)s': %(exception)s")
|
||||||
|
raise exceptions.CommandError(
|
||||||
|
msg % {"data": parsed_args.user_data,
|
||||||
|
"exception": e}
|
||||||
|
)
|
||||||
|
|
||||||
|
boot_kwargs = dict(
|
||||||
name=parsed_args.name,
|
name=parsed_args.name,
|
||||||
image_uuid=image_data.id,
|
image_uuid=image_data.id,
|
||||||
flavor_uuid=flavor_data.uuid,
|
flavor_uuid=flavor_data.uuid,
|
||||||
description=parsed_args.description,
|
description=parsed_args.description,
|
||||||
networks=parsed_args.nic,
|
networks=parsed_args.nic,
|
||||||
availability_zone=parsed_args.availability_zone,
|
availability_zone=parsed_args.availability_zone,
|
||||||
|
userdata=userdata,
|
||||||
extra=parsed_args.extra,
|
extra=parsed_args.extra,
|
||||||
min_count=parsed_args.min_count,
|
min_count=parsed_args.min_count,
|
||||||
max_count=parsed_args.max_count
|
max_count=parsed_args.max_count
|
||||||
)
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
data = bc_client.server.create(**boot_kwargs)
|
||||||
|
finally:
|
||||||
|
if hasattr(userdata, 'close'):
|
||||||
|
userdata.close()
|
||||||
|
|
||||||
info = {}
|
info = {}
|
||||||
info.update(data._info)
|
info.update(data._info)
|
||||||
return zip(*sorted(info.items()))
|
return zip(*sorted(info.items()))
|
||||||
|
@ -13,6 +13,11 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
import base64
|
||||||
|
|
||||||
|
from oslo_utils import encodeutils
|
||||||
|
import six
|
||||||
|
|
||||||
from moganclient.common import base
|
from moganclient.common import base
|
||||||
|
|
||||||
|
|
||||||
@ -25,7 +30,7 @@ class ServerManager(base.ManagerWithFind):
|
|||||||
|
|
||||||
def create(self, name, image_uuid, flavor_uuid, networks,
|
def create(self, name, image_uuid, flavor_uuid, networks,
|
||||||
description=None, availability_zone=None, extra=None,
|
description=None, availability_zone=None, extra=None,
|
||||||
min_count=None, max_count=None):
|
userdata=None, min_count=None, max_count=None):
|
||||||
url = '/instances'
|
url = '/instances'
|
||||||
data = {
|
data = {
|
||||||
'name': name,
|
'name': name,
|
||||||
@ -33,6 +38,28 @@ class ServerManager(base.ManagerWithFind):
|
|||||||
'instance_type_uuid': flavor_uuid,
|
'instance_type_uuid': flavor_uuid,
|
||||||
'networks': networks
|
'networks': networks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if userdata is not None:
|
||||||
|
if hasattr(userdata, 'read'):
|
||||||
|
userdata = userdata.read()
|
||||||
|
|
||||||
|
# NOTE(melwitt): Text file data is converted to bytes prior to
|
||||||
|
# base64 encoding. The utf-8 encoding will fail for binary files.
|
||||||
|
if six.PY3:
|
||||||
|
try:
|
||||||
|
userdata = userdata.encode("utf-8")
|
||||||
|
except AttributeError:
|
||||||
|
# In python 3, 'bytes' object has no attribute 'encode'
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
userdata = encodeutils.safe_encode(userdata)
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
userdata_b64 = base64.b64encode(userdata).decode('utf-8')
|
||||||
|
data["user_data"] = userdata_b64
|
||||||
|
|
||||||
if availability_zone is not None:
|
if availability_zone is not None:
|
||||||
data['availability_zone'] = availability_zone
|
data['availability_zone'] = availability_zone
|
||||||
if description is not None:
|
if description is not None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user