Skip to content

Commit d7774b9

Browse files
agbishopWitness Patrolclaude
authored
feat: parity batch 11 (Cloud SQL backups, flags and PITR across failover; Compute schedule policies; Azure flexible server parameters, restores and replicas with data) (#2545)
* feat(gcp): Cloud SQL backups resource, final backups, demote, PITR across failover and flag validation; Compute schedule policies, group errors and template images Cloud SQL serves projects.backups with expiry and verified restores, takes a final backup on delete, rolls back a failed restore, records demotion as a read-only replica, keeps the PostgreSQL WAL archive with the instance so point-in-time restore survives a failover, and validates and applies database flags without resetting system settings. Compute runs instance and snapshot schedule policies, reports managed instance group creation errors with retries, applies instanceFlexibilityPolicy and creates custom images for template source disks. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NVwMjLknMM32Xje6Qik6wY * feat(pgengine,mysqlengine): parameter changes and dump/restore streams Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NVwMjLknMM32Xje6Qik6wY * feat(azurearm): flexible server parameters reach the engine; point-in-time restore and replicas carry data; restart with failover Configuration changes are applied to the running PostgreSQL or MySQL engine, with static parameters pending until restart. Backups are verified dumps kept under the data directory; PointInTimeRestore and GeoRestore load the newest backup at or before the requested time and replicas are seeded from the source and read-only until promoted. The restart action honours restartWithFailover. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NVwMjLknMM32Xje6Qik6wY * fix(gcp): accept unknown Cloud SQL flags without applying them, protect emulator engine settings, keep cron schedules terminating across DST Flags outside the catalogue were rejected, breaking documented flags such as cloudsql.iam_authentication; they are now stored and only catalogue flags reach the engine, which also stops a flag from overriding archive settings or resetting primary_conninfo on a replica. Backup dumps are encoded outside the backend lock, the WAL archive command copies atomically, final backups re-check the project cap. The schedule evaluator no longer loops forever on a spring-forward day, fires once on fall-back, and failing group members are bounded. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NVwMjLknMM32Xje6Qik6wY * fix(pgengine,mysqlengine): refuse protected settings, bind only canonical numbers, read-only on MySQL 5.7 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NVwMjLknMM32Xje6Qik6wY * fix(azurearm): validate flexible server dump references, keep newer values on a refused parameter push, namespace dump files per instance A restored snapshot could point a backup at any file in the dump directory; names must now match the generated form. A refused engine push reverted unconditionally and erased a newer concurrent change. Dump files carry the provider instance so a sweep cannot delete another emulator's files. NaN and non-decimal numeric parameters are rejected or normalised, and an unknown restartWithFailover value is a 400. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NVwMjLknMM32Xje6Qik6wY * docs(azure): regenerate service READMEs Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NVwMjLknMM32Xje6Qik6wY * docs: regenerate service READMEs and operation badge Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NVwMjLknMM32Xje6Qik6wY * test(gcpcompute): clone the policy in named() so parallel cases do not share it Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NVwMjLknMM32Xje6Qik6wY --------- Co-authored-by: Witness Patrol <blackbird7181@gmail.com> Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 6639c2c commit d7774b9

95 files changed

Lines changed: 10457 additions & 327 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.badges/operations.svg‎

Lines changed: 3 additions & 3 deletions
Loading

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -968,11 +968,11 @@ Every service links to its own page with a coverage breakdown — audited operat
968968
| [Cloud Logging](services/gcplogging/README.md) | B | 43 | 3 gaps |
969969
| [Cloud Monitoring](services/gcpmonitoring/README.md) | B | 54 | 5 gaps |
970970
| [Cloud Run](services/gcprun/README.md) | B | 34 | 3 gaps |
971-
| [Cloud SQL](services/gcpsql/README.md) | B | 45 | 5 gaps |
971+
| [Cloud SQL](services/gcpsql/README.md) | B | 52 | 5 gaps |
972972
| [Cloud Scheduler](services/gcpscheduler/README.md) | B | 10 | clean |
973973
| [Cloud Spanner](services/gcpspanner/README.md) | B | 31 | 2 gaps |
974974
| [Cloud Tasks](services/gcpcloudtasks/README.md) | B | 20 | clean |
975-
| [Compute Engine](services/gcpcompute/README.md) | B | — | 8 families; 7 gaps |
975+
| [Compute Engine](services/gcpcompute/README.md) | B | — | 8 families; 6 gaps |
976976
| [Eventarc](services/gcpeventarc/README.md) | B | 47 | 3 gaps |
977977
| [Google Cloud Batch](services/gcpbatch/README.md) | B | 14 | 4 gaps |
978978
| [Google Cloud Firestore](services/gcpfirestore/README.md) | B | 17 | 3 gaps |
@@ -1019,7 +1019,7 @@ Every service links to its own page with a coverage breakdown — audited operat
10191019
| [Amp](services/amp/README.md) | B | 44 | 3 gaps; 2 structural gaps |
10201020
| [AppStream 2.0](services/appstream/README.md) | A | 44 | 5 structural gaps |
10211021
| [Appintegrations](services/appintegrations/README.md) | B | 23 | 1 structural gap |
1022-
| [Azurearm](services/azurearm/README.md) | B | 21 | 43 gaps; 3 deferred |
1022+
| [Azurearm](services/azurearm/README.md) | B | 21 | 42 gaps; 3 deferred |
10231023
| [Azurestoragevhost](services/azurestoragevhost/README.md) | B | 2 | 2 gaps; 1 deferred |
10241024
| [Bcmdataexports](services/bcmdataexports/README.md) | B | 12 | clean |
10251025
| [Bedrockagentcore](services/bedrockagentcore/README.md) | B | 32 | 7 gaps; 2 structural gaps |

‎internal/app/cli.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,12 @@ func (c *CLI) GetAzureServiceBusSettings() azureservicebusbackend.Settings {
10901090
// GetAzureARMSettings returns Azure Resource Manager settings
10911091
// (azurearm.ConfigProvider).
10921092
func (c *CLI) GetAzureARMSettings() azurearmbackend.Settings {
1093-
return c.AzureARM
1093+
s := c.AzureARM
1094+
if s.FlexDumpDir == "" && c.Persist {
1095+
s.FlexDumpDir = filepath.Join(c.resolvedDataDir(), "azure-flex-dumps")
1096+
}
1097+
1098+
return s
10941099
}
10951100

10961101
// GetAzureStorageVHostSettings returns the shared Azure Storage

‎pkgs/mysqlengine/binlog.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ func (s *Server) writeBase(ctx context.Context, dbs []string) error {
9090

9191
cmd := append([]string{
9292
mysqldumpBin, "-u" + RootUser, argSingleTx, argRoutines,
93-
"--events", "--triggers", "--skip-dump-date", argAddDrop, "--source-data=2", "--flush-logs",
94-
"--set-gtid-purged=OFF", "-r", baseFile, argDatabases,
93+
argEvents, argTriggers, "--skip-dump-date", argAddDrop, "--source-data=2", "--flush-logs",
94+
argGtidOff, "-r", baseFile, argDatabases,
9595
}, dbs...)
9696

9797
_, err := s.RunAuth(ctx, cmd)

‎pkgs/mysqlengine/mysqlengine.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ const (
4949
argAddDrop = "--add-drop-database"
5050
argDatabases = "--databases"
5151
argSingleTx = "--single-transaction"
52+
argEvents = "--events"
53+
argTriggers = "--triggers"
54+
argGtidOff = "--set-gtid-purged=OFF"
5255
)
5356

5457
var (
@@ -688,7 +691,7 @@ func (s *Server) DumpDatabases(ctx context.Context, dbs []string) ([]byte, error
688691

689692
cmd := append([]string{
690693
mysqldumpBin, "-u" + RootUser, argSingleTx, argRoutines,
691-
argAddDrop, "--set-gtid-purged=OFF", "-r", dumpMount + "/dump.sql", argDatabases,
694+
argAddDrop, argGtidOff, "-r", dumpMount + "/dump.sql", argDatabases,
692695
}, dbs...)
693696

694697
if err := s.run(ctx, "mysqldump", cmd, s.pwdEnv()); err != nil {

‎pkgs/mysqlengine/replication.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (s *Server) ReplicaSeed(ctx context.Context, dbs []string) ([]byte, error)
168168

169169
cmd := append([]string{
170170
mysqldumpBin, "-u" + RootUser, argSingleTx, "--skip-dump-date",
171-
argRoutines, "--events", "--triggers", "--set-gtid-purged=ON", argAddDrop,
171+
argRoutines, argEvents, argTriggers, "--set-gtid-purged=ON", argAddDrop,
172172
"-r", dumpMount + "/seed.sql", argDatabases,
173173
}, dbs...)
174174

‎pkgs/mysqlengine/stream.go‎

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
package mysqlengine
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"io"
8+
"math"
9+
"regexp"
10+
"slices"
11+
"strconv"
12+
"strings"
13+
14+
"github.com/blackbirdworks/gopherstack/pkgs/container"
15+
)
16+
17+
const headLimit = 512
18+
19+
var (
20+
// ErrInvalidVariable reports a server variable name or value the engine refuses to put into SQL.
21+
ErrInvalidVariable = errors.New("mysqlengine: invalid server variable")
22+
// ErrUnsupported reports an operation the server's version cannot perform.
23+
ErrUnsupported = errors.New("mysqlengine: unsupported by this server version")
24+
// ErrExecUnsupported is returned when the container runtime cannot relay standard input to a command.
25+
ErrExecUnsupported = errors.New("mysqlengine: the container runtime cannot run commands with standard input")
26+
// ErrInvalidPassword is returned when the root password cannot be handed to a client tool on standard input.
27+
ErrInvalidPassword = errors.New("mysqlengine: the root password cannot be passed to a client tool")
28+
29+
varNameRE = regexp.MustCompile(`^[a-z][a-z0-9_]{0,63}$`)
30+
canonicalNumberRE = regexp.MustCompile(`^-?(0|[1-9][0-9]*)(\.[0-9]+)?$`)
31+
)
32+
33+
// protectedVariable reports a variable that points the server at files, programs or plugins, changes who can
34+
// reach it, or that the emulator itself relies on; SetVariable refuses it.
35+
func protectedVariable(name string) bool {
36+
return strings.HasPrefix(name, "ssl_") || slices.Contains([]string{
37+
"secure_file_priv",
38+
"plugin_dir",
39+
"init_file",
40+
"general_log_file",
41+
"slow_query_log_file",
42+
"local_infile",
43+
"port",
44+
"bind_address",
45+
"admin_address",
46+
"log_bin",
47+
"log_bin_basename",
48+
"log_bin_index",
49+
"log_error",
50+
"datadir",
51+
"basedir",
52+
"socket",
53+
"tmpdir",
54+
"skip_grant_tables",
55+
"skip_networking",
56+
"read_only",
57+
"super_read_only",
58+
"server_id",
59+
"gtid_mode",
60+
"enforce_gtid_consistency",
61+
"sql_log_bin",
62+
"lower_case_table_names",
63+
"early_plugin_load",
64+
"plugin_load",
65+
"plugin_load_add",
66+
"lc_messages_dir",
67+
"innodb_data_home_dir",
68+
"innodb_log_group_home_dir",
69+
"innodb_undo_directory",
70+
"innodb_directories",
71+
"innodb_force_recovery",
72+
"named_pipe",
73+
"shared_memory",
74+
"secure_log_path",
75+
"mysqlx_port",
76+
"mysqlx_bind_address",
77+
"mysqlx_socket",
78+
}, name)
79+
}
80+
81+
// CheckVariable reports whether SetVariable would accept the name, without touching a server.
82+
func CheckVariable(name string) error {
83+
if !varNameRE.MatchString(name) {
84+
return fmt.Errorf("%w: name", ErrInvalidVariable)
85+
}
86+
87+
if protectedVariable(name) {
88+
return fmt.Errorf("%w: %s is managed by the emulator", ErrInvalidVariable, name)
89+
}
90+
91+
return nil
92+
}
93+
94+
// SetVariable persists a global system variable (SET PERSIST). static variables use SET PERSIST_ONLY and take
95+
// effect after the next restart; reset restores the default. name is matched against an identifier pattern and
96+
// the value is bound as a parameter. MySQL 5.7 has no SET PERSIST, so it only supports dynamic variables, set for
97+
// the life of the server.
98+
func (s *Server) SetVariable(ctx context.Context, name, value string, reset, static bool) error {
99+
if err := CheckVariable(name); err != nil {
100+
return err
101+
}
102+
103+
verb := "SET PERSIST "
104+
if static {
105+
verb = "SET PERSIST_ONLY "
106+
}
107+
108+
if s.noPersist() {
109+
if static {
110+
return ErrUnsupported
111+
}
112+
113+
verb = "SET GLOBAL "
114+
}
115+
116+
if reset {
117+
return s.exec(ctx, verb+name+" = DEFAULT")
118+
}
119+
120+
return s.exec(ctx, verb+name+" = ?", variableArg(value))
121+
}
122+
123+
// noPersist reports a MySQL 5.7 server, which has no SET PERSIST: its changes last until the server restarts.
124+
func (s *Server) noPersist() bool { return strings.HasSuffix(s.spec.Image, ":5.7") }
125+
126+
// variableArg binds canonical decimal numbers as numbers (SET rejects a quoted string for a numeric variable) and
127+
// everything else, including "007", "1e3" and "+5", as the string it is.
128+
func variableArg(v string) any {
129+
if !canonicalNumberRE.MatchString(v) {
130+
return v
131+
}
132+
133+
if n, err := strconv.ParseInt(v, 10, 64); err == nil {
134+
return n
135+
}
136+
137+
if f, err := strconv.ParseFloat(v, 64); err == nil && !math.IsNaN(f) && !math.IsInf(f, 0) {
138+
return f
139+
}
140+
141+
return v
142+
}
143+
144+
// SetReadOnly turns super_read_only (and read_only) on or off, persisted across restarts except on MySQL 5.7.
145+
func (s *Server) SetReadOnly(ctx context.Context, on bool) error {
146+
for _, stmt := range readOnlyStmts(on, s.noPersist()) {
147+
if err := s.exec(ctx, stmt); err != nil {
148+
return err
149+
}
150+
}
151+
152+
return nil
153+
}
154+
155+
func readOnlyStmts(on, noPersist bool) []string {
156+
v, verb := "OFF", "SET PERSIST "
157+
if on {
158+
v = "ON"
159+
}
160+
161+
if noPersist {
162+
verb = "SET GLOBAL "
163+
}
164+
165+
return []string{verb + "read_only = " + v, verb + "super_read_only = " + v}
166+
}
167+
168+
type headBuf struct{ buf []byte }
169+
170+
func (h *headBuf) Write(p []byte) (int, error) {
171+
if room := headLimit - len(h.buf); room > 0 {
172+
h.buf = append(h.buf, p[:min(room, len(p))]...)
173+
}
174+
175+
return len(p), nil
176+
}
177+
178+
// clientScript runs a mysql client tool with the root password read from the first line of standard input, so it
179+
// is never in argv or the exec environment record.
180+
const clientScript = `IFS= read -r pw; MYSQL_PWD="$pw" exec "$@"`
181+
182+
func (s *Server) stdinRun(ctx context.Context, cmd []string, in io.Reader, out io.Writer) (int, error) {
183+
x, ok := s.e.cfg.Runtime.(container.StdinExecer)
184+
if !ok {
185+
return 0, ErrExecUnsupported
186+
}
187+
188+
pw := s.pass()
189+
if strings.ContainsAny(pw, "\r\n") {
190+
return 0, ErrInvalidPassword
191+
}
192+
193+
stdin := io.Reader(strings.NewReader(pw + "\n"))
194+
if in != nil {
195+
stdin = io.MultiReader(stdin, in)
196+
}
197+
198+
code, err := x.ExecStdin(
199+
ctx,
200+
s.container(),
201+
append([]string{"sh", "-c", clientScript, "sh"}, cmd...),
202+
stdin,
203+
out,
204+
0,
205+
0,
206+
)
207+
if err != nil {
208+
return 0, fmt.Errorf("%w: exec: %s", errExec, s.scrub(err.Error()))
209+
}
210+
211+
return code, nil
212+
}
213+
214+
// DumpTo streams a logical dump of dbs to w; accounts are not included. Standard error is discarded.
215+
func (s *Server) DumpTo(ctx context.Context, dbs []string, w io.Writer) error {
216+
if len(dbs) == 0 {
217+
return nil
218+
}
219+
220+
for _, d := range dbs {
221+
if !dbNameRE.MatchString(d) {
222+
return ErrInvalidIdentifier
223+
}
224+
}
225+
226+
cmd := append([]string{
227+
mysqldumpBin, "-u" + RootUser, argSingleTx, argRoutines, argEvents, argTriggers,
228+
argAddDrop, argGtidOff, argDatabases,
229+
}, dbs...)
230+
231+
cmd = append([]string{"sh", "-c", `exec "$@" 2>/dev/null`, "sh"}, cmd...)
232+
233+
code, err := s.stdinRun(ctx, cmd, nil, w)
234+
if err != nil {
235+
return err
236+
}
237+
238+
if code != 0 {
239+
return fmt.Errorf("%w: mysqldump exited %d", errExec, code)
240+
}
241+
242+
return nil
243+
}
244+
245+
// RestoreFrom runs a SQL stream such as DumpTo's output.
246+
func (s *Server) RestoreFrom(ctx context.Context, r io.Reader) error {
247+
var head headBuf
248+
249+
code, err := s.stdinRun(ctx, []string{mysqlBin, "-u" + RootUser}, r, &head)
250+
if err != nil {
251+
return err
252+
}
253+
254+
if code != 0 {
255+
return fmt.Errorf("%w: mysql exited %d: %s", errExec, code, s.scrub(strings.TrimSpace(string(head.buf))))
256+
}
257+
258+
return nil
259+
}
260+
261+
var dbNameRE = regexp.MustCompile(`^[A-Za-z0-9_$][A-Za-z0-9_$-]{0,63}$`)

0 commit comments

Comments
 (0)