2016-09-28 19:32:58 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
Introduce ability to edit a dashboard cell
* Correct documentation for dashboards
* Exclude .git and use 'make run-dev' in 'make continuous'
* Fix dashboard deletion bug where id serialization was wrong
* Commence creation of overlay technology, add autoRefresh props to DashboardPage
* Enhance overlay magnitude of overlay technology
* Add confirm buttons to overlay technology
* Refactor ResizeContainer to accommodate arbitrary containers
* Refactor ResizeContainer to require explicit ResizeTop and ResizeBottom for clarity
* Add markup and styles for OverlayControls
* CellEditorOverlay needs a larger minimum bottom height to accommodate more things
* Revert Visualization to not use ResizeTop or flex-box
* Remove TODO and move to issue
* Refactor CellEditorOverlay to allow selection of graph type
* Style Overlay controls, move confirm buttons to own stylesheet
* Fix toggle buttons in overlay so active is actually active
* Block user-select on a few UI items
* Update cell query shape to support Visualization and LayoutRenderer
* Code cleanup
* Repair fixture schema; update props for affected components
* Wired up selectedGraphType and activeQueryID in CellEditorOverlay
* Wire up chooseMeasurements in QueryBuilder
Pass queryActions into QueryBuilder so that DataExplorer can provide
actionCreators and CellEditorOverlay can provide functions that
modify its component state
* semicolon cleanup
* Bind all queryModifier actions to component state with a stateReducer
* Overlay Technologies™ can add and delete a query from a cell
* Semicolon cleanup
* Add conversion of InfluxQL to QueryConfig for dashboards
* Update go deps to add influxdb at af72d9b0e4ebe95be30e89b160f43eabaf0529ed
* Updated docs for dashboard query config
* Update CHANGELOG to mention InfluxQL to QueryConfig
* Make reducer’s name more specific for clarity
* Remove 'table' as graphType
* Make graph renaming prettier
* Remove duplicate DashboardQuery in swagger.json
* Fix swagger to include name and links for Cell
* Refactor CellEditorOverlay to enable graph type selection
* Add link.self to all Dashboard cells; add bolt migrations
* Make dash graph names only hover on contents
* Consolidate timeRange format patterns, clean up
* Add cell endpoints to dashboards
* Include Line + Stat in Visualization Type list
* Add cell link to dashboards
* Enable step plot and stacked graph in Visualization
* Overlay Technologies are summonable and dismissable
* OverlayTechnologies saves changes to a cell
* Convert NameableGraph to createClass for state
This was converted from a pure function to encapsulate the state of the
buttons. An attempt was made previously to store this state in Redux,
but it proved too convoluted with the current state of the reducers for
cells and dashboards. Another effort must take place to separate a cell
reducer to manage the state of an individual cell in Redux in order for
this state to be sanely kept in Redux as well.
For the time being, this state is being kept in the component for the
sake of expeditiousness, since this is needed for Dashboards to be
released. A refactor of this will occur later.
* Cells should contain a links key in server response
* Clean up console logs
* Use live data instead of a cellQuery fixture
* Update docs for dashboard creation
* DB and RP are already present in the Command field
* Fix LayoutRenderer’s understanding of query schema
* Return a new object, rather that mutate in place
* Visualization doesn’t use activeQueryID
* Selected is an object, not a string
* QueryBuilder refactored to use query index instead of query id
* CellEditorOverlay refactored to use query index instead of query id
* ConfirmButtons doesn’t need to act on an item
* Rename functions to follow convention
* Queries are no longer guaranteed to have ids
* Omit WHERE and GROUP BY clauses when saving query
* Select new query on add in OverlayTechnologies
* Add click outside to dash graph menu, style menu also
* Change context menu from ... to a caret
More consistent with the rest of the UI, better affordance
* Hide graph context menu in presentation mode
Don’t want people editing a dashboard from presentation mode
* Move graph refreshing spinner so it does not overlap with context menu
* Wire up Cell Menu to Overlay Technologies
* Correct empty dashboard type
* Refactor dashboard spec fixtures
* Test syncDashboardCell reducer
* Remove Delete button from graph dropdown menu (for now)
* Update changelog
2017-03-24 00:12:33 +00:00
|
|
|
"context"
|
2017-12-16 21:32:21 +00:00
|
|
|
"fmt"
|
2017-12-16 09:01:14 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2017-10-20 17:50:19 +00:00
|
|
|
"path"
|
2016-09-28 19:32:58 +00:00
|
|
|
"time"
|
|
|
|
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/chronograf"
|
|
|
|
"github.com/influxdata/influxdb/v2/chronograf/id"
|
2020-09-23 21:07:13 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2016-09-28 19:32:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client is a client for the boltDB data store.
|
|
|
|
type Client struct {
|
2016-10-10 22:00:27 +00:00
|
|
|
Path string
|
|
|
|
db *bolt.DB
|
2017-12-18 20:06:17 +00:00
|
|
|
logger chronograf.Logger
|
2017-12-16 21:06:43 +00:00
|
|
|
isNew bool
|
2016-10-10 22:00:27 +00:00
|
|
|
Now func() time.Time
|
2016-10-20 14:38:23 +00:00
|
|
|
LayoutIDs chronograf.ID
|
2016-09-28 19:32:58 +00:00
|
|
|
|
2018-07-06 01:40:52 +00:00
|
|
|
BuildStore *BuildStore
|
|
|
|
SourcesStore *SourcesStore
|
|
|
|
ServersStore *ServersStore
|
|
|
|
LayoutsStore *LayoutsStore
|
|
|
|
DashboardsStore *DashboardsStore
|
|
|
|
UsersStore *UsersStore
|
|
|
|
OrganizationsStore *OrganizationsStore
|
|
|
|
ConfigStore *ConfigStore
|
|
|
|
MappingsStore *MappingsStore
|
|
|
|
OrganizationConfigStore *OrganizationConfigStore
|
2016-09-28 19:32:58 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 16:11:52 +00:00
|
|
|
// NewClient initializes all stores
|
2016-09-28 19:32:58 +00:00
|
|
|
func NewClient() *Client {
|
|
|
|
c := &Client{Now: time.Now}
|
2017-12-16 09:01:14 +00:00
|
|
|
c.BuildStore = &BuildStore{client: c}
|
2016-09-30 20:39:27 +00:00
|
|
|
c.SourcesStore = &SourcesStore{client: c}
|
2016-09-30 23:46:28 +00:00
|
|
|
c.ServersStore = &ServersStore{client: c}
|
2017-10-24 22:50:11 +00:00
|
|
|
c.LayoutsStore = &LayoutsStore{
|
2016-10-10 22:00:27 +00:00
|
|
|
client: c,
|
2017-12-15 04:53:14 +00:00
|
|
|
IDs: &id.UUID{},
|
2016-10-10 22:00:27 +00:00
|
|
|
}
|
Introduce ability to edit a dashboard cell
* Correct documentation for dashboards
* Exclude .git and use 'make run-dev' in 'make continuous'
* Fix dashboard deletion bug where id serialization was wrong
* Commence creation of overlay technology, add autoRefresh props to DashboardPage
* Enhance overlay magnitude of overlay technology
* Add confirm buttons to overlay technology
* Refactor ResizeContainer to accommodate arbitrary containers
* Refactor ResizeContainer to require explicit ResizeTop and ResizeBottom for clarity
* Add markup and styles for OverlayControls
* CellEditorOverlay needs a larger minimum bottom height to accommodate more things
* Revert Visualization to not use ResizeTop or flex-box
* Remove TODO and move to issue
* Refactor CellEditorOverlay to allow selection of graph type
* Style Overlay controls, move confirm buttons to own stylesheet
* Fix toggle buttons in overlay so active is actually active
* Block user-select on a few UI items
* Update cell query shape to support Visualization and LayoutRenderer
* Code cleanup
* Repair fixture schema; update props for affected components
* Wired up selectedGraphType and activeQueryID in CellEditorOverlay
* Wire up chooseMeasurements in QueryBuilder
Pass queryActions into QueryBuilder so that DataExplorer can provide
actionCreators and CellEditorOverlay can provide functions that
modify its component state
* semicolon cleanup
* Bind all queryModifier actions to component state with a stateReducer
* Overlay Technologies™ can add and delete a query from a cell
* Semicolon cleanup
* Add conversion of InfluxQL to QueryConfig for dashboards
* Update go deps to add influxdb at af72d9b0e4ebe95be30e89b160f43eabaf0529ed
* Updated docs for dashboard query config
* Update CHANGELOG to mention InfluxQL to QueryConfig
* Make reducer’s name more specific for clarity
* Remove 'table' as graphType
* Make graph renaming prettier
* Remove duplicate DashboardQuery in swagger.json
* Fix swagger to include name and links for Cell
* Refactor CellEditorOverlay to enable graph type selection
* Add link.self to all Dashboard cells; add bolt migrations
* Make dash graph names only hover on contents
* Consolidate timeRange format patterns, clean up
* Add cell endpoints to dashboards
* Include Line + Stat in Visualization Type list
* Add cell link to dashboards
* Enable step plot and stacked graph in Visualization
* Overlay Technologies are summonable and dismissable
* OverlayTechnologies saves changes to a cell
* Convert NameableGraph to createClass for state
This was converted from a pure function to encapsulate the state of the
buttons. An attempt was made previously to store this state in Redux,
but it proved too convoluted with the current state of the reducers for
cells and dashboards. Another effort must take place to separate a cell
reducer to manage the state of an individual cell in Redux in order for
this state to be sanely kept in Redux as well.
For the time being, this state is being kept in the component for the
sake of expeditiousness, since this is needed for Dashboards to be
released. A refactor of this will occur later.
* Cells should contain a links key in server response
* Clean up console logs
* Use live data instead of a cellQuery fixture
* Update docs for dashboard creation
* DB and RP are already present in the Command field
* Fix LayoutRenderer’s understanding of query schema
* Return a new object, rather that mutate in place
* Visualization doesn’t use activeQueryID
* Selected is an object, not a string
* QueryBuilder refactored to use query index instead of query id
* CellEditorOverlay refactored to use query index instead of query id
* ConfirmButtons doesn’t need to act on an item
* Rename functions to follow convention
* Queries are no longer guaranteed to have ids
* Omit WHERE and GROUP BY clauses when saving query
* Select new query on add in OverlayTechnologies
* Add click outside to dash graph menu, style menu also
* Change context menu from ... to a caret
More consistent with the rest of the UI, better affordance
* Hide graph context menu in presentation mode
Don’t want people editing a dashboard from presentation mode
* Move graph refreshing spinner so it does not overlap with context menu
* Wire up Cell Menu to Overlay Technologies
* Correct empty dashboard type
* Refactor dashboard spec fixtures
* Test syncDashboardCell reducer
* Remove Delete button from graph dropdown menu (for now)
* Update changelog
2017-03-24 00:12:33 +00:00
|
|
|
c.DashboardsStore = &DashboardsStore{
|
|
|
|
client: c,
|
2017-12-15 04:53:14 +00:00
|
|
|
IDs: &id.UUID{},
|
Introduce ability to edit a dashboard cell
* Correct documentation for dashboards
* Exclude .git and use 'make run-dev' in 'make continuous'
* Fix dashboard deletion bug where id serialization was wrong
* Commence creation of overlay technology, add autoRefresh props to DashboardPage
* Enhance overlay magnitude of overlay technology
* Add confirm buttons to overlay technology
* Refactor ResizeContainer to accommodate arbitrary containers
* Refactor ResizeContainer to require explicit ResizeTop and ResizeBottom for clarity
* Add markup and styles for OverlayControls
* CellEditorOverlay needs a larger minimum bottom height to accommodate more things
* Revert Visualization to not use ResizeTop or flex-box
* Remove TODO and move to issue
* Refactor CellEditorOverlay to allow selection of graph type
* Style Overlay controls, move confirm buttons to own stylesheet
* Fix toggle buttons in overlay so active is actually active
* Block user-select on a few UI items
* Update cell query shape to support Visualization and LayoutRenderer
* Code cleanup
* Repair fixture schema; update props for affected components
* Wired up selectedGraphType and activeQueryID in CellEditorOverlay
* Wire up chooseMeasurements in QueryBuilder
Pass queryActions into QueryBuilder so that DataExplorer can provide
actionCreators and CellEditorOverlay can provide functions that
modify its component state
* semicolon cleanup
* Bind all queryModifier actions to component state with a stateReducer
* Overlay Technologies™ can add and delete a query from a cell
* Semicolon cleanup
* Add conversion of InfluxQL to QueryConfig for dashboards
* Update go deps to add influxdb at af72d9b0e4ebe95be30e89b160f43eabaf0529ed
* Updated docs for dashboard query config
* Update CHANGELOG to mention InfluxQL to QueryConfig
* Make reducer’s name more specific for clarity
* Remove 'table' as graphType
* Make graph renaming prettier
* Remove duplicate DashboardQuery in swagger.json
* Fix swagger to include name and links for Cell
* Refactor CellEditorOverlay to enable graph type selection
* Add link.self to all Dashboard cells; add bolt migrations
* Make dash graph names only hover on contents
* Consolidate timeRange format patterns, clean up
* Add cell endpoints to dashboards
* Include Line + Stat in Visualization Type list
* Add cell link to dashboards
* Enable step plot and stacked graph in Visualization
* Overlay Technologies are summonable and dismissable
* OverlayTechnologies saves changes to a cell
* Convert NameableGraph to createClass for state
This was converted from a pure function to encapsulate the state of the
buttons. An attempt was made previously to store this state in Redux,
but it proved too convoluted with the current state of the reducers for
cells and dashboards. Another effort must take place to separate a cell
reducer to manage the state of an individual cell in Redux in order for
this state to be sanely kept in Redux as well.
For the time being, this state is being kept in the component for the
sake of expeditiousness, since this is needed for Dashboards to be
released. A refactor of this will occur later.
* Cells should contain a links key in server response
* Clean up console logs
* Use live data instead of a cellQuery fixture
* Update docs for dashboard creation
* DB and RP are already present in the Command field
* Fix LayoutRenderer’s understanding of query schema
* Return a new object, rather that mutate in place
* Visualization doesn’t use activeQueryID
* Selected is an object, not a string
* QueryBuilder refactored to use query index instead of query id
* CellEditorOverlay refactored to use query index instead of query id
* ConfirmButtons doesn’t need to act on an item
* Rename functions to follow convention
* Queries are no longer guaranteed to have ids
* Omit WHERE and GROUP BY clauses when saving query
* Select new query on add in OverlayTechnologies
* Add click outside to dash graph menu, style menu also
* Change context menu from ... to a caret
More consistent with the rest of the UI, better affordance
* Hide graph context menu in presentation mode
Don’t want people editing a dashboard from presentation mode
* Move graph refreshing spinner so it does not overlap with context menu
* Wire up Cell Menu to Overlay Technologies
* Correct empty dashboard type
* Refactor dashboard spec fixtures
* Test syncDashboardCell reducer
* Remove Delete button from graph dropdown menu (for now)
* Update changelog
2017-03-24 00:12:33 +00:00
|
|
|
}
|
2017-10-24 17:29:46 +00:00
|
|
|
c.UsersStore = &UsersStore{client: c}
|
|
|
|
c.OrganizationsStore = &OrganizationsStore{client: c}
|
2017-12-13 18:38:01 +00:00
|
|
|
c.ConfigStore = &ConfigStore{client: c}
|
2018-02-05 19:54:39 +00:00
|
|
|
c.MappingsStore = &MappingsStore{client: c}
|
2018-07-06 01:40:52 +00:00
|
|
|
c.OrganizationConfigStore = &OrganizationConfigStore{client: c}
|
2016-09-28 19:32:58 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-07-24 19:55:49 +00:00
|
|
|
// WithDB sets the boltdb database for a client. It should not be called
|
|
|
|
// after a call to Open.
|
|
|
|
func (c *Client) WithDB(db *bolt.DB) {
|
|
|
|
c.db = db
|
|
|
|
}
|
|
|
|
|
2017-12-18 20:06:17 +00:00
|
|
|
// Option to change behavior of Open()
|
|
|
|
type Option interface {
|
|
|
|
Backup() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBackup returns a Backup
|
|
|
|
func WithBackup() Option {
|
|
|
|
return Backup{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backup tells Open to perform a backup prior to initialization
|
|
|
|
type Backup struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backup returns true
|
|
|
|
func (b Backup) Backup() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open / create boltDB file.
|
|
|
|
func (c *Client) Open(ctx context.Context, logger chronograf.Logger, build chronograf.BuildInfo, opts ...Option) error {
|
2018-07-24 19:55:49 +00:00
|
|
|
if c.db == nil {
|
|
|
|
if _, err := os.Stat(c.Path); os.IsNotExist(err) {
|
|
|
|
c.isNew = true
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-18 20:06:17 +00:00
|
|
|
|
2018-07-24 19:55:49 +00:00
|
|
|
// Open database file.
|
|
|
|
db, err := bolt.Open(c.Path, 0600, &bolt.Options{Timeout: 1 * time.Second})
|
|
|
|
if err != nil {
|
2018-11-21 14:22:35 +00:00
|
|
|
return fmt.Errorf("unable to open boltdb; is there a chronograf already running? %v", err)
|
2018-07-24 19:55:49 +00:00
|
|
|
}
|
|
|
|
c.db = db
|
|
|
|
c.logger = logger
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
if opt.Backup() {
|
|
|
|
if err = c.backup(ctx, build); err != nil {
|
2018-11-21 14:22:35 +00:00
|
|
|
return fmt.Errorf("unable to backup your database prior to migrations: %v", err)
|
2018-07-24 19:55:49 +00:00
|
|
|
}
|
2017-12-18 20:06:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 19:55:49 +00:00
|
|
|
if err := c.initialize(ctx); err != nil {
|
2018-11-21 14:22:35 +00:00
|
|
|
return fmt.Errorf("unable to boot boltdb: %v", err)
|
2017-12-18 20:06:17 +00:00
|
|
|
}
|
2018-07-24 19:55:49 +00:00
|
|
|
if err := c.migrate(ctx, build); err != nil {
|
2018-11-21 14:22:35 +00:00
|
|
|
return fmt.Errorf("unable to migrate boltdb: %v", err)
|
2017-12-18 20:06:17 +00:00
|
|
|
}
|
2016-09-28 19:32:58 +00:00
|
|
|
|
2017-12-17 22:11:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-18 20:06:17 +00:00
|
|
|
// initialize creates Buckets that are missing
|
|
|
|
func (c *Client) initialize(ctx context.Context) error {
|
2016-09-28 19:32:58 +00:00
|
|
|
if err := c.db.Update(func(tx *bolt.Tx) error {
|
2018-03-06 02:31:50 +00:00
|
|
|
// Always create SchemaVersions bucket.
|
|
|
|
if _, err := tx.CreateBucketIfNotExists(SchemaVersionBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-25 18:29:14 +00:00
|
|
|
// Always create Organizations bucket.
|
|
|
|
if _, err := tx.CreateBucketIfNotExists(OrganizationsBucket); err != nil {
|
2017-10-20 17:50:19 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-09-30 20:39:27 +00:00
|
|
|
// Always create Sources bucket.
|
|
|
|
if _, err := tx.CreateBucketIfNotExists(SourcesBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-30 23:46:28 +00:00
|
|
|
// Always create Servers bucket.
|
|
|
|
if _, err := tx.CreateBucketIfNotExists(ServersBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-06 04:26:39 +00:00
|
|
|
// Always create Layouts bucket.
|
2017-10-24 22:50:11 +00:00
|
|
|
if _, err := tx.CreateBucketIfNotExists(LayoutsBucket); err != nil {
|
2016-10-06 04:26:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-12-15 08:49:43 +00:00
|
|
|
// Always create Dashboards bucket.
|
2017-10-24 22:50:11 +00:00
|
|
|
if _, err := tx.CreateBucketIfNotExists(DashboardsBucket); err != nil {
|
2016-12-15 08:49:43 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-11-17 23:57:46 +00:00
|
|
|
// Always create Users bucket.
|
|
|
|
if _, err := tx.CreateBucketIfNotExists(UsersBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-13 18:38:01 +00:00
|
|
|
// Always create Config bucket.
|
|
|
|
if _, err := tx.CreateBucketIfNotExists(ConfigBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-16 20:27:24 +00:00
|
|
|
// Always create Build bucket.
|
|
|
|
if _, err := tx.CreateBucketIfNotExists(BuildBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-05 19:54:39 +00:00
|
|
|
// Always create Mapping bucket.
|
|
|
|
if _, err := tx.CreateBucketIfNotExists(MappingsBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-06 01:40:52 +00:00
|
|
|
// Always create OrganizationConfig bucket.
|
|
|
|
if _, err := tx.CreateBucketIfNotExists(OrganizationConfigBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-28 19:32:58 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-16 20:27:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-10-31 23:50:03 +00:00
|
|
|
|
2017-12-18 20:06:17 +00:00
|
|
|
// migrate moves data from an old schema to a new schema in each Store
|
|
|
|
func (c *Client) migrate(ctx context.Context, build chronograf.BuildInfo) error {
|
2017-12-16 20:27:24 +00:00
|
|
|
if c.db != nil {
|
|
|
|
// Runtime migrations
|
|
|
|
if err := c.OrganizationsStore.Migrate(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.SourcesStore.Migrate(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.ServersStore.Migrate(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.LayoutsStore.Migrate(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-04 01:01:57 +00:00
|
|
|
if err := c.DashboardsStore.Migrate(ctx); err != nil {
|
2017-12-16 20:27:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.ConfigStore.Migrate(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-17 22:11:27 +00:00
|
|
|
if err := c.BuildStore.Migrate(ctx, build); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-05 19:54:39 +00:00
|
|
|
if err := c.MappingsStore.Migrate(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-06 01:40:52 +00:00
|
|
|
if err := c.OrganizationConfigStore.Migrate(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-07 01:05:01 +00:00
|
|
|
|
2018-04-04 00:25:48 +00:00
|
|
|
MigrateAll(c)
|
2017-12-16 20:27:24 +00:00
|
|
|
}
|
2017-10-31 23:50:03 +00:00
|
|
|
return nil
|
2016-09-28 19:32:58 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 16:11:52 +00:00
|
|
|
// Close the connection to the bolt database
|
2016-09-28 19:32:58 +00:00
|
|
|
func (c *Client) Close() error {
|
|
|
|
if c.db != nil {
|
|
|
|
return c.db.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-20 17:50:19 +00:00
|
|
|
|
2017-12-18 20:06:17 +00:00
|
|
|
// copy creates a copy of the database in toFile
|
|
|
|
func (c *Client) copy(ctx context.Context, version string) error {
|
|
|
|
backupDir := path.Join(path.Dir(c.Path), "backup")
|
|
|
|
if _, err := os.Stat(backupDir); os.IsNotExist(err) {
|
|
|
|
if err = os.Mkdir(backupDir, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-17 22:11:27 +00:00
|
|
|
fromFile, err := os.Open(c.Path)
|
2017-12-16 09:01:14 +00:00
|
|
|
if err != nil {
|
2017-12-16 21:52:21 +00:00
|
|
|
return err
|
2017-12-16 09:01:14 +00:00
|
|
|
}
|
2017-12-17 22:11:27 +00:00
|
|
|
defer fromFile.Close()
|
2017-12-16 09:01:14 +00:00
|
|
|
|
2017-12-17 22:11:27 +00:00
|
|
|
toName := fmt.Sprintf("%s.%s", path.Base(c.Path), version)
|
2017-12-16 21:32:21 +00:00
|
|
|
toPath := path.Join(backupDir, toName)
|
2017-12-17 22:11:27 +00:00
|
|
|
toFile, err := os.OpenFile(toPath, os.O_RDWR|os.O_CREATE, 0600)
|
2017-12-16 09:01:14 +00:00
|
|
|
if err != nil {
|
2017-12-16 21:52:21 +00:00
|
|
|
return err
|
2017-12-16 09:01:14 +00:00
|
|
|
}
|
2017-12-17 22:11:27 +00:00
|
|
|
defer toFile.Close()
|
2017-12-16 09:01:14 +00:00
|
|
|
|
2017-12-17 22:11:27 +00:00
|
|
|
_, err = io.Copy(toFile, fromFile)
|
2017-12-16 09:01:14 +00:00
|
|
|
if err != nil {
|
2017-12-16 21:52:21 +00:00
|
|
|
return err
|
2017-12-16 09:01:14 +00:00
|
|
|
}
|
|
|
|
|
2017-12-18 20:06:17 +00:00
|
|
|
c.logger.Info("Successfully created ", toPath)
|
2017-12-17 22:11:27 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-18 20:06:17 +00:00
|
|
|
// backup makes a copy of the database to the backup/ directory, if necessary:
|
2017-12-17 22:11:27 +00:00
|
|
|
// - If this is a fresh install, don't create a backup and store the current version
|
|
|
|
// - If we are on the same version, don't create a backup
|
|
|
|
// - If the version has changed, create a backup and store the current version
|
2017-12-18 20:06:17 +00:00
|
|
|
func (c *Client) backup(ctx context.Context, build chronograf.BuildInfo) error {
|
2017-12-17 22:11:27 +00:00
|
|
|
lastBuild, err := c.BuildStore.Get(ctx)
|
|
|
|
if err != nil {
|
2017-12-16 21:52:21 +00:00
|
|
|
return err
|
2017-12-16 20:27:24 +00:00
|
|
|
}
|
2017-12-17 22:11:27 +00:00
|
|
|
if lastBuild.Version == build.Version {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if c.isNew {
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-16 20:27:24 +00:00
|
|
|
|
2017-12-17 22:11:27 +00:00
|
|
|
// The database was pre-existing, and the version has changed
|
|
|
|
// and so create a backup
|
|
|
|
|
2017-12-18 20:06:17 +00:00
|
|
|
c.logger.Info("Moving from version ", lastBuild.Version)
|
|
|
|
c.logger.Info("Moving to version ", build.Version)
|
2017-12-17 22:11:27 +00:00
|
|
|
|
2017-12-18 20:06:17 +00:00
|
|
|
return c.copy(ctx, lastBuild.Version)
|
2017-12-16 09:01:14 +00:00
|
|
|
}
|