-
Notifications
You must be signed in to change notification settings - Fork 21
/
versions.go
162 lines (152 loc) · 4.43 KB
/
versions.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package versions
import (
"bufio"
"fmt"
"github.com/MG-RAST/AWE/lib/conf"
"github.com/MG-RAST/AWE/lib/core"
"github.com/MG-RAST/AWE/lib/db"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"os"
"strconv"
)
type Version struct {
Name string `bson:"name" json:"name"`
Version int `bson:"version" json:"version"`
}
type Versions []Version
var VersionMap = make(map[string]int)
func Initialize() (err error) {
session := db.Connection.Session.Copy()
defer session.Close()
c := session.DB(conf.MONGODB_DATABASE).C("Versions")
c.EnsureIndex(mgo.Index{Key: []string{"name"}, Unique: true})
var versions = new(Versions)
err = c.Find(bson.M{}).All(versions)
if err != nil {
return
}
for _, v := range *versions {
VersionMap[v.Name] = v.Version
}
if err = RunVersionUpdates(); err != nil {
return
}
// After version updates have succeeded without error, we can push the configured version numbers into the mongo db
// Note: configured version numbers are configured in conf.go but are NOT user configurable by design
if err = PushVersionsToDatabase(); err != nil {
return
}
return
}
func Print() (err error) {
fmt.Printf("##### Versions ####\n")
session := db.Connection.Session.Copy()
defer session.Close()
c := session.DB(conf.MONGODB_DATABASE).C("Versions")
var versions = new(Versions)
if err = c.Find(bson.M{}).All(versions); err != nil {
return err
}
for _, v := range *versions {
fmt.Printf("name: %v\tversion number: %v\n", v.Name, v.Version)
}
fmt.Println("")
return
}
func PushVersionsToDatabase() (err error) {
session := db.Connection.Session.Copy()
defer session.Close()
c := session.DB(conf.MONGODB_DATABASE).C("Versions")
for k, v := range conf.VERSIONS {
if _, err = c.Upsert(bson.M{"name": k}, bson.M{"$set": bson.M{"name": k, "version": v}}); err != nil {
return err
}
}
return
}
func RunVersionUpdates() (err error) {
// get Job struct version
confVersionJob, ok1 := conf.VERSIONS["Job"]
dbVersionJob, ok2 := VersionMap["Job"]
// Upgrading database if the Job version is less than 2 (or no Job version number exists) and
// the Job version in the config file exists and is greater than or equal to 2.
if (ok1 && confVersionJob >= 2) && (!ok2 || (ok2 && dbVersionJob < 2)) {
consoleReader := bufio.NewReader(os.Stdin)
session := db.Connection.Session.Copy()
defer session.Close()
c := session.DB(conf.MONGODB_DATABASE).C("Jobs")
jCount, err := c.Find(bson.M{}).Count()
if err != nil {
return err
}
if jCount > 0 {
fmt.Println("The Job struct version in your database needs updating to version 2 to run this version of the AWE server.")
fmt.Println("Your AWE server currently has " + strconv.Itoa(jCount) + " jobs in it.")
fmt.Print("Would you like to run the update to convert these jobs to the version 2 Job struct? (y/n): ")
text, _ := consoleReader.ReadString('\n')
if text[0] == 'y' {
var job = new(core.Job)
var jobDep = new(core.JobDep)
cnames, err := session.DB(conf.MONGODB_DATABASE).CollectionNames()
if err != nil {
return err
}
// Get temporary name for new Jobs collection
cname_tmp := ""
num := 1
var cnameExists = true
for cnameExists {
cnameExists = false
cname_tmp = "Jobs_update_" + strconv.Itoa(num)
for _, val := range cnames {
if val == cname_tmp {
cnameExists = true
}
}
num++
}
// Get name for storing old Jobs collection
cname_old := ""
num = 1
cnameExists = true
for cnameExists {
cnameExists = false
cname_old = "Jobs_old_" + strconv.Itoa(num)
for _, val := range cnames {
if val == cname_old {
cnameExists = true
}
}
num++
}
// Create handle to new collection
cNew := session.DB(conf.MONGODB_DATABASE).C(cname_tmp)
iter := c.Find(bson.M{}).Iter()
defer iter.Close()
for iter.Next(jobDep) {
job, err = core.JobDepToJob(jobDep)
if err != nil {
return err
}
err = cNew.Insert(&job)
if err != nil {
return err
}
}
err = session.Run(bson.D{{"renameCollection", conf.MONGODB_DATABASE + ".Jobs"}, {"to", conf.MONGODB_DATABASE + "." + cname_old}}, bson.M{})
if err != nil {
return err
}
err = session.Run(bson.D{{"renameCollection", conf.MONGODB_DATABASE + "." + cname_tmp}, {"to", conf.MONGODB_DATABASE + ".Jobs"}}, bson.M{})
if err != nil {
return err
}
} else {
fmt.Println("Exiting.")
os.Exit(0)
}
}
}
return
}