-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
197 lines (181 loc) · 5.3 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2017 Applatix, Inc.
package main
import (
"errors"
"fmt"
"log"
"os"
"time"
"github.com/applatix/claudia"
"github.com/applatix/claudia/billingbucket"
"github.com/applatix/claudia/costdb"
"github.com/applatix/claudia/ingest"
"github.com/applatix/claudia/userdb"
"github.com/applatix/claudia/util"
"github.com/urfave/cli"
)
func fetchReports(c *cli.Context) error {
reportDir := c.String("reportDir")
awsAccessKeyID := c.String("awsAccessKeyID")
awsSecretAccessKey := c.String("awsSecretAccessKey")
s3bucket := c.String("bucket")
reportPath := c.String("reportPath")
_ = c.BoolT("purgePrevious")
if reportDir == "" {
return errors.New("Local report dir unspecified")
}
if s3bucket == "" || reportPath == "" {
return errors.New("S3 bucket and report path must be specified")
}
region, err := billingbucket.GetBucketRegion(s3bucket)
if err != nil {
return err
}
_, err = billingbucket.NewAWSBillingBucket(awsAccessKeyID, awsSecretAccessKey, s3bucket, region, reportPath)
if err != nil {
log.Printf("Could not access billing bucket: %s", err)
return err
}
//return ingest.DownloadReports(billbuck, reportDir, nil, purgePrevious)
return errors.New("Fetch reports not yet implemented")
}
func processReports(c *cli.Context) error {
reportDir := c.String("reportDir")
costDBURL := c.String("costdb")
dropDatabase := c.Bool("dropDb")
reportID := c.String("reportID")
if reportID == "" {
return errors.New("reportID unspecified")
}
if reportDir == "" {
return errors.New("Local report dir unspecified")
}
costDB, err := costdb.NewCostDatabase(costDBURL)
if err != nil {
return err
}
defer costDB.Close()
if dropDatabase {
costDB.DropDatabase()
}
_ = costDB.NewCostReportContext(reportID)
return errors.New("Process reports not yet implemented")
}
func openUserDatabase() (*userdb.UserDatabase, error) {
var datasource string
// PostgreSQL. TODO: enable ssl
host := os.Getenv("USERDB_HOST")
if host == "" {
host = "userdb:5432"
}
postgresUser := os.Getenv("POSTGRES_USER")
if postgresUser == "" {
postgresUser = "postgres"
}
postgresPassword := os.Getenv("POSTGRES_PASSWORD")
postgresDatabase := os.Getenv("POSTGRES_DB")
if postgresDatabase == "" {
postgresDatabase = postgresUser
}
var credentials string
if postgresPassword == "" {
credentials = postgresUser
} else {
credentials = fmt.Sprintf("%s:%s", postgresUser, postgresPassword)
}
//datasource := fmt.Sprintf("postgres://%s:%s@%s/?sslmode=verify-full", username, password, host)
datasource = fmt.Sprintf("postgres://%s@%s/%s?sslmode=disable", credentials, host, postgresDatabase)
var userDB *userdb.UserDatabase
var err error
for i := 1; i <= 10; i++ {
userDB, err = userdb.Open(datasource)
if err == nil {
break
} else {
log.Println("Failed to open connection. Retrying", err)
time.Sleep(5 * time.Second)
}
}
if err != nil {
return nil, err
}
return userDB, nil
}
func dropDatabase(c *cli.Context) error {
costDBURL := c.String("costdb")
costDB, err := costdb.NewCostDatabase(costDBURL)
if err != nil {
return err
}
defer costDB.Close()
return costDB.DropDatabase()
}
func run(c *cli.Context) error {
reportDir := c.String("reportDir")
costDBURL := c.String("costdb")
workers := c.Int("workers")
port := c.Int("port")
userDB, err := openUserDatabase()
if err != nil {
return err
}
isc, err := ingest.NewIngestSvcContext(userDB, costDBURL, reportDir, workers)
if err != nil {
return err
}
return isc.Run(port)
}
func main() {
util.RegisterStackDumper()
util.StartStatsTicker(10 * time.Minute)
app := cli.NewApp()
app.Name = "ingestd"
app.Version = claudia.DisplayVersion
reportDirFlag := cli.StringFlag{Name: "reportDir", Value: claudia.ApplicationDir + "/reports", Usage: "Local directory to download and process reports"}
costDBFlag := cli.StringFlag{Name: "costdb", Value: claudia.CostDatabaseURL, Usage: "URL of cost database"}
app.Commands = []cli.Command{
{
Name: "fetch",
Usage: "Fetch the cost and usage reports to local directory",
Flags: []cli.Flag{
reportDirFlag,
cli.StringFlag{Name: "bucket", Value: "", Usage: "Name of S3 bucket containing the reports"},
cli.StringFlag{Name: "awsAccessKeyId", Value: "", Usage: "AWS access key ID"},
cli.StringFlag{Name: "awsSecretAccessKey", Value: "", Usage: "AWS secret access key"},
cli.StringFlag{Name: "reportPath", Value: "", Usage: "Report path to reports (excluding date range)"},
cli.BoolTFlag{Name: "purgePrevious", Usage: "Purge any previous outdated reports"},
},
Action: fetchReports,
},
{
Name: "process",
Usage: "Process reports and ingest to cost database",
Flags: []cli.Flag{
reportDirFlag,
costDBFlag,
cli.BoolFlag{Name: "dropDb", Usage: "Drop the database before importing"},
},
Action: processReports,
},
{
Name: "drop",
Usage: "Drop the cost and usage database",
Flags: []cli.Flag{
costDBFlag,
},
Action: dropDatabase,
},
{
Name: "run",
Usage: "Run as a service which periodically polls and fetches/processes reports",
Flags: []cli.Flag{
reportDirFlag,
costDBFlag,
cli.IntFlag{Name: "workers", Value: claudia.IngestdWorkers, Usage: "Number of concurrent ingest workers"},
cli.IntFlag{Name: "port", Value: claudia.IngestdPort, Usage: "Port to run on"},
},
Action: run,
},
}
app.Run(os.Args)
}