Commit 654bd899 authored by Aleksey Shirokih's avatar Aleksey Shirokih
Browse files

add config for tower

parent e189bd34
path:
db: /var/lib/noc-tower/db/config.db
deploy_keys: /etc/noc-tower/keys/id_rsa
noc_keys: /etc/noc-tower/noc_keys
repo: /var/lib/noc-tower/repo
joblog: /var/log/noc-tower/jobs
playbooks: /var/lib/noc-tower/playbooks
data: /var/lib/noc-tower/data
crashinfo: /var/lib/crashinfo/collect
cp: /var/log/noc-tower/ansible/cp
ui_root: /usr/lib/python2.7/site-packages/tower/ui/
cache: /var/cache/noc-tower
hg_path: /bin/hg
web:
address: 0.0.0.0
port: 8888
children: 1
url: http://example.com/
repo_url: http://example.com/hg
installation_name: Unconfigured installation
......@@ -101,7 +101,10 @@ class DeployHandler(BaseHandler):
# Generate ssh keys
self.env.build_ssh_keys()
# Run playbook
bin_path = os.path.abspath(os.path.join(os.getcwd(), "bin"))
if os.path.isdir(os.path.abspath(os.path.join(os.getcwd(), "bin"))):
bin_path = os.path.abspath(os.path.join(os.getcwd(), "bin"))
else:
bin_path = os.path.abspath("/usr/bin")
ansible_ssh_cp = os.path.join(
"/tmp/tower-%%r-%%h-%%r"
)
......
......@@ -20,6 +20,7 @@ from base import API, api
from tower.models.db import db
from tower.models.environment import Environment
from tower.models.pulllog import PullLog
from tower.models.settings import Settings
logger = logging.getLogger(__name__)
......@@ -27,6 +28,7 @@ logger = logging.getLogger(__name__)
class PullAPI(API):
name = "pull"
executor = ThreadPoolExecutor(2)
HG_PATH = Settings.config["path"]["hg_path"]
@api
def is_pulled(self, env_id):
......@@ -124,7 +126,7 @@ class PullAPI(API):
# Clone directory
subprocess.check_call(
[
"./bin/hg",
self.HG_PATH,
"-q",
"clone",
"-b", env.branch,
......@@ -140,7 +142,7 @@ class PullAPI(API):
logger.info("Updating %s", env.repo_path)
subprocess.check_call(
[
"./bin/hg",
self.HG_PATH,
"-q",
"--cwd=%s" % env.repo_path,
"pull",
......@@ -157,7 +159,7 @@ class PullAPI(API):
rev = env.changeset
subprocess.check_call(
[
"./bin/hg",
self.HG_PATH,
"-q",
"--cwd=%s" % env.repo_path,
"archive",
......@@ -175,7 +177,7 @@ class PullAPI(API):
# Clone directory
subprocess.check_call(
[
"./bin/hg",
self.HG_PATH,
"-q",
"clone",
"-b", env.custom_branch,
......@@ -191,7 +193,7 @@ class PullAPI(API):
logger.info("Updating %s", env.custom_repo_path)
subprocess.check_call(
[
"./bin/hg",
self.HG_PATH,
"-q",
"--cwd=%s" % env.custom_repo_path,
"pull",
......
......@@ -13,12 +13,14 @@ import mercurial.ui
from mercurial.hgweb.hgwebdir_mod import hgwebdir
import tornado.web
import tornado.wsgi
from tower.models.settings import Settings
logger = logging.getLogger(__name__)
class RepoHandler(tornado.web.FallbackHandler):
def initialize(self):
repo = "/".join([Settings.config["path"]["repo"],"*"])
ui = mercurial.ui.ui()
ui.setconfig('ui', 'report_untrusted', 'off', 'hgwebdir')
ui.setconfig('ui', 'nontty', 'true', 'hgwebdir')
......@@ -27,7 +29,7 @@ class RepoHandler(tornado.web.FallbackHandler):
ui.setconfig("web", "logourl", "http://nocproject.org/", "hgwebdir")
# @todo: Set proper paths, use prefix
fallback = tornado.wsgi.WSGIContainer(
hgwebdir([("/", "var/tower/repo/*")], ui)
hgwebdir([("/", repo)], ui)
)
super(RepoHandler, self).initialize(fallback)
......
......@@ -25,7 +25,7 @@ logger = logging.getLogger("ui")
class UIHandler(tornado.web.RequestHandler):
hash = None
CACHE_ROOT = "var/tower/cache"
CACHE_ROOT = Settings.config["path"]["cache"]
def initialize(self, path, *args, **kwargs):
self.root = path
......
......@@ -43,13 +43,15 @@ def run():
)
tornado.options.define(
"listen",
default=os.environ.get("TOWER_LISTEN", "0.0.0.0:8888"),
default=os.environ.get("TOWER_LISTEN",
":".join([Settings.config["web"]["address"],
str(Settings.config["web"]["port"])])),
help="Listen on specified address",
type=str
)
tornado.options.define(
"children",
default=os.environ.get("TOWER_CHILDREN", 1),
default=os.environ.get("TOWER_CHILDREN", Settings.config["web"]["children"]),
help="Run several processes",
type=int
)
......@@ -59,7 +61,7 @@ def run():
Migration.migrate()
logger.info("Loading service")
# Get static files path
ui_root = os.path.join(tower.__path__[0], "ui")
ui_root = Settings.config["path"]["ui_root"]
logger.info("Serving UI files from %s", ui_root)
settings = {
"template_path": os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "templates")),
......@@ -79,16 +81,17 @@ def run():
"path": ui_root
})
], **settings)
if ":" in tornado.options.options.listen:
if not tornado.options.options.listen:
addr = Settings.config["web"]["address"]
port = c
elif ":" in tornado.options.options.listen:
addr, port = tornado.options.options.listen.split(":")
port = int(port)
else:
addr = None
port = int(tornado.options.options.listen)
server = tornado.httpserver.HTTPServer(app, xheaders=True)
server.bind(port, address=addr)
server.start(tornado.options.options.children)
logger.info("Service is ready")
logger.info("Service is ready listeing on http://%s:%s with max childrens %s",
addr,port, tornado.options.options.children)
logging.root.setLevel(logging.DEBUG)
tornado.ioloop.IOLoop.current().start()
......
......@@ -7,12 +7,17 @@
##----------------------------------------------------------------------
## Python modules
from os.path import realpath, join, dirname, abspath
import logging
import yaml
## Third-party packages
from peewee import SqliteDatabase
dbpath = realpath(join(dirname(abspath(__file__)), '../../../../../var/tower/db/config.db'))
logger = logging.getLogger(__name__)
with open("/etc/noc-tower/tower.yml") as f:
config = yaml.load(f)
dbpath = config["path"]["db"]
logger.info("Will work with %s", dbpath)
db = SqliteDatabase(dbpath,
autocommit=False, threadlocals=True)
......
......@@ -433,11 +433,11 @@ class Environment(Model):
@property
def playbook_path(self):
return os.path.join("var", "tower", "playbooks", self.name)
return os.path.join(Settings.config["path"]["playbooks"], self.name)
@property
def services_path(self):
return os.path.join("var", "tower", "playbooks", self.name,
return os.path.join(Settings.config["path"]["playbooks"], self.name,
"ansible", "config", "services.yml")
@property
......@@ -446,16 +446,16 @@ class Environment(Model):
@property
def repo_path(self):
return os.path.join("var", "tower", "repo", self.repo_hash)
return os.path.join(Settings.config["path"]["repo"], self.repo_hash)
@property
def custom_repo_path(self):
return os.path.join("var", "tower", "repo", self.custom_repo_hash)
return os.path.join(Settings.config["path"]["repo"], self.custom_repo_hash)
@property
def data_path(self):
return os.path.abspath(
os.path.join("var", "tower", "data", self.name)
os.path.join(Settings.config["path"]["data"], self.name)
)
@property
......@@ -464,13 +464,16 @@ class Environment(Model):
return os.path.abspath(
os.path.join("var", "tower", "data", "deploy_keys", "id_rsa")
)
elif os.path.exists(Settings.config["path"]["deploy_keys"]):
return Settings.config["path"]["deploy_keys"]
elif os.path.exists(os.path.expanduser("~/.ssh/id_rsa")):
return os.path.expanduser("~/.ssh/id_rsa")
@property
def ssh_keys_path(self):
return os.path.abspath(
os.path.join("var", "tower", "ssh", self.name)
os.path.join(Settings.config["path"]["noc_keys"], self.name)
)
def get_services_description(self):
......
......@@ -14,6 +14,7 @@ from peewee import (Model, CharField, TextField, ForeignKeyField,
# Tower modules
from db import db
from environment import Environment
from tower.models.settings import Settings
class JobLog(Model):
......@@ -35,7 +36,7 @@ class JobLog(Model):
@property
def log_path(self):
return os.path.join("var", "tower", "log", "jobs", "%s.log" % self.id)
return os.path.join(Settings.config["path"]["joblog"], "%s.log" % self.id)
def append_log(self, data):
with open(self.log_path, "a") as f:
......
......@@ -9,6 +9,7 @@
# Python modules
import base64
import json
import yaml
import os
......@@ -26,10 +27,40 @@ class Settings(Model):
key = CharField(primary_key=True)
value = TextField()
try:
with open("/etc/noc-tower/tower.yml") as f:
config = yaml.load(f)
except IOError:
default = """
path:
db: /opt/tower/var/tower/db/config.db
deploy_keys: /opt/tower/var/tower/data/deploy_keys/id_rsa
noc_keys: /opt/tower/var/tower/data/
repo: /opt/tower/var/tower/repo/
joblog: /opt/tower/var/tower/log/jobs/
playbooks: /opt/tower/var/tower/playbooks
data: /opt/tower/var/tower/data
crashinfo: /opt/tower/var/tower/log/crashinfo/
cp: /opt/tower/var/tower/ansible/cp
ui_root: /opt/tower/lib/python2.7/site-packages/tower/ui
cache: /opt/tower/var/tower/cache/
hg_path: /bin/hg
web:
address: 127.0.0.1
port: 8888
children: 1
url: http://example.com/
repo_url: http://example.com/hg
installation_name: Unconfigured installation
"""
config = yaml.load(default)
DEFAULTS = {
"url": "http://example.com/",
"repo_url": "http://example.com/hg",
"installation_name": "Unconfigured installation"
"url": config["web"]["url"],
"repo_url": config["web"]["repo_url"],
"installation_name": config["web"]["installation_name"]
}
@classmethod
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment