Migrate a project to Divio Cloud¶

In this tutorial you will take an existing Django project and port it to Divio Cloud.

The project (The Opinions Company) complete withcustom applications, database content, media files and styling is contained in a GitHub repository.

Using it will allow you to go through all the required steps for a project migration in a controlledenvironment, and practise them before applying your knowledge to a project of your own.

In the tutorial, we’ll be using the command line rather than the Divio app. Although nearly everystep here could equally well be run via the Divio app, the feedback from the command line tools ismore explicit and helps see what exactly is happening.

If you’re interested in knowing how the controls of the Divio app correspond to commands, seeThe Divio application.

Set the project up locally¶

We’ll begin by setting up the source project locally. Clone the project, and cd into the directory:

  1. git clone git@github.com:divio/the-opinions-company.git
  2. cd the-opinions-company

Create and activate a virtual environment:

  1. python3.6 -m venv env
  2. source env/bin/activate

Install the project’s dependencies into the virtual environment:

  1. pip install -r requirements.txt

Run migrations to create the empty database tables:

  1. python manage.py migrate

Load the database content from the database.json file:

  1. python manage.py loaddata database.json

Start the runserver:

  1. python manage.py runserver

You can now open the site at http://localhost:8000. The username and password areadmin/admin. Check that you can log in and use the site and its admin.

Dump database to a JSON file (if required)¶

If you have made some additional changes to the database that you’d like to load into the DivioCloud project, you can dump them with:

  1. python manage.py dumpdata --natural-foreign --natural-primary --indent 4 > database.json

Migrate to Divio Cloud¶

Create a new Divio Cloud project¶

On the Divio Cloud control panel, create a new project, with the options:

  • Python 3
  • django CMS
  • Blank boilerplate
    Once the new project has been provisioned, see its Addons view.

You need to install django CMS Bootstrap 4 (important: make sure you select version 1.0.0)

Select the Installed button, and check the other versions.

|Addon|Version
|Aldryn Addons|1.0.2
|Aldryn Django|1.11.x.y
|Aldryn django CMS|3.5.x.y
|Aldryn SSO|1.3.0
|django CMS Bootstrap 4|1.1.1
|django CMS File|2.0.2
|django CMS Google Map|1.1.0
|django CMS History|0.5.3
|django CMS Link|2.1.2
|django CMS Picture|2.0.6
|django CMS Snippet|2.0.0
|django CMS Style|2.0.2
|django CMS Text CKEditor|3.5.3
|django CMS Video|2.0.4
|django Filer|1.3.2

Important

If necessary, downgrade any installed addons to the versions listed above . This is toensure compatibility with the database provided for the purposes of this tutorial. If you runinto an error, particularly when attempting to load the database into your new project, it isprobably because one of the addons has been updated and requires a new database schema.

In this case, select the correct version of the addon on the Control Panel, and in your project,run:

  1. git pull # pull down the configuration into the local project
  2. docker-compose build web # rebuild the web container

Set the project up locally¶

Run the divio project setup command (the exact command can be found in the _Local development_view).

Check the Divio project’s dependencies¶

Paste the entire contents of your original project’s requirements.txt file into therequirements.in of the Divio project, after the auto-generated section.

We now have to scan through this line-by-line, to check for conflicts. In the Addons view for theproject in the Control Panel, select Installed addons, and for each line you have added,cross-check to see whether it has already been included.

You will find that most are already present, and you’ll also be able to see that in them listed inthe auto-generated section in the file.

As you confirm that each one is present, you can remove the corresponding line that you copied over- it’s no longer required. For example (note that our projects, versionsand dependencies all change over time, so the actual results you see may be slightly different):

  1. django-cms>=3.5,<3.6 # No need to add this manually
  2.  
  3. # django CMS plugins and addons
  4.  
  5. djangocms-text-ckeditor>=3.6.0 #
  6. djangocms-link>=2.1 #
  7. djangocms-style>=2.0 # You can expect all of these to be
  8. djangocms-googlemap>=1.1 # present already amongst the addons
  9. djangocms-snippet>=2.0 # included in the project, so there
  10. djangocms-video>=2.0 # is probably no need for them to be
  11. djangocms-file>=2.0,<3.0 # added manually to the requirements.in
  12. djangocms-picture>=2.0,<3.0 # file.
  13. django-filer>=1.3 #
  14. djangocms-bootstrap4==1.0.0 #

Some may or may not be explicitly listed amongst the project’s addons, but can be found in thesetup.py of django CMS (make sureyou’re looking at the right version), so once again, they won’t need to be included, though youshould still check that the version numbers are compatible.

  1. # Django dependencies (specified in django CMS's setup.py)
  2.  
  3. Django<2.0 # Already installed by Aldryn Django
  4.  
  5. django-classy-tags>=0.7 # These dependencies are specifed by
  6. django-sekizai>=0.9 # django CMS, so will be installed
  7. django-treebeard>=4.0,<5.0 # automatically anyway.
  8. djangocms-admin-style>=1.2,<1.3 #

The original requirements.txt file lists some further Python dependencies. You may recogisesome of them (and that, for example, easy_thumbnails is a dependency of Django Filer). Howeverif you need to, you can check the Divio Project to see what has already been installed, withdocker-compose run —rm web pip freeze. All the following should already be present, and do notneed to be listed manually:

  1. # Python dependencies
  2.  
  3. html5lib>=0.999999,<0.99999999 #
  4. Pillow>=3.0 # Should all be present in the Divio
  5. pytz # Project's environment.
  6. six #
  7. easy_thumbnails #

Finally, there is the Polls application, installed via pip from GitHub:

  1. # polls
  2.  
  3. -e git+git@github.com:divio/django-polls.git#egg=django-polls

This will need to be specified in the requirements.in file. However, the -e (editable)option makes little sense in this context, and our pip setup cannot handle requirements inthis format. Instead, you need to provide the URL of an archive, in thiscase:

  1. https://github.com/divio/django-polls/archive/b89f59b933113b82c49062830912c42a8fc15c77.zip

We use the commit, because otherwise our pip system could cache an older version.

And that is the only requirement you need to add manually to the requirements.in file.

Copy the polls_cms_integration application¶

The polls_cms_integration application is in the project folder of the original project. Thisneeds to be copied to the same place in the Divio project.

Test the build¶

You can now test whether the project will build:

  1. docker-compose build web

If you run into an error, you most likely either have a dependency version conflict, or thecollectstatic command in the Dockerfile cannot run, because a required dependency ismissing. This will need to be resolved before you can proceed.

Populate the INSTALLED_APPS¶

In the original project, all the INSTALLED_APPS are listed manually. In a Divio project,most of them will be added automatically by the addons. You need to ensure that your Divioproject includes all those in the original project.

In this case,

  1. polls
  2. polls_cms_integration

both need to be added manually (the_opinions_company is also listed, but this is just theproject name and doesn’t need to be added).

List:

  1. 'polls_cms_integration',
  2. 'polls',

in the:

  1. INSTALLED_APPS.extend([
  2. [...]
  3. ])

section so that they will be added.

Note

Our project is quite simple - in a more complex project, you can use diff on the lists ofINSTALLED_APPS to help ensure you don’t miss any.

Run migrations to create tables for the new applications:

  1. docker-compose run --rm web python manage.py migrate

Transfer other settings¶

Your original project’s settings need to be transferred to the Divio project. Settings in Divioprojects can be handled in multiple ways:

The best way to maintain the CMS_TEMPLATES setting in a Divio project is via the Aldryn djangoCMS addon’s configuration form, and ultimately that is what we will do (in the local version of theproject, you can see this configuration stored in addons/aldryn-djangocms/settings.json).

For now however it’s easiest to include the setting in the settings.py file temporarily, soadd:

  1. CMS_TEMPLATES = (
  2. ('content.html', 'Content'),
  3. )

Prepare the Postgres database of the Divio project¶

The database has so far been migrated, but that’s all.

Now you can import the dumped JSON data. Copy database.json over from the original project, andrun:

  1. docker-compose run --rm web python manage.py loaddata database.json

Errors from loaddata

If this doesn’t work, it’s most likely for one of two reasons:

  • you may have performed an operation that writes data to the tables - even logging in justonce will do this
  • one of the addons in the project does not match the version in the original project -check the versions carefully, and if necessary rebuild theweb container with the correct versions.
    In either case, you will need to restore the database to its newly-migrated state, followingthe steps in Reset the database.

Copy site templates¶

Next, we need to copy the two templates base.html and content.html template fromthe_opinions_company/templates in the original project to templates in the Divio project,as well as the includes directory.

Copy static files¶

Copy all the folders in the_opinions_company/static to static.

Copy media¶

Copy media into the data directory of the Divio project.

Start the runserver¶

  1. docker-compose up

Check the site¶

Once again, check that the site works as expected.

Now you’re ready to push your work to the Cloud.

Push your changes to the Divio Cloud environment¶

Code¶

Earlier, we added:

  1. CMS_TEMPLATES = (
  2. ('content.html', 'Content'),
  3. )

to the settings.py. That was only a temporary expedient - remove that now, because you don’twant to push that.

Instead, in your project in the Control Panel, go to the Addons > Aldryn django CMS >Configure, and in the CMS Templates field apply:

  1. [["content.html", "Content"]]

Now you can push the rest of your code. Run git status to see what has been changed. git
add
the changes you want to push:

  1. git add requirements.in settings.py polls_cms_integration static templates

And:

  1. git commit -m "Set up The Opinions Company as a Divio project"

Finally:

  1. git pull # merge the changes you made in the Control Panel
  2. git push origin develop # push local changes

Database¶

Push the database:

  1. divio project push db

Media¶

And the media files:

  1. divio project push media

Deploy the new Divio Cloud project¶

On the Control Panel, you see that there are now a number of undeployed commits, representing thework you have done.

You can hit Deploy on the Control Panel, or run:

  1. divio project deploy

And that’s it! Your project is now running in the Cloud.

原文: http://docs.divio.com/en/latest/introduction/09-migrate-project.html