Created
November 16, 2013 14:32
-
-
Save dir01/7500769 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf8 -*- | |
from datetime import timedelta | |
from fabric.context_managers import settings | |
from fabric.contrib.files import sed, exists | |
from fabric.operations import sudo, run, put | |
from fabric.state import env | |
useful_software_list = [ | |
'htop', | |
] | |
def setup_service_autorun(service_name): | |
""" Добавляет сервис в автозапуск """ | |
sudo('update-rc.d {0} defaults'.format(service_name)) | |
def setup_passworldless_sudo(): | |
""" Позволяет не вводить пароль пользователя для sudo""" | |
filename = '/etc/sudoers.d/{user}'.format(**env) | |
sudo('echo "{user} ALL=(ALL) NOPASSWD:ALL" > {0}'.format(filename, **env)) | |
sudo('chmod 440 {}'.format(filename)) | |
def setup_shell(): | |
""" Устанавливает zsh как шелл по умолчанию, """ | |
aptitude_update() | |
aptitude_install('zsh') | |
aptitude_install('git-core') | |
run('wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | zsh') | |
sudo('chsh -s `which zsh` `whoami`') | |
put(local_path='~/.zshrc', remote_path='~') | |
def install_useful_software(): | |
_install_add_apt_repository_command() | |
aptitude_install(*useful_software_list) | |
def install_java(): | |
_install_add_apt_repository_command() | |
aptitude_add_repo('ppa:webupd8team/java') | |
aptitude_update() | |
aptitude_install('oracle-java8-installer', pty=False, quiet=False) | |
def _install_add_apt_repository_command(): | |
aptitude_install('python-software-properties') | |
# Aptitude stuff | |
_aptitude_updated_flag_file = '/tmp/.aptitude_updated' | |
_aptitude_update_max_age = timedelta(days=15) | |
def _is_aptitude_update_required(): | |
if not exists(_aptitude_updated_flag_file): | |
return True | |
flag_modification_epoch = int(run( | |
'stat -c %Y {}'.format(_aptitude_updated_flag_file), quiet=True | |
)) | |
current_server_epoch = int(run('date +%s', quiet=True)) | |
flag_age = timedelta(seconds=current_server_epoch - flag_modification_epoch) | |
return flag_age > _aptitude_update_max_age | |
def _force_aptitude_update(): | |
if exists(_aptitude_updated_flag_file): | |
sudo('rm {}'.format(_aptitude_updated_flag_file)) | |
def aptitude(command, **kwargs): | |
quiet = '' if kwargs.pop('quiet', None) is False else '--quiet' | |
sudo('aptitude {} --assume-yes {}'.format(command, quiet), **kwargs) | |
def aptitude_install(*packages, **kwargs): | |
aptitude('install %s' % ' '.join(packages), **kwargs) | |
def aptitude_update(): | |
if _is_aptitude_update_required(): | |
aptitude('update') | |
run('touch {}'.format(_aptitude_updated_flag_file)) | |
else: | |
print 'Skipping aptitude update' | |
def aptitude_add_repo(deb_str, key_url=None, custom_list_file=None): | |
if 'ppa' in deb_str: # На новый манер | |
sudo('add-apt-repository {} --yes'.format(deb_str)) | |
_force_aptitude_update() | |
return | |
elif not key_url: | |
raise ValueError('Давайте-ка как-нибудь или PPA, или ключик сообщите') | |
# По старинке | |
if custom_list_file: | |
destination = '/etc/apt/sources.list.d/{}'.format(custom_list_file) | |
operation = '>' | |
else: | |
destination = '/etc/apt/sources.list' | |
operation = '>>' | |
sudo('echo "{deb_str}" {operation} {destination}'.format(**locals())) | |
sudo('wget --quiet -O - "{key_url}" | sudo apt-key add -'.format(key_url=key_url)) | |
_force_aptitude_update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment