Cardiff University
School of Computer Science & Informatics Dr
CMT302 EC & Innovation
SPRING LABS – WEEK 7 Website Deployment on OpenShift.
Copyright By PowCoder代写 加微信 powcoder
Objectives:
– Learn how to deploy a Flask website on OpenShift
1. Initial setup:
We will be using Gunicorn1 (a Python Web Server Gateway Interface HTTP server) to deploy our flask website to OpenShift.
1.1. To make it work, you need to rename run.py into wsgi.py.
1.2. Make sure that the first line reads as following (you have add “as
application” added):
1.3. Check the structure of your project is similar to that shown in Fig.1.
1.4. Push your local repo to the remote repo on GitLab, excluding unnecessary files and directories, e.g. __pycache__, your virtual environment dir venv, etc. These are not needed for deployment on OpenShift, and will just clog up your GitLab. (Hint: specify all the files you want to exclude in .gitignore file.)
1.5. Go to GitLab, and check that all the files from the local directory have been successfully pushed.
Fig. 1. Project dir structure
NB: OpenShift, like other systems (MySQL server, etc.) will require you to use a VPN, if you are connecting to them from a non-Cardiff network.
If you have problems pushing to your remote, the most likely culprit is not supplying GitLab with an SSH key. See instructions here on how to add an SSH key to GitLab: https://docs.cs.cf.ac.uk/notes/ using-gitlab/
1 https://gunicorn.org
A short video, showing the steps you need go through to deploy your website is available on Learning Central.
2. OpenShift:
Go to our OpenShift server, https://openshift.cs.cf.ac.uk/, and log in with your University network credentials. 2
Create a project, as follows:
2.1. Click on Create Project. (In this example we are creating my-store project, but you can name your project whatever you like).
2.2. Fill in the form, e.g. ‘Name: my-store’, ‘Display Name: Books Online’, and anything you wish to use to describe your project (Fig.2).
2.3. Click on Create button.
2.4. Click on the project name you have just created.
2.5. Click on Browse Catalog.
Add a Python Cartridge:
2.6. Search catalog for Python.
2.7. On the pop-up dialog, click Next
2.8. On ‘Configuration’ page:
Fig. 2. Create Project Dialog
– for *Application Name type the name of the project you have just created (i.e. my-store); – and for *Git Repository the name of your git repository, by copying it from GitLab: go to the
repository ‘home’ page -> click on Clone dropdown button -> Clone with SSH. – Click on Close.
Fig.3 shows an example of the dialogs you will see when performing Tasks 2.7 and 2.8.
Fig. 3. Adding a Python Cartridge to the project
2 If you are on a Mac, Safari might not work well if at all with our OpenShift. Instead, use another browser, e.g. Firefox, Brave or Opera.
Project Build:
2.9. Go back to your project, and then Builds/Builds. You will see that fetching of the source fails (Fig.4). If you inspect the Build’s log, you will see the reason why this has happened, i.e. you have not yet given OpenShift permission to connect to your repo. This is done by creating a ‘Secret’.
Create a Secret:
2.10. To create a Secret (Fig. 5):
– Go to Resources -> Secrets -> Create Secret button;
– select ‘Source Secrete’ for Secret Type; type *Secret Name (e.g. blog-secret);
– select ‘SSH Key’ for Authentication Type;
– and then either select the file with your SSH private key, or
paste your private key into the text box;
– click on Create.
– Go back to Resources -> Secrets. Select the secret you
– have just created and click Add to Application -> select your application from the dropdown menu -> Save.
2.11. The secret also needs to be added to the builds:
– Builds -> Builds,
– then select your application,
Fig. 5. Example, showing creation of Secret books-secret
– then Actions button -> Edit option -> under *Git Repository URL select ‘advanced options’ link (in blue) -> Source Secret -> select the secret you have just created -> Save.
Fig. 6. Example, showing adding secret ‘books-secret’ to the Build. 3/4
Project Re-Build and Deployment:
– Go back to Builds, and click on Start Build. The second build should now succeed.
– After the build has completed successfully, OpenShift will deploy your application (this might take some time), and you can then access your website from URL specified on the Overview page. The URL will be something like: http://
– NB: Deployment might not start automatically. To start it manually: go to Applications -> Deployments -> select your project -> click on Deploy button.
NB: If you modify your code, you need to push your modified code to GitLab, and then re-build and re-deploy your website on OpenShift.
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com