|
17 | 17 | package cmd |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "encoding/json" |
20 | 21 | "fmt" |
21 | 22 | log "github.com/Sirupsen/logrus" |
| 23 | + "github.com/crunchydata/postgres-operator/backupservice" |
22 | 24 | "github.com/crunchydata/postgres-operator/tpr" |
23 | 25 | "github.com/spf13/cobra" |
24 | 26 | "github.com/spf13/viper" |
25 | 27 | "k8s.io/apimachinery/pkg/api/errors" |
26 | 28 | meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
27 | | - "time" |
| 29 | + "net/http" |
28 | 30 | ) |
29 | 31 |
|
30 | 32 | var backupCmd = &cobra.Command{ |
@@ -52,145 +54,42 @@ func showBackup(args []string) { |
52 | 54 |
|
53 | 55 | //show pod information for job |
54 | 56 | for _, arg := range args { |
55 | | - log.Debug("show backup called for " + arg) |
56 | | - //pg-database=basic or |
57 | | - //pgbackup=true |
58 | | - if arg == "all" { |
59 | | - lo := meta_v1.ListOptions{LabelSelector: "pgbackup=true"} |
60 | | - log.Debug("label selector is " + lo.LabelSelector) |
61 | | - pods, err2 := Clientset.CoreV1().Pods(Namespace).List(lo) |
62 | | - if err2 != nil { |
63 | | - log.Error(err2.Error()) |
64 | | - return |
65 | | - } |
66 | | - for _, pod := range pods.Items { |
67 | | - showBackupInfo(pod.ObjectMeta.Labels["pg-database"]) |
68 | | - } |
69 | | - |
70 | | - } else { |
71 | | - showBackupInfo(arg) |
72 | 57 |
|
| 58 | + url := "http://localhost:8080/backups/somename?showsecrets=true&other=thing" |
| 59 | + action := "GET" |
| 60 | + req, err := http.NewRequest(action, url, nil) |
| 61 | + if err != nil { |
| 62 | + log.Fatal("NewRequest: ", err) |
| 63 | + return |
73 | 64 | } |
74 | | - |
75 | | - } |
76 | | - |
77 | | -} |
78 | | -func showBackupInfo(name string) { |
79 | | - fmt.Println("\nbackup information for " + name + "...") |
80 | | - //print the pgbackups TPR if it exists |
81 | | - result := tpr.PgBackup{} |
82 | | - err := Tprclient.Get(). |
83 | | - Resource(tpr.BACKUP_RESOURCE). |
84 | | - Namespace(Namespace). |
85 | | - Name(name). |
86 | | - Do(). |
87 | | - Into(&result) |
88 | | - if err == nil { |
89 | | - printBackupTPR(&result) |
90 | | - } else if errors.IsNotFound(err) { |
91 | | - fmt.Println("\npgbackup TPR not found ") |
92 | | - } else { |
93 | | - log.Errorf("\npgbackup %s\n", name+" lookup error ") |
94 | | - log.Error(err.Error()) |
95 | | - return |
96 | | - } |
97 | | - |
98 | | - //print the backup jobs if any exists |
99 | | - lo := meta_v1.ListOptions{LabelSelector: "pgbackup=true,pg-database=" + name} |
100 | | - log.Debug("label selector is " + lo.LabelSelector) |
101 | | - pods, err2 := Clientset.CoreV1().Pods(Namespace).List(lo) |
102 | | - if err2 != nil { |
103 | | - log.Error(err2.Error()) |
104 | | - } |
105 | | - fmt.Printf("\nbackup job pods for database %s\n", name+"...") |
106 | | - |
107 | | - pvcMap := make(map[string]string) |
108 | | - |
109 | | - for _, p := range pods.Items { |
110 | | - |
111 | | - //get the pgdata volume info |
112 | | - for _, v := range p.Spec.Volumes { |
113 | | - if v.Name == "pgdata" { |
114 | | - fmt.Printf("%s%s (pvc %s)\n\n", TREE_TRUNK, p.Name, v.VolumeSource.PersistentVolumeClaim.ClaimName) |
115 | | - pvcMap[v.VolumeSource.PersistentVolumeClaim.ClaimName] = v.VolumeSource.PersistentVolumeClaim.ClaimName |
116 | | - } |
| 65 | + client := &http.Client{} |
| 66 | + resp, err := client.Do(req) |
| 67 | + if err != nil { |
| 68 | + log.Fatal("Do: ", err) |
| 69 | + return |
117 | 70 | } |
118 | | - fmt.Println("") |
| 71 | + defer resp.Body.Close() |
| 72 | + var response backupservice.ShowBackupResponse |
119 | 73 |
|
120 | | - } |
121 | | - |
122 | | - log.Debugf("ShowPVC is %v\n", ShowPVC) |
123 | | - |
124 | | - if ShowPVC { |
125 | | - //print pvc information for all jobs |
126 | | - for key, _ := range pvcMap { |
127 | | - PrintPVCListing(key) |
| 74 | + if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { |
| 75 | + log.Println(err) |
128 | 76 | } |
129 | | - } |
130 | | -} |
| 77 | + fmt.Println("Name = ", response.Items[0].Name) |
131 | 78 |
|
132 | | -func printBackupTPR(result *tpr.PgBackup) { |
133 | | - fmt.Printf("%s%s\n", "", "") |
134 | | - fmt.Printf("%s%s\n", "", "pgbackup : "+result.Spec.Name) |
| 79 | + log.Debug("show backup called for " + arg) |
135 | 80 |
|
136 | | - fmt.Printf("%s%s\n", TREE_BRANCH, "PVC Name:\t"+result.Spec.StorageSpec.PvcName) |
137 | | - fmt.Printf("%s%s\n", TREE_BRANCH, "PVC Access Mode:\t"+result.Spec.StorageSpec.PvcAccessMode) |
138 | | - fmt.Printf("%s%s\n", TREE_BRANCH, "PVC Size:\t\t"+result.Spec.StorageSpec.PvcSize) |
139 | | - fmt.Printf("%s%s\n", TREE_BRANCH, "CCP_IMAGE_TAG:\t"+result.Spec.CCP_IMAGE_TAG) |
140 | | - fmt.Printf("%s%s\n", TREE_BRANCH, "Backup Status:\t"+result.Spec.BACKUP_STATUS) |
141 | | - fmt.Printf("%s%s\n", TREE_BRANCH, "Backup Host:\t"+result.Spec.BACKUP_HOST) |
142 | | - fmt.Printf("%s%s\n", TREE_BRANCH, "Backup User:\t"+result.Spec.BACKUP_USER) |
143 | | - fmt.Printf("%s%s\n", TREE_BRANCH, "Backup Pass:\t"+result.Spec.BACKUP_PASS) |
144 | | - fmt.Printf("%s%s\n", TREE_TRUNK, "Backup Port:\t"+result.Spec.BACKUP_PORT) |
| 81 | + } |
145 | 82 |
|
146 | 83 | } |
147 | 84 |
|
148 | 85 | func createBackup(args []string) { |
149 | 86 | log.Debugf("createBackup called %v\n", args) |
150 | 87 |
|
151 | | - var err error |
152 | | - var newInstance *tpr.PgBackup |
| 88 | + //var err error |
153 | 89 |
|
154 | 90 | for _, arg := range args { |
155 | 91 | log.Debug("create backup called for " + arg) |
156 | | - result := tpr.PgBackup{} |
157 | | - |
158 | | - // error if it already exists |
159 | | - err = Tprclient.Get(). |
160 | | - Resource(tpr.BACKUP_RESOURCE). |
161 | | - Namespace(Namespace). |
162 | | - Name(arg). |
163 | | - Do(). |
164 | | - Into(&result) |
165 | | - if err == nil { |
166 | | - fmt.Println("pgbackup " + arg + " was found so we recreate it") |
167 | | - dels := make([]string, 1) |
168 | | - dels[0] = arg |
169 | | - deleteBackup(dels) |
170 | | - time.Sleep(2000 * time.Millisecond) |
171 | | - } else if errors.IsNotFound(err) { |
172 | | - log.Debug("pgbackup " + arg + " not found so we will create it") |
173 | | - } else { |
174 | | - log.Error("error getting pgbackup " + arg) |
175 | | - log.Error(err.Error()) |
176 | | - break |
177 | | - } |
178 | | - // Create an instance of our TPR |
179 | | - newInstance, err = getBackupParams(arg) |
180 | | - if err != nil { |
181 | | - log.Error("error creating backup") |
182 | | - break |
183 | | - } |
184 | 92 |
|
185 | | - err = Tprclient.Post(). |
186 | | - Resource(tpr.BACKUP_RESOURCE). |
187 | | - Namespace(Namespace). |
188 | | - Body(newInstance). |
189 | | - Do().Into(&result) |
190 | | - if err != nil { |
191 | | - log.Error("error in creating PgBackup TPR instance") |
192 | | - log.Error(err.Error()) |
193 | | - } |
194 | 93 | fmt.Println("created PgBackup " + arg) |
195 | 94 |
|
196 | 95 | } |
|
0 commit comments