Skip to content

Commit

Permalink
add cli for aws operations (0glabs#17)
Browse files Browse the repository at this point in the history
* add cli
  • Loading branch information
0g-peterzhb authored Mar 21, 2024
1 parent 98405b9 commit 55aadcb
Show file tree
Hide file tree
Showing 12 changed files with 435 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store

inabox/testdata/*
inabox/anvil.pid

Expand Down
20 changes: 20 additions & 0 deletions cli/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"github.com/urfave/cli"
"github.com/0glabs/0g-data-avail/cli/flags"
"github.com/0glabs/0g-data-avail/common/aws"
"github.com/0glabs/0g-data-avail/common/logging"
)

type Config struct {
AwsClientConfig aws.ClientConfig
LoggerConfig logging.Config
}

func NewConfig(ctx *cli.Context) *Config {
return &Config{
AwsClientConfig: aws.ReadClientConfig(ctx, flags.FlagPrefix),
LoggerConfig: logging.ReadCLIConfig(ctx, flags.FlagPrefix),
}
}
36 changes: 36 additions & 0 deletions cli/flags/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package flags

import (
"github.com/urfave/cli"
"github.com/0glabs/0g-data-avail/common"
"github.com/0glabs/0g-data-avail/common/aws"
"github.com/0glabs/0g-data-avail/common/logging"
)

const (
FlagPrefix = "aws-cli"
envVarPrefix = "AWS_CLI"
)

var (
/* Required Flags */
S3BucketNameFlag = cli.StringFlag{
Name: common.PrefixFlag(FlagPrefix, "s3-bucket-name"),
Usage: "Name of the bucket to store blobs",
Required: true,
EnvVar: common.PrefixEnvVar(envVarPrefix, "S3_BUCKET_NAME"),
}
DynamoDBTableNameFlag = cli.StringFlag{
Name: common.PrefixFlag(FlagPrefix, "table-name"),
Usage: "Name of the dynamodb table",
Required: true,
EnvVar: common.PrefixEnvVar(envVarPrefix, "TABLE_NAME"),
}
)

// Flags contains the list of configuration options available to the binary.
var Flags []cli.Flag

func init() {
Flags = append(logging.CLIFlags(envVarPrefix, FlagPrefix), aws.ClientFlags(envVarPrefix, FlagPrefix)...)
}
273 changes: 273 additions & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
package main

import (
"context"
"fmt"
"log"
"os"

"github.com/urfave/cli"
"github.com/0glabs/0g-data-avail/cli/flags"
"github.com/0glabs/0g-data-avail/common/aws/dynamodb"
"github.com/0glabs/0g-data-avail/common/aws/s3"
"github.com/0glabs/0g-data-avail/common/logging"
"github.com/0glabs/0g-data-avail/common/store"
"github.com/0glabs/0g-data-avail/disperser/common/blobstore"
)

var (
Version = ""
GitCommit = ""
GitDate = ""
)

func main() {
app := cli.NewApp()
app.Version = fmt.Sprintf("%s-%s-%s", Version, GitCommit, GitDate)
app.Name = "aws-cli"
app.Usage = "ZGDA CLI"
app.Description = "Service for S3 Operations"
app.Flags = flags.Flags
app.Commands = []cli.Command{
{
Name: "bucket",
Usage: "bucket operation in s3",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "create a new bucket",
Flags: append(flags.Flags, flags.S3BucketNameFlag),
Action: CreateBucket,
},
{
Name: "delete",
Usage: "delete a bucket",
Flags: append(flags.Flags, flags.S3BucketNameFlag),
Action: DeleteBucket,
},
{
Name: "clear",
Usage: "clear a bucket",
Flags: append(flags.Flags, flags.S3BucketNameFlag),
Action: ClearBucket,
},
},
},
{
Name: "dynamodb",
Usage: "dynamodb operation in s3",
Subcommands: []cli.Command{
{
Name: "create_metadata_table",
Aliases: []string{"cmt"},
Usage: "create a metadata table",
Flags: append(flags.Flags, flags.DynamoDBTableNameFlag),
Action: CreateMetadataTable,
},
{
Name: "create_bucket_table",
Aliases: []string{"cbt"},
Usage: "create a bucket table",
Flags: append(flags.Flags, flags.DynamoDBTableNameFlag),
Action: CreateBucketTable,
},
{
Name: "delete_metadata_table",
Aliases: []string{"dmt"},
Usage: "delete a metadata table",
Flags: append(flags.Flags, flags.DynamoDBTableNameFlag),
Action: DeleteTable,
},
{
Name: "delete_bucket_table",
Aliases: []string{"dbt"},
Usage: "delete a bucket table",
Flags: append(flags.Flags, flags.DynamoDBTableNameFlag),
Action: DeleteTable,
},
{
Name: "empty_metadata_table",
Aliases: []string{"emt"},
Usage: "empty a metadata table",
Flags: append(flags.Flags, flags.DynamoDBTableNameFlag),
Action: ClearMetadataTable,
},
{
Name: "empty_bucket_table",
Aliases: []string{"ebt"},
Usage: "empty a bucket table",
Flags: append(flags.Flags, flags.DynamoDBTableNameFlag),
Action: ClearBucketTable,
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatalf("application failed: %v", err)
}
}

func CreateBucket(ctx *cli.Context) error {
config := NewConfig(ctx)

s3Client, err := getS3Client(config)
if err != nil {
return err
}

ctx_bg := context.Background()
bucketName := ctx.String(flags.S3BucketNameFlag.Name)
log.Println("bucketName: ", bucketName)
log.Println("region: ", config.AwsClientConfig.Region)
err = s3Client.CreateBucket(ctx_bg, bucketName, config.AwsClientConfig.Region)

if err != nil {
return err
}

return nil
}

func DeleteBucket(ctx *cli.Context) error {
config := NewConfig(ctx)

s3Client, err := getS3Client(config)
if err != nil {
return err
}

ctx_bg := context.Background()
bucketName := ctx.String(flags.S3BucketNameFlag.Name)
err = s3Client.DeleteBucket(ctx_bg, bucketName)

if err != nil {
return err
}

return nil
}

func ClearBucket(ctx *cli.Context) error {
config := NewConfig(ctx)

s3Client, err := getS3Client(config)
if err != nil {
return err
}

ctx_bg := context.Background()
bucketName := ctx.String(flags.S3BucketNameFlag.Name)
err = s3Client.ClearBucket(ctx_bg, bucketName)

if err != nil {
return err
}

return nil
}

func CreateMetadataTable(ctx *cli.Context) error {
return createTable(ctx, true)
}

func CreateBucketTable(ctx *cli.Context) error {
return createTable(ctx, false)
}

func createTable(ctx *cli.Context, isMetadata bool) error {
config := NewConfig(ctx)

dynamoClient, err := getDynamodbClient(config)
ctx_bg := context.Background()

if err != nil {
return err
}

tableName := ctx.String(flags.DynamoDBTableNameFlag.Name)
if isMetadata {
_, err = dynamoClient.CreateTable(ctx_bg, config.AwsClientConfig, tableName, blobstore.GenerateTableSchema(tableName, 10, 10))
} else {
_, err = dynamoClient.CreateTable(ctx_bg, config.AwsClientConfig, tableName, store.GenerateTableSchema(10, 10, tableName))
}

if err != nil {
return err
}

return nil
}

func DeleteTable(ctx *cli.Context) error {
config := NewConfig(ctx)

dynamoClient, err := getDynamodbClient(config)
ctx_bg := context.Background()

if err != nil {
return err
}

tableName := ctx.String(flags.DynamoDBTableNameFlag.Name)
err = dynamoClient.DeleteTable(ctx_bg, tableName)

if err != nil {
return err
}

return nil
}

func ClearMetadataTable(ctx *cli.Context) error {
err := DeleteTable(ctx)
if err != nil {
return err
}

err = CreateMetadataTable(ctx)
if err != nil {
return err
}
return nil
}

func ClearBucketTable(ctx *cli.Context) error {
err := DeleteTable(ctx)
if err != nil {
return err
}

err = CreateBucketTable(ctx)
if err != nil {
return err
}
return nil
}

func getS3Client(cfg *Config) (*s3.Client, error) {
logger, err := logging.GetLogger(cfg.LoggerConfig)
if err != nil {
return nil, err
}

s3Client, err := s3.NewClient(cfg.AwsClientConfig, logger)
if err != nil {
return nil, err
}

return s3Client, nil
}

func getDynamodbClient(cfg *Config) (*dynamodb.Client, error) {
logger, err := logging.GetLogger(cfg.LoggerConfig)
if err != nil {
return nil, err
}

dynamoClient, err := dynamodb.NewClient(cfg.AwsClientConfig, logger)
if err != nil {
return nil, err
}

return dynamoClient, nil
}
Loading

0 comments on commit 55aadcb

Please sign in to comment.