/
main.go
128 lines (110 loc) · 3.18 KB
/
main.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// This command populates the blobhash256 field of all entities.
// This command is intended to be run on the production db and then discarded.
// The first time this command is executed, all the entities are updated.
// Subsequent runs have no effect.
package main
import (
"crypto/sha256"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"github.com/juju/loggo"
"gopkg.in/errgo.v1"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"gopkg.in/juju/charmstore.v4/config"
"gopkg.in/juju/charmstore.v4/internal/charmstore"
"gopkg.in/juju/charmstore.v4/internal/mongodoc"
)
var (
logger = loggo.GetLogger("cshash256")
loggingConfig = flag.String("logging-config", "INFO", "specify log levels for modules e.g. <root>=TRACE")
)
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [options] <config path>\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
os.Exit(2)
}
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
}
if *loggingConfig != "" {
if err := loggo.ConfigureLoggers(*loggingConfig); err != nil {
fmt.Fprintf(os.Stderr, "cannot configure loggers: %v", err)
os.Exit(1)
}
}
if err := run(flag.Arg(0)); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
func run(confPath string) error {
logger.Infof("reading configuration")
conf, err := config.Read(confPath)
if err != nil {
return errgo.Notef(err, "cannot read config file %q", confPath)
}
logger.Infof("connecting to mongo")
session, err := mgo.Dial(conf.MongoURL)
if err != nil {
return errgo.Notef(err, "cannot dial mongo at %q", conf.MongoURL)
}
defer session.Close()
db := session.DB("juju")
logger.Infof("instantiating the store")
pool, err := charmstore.NewPool(db, nil, nil)
if err != nil {
return errgo.Notef(err, "cannot create a new store")
}
store := pool.Store()
defer store.Close()
logger.Infof("updating entities")
if err := update(store); err != nil {
return errgo.Notef(err, "cannot update entities")
}
logger.Infof("done")
return nil
}
func update(store *charmstore.Store) error {
entities := store.DB.Entities()
var entity mongodoc.Entity
iter := entities.Find(bson.D{{"blobhash256", ""}}).Select(bson.D{{"blobname", 1}}).Iter()
defer iter.Close()
counter := 0
for iter.Next(&entity) {
// Retrieve the archive contents.
r, _, err := store.BlobStore.Open(entity.BlobName)
if err != nil {
return errgo.Notef(err, "cannot open archive data for %s", entity.URL)
}
// Calculate the contents hash.
hash := sha256.New()
if _, err = io.Copy(hash, r); err != nil {
r.Close()
return errgo.Notef(err, "cannot calculate archive sha256 for %s", entity.URL)
}
r.Close()
// Update the entity document.
if err := entities.UpdateId(entity.URL, bson.D{{
"$set", bson.D{{"blobhash256", fmt.Sprintf("%x", hash.Sum(nil))}},
}}); err != nil {
return errgo.Notef(err, "cannot update entity id %s", entity.URL)
}
counter++
if counter%100 == 0 {
logger.Infof("%d entities updated", counter)
}
}
if err := iter.Close(); err != nil {
return errgo.Notef(err, "cannot iterate entities")
}
logger.Infof("%d entities updated", counter)
return nil
}