bootstrap-vz/common/tasks/apt.py

99 lines
3.5 KiB
Python
Raw Normal View History

2013-07-01 22:51:28 +02:00
from base import Task
from common import phases
from common.tools import log_check_call
import network
import locale
2013-07-01 22:51:28 +02:00
import os
2013-07-01 22:52:54 +02:00
class AptSources(Task):
description = 'Adding aptitude sources'
phase = phases.system_modification
def run(self, info):
sources_path = os.path.join(info.root, 'etc/apt/sources.list')
with open(sources_path, 'w') as apt_sources:
apt_sources.write(('deb {apt_mirror} {release} main\n'
2013-07-01 23:25:11 +02:00
'deb-src {apt_mirror} {release} main\n'
.format(apt_mirror='http://http.debian.net/debian',
release=info.manifest.system['release'])))
2013-07-01 22:52:54 +02:00
apt_sources.write(('deb {apt_mirror} {release}/updates main\n'
2013-07-01 23:25:11 +02:00
'deb-src {apt_mirror} {release}/updates main\n'
.format(apt_mirror='http://security.debian.org/',
release=info.manifest.system['release'])))
2013-07-01 22:51:28 +02:00
class DisableDaemonAutostart(Task):
description = 'Disabling daemon autostart'
2013-07-01 22:51:28 +02:00
phase = phases.system_modification
def run(self, info):
rc_policy_path = os.path.join(info.root, 'usr/sbin/policy-rc.d')
with open(rc_policy_path, 'w') as rc_policy:
2013-07-07 17:06:44 +02:00
rc_policy.write(('#!/bin/sh\n'
'exit 101'))
2013-07-01 22:51:28 +02:00
import stat
os.chmod(rc_policy_path,
stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
stat.S_IRGRP | stat.S_IXGRP |
stat.S_IROTH | stat.S_IXOTH)
class AptUpdate(Task):
description = 'Updating the package cache'
phase = phases.system_modification
predecessors = [locale.GenerateLocale, AptSources]
successors = [network.RemoveDNSInfo]
def run(self, info):
log_check_call(['/usr/sbin/chroot', info.root, '/usr/bin/apt-get', 'update'])
log_check_call(['/usr/sbin/chroot', info.root, '/usr/bin/apt-get',
'--fix-broken',
'--assume-yes',
'install'])
log_check_call(['/usr/sbin/chroot', info.root, '/usr/bin/apt-get', '--assume-yes', 'upgrade'])
class AptUpgrade(Task):
description = 'Upgrading packages and fixing broken dependencies'
phase = phases.system_modification
predecessors = [AptUpdate, DisableDaemonAutostart]
successors = [network.RemoveDNSInfo]
def run(self, info):
2013-07-07 19:38:34 +02:00
log_check_call(['/usr/sbin/chroot', info.root, '/usr/bin/apt-get', 'update'])
log_check_call(['/usr/sbin/chroot', info.root, '/usr/bin/apt-get',
'--fix-broken',
'--assume-yes',
'install'])
log_check_call(['/usr/sbin/chroot', info.root, '/usr/bin/apt-get', '--assume-yes', 'upgrade'])
2013-07-07 18:03:49 +02:00
class PurgeUnusedPackages(Task):
description = 'Removing unused packages'
phase = phases.system_cleaning
def run(self, info):
2013-07-07 19:38:34 +02:00
log_check_call(['/usr/sbin/chroot', info.root, '/usr/bin/apt-get', 'autoremove', '--purge'])
2013-07-07 18:03:49 +02:00
class AptClean(Task):
description = 'Clearing the aptitude cache'
phase = phases.system_cleaning
def run(self, info):
2013-07-07 19:38:34 +02:00
log_check_call(['/usr/sbin/chroot', info.root, '/usr/bin/apt-get', 'clean'])
2013-07-07 18:03:49 +02:00
2013-07-07 18:07:15 +02:00
lists = os.path.join(info.root, 'var/lib/apt/lists')
for list_file in [os.path.join(lists, f) for f in os.listdir(lists)]:
2013-07-07 18:03:49 +02:00
if os.path.isfile(list_file):
os.remove(list_file)
class EnableDaemonAutostart(Task):
description = 'Re-enabling daemon autostart after installation'
phase = phases.system_cleaning
def run(self, info):
os.remove(os.path.join(info.root, 'usr/sbin/policy-rc.d'))