Skip to content

Commit 2edbb3d

Browse files
authored
Ensure registration file is cleared on restore (#134)
1 parent 21516be commit 2edbb3d

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

internal/flypg/node.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ func (n *Node) PostInit(ctx context.Context) error {
330330
}
331331

332332
// Let the boot process know that we've already been configured.
333-
if err := issueRegistrationCertificate(); err != nil {
333+
if err := issueRegistrationCert(); err != nil {
334334
return fmt.Errorf("failed to issue registration certificate: %s", err)
335335
}
336336

@@ -346,7 +346,7 @@ func (n *Node) PostInit(ctx context.Context) error {
346346
}
347347

348348
// Let the boot process know that we've already been configured.
349-
if err := issueRegistrationCertificate(); err != nil {
349+
if err := issueRegistrationCert(); err != nil {
350350
return fmt.Errorf("failed to issue registration certificate: %s", err)
351351
}
352352
}

internal/flypg/registration.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
"github.com/jackc/pgx/v5"
1111
)
1212

13-
const rCertPath = "/data/.registration"
13+
const registrationFile = "/data/.registration"
1414

1515
func isRegistered(ctx context.Context, conn *pgx.Conn, n *Node) (bool, error) {
1616
// Short-circuit if we are holding a certificate
17-
if hasRegistrationCertificate() {
17+
if registrationCertExists() {
1818
return true, nil
1919
}
2020

@@ -52,7 +52,7 @@ func isRegistered(ctx context.Context, conn *pgx.Conn, n *Node) (bool, error) {
5252

5353
// If we are active, issue registration certificate
5454
if member.Active {
55-
if err := issueRegistrationCertificate(); err != nil {
55+
if err := issueRegistrationCert(); err != nil {
5656
fmt.Println("failed to issue registration certificate.")
5757
return true, nil
5858
}
@@ -61,15 +61,19 @@ func isRegistered(ctx context.Context, conn *pgx.Conn, n *Node) (bool, error) {
6161
return true, nil
6262
}
6363

64-
func issueRegistrationCertificate() error {
65-
return os.WriteFile(rCertPath, []byte(""), 0600)
64+
func issueRegistrationCert() error {
65+
return os.WriteFile(registrationFile, []byte(""), 0600)
6666
}
6767

68-
func hasRegistrationCertificate() bool {
69-
if _, err := os.Stat(rCertPath); err != nil {
68+
func registrationCertExists() bool {
69+
if _, err := os.Stat(registrationFile); err != nil {
7070
if os.IsNotExist(err) {
7171
return false
7272
}
7373
}
7474
return true
7575
}
76+
77+
func removeRegistrationCert() error {
78+
return os.Remove(registrationFile)
79+
}

internal/flypg/restore.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,5 +222,11 @@ func clearLocks() error {
222222
}
223223
}
224224

225+
if err := removeRegistrationCert(); err != nil {
226+
if !os.IsNotExist(err) {
227+
return fmt.Errorf("failed to remove registration certificate: %s", err)
228+
}
229+
}
230+
225231
return nil
226232
}

0 commit comments

Comments
 (0)