Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Arkindex
Backend
Commits
8445ee09
Commit
8445ee09
authored
3 years ago
by
Erwan Rouchet
Browse files
Options
Downloads
Plain Diff
Merge branch 'bootstrap-script' into 'master'
Bootstrap dev environment with a script Closes
#843
See merge request
!1493
parents
a96f6e4e
1272bfd2
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!1493
Bootstrap dev environment with a script
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
arkindex/documents/management/commands/bootstrap.py
+140
-0
140 additions, 0 deletions
arkindex/documents/management/commands/bootstrap.py
with
140 additions
and
0 deletions
arkindex/documents/management/commands/bootstrap.py
0 → 100644
+
140
−
0
View file @
8445ee09
#!/usr/bin/env python3
from
django.conf
import
settings
from
django.core.management.base
import
BaseCommand
from
django.db
import
transaction
from
django.db.utils
import
IntegrityError
from
rest_framework.authtoken.models
import
Token
from
arkindex.images.models
import
ImageServer
from
arkindex.users.models
import
User
from
ponos.models
import
Farm
# Constants used in architecture project
IMAGE_SERVER_ID
=
12345
IMAGE_SERVER_BUCKET
=
'
iiif
'
IMAGE_SERVER_REGION
=
'
local
'
PONOS_FARM_ID
=
"
001e411a-1111-2222-3333-444455556666
"
PONOS_FARM_SEED
=
"
b12868101dab84984481741663d809d2393784894d6e807ceee0bd95051bf971
"
INTERNAL_API_TOKEN
=
"
deadbeefTestToken
"
class
Command
(
BaseCommand
):
help
=
'
Build required Database model instances for local development.
'
def
success
(
self
,
msg
):
"""
Helper to display success messages
"""
self
.
stdout
.
write
(
self
.
style
.
SUCCESS
(
f
"
✅
{
msg
}
"
))
def
warn
(
self
,
msg
):
"""
Helper to display warning messages
"""
self
.
stdout
.
write
(
self
.
style
.
WARNING
(
f
"
⚠
{
msg
}
"
))
def
fail
(
self
,
msg
):
"""
Helper to display error messages
"""
self
.
stdout
.
write
(
self
.
style
.
ERROR
(
f
"
❌
{
msg
}
"
))
def
check_user
(
self
,
user
):
"""
Ensure a user is admin + internal
"""
if
user
.
is_internal
and
user
.
is_admin
:
self
.
success
(
f
"
Internal user
{
user
}
is valid
"
)
else
:
user
.
is_internal
=
True
user
.
is_admin
=
True
user
.
save
()
self
.
warn
(
f
"
Updated user
{
user
}
to internal+admin
"
)
def
handle
(
self
,
**
options
):
# Never allow running this script in production
if
not
settings
.
DEBUG
:
self
.
fail
(
"
You cannot run this script in production.
"
)
return
# a Ponos farm with specific ID & seed
try
:
farm
=
Farm
.
objects
.
get
(
id
=
PONOS_FARM_ID
)
if
farm
.
seed
==
PONOS_FARM_SEED
:
self
.
success
(
"
Ponos farm valid
"
)
else
:
farm
.
seed
=
PONOS_FARM_SEED
farm
.
save
()
self
.
warn
(
f
"
Ponos farm
{
farm
.
name
}
updated
"
)
except
Farm
.
DoesNotExist
:
Farm
.
objects
.
create
(
id
=
PONOS_FARM_ID
,
seed
=
PONOS_FARM_SEED
,
)
self
.
success
(
"
Ponos farm created
"
)
# an internal API user with a specific token
try
:
token
=
Token
.
objects
.
get
(
key
=
INTERNAL_API_TOKEN
)
self
.
check_user
(
token
.
user
)
except
Token
.
DoesNotExist
:
# Create a new internal user
user
,
_
=
User
.
objects
.
get_or_create
(
email
=
'
internal+bootstrap@teklia.com
'
,
defaults
=
{
'
display_name
'
:
'
Bootstrap Internal user
'
,
'
is_internal
'
:
True
,
'
is_admin
'
:
True
,
}
)
self
.
success
(
"
Created internal user
"
)
self
.
check_user
(
user
)
# Finally create a specific token for that user
if
hasattr
(
user
,
"
auth_token
"
):
# Support One-To-One relation
user
.
auth_token
.
delete
()
Token
.
objects
.
create
(
key
=
INTERNAL_API_TOKEN
,
user
=
user
)
self
.
success
(
f
"
Created token
{
INTERNAL_API_TOKEN
}
"
)
# an image server for local cantaloupe https://ark.localhost/iiif/2
try
:
server
=
ImageServer
.
objects
.
get
(
url
=
'
https://ark.localhost/iiif/2
'
)
if
server
.
id
!=
IMAGE_SERVER_ID
:
# Migrate existing images & server id in a single transaction
with
transaction
.
atomic
():
server
.
images
.
update
(
server_id
=
IMAGE_SERVER_ID
)
ImageServer
.
objects
.
filter
(
id
=
server
.
id
).
update
(
id
=
IMAGE_SERVER_ID
)
self
.
warn
(
f
"
Image server
{
server
.
id
}
updated to
{
IMAGE_SERVER_ID
}
"
)
# Update internal reference for updates below
server
.
id
=
IMAGE_SERVER_ID
# Update base settings
if
server
.
s3_bucket
!=
IMAGE_SERVER_BUCKET
or
server
.
s3_region
!=
IMAGE_SERVER_REGION
or
not
server
.
validated
:
server
.
s3_bucket
=
IMAGE_SERVER_BUCKET
server
.
s3_region
=
IMAGE_SERVER_REGION
server
.
validated
=
True
server
.
save
()
self
.
warn
(
"
Updated image server S3 settings
"
)
else
:
self
.
success
(
f
"
Image server
{
server
.
id
}
valid
"
)
except
ImageServer
.
DoesNotExist
:
try
:
server
=
ImageServer
.
objects
.
create
(
id
=
IMAGE_SERVER_ID
,
url
=
'
https://ark.localhost/iiif/2
'
,
s3_bucket
=
IMAGE_SERVER_BUCKET
,
s3_region
=
IMAGE_SERVER_REGION
,
display_name
=
'
Development local server
'
,
validated
=
True
,
)
self
.
success
(
"
Image server created
"
)
except
IntegrityError
as
e
:
self
.
fail
(
f
"
Failed to create image server:
{
e
}
"
)
return
# Check there is not already a local server with invalid path
# We'll merge its image into the new one
# This bad server may have been created by automatic IIIF server detection
try
:
bad_server
=
ImageServer
.
objects
.
get
(
url
=
'
https://ark.localhost/iiif
'
)
bad_server
.
merge_into
(
server
)
self
.
warn
(
f
"
Merged images from
{
bad_server
.
id
}
into
{
server
.
id
}
"
)
bad_server
.
delete
()
self
.
warn
(
"
Deleted old server
"
)
except
ImageServer
.
DoesNotExist
:
pass
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment