Skip to content

Instantly share code, notes, and snippets.

@Jamesits
Jamesits / postgresql.tf
Created April 18, 2024 08:15
Auto set max_connections on Azure PostgreSQL Flexible Server with Terraform
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
}
}
}
variable "sku_name" {
type = string
@Jamesits
Jamesits / pghero.sql
Last active April 11, 2024 08:24
The missing database migration for PgHero, with permission isolation setup (separate schema for PgHero, read only access for the application schema)
-- database
CREATE DATABASE "app" WITH OWNER = "azure_pg_admin";
\c app
-- app user (just as an example)
CREATE ROLE "appuser" LOGIN ENCRYPTED PASSWORD '114514' ADMIN azure_pg_admin;
GRANT ALL PRIVILEGES ON DATABASE "app" TO "appuser";
GRANT ALL ON SCHEMA public TO "appuser";
-- PgHero user
# Memory usage
# https://blog.cloudflare.com/the-story-of-one-latency-spike/
# https://cloud.google.com/architecture/tcp-optimization-for-network-performance-in-gcp-and-hybrid/
# https://zhensheng.im/2021/01/31/linux-wmem-and-rmem-adjustments.meow
# https://github.com/redhat-performance/tuned/blob/master/profiles/network-throughput/tuned.conf
# ReceiveBuffer: X - (X / (2 ^ tcp_adv_win_scale)) = RTT * Bandwidth / 8
# SendBuffer: RTT * Bandwidth / 8 * 0.7
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_max_syn_backlog = 8192
net.core.rmem_max = 67108864
@Jamesits
Jamesits / srx_ip_monitoring.txt
Last active June 22, 2023 02:51
Example: monitor the availability of the same internet IP address with different upstream ISPs, and select the default route for access users based on ip monitoring result. (Junos OS, Juniper SRX)
groups {
ri_vr_defaults {
routing-instances {
<*> {
instance-type virtual-router;
routing-options {
static {
defaults {
install;
resolve;
@Jamesits
Jamesits / Set-StrictMode.ps1
Last active June 16, 2023 16:33
Pedantic mode in PowerShell
# set -e (errexit)
$ErrorActionPreference = 'Stop'
$PSNativeCommandUseErrorActionPreference = $True
$ExecutionContext.InvokeCommand.PostCommandLookupAction = {
param([string]$command, [System.Management.Automation.CommandLookupEventArgs]$eventArgs);
if ($eventArgs.CommandOrigin -ne [System.Management.Automation.CommandOrigin]::Runspace) {return}
if ($eventArgs.Command.CommandType -eq [System.Management.Automation.CommandTypes]::Application) {
$cmd = $eventArgs.Command.Path
$eventArgs.CommandScriptBlock = {
Param([parameter(Position=0,ValueFromRemainingArguments=$true)][string]$arg);
@Jamesits
Jamesits / file2patch.sh
Last active March 11, 2023 12:58
Convert a text file to a patch file
#!/usr/bin/env bash
# convert a text file to a patch file
set -Eeuo pipefail
# usage: file2patch "source/file" "relative/destination/path" "patch/file"
file2patch() {
local SRCFILE="$1"
local PATCHPATH="$2"
local OUTFILE="$3"
local FILEMODE="$(stat -c '%a' "${SRCFILE}")"
@Jamesits
Jamesits / git-extract-submodules.sh
Last active April 10, 2023 18:45
Revert `git submodule absorbgitdirs`
#!/usr/bin/env bash
# Revert `git submodule absorbgitdirs`.
# Source: https://stackoverflow.com/a/51763718
# Notes:
# - Nested submodules must be moved first, so if you are specifying submodules manually, make sure the dependencies are correct
# - Assuming latest version of Git and GNU coreutils has been installed in the $PATH
gitextractsubmodules() {
set -e
pushd -- "$(git rev-parse --show-superproject-working-tree)" >/dev/null 2>&1
@Jamesits
Jamesits / SF2910-8GT2MG2XS-P.log
Last active October 15, 2022 06:40
Ruijie SF2910-8GT2MG2XS-P boot log
Ruijie#reload
Reload system?(Y/N)y
Press RETURN to get started
*Jan 1 00:02:28: %DP-3-RESET_DEV: Reset device 1 due to reload command.
*Jan SysRq : Resettin
Boot SPL 1.2.41-f94ff99 (Feb 04 2021 - 19:27:36)
NAND: Detected flash
@Jamesits
Jamesits / debian-bullseye-cloud-image-build.sh
Last active February 3, 2023 01:57
Build a new Debian bullseye image which works around the problem that when cloud-init is fed a static IPv6 config, ifup/ifdown will fail
#!/bin/bash
set -Eeuo pipefail
# Build a new Debian bullseye image which works around the problem that
# when cloud-init is fed a static IPv6 config, ifup/ifdown will fail with:
# "/etc/network/if-pre-up.d/cloud_inet6: 12: IF_TRY_DHCP: parameter not set"
#
# Requirements:
# - wget
# - qemu-utils
@Jamesits
Jamesits / crs309-initial.rsc
Created September 22, 2021 14:47
MikroTik initialization script
#| Welcome to RouterOS!
#| 1) Set a strong router password in the System > Users menu
#| 2) Upgrade the software in the System > Packages menu
#| 3) Enable firewall on untrusted networks
#| -----------------------------------------------------------------------------
#| Switch mode:
#| * all interfaces switched;
#| LAN Configuration:
:global defconfMode;