Skip to content

OTA v2: support for cancel of an in-progess/pending ota #153

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

Merged
merged 2 commits into from
May 17, 2024
Merged
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
64 changes: 64 additions & 0 deletions cli/ota/cancel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// 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.
//
// 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.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package ota

import (
"fmt"
"os"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cloud-cli/command/ota"
"github.com/arduino/arduino-cloud-cli/config"
"github.com/spf13/cobra"
)

type cancelFlags struct {
otaID string
}

func initOtaCancelCommand() *cobra.Command {
flags := &cancelFlags{}
uploadCommand := &cobra.Command{
Use: "cancel",
Short: "OTA cancel",
Long: "Cancel OTA by OTA ID",
Run: func(cmd *cobra.Command, args []string) {
if err := runOtaCancelCommand(flags); err != nil {
feedback.Errorf("Error during ota cancel: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
},
}
uploadCommand.Flags().StringVarP(&flags.otaID, "ota-id", "o", "", "OTA ID")

return uploadCommand
}

func runOtaCancelCommand(flags *cancelFlags) error {
if flags.otaID == "" {
return fmt.Errorf("required flag \"ota-id\" not set")
}

cred, err := config.RetrieveCredentials()
if err != nil {
return fmt.Errorf("retrieving credentials: %w", err)
}

return ota.CancelOta(flags.otaID, cred)
}
1 change: 1 addition & 0 deletions cli/ota/ota.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NewCommand() *cobra.Command {
otaCommand.AddCommand(initOtaStatusCommand())
otaCommand.AddCommand(initEncodeBinaryCommand())
otaCommand.AddCommand(initDecodeHeaderCommand())
otaCommand.AddCommand(initOtaCancelCommand())

return otaCommand
}
4 changes: 2 additions & 2 deletions cli/ota/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func initOtaStatusCommand() *cobra.Command {
Short: "OTA status",
Long: "Get OTA status by OTA or device ID",
Run: func(cmd *cobra.Command, args []string) {
if err := runOtaStatusCommand(flags); err != nil {
if err := runPrintOtaStatusCommand(flags); err != nil {
feedback.Errorf("Error during ota get status: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
Expand All @@ -58,7 +58,7 @@ func initOtaStatusCommand() *cobra.Command {
return uploadCommand
}

func runOtaStatusCommand(flags *statusFlags) error {
func runPrintOtaStatusCommand(flags *statusFlags) error {
if flags.otaID == "" && flags.deviceId == "" && flags.otaIDs == "" {
return fmt.Errorf("required flag(s) \"ota-id\" or \"device-id\" or \"ota-ids\" not set")
}
Expand Down
36 changes: 36 additions & 0 deletions command/ota/cancel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ota

import (
"fmt"

"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cloud-cli/config"
otaapi "github.com/arduino/arduino-cloud-cli/internal/ota-api"
)

func CancelOta(otaid string, cred *config.Credentials) error {

if feedback.GetFormat() == feedback.JSONMini {
return fmt.Errorf("jsonmini format is not supported for this command")
}

otapi := otaapi.NewClient(cred)

if otaid != "" {
_, err := otapi.CancelOta(otaid)
if err != nil {
return err
}
// No error, get current status
res, err := otapi.GetOtaStatusByOtaID(otaid, 1, otaapi.OrderDesc)
if err != nil {
return err
}
if res != nil {
feedback.PrintResult(res.Ota)
}
return nil
}

return nil
}
39 changes: 38 additions & 1 deletion internal/ota-api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
)

var ErrAlreadyInProgress = fmt.Errorf("already in progress")
var ErrAlreadyCancelled = fmt.Errorf("already cancelled")

type OtaApiClient struct {
client *http.Client
Expand All @@ -58,7 +59,11 @@ func NewClient(credentials *config.Credentials) *OtaApiClient {
}

func (c *OtaApiClient) performGetRequest(endpoint, token string) (*http.Response, error) {
req, err := http.NewRequest("GET", endpoint, nil)
return c.performRequest(endpoint, "GET", token)
}

func (c *OtaApiClient) performRequest(endpoint, method, token string) (*http.Response, error) {
req, err := http.NewRequest(method, endpoint, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -205,3 +210,35 @@ func (c *OtaApiClient) GetOtaStatusByDeviceID(deviceID string, limit int, order

return nil, err
}

func (c *OtaApiClient) CancelOta(otaid string) (bool, error) {

if otaid == "" {
return false, fmt.Errorf("invalid ota-id: empty")
}

userRequestToken, err := c.src.Token()
if err != nil {
if strings.Contains(err.Error(), "401") {
return false, errors.New("wrong credentials")
}
return false, fmt.Errorf("cannot retrieve a valid token: %w", err)
}

endpoint := c.host + "/ota/v1/ota/" + otaid + "/cancel"
res, err := c.performRequest(endpoint, "PUT", userRequestToken.AccessToken)
if err != nil {
return false, err
}
defer res.Body.Close()

if res.StatusCode == 200 {
return true, nil
} else if res.StatusCode == 404 || res.StatusCode == 400 {
return false, fmt.Errorf("ota-id %s not found", otaid)
} else if res.StatusCode == 409 {
return false, ErrAlreadyCancelled
}

return false, err
}
Loading