Replace part_handler.py with write-files cloudinit mechanism
part-handler.py was acting as a write-files mechanism. Instead just use the write-files mechanism directly to avoid the complexities of the part-handler. blueprint: use-cloudinit-write Change-Id: I6e80c344743d6fd2fa9a49507de6d50e3d9eea73
This commit is contained in:
parent
87e02d79b8
commit
621f5bfdba
|
@ -2,6 +2,7 @@
|
||||||
setenforce 0
|
setenforce 0
|
||||||
useradd -m @INSTANCE_USER@
|
useradd -m @INSTANCE_USER@
|
||||||
echo -e '@INSTANCE_USER@\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers
|
echo -e '@INSTANCE_USER@\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers
|
||||||
|
mkdir /var/lib/heat-cfntools
|
||||||
|
|
||||||
# Do not remove - the cloud boothook should always return success
|
# Do not remove - the cloud boothook should always return success
|
||||||
exit 0
|
exit 0
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#cloud-config
|
||||||
user: @INSTANCE_USER@
|
user: @INSTANCE_USER@
|
||||||
|
|
||||||
cloud_config_modules:
|
cloud_config_modules:
|
||||||
|
@ -6,6 +7,7 @@ cloud_config_modules:
|
||||||
- timezone
|
- timezone
|
||||||
- update_etc_hosts
|
- update_etc_hosts
|
||||||
- update_hostname
|
- update_hostname
|
||||||
|
- write-files
|
||||||
|
|
||||||
# Capture all subprocess output into a logfile
|
# Capture all subprocess output into a logfile
|
||||||
# Useful for troubleshooting cloud-init issues
|
# Useful for troubleshooting cloud-init issues
|
||||||
|
|
|
@ -78,7 +78,6 @@ def main():
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
userdata_path = os.path.join(VAR_PATH, 'cfn-userdata')
|
userdata_path = os.path.join(VAR_PATH, 'cfn-userdata')
|
||||||
os.chmod(userdata_path, 0o700)
|
|
||||||
|
|
||||||
LOG.info('Provision began: %s\n' % datetime.datetime.now())
|
LOG.info('Provision began: %s\n' % datetime.datetime.now())
|
||||||
returncode = call([userdata_path])
|
returncode = call([userdata_path])
|
||||||
|
|
|
@ -1,46 +0,0 @@
|
||||||
#part-handler
|
|
||||||
|
|
||||||
# Licensed 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.
|
|
||||||
|
|
||||||
import datetime
|
|
||||||
import errno
|
|
||||||
import os
|
|
||||||
|
|
||||||
|
|
||||||
def list_types():
|
|
||||||
return(["text/x-cfninitdata"])
|
|
||||||
|
|
||||||
|
|
||||||
def handle_part(data, ctype, filename, payload):
|
|
||||||
if ctype == "__begin__":
|
|
||||||
try:
|
|
||||||
os.makedirs('/var/lib/heat-cfntools', 0o700)
|
|
||||||
except OSError as e:
|
|
||||||
if e.errno != errno.EEXIST:
|
|
||||||
raise
|
|
||||||
return
|
|
||||||
|
|
||||||
if ctype == "__end__":
|
|
||||||
return
|
|
||||||
|
|
||||||
with open('/var/log/part-handler.log', 'a') as log:
|
|
||||||
timestamp = datetime.datetime.now()
|
|
||||||
log.write('%s filename:%s, ctype:%s\n' % (timestamp, filename, ctype))
|
|
||||||
|
|
||||||
if ctype == 'text/x-cfninitdata':
|
|
||||||
with open('/var/lib/heat-cfntools/%s' % filename, 'w') as f:
|
|
||||||
f.write(payload)
|
|
||||||
|
|
||||||
# TODO(sdake) hopefully temporary until users move to heat-cfntools-1.3
|
|
||||||
with open('/var/lib/cloud/data/%s' % filename, 'w') as f:
|
|
||||||
f.write(payload)
|
|
|
@ -19,6 +19,7 @@ import json
|
||||||
import os
|
import os
|
||||||
import pkgutil
|
import pkgutil
|
||||||
from urlparse import urlparse
|
from urlparse import urlparse
|
||||||
|
import yaml
|
||||||
|
|
||||||
from oslo.config import cfg
|
from oslo.config import cfg
|
||||||
|
|
||||||
|
@ -201,30 +202,33 @@ class Instance(resource.Resource):
|
||||||
filename=filename)
|
filename=filename)
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
def read_cloudinit_file(fn):
|
def read_cfg_file():
|
||||||
|
rd = pkgutil.get_data('heat', 'cloudinit/config')
|
||||||
|
rd = rd.replace('@INSTANCE_USER@',
|
||||||
|
cfg.CONF.instance_user)
|
||||||
|
rd = yaml.safe_load(rd)
|
||||||
|
return rd
|
||||||
|
|
||||||
|
def add_data_file(fn, data):
|
||||||
|
rd = [{'owner': 'root:root',
|
||||||
|
'path': '/var/lib/heat-cfntools/%s' % fn,
|
||||||
|
'permissions': '0700',
|
||||||
|
'content': '%s' % data}]
|
||||||
|
return rd
|
||||||
|
|
||||||
|
def yaml_add(yd, fn, data):
|
||||||
|
rd = '%s%s' % (yd, yaml.safe_dump(add_data_file(fn, data)))
|
||||||
|
return rd
|
||||||
|
|
||||||
|
def read_data_file(fn):
|
||||||
data = pkgutil.get_data('heat', 'cloudinit/%s' % fn)
|
data = pkgutil.get_data('heat', 'cloudinit/%s' % fn)
|
||||||
data = data.replace('@INSTANCE_USER@',
|
rd = add_data_file(fn, data)
|
||||||
cfg.CONF.instance_user)
|
return rd
|
||||||
return data
|
|
||||||
|
|
||||||
attachments = [(read_cloudinit_file('config'), 'cloud-config'),
|
def read_cloudinit_file(fn):
|
||||||
(read_cloudinit_file('boothook.sh'), 'boothook.sh',
|
rd = pkgutil.get_data('heat', 'cloudinit/%s' % fn)
|
||||||
'cloud-boothook'),
|
rd = rd.replace('@INSTANCE_USER@', cfg.CONF.instance_user)
|
||||||
(read_cloudinit_file('part_handler.py'),
|
return rd
|
||||||
'part-handler.py'),
|
|
||||||
(userdata, 'cfn-userdata', 'x-cfninitdata'),
|
|
||||||
(read_cloudinit_file('loguserdata.py'),
|
|
||||||
'loguserdata.py', 'x-shellscript')]
|
|
||||||
|
|
||||||
if 'Metadata' in self.t:
|
|
||||||
attachments.append((json.dumps(self.metadata),
|
|
||||||
'cfn-init-data', 'x-cfninitdata'))
|
|
||||||
|
|
||||||
attachments.append((cfg.CONF.heat_watch_server_url,
|
|
||||||
'cfn-watch-server', 'x-cfninitdata'))
|
|
||||||
|
|
||||||
attachments.append((cfg.CONF.heat_metadata_server_url,
|
|
||||||
'cfn-metadata-server', 'x-cfninitdata'))
|
|
||||||
|
|
||||||
# Create a boto config which the cfntools on the host use to know
|
# Create a boto config which the cfntools on the host use to know
|
||||||
# where the cfn and cw API's are to be accessed
|
# where the cfn and cw API's are to be accessed
|
||||||
|
@ -242,9 +246,30 @@ class Instance(resource.Resource):
|
||||||
"cloudwatch_region_name = heat",
|
"cloudwatch_region_name = heat",
|
||||||
"cloudwatch_region_endpoint = %s" %
|
"cloudwatch_region_endpoint = %s" %
|
||||||
cw_url.hostname])
|
cw_url.hostname])
|
||||||
attachments.append((boto_cfg,
|
|
||||||
'cfn-boto-cfg', 'x-cfninitdata'))
|
|
||||||
|
|
||||||
|
# Build yaml write_files section
|
||||||
|
yamld = ''
|
||||||
|
yamld = yaml_add(yamld, 'cfn-watch-server',
|
||||||
|
cfg.CONF.heat_watch_server_url)
|
||||||
|
yamld = yaml_add(yamld, 'cfn-metadata-server',
|
||||||
|
cfg.CONF.heat_metadata_server_url)
|
||||||
|
yamld = yaml_add(yamld, 'cfn-boto-cfg',
|
||||||
|
boto_cfg)
|
||||||
|
yamld = yaml_add(yamld, 'cfn-userdata',
|
||||||
|
userdata)
|
||||||
|
|
||||||
|
if 'Metadata' in self.t:
|
||||||
|
yamld = yaml_add(yamld, 'cfn-init-data',
|
||||||
|
json.dumps(self.metadata))
|
||||||
|
|
||||||
|
yamld = "%swrite_files:\n%s" % (yaml.safe_dump(read_cfg_file()),
|
||||||
|
yamld)
|
||||||
|
|
||||||
|
attachments = [(yamld, 'cloud-config'),
|
||||||
|
(read_cloudinit_file('boothook.sh'), 'boothook.sh',
|
||||||
|
'cloud-boothook'),
|
||||||
|
(read_cloudinit_file('loguserdata.py'),
|
||||||
|
'x-shellscript')]
|
||||||
subparts = [make_subpart(*args) for args in attachments]
|
subparts = [make_subpart(*args) for args in attachments]
|
||||||
mime_blob = MIMEMultipart(_subparts=subparts)
|
mime_blob = MIMEMultipart(_subparts=subparts)
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,6 @@ class LoguserdataTest(HeatTestCase):
|
||||||
super(LoguserdataTest, self).setUp()
|
super(LoguserdataTest, self).setUp()
|
||||||
self.m.StubOutWithMock(pkg_resources, 'get_distribution')
|
self.m.StubOutWithMock(pkg_resources, 'get_distribution')
|
||||||
self.m.StubOutWithMock(subprocess, 'Popen')
|
self.m.StubOutWithMock(subprocess, 'Popen')
|
||||||
self.m.StubOutWithMock(os, 'chmod')
|
|
||||||
|
|
||||||
def test_ci_version(self):
|
def test_ci_version(self):
|
||||||
# too old versions
|
# too old versions
|
||||||
|
@ -90,7 +89,6 @@ class LoguserdataTest(HeatTestCase):
|
||||||
pkg_resources.get_distribution('cloud-init').AndReturn(
|
pkg_resources.get_distribution('cloud-init').AndReturn(
|
||||||
FakeCiVersion('0.7.0'))
|
FakeCiVersion('0.7.0'))
|
||||||
|
|
||||||
os.chmod('/var/lib/heat-cfntools/cfn-userdata', 0o700).AndReturn(None)
|
|
||||||
subprocess.Popen(
|
subprocess.Popen(
|
||||||
['/var/lib/heat-cfntools/cfn-userdata'],
|
['/var/lib/heat-cfntools/cfn-userdata'],
|
||||||
stderr=mox.IgnoreArg(),
|
stderr=mox.IgnoreArg(),
|
||||||
|
@ -105,7 +103,6 @@ class LoguserdataTest(HeatTestCase):
|
||||||
pkg_resources.get_distribution('cloud-init').AndReturn(
|
pkg_resources.get_distribution('cloud-init').AndReturn(
|
||||||
FakeCiVersion('0.7.0'))
|
FakeCiVersion('0.7.0'))
|
||||||
|
|
||||||
os.chmod('/var/lib/heat-cfntools/cfn-userdata', 0o700).AndReturn(None)
|
|
||||||
subprocess.Popen(
|
subprocess.Popen(
|
||||||
['/var/lib/heat-cfntools/cfn-userdata'],
|
['/var/lib/heat-cfntools/cfn-userdata'],
|
||||||
stderr=mox.IgnoreArg(),
|
stderr=mox.IgnoreArg(),
|
||||||
|
@ -122,7 +119,6 @@ class LoguserdataTest(HeatTestCase):
|
||||||
pkg_resources.get_distribution('cloud-init').AndReturn(
|
pkg_resources.get_distribution('cloud-init').AndReturn(
|
||||||
FakeCiVersion('0.7.0'))
|
FakeCiVersion('0.7.0'))
|
||||||
|
|
||||||
os.chmod('/var/lib/heat-cfntools/cfn-userdata', 0o700).AndReturn(None)
|
|
||||||
subprocess.Popen(
|
subprocess.Popen(
|
||||||
['/var/lib/heat-cfntools/cfn-userdata'],
|
['/var/lib/heat-cfntools/cfn-userdata'],
|
||||||
stderr=mox.IgnoreArg(),
|
stderr=mox.IgnoreArg(),
|
||||||
|
@ -137,7 +133,6 @@ class LoguserdataTest(HeatTestCase):
|
||||||
def test_main_error_other(self):
|
def test_main_error_other(self):
|
||||||
pkg_resources.get_distribution('cloud-init').AndReturn(
|
pkg_resources.get_distribution('cloud-init').AndReturn(
|
||||||
FakeCiVersion('0.7.0'))
|
FakeCiVersion('0.7.0'))
|
||||||
os.chmod('/var/lib/heat-cfntools/cfn-userdata', 0o700).AndReturn(None)
|
|
||||||
subprocess.Popen(
|
subprocess.Popen(
|
||||||
['/var/lib/heat-cfntools/cfn-userdata'],
|
['/var/lib/heat-cfntools/cfn-userdata'],
|
||||||
stderr=mox.IgnoreArg(),
|
stderr=mox.IgnoreArg(),
|
||||||
|
@ -156,7 +151,6 @@ class LoguserdataTest(HeatTestCase):
|
||||||
pkg_resources.get_distribution('cloud-init').AndReturn(
|
pkg_resources.get_distribution('cloud-init').AndReturn(
|
||||||
FakeCiVersion('0.7.0'))
|
FakeCiVersion('0.7.0'))
|
||||||
|
|
||||||
os.chmod('/var/lib/heat-cfntools/cfn-userdata', 0o700).AndReturn(None)
|
|
||||||
subprocess.Popen(
|
subprocess.Popen(
|
||||||
['/var/lib/heat-cfntools/cfn-userdata'],
|
['/var/lib/heat-cfntools/cfn-userdata'],
|
||||||
stderr=mox.IgnoreArg(),
|
stderr=mox.IgnoreArg(),
|
||||||
|
|
Loading…
Reference in New Issue