summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xec2-init-appliance-ebs-volume-mount.sh48
-rwxr-xr-xec2-run-user-data.py23
-rwxr-xr-xsetup.py4
-rw-r--r--tests.py37
4 files changed, 108 insertions, 4 deletions
diff --git a/ec2-init-appliance-ebs-volume-mount.sh b/ec2-init-appliance-ebs-volume-mount.sh
new file mode 100755
index 00000000..7106b353
--- /dev/null
+++ b/ec2-init-appliance-ebs-volume-mount.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+if [ -n "$EBSMOUNT_DEBUG" ]
+then
+ do="echo"
+ mktemp_args="-u"
+else
+ do=""
+ mktemp_args=""
+fi
+
+if [ "$#" -lt 2 ]
+then
+ echo "Usage: $0 <EBS device> <path> [<path> [<path>...]]"
+ exit 1
+fi
+
+ebs_volume_device="$1"
+shift
+
+canonicalise_dir() {
+ dirname="$1"
+ echo "${dirname}" | sed -e 's/[^a-zA-Z0-9]/_/g'
+}
+
+# The blkid call will detect whether there's already a filesystem on the EBS volume
+if [ -n "$(blkid -p -o udev "${ebs_volume_device}")" ]
+then
+ $do mkfs.ext3 "${ebs_volume_device}"
+fi
+
+tmpdir="$(mktemp -d $mktemp_args --tmpdir=/var/run/ec2-init)"
+$do mount ${ebs_volume_device} ${tmpdir}
+
+for dir in "$@"
+do
+ ebsdir="${tmpdir}/$(canonicalise_dir "${dir}")"
+ if [ ! -d "${ebsdir}" ]
+ then
+ # We bootstrap the storage with the existing data
+ $do mkdir "${ebsdir}"
+ $do cp -a ${dir} "${ebsdir}"
+ $do chown --reference "${dir}" "${ebsdir}"
+ $do chmod --reference "${dir}" "${ebsdir}"
+ fi
+ # Finally, we mount it on top of the old directory.
+ $do mount --bind "${ebsdir}" "${dir}"
+done
diff --git a/ec2-run-user-data.py b/ec2-run-user-data.py
index 67ecf219..d7e8e632 100755
--- a/ec2-run-user-data.py
+++ b/ec2-run-user-data.py
@@ -61,7 +61,7 @@ def handle_appliance_config(payload):
@handler('text/x-ebs-mount-description')
def handle_ebs_mount_description(payload):
- (volume_description, path) = payload.split(':')
+ (volume_description, paths) = payload.split(':')
(identifier_type, identifier) = volume_description.split('=')
if identifier_type == 'device':
@@ -73,6 +73,17 @@ def handle_ebs_mount_description(payload):
else:
return
+ mount_ebs_volume(device, paths.split(','))
+
+def mount_ebs_volume(device, paths):
+ if os.path.exists('ec2-init-appliance-ebs-volume-mount.sh'):
+ helper = './ec2-init-appliance-ebs-volume-mount.sh'
+ else:
+ helper = '/usr/share/ec2-init/ec2-init-appliance-ebs-volume-mount.sh'
+ helper = subprocess.Popen([helper, device] + paths, stdout=subprocess.PIPE)
+ stdout, stderr = helper.communicate()
+ return stdout
+
@handler('text/x-shellscript')
def handle_shell_script(payload):
(fd, path) = tempfile.mkstemp()
@@ -129,6 +140,16 @@ class ApplianceConfig(object):
# An empty script?
continue
content_type_handlers['text/x-shellscript'](script)
+ elif node.tagName == 'storage':
+ paths = []
+ device = node.getAttribute('device')
+ for subnode in node.childNodes:
+ if subnode.tagName == 'path':
+ for subsubnode in subnode.childNodes:
+ if subsubnode.nodeType == root.TEXT_NODE:
+ paths += [subsubnode.nodeValue.strip()]
+ break
+ mount_ebs_volume(device, paths)
def main():
ec2 = ec2init.EC2Init()
diff --git a/setup.py b/setup.py
index fa855bd6..dab57103 100755
--- a/setup.py
+++ b/setup.py
@@ -37,5 +37,7 @@ setup(name='EC2-init',
'ec2-wait-for-meta-data-service.py'],
data_files=[('/etc/ec2-init', ['ec2-config.cfg']),
('/etc/ec2-init/templates', glob('templates/*')),
- ('/etc/init.d', ['ec2-init'])],
+ ('/etc/init.d', ['ec2-init']),
+ ('/usr/share/ec2-init', ['ec2-init-appliance-ebs-volume-mount.sh']),
+ ],
)
diff --git a/tests.py b/tests.py
index 33c45ec7..84103745 100644
--- a/tests.py
+++ b/tests.py
@@ -18,9 +18,42 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
+import re
+import os
import unittest
-class RunUserDataApplianceConfigScript(unittest.TestCase):
+class RunUserDataApplianceTestCase(unittest.TestCase):
+ def handle_xml(self, xml):
+ msg = self.ec2_run_user_data.parse_user_data(xml)
+ self.ec2_run_user_data.handle_part(msg)
+
+class RunUserDataApplianceConfigEBS(RunUserDataApplianceTestCase):
+ def setUp(self):
+ self.ec2_run_user_data = __import__('ec2-run-user-data')
+ reload(self.ec2_run_user_data)
+ self.real_mount_ebs_volume = self.ec2_run_user_data.mount_ebs_volume
+ self.ec2_run_user_data.mount_ebs_volume = self.fake_mount_ebs_volume
+
+ def fake_mount_ebs_volume(self, device, paths):
+ self.assertEqual(device, '/dev/sdc')
+ self.assertEqual(paths, ['/etc/alfresco', '/var/lib/mysql'])
+
+ def testApplianceConfigEBS(self):
+ os.environ['EBSMOUNT_DEBUG'] = 'yes, please'
+ xml = '<appliance><storage device="/dev/sdc"><path>/etc/alfresco</path><path>/var/lib/mysql</path></storage></appliance>'
+ self.handle_xml(xml)
+
+ def testMountEBSVolume(self):
+ output = self.real_mount_ebs_volume('/dev/sdh', ['/foo', '/bar'])
+ lines = output.strip().split('\n')
+ self.assertEqual(len(lines), 11)
+ match = re.match('mount /dev/sdh (/var/run/ec2-init/tmp.[a-zA-Z0-9]+)', lines[0])
+ self.assertNotEqual(match, None)
+ tmpdir = match.group(1)
+ for (i, s) in zip(range(10), ['mkdir %s/_foo', 'cp -a /foo %s/_foo', 'chown --reference /foo %s/_foo', 'chmod --reference /foo %s/_foo', 'mount --bind %s/_foo /foo', 'mkdir %s/_bar', 'cp -a /bar %s/_bar', 'chown --reference /bar %s/_bar', 'chmod --reference /bar %s/_bar', 'mount --bind %s/_bar /bar']):
+ self.assertEqual(s % tmpdir, lines[i+1])
+
+class RunUserDataApplianceConfigScript(RunUserDataApplianceTestCase):
def setUp(self):
self.ec2_run_user_data = __import__('ec2-run-user-data')
self.fake_handle_shell_script_counter = 0
@@ -62,7 +95,7 @@ echo hey'''
self.handle_xml(xml)
self.assertEqual(self.fake_handle_shell_script_counter, 1)
-class RunUserDataApplianceConfigPackageHandling(unittest.TestCase):
+class RunUserDataApplianceConfigPackageHandling(RunUserDataApplianceTestCase):
def setUp(self):
self.fake_install_remove_package_counter = 0