blob: 0cf517203885089e6ad971a2661c19fd1b05d9a1 [file] [log] [blame]
Don Newtone973d342018-10-26 16:44:12 -04001/*
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*/
16package impl
17
18import (
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 Newton16520672018-11-28 14:44:42 -050027 "github.com/mongodb/mongo-go-driver/mongo/options"
Don Newtone973d342018-10-26 16:44:12 -040028 context "golang.org/x/net/context"
29)
30
31/*
32DoOutput - creates a backup and stores it to disk/mongodb
33*/
34func 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 Newton16520672018-11-28 14:44:42 -050041 clientOptions := options.Client()
Don Newton276cd1f2019-02-06 17:14:03 -050042 creds := options.Credential{AuthMechanism: "SCRAM-SHA-256", AuthSource: "AbstractOLT", Username: settings.GetMongoUser(), Password: settings.GetMongoPassword()}
Don Newton16520672018-11-28 14:44:42 -050043 clientOptions.SetAuth(creds)
44
45 client, err := mongo.NewClientWithOptions(settings.GetMongodb(), clientOptions)
Don Newtone973d342018-10-26 16:44:12 -040046 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 Newton16520672018-11-28 14:44:42 -050053 collection := client.Database("AbstractOLT").Collection("backups")
Don Newtone973d342018-10-26 16:44:12 -040054 //update or insert if not existent
Don Newton0766ec22018-11-06 15:18:05 -050055 upsert := true
Don Newton16520672018-11-28 14:44:42 -050056 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 Newtone973d342018-10-26 16:44:12 -040068 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}