Commit 47122a24 authored by Daniel Dehennin's avatar Daniel Dehennin

feat(setup): new rake task to create users without question

The default “rake admin:create” ask question, this one takes arguments
from the command line:

- email
- password, can be empty to generate a random one
- enabled, can be True/False to make user active or not
- admin, can be True/False to make user admin or not
parent 79a95dd4
......@@ -3,12 +3,12 @@
{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- set sls_sources_clone = tplroot ~ '.sources.clone' %}
{%- set sls_sources_hack = tplroot ~ '.sources.hack' %}
{%- from tplroot | path_join("map.jinja") import discourse with context %}
{%- from tplroot | path_join("libtofs.jinja") import files_switch with context %}
include:
- {{ sls_sources_clone }}
- {{ sls_sources_hack }}
{%- set config = discourse.directory | path_join('containers', 'app.yml') %}
......@@ -28,4 +28,4 @@ discourse-config-file-file-managed:
- context:
discourse: {{ discourse | json }}
- require:
- sls: {{ sls_sources_clone }}
- sls: {{ sls_sources_hack }}
......@@ -17,6 +17,7 @@ templates:
- "templates/web.ssl.template.yml"
- "templates/web.letsencrypt.ssl.template.yml"
{%- endif %}
- "templates/rake.autosetup.template.yml"
## which TCP/IP ports should this container expose?
## If you want Discourse to share a port with another webserver like Apache or nginx,
......
......@@ -5,12 +5,14 @@
{%- set tplroot = tpldir.split('/')[0] %}
{%- set sls_discourse_netcat_package_install = tplroot ~ '.netcat.install' %}
{%- set sls_discourse_net_tools_package_install = tplroot ~ '.net-tools.install' %}
{%- set sls_discourse_sources_hack = tplroot ~ '.sources.hack' %}
{%- set sls_discourse_config_file = tplroot ~ '.config.file' %}
{%- from tplroot | path_join("map.jinja") import discourse with context %}
include:
- {{ sls_discourse_netcat_package_install }}
- {{ sls_discourse_net_tools_package_install }}
- {{ sls_discourse_sources_hack }}
- {{ sls_discourse_config_file }}
discourse-service-rebuild-cmd-run:
......@@ -21,4 +23,5 @@ discourse-service-rebuild-cmd-run:
- sls: {{ sls_discourse_netcat_package_install }}
- sls: {{ sls_discourse_net_tools_package_install }}
- onchanges:
- sls: {{ sls_discourse_sources_hack }}
- sls: {{ sls_discourse_config_file }}
run:
- file:
path: /var/www/discourse/lib/tasks/autosetup.rake
contents: |
desc "Creates a forum user"
task "autosetup:createuser", [:email, :password, :enabled, :is_admin] => :environment do |task, args|
if args[:email].nil? or args[:email].empty?
puts "You must provide an email address"
exit 1
end
enabled = args[:enabled] !~ /false/i
is_admin = args[:is_admin] =~ /true/i
existing_user = User.find_by_email(args[:email])
begin
# check if user account already exixts
if existing_user
puts "User #{args[:email]} already exists"
user = existing_user
elsif !enabled
puts "User #{args[:email]} does not exists, nothing to do"
exit 0
else
# create new user
user = User.new
user.email = args[:email]
user.username = UserNameSuggester.suggest(user.email)
end
# save/update user account
saved = user.save
puts "User #{args[:email]}: #{user.errors.full_messages.join("\n")}" unless saved
end while !saved
if !enabled
puts "User #{args[:email]}: ensuring account is not active!"
user.active = false
else
puts "User #{args[:email]}: ensuring account is active!"
user.active = true
end
user.save
if existing_user
puts "User #{args[:email]}: account updated successfully!"
else
if enabled and (args[:password].nil? or args[:password].empty?)
puts "User #{args[:email]}: send password reset email"
email_token = user.email_tokens.create(email: user.email)
Jobs.enqueue(:critical_user_email, type: :forgot_password, user_id: user.id, email_token: email_token.token)
else
puts "User #{args[:email]}: set password"
password = password_confirmation = args[:password]
user.password = password
user.save
end
puts "User #{args[:email]}: account created successfully with username #{user.username}"
end
if is_admin
user.grant_admin!
if user.trust_level < 1
user.change_trust_level!(1)
end
user.email_tokens.update_all confirmed: true
user.activate
puts "User #{args[:email]}: account now has Admin privileges!"
else
user.revoke_admin!
if user.trust_level < 1
user.change_trust_level!(0)
end
user.email_tokens.update_all confirmed: true
user.activate
puts "User #{args[:email]}: account now has User privileges!"
end
end
# -*- coding: utf-8 -*-
# vim: ft=sls
{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- set sls_sources_clone = tplroot ~ '.sources.clone' %}
{%- from tplroot | path_join("map.jinja") import discourse with context %}
{%- from tplroot | path_join("libtofs.jinja") import files_switch with context %}
include:
- {{ sls_sources_clone }}
discourse-sources-hack-autosetup-template-file-managed:
file.managed:
- name: {{ discourse.directory | path_join('templates', 'rake.autosetup.template.yml') }}
- source: {{ files_switch(['rake.autosetup.template.yml'],
lookup='discourse-sources-hack-autosetup-template-file-managed',
use_subpath=True
)
}}
- mode: '644'
- owner: 'root'
- group: 'root'
- require:
- sls: {{ sls_sources_clone }}
......@@ -4,3 +4,4 @@
include:
- .package
- .clone
- .hack
......@@ -52,7 +52,7 @@ Available states
This state will configure the deployement by generating the
``containers/app.yml`` file.
It depends on ``discourse.sources.clone``.
It depends on ``discourse.sources.hack``.
``discourse.service``
^^^^^^^^^^^^^^^^^^^^^
......@@ -60,7 +60,7 @@ It depends on ``discourse.sources.clone``.
*Meta-state (This is a state that includes other states)*.
This state will rebuild the ``discourse`` application container if the
configuration changes.
``discourse.config`` has changes.
``discourse.clean``
^^^^^^^^^^^^^^^^^^^
......@@ -148,6 +148,7 @@ environment.
- install the git package
- clone the latest `discourse_docker`_ repository
- install new rake tasks
``discourse.docker``
````````````````````
......
Markdown is supported
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