Removes the dep with write-multipart

This commit is contained in:
Sahid Orentino Ferdjaoui 2013-12-01 16:43:17 +01:00
parent 0cff150926
commit a8bc5a8872
4 changed files with 73 additions and 9 deletions

2
README
View File

@ -44,11 +44,9 @@ Notes:
build-essential
python-dev
python-pip
cloud-utils # write-mime-multipart
libssl-dev
Roadmap:
========
Add write-mime-multipart in the package
Add floating-ip
pylint, pep8

View File

@ -19,6 +19,7 @@
import os
import uuid
import warm.utils
from neutronclient.neutron import v2_0 as neutronV20
from neutronclient.common.exceptions import NeutronClientException
@ -226,11 +227,12 @@ class Server(Base):
userdata = None
if "userdata" in options:
tmpname = uuid.uuid1()
os.system("/usr/bin/write-mime-multipart --output=/tmp/%s %s " %
(tmpname, " ".join(options["userdata"])))
userdata = open("/tmp/%s" % tmpname)
tmpfile = "/tmp/%s" % uuid.uuid1()
content = warm.utils.multipart_content(*options["userdata"])
with open(tmpfile, "w+") as output:
output.write(content)
userdata = file(tmpfile)
whitelist = dict(
name=options.get("name"),
image=image.id,
@ -242,7 +244,6 @@ class Server(Base):
key_name=options.get("key"),
min_count=options.get("min_count"),
max_count=options.get("max_count"))
return self._agent.client.compute.servers.create(**whitelist)
def _PostExecute(self, options):

65
warm/utils.py Normal file
View File

@ -0,0 +1,65 @@
# Copyright 2013 Cloudwatt
#
# Author: Sahid Orentino Ferdjaoui <sahid.ferdjaoui@cloudwatt.com>
#
# 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.
#
"""Some utilities script to be used with warm."""
import os
import sys
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
MULTIPART_MAPPINGS = {
'#include' : 'text/x-include-url',
'#!' : 'text/x-shellscript',
'#cloud-config' : 'text/cloud-config',
'#upstart-job' : 'text/upstart-job',
'#part-handler' : 'text/part-handler',
'#cloud-boothook' : 'text/cloud-boothook'
}
def get_type(fname, deftype="text/plain"):
rtype = deftype
with open(fname, "rb") as f:
line = f.readline()
for s, mtype in MULTIPART_MAPPINGS.items():
if line.startswith(s):
rtype = mtype
break
return rtype
def multipart_content(*files):
"""Returns a mutlipart content.
Note:
This script was clearly inspired by write-mime-multipart.
"""
outer = MIMEMultipart()
for fname in files:
mtype = get_type(fname)
maintype, subtype = mtype.split('/', 1)
with open(fname) as f:
msg = MIMEText(f.read(), _subtype=subtype)
msg.add_header('Content-Disposition', 'attachment',
filename=os.path.basename(fname))
outer.attach(msg)
return outer.as_string()
if __name__ == "__main__":
print multipart_content(*sys.argv)

View File

@ -14,4 +14,4 @@
# License for the specific language governing permissions and limitations
# under the License.
#
__version__ = "0.1.26"
__version__ = "0.2.0"