-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Сергей Самойлов
committed
Aug 10, 2016
1 parent
b44be76
commit c584735
Showing
4 changed files
with
141 additions
and
124 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,10 @@ | ||
# coding: utf-8 | ||
import os | ||
from fabric.state import env | ||
|
||
import yaml | ||
from fabric.decorators import roles | ||
from fab.main import ServerSSH | ||
from settings.settings_class import Settings | ||
from fablib import init, deploy, rollback | ||
|
||
# Yaml-файл содержит необходимые настройки, в том числе пароли. | ||
# Нужен тут только для того, чтобы не хранить эту информацию в коммите | ||
with open('settings/productionserver.yaml') as f: | ||
prod_yaml = yaml.load(f.read()) | ||
|
||
# Аналогично | ||
with open('settings/testserver.yaml') as f: | ||
test_yaml = yaml.load(f.read()) | ||
|
||
# ----------------------------- Настройка продакшена --------------------------# | ||
# Создание объекта с настройками для продакшена | ||
prod_settings = Settings() | ||
prod_settings.USER = prod_yaml['USER'] | ||
prod_settings.PASSWORD = prod_yaml['PASSWORD'] | ||
|
||
# Создание объекта - удаленного сервера | ||
production_server = ServerSSH(prod_settings) | ||
|
||
|
||
# Инициализация production-сервера | ||
@roles(prod_settings.SERVER) | ||
def init_production(): | ||
# Метод create_dir нужен для создания рабочего каталога на сервере | ||
production_server.create_dir() | ||
# Метод init базового класса ServerSSH инициализирует git, устанавливает виртуальное окружение | ||
# и скачивает последнюю версию проекта | ||
production_server.init() | ||
# Метод pip позволяет выполнять команды от имени pip вируального окружения сервера | ||
production_server.pip('install -r requirements.txt') | ||
# Метод python позволяет выполнять команды "от лица" python виртуального окружения сервера | ||
production_server.python('hello.py') # В данном примере запускает скрипт hello.py, | ||
# но вместо него можно было запустить и скрипт с миграцией базы данных | ||
|
||
# Метод control_service позволяет совершать операции restart | stop | start с каким - либо сервисом на сервере | ||
# production_server.control_service('restart', 'rabbitmq-server' ) # Сервис можно указать явно | ||
production_server.control_service('restart') # Или просто указать дейтвие, | ||
# которое будете применено к серверу по умолчанию | ||
|
||
|
||
# Обновление production-сервера | ||
@roles(prod_settings.SERVER) | ||
def deploy_production(): | ||
production_server.deploy() | ||
|
||
|
||
# Обновление виртуального окружения | ||
@roles(prod_settings.SERVER) | ||
def venv_update_production(): | ||
production_server.venv_update() | ||
|
||
|
||
# --------------------------------------------------------------------------------# | ||
|
||
|
||
# ----------------------------- Настройка тестового сервера ----------------------# | ||
|
||
# Изменение настроек по умолчанию. Можно вынести в отдельный файл. | ||
# Для этого можно в отдельном файле создать класс, который будет наследоваться от класса Settings. | ||
# Тут создается класс только для примера. | ||
class TestServerSettings(Settings): | ||
SERVER = 'test' | ||
USER = test_yaml['USER'] | ||
PASSWORD = test_yaml['PASSWORD'] | ||
REPOSITORY = test_yaml['REPOSITORY'] | ||
HOST = test_yaml['HOST'] | ||
PATH = test_yaml['PATH'] | ||
|
||
|
||
# Создание еще одного объекта с настройками для тестового сервера | ||
test_settings = TestServerSettings() | ||
|
||
# Создание тестового сервера | ||
test_server = ServerSSH(test_settings) | ||
|
||
|
||
@roles(test_settings.SERVER) | ||
def init_test_server(): | ||
test_server.init() | ||
|
||
|
||
@roles(test_settings.SERVER) | ||
def deploy_test_server(): | ||
test_server.deploy() | ||
# --------------------------------------------------------------------------------# | ||
# fablib.before_init = lambda: 'Hello there' | ||
|
||
def before_init(): | ||
return 'Hello there' | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# coding: utf-8 | ||
import yaml | ||
from fabric.api import env, run | ||
|
||
from fab.serverssh import ServerSSH | ||
|
||
env.hosts = [] | ||
|
||
|
||
def before_init(): | ||
pass | ||
|
||
|
||
def after_init(): | ||
pass | ||
|
||
|
||
def before_deploy(): | ||
pass | ||
|
||
|
||
def after_deploy(): | ||
pass | ||
|
||
|
||
def before_rollback(): | ||
pass | ||
|
||
|
||
def after_rollback(): | ||
pass | ||
|
||
|
||
def get_hosts(filename): | ||
with open(filename) as f: | ||
config = yaml.load(f.read()) | ||
for host in config['HOSTS']: | ||
yield host | ||
# env.hosts += [host] | ||
|
||
|
||
def init(service=None): | ||
before_init() | ||
hosts = get_hosts(env.rcfile) | ||
try: | ||
while True: | ||
host = next(hosts) | ||
server = ServerSSH(env.rcfile, host) | ||
server.create_dir() | ||
server.do('sudo apt-get install gcc') | ||
server.do('sudo apt-get install build-essential autoconf libtool pkg-config python-opengl python-imaging ' + | ||
'python-pyrex python-pyside.qtopengl idle-python2.7 qt4-dev-tools qt4-designer libqtgui4 ' + | ||
'libqtcore4 libqt4-xml libqt4-test libqt4-script libqt4-network libqt4-dbus python-qt4 ' + | ||
'python-qt4-gl libgle3 python-dev libxml2-dev libxslt1-dev libevent-dev') | ||
server.init() | ||
server.pip_install_requirements() | ||
if not service: | ||
server.control_service(service=service, action='start') | ||
except StopIteration: | ||
pass | ||
after_init() | ||
|
||
|
||
def deploy(service=None): | ||
before_deploy() | ||
server = ServerSSH(env.rcfile) | ||
server.deploy() | ||
if not service: | ||
server.control_service(service=service, action='restart') | ||
after_deploy() | ||
|
||
|
||
def rollback(service=None): | ||
before_rollback() | ||
server = ServerSSH(env.rcfile) | ||
server.rollback() | ||
if not service: | ||
server.control_service(service=service, action='restart') | ||
after_rollback() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
SERVER: "production" | ||
PORT: 22 | ||
BRANCH: "dev" | ||
WORK_SERVER: "rabbitmq-server" | ||
VENV_REQUIREMENTS: "requirements.txt" | ||
|
||
USER: "sebuntu-1" | ||
PASSWORD: "njkelfrjq" | ||
HOST: "192.168.56.101" | ||
PATH: "/home/sebuntu-1/work/fabtest/" | ||
REPOSITORY: "https://github.com/faoxis/gittest.git" | ||
PATH_KEY: '~/.ssh/id_rsa' |