Support automatic installation via configuration management (ansible, puppet, chef, etc) #973

Closed
opened 2025-11-02 03:43:52 -06:00 by GiteaMirror · 7 comments
Owner

Originally created by @vizcay on GitHub (Aug 17, 2017).

I'm trying to setup an ansible playbook that installs and configures everything but it needs manual intervention and that's not desirable from a devops point of view.

The main problem is, to configure the admin user account, I need the database schema to be created before issuing gitea create-user .. that is currently failing with "CreateUser: no such table: user". That needs to server to be started and navigated at least once.

Just for the record, this is as far as I can get (it's very easy to follow):

- block:
  - name: Create gitea directory
    file:
      path: /home/deploy/applications/gitea/
      state: directory
      owner: deploy
  - name: Download gitea executable
    get_url:
      url: https://dl.gitea.io/gitea/master/gitea-master-linux-amd64
      dest: /home/deploy/applications/gitea/gitea
      mode: 0550
      owner: deploy
      force: no
  - name: Create gitea config directory
    file:
      path: /home/deploy/applications/gitea/custom/conf/
      state: directory
      owner: deploy
  - name: Copy config
    template:
      src: templates/app.ini
      dest: /home/deploy/applications/gitea/custom/conf/app.ini
      owner: deploy
  - name: Copy nginx site for gitea
    template:
      src: templates/nginx
      dest: /etc/nginx/sites-available/gitea
  - name: Enable nginx site for gitea
    file:
      src: /etc/nginx/sites-available/gitea
      dest: /etc/nginx/sites-enabled/gitea
      state: link
  - name: Restart nginx to pick config
    service:
      name: nginx
      state: reloaded
  - name: Copy monit config for gitea
    template:
      src: templates/monit
      dest: /etc/monit/conf-available/gitea
  - name: Enable monit config for gitea
    file:
      src: /etc/monit/conf-available/gitea
      dest: /etc/monit/conf-enabled/gitea
      state: link
  - name: Restart monit to pick config
    service:
      name: monit
      state: reloaded
  become: true
- name: Create admin user for gitea
  command: /home/deploy/applications/gitea/gitea admin create-user --name xxxxx --password xxxxx --admin
  ignore_errors: yes
  args:
    chdir: /home/deploy/applications/gitea

Please: manual intervention should never be needed to install software.

By the way, I love the simplicity and requirements of gitea = )

Originally created by @vizcay on GitHub (Aug 17, 2017). I'm trying to setup an ansible playbook that installs and configures everything but it needs manual intervention and that's not desirable from a devops point of view. The main problem is, to configure the admin user account, I need the database schema to be created before issuing ```gitea create-user ..``` that is currently failing with "CreateUser: no such table: user". That needs to server to be started and navigated at least once. Just for the record, this is as far as I can get (it's very easy to follow): ``` - block: - name: Create gitea directory file: path: /home/deploy/applications/gitea/ state: directory owner: deploy - name: Download gitea executable get_url: url: https://dl.gitea.io/gitea/master/gitea-master-linux-amd64 dest: /home/deploy/applications/gitea/gitea mode: 0550 owner: deploy force: no - name: Create gitea config directory file: path: /home/deploy/applications/gitea/custom/conf/ state: directory owner: deploy - name: Copy config template: src: templates/app.ini dest: /home/deploy/applications/gitea/custom/conf/app.ini owner: deploy - name: Copy nginx site for gitea template: src: templates/nginx dest: /etc/nginx/sites-available/gitea - name: Enable nginx site for gitea file: src: /etc/nginx/sites-available/gitea dest: /etc/nginx/sites-enabled/gitea state: link - name: Restart nginx to pick config service: name: nginx state: reloaded - name: Copy monit config for gitea template: src: templates/monit dest: /etc/monit/conf-available/gitea - name: Enable monit config for gitea file: src: /etc/monit/conf-available/gitea dest: /etc/monit/conf-enabled/gitea state: link - name: Restart monit to pick config service: name: monit state: reloaded become: true - name: Create admin user for gitea command: /home/deploy/applications/gitea/gitea admin create-user --name xxxxx --password xxxxx --admin ignore_errors: yes args: chdir: /home/deploy/applications/gitea ``` **Please: manual intervention should never be needed to install software.** By the way, I love the simplicity and requirements of gitea = )
GiteaMirror added the type/bug label 2025-11-02 03:43:52 -06:00
Author
Owner

@lunny commented on GitHub (Aug 17, 2017):

First you have to keep INSTALL_LOCK = true and change you database options

[database]
;DB_TYPE  = mysql
;DB_TYPE  = postgres
;DB_TYPE  = mssql
DB_TYPE  = sqlite3
;HOST     = 127.0.0.1:3306
;HOST     = 127.0.0.1:5432
HOST     = 127.0.0.1
NAME     = gitea
USER     = sa
PASSWD   = 
SSL_MODE = disable
PATH     = data/gitea.db

[security]
INSTALL_LOCK   = true

And the command may should be

gitea admin create-user --name xxx --password xxxx --email ffddf admin
@lunny commented on GitHub (Aug 17, 2017): First you have to keep `INSTALL_LOCK = true` and change you database options ``` [database] ;DB_TYPE = mysql ;DB_TYPE = postgres ;DB_TYPE = mssql DB_TYPE = sqlite3 ;HOST = 127.0.0.1:3306 ;HOST = 127.0.0.1:5432 HOST = 127.0.0.1 NAME = gitea USER = sa PASSWD = SSL_MODE = disable PATH = data/gitea.db [security] INSTALL_LOCK = true ``` And the command may should be ``` gitea admin create-user --name xxx --password xxxx --email ffddf admin ```
Author
Owner

@vizcay commented on GitHub (Aug 17, 2017):

  1. @lunny if you try to create an user, before starting the web server you get this error:
~/applications/gitea$ ./gitea admin create-user --name xxxx --password xxxx --email xxxx --admin
2017/08/17 12:23:52 [I] XORM Log Mode: File(Info)
CreateUser: no such table: user

It looks like create-user is not checking if the db is migrated.

  1. How I can daemonize gitea? Usually there is a --daemon flag..

PD: After everything is configured I would love to write a medium article on how to install (with nginx as a reverse proxy), configure and monitor (with monit as a watchdog) gitea; maybe it will be useful for someone else.

@vizcay commented on GitHub (Aug 17, 2017): 1) @lunny if you try to create an user, before starting the web server you get this error: ``` ~/applications/gitea$ ./gitea admin create-user --name xxxx --password xxxx --email xxxx --admin 2017/08/17 12:23:52 [I] XORM Log Mode: File(Info) CreateUser: no such table: user ``` It looks like create-user is not checking if the db is migrated. 2) How I can daemonize gitea? Usually there is a --daemon flag.. PD: After everything is configured I would love to write a medium article on how to install (with nginx as a reverse proxy), configure and monitor (with monit as a watchdog) gitea; maybe it will be useful for someone else.
Author
Owner

@lunny commented on GitHub (Aug 18, 2017):

This is because db is not initialed. A trick method to resolve this before it fixed is runing gitea web at first and then run the ./gitea admin create-user --name xxxx --password xxxx --email xxxx --admin. The long-term method should add a new command maybe gitea init to initial the system.

@lunny commented on GitHub (Aug 18, 2017): This is because db is not initialed. A trick method to resolve this before it fixed is runing `gitea web` at first and then run the `./gitea admin create-user --name xxxx --password xxxx --email xxxx --admin`. The long-term method should add a new command maybe `gitea init` to initial the system.
Author
Owner

@philfry commented on GitHub (Aug 18, 2017):

How about waiting for the database before proceeding with the playbook?

- name: "start and enable for boot"
  systemd: name=gitea state=started enabled=yes daemon_reload=yes
  register: onboot_gitea

- block:
  - name: "pause until mysql db is initialized"
    shell: sleep 5; mysql {{gitea_mysql_db}} -Ne 'show tables' | wc -l
    register: gitea_db_init_r
    retries: 6
    until: gitea_db_init_r.stdout|int >= 30
    when: gitea_db == "mysql"
  - name: "pause until sqlite db is initialized"
    shell: sleep 5; sqlite3 {{gitea_sqlite_path}} "select name from sqlite_master where type = 'table' and name not like 'sqlite_%'" | wc  -l
    register: gitea_db_init_r
    retries: 6
    until: gitea_db_init_r.stdout|int >= 30
    when: gitea_db == "sqlite3"
  when: onboot_gitea.changed
@philfry commented on GitHub (Aug 18, 2017): How about waiting for the database before proceeding with the playbook? ```yml - name: "start and enable for boot" systemd: name=gitea state=started enabled=yes daemon_reload=yes register: onboot_gitea - block: - name: "pause until mysql db is initialized" shell: sleep 5; mysql {{gitea_mysql_db}} -Ne 'show tables' | wc -l register: gitea_db_init_r retries: 6 until: gitea_db_init_r.stdout|int >= 30 when: gitea_db == "mysql" - name: "pause until sqlite db is initialized" shell: sleep 5; sqlite3 {{gitea_sqlite_path}} "select name from sqlite_master where type = 'table' and name not like 'sqlite_%'" | wc -l register: gitea_db_init_r retries: 6 until: gitea_db_init_r.stdout|int >= 30 when: gitea_db == "sqlite3" when: onboot_gitea.changed ```
Author
Owner

@vizcay commented on GitHub (Aug 18, 2017):

@philfry well yes for sure it can be done something like that.. but is fragile and a little hacky. Is not like I'm installing a hundread instances so I will opt to just finish the install manually, but for sure ./gitea admin init or something like that is the way to go.

@vizcay commented on GitHub (Aug 18, 2017): @philfry well yes for sure it can be done something like that.. but is fragile and a little hacky. Is not like I'm installing a hundread instances so I will opt to just finish the install manually, but for sure ```./gitea admin init``` or something like that is the way to go.
Author
Owner

@nblock commented on GitHub (Nov 23, 2017):

This may also be solved without shell tasks. Tested with Gitea 1.2.3:

# Setup tasks snipped

- name: Gitea should be running
  service:
    name: gitea
    state: started
    enabled: true

- name: Flush handlers to run 
  meta: flush_handlers

# It takes a while until Gitea has created its database on the first run.
# This might also be true for updates. After Gitea completed its operation, it
# starts to listen on its service port and we might continue with the
# configuration.
- name: Wait 300 seconds for Gitea to become ready
  wait_for:
    port: "{{ gitea_port }}"

- name: Create a Gitea admin user
  command: "{{ gitea_basepath }}/gitea-{{ gitea_version }} admin create-user --name {{ gitea_admin_username }} --password {{ gitea_admin_password }} --email {{ gitea_admin_email }} --admin --config {{ gitea_basepath }}/gitea/custom/conf/app.ini"
  register: _gitea_create_admin_user
  failed_when: gitea_create_admin_user.rc == 1 and 'user already exists' not in gitea_create_admin_user.stderr
  changed_when: gitea_create_admin_user.rc == 0
  become_user: "{{ gitea_user }}"
  no_log: true
@nblock commented on GitHub (Nov 23, 2017): This may also be solved without shell tasks. Tested with Gitea 1.2.3: ```yaml # Setup tasks snipped - name: Gitea should be running service: name: gitea state: started enabled: true - name: Flush handlers to run meta: flush_handlers # It takes a while until Gitea has created its database on the first run. # This might also be true for updates. After Gitea completed its operation, it # starts to listen on its service port and we might continue with the # configuration. - name: Wait 300 seconds for Gitea to become ready wait_for: port: "{{ gitea_port }}" - name: Create a Gitea admin user command: "{{ gitea_basepath }}/gitea-{{ gitea_version }} admin create-user --name {{ gitea_admin_username }} --password {{ gitea_admin_password }} --email {{ gitea_admin_email }} --admin --config {{ gitea_basepath }}/gitea/custom/conf/app.ini" register: _gitea_create_admin_user failed_when: gitea_create_admin_user.rc == 1 and 'user already exists' not in gitea_create_admin_user.stderr changed_when: gitea_create_admin_user.rc == 0 become_user: "{{ gitea_user }}" no_log: true ```
Author
Owner

@lunny commented on GitHub (Nov 9, 2018):

Since https://github.com/go-gitea/gitea/pull/4954 has been merged, I think you can do gitea migrate before you do other operations. So I will close this one.

@lunny commented on GitHub (Nov 9, 2018): Since https://github.com/go-gitea/gitea/pull/4954 has been merged, I think you can do `gitea migrate` before you do other operations. So I will close this one.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#973