mirror of
https://github.com/apricote/hcloud-upload-image.git
synced 2026-01-13 13:21:01 +00:00
refactor: change package names
This commit is contained in:
parent
62578ad5c5
commit
4b77b81689
24 changed files with 167 additions and 155 deletions
39
hcloudimages/internal/control/retry.go
Normal file
39
hcloudimages/internal/control/retry.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package control
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/apricote/hcloud-upload-image/hcloudimages/backoff"
|
||||
"github.com/apricote/hcloud-upload-image/hcloudimages/contextlogger"
|
||||
)
|
||||
|
||||
// From https://github.com/hetznercloud/terraform-provider-hcloud/blob/v1.46.1/internal/control/retry.go
|
||||
// Copyright (c) Hetzner Cloud GmbH
|
||||
|
||||
// Retry executes f at most maxTries times.
|
||||
func Retry(ctx context.Context, maxTries int, f func() error) error {
|
||||
logger := contextlogger.From(ctx)
|
||||
|
||||
var err error
|
||||
|
||||
backoffFunc := backoff.ExponentialBackoffWithLimit(2, 1*time.Second, 30*time.Second)
|
||||
|
||||
for try := 0; try < maxTries; try++ {
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
err = f()
|
||||
if err != nil {
|
||||
sleep := backoffFunc(try)
|
||||
logger.DebugContext(ctx, "operation failed, waiting before trying again", "try", try, "backoff", sleep)
|
||||
time.Sleep(sleep)
|
||||
continue
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
19
hcloudimages/internal/randomid/randomid.go
Normal file
19
hcloudimages/internal/randomid/randomid.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package randomid
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// From https://gitlab.com/hetznercloud/fleeting-plugin-hetzner/-/blob/0f60204582289c243599f8ca0f5be4822789131d/internal/utils/random.go
|
||||
// Copyright (c) 2024 Hetzner Cloud GmbH
|
||||
|
||||
func Generate() (string, error) {
|
||||
b := make([]byte, 4)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to generate random string: %w", err)
|
||||
}
|
||||
return hex.EncodeToString(b), nil
|
||||
}
|
||||
18
hcloudimages/internal/randomid/randomid_test.go
Normal file
18
hcloudimages/internal/randomid/randomid_test.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package randomid
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGenerateRandomID(t *testing.T) {
|
||||
found1, err := Generate()
|
||||
assert.NoError(t, err)
|
||||
found2, err := Generate()
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Len(t, found1, 8)
|
||||
assert.Len(t, found2, 8)
|
||||
assert.NotEqual(t, found1, found2)
|
||||
}
|
||||
48
hcloudimages/internal/sshkey/ssh_key.go
Normal file
48
hcloudimages/internal/sshkey/ssh_key.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package sshkey
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"encoding/pem"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
// From https://gitlab.com/hetznercloud/fleeting-plugin-hetzner/-/blob/0f60204582289c243599f8ca0f5be4822789131d/internal/utils/ssh_key.go
|
||||
// Copyright (c) 2024 Hetzner Cloud GmbH
|
||||
|
||||
func GenerateKeyPair() ([]byte, []byte, error) {
|
||||
pub, priv, err := ed25519.GenerateKey(nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
pubBytes, err := encodePublicKey(pub)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
privBytes, err := encodePrivateKey(priv)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return pubBytes, privBytes, nil
|
||||
}
|
||||
|
||||
func encodePublicKey(pub ed25519.PublicKey) ([]byte, error) {
|
||||
sshPub, err := ssh.NewPublicKey(pub)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ssh.MarshalAuthorizedKey(sshPub), nil
|
||||
}
|
||||
|
||||
func encodePrivateKey(priv ed25519.PrivateKey) ([]byte, error) {
|
||||
privPem, err := ssh.MarshalPrivateKey(priv, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pem.EncodeToMemory(privPem), nil
|
||||
}
|
||||
25
hcloudimages/internal/sshkey/ssh_key_test.go
Normal file
25
hcloudimages/internal/sshkey/ssh_key_test.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package sshkey
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGenerateSSHKeyPair(t *testing.T) {
|
||||
pubBytes, privBytes, err := GenerateKeyPair()
|
||||
assert.Nil(t, err)
|
||||
|
||||
pub := string(pubBytes)
|
||||
priv := string(privBytes)
|
||||
|
||||
if !(strings.HasPrefix(priv, "-----BEGIN OPENSSH PRIVATE KEY-----\n") &&
|
||||
strings.HasSuffix(priv, "-----END OPENSSH PRIVATE KEY-----\n")) {
|
||||
assert.Fail(t, "private key is invalid", priv)
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(pub, "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA") {
|
||||
assert.Fail(t, "public key is invalid", pub)
|
||||
}
|
||||
}
|
||||
12
hcloudimages/internal/sshsession/session.go
Normal file
12
hcloudimages/internal/sshsession/session.go
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
package sshsession
|
||||
|
||||
import "golang.org/x/crypto/ssh"
|
||||
|
||||
func Run(client *ssh.Client, cmd string) ([]byte, error) {
|
||||
sess, err := client.NewSession()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer sess.Close()
|
||||
return sess.CombinedOutput(cmd)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue