-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
contabo
committed
Nov 3, 2023
1 parent
33d9ae5
commit db1f708
Showing
524 changed files
with
2,599 additions
and
62,920 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// reinstallCmd represents the reinstall command | ||
var RescueCmd = &cobra.Command{ | ||
Use: "rescue", | ||
Short: "rescue an existing instance", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmd.Help() | ||
os.Exit(0) | ||
}, | ||
Args: cobra.OnlyValidArgs, | ||
SuggestFor: []string{"rescue"}, | ||
ValidArgs: []string{"instance"}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(RescueCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"contabo.com/cli/cntb/client" | ||
contaboCmd "contabo.com/cli/cntb/cmd" | ||
"contabo.com/cli/cntb/cmd/util" | ||
instancesClient "contabo.com/cli/cntb/openapi" | ||
"contabo.com/cli/cntb/outputFormatter" | ||
uuid "github.com/satori/go.uuid" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// startInstance represents the start instance command | ||
var rescueInstanceCmd = &cobra.Command{ | ||
Use: "instance [instanceId]", | ||
Short: "Rescue a compute instance", | ||
Long: `Rescue a specific compute instance by its id`, | ||
Example: `cntb start instance 12345`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
instancesActionsRescueRequest := *instancesClient.NewInstancesActionsRescueRequestWithDefaults() | ||
content := contaboCmd.OpenStdinOrFile() | ||
switch content { | ||
case nil: | ||
// optional flags | ||
if rescueRootPassword != 0 { | ||
instancesActionsRescueRequest.RootPassword = &rescueRootPassword | ||
} | ||
if rescueSshKeys != nil { | ||
instancesActionsRescueRequest.SshKeys = &rescueSshKeys | ||
} | ||
|
||
if rescueUserData != "" { | ||
instancesActionsRescueRequest.UserData = &rescueUserData | ||
} | ||
|
||
default: | ||
// from file / stdin | ||
var requestFromFile instancesClient.InstancesActionsRescueRequest | ||
err := json.Unmarshal(content, &requestFromFile) | ||
if err != nil { | ||
log.Fatal(fmt.Sprintf("format invalid. Please check your syntax: %v", err)) | ||
} | ||
// merge createTagRequest with one from file to have the defaults there | ||
json.NewDecoder(strings.NewReader(string(content))).Decode(&instancesActionsRescueRequest) | ||
} | ||
|
||
resp, httpResp, err := client.ApiClient().InstanceActionsApi.Rescue(context.Background(), rescueInstanceId). | ||
XRequestId(uuid.NewV4().String()).InstancesActionsRescueRequest(instancesActionsRescueRequest).Execute() | ||
|
||
|
||
util.HandleErrors(err, httpResp, "while rescuing instance") | ||
responseJson, _ := json.Marshal(resp.Data) | ||
|
||
configFormatter := outputFormatter.FormatterConfig{ | ||
Filter: []string{"instanceId", "action"}, | ||
WideFilter: []string{"instanceId", "action"}, | ||
JsonPath: contaboCmd.OutputFormatDetails, | ||
} | ||
|
||
util.HandleResponse(responseJson, configFormatter) | ||
}, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
// contaboCmd.ValidateOutputFormat() | ||
|
||
if len(args) > 1 { | ||
cmd.Help() | ||
log.Fatal("Too many positional arguments.") | ||
} | ||
|
||
if len(args) < 1 { | ||
cmd.Help() | ||
log.Fatal("Please provide an instanceId.") | ||
} | ||
|
||
instanceId64, err := strconv.ParseInt(args[0], 10, 64) | ||
if err != nil { | ||
log.Fatal(fmt.Sprintf("Provided instanceId %v is not valid.", args[0])) | ||
} | ||
rescueInstanceId = instanceId64 | ||
|
||
viper.BindPFlag("rootPassword", cmd.Flags().Lookup("rootPassword")) | ||
rescueRootPassword = viper.GetInt64("rootPassword") | ||
|
||
viper.BindPFlag("sshKeys", cmd.Flags().Lookup("sshKeys")) | ||
for i := range viper.GetIntSlice("sshKeys") { | ||
rescueSshKeys[i] = int64(viper.GetIntSlice("sshKeys")[i]) | ||
} | ||
|
||
viper.BindPFlag("userData", cmd.Flags().Lookup("userData")) | ||
rescueUserData = viper.GetString("userData") | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
contaboCmd.RescueCmd.AddCommand(rescueInstanceCmd) | ||
|
||
rescueInstanceCmd.Flags().Int64Var(&rescueRootPassword, "rootPassword", 0, | ||
`Id of stored password.`) | ||
|
||
rescueInstanceCmd.Flags().Int64SliceVar(&rescueSshKeys, "sshKeys", nil, | ||
`Ids of stored ssh public keys.`) | ||
|
||
rescueInstanceCmd.Flags().StringVar(&rescueUserData, "userData", "", | ||
`Cloud-init script (user data)`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
|
||
uuid "github.com/satori/go.uuid" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"contabo.com/cli/cntb/client" | ||
contaboCmd "contabo.com/cli/cntb/cmd" | ||
"contabo.com/cli/cntb/cmd/util" | ||
"contabo.com/cli/cntb/outputFormatter" | ||
|
||
instancesClient "contabo.com/cli/cntb/openapi" | ||
) | ||
|
||
var instanceResetPassword = &cobra.Command{ | ||
Use: "instance [instanceId]", | ||
Short: "Reset the password or ssh key of your instance", | ||
Long: "Reset the password or ssh key of your instance", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
instanceResetPasswordRequest := *instancesClient. | ||
NewInstancesResetPasswordActionsRequestWithDefaults() | ||
|
||
if resetPasswordRootPassword != 0 { | ||
instanceResetPasswordRequest.RootPassword = &resetPasswordRootPassword | ||
} | ||
|
||
if resetPasswordSshKeys != nil { | ||
instanceResetPasswordRequest.SshKeys = &resetPasswordSshKeys | ||
} | ||
|
||
if resetPasswordUserData != "" { | ||
instanceResetPasswordRequest.UserData = &resetPasswordUserData | ||
} | ||
|
||
resp, httpResp, err := client.ApiClient().InstanceActionsApi. | ||
ResetPasswordAction(context.Background(), resetPasswordInstanceId). | ||
XRequestId(uuid.NewV4().String()). | ||
InstancesResetPasswordActionsRequest(instanceResetPasswordRequest). | ||
Execute() | ||
|
||
util.HandleErrors(err, httpResp, "while resetting instance password") | ||
|
||
responseJson, _ := json.Marshal(resp.Data) | ||
|
||
configFormatter := outputFormatter.FormatterConfig{ | ||
Filter: []string{"instanceId", "action"}, | ||
WideFilter: []string{"instanceId", "action"}, | ||
JsonPath: contaboCmd.OutputFormatDetails, | ||
} | ||
|
||
util.HandleResponse(responseJson, configFormatter) | ||
|
||
}, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) > 1 { | ||
cmd.Help() | ||
log.Fatal("Too many positional arguments.") | ||
} | ||
|
||
if len(args) < 1 { | ||
cmd.Help() | ||
log.Fatal("Please provide an instanceId.") | ||
} | ||
|
||
instanceId64, err := strconv.ParseInt(args[0], 10, 64) | ||
if err != nil { | ||
log.Fatal(fmt.Sprintf("Provided instanceId %v is not valid.", args[0])) | ||
} | ||
resetPasswordInstanceId = instanceId64 | ||
|
||
viper.BindPFlag("rootPassword", cmd.Flags().Lookup("rootPassword")) | ||
resetPasswordRootPassword = viper.GetInt64("rootPassword") | ||
|
||
viper.BindPFlag("sshKeys", cmd.Flags().Lookup("sshKeys")) | ||
for i := range viper.GetIntSlice("sshKeys") { | ||
resetPasswordSshKeys[i] = int64(viper.GetIntSlice("sshKeys")[i]) | ||
} | ||
|
||
viper.BindPFlag("userData", cmd.Flags().Lookup("userData")) | ||
resetPasswordUserData = viper.GetString("userData") | ||
|
||
if resetPasswordRootPassword == 0 && | ||
resetPasswordSshKeys == nil && | ||
resetPasswordUserData == "" { | ||
log.Fatalf("Please provide at least one of --rootPassword, --sshKeys or --userData") | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
contaboCmd.ResetPasswordCmd.AddCommand(instanceResetPassword) | ||
|
||
instanceResetPassword.Flags().Int64Var(&resetPasswordRootPassword, "rootPassword", 0, | ||
`Id of stored password.`) | ||
|
||
instanceResetPassword.Flags().Int64SliceVar(&resetPasswordSshKeys, "sshKeys", nil, | ||
`Ids of stored ssh public keys.`) | ||
|
||
instanceResetPassword.Flags().StringVar(&resetPasswordUserData, "userData", "", | ||
`Cloud-init script (user data)`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.