Skip to content

Commit 987ae0d

Browse files
techknowlogickalexellis
authored andcommitted
switch to use faasd
Signed-off-by: Matti R <[email protected]>
1 parent 5462a7d commit 987ae0d

File tree

4 files changed

+97
-32
lines changed

4 files changed

+97
-32
lines changed

_posts/2021-01-31-gitea-faas.md

Lines changed: 97 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "Gitea automation using OpenFaaS"
2+
title: "Extend and automate self-hosted Gitea with functions"
33
description: "Switching to Gitea doesn’t mean having to give up your bot automations"
44
date: 2021-01-31
55
image: /images/2021-01-gitea/gitea-sticker-header.jpg
@@ -23,46 +23,113 @@ In this post we will walk through building a simple bot to label pull-requests.
2323

2424
## Pre-requisites
2525

26-
For the purposes of this guide we'll use a local install of Kubernetes, but any Kubernetes cluster could be used. The tutorial should take you less than 15-30 minutes to try.
26+
For the purposes of this guide we'll create a local virtual machine and install faasd on it, but these instructions can be extended to use a Digital Ocean Droplet, or even a Raspberry Pi. The tutorial should take you less than 15-30 minutes to try.
2727

28-
A quick way to install all the tools you need is to use the [arkade](https://get-arkade.dev) cli tool.
28+
### Create a Virtual Machine and install faasd
29+
30+
[Multipass](https://multipass.run) is a lightweight virtual machine runner, think docker-compose but for ubuntu virtual machines. We'll use this to get a virtual machine with faasd up and running quickly.
2931

3032
```bash
31-
# Get arkade, and move it to $PATH
32-
curl -sLS https://dl.get-arkade.dev | sh
33-
sudo mv arkade /usr/local/bin/
33+
# Install multipass
34+
## On macOS you can install multipass with the brew command
35+
brew install multipass
36+
## or, on Linux, you can install using the snap command
37+
snap install multipass
3438

35-
# Run Kubernetes locally
36-
arkade get kind
39+
# Get VM Bootstrap instructions
40+
wget https://raw.githubusercontent.com/openfaas/faasd/master/cloud-config.txt
3741

38-
# OpenFaaS CLI
39-
arkade get faas-cli
42+
# Generate a key for SSH-ing into your VM
43+
ssh-keygen -t rsa -b 4096 -C "faasd" -f $PWD/faasd_ssh
4044

41-
# Create a cluster
42-
kind create cluster
45+
# Add your local key to the cloud-init file
46+
awk "NR==4 {\$0=\" - $(cat faasd_ssh.pub)\"} { print }" cloud-config.txt > tmp
47+
mv tmp cloud-config.txt
4348

44-
# Install OpenFaaS
45-
arkade install openfaas
49+
# Run a local VM
50+
multipass launch \
51+
--cloud-init cloud-config.txt \
52+
--name faasd
4653

47-
# Install Gitea
48-
arkade install gitea
54+
# Verify your VM has been started
55+
multipass info faasd
4956
```
5057

51-
Next, you'll need to portforward both Gitea, and OpenFaaS, as well as login into the OpenFaaS gateway with faas-cli
58+
### Connect faas-cli to your faasd install
59+
60+
Now that faasd is up and running, we'll need to login into the gateway with the `faas-cli`.
5261

5362
```bash
54-
# Forward the OpenFaaS gateway to your machine
55-
kubectl rollout status -n openfaas deploy/gateway
56-
kubectl port-forward -n openfaas svc/gateway 8080:8080 &
63+
# Install faas-cli on your host machine
64+
curl -sSLf https://cli.openfaas.com | sh
65+
66+
# Get IP
67+
export VM_IP=$(multipass info faasd | grep IPv4 | awk '{print $2}')
68+
echo "Your IP is: $VM_IP"
69+
70+
# Login into the faasd gateway
71+
export FAAS_PASSWORD=$(multipass exec faasd sudo cat /var/lib/faasd/secrets/basic-auth-password)
72+
export OPENFAAS_URL="http://$VM_IP:8080"
73+
echo $FAAS_PASSWORD | faas-cli login --password-stdin
5774

58-
# If basic auth is enabled, you can now log into your gateway:
59-
PASSWORD=$(kubectl get secret -n openfaas basic-auth -o jsonpath="{.data.basic-auth-password}" | base64 --decode; echo)
60-
echo -n $PASSWORD | faas-cli login --username admin --password-stdin
75+
# Test that faas-cli connects to gateway
76+
faas-cli list
77+
```
78+
79+
### Install Gitea on your Virtual Machine
6180

62-
# Forward the Gitea application to your machine
63-
kubectl -n default port-forward svc/gitea-http 3000:3000 &
81+
Next we'll need to install Gitea it up and running. This example uses the bleeding edge nightly version, but for production use you may wish to use the current stable version of Gitea.
82+
83+
```bash
84+
# SSH into your VM
85+
ssh -i ./faasd_ssh ubuntu@$VM_IP
86+
87+
# Download Gitea nightly version
88+
wget https://dl.gitea.io/gitea/master/gitea-master-linux-amd64
89+
mv gitea-master-linux-amd64 gitea
90+
chmod +x gitea
91+
92+
# Create base Gitea configuration
93+
cat >/home/ubuntu/app.ini <<EOL
94+
RUN_USER = ubuntu
95+
[server]
96+
DOMAIN = $(hostname -I | awk '{print$1}')
97+
DISABLE_SSH=true
98+
[database]
99+
DB_TYPE = sqlite3
100+
PATH = /home/ubuntu/gitea.db
101+
[security]
102+
INSTALL_LOCK = true
103+
[oauth2]
104+
ENABLE=false
105+
EOL
106+
107+
# Create Gitea database
108+
./gitea --config $(pwd)/app.ini migrate
109+
110+
# Create a Gitea user
111+
export GITEA_PASSWORD="TEMPOPENFAASPASSWORD"
112+
./gitea --config $(pwd)/app.ini admin user create --admin --name gitea_admin --password $GITEA_PASSWORD --email [email protected]
113+
114+
# Run Gitea
115+
screen -d -m ./gitea --config $(pwd)/app.ini web
116+
117+
# Exit out of the SSH connection
118+
exit
64119
```
65120

121+
### Sign into Gitea and create test repo
122+
123+
Finally after Gitea is installed, we'll need to verify the installation. To do this we will open up the Gitea interface in the browser and login into it.
124+
125+
In the install step we created an admin user with the username `gitea_admin` and password `TEMPOPENFAASPASSWORD`, we will take those and go to the login page at http://$VM_IP:3000/user/login
126+
127+
![gitea login page screenshot](/images/2021-01-gitea/gitea_login_screenshot.png)
128+
129+
Once logged in, we will create a git repository so when we need to add webhooks to it later it will be ready. In the top navigation bar we can select the `+` dropdown, and click the "New Repository" link. When we get to the new repository page we will name the repo `test_repo` and check the "Initialize Repository" option, just to have an example repository for testing purposes.
130+
131+
![screenshot to create a repository in gitea](/images/2021-01-gitea/create_repo_screenshot.png)
132+
66133
## Build and deploy the Gitea bot function
67134

68135
For this guide we will be using golang for the Gitea bot, but you can use any language that you are comfortable working with.
@@ -256,28 +323,26 @@ With the function now built, now it is time to deploy the function. First you'll
256323
GO111MODULE: on
257324
secrets:
258325
- webhook-secret # random string generated by you
259-
- gitea-host # host of your gitea instance, ex. https://gitea.example.com/
260-
- gitea-token # gitea api token generated from https://gitea.example.com/user/settings/applications
326+
- gitea-host # host of your gitea instance, ex. http://$VM_IP:3000/
327+
- gitea-token # gitea api token generated from http://$VM_IP:3000/user/settings/applications
261328
```
262329
263330
```bash
264331
faas-cli secret create webhook-secret --from-literal "abc123"
265-
faas-cli secret create gitea-host --from-literal "http://gitea-http.default.svc.cluster.local:3000/"
332+
faas-cli secret create gitea-host --from-literal "http://$VM_IP:3000/" # Use IP from setup
266333
faas-cli secret create gitea-token --from-literal "GET FROM GITEA GUI"
267334

268335
faas-cli up -f lgtmbot.yml
269336

270337
```
271338

272-
Finally, we need to configure the repo in Gitea that you would like to manage with this function. This can be done by going to the webhooks settings of the repo (ex https://gitea.example.com/org/repo/settings/hooks), and creating a new Gitea webhook with the secret being the one you set above.
339+
Finally, we need to configure the repo in Gitea that you would like to manage with this function. This can be done by going to the webhooks settings of the repo (ex http://$VM_IP:3000/gitea_admin/test_repo/settings/hooks), and creating a new Gitea webhook with the secret being the one you set above.
273340

274341
![setup webhook in gitea](/images/2021-01-gitea/setup_gitea_webhook.png)
275342

276343
## Wrapping Up
277344

278-
Now that we’ve seen how to create a simple bot using OpenFaaS, from here we can build upon this base and make more complex actions. The next step could be transforming the [lockbot](https://www.openfaas.com/blog/schedule-your-functions/) from an earlier post to support Gitea as well, or even extending [Derek](https://github.com/alexellis/derek/) to support Gitea. The possibilities are endless. Eventually we could end up with a number of functions to rival the GitHub apps marketplace.
279-
280-
This post uses OpenFaaS, but you could use faasd to keep the installation requirements a minimum.
345+
Now that we’ve seen how to create a simple bot using faasd, from here we can build upon this base and make more complex actions. The next step could be transforming the [lockbot](https://www.openfaas.com/blog/schedule-your-functions/) from an earlier post to support Gitea as well, or even extending [Derek](https://github.com/alexellis/derek/) to support Gitea. The possibilities are endless. Eventually we could end up with a number of functions to rival the GitHub apps marketplace.
281346

282347
### Taking it further
283348

113 KB
Loading
37.9 KB
Loading
6.06 KB
Loading

0 commit comments

Comments
 (0)