Browse Source

Initial commit.

Jeff Geerling 10 years ago
commit
19983ce440
6 changed files with 226 additions and 0 deletions
  1. 63 0
      README.md
  2. 23 0
      meta/main.yml
  3. 18 0
      tasks/gogs-mysql.yml
  4. 51 0
      tasks/main.yml
  5. 62 0
      templates/gogs-init.j2
  6. 9 0
      vars/main.yml

+ 63 - 0
README.md

@@ -0,0 +1,63 @@
+# Ansible Role: Gogs
+
+Installs [Gogs](https://github.com/gogits/gogs), a Go-based front-end to Git, on RedHat or Debian-based linux systems.
+
+After the playbook is finished, visit the gogs server (on port 3000 by default), and you will be redirected to the /install page, where you can configure an administrator account and other default options.
+
+## Requirements
+
+Requires git (via `geerlingguy.git`), and at least the Gogs HTTP port (3000 by default) open on your system's firewall. Install MySQL (e.g. via `geerlingguy.mysql`) prior to installing Gogs if you would like to use MySQL instead of built-in SQLite support.
+
+## Role Variables
+
+Available variables are listed below, along with default values (see `vars/main.yml`):
+
+    gogs_user: git
+    gogs_user_home: /home/git
+
+The user and home under which Gogs will run and be installed.
+
+    gogs_binary_url: https://github.com/gogits/gogs/releases/download/v0.3.1/linux_amd64.zip
+
+Download URL for the Gogs binary.
+
+    gogs_http_port: "3000"
+
+HTTP port over which Gogs will be accessed.
+
+    gogs_use_mysql: false
+    gogs_db_name: gogs
+    gogs_db_username: gogs
+    gogs_db_password: root
+
+MySQL database support. Set `gogs_use_mysql` to `true` to configure MySQL for gogs, using the database name, username, and password defined by the respective variables.
+
+## Dependencies
+
+  - geerlingguy.git
+
+## Example Playbook
+
+    - hosts: servers
+      vars_files:
+        - vars/main.yml
+      roles:
+        - { role: geerlingguy.gogs }
+
+*Inside `vars/main.yml`*:
+
+    gogs_http_port: "8080"
+
+## TODO
+
+  - Preconfigure gogs so installation is simpler/more robust.
+  - Make Gogs version more easily configurable.
+  - (Maybe) add option to build Gogs from source (instead of binary installation).
+
+## License
+
+MIT / BSD
+
+## Author Information
+
+This role was created in 2014 by [Jeff Geerling](http://jeffgeerling.com/), author of [Ansible for DevOps](http://ansiblefordevops.com/).

+ 23 - 0
meta/main.yml

@@ -0,0 +1,23 @@
+---
+dependencies:
+  - { role: geerlingguy.git }
+
+galaxy_info:
+  author: geerlingguy
+  description: Gogs: Go Git Service
+  company: "Midwestern Mac, LLC"
+  license: "license (BSD, MIT)"
+  min_ansible_version: 1.4
+  platforms:
+  - name: EL
+    versions:
+    - 6
+  - name: Ubuntu
+    versions:
+    - all
+  - name: Debian
+    versions:
+    - all
+  categories:
+    - development
+    - web

+ 18 - 0
tasks/gogs-mysql.yml

@@ -0,0 +1,18 @@
+---
+- name: Create Gogs MySQL user.
+  mysql_user: >
+    name={{ gogs_db_username }}
+    host={{ item }}
+    priv={{ gogs_db_name }}.*:ALL
+    password={{ gogs_db_password }}
+  with_items:
+    - 127.0.0.1
+    - ::1
+    - localhost
+  when: gogs_use_mysql
+
+- name: Create Gogs MySQL database.
+  mysql_db: >
+    db={{ gogs_db_name }}
+    state=present
+  when: gogs_use_mysql

+ 51 - 0
tasks/main.yml

@@ -0,0 +1,51 @@
+---
+- name: Ensure unzip is installed (RedHat).
+  yum: pkg=unzip state=installed
+  when: ansible_os_family == 'RedHat'
+
+- name: Ensure unzip is installed (Debian).
+  apt: pkg=unzip state=installed
+  when: ansible_os_family == 'Debian'
+
+- name: Create user for Gogs.
+  user: >
+    name={{ gogs_user }}
+    comment=Gogs
+    home={{ gogs_user_home }}
+    shell=/bin/bash
+
+- name: Download Gogs.
+  get_url: >
+    url={{ gogs_binary_url }}
+    dest={{ gogs_user_home }}/gogs.zip
+    owner={{ gogs_user }} group={{ gogs_user }}
+
+- name: Expand Gogs.
+  shell: >
+    su -c "unzip {{ gogs_user_home }}/gogs.zip -d {{ gogs_user_home }}" -s /bin/bash {{ gogs_user }} 
+    chdir={{ gogs_user_home }}
+    creates={{ gogs_user_home }}/gogs/gogs
+
+- include: gogs-mysql.yml
+
+- name: Copy gogs init file.
+  template: >
+    src=gogs-init.j2
+    dest=/etc/init.d/gogs
+    owner=root group=root mode=755
+
+- name: Create Gogs log folder.
+  file: >
+    path={{ gogs_user_home }}/gogs/log
+    state=directory
+    owner={{ gogs_user }} group={{ gogs_user }} mode=755
+
+- name: Check if Gogs is running.
+  command: service gogs status
+  changed_when: false
+  register: gogs_status
+
+- name: Ensure Gogs is running.
+  shell: >
+    su -c "service gogs start" -s /bin/bash {{ gogs_user }}
+  when: "'running' not in gogs_status.stdout"

+ 62 - 0
templates/gogs-init.j2

@@ -0,0 +1,62 @@
+#! /bin/bash
+# /etc/init.d/gogs
+#
+
+### BEGIN INIT INFO
+# Provides:          gogs
+# Required-Start:    $remote_fs $syslog
+# Required-Stop:     $remote_fs $syslog
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: Start gogs at boot time.
+# Description:       Control gogs.
+### END INIT INFO
+
+start_gogs() {
+  nohup {{ gogs_user_home }}/gogs/start.sh > {{ gogs_user_home }}/gogs/log/gogs.log 2>&1&
+}
+
+# Gogs needs a home path.
+export HOME={{ gogs_user_home }}
+
+# Carry out specific functions when asked to by the system
+case "$1" in
+  start)
+    ps aux | grep 'gogs' | grep -v grep | grep -vq start
+    if [ $? = 0 ]; then
+      echo "Gogs is already running."
+    else
+      start_gogs
+    fi
+    ;;
+  stop)
+    ps aux | grep 'gogs' | grep -v grep | grep -vq stop
+    if [ $? = 0 ]; then
+      echo "Stopping Gogs."
+      kill `ps -ef | grep 'gogs' | grep -v grep | awk '{print $2}'`
+    else
+      echo "Gogs is already stopped."
+    fi
+    ;;
+  restart)
+    ps aux | grep 'gogs' | grep -v grep | grep -vq restart
+    if [ $? = 0 ]; then
+      kill `ps -ef | grep 'gogs' | grep -v grep | awk '{print $2}'`
+    fi
+    start_gogs
+    ;;
+  status)
+    ps aux | grep 'gogs' | grep -v grep | grep -vq status
+    if [ $? = 0 ]; then
+      echo "Gogs is running."
+    else
+      echo "Gogs is stopped."
+    fi
+    ;;
+  *)
+    echo "Usage: /etc/init.d/gogs {start|stop|restart|status}"
+    exit 1
+    ;;
+esac
+
+exit 0

+ 9 - 0
vars/main.yml

@@ -0,0 +1,9 @@
+---
+gogs_user: git
+gogs_user_home: /home/git
+gogs_binary_url: https://github.com/gogits/gogs/releases/download/v0.3.1/linux_amd64.zip
+gogs_http_port: "3000"
+gogs_use_mysql: false
+gogs_db_name: gogs
+gogs_db_username: gogs
+gogs_db_password: root