Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hypha
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ots
hypha
Commits
de4a4946
Unverified
Commit
de4a4946
authored
7 years ago
by
Todd Dembrey
Committed by
GitHub
7 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #14 from OpenTechFund/add-deployment-scripts
Add deployment scripts
parents
29da3cb4
549950b2
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fabfile.py
+171
-0
171 additions, 0 deletions
fabfile.py
with
171 additions
and
0 deletions
fabfile.py
0 → 100644
+
171
−
0
View file @
de4a4946
from
datetime
import
datetime
from
fabric.api
import
lcd
,
roles
,
runs_once
,
run
,
local
,
env
,
prompt
,
get
env
.
roledefs
=
{
'
production
'
:
[],
'
pre-production
'
:
[
'
otfpreprod@staging-1-a.bmyrk.torchbox.net
'
],
'
staging
'
:
[
'
otfstaging@staging-1-a.bmyrk.torchbox.net
'
],
}
@roles
(
'
production
'
)
def
deploy_production
():
# Remove this line when you're happy that this task is correct
raise
RuntimeError
(
"
Please check the fabfile before using it
"
)
run
(
'
git pull
'
)
run
(
'
pip install -r requirements.txt
'
)
_run_migrate
()
run
(
'
django-admin collectstatic --noinput
'
)
# 'restart' should be an alias to a script that restarts the web server
run
(
'
restart
'
)
_post_deploy
()
@runs_once
@roles
(
'
production
'
)
def
pull_production_data
():
# Remove this line when you're happy that this task is correct
raise
RuntimeError
(
"
Please check the fabfile before using it
"
)
_pull_data
(
env_name
=
'
production
'
,
remote_db_name
=
'
opentech
'
,
local_db_name
=
'
opentech
'
,
remote_dump_path
=
'
/usr/local/django/otf/tmp/
'
,
local_dump_path
=
'
/tmp/
'
,
)
@runs_once
@roles
(
'
production
'
)
def
pull_production_media
():
local
(
'
rsync -avz %s:
\'
%s
\'
/vagrant/media/
'
%
(
env
[
'
host_string
'
],
'
$CFG_MEDIA_DIR
'
))
@roles
(
'
staging
'
)
def
deploy_staging
():
_build_static
()
_deploy_static
()
run
(
'
git pull
'
)
run
(
'
pip install -r requirements.txt
'
)
_run_migrate
()
run
(
'
django-admin collectstatic --noinput
'
)
# 'restart' should be an alias to a script that restarts the web server
run
(
'
restart
'
)
_post_deploy
()
@runs_once
@roles
(
'
staging
'
)
def
pull_staging_data
():
# Remove this line when you're happy that this task is correct
raise
RuntimeError
(
"
Please check the fabfile before using it
"
)
_pull_data
(
env_name
=
'
staging
'
,
remote_db_name
=
'
otfstaging
'
,
local_db_name
=
'
otfstaging
'
,
remote_dump_path
=
'
/usr/local/django/otf/tmp/
'
,
local_dump_path
=
'
/tmp/
'
,
)
@runs_once
@roles
(
'
staging
'
)
def
pull_staging_media
():
local
(
'
rsync -avz %s:
\'
%s
\'
/vagrant/media/
'
%
(
env
[
'
host_string
'
],
'
$CFG_MEDIA_DIR
'
))
@runs_once
def
_pull_data
(
env_name
,
remote_db_name
,
local_db_name
,
remote_dump_path
,
local_dump_path
):
timestamp
=
datetime
.
now
().
strftime
(
'
%Y%m%d-%I%M%S
'
)
filename
=
'
.
'
.
join
([
env_name
,
remote_db_name
,
timestamp
,
'
sql
'
])
remote_filename
=
remote_dump_path
+
filename
local_filename
=
local_dump_path
+
filename
params
=
{
'
remote_db_name
'
:
remote_db_name
,
'
remote_filename
'
:
remote_filename
,
'
local_db_name
'
:
local_db_name
,
'
local_filename
'
:
local_filename
,
}
# Dump/download database from server
run
(
'
pg_dump {remote_db_name} -xOf {remote_filename}
'
.
format
(
**
params
))
run
(
'
gzip {remote_filename}
'
.
format
(
**
params
))
get
(
'
{remote_filename}.gz
'
.
format
(
**
params
),
'
{local_filename}.gz
'
.
format
(
**
params
))
run
(
'
rm {remote_filename}.gz
'
.
format
(
**
params
))
# Load database locally
local
(
'
gunzip {local_filename}.gz
'
.
format
(
**
params
))
_restore_db
(
local_db_name
,
local_filename
)
def
_restore_db
(
local_db_name
,
local_dump_path
):
params
=
{
'
local_db_name
'
:
local_db_name
,
'
local_dump_path
'
:
local_dump_path
,
}
local
(
'
dropdb {local_db_name}
'
.
format
(
**
params
))
local
(
'
createdb {local_db_name}
'
.
format
(
**
params
))
local
(
'
psql {local_db_name} -f {local_dump_path}
'
.
format
(
**
params
))
local
(
'
rm {local_dump_path}
'
.
format
(
**
params
))
newsuperuser
=
prompt
(
'
Any superuser accounts you previously created locally will
'
'
have been wiped. Do you wish to create a new superuser? (Y/n):
'
,
default
=
"
Y
"
)
if
newsuperuser
.
strip
().
lower
()
==
'
y
'
:
local
(
'
django-admin createsuperuser
'
)
@runs_once
def
_run_migrate
():
# Run migrations
run
(
'
django-admin migrate --noinput
'
)
# Create a table for database cache backend
run
(
'
django-admin createcachetable
'
)
@runs_once
def
_post_deploy
():
# clear frontend cache only on production
if
'
production
'
in
env
.
effective_roles
:
run
(
'
for host in $(echo $CFG_HOSTNAMES | tr
\'
,
\'
\'
\'
); do echo
"
Purge cache for $host
"
;
'
'
ats-cache-purge $host;
'
'
done
'
)
# update search index
run
(
'
django-admin update_index
'
)
@runs_once
def
_build_static
():
# Build a specific branch
build_branch
=
'
master
'
current_branch
=
local
(
'
git rev-parse --abbrev-ref HEAD
'
)
if
current_branch
!=
build_branch
:
raise
RuntimeError
(
"
Please switch to
'
{}
'
before deploying
"
.
format
(
build_branch
))
local
(
'
git pull
'
)
with
lcd
(
'
/vagrant/opentech/static_src/
'
):
local
(
'
yarn build:prod --silent
'
)
@runs_once
def
_deploy_static
():
# Copy the compiled static files to the server
local
(
"
rsync -avz /vagrant/opentech/static_compiled %s:
'
../app/opentech/
'"
%
(
env
[
'
host_string
'
]))
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