Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/goharbor/harbor-cli/cmd/harbor/root/context"
"github.com/goharbor/harbor-cli/cmd/harbor/root/cve"
"github.com/goharbor/harbor-cli/cmd/harbor/root/instance"
"github.com/goharbor/harbor-cli/cmd/harbor/root/jobservice"
"github.com/goharbor/harbor-cli/cmd/harbor/root/labels"
"github.com/goharbor/harbor-cli/cmd/harbor/root/ldap"
"github.com/goharbor/harbor-cli/cmd/harbor/root/project"
Expand Down Expand Up @@ -203,6 +204,10 @@ harbor help
cmd.GroupID = "system"
root.AddCommand(cmd)

cmd = jobservice.JobServiceCmd()
cmd.GroupID = "system"
root.AddCommand(cmd)

// Utils
cmd = versionCommand()
cmd.GroupID = "utils"
Expand Down
55 changes: 55 additions & 0 deletions cmd/harbor/root/jobservice/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package jobservice

import (
"github.com/spf13/cobra"
)

func JobServiceCmd() *cobra.Command {
// jobserviceCmd represents the jobservice command.
var jobserviceCmd = &cobra.Command{
Use: "jobservice",
Aliases: []string{"js"},
Short: "Manage Harbor job service",
Long: `Manage Harbor job service, including queues, jobs, worker pools and workers.

This command provides terminal-based access to the Jobservice dashboard, allowing you to:
- Monitor and manage job queues (pause, resume, clear)
- View running jobs and stop them if necessary
- Inspect worker pools and active workers
- Access job logs with real-time tailing support`,
Example: ` # List all job queues
harbor jobservice queue list

# Pause a specific job queue
harbor jobservice queue pause IMAGE_SCAN

# Stop a running job
harbor jobservice job stop <job-id>

# Follow job logs in real-time
harbor jobservice job log <job-id> --follow`,
}

jobserviceCmd.AddCommand(
QueueCommand(),
JobCommand(),
PoolCommand(),
WorkerCommand(),
)

return jobserviceCmd
}
106 changes: 106 additions & 0 deletions cmd/harbor/root/jobservice/job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package jobservice

import (
"fmt"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/base/logviewer"
view "github.com/goharbor/harbor-cli/pkg/views/jobservice"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func JobCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "job",
Short: "Manage individual jobs",
}

cmd.AddCommand(
StopJobCommand(),
LogJobCommand(),
)

return cmd
}

func StopJobCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "stop [job-id]",
Short: "Stop a particular job",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var jobID string
if len(args) > 0 {
jobID = args[0]
} else {
log.Debug("No job ID provided, switching to interactive selection...")
var err error
jobID, err = view.SelectRunningJobAsync()
if err != nil {
return err
}
}

log.Debugf("Attempting to stop job: %s", jobID)
err := api.StopJob(jobID)
if err != nil {
return fmt.Errorf("failed to stop job: %v", utils.ParseHarborErrorMsg(err))
}
fmt.Printf("Job \"%s\" stopped successfully\n", jobID)
return nil
},
}
return cmd
}

func LogJobCommand() *cobra.Command {
var follow bool
var refreshInterval string

cmd := &cobra.Command{
Use: "log <job-id>",
Short: "Display logs of a particular job",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
jobID := args[0]

interval := 5 * time.Second
if refreshInterval != "" {
var err error
interval, err = time.ParseDuration(refreshInterval)
if err != nil {
return fmt.Errorf("invalid refresh interval: %w", err)
}
}

m := logviewer.NewModel(jobID, api.GetJobLog, follow, interval)
if _, err := tea.NewProgram(m, tea.WithAltScreen()).Run(); err != nil {
return fmt.Errorf("error running log viewer: %w", err)
}
return nil
},
}

cmd.Flags().BoolVarP(&follow, "follow", "f", false, "Follow log output")
cmd.Flags().StringVarP(&refreshInterval, "refresh-interval", "n", "", "Interval to refresh logs (default 5s)")

return cmd
}
67 changes: 67 additions & 0 deletions cmd/harbor/root/jobservice/pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package jobservice

import (
"fmt"

"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
view "github.com/goharbor/harbor-cli/pkg/views/jobservice"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func PoolCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "pool",
Short: "Manage worker pools",
}

cmd.AddCommand(ListPoolCommand())

return cmd
}

func ListPoolCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List all the worker pools",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
formatFlag := viper.GetString("output-format")
if formatFlag != "" {
log.Debug("Attempting to list worker pools for formatted output...")
pools, err := api.ListWorkerPools()
if err != nil {
return fmt.Errorf("failed to list worker pools: %v", utils.ParseHarborErrorMsg(err))
}
log.WithField("output_format", formatFlag).Debug("Output format selected")
err = utils.PrintFormat(pools, formatFlag)
if err != nil {
return err
}
} else {
err := view.ListWorkerPoolsAsync()
if err != nil {
return fmt.Errorf("failed to list worker pools: %w", err)
}
}
return nil
},
}
return cmd
}
Loading