Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cobra cli #79

Merged
merged 5 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@ builds:
- amd64
main: .
binary: datasetGetProposal

- id: "scicat-cli"
flags:
- -trimpath
ldflags:
- "-s -w -X 'github.com/paulscherrerinstitute/scicat/cmd/commands.VERSION={{.Version}}'"
env:
- CGO_ENABLED=0
dir: ./cmd/
goos:
- linux
- windows
- darwin
goarch:
- amd64
main: .
binary: scicat-cli


archives:
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Building

### General Informations

For testing, just build `main.go` for each command:

```
Expand All @@ -22,6 +24,13 @@ Tools are compiled for the following architectures:

These can be cross-compiled from any system.

### Building the CLI based on Cobra

```
cd cmd
go build -o scicat-cli
```

## Deployment

PSI deploys tools to the following locations.
Expand Down
18 changes: 18 additions & 0 deletions cmd/commands/commonConstants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

const MANUAL = "http://melanie.gitpages.psi.ch/SciCatPages"

const PROD_API_SERVER string = "https://dacat.psi.ch/api/v3"
const TEST_API_SERVER string = "https://dacat-qa.psi.ch/api/v3"
const DEV_API_SERVER string = "https://dacat-development.psi.ch/api/v3"
const LOCAL_API_SERVER string = "http://localhost:3000/api/v3"
const TUNNEL_API_SERVER string = "https://dacat-development.psi.ch:5443/api/v3"

const PROD_RSYNC_ARCHIVE_SERVER string = "pb-archive.psi.ch"
const TEST_RSYNC_ARCHIVE_SERVER string = "pbt-archive.psi.ch"
const DEV_RSYNC_ARCHIVE_SERVER string = "arematest2in.psi.ch"
const LOCAL_RSYNC_ARCHIVE_SERVER string = "localhost"
const TUNNEL_RSYNC_ARCHIVE_SERVER string = "arematest2in.psi.ch:2022"

const PUBLISHServer string = "doi2.psi.ch"
const RETRIEVELocation string = "/data/archiveManager/retrieve/"
130 changes: 130 additions & 0 deletions cmd/commands/datasetArchiver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package cmd

import (
"bufio"
"crypto/tls"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"

"github.com/fatih/color"
"github.com/paulscherrerinstitute/scicat/datasetUtils"
"github.com/spf13/cobra"
)

var datasetArchiverCmd = &cobra.Command{
Use: "datasetArchiver [options] (ownerGroup | space separated list of datasetIds)",
Short: "Archives all datasets in state datasetCreated from a given ownerGroup",
Long: `Tool to archive datasets to the data catalog.

You must choose either an ownerGroup, in which case all archivable datasets
of this ownerGroup not yet archived will be archived.
Or you choose a (list of) datasetIds, in which case all archivable datasets
of this list not yet archived will be archived.

For further help see "` + MANUAL + `"`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// consts & vars
var client = &http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: false}},
Timeout: 10 * time.Second}

const CMD = "datasetArchiver"
var scanner = bufio.NewScanner(os.Stdin)

var APIServer string
var env string

// pass parameters
userpass, _ := cmd.Flags().GetString("user")
token, _ := cmd.Flags().GetString("token")
tapecopies, _ := cmd.Flags().GetInt("tapecopies")
testenvFlag, _ := cmd.Flags().GetBool("testenv")
localenvFlag, _ := cmd.Flags().GetBool("localenv")
devenvFlag, _ := cmd.Flags().GetBool("devenv")
nonInteractiveFlag, _ := cmd.Flags().GetBool("noninteractive")
showVersion, _ := cmd.Flags().GetBool("version")

// execute command
if showVersion {
fmt.Printf("%s\n", VERSION)
return
}

// check for program version only if running interactively
datasetUtils.CheckForNewVersion(client, CMD, VERSION)

if testenvFlag {
APIServer = TEST_API_SERVER
env = "test"
} else if devenvFlag {
APIServer = DEV_API_SERVER
env = "dev"
} else if localenvFlag {
APIServer = LOCAL_API_SERVER
env = "local"
} else {
APIServer = PROD_API_SERVER
env = "production"
}

color.Set(color.FgGreen)
log.Printf("You are about to archive dataset(s) to the === %s === data catalog environment...", env)
color.Unset()

ownerGroup := ""
inputdatasetList := make([]string, 0)

// argsWithoutProg := os.Args[1:]
if len(args) == 0 {
log.Println("invalid number of args")
return
} else if len(args) == 1 && !strings.Contains(args[0], "/") {
ownerGroup = args[0]
} else {
inputdatasetList = args[0:]
}

auth := &datasetUtils.RealAuthenticator{}
user, _ := datasetUtils.Authenticate(auth, client, APIServer, &token, &userpass)

archivableDatasets := datasetUtils.GetArchivableDatasets(client, APIServer, ownerGroup, inputdatasetList, user["accessToken"])
if len(archivableDatasets) > 0 {
archive := ""
if nonInteractiveFlag {
archive = "y"
} else {
fmt.Printf("\nDo you want to archive these %v datasets (y/N) ? ", len(archivableDatasets))
scanner.Scan()
archive = scanner.Text()
}
if archive != "y" {
log.Fatalf("Okay the archive process is stopped here, no datasets will be archived\n")
} else {
log.Printf("You chose to archive the new datasets\n")
log.Printf("Submitting Archive Job for the ingested datasets.\n")
jobId := datasetUtils.CreateJob(client, APIServer, user, archivableDatasets, &tapecopies)
fmt.Println(jobId)
}
} else {
log.Fatalf("No archivable datasets remaining")
}
},
}

func init() {
rootCmd.AddCommand(datasetArchiverCmd)

datasetArchiverCmd.Flags().String("user", "", "Defines optional username and password")
datasetArchiverCmd.Flags().String("token", "", "Defines optional API token instead of username:password")
datasetArchiverCmd.Flags().Int("tapecopies", 1, "Number of tapecopies to be used for archiving")
datasetArchiverCmd.Flags().Bool("testenv", false, "Use test environment (qa) instead or production")
datasetArchiverCmd.Flags().Bool("localenv", false, "Use local environment (local) instead or production")
datasetArchiverCmd.Flags().Bool("devenv", false, "Use development environment instead or production")
datasetArchiverCmd.Flags().Bool("noninteractive", false, "Defines if no questions will be asked, just do it - make sure you know what you are doing")
datasetArchiverCmd.Flags().Bool("version", false, "Show version number and exit")
}
116 changes: 116 additions & 0 deletions cmd/commands/datasetCleaner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cmd

import (
"crypto/tls"
"fmt"
"log"
"net/http"
"time"

"github.com/fatih/color"
"github.com/paulscherrerinstitute/scicat/datasetUtils"
"github.com/spf13/cobra"
)

var datasetCleanerCmd = &cobra.Command{
Use: "datasetCleaner [options] datasetPid",
Short: "Remove dataset from archive and optionally from data catalog",
Long: `Tool to remove datasets from the data catalog.

If Datablock entries exist for a given dataset, a reset job will be launched.

If the Dataset should be removed from the data catalog, the corresponding
documents in Dataset and OrigDatablock will be deleted as well. This will only
happen once the reset job is finished. The tool will try to remove the dataset
catalog entries each minute until Dataset is found to be in archivable state again,
and only then it will be deleted in the data catalog.

Note: these actions can not be un-done! Be careful!

For further help see "` + MANUAL + `"`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// vars & consts
var client = &http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: false}},
Timeout: 10 * time.Second}

const CMD = "datasetCleaner"

var APIServer string
var env string

// pass parameters
removeFromCatalogFlag, _ := cmd.Flags().GetBool("removeFromCatalog")
nonInteractiveFlag, _ := cmd.Flags().GetBool("nonInteractive")
testenvFlag, _ := cmd.Flags().GetBool("testenv")
devenvFlag, _ := cmd.Flags().GetBool("devenv")
userpass, _ := cmd.Flags().GetString("user")
token, _ := cmd.Flags().GetString("token")
showVersion, _ := cmd.Flags().GetBool("version")

// execute command
if showVersion {
fmt.Printf("%s\n", VERSION)
return
}

// check for program version only if running interactively

datasetUtils.CheckForNewVersion(client, CMD, VERSION)
datasetUtils.CheckForServiceAvailability(client, testenvFlag, true)

//}

if testenvFlag {
APIServer = TEST_API_SERVER
env = "test"
} else if devenvFlag {
APIServer = DEV_API_SERVER
env = "dev"
} else {
APIServer = PROD_API_SERVER
env = "production"
}

color.Set(color.FgRed)
log.Printf("You are about to remove a dataset from the === %s === data catalog environment...", env)
color.Unset()

pid := ""

if len(args) == 1 {
pid = args[0]
} else {
log.Println("invalid number of args")
return
}

auth := &datasetUtils.RealAuthenticator{}
user, _ := datasetUtils.Authenticate(auth, client, APIServer, &token, &userpass)

if user["username"] != "archiveManager" {
log.Fatalf("You must be archiveManager to be allowed to delete datasets\n")
}

datasetUtils.RemoveFromArchive(client, APIServer, pid, user, nonInteractiveFlag)

if removeFromCatalogFlag {
datasetUtils.RemoveFromCatalog(client, APIServer, pid, user, nonInteractiveFlag)
} else {
log.Println("To also delete the dataset from the catalog add the flag -removeFromCatalog")
}
},
}

func init() {
rootCmd.AddCommand(datasetCleanerCmd)

datasetCleanerCmd.Flags().Bool("removeFromCatalog", false, "Defines if the dataset should also be deleted from data catalog")
datasetCleanerCmd.Flags().Bool("nonInteractive", false, "Defines if no questions will be asked, just do it - make sure you know what you are doing")
datasetCleanerCmd.Flags().Bool("testenv", false, "Use test environment (qa) instead of production environment")
datasetCleanerCmd.Flags().Bool("devenv", false, "Use development environment instead of production environment (developers only)")
datasetCleanerCmd.Flags().String("user", "", "Defines optional username:password string")
datasetCleanerCmd.Flags().String("token", "", "Defines optional API token instead of username:password")
datasetCleanerCmd.Flags().Bool("version", false, "Show version number and exit")
}
Loading
Loading