Skip to content

Commit e31223d

Browse files
TyHiljpahm
andauthored
Run api-tools daily on google cloud (#54)
* Docker stuff * GitHub Action * Quotes * is the secret there? * deployment env * Revert "is the secret there?" This reverts commit 086d3f3. * use federated thing * permissions * provider * github/actions-oidc-debugger * Revert "github/actions-oidc-debugger" This reverts commit 12cfd92. * Use gcloud CLI * build-push-action * docker/login-action@ * id auth * token_format: access_token * google-github-actions/deploy-cloudrun * service -> job * bump versions * region * scrape mazevo too * only on develop * Revert "only on develop" This reverts commit 65a85db. * big region change * remove exec * Reapply "only on develop" This reverts commit eab3852. * mazevo indentation * print when done in mazevo and astra scrapers for logs * detect empty env values * Use a service account to auth the env * Revert "Reapply "only on develop"" This reverts commit aa4309d. * Reapply "Reapply "only on develop"" This reverts commit 6ca207e. --------- Co-authored-by: Josh <[email protected]>
1 parent ba272d9 commit e31223d

File tree

8 files changed

+142
-39
lines changed

8 files changed

+142
-39
lines changed

.github/workflows/deploy.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# This workflow deploys the repository to run daily on google cloud run
2+
3+
name: Deploy
4+
5+
on:
6+
push:
7+
branches: develop
8+
9+
jobs:
10+
main:
11+
runs-on: ubuntu-latest
12+
environment: deployment
13+
14+
permissions:
15+
contents: 'read'
16+
id-token: 'write'
17+
18+
steps:
19+
- id: auth
20+
name: Google Cloud Auth
21+
uses: google-github-actions/auth@v2
22+
with:
23+
token_format: access_token
24+
workload_identity_provider: projects/762526944259/locations/global/workloadIdentityPools/pool/providers/github-deploy
25+
service_account: '[email protected]'
26+
27+
- name: Set up Google Cloud
28+
uses: google-github-actions/setup-gcloud@v2
29+
30+
- name: Login to Artifact Registry
31+
uses: docker/login-action@v3
32+
with:
33+
registry: us-central1-docker.pkg.dev
34+
username: oauth2accesstoken
35+
password: ${{ steps.auth.outputs.access_token }}
36+
37+
- name: Build Docker image
38+
uses: docker/build-push-action@v6
39+
with:
40+
push: true
41+
tags: us-central1-docker.pkg.dev/api-tools-451421/runners/daily-update-events
42+
43+
- name: Upload to dev job
44+
uses: google-github-actions/deploy-cloudrun@v2
45+
with:
46+
job: 'daily-update-events-dev'
47+
image: 'us-central1-docker.pkg.dev/api-tools-451421/runners/daily-update-events'
48+
region: us-central1
49+
50+
- name: Upload to prod job
51+
uses: google-github-actions/deploy-cloudrun@v2
52+
with:
53+
job: 'daily-update-events-prod'
54+
image: 'us-central1-docker.pkg.dev/api-tools-451421/runners/daily-update-events'
55+
region: us-central1

.github/workflows/go.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ name: Go
55

66
on:
77
push:
8-
branches: [ "develop" ]
8+
branches: develop
99
pull_request:
10-
branches: [ "develop" ]
10+
branches: develop
1111
workflow_dispatch:
1212

1313
jobs:
@@ -17,10 +17,10 @@ jobs:
1717
steps:
1818

1919
- name: Checkout
20-
uses: actions/checkout@v3
20+
uses: actions/checkout@v4
2121

2222
- name: Set up Go
23-
uses: actions/setup-go@v4
23+
uses: actions/setup-go@v5
2424
with:
2525
go-version: '1.23'
2626

Dockerfile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
FROM golang:1.23 AS builder
2+
3+
WORKDIR /app
4+
COPY . .
5+
6+
# Run setup and checks
7+
RUN make setup
8+
RUN make check
9+
RUN make build
10+
11+
# Use a lightweight final image
12+
FROM debian:12-slim
13+
WORKDIR /app
14+
15+
# Install gcloud CLI
16+
RUN apt-get update && apt-get install -y wget gnupg apt-transport-https lsb-release ca-certificates
17+
RUN wget -qO - https://packages.cloud.google.com/apt/doc/apt-key.gpg | gpg --dearmor > /usr/share/keyrings/cloud.google.gpg
18+
RUN echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
19+
RUN apt-get update && apt-get install -y google-cloud-sdk
20+
21+
# Install chromium
22+
RUN apt-get update && apt-get install -y chromium
23+
ENV CHROMIUM_BIN /usr/bin/chromium
24+
ENV GOOGLE_CHROME_BIN /usr/bin/chromium # Also set this for compatibility
25+
26+
# Copy build file from builder
27+
COPY --from=builder /app/api-tools /app/api-tools
28+
COPY deploy.sh /app/deploy.sh
29+
30+
RUN chmod +x /app/deploy.sh
31+
ENTRYPOINT ["/app/deploy.sh"]

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ check:
99
go mod tidy
1010
go vet ./...
1111
staticcheck ./...
12-
gofmt -w ./..
13-
goimports -w ./..
12+
gofmt -w .
13+
goimports -w .
1414

1515
build: ./main.go
1616
go build -o $(EXEC_NAME) ./main.go

deploy.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
3+
# service account
4+
gcloud secrets versions access latest --secret="$SERVICE_ACCOUNT_SECRET_NAME" > service_account.json
5+
gcloud auth activate-service-account --key-file=service_account.json
6+
rm service_account.json
7+
8+
# .env
9+
gcloud secrets versions access latest --secret="$ENV_SECRET_NAME" > .env
10+
11+
# Scrape, parse, and upload
12+
./api-tools -scrape -mazevo -verbose
13+
#./api-tools -parse -mazevo -verbose
14+
./api-tools -scrape -astra -verbose
15+
#./api-tools -parse -astra -verbose
16+
#./api-tools -upload -events -verbose

scrapers/astra.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ func ScrapeAstra(outDir string) {
102102
date = date.Add(time.Hour * 24)
103103
}
104104

105+
log.Printf("Scraped Astra up to %s!", date.Format("2006-01-02"))
106+
105107
// Write event data to output file
106108
days = fmt.Sprintf("%s}", days)
107109
fptr, err := os.Create(fmt.Sprintf("%s/reservations.json", outDir))

scrapers/mazevo.go

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,38 +43,37 @@ func ScrapeMazevo(outDir string) {
4343
endDate := date.Add(time.Hour * 24 * 365).Format(time.RFC3339)
4444

4545
// Request events
46-
stringBody := ""
47-
{
48-
url := "https://east.mymazevo.com/api/PublicCalendar/GetCalendarEvents"
49-
requestBodyMap := map[string]string{
50-
"apiKey": apikey,
51-
"end": endDate,
52-
"start": startDate,
53-
}
54-
requestBodyBytes, _ := json.Marshal(requestBodyMap)
55-
requestBody := bytes.NewBuffer(requestBodyBytes)
56-
req, err := http.NewRequest("POST", url, requestBody)
57-
if err != nil {
58-
panic(err)
59-
}
60-
req.Header = http.Header{
61-
"Content-type": {"application/json"},
62-
"Accept": {"application/json"},
63-
}
64-
res, err := cli.Do(req)
65-
if err != nil {
66-
panic(err)
67-
}
68-
if res.StatusCode != 200 {
69-
log.Panicf("ERROR: Status was: %s\nIf the status is 404, you've likely been IP ratelimited!", res.Status)
70-
}
71-
body, err := io.ReadAll(res.Body)
72-
if err != nil {
73-
panic(err)
74-
}
75-
res.Body.Close()
76-
stringBody = string(body)
46+
url := "https://east.mymazevo.com/api/PublicCalendar/GetCalendarEvents"
47+
requestBodyMap := map[string]string{
48+
"apiKey": apikey,
49+
"end": endDate,
50+
"start": startDate,
7751
}
52+
requestBodyBytes, _ := json.Marshal(requestBodyMap)
53+
requestBody := bytes.NewBuffer(requestBodyBytes)
54+
req, err := http.NewRequest("POST", url, requestBody)
55+
if err != nil {
56+
panic(err)
57+
}
58+
req.Header = http.Header{
59+
"Content-type": {"application/json"},
60+
"Accept": {"application/json"},
61+
}
62+
res, err := cli.Do(req)
63+
if err != nil {
64+
panic(err)
65+
}
66+
if res.StatusCode != 200 {
67+
log.Panicf("ERROR: Status was: %s\nIf the status is 404, you've likely been IP ratelimited!", res.Status)
68+
}
69+
body, err := io.ReadAll(res.Body)
70+
if err != nil {
71+
panic(err)
72+
}
73+
res.Body.Close()
74+
stringBody := string(body)
75+
76+
log.Printf("Scraped Mazevo up to %s!", endDate)
7877

7978
// Write event data to output file
8079
fptr, err := os.Create(fmt.Sprintf("%s/mazevoReservations.json", outDir))

utils/methods.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var Headless = true
2828
// Finds .env value and produces proper error if not found
2929
func GetEnv(name string) (string, error) {
3030
value, exists := os.LookupEnv(name)
31-
if !exists {
31+
if !exists || value == "" {
3232
return "", errors.New(name + " is missing from .env!")
3333
}
3434
return value, nil
@@ -152,7 +152,7 @@ func RefreshAstraToken(chromedpCtx context.Context) map[string][]string {
152152
for _, cookie := range cookies {
153153
cookieStr = fmt.Sprintf("%s%s=%s; ", cookieStr, cookie.Name, cookie.Value)
154154
if cookie.Name == "UTXDallas.ASPXFORMSAUTH" {
155-
VPrintf("Got new token: PTGSESSID = %s", cookie.Value)
155+
VPrintf("Got new token: UTXDallas.ASPXFORMSAUTH = %s", cookie.Value)
156156
gotToken = true
157157
}
158158
}

0 commit comments

Comments
 (0)