Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 the original author or authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | package impl |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | "log" |
| 21 | "os" |
| 22 | |
| 23 | "gerrit.opencord.org/abstract-olt/internal/pkg/settings" |
| 24 | "gerrit.opencord.org/abstract-olt/models" |
| 25 | "github.com/mongodb/mongo-go-driver/bson" |
| 26 | "github.com/mongodb/mongo-go-driver/mongo" |
Don Newton | 0766ec2 | 2018-11-06 15:18:05 -0500 | [diff] [blame^] | 27 | "github.com/mongodb/mongo-go-driver/options" |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 28 | context "golang.org/x/net/context" |
| 29 | ) |
| 30 | |
| 31 | /* |
| 32 | DoOutput - creates a backup and stores it to disk/mongodb |
| 33 | */ |
| 34 | func DoOutput() (bool, error) { |
| 35 | if isDirty { |
| 36 | myChan := getSyncChannel() |
| 37 | <-myChan |
| 38 | defer done(myChan, true) |
| 39 | chassisMap := models.GetChassisMap() |
| 40 | if settings.GetMongo() { |
| 41 | client, err := mongo.NewClient(settings.GetMongodb()) |
| 42 | client.Connect(context.Background()) |
| 43 | if err != nil { |
| 44 | log.Printf("client connect to mongo db @%s failed with %v\n", settings.GetMongodb(), err) |
| 45 | } |
| 46 | defer client.Disconnect(context.Background()) |
| 47 | for clli, chassisHolder := range *chassisMap { |
| 48 | json, _ := (chassisHolder).Serialize() |
| 49 | collection := client.Database("AbstractOLT").Collection("backup") |
| 50 | doc := bson.NewDocument(bson.EC.String("_id", clli)) |
| 51 | filter := bson.NewDocument(bson.EC.String("_id", clli)) |
| 52 | doc.Append(bson.EC.Binary("body", json)) |
| 53 | |
| 54 | updateDoc := bson.NewDocument(bson.EC.SubDocument("$set", doc)) |
| 55 | //update or insert if not existent |
Don Newton | 0766ec2 | 2018-11-06 15:18:05 -0500 | [diff] [blame^] | 56 | upsert := true |
| 57 | res, err := collection.UpdateOne(context.Background(), filter, updateDoc, &options.UpdateOptions{Upsert: &upsert}) |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 58 | if err != nil { |
| 59 | log.Printf("collection.UpdateOne failed with %v\n", err) |
| 60 | } else { |
| 61 | id := res.UpsertedID |
| 62 | if settings.GetDebug() { |
| 63 | log.Printf("Update Succeeded with id %v\n", id) |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } else { |
| 68 | for clli, chassisHolder := range *chassisMap { |
| 69 | |
| 70 | json, _ := (chassisHolder).Serialize() |
| 71 | if settings.GetMongo() { |
| 72 | |
| 73 | } else { |
| 74 | //TODO parameterize dump location |
| 75 | backupFile := fmt.Sprintf("backup/%s", clli) |
| 76 | f, _ := os.Create(backupFile) |
| 77 | |
| 78 | defer f.Close() |
| 79 | |
| 80 | _, _ = f.WriteString(string(json)) |
| 81 | f.Sync() |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | isDirty = false |
| 86 | } else { |
| 87 | if settings.GetDebug() { |
| 88 | log.Print("Not dirty not dumping config") |
| 89 | } |
| 90 | |
| 91 | } |
| 92 | return true, nil |
| 93 | |
| 94 | } |