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 | 1652067 | 2018-11-28 14:44:42 -0500 | [diff] [blame] | 27 | "github.com/mongodb/mongo-go-driver/mongo/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() { |
Don Newton | 1652067 | 2018-11-28 14:44:42 -0500 | [diff] [blame] | 41 | clientOptions := options.Client() |
Don Newton | 276cd1f | 2019-02-06 17:14:03 -0500 | [diff] [blame] | 42 | creds := options.Credential{AuthMechanism: "SCRAM-SHA-256", AuthSource: "AbstractOLT", Username: settings.GetMongoUser(), Password: settings.GetMongoPassword()} |
Don Newton | 1652067 | 2018-11-28 14:44:42 -0500 | [diff] [blame] | 43 | clientOptions.SetAuth(creds) |
| 44 | |
| 45 | client, err := mongo.NewClientWithOptions(settings.GetMongodb(), clientOptions) |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 46 | client.Connect(context.Background()) |
| 47 | if err != nil { |
| 48 | log.Printf("client connect to mongo db @%s failed with %v\n", settings.GetMongodb(), err) |
| 49 | } |
| 50 | defer client.Disconnect(context.Background()) |
| 51 | for clli, chassisHolder := range *chassisMap { |
| 52 | json, _ := (chassisHolder).Serialize() |
Don Newton | 1652067 | 2018-11-28 14:44:42 -0500 | [diff] [blame] | 53 | collection := client.Database("AbstractOLT").Collection("backups") |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 54 | //update or insert if not existent |
Don Newton | 0766ec2 | 2018-11-06 15:18:05 -0500 | [diff] [blame] | 55 | upsert := true |
Don Newton | 1652067 | 2018-11-28 14:44:42 -0500 | [diff] [blame] | 56 | res, err := collection.UpdateOne(context.Background(), |
| 57 | bson.D{ |
| 58 | {"_id", clli}, |
| 59 | }, |
| 60 | bson.D{ |
| 61 | { |
| 62 | "$set", bson.D{ |
| 63 | {"body", json}, |
| 64 | }, |
| 65 | }, |
| 66 | }, &options.UpdateOptions{Upsert: &upsert}) |
| 67 | |
Don Newton | e973d34 | 2018-10-26 16:44:12 -0400 | [diff] [blame] | 68 | if err != nil { |
| 69 | log.Printf("collection.UpdateOne failed with %v\n", err) |
| 70 | } else { |
| 71 | id := res.UpsertedID |
| 72 | if settings.GetDebug() { |
| 73 | log.Printf("Update Succeeded with id %v\n", id) |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } else { |
| 78 | for clli, chassisHolder := range *chassisMap { |
| 79 | |
| 80 | json, _ := (chassisHolder).Serialize() |
| 81 | if settings.GetMongo() { |
| 82 | |
| 83 | } else { |
| 84 | //TODO parameterize dump location |
| 85 | backupFile := fmt.Sprintf("backup/%s", clli) |
| 86 | f, _ := os.Create(backupFile) |
| 87 | |
| 88 | defer f.Close() |
| 89 | |
| 90 | _, _ = f.WriteString(string(json)) |
| 91 | f.Sync() |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | isDirty = false |
| 96 | } else { |
| 97 | if settings.GetDebug() { |
| 98 | log.Print("Not dirty not dumping config") |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | return true, nil |
| 103 | |
| 104 | } |