Skip to content

Commit

Permalink
Merge pull request #39 from yamamoto-febc/feature/hcl
Browse files Browse the repository at this point in the history
packer v1.5.4: HCL対応
  • Loading branch information
yamamoto-febc authored Mar 10, 2020
2 parents eeca022 + 561536b commit 2bcdc9a
Show file tree
Hide file tree
Showing 1,531 changed files with 381,219 additions and 53,855 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ tools:
GO111MODULE=off go get github.com/tcnksm/ghr
GO111MODULE=off go get github.com/client9/misspell/cmd/misspell
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/v1.23.8/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.23.8
go install github.com/hashicorp/packer/cmd/mapstructure-to-hcl2

.PHONY: clean
clean:
Expand All @@ -20,12 +21,16 @@ clean:
install: build
cp -f $(CURDIR)/bin/packer-builder-sakuracloud $(GOPATH)/bin/packer-builder-sakuracloud

build: clean
build: generate clean
go build -mod vendor -ldflags "-s -w" -o $(CURDIR)/bin/packer-builder-sakuracloud $(CURDIR)/main.go

build-x: clean vet
sh -c "'$(CURDIR)/scripts/build.sh'"

generate:
go generate ./...


.PHONY: test testacc
test: vet
go test $(TEST) $(TESTARGS) -v -timeout=30m -parallel=4 ;
Expand Down
28 changes: 28 additions & 0 deletions examples/centos7/template.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
source "sakuracloud" "example" {
zone = "is1b"

os_type = "centos7"
password = "TestUserPassword01"
disk_size = 20
disk_plan = "ssd"

core = 2
memory_size = 4

archive_name = "packer-example-centos"
archive_description = "description of archive"
}

build {
sources = [
"source.sakuracloud.example"
]
provisioner "shell" {
inline = [
"yum update -y",
"curl -fsSL https://get.docker.com/ | sh",
"systemctl enable docker.service",
]
}
}

32 changes: 32 additions & 0 deletions examples/ubuntu/template.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
locals {
password = "TestUserPassword01"
}


source "sakuracloud" "example" {
zone = "is1b"

os_type = "ubuntu"
user_name = "ubuntu"
password = local.password

core = 2
memory_size = 4

archive_name = "packer-example-ubuntu"
archive_description = "description of archive"
}

build {
sources = [
"source.sakuracloud.example"
]
provisioner "shell" {
execute_command = "echo '${local.password}' | {{ .Vars }} sudo -E -S sh '{{ .Path }}'"
inline = [
"apt-get update -y",
"apt-get install -y curl"
]
}
}

37 changes: 7 additions & 30 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
module github.com/sacloud/packer-builder-sakuracloud

require (
github.com/Azure/go-ntlmssp v0.0.0-20180810175552-4a21cbd618b4 // indirect
github.com/ChrisTrenkamp/goxpath v0.0.0-20170625215350-4fe035839290 // indirect
github.com/cheggaaa/pb v1.0.26 // indirect
github.com/dylanmei/iso8601 v0.1.0 // indirect
github.com/dylanmei/winrmtest v0.0.0-20190225150635-99b7fe2fddf1 // indirect
github.com/hashicorp/consul v0.0.0-20181018204313-a06b825a2814 // indirect
github.com/hashicorp/go-checkpoint v0.0.0-20171009173528-1545e56e46de // indirect
github.com/hashicorp/go-immutable-radix v1.1.0 // indirect
github.com/hashicorp/go-msgpack v0.5.5 // indirect
github.com/hashicorp/memberlist v0.1.4 // indirect
github.com/hashicorp/packer v1.3.2
github.com/hashicorp/serf v0.0.0-20180907130240-48d579458173 // indirect
github.com/hashicorp/vault/api v1.0.4 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/masterzen/azure-sdk-for-go v0.0.0-20161014135628-ee4f0065d00c // indirect
github.com/masterzen/simplexml v0.0.0-20160608183007-4572e39b1ab9 // indirect
github.com/masterzen/winrm v0.0.0-20180224160350-7e40f93ae939 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-runewidth v0.0.3 // indirect
github.com/mitchellh/go-fs v0.0.0-20180402235330-b7b9ca407fff // indirect
github.com/hashicorp/hcl/v2 v2.3.0
github.com/hashicorp/packer v1.5.4
github.com/mitchellh/go-vnc v0.0.0-20150629162542-723ed9867aed
github.com/mitchellh/iochan v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.1.2
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/packer-community/winrmcp v0.0.0-20180102160824-81144009af58 // indirect
github.com/pkg/errors v0.8.0 // indirect
github.com/pkg/sftp v0.0.0-20160930220758-4d0e916071f6 // indirect
github.com/sacloud/ftps v0.0.0-20171205062625-42fc0f9886fe
github.com/sacloud/libsacloud/v2 v2.1.8
github.com/stretchr/testify v1.3.0
github.com/ugorji/go v0.0.0-20151218193438-646ae4a518c1 // indirect
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3 // indirect
gopkg.in/cheggaaa/pb.v1 v1.0.28 // indirect
github.com/stretchr/testify v1.4.0
github.com/zclconf/go-cty v1.2.1
golang.org/x/crypto v0.0.0-20200117160349-530e935923ad
)

replace github.com/zclconf/go-cty => github.com/azr/go-cty v1.1.1-0.20200203143058-28fcda2fe0cc

go 1.13
580 changes: 500 additions & 80 deletions go.sum

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ func main() {
if err != nil {
panic(err)
}
server.RegisterBuilder(new(sakuracloud.Builder))
if err := server.RegisterBuilder(new(sakuracloud.Builder)); err != nil {
panic(err)
}
server.Serve()
}
25 changes: 11 additions & 14 deletions sakuracloud/builder.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package sakuracloud

import (
"context"
"fmt"
"log"

"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
Expand All @@ -25,28 +27,24 @@ type Builder struct {
runner multistep.Runner
}

func (b *Builder) ConfigSpec() hcldec.ObjectSpec {
return b.config.FlatMapstructure().HCL2Spec()
}

// Prepare is responsible for configuring the builder and validating
// that configuration.
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
c, warnings, errs := NewConfig(raws...)
if errs != nil {
return warnings, errs
return nil, warnings, errs
}
b.config = *c

return nil, nil
}

// Cancel cancels a possibly running Builder.
func (b *Builder) Cancel() {
if b.runner != nil {
log.Println("Cancelling the step runner...")
b.runner.Cancel()
}
return nil, nil, nil
}

// Run is where the actual build should take place.
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
client := iaas.NewClient(b.config.AccessToken, b.config.AccessTokenSecret, b.config.Zone)

// Set up the state
Expand All @@ -62,7 +60,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe

state.Put("hook", hook)
state.Put("ui", ui)
state.Put("cache", cache)

// Build the steps
var steps []multistep.Step
Expand Down Expand Up @@ -163,7 +160,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
b.runner = &multistep.BasicRunner{Steps: steps}
}

b.runner.Run(state)
b.runner.Run(ctx, state)

// If there was an error, return that
if rawErr, ok := state.GetOk("error"); ok {
Expand Down
20 changes: 1 addition & 19 deletions sakuracloud/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"os"
"testing"

"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/stretchr/testify/assert"
)
Expand All @@ -28,28 +27,11 @@ func TestBuilder_Prepare(t *testing.T) {

t.Run("with minimum config", func(t *testing.T) {
builder := &Builder{}
warns, errs := builder.Prepare(testMinimumConfigValues)
_, warns, errs := builder.Prepare(testMinimumConfigValues)

assert.Nil(t, warns)
assert.Nil(t, errs)
})

// TODO add more unit tests after refactoring Builder/Config
}

type dummyBuildRunner struct {
cancelCalled bool
}

func (t *dummyBuildRunner) Run(bag multistep.StateBag) {}
func (t *dummyBuildRunner) Cancel() { t.cancelCalled = true }

func TestBuilder_Cancel(t *testing.T) {
runner := &dummyBuildRunner{}
builder := &Builder{
runner: runner,
}

builder.Cancel()
assert.True(t, runner.cancelCalled)
}
2 changes: 2 additions & 0 deletions sakuracloud/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:generate mapstructure-to-hcl2 -type Config

package sakuracloud

import (
Expand Down
Loading

0 comments on commit 2bcdc9a

Please sign in to comment.