Implemented GenerateLocales and SetTimezone

This commit is contained in:
Anders Ingemann 2013-07-01 21:42:40 +02:00
parent d3ab01f88f
commit 0ed091ca1c
3 changed files with 53 additions and 0 deletions

View file

@ -19,3 +19,16 @@ def log_command(command, logger):
if process.poll() is not None:
break
return process.returncode
def sed_i(file_path, pattern, subst):
from tempfile import mkstemp
from shutil import move
from os import close
temp_fd, temp_path = mkstemp()
with open(temp_path, 'w') as new_file:
with open(file_path) as old_file:
for line in old_file:
new_file.write(line.replace(pattern, subst))
close(temp_fd)
move(temp_path, file_path)

View file

@ -6,6 +6,7 @@ from tasks import host
from tasks import ebs
from tasks import filesystem
from tasks import bootstrap
from tasks import config
def initialize():
@ -29,6 +30,8 @@ def tasks(tasklist, manifest):
tasklist.add(bootstrap.MakeTarball())
tasklist.add(bootstrap.Bootstrap())
tasklist.add(filesystem.MountSpecials())
tasklist.add(config.GenerateLocale())
tasklist.add(config.SetTimezone())
from common.tasks import TriggerRollback
tasklist.add(TriggerRollback())

View file

@ -0,0 +1,37 @@
from base import Task
from common import phases
from common.exceptions import TaskError
from common.tools import log_command
import os.path
import logging
log = logging.getLogger(__name__)
class GenerateLocale(Task):
description = 'Generating the selected locale'
phase = phases.system_modification
def run(self, info):
from common.tools import sed_i
locale_gen = os.path.join(info.root, 'etc/locale.gen')
locale_str = '{locale}.{charmap} {charmap}'.format(locale=info.manifest.system['locale'],
charmap=info.manifest.system['charmap'])
search = '# ' + locale_str
sed_i(locale_gen, search, locale_str)
if log_command(['chroot', info.root, 'dpkg-reconfigure', '--priority=critical', 'locales'], log) != 0:
raise TaskError('Failed to regenerate locales')
class SetTimezone(Task):
description = 'Setting the selected timezone'
phase = phases.system_modification
def run(self, info):
from shutil import copy
tz_path = os.path.join(info.root, 'etc/timezone')
timezone = info.manifest.system['timezone']
with open(tz_path, 'w') as tz_file:
tz_file.write(timezone)
zoneinfo_path = os.path.join(info.root, '/usr/share/zoneinfo', timezone)
localtime_path = os.path.join(info.root, 'etc/localtime')
copy(zoneinfo_path, localtime_path)