Skip to content

Commit a44a40b

Browse files
authored
Merge pull request doccano#293 from CatalystCode/enhancement/auto-create-admin
Enhancement/Auto create admin
2 parents f211656 + 225c5eb commit a44a40b

File tree

4 files changed

+31
-66
lines changed

4 files changed

+31
-66
lines changed

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,19 @@ Let’s start the development server and explore it.
125125

126126
Depending on your installation method, there are two options:
127127

128-
**Option1: Running the Docker image as a Container**
128+
#### Option 1: Running the Docker image as a Container
129129

130130
First, run a Docker container:
131131

132132
```bash
133-
docker run -d --name doccano -p 8000:8000 chakkiworks/doccano
133+
docker run -d --rm --name doccano \
134+
-e "ADMIN_USERNAME=admin" \
135+
136+
-e "ADMIN_PASSWORD=password" \
137+
-p 8000:8000 chakkiworks/doccano
134138
```
135139

136-
Then, execute `create-admin.sh` script for creating a superuser.
137-
138-
```bash
139-
docker exec doccano tools/create-admin.sh "admin" "[email protected]" "password"
140-
```
141-
142-
**Option2: Running Django development server**
140+
#### Option 2: Running Django development server
143141

144142
Before running, we need to make migration. Run the following command:
145143

@@ -169,7 +167,7 @@ Optionally, you can change the bind ip and port using the command
169167
python manage.py runserver <ip>:<port>
170168
```
171169

172-
**Option3: Running the development Docker-Compose stack**
170+
#### Option 3: Running the development Docker-Compose stack
173171

174172
We can use docker-compose to set up the webpack server, django server, database, etc. all in one command:
175173

app/server/management/commands/create_admin.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from django.contrib.auth.management.commands import createsuperuser
22
from django.core.management import CommandError
3+
from django.db import IntegrityError
34

45

56
class Command(createsuperuser.Command):
67
help = 'Non-interactively create an admin user'
78

89
def add_arguments(self, parser):
9-
super(Command, self).add_arguments(parser)
10+
super().add_arguments(parser)
1011
parser.add_argument('--password', default=None,
1112
help='The password for the admin.')
1213

@@ -17,7 +18,10 @@ def handle(self, *args, **options):
1718
if password and not username:
1819
raise CommandError('--username is required if specifying --password')
1920

20-
super(Command, self).handle(*args, **options)
21+
try:
22+
super().handle(*args, **options)
23+
except IntegrityError:
24+
self.stderr.write(f'User {username} already exists.')
2125

2226
if password:
2327
database = options.get('database')

azuredeploy.json

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,8 @@
124124
"databaseUserCredentials" : "[concat(uriComponent(concat(parameters('adminUserName'), '@', variables('databaseServerName'))), ':', parameters('adminPassword'))]",
125125
"databaseFqdn" : "[concat( variables('databaseServerName'), '.postgres.database.azure.com:', variables('databaseServerPort'))]",
126126
"databaseConnectionString": "[concat('pgsql://', variables('databaseUserCredentials'), '@', variables('databaseFqdn'), '/', parameters('databaseName'))]",
127-
"setupScriptName": "[concat(parameters('appName'),'-setup')]",
128127
"appServicePlanName": "[concat(parameters('appName'),'-hosting')]",
129128
"analyticsName": "[concat(parameters('appName'),'-analytics')]",
130-
"dockerRegistryCredential": {
131-
"password": "[parameters('dockerRegistryPassword')]",
132-
"username": "[parameters('dockerRegistryUserName')]",
133-
"server": "[parameters('dockerRegistry')]"
134-
},
135129
"appFqdn": "[concat(parameters('appName'),'.azurewebsites.net')]"
136130
},
137131
"resources": [
@@ -206,54 +200,6 @@
206200
}
207201
]
208202
},
209-
{
210-
"name": "[variables('setupScriptName')]",
211-
"type": "Microsoft.ContainerInstance/containerGroups",
212-
"apiVersion": "2018-10-01",
213-
"location": "[variables('location')]",
214-
"properties": {
215-
"imageRegistryCredentials": "[if(equals(parameters('dockerRegistry'), ''), json('null'), array(variables('dockerRegistryCredential')))]",
216-
"containers": [
217-
{
218-
"name": "createadmin",
219-
"properties": {
220-
"image": "[parameters('dockerImageName')]",
221-
"command": [
222-
"tools/create-admin.sh",
223-
"[parameters('adminUserName')]",
224-
"[parameters('adminContactEmail')]",
225-
"[parameters('adminPassword')]"
226-
],
227-
"environmentVariables": [
228-
{
229-
"name": "DEBUG",
230-
"value": "False"
231-
},
232-
{
233-
"name": "SECRET_KEY",
234-
"value": "[parameters('secretKey')]"
235-
},
236-
{
237-
"name": "DATABASE_URL",
238-
"value": "[variables('databaseConnectionString')]"
239-
}
240-
],
241-
"resources": {
242-
"requests": {
243-
"cpu": "1",
244-
"memoryInGb": "1.5"
245-
}
246-
}
247-
}
248-
}
249-
],
250-
"osType": "Linux",
251-
"restartPolicy": "Never"
252-
},
253-
"dependsOn": [
254-
"[resourceId('Microsoft.DBforPostgreSQL/servers/', variables('databaseServerName'))]"
255-
]
256-
},
257203
{
258204
"type": "Microsoft.Web/sites",
259205
"apiVersion": "2016-08-01",
@@ -303,6 +249,18 @@
303249
"name": "GOOGLE_TRACKING_ID",
304250
"value": " "
305251
},
252+
{
253+
"name": "ADMIN_USERNAME",
254+
"value": "[parameters('adminUserName')]"
255+
},
256+
{
257+
"name": "ADMIN_EMAIL",
258+
"value": "[parameters('adminContactEmail')]"
259+
},
260+
{
261+
"name": "ADMIN_PASSWORD",
262+
"value": "[parameters('adminPassword')]"
263+
},
306264
{
307265
"name": "DEBUG",
308266
"value": "False"

tools/run.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ if [[ ! -d "app/staticfiles" ]]; then python app/manage.py collectstatic --noinp
66

77
python app/manage.py wait_for_db
88
python app/manage.py migrate
9+
10+
if [[ -n "${ADMIN_USERNAME}" ]] && [[ -n "${ADMIN_EMAIL}" ]] && [[ -n "${ADMIN_PASSWORD}" ]]; then
11+
python app/manage.py create_admin --noinput --username="${ADMIN_USERNAME}" --email="${ADMIN_EMAIL}" --password="${ADMIN_PASSWORD}"
12+
fi
13+
914
gunicorn --bind="0.0.0.0:${PORT:-8000}" --workers="${WORKERS:-1}" --pythonpath=app app.wsgi --timeout 300

0 commit comments

Comments
 (0)