#!/usr/bin/python
#
#    user-data - Generate user-data needed by cloud-init
#
#    Copyright (C) 2011 Canonical
#
#    Author:
#               Marc Cluet <marc.cluet@canonical.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import random
import string
import ConfigParser
import StringIO

length = 8

try:
    provisioning_conf_fh = open('/etc/orchestra/conf.d/provisioning.conf', 'r')
except Exception:
    exit(1)
provisioning_conf = ConfigParser.ConfigParser()
provisioning_conf.readfp(StringIO.StringIO(''.join(i.lstrip() for i in provisioning_conf_fh.readlines())))
provisioning_conf_fh.close()

try:
   config_hostnamestart = provisioning_conf.get('provisioning', 'hostname.root')
except Exception:
   config_hostnamestart = None

if config_hostnamestart is None:
    hostnamestart = 'ubuntu'
else:
    hostnamestart = config_hostnamestart

hostnamerandom = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(length))

print "Content-type: text/plain\n"
print "instance-id: i-12345678"
print "local-hostname: %s-%s" % (hostnamestart, hostnamerandom)
