reid 2 lat temu
rodzic
commit
928d40cc40
1 zmienionych plików z 20 dodań i 10 usunięć
  1. 20 10
      docker/wireguard.go

+ 20 - 10
docker/wireguard.go

@@ -100,16 +100,26 @@ func WriteWgConf() error {
 
 // either write directly or create volumes
 func writeWgConfToFile(filePath string, content string) error {
-	// try writing to path
-	err := ioutil.WriteFile(filePath, []byte(content), 0644)
-	if err != nil {
-		err = copyFileToVolume(filePath, "/etc/wireguard/", "wireguard")
-		// otherwise create the volume
-		if err != nil {
-			return fmt.Errorf("Failed to copy WG config file to volume: %v", err)
-		}
-	}
-	return nil
+    // try writing
+    err := ioutil.WriteFile(filePath, []byte(content), 0644)
+    if err == nil {
+        return nil
+    }
+    // ensure the directory structure exists
+    dir := filepath.Dir(filePath)
+    if err = os.MkdirAll(dir, 0755); err != nil {
+        return err
+    }
+    // try writing again
+    err = ioutil.WriteFile(filePath, []byte(content), 0644)
+    if err != nil {
+        err = copyFileToVolume(filePath, "/etc/wireguard/", "wireguard")
+        // otherwise create the volume
+        if err != nil {
+            return fmt.Errorf("Failed to copy WG config file to volume: %v", err)
+        }
+    }
+    return nil
 }
 
 // write wg conf to volume