2019-08-05 18:28:06 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 Vulcanize
|
|
|
|
|
2019-02-28 17:51:54 +00:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2019-08-05 18:28:06 +00:00
|
|
|
|
2019-02-28 17:51:54 +00:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
2019-08-05 18:28:06 +00:00
|
|
|
|
2019-02-28 17:51:54 +00:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2019-08-05 18:28:06 +00:00
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-02-28 17:51:54 +00:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-03-07 17:02:52 +00:00
|
|
|
"strconv"
|
2019-02-28 17:51:54 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/config"
|
|
|
|
p2 "github.com/vulcanize/vulcanizedb/pkg/plugin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// composeCmd represents the compose command
|
|
|
|
var composeCmd = &cobra.Command{
|
|
|
|
Use: "compose",
|
|
|
|
Short: "Composes transformer initializer plugin",
|
|
|
|
Long: `This command needs a config .toml file of form:
|
|
|
|
|
|
|
|
[database]
|
|
|
|
name = "vulcanize_public"
|
|
|
|
hostname = "localhost"
|
|
|
|
user = "vulcanize"
|
|
|
|
password = "vulcanize"
|
|
|
|
port = 5432
|
|
|
|
|
|
|
|
[client]
|
2019-03-14 21:49:27 +00:00
|
|
|
ipcPath = "/Users/user/Library/Ethereum/geth.ipc"
|
2019-02-28 17:51:54 +00:00
|
|
|
|
|
|
|
[exporter]
|
|
|
|
home = "github.com/vulcanize/vulcanizedb"
|
|
|
|
name = "exampleTransformerExporter"
|
|
|
|
save = false
|
|
|
|
transformerNames = [
|
|
|
|
"transformer1",
|
|
|
|
"transformer2",
|
|
|
|
"transformer3",
|
|
|
|
"transformer4",
|
|
|
|
]
|
|
|
|
[exporter.transformer1]
|
|
|
|
path = "path/to/transformer1"
|
|
|
|
type = "eth_event"
|
|
|
|
repository = "github.com/account/repo"
|
|
|
|
migrations = "db/migrations"
|
2019-03-07 17:17:07 +00:00
|
|
|
rank = "0"
|
2019-02-28 17:51:54 +00:00
|
|
|
[exporter.transformer2]
|
|
|
|
path = "path/to/transformer2"
|
2019-03-13 16:14:35 +00:00
|
|
|
type = "eth_contract"
|
2019-02-28 17:51:54 +00:00
|
|
|
repository = "github.com/account/repo"
|
|
|
|
migrations = "db/migrations"
|
2019-03-08 16:31:32 +00:00
|
|
|
rank = "0"
|
2019-02-28 17:51:54 +00:00
|
|
|
[exporter.transformer3]
|
|
|
|
path = "path/to/transformer3"
|
|
|
|
type = "eth_event"
|
|
|
|
repository = "github.com/account/repo"
|
|
|
|
migrations = "db/migrations"
|
2019-03-07 17:17:07 +00:00
|
|
|
rank = "0"
|
2019-02-28 17:51:54 +00:00
|
|
|
[exporter.transformer4]
|
|
|
|
path = "path/to/transformer4"
|
|
|
|
type = "eth_storage"
|
|
|
|
repository = "github.com/account2/repo2"
|
|
|
|
migrations = "to/db/migrations"
|
2019-03-07 17:17:07 +00:00
|
|
|
rank = "1"
|
2019-02-28 17:51:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
Note: If any of the plugin transformer need additional
|
|
|
|
configuration variables include them in the .toml file as well
|
|
|
|
|
|
|
|
This information is used to write and build a go plugin with a transformer
|
|
|
|
set composed from the transformer imports specified in the config file
|
|
|
|
This plugin is loaded and the set of transformer initializers is exported
|
|
|
|
from it and loaded into and executed over by the appropriate watcher.
|
|
|
|
|
|
|
|
The type of watcher that the transformer works with is specified using the
|
|
|
|
type variable for each transformer in the config. Currently there are watchers
|
|
|
|
of event data from an eth node (eth_event) and storage data from an eth node
|
2019-03-13 16:14:35 +00:00
|
|
|
(eth_storage), and a more generic interface for accepting contract_watcher pkg
|
|
|
|
based transformers which can perform both event watching and public method
|
|
|
|
polling (eth_contract).
|
2019-02-28 17:51:54 +00:00
|
|
|
|
|
|
|
Transformers of different types can be ran together in the same command using a
|
|
|
|
single config file or in separate command instances using different config files
|
|
|
|
|
|
|
|
Specify config location when executing the command:
|
|
|
|
./vulcanizedb compose --config=./environments/config_name.toml`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-07-19 01:58:01 +00:00
|
|
|
SubCommand = cmd.CalledAs()
|
|
|
|
LogWithCommand = *log.WithField("SubCommand", SubCommand)
|
2019-02-28 17:51:54 +00:00
|
|
|
compose()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func compose() {
|
|
|
|
// Build plugin generator config
|
|
|
|
prepConfig()
|
|
|
|
|
|
|
|
// Generate code to build the plugin according to the config file
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Info("generating plugin")
|
2019-02-28 17:51:54 +00:00
|
|
|
generator, err := p2.NewGenerator(genConfig, databaseConfig)
|
|
|
|
if err != nil {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Debug("initializing plugin generator failed")
|
|
|
|
LogWithCommand.Fatal(err)
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
|
|
|
err = generator.GenerateExporterPlugin()
|
|
|
|
if err != nil {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Debug("generating plugin failed")
|
|
|
|
LogWithCommand.Fatal(err)
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
|
|
|
// TODO: Embed versioning info in the .so files so we know which version of vulcanizedb to run them with
|
|
|
|
_, pluginPath, err := genConfig.GetPluginPaths()
|
|
|
|
if err != nil {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Debug("getting plugin path failed")
|
|
|
|
LogWithCommand.Fatal(err)
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
|
|
|
fmt.Printf("Composed plugin %s", pluginPath)
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Info("plugin .so file output to ", pluginPath)
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(composeCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepConfig() {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Info("configuring plugin")
|
2019-02-28 17:51:54 +00:00
|
|
|
names := viper.GetStringSlice("exporter.transformerNames")
|
|
|
|
transformers := make(map[string]config.Transformer)
|
|
|
|
for _, name := range names {
|
|
|
|
transformer := viper.GetStringMapString("exporter." + name)
|
2019-03-07 17:02:52 +00:00
|
|
|
p, pOK := transformer["path"]
|
|
|
|
if !pOK || p == "" {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Fatal(name, " transformer config is missing `path` value")
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
2019-03-07 17:02:52 +00:00
|
|
|
r, rOK := transformer["repository"]
|
|
|
|
if !rOK || r == "" {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Fatal(name, " transformer config is missing `repository` value")
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
2019-03-07 17:02:52 +00:00
|
|
|
m, mOK := transformer["migrations"]
|
|
|
|
if !mOK || m == "" {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Fatal(name, " transformer config is missing `migrations` value")
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
2019-03-07 17:02:52 +00:00
|
|
|
mr, mrOK := transformer["rank"]
|
|
|
|
if !mrOK || mr == "" {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Fatal(name, " transformer config is missing `rank` value")
|
2019-03-07 17:02:52 +00:00
|
|
|
}
|
2019-03-12 15:46:42 +00:00
|
|
|
rank, err := strconv.ParseUint(mr, 10, 64)
|
2019-03-07 17:17:07 +00:00
|
|
|
if err != nil {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Fatal(name, " migration `rank` can't be converted to an unsigned integer")
|
2019-03-07 17:17:07 +00:00
|
|
|
}
|
2019-03-07 17:02:52 +00:00
|
|
|
t, tOK := transformer["type"]
|
|
|
|
if !tOK {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Fatal(name, " transformer config is missing `type` value")
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
|
|
|
transformerType := config.GetTransformerType(t)
|
|
|
|
if transformerType == config.UnknownTransformerType {
|
2019-07-19 01:58:01 +00:00
|
|
|
LogWithCommand.Fatal(errors.New(`unknown transformer type in exporter config accepted types are "eth_event", "eth_storage"`))
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
transformers[name] = config.Transformer{
|
|
|
|
Path: p,
|
|
|
|
Type: transformerType,
|
|
|
|
RepositoryPath: r,
|
|
|
|
MigrationPath: m,
|
2019-03-07 17:02:52 +00:00
|
|
|
MigrationRank: rank,
|
2019-02-28 17:51:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
genConfig = config.Plugin{
|
|
|
|
Transformers: transformers,
|
|
|
|
FilePath: "$GOPATH/src/github.com/vulcanize/vulcanizedb/plugins",
|
|
|
|
FileName: viper.GetString("exporter.name"),
|
|
|
|
Save: viper.GetBool("exporter.save"),
|
|
|
|
Home: viper.GetString("exporter.home"),
|
|
|
|
}
|
|
|
|
}
|