introduced git.server role

Change-Id: Ic118eaadee6360e24c0a0d1add79ba8d10294f79
diff --git a/.kitchen.yml b/.kitchen.yml
index a804e9d..dcd688c 100644
--- a/.kitchen.yml
+++ b/.kitchen.yml
@@ -14,6 +14,9 @@
     - name: linux
       repo: git
       source: https://github.com/salt-formulas/salt-formula-linux
+    - name: nginx
+      repo: git
+      source: https://github.com/salt-formulas/salt-formula-nginx
   log_level: error
   formula: git
   grains:
@@ -43,5 +46,8 @@
     provisioner:
       pillars-from-files:
         git.sls: tests/pillar/client_single.sls
-
+  - name: server_single
+    provisioner:
+      pillars-from-files:
+        git.sls: tests/pillar/server_single.sls
 # vim: ft=yaml sw=2 ts=2 sts=2 tw=125
diff --git a/README.rst b/README.rst
index 838165b..0c260c1 100644
--- a/README.rst
+++ b/README.rst
@@ -62,6 +62,19 @@
         user:
         - user: ${linux:system:user:jdoe}
 
+Reclass with GIT over HTTP server setup. Requires web server.
+
+.. code-block:: yaml
+
+    git:
+      server:
+        directory: /srv/git
+        repos:
+          - name: gerritlib
+            url: https://github.com/openstack-infra/gerritlib.git
+          - name: jeepyb
+            url: https://github.com/openstack-infra/jeepyb.git
+
 
 Read more
 =========
diff --git a/git/files/nginx.conf b/git/files/nginx.conf
new file mode 100644
index 0000000..c0ee2c3
--- /dev/null
+++ b/git/files/nginx.conf
@@ -0,0 +1,49 @@
+{%- set site = salt['pillar.get']('nginx:server:site:'+site_name) %}
+
+server {
+
+  {%- include "nginx/files/_name.conf" %}
+  {%- include "nginx/files/_log.conf" %}
+  {%- include "nginx/files/_ssl.conf" %}
+
+  location / {
+    {%- if site.root is defined %}
+    root  {{ site.root }};
+    {%- else %}
+    root  /srv/static/sites/{{ site.name }}{% if site.path is defined %}/{{ site.path }}{% endif %};
+    {%- endif %}
+
+    {%- include "nginx/files/_access_policy.conf" %}
+
+    {%- if site.get('autoindex', False) %}
+    autoindex on;
+    {%- endif %}
+
+    {%- if site.webdav is defined %}
+    {%- if site.webdav.methods is defined %}
+    autoindex on;
+    dav_methods {{ site.webdav.methods }};
+    {%- endif %}
+    {%- if site.webdav.ext_methods is defined %}
+    dav_ext_methods {{ site.webdav.ext_methods }};
+    {%- endif %}
+    {%- if site.webdav.access is defined %}
+    dav_access {{ site.webdav.access }};
+    {%- endif %}
+    {%- endif %}
+
+    fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
+    fastcgi_param GIT_HTTP_EXPORT_ALL "";
+    fastcgi_param GIT_PROJECT_ROOT {{ site.root }};
+    fastcgi_param PATH_INFO $1; 
+    include fastcgi_params;
+
+    {%- if site.client_max_body_size is defined %}
+    client_max_body_size {{ site.client_max_body_size }};
+    {%- endif %}
+
+    {%- include "nginx/files/_auth.conf" %}
+
+  }
+
+}
\ No newline at end of file
diff --git a/git/init.sls b/git/init.sls
index f3e1991..d21c4ad 100644
--- a/git/init.sls
+++ b/git/init.sls
@@ -3,4 +3,7 @@
 {%- if pillar.git.client is defined %}
 - git.client
 {%- endif %}
+{%- if pillar.git.server is defined %}
+- git.server
+{%- endif %}
 {%- endif %}
\ No newline at end of file
diff --git a/git/map.jinja b/git/map.jinja
index 7a84819..f5cbd39 100644
--- a/git/map.jinja
+++ b/git/map.jinja
@@ -1,3 +1,24 @@
+{%- set server = salt['grains.filter_by']({
+    'default': {
+        'disable_ssl_verification': False,
+        'user': [],
+    },
+    'Arch': {
+        'pkgs': ['git','fcgiwrap', 'apache-tools'],
+    },
+    'Debian': {
+        'pkgs': ['git', 'fcgiwrap', 'apache2-utils'],
+    },
+    'Gentoo': {
+        'pkgs': ['dev-vcs/git', 'www-misc/fcgiwrap', 'app-admin/apache-tools'],
+    },
+    'MacOS': {
+        'pkgs': ['git', 'fcgiwrap'],
+    },
+    'RedHat': {
+        'pkgs': ['git', 'fcgiwrap', 'httpd-tools'],
+    },
+}, merge=salt['pillar.get']('git:server'), base='default') %}
 
 {%- set client = salt['grains.filter_by']({
     'default': {
diff --git a/git/server.sls b/git/server.sls
new file mode 100644
index 0000000..139200d
--- /dev/null
+++ b/git/server.sls
@@ -0,0 +1,35 @@
+{%- from "git/map.jinja" import server with context %}
+
+{%- if server.enabled %}
+
+git_packages:
+  pkg.installed:
+  - names: {{ server.pkgs }}
+
+{{ server.directory }}:
+  file.directory:
+    - user: www-data
+    - group: www-data
+    - dir_mode: 755
+    - file_mode: 644
+
+{%- for repo in server.get('repos',{}) %}
+
+git_server_{{ repo.name }}:
+  git.latest:
+    - name: {{ repo.url }}
+    - target: {{ server.directory }}/{{ repo.name }}.git
+    - force_reset: True
+    - bare: True
+    - require:
+        - file: {{ server.directory }}
+
+git_update_server_info_{{ repo.name }}:
+  cmd.run:
+    - name: git update-server-info
+    - cwd: {{ server.directory }}/{{ repo.name }}.git
+    - require:
+        - git: git_server_{{ repo.name }}
+
+{%- endfor %}
+{%- endif %}
\ No newline at end of file
diff --git a/tests/pillar/server_single.sls b/tests/pillar/server_single.sls
new file mode 100644
index 0000000..bd9190a
--- /dev/null
+++ b/tests/pillar/server_single.sls
@@ -0,0 +1,20 @@
+git:
+  server:
+    enabled: true
+    directory: /srv/git
+    repos:
+      - name: gerritlib
+        url: https://github.com/openstack-infra/gerritlib.git
+nginx:
+  server:
+    enabled: True
+    site:
+      git_server:
+        enabled: True
+        name: git_server
+        type: git
+        root: /srv/git
+        client_max_body_size: 0
+        host:
+          name: git.local
+          port: 8080
\ No newline at end of file