forked from oracle/terraform-provider-oci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
121 lines (85 loc) · 2.64 KB
/
commands.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
package oci_tool
import (
"fmt"
"os"
)
// Copy target directory and append .backup
func CreateBackup(targetDir string, backupDir string) (err error) {
fmt.Println("Creating backup...", targetDir, "-->", backupDir)
fi, err := os.Stat(targetDir)
if err != nil {
return fmt.Errorf("Error reading directory\n %s", err)
}
if !fi.IsDir() {
return fmt.Errorf("File targeted for migration")
}
_, err = os.Stat(backupDir)
if err == nil {
return fmt.Errorf("Attempting to overwrite backups")
}
fmt.Println("Copying", targetDir, "-->", backupDir)
err = ProcessDirectory(targetDir, backupDir, CopyFile)
if err != nil {
return err
}
bfi, err := os.Stat(backupDir)
if fi.Size() != bfi.Size() {
return fmt.Errorf("Backup corrupt")
}
fmt.Println("Complete")
return
}
// Overwrite target directory with contents of .backup directory
func RestoreBackup(backupDir string, targetDir string) (err error) {
fmt.Println("Restoring from backup...")
fi, err := os.Stat(backupDir)
if err != nil {
return fmt.Errorf("Error reading backup\n %s", err)
}
err = os.RemoveAll(targetDir)
if err != nil {
return fmt.Errorf("Error removing original directory\n %s", err)
}
os.MkdirAll(targetDir, fi.Mode())
err = ProcessDirectory(backupDir, targetDir, CopyFile)
if err != nil {
return fmt.Errorf("Error restoring from backup directory\n %s", err)
}
fmt.Println("Complete")
return
}
// Remove .backup directory
func DeleteBackup(backupDir string) (err error) {
fmt.Println("Purging backup...")
err = os.RemoveAll(backupDir)
if err != nil {
return fmt.Errorf("Error removing backup directory\n %s", err)
}
fmt.Println("Complete")
return
}
// Traverse all .tf files and apply transforms
func Migrate(targetDir string, backupDir string) (err error) {
fmt.Println("Migrating plan directory...")
err = CreateBackup(targetDir, backupDir)
if err != nil {
return fmt.Errorf("Error backing up directory before migration\n %s", err)
}
err = ProcessDirectory(targetDir, backupDir, MigratePlanFile, ".tf", ".tfstate")
if err != nil {
return fmt.Errorf("Error removing backup directory\n %s", err)
}
fmt.Println("Complete")
return
}
// Traverse all .tf files and insert `region = "us-phoenix-1"` where region is not specified
func AddRegionField(targetDir string, backupDir string) (err error) {
fmt.Println("Scanning plans for providers missing region value...")
err = ProcessDirectory(targetDir, backupDir, AddRegionToProvider, ".tf")
if err != nil {
return fmt.Errorf("Error scanning providers for missing region value\n %s", err)
}
fmt.Println("Complete")
return
}