Skip to content

Commit

Permalink
fix(driver): enable metadata when required by base image (#44)
Browse files Browse the repository at this point in the history
- fix(driver): enable metadata when required by base image
- chore(lib): update upcloud-go-api to v5.1.0
- chore(lib): drop deprecated io/ioutil package
  • Loading branch information
peknur authored Dec 15, 2022
1 parent b2a539a commit 9e11c8e
Show file tree
Hide file tree
Showing 31 changed files with 274 additions and 359 deletions.
44 changes: 22 additions & 22 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@ name: Documentation
on:
pull_request:
types:
- closed
- closed
branches:
- main
- test-docs-generator # for testing
- main
- test-docs-generator # for testing

jobs:
update:
name: Update
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: "1.17"
- name: Checkout
uses: actions/checkout@v2
- name: Generate documentation
run: make generate
- name: Create PR for docs update
uses: peter-evans/create-pull-request@v4
with:
add-paths: docs/
branch: chore/update-docs
commit-message: "chore(docs): update documentation for #${{ github.event.number }}"
committer: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
reviewers: ${{ github.actor }}
title: Update documentation
body: "This is an automatically created PR. Changes were created by running `make docs` after merging #${{ github.event.number }} (${{ github.sha }})."
base: ${{ github.event.pull_request.base.ref }}
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: "1.18"
- name: Checkout
uses: actions/checkout@v3
- name: Generate documentation
run: make generate
- name: Create PR for docs update
uses: peter-evans/create-pull-request@v4
with:
add-paths: docs/
branch: chore/update-docs
commit-message: "chore(docs): update documentation for #${{ github.event.number }}"
committer: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
reviewers: ${{ github.actor }}
title: Update documentation
body: "This is an automatically created PR. Changes were created by running `make docs` after merging #${{ github.event.number }} (${{ github.sha }})."
base: ${{ github.event.pull_request.base.ref }}
15 changes: 15 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Lint
on:
pull_request:
jobs:
golangci-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.18
- uses: actions/checkout@v3
- uses: golangci/golangci-lint-action@v3
with:
version: latest
args: --timeout 10m
8 changes: 4 additions & 4 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ name: release
on:
push:
tags:
- 'v*'
- "v*"
jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Unshallow
run: git fetch --prune --unshallow

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version: 1.17
go-version: 1.18

- name: Describe plugin
id: plugin_describe
Expand Down
52 changes: 24 additions & 28 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,37 @@ on: [push]

jobs:
test:

strategy:
matrix:
go-version: [1.17.x]
go-version: [1.18.x]
os: [ubuntu-latest, macos-latest, windows-latest]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v2

- name: Setup
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}

- name: Dependencies
run: |
go version
go get -u golang.org/x/lint/golint
- name: Build
run: make build

- name: Code Style
run: make lint

- name: Test PR
if: github.ref != 'refs/heads/main'
run: make test

- name: Test main
if: github.ref == 'refs/heads/main'
env:
- uses: actions/checkout@v3

- name: Setup
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}

- name: Dependencies
run: |
go version
go get -u golang.org/x/lint/golint
- name: Build
run: make build

- name: Test PR
if: github.ref != 'refs/heads/main'
run: make test

- name: Test main
if: github.ref == 'refs/heads/main'
env:
UPCLOUD_API_USER: ${{ secrets.UPCLOUD_API_USER }}
UPCLOUD_API_PASSWORD: ${{ secrets.UPCLOUD_API_PASSWORD }}
PACKER_ACC: 1
run: make test_integration
run: make test_integration
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ vendor
docs.zip
crash.log
*hcl.txt
tmp
/go.work
/go.work.sum
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)

## [Unreleased]

### Fixed
- enable server metadata automatically when required by base image

### Changed
- update `packer-plugin-sdk` to [v0.3.0](https://github.com/hashicorp/packer-plugin-sdk/blob/main/CHANGELOG.md#030-june-09-2022)
- update upcloud-go-api to v5.1.0

## [1.5.1] - 2022-06-10

Expand Down
6 changes: 4 additions & 2 deletions builder/upcloud/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/UpCloudLtd/packer-plugin-upcloud/internal/driver"
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v5/upcloud"
"github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
)

Expand Down Expand Up @@ -54,8 +54,10 @@ func (a *Artifact) State(name string) interface{} {
}

func (a *Artifact) Destroy() error {
ctx, cancel := contextWithDefaultTimeout()
defer cancel()
for _, t := range a.Templates {
err := a.driver.DeleteTemplate(t.UUID)
err := a.driver.DeleteTemplate(ctx, t.UUID)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion builder/upcloud/artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v5/upcloud"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
"github.com/stretchr/testify/assert"
Expand Down
10 changes: 8 additions & 2 deletions builder/upcloud/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package upcloud
import (
"context"
"fmt"
"time"

"github.com/UpCloudLtd/packer-plugin-upcloud/internal/driver"
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v5/upcloud"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/hashicorp/packer-plugin-sdk/multistep"
Expand All @@ -14,7 +15,10 @@ import (
"github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
)

const BuilderId = "upcloud.builder"
const (
BuilderId = "upcloud.builder"
defaultTimeout time.Duration = 1 * time.Hour
)

type Builder struct {
config Config
Expand Down Expand Up @@ -42,6 +46,8 @@ func (b *Builder) Prepare(raws ...interface{}) (generatedVars []string, warnings
}

func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
// NOTE: context deadline is not set by default.

// Setup the state bag and initial state for the steps
b.driver = driver.NewDriver(&driver.DriverConfig{
Username: b.config.Username,
Expand Down
10 changes: 5 additions & 5 deletions builder/upcloud/builder_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
_ "embed"
"errors"
"fmt"
"io/ioutil"
"io"
"os"
"os/exec"
"regexp"
Expand Down Expand Up @@ -161,7 +161,7 @@ func readLog(logfile string) (string, error) {
}
defer logs.Close()

logsBytes, err := ioutil.ReadAll(logs)
logsBytes, err := io.ReadAll(logs)
if err != nil {
return "", fmt.Errorf("Unable to read %s", logfile)
}
Expand Down Expand Up @@ -214,9 +214,9 @@ func getUuidsFromLog(log string) ([]string, error) {

func teardown(testName string) func() error {
logfile := fmt.Sprintf("packer_log_%s.txt", testName)

return func() error {

ctx, cancel := contextWithDefaultTimeout()
defer cancel()
log, err := readLog(logfile)
if err != nil {
return err
Expand All @@ -235,7 +235,7 @@ func teardown(testName string) func() error {

for _, u := range uuids {
fmt.Printf("Cleaning up created templates: %s\n", u)
if err := drv.DeleteTemplate(u); err != nil {
if err := drv.DeleteTemplate(ctx, u); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions builder/upcloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"time"

"github.com/UpCloudLtd/packer-plugin-upcloud/internal/driver"
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
"github.com/UpCloudLtd/upcloud-go-api/v5/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v5/upcloud/request"
"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down
12 changes: 7 additions & 5 deletions builder/upcloud/step_create_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type StepCreateServer struct {
}

// Run runs the actual step
func (s *StepCreateServer) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
func (s *StepCreateServer) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
drv := state.Get("driver").(driver.Driver)

Expand All @@ -30,7 +30,7 @@ func (s *StepCreateServer) Run(_ context.Context, state multistep.StateBag) mult

ui.Say("Getting storage...")

storage, err := drv.GetStorage(s.Config.StorageUUID, s.Config.StorageName)
storage, err := drv.GetStorage(ctx, s.Config.StorageUUID, s.Config.StorageName)
if err != nil {
return stepHaltWithError(state, err)
}
Expand All @@ -42,7 +42,7 @@ func (s *StepCreateServer) Run(_ context.Context, state multistep.StateBag) mult
networking = convertNetworkTypes(s.Config.NetworkInterfaces)

}
response, err := drv.CreateServer(&driver.ServerOpts{
response, err := drv.CreateServer(ctx, &driver.ServerOpts{
StorageUuid: storage.UUID,
StorageSize: s.Config.StorageSize,
Zone: s.Config.Zone,
Expand Down Expand Up @@ -90,6 +90,8 @@ func (s *StepCreateServer) Run(_ context.Context, state multistep.StateBag) mult

// Cleanup stops and destroys the server if server details are found in the state
func (s *StepCreateServer) Cleanup(state multistep.StateBag) {
ctx, cancel := contextWithDefaultTimeout()
defer cancel()
// Extract server uuid, return if no uuid has been stored
rawServerUuid, ok := state.GetOk("server_uuid")

Expand All @@ -106,7 +108,7 @@ func (s *StepCreateServer) Cleanup(state multistep.StateBag) {
// stop server
ui.Say(fmt.Sprintf("Stopping server %q...", serverTitle))

err := driver.StopServer(serverUuid)
err := driver.StopServer(ctx, serverUuid)
if err != nil {
ui.Error(err.Error())
return
Expand All @@ -115,7 +117,7 @@ func (s *StepCreateServer) Cleanup(state multistep.StateBag) {
// delete server
ui.Say(fmt.Sprintf("Deleting server %q...", serverTitle))

err = driver.DeleteServer(serverUuid)
err = driver.DeleteServer(ctx, serverUuid)
if err != nil {
ui.Error(err.Error())
return
Expand Down
8 changes: 4 additions & 4 deletions builder/upcloud/step_create_ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/hashicorp/packer-plugin-sdk/multistep"
Expand All @@ -30,11 +30,11 @@ func (s *StepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) mult
var err error
ui.Say("Using provided SSH keys...")

if config.Comm.SSHPrivateKey, err = ioutil.ReadFile(config.SSHPrivateKeyPath); err != nil {
if config.Comm.SSHPrivateKey, err = os.ReadFile(config.SSHPrivateKeyPath); err != nil {
return stepHaltWithError(state, fmt.Errorf("Failed to read private key: %s", err))
}

if config.Comm.SSHPublicKey, err = ioutil.ReadFile(config.SSHPublicKeyPath); err != nil {
if config.Comm.SSHPublicKey, err = os.ReadFile(config.SSHPublicKeyPath); err != nil {
return stepHaltWithError(state, fmt.Errorf("Failed to read public key: %s", err))
}

Expand Down Expand Up @@ -74,7 +74,7 @@ func (s *StepCreateSSHKey) Run(_ context.Context, state multistep.StateBag) mult
// If we're in debug mode, output the private key to the working directory.
if s.Debug {
ui.Say(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
err := ioutil.WriteFile(s.DebugKeyPath, config.Comm.SSHPrivateKey, 0600)
err := os.WriteFile(s.DebugKeyPath, config.Comm.SSHPrivateKey, 0600)
if err != nil {
return stepHaltWithError(state, fmt.Errorf("Error saving debug key: %s", err))
}
Expand Down
Loading

0 comments on commit 9e11c8e

Please sign in to comment.