def install(): packages([ 'build-essential', 'php5', 'php5-mysql', ], update=False) mysql(password="******") apache() stopped('apache2') site_disabled('default') site( 'mylampvm.dev', template_source=full_path + '/templates/apache2/vhost.tpl', port=80, server_name='mylampvm.dev', document_root='/srv', ) template_file( template_source = full_path + '/templates/apache2/envvars.tpl', path='/etc/apache2/envvars', context = { 'apache_run_user': '******', 'apache_run_group': 'vagrant', }, owner = 'root', group = 'root', use_sudo=True ) directory('/var/lock/apache2', True, 'vagrant', 'vagrant') # site_enabled('mylampvm.dev') restarted('apache2')
def install(): packages([ 'build-essential', 'php5', 'php5-mysql', ], update=False) mysql(password="******") apache() stopped('apache2') site_disabled('default') site( 'mylampvm.dev', template_source=full_path + '/templates/apache2/vhost.tpl', port=80, server_name='mylampvm.dev', document_root='/srv', ) template_file(template_source=full_path + '/templates/apache2/envvars.tpl', path='/etc/apache2/envvars', context={ 'apache_run_user': '******', 'apache_run_group': 'vagrant', }, owner='root', group='root', use_sudo=True) directory('/var/lock/apache2', True, 'vagrant', 'vagrant') # site_enabled('mylampvm.dev') restarted('apache2')
def site(server_name, template_contents=None, template_source=None, enabled=True, check_config=True, **kwargs): """ Require an nginx site. You must provide a template for the site configuration, either as a string (*template_contents*) or as the path to a local template file (*template_source*). :: from fabtools import require CONFIG_TPL = ''' server { listen %(port)d; server_name %(server_name)s %(server_alias)s; root %(docroot)s; access_log /var/log/nginx/%(server_name)s.log; }''' require.nginx.site('example.com', template_contents=CONFIG_TPL, port=80, server_alias='www.example.com', docroot='/var/www/mysite', ) .. seealso:: :py:func:`fabtools.require.files.template_file` """ if not is_installed('nginx-common'): # nginx-common is always installed if Nginx exists server() config_filename = '/etc/nginx/sites-available/%s.conf' % server_name context = { 'port': 80, } context.update(kwargs) context['server_name'] = server_name template_file(config_filename, template_contents, template_source, context, use_sudo=True) link_filename = '/etc/nginx/sites-enabled/%s.conf' % server_name if enabled: if not is_link(link_filename): run_as_root("ln -s %(config_filename)s %(link_filename)s" % locals()) # Make sure we don't break the config if check_config: with settings(hide('running', 'warnings'), warn_only=True): if run_as_root('nginx -t').failed: run_as_root("rm %(link_filename)s" % locals()) message = red("Error in %(server_name)s nginx site config (disabling for safety)" % locals()) abort(message) else: if is_link(link_filename): run_as_root("rm %(link_filename)s" % locals()) reload_service('nginx')
def site(server_name, template_contents=None, template_source=None, enabled=True, check_config=True, **kwargs): """ Require an nginx site """ server() config_filename = '/etc/nginx/sites-available/%s.conf' % server_name context = { 'port': 80, } context.update(kwargs) context['server_name'] = server_name template_file(config_filename, template_contents, template_source, context, use_sudo=True) link_filename = '/etc/nginx/sites-enabled/%s.conf' % server_name if enabled: if not is_link(link_filename): sudo("ln -s %(config_filename)s %(link_filename)s" % locals()) # Make sure we don't break the config if check_config: with settings(hide('running', 'warnings'), warn_only=True): if sudo("nginx -t").return_code > 0: print red("Error in %(server_name)s nginx site config (disabling for safety)" % locals()) sudo("rm %(link_filename)s" % locals()) else: if is_link(link_filename): sudo("rm %(link_filename)s" % locals()) sudo("/etc/init.d/nginx reload")
def process(name, template_contents=None, template_source=None, **kwargs): """ Require a supervisor process """ deb.package('supervisor') config_filename = '/etc/supervisor/conf.d/%s.conf' % name context = {} context.update(kwargs) context['name'] = name if (template_contents is None) and (template_source is None): template_contents = DEFAULT_TEMPLATE template_file(config_filename, template_contents, template_source, context, use_sudo=True) reload_config() if process_status(name) == 'STOPPED': start_process(name)
def site(server_name, template_contents=None, template_source=None, enabled=True, check_config=True, **kwargs): """ Require an nginx site. You must provide a template for the site configuration, either as a string (*template_contents*) or as the path to a local template file (*template_source*). :: from fabtools import require CONFIG_TPL = ''' server { listen %(port)d; server_name %(server_name)s %(server_alias)s; root %(docroot)s; access_log /var/log/nginx/%(server_name)s.log; }''' require.nginx.site('example.com', template_contents=CONFIG_TPL, port=80, server_alias='www.example.com', docroot='/var/www/mysite', ) .. seealso:: :py:func:`fabtools.require.files.template_file` """ server() config_filename = '/etc/nginx/sites-available/%s.conf' % server_name context = { 'port': 80, } context.update(kwargs) context['server_name'] = server_name template_file(config_filename, template_contents, template_source, context, use_sudo=True) link_filename = '/etc/nginx/sites-enabled/%s.conf' % server_name if enabled: if not is_link(link_filename): run_as_root("ln -s %(config_filename)s %(link_filename)s" % locals()) # Make sure we don't break the config if check_config: with settings(hide('running', 'warnings'), warn_only=True): if run_as_root('nginx -t').failed: run_as_root("rm %(link_filename)s" % locals()) message = red("Error in %(server_name)s nginx site config (disabling for safety)" % locals()) abort(message) else: if is_link(link_filename): run_as_root("rm %(link_filename)s" % locals()) reload_service('nginx')
def site(config_name, template_contents=None, template_source=None, enabled=True, check_config=True, **kwargs): """ Require an Apache site. You must provide a template for the site configuration, either as a string (*template_contents*) or as the path to a local template file (*template_source*). :: from fabtools import require CONFIG_TPL = ''' <VirtualHost *:%(port)s> ServerName %(hostname})s DocumentRoot %(document_root)s <Directory %(document_root)s> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> </VirtualHost> ''' require.apache.site( 'example.com', template_contents=CONFIG_TPL, port=80, hostname='www.example.com', document_root='/var/www/mysite', ) .. seealso:: :py:func:`fabtools.require.files.template_file` """ server() config_filename = '/etc/apache2/sites-available/%s' % _get_config_name(config_name) context = { 'port': 80, } context.update(kwargs) context['config_name'] = config_name template_file(config_filename, template_contents, template_source, context, use_sudo=True) if enabled: enable_site(config_name) else: disable_site(config_name) if check_config: with settings(hide('running', 'warnings'), warn_only=True): if run_as_root('apache2ctl configtest').failed: disable_site(config_name) message = red("Error in %(config_name)s apache site config (disabling for safety)" % locals()) abort(message) reload_service('apache2')
def push(): """ This function for create django site project work flow on remote server. Django site source cloning from remote git repository. NOTE: This function may be used in other fab file. For this need setup global `env` dict. **`env` settings** env.user - deploy user name (use for ssh) env.password - deploy user password (use for ssh) env.hosts - list deploy hosts (use for ssh) env.domain - django site domain (DNS) use for: - nginx settings - uWSGI start user - project dir name env.repository - remote git repository url, use for git clone site source env.no_input_mode - in this variable True use no input deploy mode. If no_input_mode==True using follow strategy: Abort if env.domain (env.repository) value not set or invalid. And using default confirm() value if needed. """ # cwd => ./deploy env.lcwd = os.path.abspath(os.path.dirname(__file__)) require('no_input_mode') #env.no_input_mode = False if env.no_input_mode: def confirm_local(question, default=True): puts(question) puts("Use no_input_mode [default: {0}]".format("Y" if default else "N")) return default confirm = confirm_local else: confirm = confirm_global validate = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$" if not env.get("domain"): if env.no_input_mode: abort("Need set env.domain !") else: prompt("Project DNS url: ", "domain", env.get('domain_default', ''), validate=validate) else: if not re.findall(validate, env.domain): abort("Invalid env.domain !") if not env.get("repository"): if env.no_input_mode: env.repository = env.repository_default else: prompt("Deploy from: ", "repository", env.get('repository_default', '')) require('repository', 'domain') puts("Deploy site: {0} \nFrom: {1}".format(env.domain, env.repository)) DOMAIN_WITHOUT_DOT = env.domain.replace('.', '_') env.project_user = DOMAIN_WITHOUT_DOT env.project_group = DOMAIN_WITHOUT_DOT env.project_dir_name = DOMAIN_WITHOUT_DOT env.root = posixpath.join(PROJECTS_ROOT, env.project_dir_name) env.debug = True deb.packages(['git']) files.directory(PROJECTS_ROOT, use_sudo=True, owner='root', group='root', mode='755') with cd(PROJECTS_ROOT): # pip cache files.directory('.pip.cache', use_sudo=True, owner='deploy', group='deploy', mode='755') pip_cache_dir = posixpath.join(PROJECTS_ROOT, '.pip.cache') # proj dir create if is_dir(env.project_dir_name) and confirm("proj dir exist! abort ?", default=False): return files.directory(env.project_dir_name, use_sudo=True, owner='root', group='root', mode='755') # proj user create if not fabtools.user.exists(env.project_user): fabtools.user.create(env.project_user, home=env.root, group=env.project_group, create_home=False, system=True, shell='/bin/false', create_group=True) # proj infrastructure with cd(env.project_dir_name): # proj source if not is_dir('src') or confirm("proj src exist! [rm all and re clone / git pull]?", default=False): files.directory('src', use_sudo=True, owner='deploy', group='deploy', mode='755') with cd('src'): sudo('rm -Rf .??* *') sudo('git clone {repository:s} .'.format(env), user='******') else: with cd('src'): sudo('git pull', user='******') # proj virtual env if not is_dir('.virtualenvs') or confirm("proj venv dir exist! [rm all and recreate / repeat install]?", default=False): files.directory('.virtualenvs', use_sudo=True, owner='deploy', group='deploy', mode='755') with cd('.virtualenvs'): sudo('rm -Rf .??* *') python.virtualenv('.virtualenvs', use_sudo=True, user='******', clear=True) with fabtools.python.virtualenv('.virtualenvs'): python.install_requirements('src/requirements.txt', use_mirrors=False, use_sudo=True, user='******', download_cache=pip_cache_dir) # ------------------- # # WEB SERVER SETTINGS # # ------------------- # # I`m use nginx <-> uWSGI <-> Django nginx.server() deb.packages(['uwsgi', 'uwsgi-plugin-python']) # proj conf! if not is_dir('conf') or confirm("proj conf dir exist! [backup and update? / skip]", default=False): files.directory('conf', use_sudo=True, owner='root', group='root', mode='755') with cd('conf'): local_conf_templates = os.path.join(os.path.dirname(__file__), 'template', 'conf') uwsgi_conf = os.path.join(local_conf_templates, 'uwsgi.ini') nginx_conf = os.path.join(local_conf_templates, 'nginx.conf') sudo("rm -Rf *.back") sudo("ls -d *{.conf,.ini} | sed 's/.*$/mv -fu \"&\" \"\\0.back\"/' | sh") files.template_file('uwsgi.ini', template_source=uwsgi_conf, context=env, use_sudo=True, owner='root', group='root', mode='644') files.file('reload', use_sudo=True, owner='root', group='root') sudo('ln -sf $(pwd)/uwsgi.ini /etc/uwsgi/apps-enabled/' + env.project_dir_name + '.ini') files.template_file('nginx.conf', template_source=nginx_conf, context=env, use_sudo=True, owner='root', group='root', mode='644') sudo('ln -sf $(pwd)/nginx.conf /etc/nginx/sites-enabled/' + env.project_dir_name) sudo('service nginx restart') sudo('service uwsgi restart')
def addRootFlask(webserver, appname): """ Add a flask webserver :param webserver: :param appname: :return: """ hostdir = gethostdir() # Create web directory createDirectory(hostdir, webserver) # Add a nginx CONFIG_TPL = ''' server { server_name %(server_name)s %(server_alias)s; root %(docroot)s/%(server_name)s/www; access_log %(docroot)s/%(server_name)s/log/access.log; error_log %(docroot)s/%(server_name)s/log/error.log; location / { try_files $uri @%(appname)s; } location @%(appname)s { include uwsgi_params; uwsgi_pass unix:/run/uwsgi/app/%(server_name)s_%(appname)s/socket; } }''' require.nginx.site( webserver, template_contents=CONFIG_TPL, appname=appname, server_alias='', docroot=hostdir, ) # Add a uwsgi config_filename = '/etc/uwsgi/apps-available/%(webserver)s_%(appname)s.ini' % locals() CONFIG_TPL = ''' [uwsgi] uid = %(server_name)s gid = %(server_name)s callable = app plugins = python base = %(hostdir)s/%(server_name)s/www pythonpath = %(hostdir)s/%(server_name)s/www/%(appname)s virtualenv = %(hostdir)s/%(server_name)s/venv wsgi-file = /data/backup/hosting/domotique/www/%(appname)s/sk_server.py env = %(APPNAME)s_SETTINGS=/data/backup/hosting/domotique/conf/%(appname)s.cfg logto = /var/log/uwsgi/%(server_name)s_%(appname)s.log chmod-socket = 666 # Optional emperor = /tmp emperor-tyrant = true cap = setgid,setuid ''' template_file(config_filename, template_contents=CONFIG_TPL, template_source=None, context={ 'server_name': webserver, 'hostdir': hostdir, 'appname': appname, 'APPNAME': appname.upper() } ) active_uwsgi(webserver, appname) service.restart('uwsgi') service.restart('nginx') require.network.host('127.0.0.1', webserver)
def site(config_name, template_contents=None, template_source=None, enabled=True, check_config=True, **kwargs): """ Require an Apache site. You must provide a template for the site configuration, either as a string (*template_contents*) or as the path to a local template file (*template_source*). :: from fabtools import require CONFIG_TPL = ''' <VirtualHost *:%(port)s> ServerName %(hostname})s DocumentRoot %(document_root)s <Directory %(document_root)s> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> </VirtualHost> ''' require.apache.site( 'example.com', template_contents=CONFIG_TPL, port=80, hostname='www.example.com', document_root='/var/www/mysite', ) .. seealso:: :py:func:`fabtools.require.files.template_file` """ server() config_filename = '/etc/apache2/sites-available/%s' % _get_config_name( config_name) context = { 'port': 80, } context.update(kwargs) context['config_name'] = config_name template_file(config_filename, template_contents, template_source, context, use_sudo=True) if enabled: enable_site(config_name) else: disable_site(config_name) if check_config: with settings(hide('running', 'warnings'), warn_only=True): if run_as_root('apache2ctl configtest').failed: disable_site(config_name) message = red( "Error in %(config_name)s apache site config (disabling for safety)" % locals()) abort(message) reload_service('apache2')
def addRootFlask(webserver, appname): """ Add a flask webserver :param webserver: :param appname: :return: """ hostdir = gethostdir() # Create web directory createDirectory(hostdir, webserver) # Add a nginx CONFIG_TPL = ''' server { server_name %(server_name)s %(server_alias)s; root %(docroot)s/%(server_name)s/www; access_log %(docroot)s/%(server_name)s/log/access.log; error_log %(docroot)s/%(server_name)s/log/error.log; location / { try_files $uri @%(appname)s; } location @%(appname)s { include uwsgi_params; uwsgi_pass unix:/run/uwsgi/app/%(server_name)s_%(appname)s/socket; } }''' require.nginx.site( webserver, template_contents=CONFIG_TPL, appname=appname, server_alias='', docroot=hostdir, ) # Add a uwsgi config_filename = '/etc/uwsgi/apps-available/%(webserver)s_%(appname)s.ini' % locals( ) CONFIG_TPL = ''' [uwsgi] uid = %(server_name)s gid = %(server_name)s callable = app plugins = python base = %(hostdir)s/%(server_name)s/www pythonpath = %(hostdir)s/%(server_name)s/www/%(appname)s virtualenv = %(hostdir)s/%(server_name)s/venv wsgi-file = /data/backup/hosting/domotique/www/%(appname)s/sk_server.py env = %(APPNAME)s_SETTINGS=/data/backup/hosting/domotique/conf/%(appname)s.cfg logto = /var/log/uwsgi/%(server_name)s_%(appname)s.log chmod-socket = 666 # Optional emperor = /tmp emperor-tyrant = true cap = setgid,setuid ''' template_file(config_filename, template_contents=CONFIG_TPL, template_source=None, context={ 'server_name': webserver, 'hostdir': hostdir, 'appname': appname, 'APPNAME': appname.upper() }) active_uwsgi(webserver, appname) service.restart('uwsgi') service.restart('nginx') require.network.host('127.0.0.1', webserver)
def site(server_name, template_contents=None, template_source=None, enabled=True, check_config=True, **kwargs): """ Require an apache2 site. You must provide a template for the site configuration, either as a string (*template_contents*) or as the path to a local template file (*template_source*). :: from fabtools import require VHOST_SITE_TEMPLATE = ''' NameVirtualHost %(server_name)s:%(port)s <VirtualHost %(server_name)s:%(port)s> ServerName %(server_name)s ServerAdmin webmaster@%(server_name)s DocumentRoot %(docroot)s <Directory %(docroot)s> AllowOverride all Order allow,deny allow from all </Directory> ErrorLog /var/log/apache2/%(server_name)s.error.log CustomLog /var/log/apache2/%(server_name)s.access.log combined LogLevel warn ServerSignature Off </VirtualHost> ''' require.apache2.site('example.com', template_contents=VHOST_SITE_TEMPLATE, port=80, server_alias='www.example.com', docroot='/var/www/mysite', ) ) .. seealso:: :py:func:`fabtools.require.files.template_file` """ server() config_filename = "/etc/apache2/sites-available/%s.conf" % server_name context = {"port": 80} context.update(kwargs) context["server_name"] = server_name template_file(config_filename, template_contents, template_source, context, use_sudo=True) link_filename = "/etc/apache2/sites-enabled/%s.conf" % server_name if enabled: if not is_link(link_filename): sudo("ln -s %(config_filename)s %(link_filename)s" % locals()) # Make sure we don't break the config if check_config: with settings(hide("running", "warnings"), warn_only=True): if not sudo("apache2ctl configtest" % locals()) == "Syntax OK": print red("Error in %(server_name)s apache2 site config (disabling for safety)" % locals()) sudo("rm %(link_filename)s" % locals()) else: if is_link(link_filename): sudo("rm %(link_filename)s" % locals()) restarted("apache2")