Compare commits
9
Commits
apps-monitor
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ceba2d26e6 | ||
|
|
dd8b610ea5 | ||
|
|
13ea6a0fe3 | ||
|
|
303016681a | ||
|
|
2fe7fb386b | ||
|
|
1d11d58b86 | ||
|
|
7c3eb7debd | ||
|
|
cf977e65da | ||
|
|
035d23d1e3 |
No files matched your search
File renamed without changes.
@@ -5,8 +5,8 @@ on:
|
||||
branches: ['main']
|
||||
workflow_dispatch:
|
||||
|
||||
# Cottage: deploys only once the repository variable CLOUDFLARE_DEPLOY is "true" (set at launch,
|
||||
# with the CLOUDFLARE_API_TOKEN secret). Until then a push to main does nothing.
|
||||
# Cottage (Gitea Actions): deploys only once the repository variable CLOUDFLARE_DEPLOY is "true" (set
|
||||
# at launch, with the CLOUDFLARE_API_TOKEN secret). Until then a push to main does nothing.
|
||||
env:
|
||||
WRANGLER_SEND_METRICS: 'false'
|
||||
|
||||
@@ -63,6 +63,9 @@ jobs:
|
||||
run: |
|
||||
npx @cloudflare/next-on-pages
|
||||
|
||||
- name: Install Python dependencies (the Gitea runner image lacks them)
|
||||
run: pip install --quiet requests
|
||||
|
||||
- name: Create D1 database and tables
|
||||
run: |
|
||||
python3 deploy/init_d1.py # This sets D1_ID in GITHUB_ENV
|
||||
@@ -104,6 +107,8 @@ jobs:
|
||||
env:
|
||||
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
||||
TF_VAR_CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }}
|
||||
TF_VAR_APPS_US_EAST_TARGET: ${{ secrets.APPS_US_EAST_TARGET }}
|
||||
TF_VAR_MATTERMOST_WEBHOOK: ${{ secrets.MATTERMOST_WEBHOOK }}
|
||||
D1_ID: ${{ env.D1_ID }}
|
||||
|
||||
# Currently Terraform Cloudflare provider doesn't support direct upload, use wrangler to upload instead.
|
||||
@@ -1,9 +1,8 @@
|
||||
name: Upstream Sync
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
actions: write
|
||||
|
||||
# Pulls the latest UptimeFlare code from github.com/lyc8503/UptimeFlare while keeping our
|
||||
# uptime.config.ts. The push uses SYNC_TOKEN (an access token with write access to this
|
||||
# repository) so that it starts the Deploy workflow like any other push to main.
|
||||
on:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
@@ -12,9 +11,6 @@ on:
|
||||
type: boolean
|
||||
required: true
|
||||
default: false
|
||||
override-token:
|
||||
description: '[Optional] Paste a PAT here. If left blank, PAT_TOKEN secret will be used (if set). Used to solve `refusing to allow a GitHub App to create or update workflow... `, more info at https://github.com/lyc8503/UptimeFlare/wiki/Synchronize-updates-from-upstream#upgrade-methods'
|
||||
required: false
|
||||
|
||||
jobs:
|
||||
sync_latest_from_upstream:
|
||||
@@ -23,9 +19,9 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout target repo
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
token: ${{ inputs.override-token || secrets.PAT_TOKEN || secrets.GITHUB_TOKEN }}
|
||||
token: ${{ secrets.SYNC_TOKEN }}
|
||||
|
||||
- name: Current config
|
||||
run: |
|
||||
@@ -33,10 +29,9 @@ jobs:
|
||||
cp uptime.config.ts /tmp/origin.config.ts
|
||||
|
||||
- name: Sync upstream changes
|
||||
id: sync
|
||||
run: |
|
||||
git config user.name "github-actions[bot]"
|
||||
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
|
||||
git config user.name "Cottage status sync"
|
||||
git config user.email "[email protected]"
|
||||
|
||||
# Fetch latest code
|
||||
git clone https://github.com/lyc8503/UptimeFlare /tmp/latest
|
||||
@@ -49,9 +44,3 @@ jobs:
|
||||
git add .
|
||||
git commit -m "Sync latest code from upstream"
|
||||
git push
|
||||
|
||||
- name: Trigger deployment
|
||||
if: ${{ inputs.override-token == '' }}
|
||||
uses: benc-uk/workflow-dispatch@v1
|
||||
with:
|
||||
workflow: deploy.yml
|
||||
@@ -32,7 +32,7 @@ Without a D1 database, `npm run dev` shows sample data (`dev/sample.ts`). Add `?
|
||||
|
||||
Live at status.cottage.host. A push to `main` deploys it through the "Deploy to Cloudflare" workflow, which runs only while the repository variable `CLOUDFLARE_DEPLOY` is `true`. It uses the Actions secrets `CLOUDFLARE_API_TOKEN` (Workers Scripts, D1 and Cloudflare Pages edit on the Cottage account) and `CLOUDFLARE_ACCOUNT_ID`.
|
||||
|
||||
The "Apps in US East" check is commented out in `uptime.config.ts` until a canary app runs on the node. Once one does, put its address in the `apps-us-east` monitor and uncomment it and its group.
|
||||
The "Apps in US East" check loads the health endpoint of a status canary: a small ntfy app Cottage runs on its own (internal, unbilled) account on the Virginia servers. A new region gets its own canary and monitor.
|
||||
|
||||
Cottage's footer status ("All systems normal") can read `/api/data`, which returns the current state as JSON.
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ export default function Footer() {
|
||||
<span>
|
||||
Built on{' '}
|
||||
<a href="https://github.com/lyc8503/UptimeFlare">UptimeFlare</a>, open source (
|
||||
<a href="https://github.com/cottage-host/status">our copy</a>)
|
||||
<a href="https://git.cottage.host/cottage/status">our copy</a>)
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -16,6 +16,19 @@ variable "CLOUDFLARE_ACCOUNT_ID" {
|
||||
type = string
|
||||
}
|
||||
|
||||
# Cottage: private monitor addresses (see `secret:` targets in uptime.config.ts).
|
||||
variable "APPS_US_EAST_TARGET" {
|
||||
# read from $TF_VAR_APPS_US_EAST_TARGET (the GitHub Actions secret of the same name)
|
||||
type = string
|
||||
sensitive = true
|
||||
}
|
||||
|
||||
variable "MATTERMOST_WEBHOOK" {
|
||||
# read from $TF_VAR_MATTERMOST_WEBHOOK (the GitHub Actions secret of the same name)
|
||||
type = string
|
||||
sensitive = true
|
||||
}
|
||||
|
||||
variable "enable_do_migration" {
|
||||
type = bool
|
||||
default = false
|
||||
@@ -59,6 +72,14 @@ resource "cloudflare_workers_script" "uptimeflare_worker" {
|
||||
name = "UPTIMEFLARE_D1"
|
||||
type = "d1"
|
||||
id = cloudflare_d1_database.uptimeflare_d1.id
|
||||
}, {
|
||||
name = "APPS_US_EAST_TARGET"
|
||||
type = "secret_text"
|
||||
text = var.APPS_US_EAST_TARGET
|
||||
}, {
|
||||
name = "MATTERMOST_WEBHOOK"
|
||||
type = "secret_text"
|
||||
text = var.MATTERMOST_WEBHOOK
|
||||
}]
|
||||
}
|
||||
|
||||
|
||||
+28
-16
@@ -14,7 +14,7 @@ const pageConfig: PageConfig = {
|
||||
// Sections, in order. Every monitor below should appear in exactly one.
|
||||
group: {
|
||||
Cottage: ['website', 'dashboard'],
|
||||
// 'Apps by region': ['apps-us-east'], // add back with the monitor below
|
||||
'Apps by region': ['apps-us-east'],
|
||||
},
|
||||
}
|
||||
|
||||
@@ -42,22 +42,34 @@ const workerConfig: WorkerConfig = {
|
||||
timeout: 10000,
|
||||
headers: { 'User-Agent': 'cottage-status (+https://status.cottage.host)' },
|
||||
},
|
||||
// Not checked yet: no canary app runs on the node. Deploy one, then uncomment this
|
||||
// and the 'Apps by region' group above.
|
||||
// {
|
||||
// // A small app Cottage runs for itself on the region's servers, so this checks the
|
||||
// // whole path a customer's app takes: Cloudflare, the node's proxy and a running app.
|
||||
// // TODO at launch: deploy the canary app and put its address here.
|
||||
// id: 'apps-us-east',
|
||||
// name: 'Apps in US East',
|
||||
// tooltip: 'Customer apps hosted in Virginia',
|
||||
// method: 'GET',
|
||||
// target: 'https://status-canary-vin1.cottagehost.com/',
|
||||
// timeout: 10000,
|
||||
// headers: { 'User-Agent': 'cottage-status (+https://status.cottage.host)' },
|
||||
// },
|
||||
{
|
||||
// A real app running on the region's servers (on Cottage's own internal account), so this
|
||||
// checks the whole path a customer's app takes: Cloudflare, the node's proxy, Harbor and a
|
||||
// running container. Its address is private: the worker reads it from the Worker secret
|
||||
// APPS_US_EAST_TARGET (GitHub Actions secret of the same name, bound in deploy.tf).
|
||||
id: 'apps-us-east',
|
||||
name: 'Apps in US East',
|
||||
tooltip: 'Customer apps hosted in Virginia',
|
||||
method: 'GET',
|
||||
target: 'secret:APPS_US_EAST_TARGET',
|
||||
expectedCodes: [200],
|
||||
timeout: 10000,
|
||||
headers: { 'User-Agent': 'cottage-status (+https://status.cottage.host)' },
|
||||
},
|
||||
],
|
||||
// Alerts come from Grafana Cloud (hearth infra/monitoring). A webhook here could page too.
|
||||
// Down and back-up messages go to the founder's Mattermost (channel "alerts"). The webhook address
|
||||
// is private: the worker reads it from the Worker secret MATTERMOST_WEBHOOK (GitHub Actions secret of
|
||||
// the same name, bound in deploy.tf). Grafana Cloud (hearth infra/monitoring) alerts there too.
|
||||
notification: {
|
||||
webhook: {
|
||||
url: 'secret:MATTERMOST_WEBHOOK',
|
||||
payloadType: 'json',
|
||||
payload: { text: '$MSG', username: 'Status' },
|
||||
},
|
||||
timeZone: 'Etc/UTC',
|
||||
// Wait a few minutes before saying something is down, so a single failed check doesn't page.
|
||||
gracePeriod: 3,
|
||||
},
|
||||
}
|
||||
|
||||
// Notices shown on the page: incident updates and planned maintenance. Newest last.
|
||||
|
||||
+35
-2
@@ -2,7 +2,7 @@ import { DurableObject } from 'cloudflare:workers'
|
||||
import { MonitorTarget } from '../../types/config'
|
||||
import { workerConfig } from '../../uptime.config'
|
||||
import { doMonitor, getStatus } from './monitor'
|
||||
import { formatAndNotify, getWorkerLocation } from './util'
|
||||
import { formatAndNotify, getWorkerLocation, setWebhookSecrets } from './util'
|
||||
import { CompactedMonitorStateWrapper, getFromStore, setToStore } from './store'
|
||||
import pLimit from 'p-limit'
|
||||
|
||||
@@ -11,8 +11,41 @@ export interface Env {
|
||||
UPTIMEFLARE_D1: D1Database
|
||||
}
|
||||
|
||||
// Cottage: a monitor's target may be `secret:NAME`, read at check time from the Worker secret
|
||||
// NAME (bound in deploy.tf from a GitHub Actions secret), so a private address never appears in
|
||||
// this public repo. Error text is scrubbed of the address before it's stored or shown.
|
||||
type CheckStatus = { ping: number; up: boolean; err: string }
|
||||
|
||||
function secretTarget(monitor: MonitorTarget, env: Env): { monitor: MonitorTarget; secret?: string } | { missing: string } {
|
||||
if (!monitor.target.startsWith('secret:')) return { monitor }
|
||||
const name = monitor.target.slice('secret:'.length)
|
||||
const value = (env as unknown as Record<string, unknown>)[name]
|
||||
if (typeof value !== 'string' || value === '') return { missing: name }
|
||||
return { monitor: { ...monitor, target: value }, secret: value }
|
||||
}
|
||||
|
||||
function scrub(status: CheckStatus, secret: string): CheckStatus {
|
||||
let err = status.err
|
||||
const parts = [secret]
|
||||
try {
|
||||
parts.push(new URL(secret).host, new URL(secret).hostname)
|
||||
} catch {}
|
||||
for (const p of parts) if (p) err = err.split(p).join('the app')
|
||||
return { ...status, err }
|
||||
}
|
||||
|
||||
async function checkMonitor(monitor: MonitorTarget, location: string, env: Env) {
|
||||
const r = secretTarget(monitor, env)
|
||||
if ('missing' in r) {
|
||||
return { id: monitor.id, location, status: { ping: 0, up: false, err: `Check address not configured (${r.missing})` } }
|
||||
}
|
||||
const result = await doMonitor(r.monitor, location, env)
|
||||
return r.secret ? { ...result, status: scrub(result.status, r.secret) } : result
|
||||
}
|
||||
|
||||
const Worker = {
|
||||
async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext): Promise<void> {
|
||||
setWebhookSecrets(env)
|
||||
const workerLocation = (await getWorkerLocation()) || 'ERROR'
|
||||
console.log(`Running scheduled event on ${workerLocation}...`)
|
||||
|
||||
@@ -31,7 +64,7 @@ const Worker = {
|
||||
let checkResult: Record<string, CheckResult> = {};
|
||||
const limit = pLimit(5);
|
||||
for (const monitor of workerConfig.monitors) {
|
||||
checkQueue.push(limit(() => doMonitor(monitor, workerLocation, env)))
|
||||
checkQueue.push(limit(() => checkMonitor(monitor, workerLocation, env)))
|
||||
}
|
||||
for (const result of await Promise.all(checkQueue)) {
|
||||
checkResult[result.id] = result
|
||||
|
||||
+27
-3
@@ -79,6 +79,24 @@ function templateWebhookPlayload(payload: any, message: string) {
|
||||
}
|
||||
}
|
||||
|
||||
// Cottage: a webhook url may be `secret:NAME`, read at send time from the Worker secret NAME (bound in
|
||||
// deploy.tf from a GitHub Actions secret), so the address never appears in this public repo or the logs.
|
||||
let webhookSecrets: Record<string, unknown> = {}
|
||||
|
||||
function setWebhookSecrets(env: object) {
|
||||
webhookSecrets = env as Record<string, unknown>
|
||||
}
|
||||
|
||||
function resolveWebhookUrl(url: string): string | undefined {
|
||||
if (!url.startsWith('secret:')) return url
|
||||
const value = webhookSecrets[url.slice('secret:'.length)]
|
||||
return typeof value === 'string' && value !== '' ? value : undefined
|
||||
}
|
||||
|
||||
function hideUrl(text: string, url: string, real: string): string {
|
||||
return real === url ? text : text.split(real).join(url)
|
||||
}
|
||||
|
||||
async function webhookNotify(webhook: WebhookConfig, message: string) {
|
||||
if (Array.isArray(webhook)) {
|
||||
for (const w of webhook) {
|
||||
@@ -90,8 +108,13 @@ async function webhookNotify(webhook: WebhookConfig, message: string) {
|
||||
console.log(
|
||||
'Sending webhook notification: ' + JSON.stringify(message) + ' to webhook ' + webhook.url
|
||||
)
|
||||
const realUrl = resolveWebhookUrl(webhook.url)
|
||||
if (realUrl === undefined) {
|
||||
console.log(`Webhook address not configured (${webhook.url}), skipping notification`)
|
||||
return
|
||||
}
|
||||
try {
|
||||
let url = webhook.url
|
||||
let url = realUrl
|
||||
let method = webhook.method
|
||||
let headers = new Headers(webhook.headers as any)
|
||||
let payloadTemplated: { [key: string]: string | number } = JSON.parse(
|
||||
@@ -128,7 +151,7 @@ async function webhookNotify(webhook: WebhookConfig, message: string) {
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Webhook finalized parameters: ${method} ${url}, headers ${JSON.stringify(
|
||||
`Webhook finalized parameters: ${method} ${hideUrl(url, webhook.url, realUrl)}, headers ${JSON.stringify(
|
||||
Object.fromEntries(headers.entries())
|
||||
)}, body ${JSON.stringify(body)}`
|
||||
)
|
||||
@@ -142,7 +165,7 @@ async function webhookNotify(webhook: WebhookConfig, message: string) {
|
||||
console.log('Webhook notification sent successfully, code: ' + resp.status)
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('Error calling webhook server: ' + e)
|
||||
console.log('Error calling webhook server: ' + hideUrl(String(e), webhook.url, realUrl))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,4 +221,5 @@ export {
|
||||
webhookNotify,
|
||||
formatStatusChangeNotification,
|
||||
formatAndNotify,
|
||||
setWebhookSecrets,
|
||||
}
|
||||
Reference in new issue
Block a user