diff --git a/.goreleaser.yml b/.goreleaser.yml index ae0df00..261e87a 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -7,12 +7,15 @@ before: builds: - env: - CGO_ENABLED=0 + # https://github.com/italia/publiccode-parser-go/issues/50 + ignore: + - goos: darwin + goarch: 386 archives: - replacements: darwin: Darwin linux: Linux windows: Windows - 386: i386 amd64: x86_64 checksum: name_template: 'checksums.txt' diff --git a/go.mod b/go.mod index d83bafb..9ffec39 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,6 @@ require ( github.com/hashicorp/hcl/v2 v2.8.0 github.com/hashicorp/packer v1.6.7-0.20210217093213-201869d627bf github.com/hashicorp/packer-plugin-sdk v0.0.14 - github.com/oracle/oci-go-sdk v19.0.0+incompatible + github.com/oracle/oci-go-sdk/v36 v36.2.0 github.com/zclconf/go-cty v1.7.0 ) diff --git a/go.sum b/go.sum index 59ae2e6..041fb05 100644 --- a/go.sum +++ b/go.sum @@ -447,9 +447,12 @@ github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= github.com/olekukonko/tablewriter v0.0.0-20180105111133-96aac992fc8b/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= +github.com/oracle/oci-go-sdk v18.0.0+incompatible h1:FLV4KixsVfF3rwyVTMI6Ryp/Q+OSb9sR5TawbfjFLN4= github.com/oracle/oci-go-sdk v18.0.0+incompatible/go.mod h1:VQb79nF8Z2cwLkLS35ukwStZIg5F66tcBccjip/j888= github.com/oracle/oci-go-sdk v19.0.0+incompatible h1:UQnOTZBLEtrqNnJ7jQJiaadScBQ7+CF61+OIvWktkoc= github.com/oracle/oci-go-sdk v19.0.0+incompatible/go.mod h1:VQb79nF8Z2cwLkLS35ukwStZIg5F66tcBccjip/j888= +github.com/oracle/oci-go-sdk/v36 v36.2.0 h1:oBaN/FnBDy3ohMyVZ/rKfekYxnyksG2KK0YAhT5HSnk= +github.com/oracle/oci-go-sdk/v36 v36.2.0/go.mod h1:t8Y/M3Lh8X4BOJhtThJKe1skRTg7qom7oWyHiNjo4RM= github.com/outscale/osc-sdk-go/osc v0.0.0-20200722135656-d654809d0699/go.mod h1:5AqqNH1X8zCHescKVlpSHRzrat1KCKDXqZoQPe8fY3A= github.com/packer-community/winrmcp v0.0.0-20180921204643-0fd363d6159a h1:A3QMuteviunoaY/8ex+RKFqwhcZJ/Cf3fCW3IwL2wx4= github.com/packer-community/winrmcp v0.0.0-20180921204643-0fd363d6159a/go.mod h1:f6Izs6JvFTdnRbziASagjZ2vmf55NSIkC/weStxCHqk= diff --git a/pkg/ocisurrogate/artifact.go b/pkg/ocisurrogate/artifact.go index e6ae29d..62c0e5e 100644 --- a/pkg/ocisurrogate/artifact.go +++ b/pkg/ocisurrogate/artifact.go @@ -4,7 +4,7 @@ import ( "context" "fmt" - "github.com/oracle/oci-go-sdk/core" + "github.com/oracle/oci-go-sdk/v36/core" ) // Artifact is an artifact implementation that contains a built Custom Image. diff --git a/pkg/ocisurrogate/builder.go b/pkg/ocisurrogate/builder.go index a44f169..528c651 100644 --- a/pkg/ocisurrogate/builder.go +++ b/pkg/ocisurrogate/builder.go @@ -12,7 +12,7 @@ import ( commonsteps "github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps" "github.com/hashicorp/packer-plugin-sdk/packer" ocommon "github.com/hashicorp/packer/builder/oracle/common" - "github.com/oracle/oci-go-sdk/core" + "github.com/oracle/oci-go-sdk/v36/core" ) // BuilderId uniquely identifies the builder diff --git a/pkg/ocisurrogate/config.go b/pkg/ocisurrogate/config.go index 600b138..37cfbfd 100644 --- a/pkg/ocisurrogate/config.go +++ b/pkg/ocisurrogate/config.go @@ -19,10 +19,15 @@ import ( "github.com/hashicorp/packer-plugin-sdk/pathing" "github.com/hashicorp/packer-plugin-sdk/template/config" "github.com/hashicorp/packer-plugin-sdk/template/interpolate" - ocicommon "github.com/oracle/oci-go-sdk/common" - ociauth "github.com/oracle/oci-go-sdk/common/auth" + ocicommon "github.com/oracle/oci-go-sdk/v36/common" + ociauth "github.com/oracle/oci-go-sdk/v36/common/auth" ) +type FlexShapeConfig struct { + Ocpus *float32 `mapstructure:"ocpus" required:"false"` + MemoryInGBs *float32 `mapstructure:"memory_in_gbs" required:"false"` +} + type Config struct { common.PackerConfig `mapstructure:",squash"` Comm communicator.Config `mapstructure:",squash"` @@ -59,11 +64,13 @@ type Config struct { // Image BaseImageID string `mapstructure:"base_image_ocid"` BaseImageName string `mapstructure:"base_image_name"` - Shape string `mapstructure:"shape"` ImageName string `mapstructure:"image_name"` - BootVolumeSizeInGBs int64 `mapstructure:"bootvolumesize"` + // Instance InstanceName string `mapstructure:"instance_name"` + Shape string `mapstructure:"shape"` + ShapeConfig FlexShapeConfig `mapstructure:"shape_config"` + BootVolumeSizeInGBs int64 `mapstructure:"bootvolumesize"` // Metadata optionally contains custom metadata key/value pairs provided in the // configuration. While this can be used to set metadata["user_data"] the explicit @@ -194,7 +201,7 @@ func (c *Config) Prepare(raws ...interface{}) error { } providers := []ocicommon.ConfigurationProvider{ - NewRawConfigurationProvider(c.TenancyID, c.UserID, c.Region, c.Fingerprint, string(keyContent), &c.PassPhrase), + ocicommon.NewRawConfigurationProvider(c.TenancyID, c.UserID, c.Region, c.Fingerprint, string(keyContent), &c.PassPhrase), } if fileProvider != nil { @@ -245,6 +252,18 @@ func (c *Config) Prepare(raws ...interface{}) error { errs, errors.New("'shape' must be specified")) } + if strings.HasSuffix(c.Shape, "Flex") { + if c.ShapeConfig.Ocpus == nil { + errs = packer.MultiErrorAppend( + errs, errors.New("'Ocpus' must be specified when using flexible shapes")) + } + } + + if c.ShapeConfig.MemoryInGBs != nil && c.ShapeConfig.Ocpus == nil { + errs = packer.MultiErrorAppend( + errs, errors.New("'Ocpus' must be specified if memory_in_gbs is specified")) + } + if c.SubnetID == "" { errs = packer.MultiErrorAppend( errs, errors.New("'subnet_ocid' must be specified")) diff --git a/pkg/ocisurrogate/config.hcl2spec.go b/pkg/ocisurrogate/config.hcl2spec.go index 7e2fec9..5fd39c7 100644 --- a/pkg/ocisurrogate/config.hcl2spec.go +++ b/pkg/ocisurrogate/config.hcl2spec.go @@ -71,6 +71,7 @@ type FlatConfig struct { BaseImageID *string `mapstructure:"base_image_ocid" cty:"base_image_ocid"` BaseImageName *string `mapstructure:"base_image_name" cty:"base_image_name"` Shape *string `mapstructure:"shape" cty:"shape"` + ShapeConfig *FlatFlexShapeConfig `mapstructure:"shape_config" cty:"shape_config" hcl:"shape_config"` ImageName *string `mapstructure:"image_name" cty:"image_name"` BootVolumeSizeInGBs *int64 `mapstructure:"bootvolumesize" cty:"bootvolumesize"` InstanceName *string `mapstructure:"instance_name" cty:"instance_name"` @@ -156,6 +157,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "base_image_ocid": &hcldec.AttrSpec{Name: "base_image_ocid", Type: cty.String, Required: false}, "base_image_name": &hcldec.AttrSpec{Name: "base_image_name", Type: cty.String, Required: false}, "shape": &hcldec.AttrSpec{Name: "shape", Type: cty.String, Required: false}, + "shape_config": &hcldec.BlockSpec{TypeName: "shape_config", Nested: hcldec.ObjectSpec((*FlatFlexShapeConfig)(nil).HCL2Spec())}, "image_name": &hcldec.AttrSpec{Name: "image_name", Type: cty.String, Required: false}, "bootvolumesize": &hcldec.AttrSpec{Name: "bootvolumesize", Type: cty.Number, Required: false}, "instance_name": &hcldec.AttrSpec{Name: "instance_name", Type: cty.String, Required: false}, @@ -167,3 +169,29 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { } return s } + + +// FlatFlexShapeConfig is an auto-generated flat version of FlexShapeConfig. +// Where the contents of a field with a `mapstructure:,squash` tag are bubbled up. +type FlatFlexShapeConfig struct { + Ocpus *float32 `mapstructure:"ocpus" required:"false" cty:"ocpus" hcl:"ocpus"` + MemoryInGBs *float32 `mapstructure:"memory_in_gbs" required:"false" cty:"memory_in_gbs" hcl:"memory_in_gbs"` +} + +// FlatMapstructure returns a new FlatFlexShapeConfig. +// FlatFlexShapeConfig is an auto-generated flat version of FlexShapeConfig. +// Where the contents a fields with a `mapstructure:,squash` tag are bubbled up. +func (*FlexShapeConfig) FlatMapstructure() interface{ HCL2Spec() map[string]hcldec.Spec } { + return new(FlatFlexShapeConfig) +} + +// HCL2Spec returns the hcl spec of a FlexShapeConfig. +// This spec is used by HCL to read the fields of FlexShapeConfig. +// The decoded values from this spec will then be applied to a FlatFlexShapeConfig. +func (*FlatFlexShapeConfig) HCL2Spec() map[string]hcldec.Spec { + s := map[string]hcldec.Spec{ + "ocpus": &hcldec.AttrSpec{Name: "ocpus", Type: cty.Number, Required: false}, + "memory_in_gbs": &hcldec.AttrSpec{Name: "memory_in_gbs", Type: cty.Number, Required: false}, + } + return s +} diff --git a/pkg/ocisurrogate/config_provider.go b/pkg/ocisurrogate/config_provider.go deleted file mode 100644 index 635bd44..0000000 --- a/pkg/ocisurrogate/config_provider.go +++ /dev/null @@ -1,76 +0,0 @@ -package ocisurrogate - -import ( - "crypto/rsa" - "errors" - "fmt" - - "github.com/oracle/oci-go-sdk/common" -) - -// rawConfigurationProvider allows a user to simply construct a configuration -// provider from raw values. It errors on access when those values are empty. -type rawConfigurationProvider struct { - tenancy string - user string - region string - fingerprint string - privateKey string - privateKeyPassphrase *string -} - -// NewRawConfigurationProvider will create a rawConfigurationProvider. -func NewRawConfigurationProvider(tenancy, user, region, fingerprint, privateKey string, privateKeyPassphrase *string) common.ConfigurationProvider { - return rawConfigurationProvider{tenancy, user, region, fingerprint, privateKey, privateKeyPassphrase} -} - -func (p rawConfigurationProvider) PrivateRSAKey() (key *rsa.PrivateKey, err error) { - return common.PrivateKeyFromBytes([]byte(p.privateKey), p.privateKeyPassphrase) -} - -func (p rawConfigurationProvider) KeyID() (keyID string, err error) { - tenancy, err := p.TenancyOCID() - if err != nil { - return - } - - user, err := p.UserOCID() - if err != nil { - return - } - - fingerprint, err := p.KeyFingerprint() - if err != nil { - return - } - - return fmt.Sprintf("%s/%s/%s", tenancy, user, fingerprint), nil -} - -func (p rawConfigurationProvider) TenancyOCID() (string, error) { - if p.tenancy == "" { - return "", errors.New("no tenancy provided") - } - return p.tenancy, nil -} - -func (p rawConfigurationProvider) UserOCID() (string, error) { - if p.user == "" { - return "", errors.New("no user provided") - } - return p.user, nil -} - -func (p rawConfigurationProvider) KeyFingerprint() (string, error) { - if p.fingerprint == "" { - return "", errors.New("no fingerprint provided") - } - return p.fingerprint, nil -} - -func (p rawConfigurationProvider) Region() (string, error) { - if p.region == "" { - return "", errors.New("no region provided") - } - return p.region, nil -} diff --git a/pkg/ocisurrogate/driver.go b/pkg/ocisurrogate/driver.go index 636fec0..fcad871 100644 --- a/pkg/ocisurrogate/driver.go +++ b/pkg/ocisurrogate/driver.go @@ -3,7 +3,7 @@ package ocisurrogate import ( "context" - "github.com/oracle/oci-go-sdk/core" + "github.com/oracle/oci-go-sdk/v36/core" ) // Driver interfaces between the builder steps and the OCI SDK. diff --git a/pkg/ocisurrogate/driver_mock.go b/pkg/ocisurrogate/driver_mock.go index fe7213a..47fe0df 100644 --- a/pkg/ocisurrogate/driver_mock.go +++ b/pkg/ocisurrogate/driver_mock.go @@ -3,7 +3,7 @@ package ocisurrogate import ( "context" - "github.com/oracle/oci-go-sdk/core" + "github.com/oracle/oci-go-sdk/v36/core" ) // driverMock implements the Driver interface and communicates with Oracle diff --git a/pkg/ocisurrogate/driver_oci.go b/pkg/ocisurrogate/driver_oci.go index dd60841..e43f873 100644 --- a/pkg/ocisurrogate/driver_oci.go +++ b/pkg/ocisurrogate/driver_oci.go @@ -7,7 +7,7 @@ import ( "log" "time" - core "github.com/oracle/oci-go-sdk/core" + core "github.com/oracle/oci-go-sdk/v36/core" ) // driverOCI implements the Driver interface and communicates with Oracle @@ -94,6 +94,14 @@ func (d *driverOCI) CreateInstance(ctx context.Context, publicKey string, surrog instanceDetails.DisplayName = &d.cfg.InstanceName } + if d.cfg.ShapeConfig.Ocpus != nil { + LaunchInstanceShapeConfigDetails := core.LaunchInstanceShapeConfigDetails{ + Ocpus: d.cfg.ShapeConfig.Ocpus, + MemoryInGBs: d.cfg.ShapeConfig.MemoryInGBs, + } + instanceDetails.ShapeConfig = &LaunchInstanceShapeConfigDetails + } + instance, err := d.computeClient.LaunchInstance(context.TODO(), core.LaunchInstanceRequest{LaunchInstanceDetails: instanceDetails}) if err != nil {