Commit facdc40a authored by Miklós Tóth's avatar Miklós Tóth 🤷
Browse files

Login and team getting done

parent 5921d9e1
Pipeline #8881 failed with stages
in 1 minute and 11 seconds
image: golang:alpine
variables:
CI_REGISTRY_IMAGE: miketth/qpa-puller
CI_REGISTRY: docker.io
stages:
- build
- deploy
build:
stage: build
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [ "" ]
cache:
paths:
- cache
only:
refs:
- master
script:
- mkdir -p cache
- TAG=latest
- |
[[ ! "${CI_COMMIT_REF_NAME:-master}" = "master" ]] && TAG="$CI_COMMIT_REF_NAME"
- mkdir -p /kaniko/.docker
- AUTH=$(echo -n "${CI_REGISTRY_USER}:${CI_REGISTRY_PASSWORD}" | base64)
- |
cat << EOF > /kaniko/.docker/config.json
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "${AUTH}"
}
}
}
EOF
- /kaniko/executor --cache=true --cache-repo="${CI_REGISTRY_IMAGE}-cache:${CI_COMMIT_SHORT_SHA}" --cache-dir=$CI_PROJECT_DIR/cache --use-new-run --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination "${CI_REGISTRY_IMAGE}:${TAG}" --destination "${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHORT_SHA}"
deploy:
stage: deploy
image:
name: bitnami/kubectl
entrypoint: [ "" ]
environment:
name: matrix
kubernetes:
namespace: matrix
only:
refs:
- master
script:
- kubectl rollout restart deployment qpa-puller
FROM golang:alpine as build
COPY . /build
WORKDIR /build
RUN go build -v -o /app .
FROM alpine
WORKDIR /app
COPY --from=build /app /app/app
ENV GIN_MODE release
ENTRYPOINT ["/bin/sh", "-c"]
CMD ["/app/app"]
EXPOSE 8086
\ No newline at end of file
package main
import (
"github.com/caarlos0/env/v6"
"log"
)
type config struct {
MgmtPort string `env:"MGMT_PORT" envDefault:":8086"`
}
var c config
func init() {
e := env.Parse(&c)
if e != nil {
log.Fatal(e)
}
}
......@@ -3,3 +3,37 @@ module git.sch.bme.hu/kszk/schmatrix/games/qpa-team-puller
replace git.sch.bme.hu/kszk/schmatrix/games/qpa-team-puller => ./
go 1.17
require (
git.sch.bme.hu/kszk/schmatrix/games/aggregator-backend v0.0.0-20211002191021-b2ba927df5b8 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/caarlos0/env/v6 v6.7.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.7.4 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.9.0 // indirect
github.com/go-redis/redis/v8 v8.11.3 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/heptiolabs/healthcheck v0.0.0-20180807145615-6ff867650f40 // indirect
github.com/jinzhu/copier v0.3.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.31.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
gitlab.com/MikeTTh/env v0.0.0-20210102155928-2e9be3823cc7 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/sys v0.0.0-20211002104244-808efd93c36d // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
This diff is collapsed.
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
apiVersion: v2
name: matrix-aggregator
description: Helm chart for Matrix aggregator backend 2021
type: application
version: 0.0.0-master
appVersion: 0.0.0-master
{{ $app := "mtrx-aggregator" }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: matrix-aggregator
labels:
app: {{ $app }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels:
app: {{ $app }}
template:
metadata:
annotations:
prometheus.io/path: /metrics
prometheus.io/port: "8086"
prometheus.io/scrape: "true"
labels:
app: {{ $app }}
spec:
priorityClassName: {{ .Values.prioClass }}
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
ports:
- name: http
containerPort: 8080
protocol: TCP
- name: admin
containerPort: 8000
protocol: TCP
- name: mgmt
containerPort: 8086
protocol: TCP
envFrom:
- secretRef:
name: mtrx-redis
livenessProbe:
httpGet:
path: /live
port: mgmt
readinessProbe:
httpGet:
path: /ready
port: mgmt
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- if .Values.autoscaling.enabled }}
{{ $app := "mtrx-aggregator" }}
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: matrix-backend
labels:
app: {{ $app }}
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: matrix-aggregator
minReplicas: {{ .Values.autoscaling.minReplicas }}
maxReplicas: {{ .Values.autoscaling.maxReplicas }}
metrics:
{{- if .Values.autoscaling.targetCPUUtilizationPercentage }}
- type: Resource
resource:
name: cpu
targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }}
{{- end }}
{{- if .Values.autoscaling.targetMemoryUtilizationPercentage }}
- type: Resource
resource:
name: memory
targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }}
{{- end }}
{{- end }}
apiVersion: v1
kind: Secret
metadata:
name: mtrx-redis
labels:
app: mtrx-aggregator
stringData:
REDIS: {{.Values.redis}}
AUTHSCH: "{49bf9076771e2631f68a1bc7833d0ed4:9d17b96b48ddd29bfbd0720f836bae0b6223cf77a%3A4%3A%7Bi%3A0%3Bs%3A11%3A%22mikewashere%22%3Bi%3A1%3Bs%3A11%3A%22mikewashere%22%3Bi%3A2%3Bi%3A2592000%3Bi%3A3%3Ba%3A1%3A%7Bs%3A11%3A%22internal_id%22%3Bs%3A36%3A%2234033625-3020-e612-13b9-fde6a8791cb0%22%3B%7D%7D}"
{{ $app := "mtrx-aggregator" }}
apiVersion: v1
kind: Service
metadata:
name: matrix-aggregator
labels:
app: {{$app}}
spec:
ports:
- port: 8080
targetPort: http
protocol: TCP
name: http
- port: 8000
targetPort: admin
protocol: TCP
name: admin
selector:
app: {{ $app }}
replicaCount: 1
image:
repository: docker.io/miketth/qpa-puller
tag: latest
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: "4"
memory: "1Gi"
autoscaling:
enabled: true
minReplicas: 1
maxReplicas: 5
targetCPUUtilizationPercentage: 300
# targetMemoryUtilizationPercentage: 80
redis: "redis://default:password@redis-cluster-leader-headless:6379,redis-cluster-follower-headless:6379"
prioClass: apps
\ No newline at end of file
package main
import "fmt"
import (
"git.sch.bme.hu/kszk/schmatrix/games/qpa-team-puller/monitor"
"git.sch.bme.hu/kszk/schmatrix/games/qpa-team-puller/sync"
"log"
)
var _ = func() error {
log.SetFlags(log.LstdFlags | log.Lshortfile)
return nil
}()
func main() {
fmt.Println("hello world")
er := sync.Do()
if er != nil {
panic(er)
}
e := monitor.Run(c.MgmtPort)
if e != nil {
log.Fatal(e)
}
}
package monitor
import (
"context"
"git.sch.bme.hu/kszk/schmatrix/games/aggregator-backend/db"
"github.com/gin-gonic/gin"
"github.com/heptiolabs/healthcheck"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var server = gin.New()
var health = healthcheck.NewHandler()
func init() {
health.AddReadinessCheck("database", func() error {
return db.Ping(context.Background())
})
server.Use(gin.Recovery())
server.GET("/metrics", gin.WrapH(promhttp.Handler()))
server.GET("/live", gin.WrapH(health))
server.GET("/ready", gin.WrapH(health))
}
func Run(addr string) error {
return server.Run(addr)
}
package monitor
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var Teams = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "matrix",
Name: "teams",
Help: "number of teams added",
})
package qpa
import (
"encoding/json"
"fmt"
)
type Team struct {
Id int
Name string
Archived bool
Score int
}
func GetTeams() ([]Team, error) {
r, e := client.Get("https://schq.party/api/up/server/api/team/listActiveTeamsWithScores")
if e != nil {
fmt.Println(e)
}
dec := json.NewDecoder(r.Body)
var teams []Team
e = dec.Decode(&teams)
return teams, e
}
package qpa
import (
"encoding/json"
"errors"
"fmt"
"github.com/caarlos0/env/v6"
"html"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
)
func login() error {
/*
X-Oauth2-Override-Redirection-Origin: https://schq.party
*/
req, e := http.NewRequest(http.MethodGet, "https://schq.party/api/up/server/login/oauth2/authsch", nil)
if e != nil {
return e
}
req.Header.Set("X-Oauth2-Override-Redirection-Origin", "https://schq.party")
req.Header.Set("Return-Api-Oauth-Redirection-Response", "true")
r, e := client.Do(req)
if e != nil {
return e
}
if r.StatusCode == 299 { // WTF?
r, e = client.Get(r.Header.Get("location"))
if e != nil {
log.Fatal(e)
}
}
if r.Request.URL.Host == "auth.sch.bme.hu" && r.StatusCode == 200 { // approve prompt
b, e := ioutil.ReadAll(r.Body)
if e != nil {
return e
}
action := strings.Split(string(b), `action="`)[1]
action = strings.Split(action, `"`)[0]
action = html.UnescapeString(action)
r, e = client.PostForm(fmt.Sprintf("https://auth.sch.bme.hu%s", action), url.Values{
"authorized": []string{"yes"},
})
}
u := r.Request.URL
u.Path = "/api/up/server/login/oauth2/code/authsch"
u.Query().Add("oauth2provider", "authsch")
r, e = client.Get(u.String())
if e != nil {
return e
}
return nil
}
type AuthSch struct {
Key string
Value string
}
func (a *AuthSch) UnmarshalText(text []byte) error {
var m map[string]string
e := json.Unmarshal(text, &m)
if e != nil {
return e
}
for k, v := range m {
if k != "" && v != "" {
a.Key, a.Value = k, v
return nil
}
}
return errors.New("no data")
}
type config struct {
AuthSch AuthSch `env:"AUTHSCH" required:"true"`
}
var c config
var client *http.Client
func init() {
e := env.Parse(&c)
if e != nil {
log.Fatal(e)
}
j, e := cookiejar.New(nil)
if e != nil {
log.Fatal(e)
}
u, e := url.Parse("https://auth.sch.bme.hu/")
if e != nil {
log.Fatal(e)
}
j.SetCookies(u, []*http.Cookie{
{
Name: c.AuthSch.Key,
Value: c.AuthSch.Value,
Path: "/",
Domain: "auth.sch.bme.hu",
Expires: time.Now().Add(time.Hour * 9999),
},
})
client = &http.Client{
Jar: j,
}
e = login()
if e != nil {
log.Fatal(e)
}
}
package sync
import (
"fmt"
"git.sch.bme.hu/kszk/schmatrix/games/qpa-team-puller/qpa"
)
func Do() error {
teams, e := qpa.GetTeams()
if e != nil {
return e
}
fmt.Println(teams)
return nil
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment