Jump to content
Wikitech

Help:Toolforge/Building container images/My first Buildpack Python tool

From Wikitech

Toolforge

[edit ]

Python webservices are used by many existing tools. Python is a high-level, interpreted programming language with many available libraries for making webservices and integrating with MediaWiki.

This stub webservice is designed to get a sample Python application installed onto Toolforge using the new build service, as quickly as possible. The application is written using the Flask framework.

The guide will teach you how to:

Getting started

Prerequisites

Skills

Accounts

Step-by-step guide

Step 1: Create a new tool account

  1. Follow the Toolforge quickstart guide to create a Toolforge tool and SSH into Toolforge.
    • For the examples in this tutorial, sample-python-buildpack-app is used to indicate places where your unique tool name is used in another command.
  2. Make sure to create a git repository for the tool, you can get one like this:
    1. Log into the toolforge admin page
    2. Select your tool
    3. On the left side panel, under Git repositories click create repository
    4. Copy the url in the Clone section
      1. There's a private url, that we will use to clone it locally, starting with "git": git@gitlab-ssh.wikimedia.org:toolforge-repos/sample-python-buildpack-app.git
      2. And a public one, that we will use to build the app in toolforge, starting with "https": https://gitlab.wikimedia.org/toolforge-repos/sample-python-buildpack-app.git

Step 2: Create a basic Flask WSGI webservice for gunicorn

What is Flask?

Flask is a popular web development framework for Python.

What is gunicorn?

Gunicorn is a web server popular for running Python applications.

How to create a basic Flask WSGI webservice

Clone your tool git repository

You will have to clone the tool repository to be able to add code to it, on your local computer (with git installed) you can run:

laptop:~$ gitclonegit@gitlab-ssh.wikimedia.org:toolforge-repos/sample-python-buildpack-app.git
laptop:~$ cdsample-python-buildpack-app

That will create a folder called sample-python-buildpack-app. We are going to put the code in that folder.

Add the Flask and gunicorn dependencies
It is Python best practice to use a file named requirements.txt to keep track of the library dependencies of applications. If you use poetry, you should use the latest buildpack
laptop:~sample-python-buildpack-app$ cat>requirements.txt<<EOF
flask
gunicorn
EOF
Create a 'hello world' WSGI application
app.py
# -*-coding:utf-8-*-
#
# ThisfileispartoftheToolforgeflaskWSGItutorial
#
# Copyright(C)2017BryanDavisandcontributors
#
# Thisprogramisfreesoftware:youcanredistributeitand/ormodifyit
# underthetermsoftheGNUGeneralPublicLicenseaspublishedbytheFree
# SoftwareFoundation,eitherversion3oftheLicense,or(atyouroption)
# anylaterversion.
#
# Thisprogramisdistributedinthehopethatitwillbeuseful,butWITHOUT
# ANYWARRANTY;withouteventheimpliedwarrantyofMERCHANTABILITYor
# FITNESSFORAPARTICULARPURPOSE.SeetheGNUGeneralPublicLicensefor
# moredetails.
#
# YoushouldhavereceivedacopyoftheGNUGeneralPublicLicensealong
# withthisprogram.Ifnot,see<http://www.gnu.org/licenses/>.
import flask
app = flask.Flask(__name__)
@app.route('/')
def index():
 return 'Hello World!'
The 'Hello World!' file above starts with a license header that places it under the GPLv3+ license.

Code on Toolforge must always be licensed under an Open Source Initiative (OSI) approved license. See the Right to fork policy for more information on this Toolforge policy.

Create the Procfile

The Procfile is based on heroku's procfile, though we don't support all it's features, for now we only use the web entry point to get the command your server will be start with:

laptop:~sample-python-buildpack-app$ cat>Procfile<<EOF
web: gunicorn --workers=4 --bind=0.0.0.0 --forwarded-allow-ips=* app:app
EOF
Commit your changes and push
laptop:~sample-python-buildpack-app$ gitadd.
laptop:~sample-python-buildpack-app$ gitcommit-m"First commit"
laptop:~sample-python-buildpack-app$ gitpushoriginmain


Build the image

Now we have to ssh to login.toolforge.org and start the build for the image:

laptop:~sample-python-buildpack-app$ sshlogin.toolforge.org# or the equivalent with PuTTY
dcaro@tools-sgebastion-10$ becomesample-python-buildpack-app
tools.sample-python-buildpack-app@tools-sgebastion-10$ toolforgebuildstarthttps://gitlab.wikimedia.org/toolforge-repos/sample-python-buildpack-app.git
You have to pass the public url of the git repository otherwise it will not be able to clone it. For GitLab, you can get this by clicking the Code button and coying the "Clone with HTTPS" URL.
Wait for the build to finish

You can check the status of the build like this:

tools.sample-python-buildpack-app@tools-sgebastion-10:~$ toolforgebuildshow

You have to wait for the status to be Status: ok.

Start the webservice
tools.sample-python-buildpack-app@tools-sgebastion-10$ toolforgewebservicebuildservicestart--mount=none
Starting webservice.

Once the webservice is started, navigate to https://sample-python-buildpack-app.toolforge.org in your web browser, and see a 'Hello World!' message. It might take a few minutes until it is reachable.

Notes

You can see the code used in this example here: https://gitlab.wikimedia.org/toolforge-repos/sample-python-buildpack-app

Troubleshooting

See Help:Toolforge/Build_Service#Troubleshooting.

See also

Communication and support

Support and administration of the WMCS resources is provided by Wikimedia Foundation staff and Wikimedia movement volunteers. Please reach out with questions and join the conversation:

Discuss and receive general support
Stay aware of critical changes and plans
Track work tasks and report bugs

Use a subproject of the #Cloud-Services Phabricator project to track confirmed bug reports and feature requests about the Cloud Services infrastructure itself

Read stories and WMCS blog posts

Read posts about Cloud Services on the Wikimedia Technical Blog. (Older posts can be read from the Cloud Services Blog.)

AltStyle によって変換されたページ (->オリジナル) /