From 98de220f782ea886c6d304d728cee168f78b341d Mon Sep 17 00:00:00 2001 From: Anders Ingemann Date: Sat, 4 Jun 2016 21:56:08 +0200 Subject: [PATCH] ec2: Add tasks for EC2 tuning Snatched from PR #256 by @JamesBromberger --- bootstrapvz/providers/ec2/__init__.py | 6 +++ .../ec2/assets/cloud-init/debian_cloud.conf | 1 + .../providers/ec2/assets/sysctl.d/tuning.conf | 15 +++++++ bootstrapvz/providers/ec2/tasks/tuning.py | 41 +++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 bootstrapvz/providers/ec2/assets/cloud-init/debian_cloud.conf create mode 100644 bootstrapvz/providers/ec2/assets/sysctl.d/tuning.conf create mode 100644 bootstrapvz/providers/ec2/tasks/tuning.py diff --git a/bootstrapvz/providers/ec2/__init__.py b/bootstrapvz/providers/ec2/__init__.py index 2058566..3dfe82e 100644 --- a/bootstrapvz/providers/ec2/__init__.py +++ b/bootstrapvz/providers/ec2/__init__.py @@ -8,6 +8,7 @@ import tasks.filesystem import tasks.boot import tasks.network import tasks.initd +import tasks.tuning from bootstrapvz.common.tasks import volume from bootstrapvz.common.tasks import filesystem from bootstrapvz.common.tasks import boot @@ -15,6 +16,7 @@ from bootstrapvz.common.tasks import grub from bootstrapvz.common.tasks import initd from bootstrapvz.common.tasks import loopback from bootstrapvz.common.tasks import kernel +from bootstrapvz.common.tasks import apt def validate_manifest(data, validator, error): @@ -65,6 +67,8 @@ def resolve_tasks(taskset, manifest): tasks.ami.AMIName, tasks.connection.Connect, + tasks.tuning.TuneSystem, + tasks.tuning.BlackListModules, boot.BlackListModules, boot.DisableGetTTYs, initd.AddExpandRoot, @@ -82,6 +86,8 @@ def resolve_tasks(taskset, manifest): taskset.add(tasks.network.EnableDHCPCDDNS) if manifest.release >= jessie: + taskset.add(apt.AddBackports) + taskset.add(tasks.tuning.SetCloudInitMountOptions) taskset.add(tasks.packages.AddWorkaroundGrowpart) taskset.add(initd.AdjustGrowpartWorkaround) taskset.add(grub.EnableSystemd) diff --git a/bootstrapvz/providers/ec2/assets/cloud-init/debian_cloud.conf b/bootstrapvz/providers/ec2/assets/cloud-init/debian_cloud.conf new file mode 100644 index 0000000..ed427ed --- /dev/null +++ b/bootstrapvz/providers/ec2/assets/cloud-init/debian_cloud.conf @@ -0,0 +1 @@ +mount_default_fields: [ None, None, "auto", "defaults,nofail", "0", "2" ] diff --git a/bootstrapvz/providers/ec2/assets/sysctl.d/tuning.conf b/bootstrapvz/providers/ec2/assets/sysctl.d/tuning.conf new file mode 100644 index 0000000..3090b76 --- /dev/null +++ b/bootstrapvz/providers/ec2/assets/sysctl.d/tuning.conf @@ -0,0 +1,15 @@ +vm.swappiness = 0 +vm.dirty_ratio = 80 +vm.dirty_background_ratio = 5 +vm.dirty_expire_centisecs = 12000 +net.core.somaxconn = 1000 +net.core.netdev_max_backlog = 5000 +net.core.rmem_max = 16777216 +net.core.wmem_max = 16777216 +net.ipv4.tcp_wmem = 4096 12582912 16777216 +net.ipv4.tcp_rmem = 4096 12582912 16777216 +net.ipv4.tcp_max_syn_backlog = 8096 +net.ipv4.tcp_slow_start_after_idle = 0 +net.ipv4.tcp_tw_reuse = 1 +net.ipv4.ip_local_port_range = 10240 65535 +kernel.sysrq = 0 diff --git a/bootstrapvz/providers/ec2/tasks/tuning.py b/bootstrapvz/providers/ec2/tasks/tuning.py new file mode 100644 index 0000000..0638a0e --- /dev/null +++ b/bootstrapvz/providers/ec2/tasks/tuning.py @@ -0,0 +1,41 @@ +from bootstrapvz.base import Task +from bootstrapvz.common import phases +from . import assets +from shutil import copy +import os + + +class TuneSystem(Task): + description = 'Tuning system for EC2' + phase = phases.system_modification + + @classmethod + def run(cls, info): + sysctl_src = os.path.join(assets, 'sysctl.d/tuning.conf') + sysctl_dst = os.path.join(info.root, 'etc/sysctl.d/01_ec2.conf') + copy(sysctl_src, sysctl_dst) + os.chmod(sysctl_dst, 0644) + + +class SetCloudInitMountOptions(Task): + description = 'Setting cloud-init default mount options' + phase = phases.system_modification + + @classmethod + def run(cls, info): + cloud_init_src = os.path.join(assets, 'cloud-init/debian_cloud.conf') + cloud_init_dst = os.path.join(info.root, 'etc/cloud/cloud.cfg.d/01_debian_cloud.conf') + copy(cloud_init_src, cloud_init_dst) + os.chmod(cloud_init_dst, 0644) + + +class BlackListModules(Task): + description = 'Blacklisting unused kernel modules' + phase = phases.system_modification + + @classmethod + def run(cls, info): + blacklist_path = os.path.join(info.root, 'etc/modprobe.d/blacklist.conf') + with open(blacklist_path, 'a') as blacklist: + blacklist.write(('blacklist i2c_piix4\n' + 'blacklist psmouse\n'))