Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Packer
Commits
b844b7f1
Commit
b844b7f1
authored
4 years ago
by
Marin Salinas
Browse files
Options
Download
Email Patches
Plain Diff
refactor: change bsusurrogate builder to new SDK
parent
007f6cce
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
builder/osc/bsusurrogate/builder.go
+9
-5
builder/osc/bsusurrogate/builder.go
builder/osc/bsusurrogate/builder_acc_test.go
+1
-1
builder/osc/bsusurrogate/builder_acc_test.go
builder/osc/bsusurrogate/step_register_omi.go
+36
-24
builder/osc/bsusurrogate/step_register_omi.go
builder/osc/bsusurrogate/step_snapshop_volumes.go
+18
-13
builder/osc/bsusurrogate/step_snapshop_volumes.go
with
64 additions
and
43 deletions
+64
-43
builder/osc/bsusurrogate/builder.go
+
9
-
5
View file @
b844b7f1
...
...
@@ -117,18 +117,21 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
oapiconn
:=
oapi
.
NewClient
(
clientConfig
,
skipClient
)
oscConn
:=
b
.
config
.
NewOSCClient
()
// Setup the state bag and initial state for the steps
state
:=
new
(
multistep
.
BasicStateBag
)
state
.
Put
(
"config"
,
&
b
.
config
)
state
.
Put
(
"oapi"
,
oapiconn
)
state
.
Put
(
"clientConfig"
,
clientConfig
)
state
.
Put
(
"osc"
,
oscConn
)
state
.
Put
(
"accessConfig"
,
&
b
.
config
.
AccessConfig
)
state
.
Put
(
"hook"
,
hook
)
state
.
Put
(
"ui"
,
ui
)
//VMStep
omiDevices
:=
b
.
config
.
BuildOMIDevices
()
launchDevices
:=
b
.
config
.
BuildLaunchDevices
()
omiDevices
:=
b
.
config
.
BuildO
scO
MIDevices
()
launch
OSC
Devices
:=
b
.
config
.
Build
OSC
LaunchDevices
()
steps
:=
[]
multistep
.
Step
{
&
osccommon
.
StepPreValidate
{
...
...
@@ -207,7 +210,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
DisableStopVm
:
b
.
config
.
DisableStopVm
,
},
&
StepSnapshotVolumes
{
LaunchDevices
:
launchDevices
,
LaunchDevices
:
launch
OSC
Devices
,
},
&
osccommon
.
StepDeregisterOMI
{
AccessConfig
:
&
b
.
config
.
AccessConfig
,
...
...
@@ -219,7 +222,8 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&
StepRegisterOMI
{
RootDevice
:
b
.
config
.
RootDevice
,
OMIDevices
:
omiDevices
,
LaunchDevices
:
launchDevices
,
LaunchDevices
:
launchOSCDevices
,
RawRegion
:
b
.
config
.
RawRegion
,
},
&
osccommon
.
StepUpdateOMIAttributes
{
AccountIds
:
b
.
config
.
OMIAccountIDs
,
...
...
This diff is collapsed.
Click to expand it.
builder/osc/bsusurrogate/builder_acc_test.go
+
1
-
1
View file @
b844b7f1
...
...
@@ -24,7 +24,7 @@ const testBuilderAccBasic = `
"type": "test",
"region": "eu-west-2",
"vm_type": "t2.micro",
"source_omi": "ami-
65efcc11
",
"source_omi": "ami-
abe953fa
",
"ssh_username": "outscale",
"omi_name": "packer-test {{timestamp}}",
"omi_virtualization_type": "hvm",
...
...
This diff is collapsed.
Click to expand it.
builder/osc/bsusurrogate/step_register_omi.go
+
36
-
24
View file @
b844b7f1
...
...
@@ -4,23 +4,27 @@ import (
"context"
"fmt"
"github.com/antihax/optional"
osccommon
"github.com/hashicorp/packer/builder/osc/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
)
// StepRegisterOMI creates the OMI.
type
StepRegisterOMI
struct
{
RootDevice
RootBlockDevice
OMIDevices
[]
oapi
.
BlockDeviceMappingImage
LaunchDevices
[]
oapi
.
BlockDeviceMappingVmCreation
image
*
oapi
.
Image
OMIDevices
[]
osc
.
BlockDeviceMappingImage
LaunchDevices
[]
osc
.
BlockDeviceMappingVmCreation
image
*
osc
.
Image
RawRegion
string
}
func
(
s
*
StepRegisterOMI
)
Run
(
ctx
context
.
Context
,
state
multistep
.
StateBag
)
multistep
.
StepAction
{
config
:=
state
.
Get
(
"config"
)
.
(
*
Config
)
oapiconn
:=
state
.
Get
(
"oapi"
)
.
(
*
oapi
.
Client
)
oscconn
:=
state
.
Get
(
"osc"
)
.
(
*
osc
.
APIClient
)
snapshotIds
:=
state
.
Get
(
"snapshot_ids"
)
.
(
map
[
string
]
string
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
...
...
@@ -28,14 +32,16 @@ func (s *StepRegisterOMI) Run(ctx context.Context, state multistep.StateBag) mul
blockDevices
:=
s
.
combineDevices
(
snapshotIds
)
registerOpts
:=
o
api
.
CreateImageRequest
{
registerOpts
:=
o
sc
.
CreateImageRequest
{
ImageName
:
config
.
OMIName
,
Architecture
:
"x86_64"
,
RootDeviceName
:
s
.
RootDevice
.
DeviceName
,
BlockDeviceMappings
:
blockDevices
,
}
registerResp
,
err
:=
oapiconn
.
POST_CreateImage
(
registerOpts
)
registerResp
,
_
,
err
:=
oscconn
.
ImageApi
.
CreateImage
(
context
.
Background
(),
&
osc
.
CreateImageOpts
{
CreateImageRequest
:
optional
.
NewInterface
(
registerOpts
),
})
if
err
!=
nil
{
state
.
Put
(
"error"
,
fmt
.
Errorf
(
"Error registering OMI: %s"
,
err
))
ui
.
Error
(
state
.
Get
(
"error"
)
.
(
error
)
.
Error
())
...
...
@@ -43,24 +49,26 @@ func (s *StepRegisterOMI) Run(ctx context.Context, state multistep.StateBag) mul
}
// Set the OMI ID in the state
ui
.
Say
(
fmt
.
Sprintf
(
"OMI: %s"
,
registerResp
.
OK
.
Image
.
ImageId
))
ui
.
Say
(
fmt
.
Sprintf
(
"OMI: %s"
,
registerResp
.
Image
.
ImageId
))
omis
:=
make
(
map
[
string
]
string
)
omis
[
oapiconn
.
GetConfig
()
.
Region
]
=
registerResp
.
OK
.
Image
.
ImageId
omis
[
oapiconn
.
GetConfig
()
.
Region
]
=
registerResp
.
Image
.
ImageId
state
.
Put
(
"omis"
,
omis
)
// Wait for the image to become ready
ui
.
Say
(
"Waiting for OMI to become ready..."
)
if
err
:=
osccommon
.
WaitUntilImageAvailable
(
o
api
conn
,
registerResp
.
OK
.
Image
.
ImageId
);
err
!=
nil
{
if
err
:=
osccommon
.
WaitUntil
Osc
ImageAvailable
(
o
sc
conn
,
registerResp
.
Image
.
ImageId
);
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error waiting for OMI: %s"
,
err
)
state
.
Put
(
"error"
,
err
)
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
imagesResp
,
err
:=
oapiconn
.
POST_ReadImages
(
oapi
.
ReadImagesRequest
{
Filters
:
oapi
.
FiltersImage
{
ImageIds
:
[]
string
{
registerResp
.
OK
.
Image
.
ImageId
},
},
imagesResp
,
_
,
err
:=
oscconn
.
ImageApi
.
ReadImages
(
context
.
Background
(),
&
osc
.
ReadImagesOpts
{
ReadImagesRequest
:
optional
.
NewInterface
(
osc
.
ReadImagesRequest
{
Filters
:
osc
.
FiltersImage
{
ImageIds
:
[]
string
{
registerResp
.
Image
.
ImageId
},
},
}),
})
if
err
!=
nil
{
...
...
@@ -69,12 +77,12 @@ func (s *StepRegisterOMI) Run(ctx context.Context, state multistep.StateBag) mul
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
s
.
image
=
&
imagesResp
.
OK
.
Images
[
0
]
s
.
image
=
&
imagesResp
.
Images
[
0
]
snapshots
:=
make
(
map
[
string
][]
string
)
for
_
,
blockDeviceMapping
:=
range
imagesResp
.
OK
.
Images
[
0
]
.
BlockDeviceMappings
{
for
_
,
blockDeviceMapping
:=
range
imagesResp
.
Images
[
0
]
.
BlockDeviceMappings
{
if
blockDeviceMapping
.
Bsu
.
SnapshotId
!=
""
{
snapshots
[
oapiconn
.
GetConfig
()
.
Region
]
=
append
(
snapshots
[
oapiconn
.
GetConfig
()
.
Region
],
blockDeviceMapping
.
Bsu
.
SnapshotId
)
snapshots
[
s
.
Raw
Region
]
=
append
(
snapshots
[
s
.
Raw
Region
],
blockDeviceMapping
.
Bsu
.
SnapshotId
)
}
}
state
.
Put
(
"snapshots"
,
snapshots
)
...
...
@@ -93,19 +101,23 @@ func (s *StepRegisterOMI) Cleanup(state multistep.StateBag) {
return
}
o
api
conn
:=
state
.
Get
(
"o
api
"
)
.
(
*
o
api
.
Client
)
o
sc
conn
:=
state
.
Get
(
"o
sc
"
)
.
(
*
o
sc
.
API
Client
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
ui
.
Say
(
"Deregistering the OMI because cancellation or error..."
)
deregisterOpts
:=
oapi
.
DeleteImageRequest
{
ImageId
:
s
.
image
.
ImageId
}
if
_
,
err
:=
oapiconn
.
POST_DeleteImage
(
deregisterOpts
);
err
!=
nil
{
deregisterOpts
:=
osc
.
DeleteImageRequest
{
ImageId
:
s
.
image
.
ImageId
}
_
,
_
,
err
:=
oscconn
.
ImageApi
.
DeleteImage
(
context
.
Background
(),
&
osc
.
DeleteImageOpts
{
DeleteImageRequest
:
optional
.
NewInterface
(
deregisterOpts
),
})
if
err
!=
nil
{
ui
.
Error
(
fmt
.
Sprintf
(
"Error deregistering OMI, may still be around: %s"
,
err
))
return
}
}
func
(
s
*
StepRegisterOMI
)
combineDevices
(
snapshotIds
map
[
string
]
string
)
[]
o
api
.
BlockDeviceMappingImage
{
devices
:=
map
[
string
]
o
api
.
BlockDeviceMappingImage
{}
func
(
s
*
StepRegisterOMI
)
combineDevices
(
snapshotIds
map
[
string
]
string
)
[]
o
sc
.
BlockDeviceMappingImage
{
devices
:=
map
[
string
]
o
sc
.
BlockDeviceMappingImage
{}
for
_
,
device
:=
range
s
.
OMIDevices
{
devices
[
device
.
DeviceName
]
=
device
...
...
@@ -125,18 +137,18 @@ func (s *StepRegisterOMI) combineDevices(snapshotIds map[string]string) []oapi.B
devices
[
device
.
DeviceName
]
=
copyToDeviceMappingImage
(
device
)
}
blockDevices
:=
[]
o
api
.
BlockDeviceMappingImage
{}
blockDevices
:=
[]
o
sc
.
BlockDeviceMappingImage
{}
for
_
,
device
:=
range
devices
{
blockDevices
=
append
(
blockDevices
,
device
)
}
return
blockDevices
}
func
copyToDeviceMappingImage
(
device
o
api
.
BlockDeviceMappingVmCreation
)
o
api
.
BlockDeviceMappingImage
{
deviceImage
:=
o
api
.
BlockDeviceMappingImage
{
func
copyToDeviceMappingImage
(
device
o
sc
.
BlockDeviceMappingVmCreation
)
o
sc
.
BlockDeviceMappingImage
{
deviceImage
:=
o
sc
.
BlockDeviceMappingImage
{
DeviceName
:
device
.
DeviceName
,
VirtualDeviceName
:
device
.
VirtualDeviceName
,
Bsu
:
o
api
.
BsuToCreate
{
Bsu
:
o
sc
.
BsuToCreate
{
DeleteOnVmDeletion
:
device
.
Bsu
.
DeleteOnVmDeletion
,
Iops
:
device
.
Bsu
.
Iops
,
SnapshotId
:
device
.
Bsu
.
SnapshotId
,
...
...
This diff is collapsed.
Click to expand it.
builder/osc/bsusurrogate/step_snapshop_volumes.go
+
18
-
13
View file @
b844b7f1
...
...
@@ -6,11 +6,12 @@ import (
"sync"
"time"
"github.com/antihax/optional"
multierror
"github.com/hashicorp/go-multierror"
osccommon
"github.com/hashicorp/packer/builder/osc/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/outscale/osc-go/o
api
"
"github.com/outscale/osc-
sdk-
go/o
sc
"
)
// StepSnapshotVolumes creates snapshots of the created volumes.
...
...
@@ -18,14 +19,14 @@ import (
// Produces:
// snapshot_ids map[string]string - IDs of the created snapshots
type
StepSnapshotVolumes
struct
{
LaunchDevices
[]
o
api
.
BlockDeviceMappingVmCreation
LaunchDevices
[]
o
sc
.
BlockDeviceMappingVmCreation
snapshotIds
map
[
string
]
string
}
func
(
s
*
StepSnapshotVolumes
)
snapshotVolume
(
ctx
context
.
Context
,
deviceName
string
,
state
multistep
.
StateBag
)
error
{
o
api
conn
:=
state
.
Get
(
"o
api
"
)
.
(
*
o
api
.
Client
)
o
sc
conn
:=
state
.
Get
(
"o
sc
"
)
.
(
*
o
sc
.
API
Client
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
vm
:=
state
.
Get
(
"vm"
)
.
(
o
api
.
Vm
)
vm
:=
state
.
Get
(
"vm"
)
.
(
o
sc
.
Vm
)
var
volumeId
string
for
_
,
volume
:=
range
vm
.
BlockDeviceMappings
{
...
...
@@ -40,19 +41,21 @@ func (s *StepSnapshotVolumes) snapshotVolume(ctx context.Context, deviceName str
ui
.
Say
(
fmt
.
Sprintf
(
"Creating snapshot of EBS Volume %s..."
,
volumeId
))
description
:=
fmt
.
Sprintf
(
"Packer: %s"
,
time
.
Now
()
.
String
())
createSnapResp
,
err
:=
oapiconn
.
POST_CreateSnapshot
(
oapi
.
CreateSnapshotRequest
{
VolumeId
:
volumeId
,
Description
:
description
,
createSnapResp
,
_
,
err
:=
oscconn
.
SnapshotApi
.
CreateSnapshot
(
context
.
Background
(),
&
osc
.
CreateSnapshotOpts
{
CreateSnapshotRequest
:
optional
.
NewInterface
(
osc
.
CreateSnapshotRequest
{
VolumeId
:
volumeId
,
Description
:
description
,
}),
})
if
err
!=
nil
{
return
err
}
// Set the snapshot ID so we can delete it later
s
.
snapshotIds
[
deviceName
]
=
createSnapResp
.
OK
.
Snapshot
.
SnapshotId
s
.
snapshotIds
[
deviceName
]
=
createSnapResp
.
Snapshot
.
SnapshotId
// Wait for snapshot to be created
err
=
osccommon
.
WaitUntilSnapshotCompleted
(
o
api
conn
,
createSnapResp
.
OK
.
Snapshot
.
SnapshotId
)
err
=
osccommon
.
WaitUntil
Osc
SnapshotCompleted
(
o
sc
conn
,
createSnapResp
.
Snapshot
.
SnapshotId
)
return
err
}
...
...
@@ -65,7 +68,7 @@ func (s *StepSnapshotVolumes) Run(ctx context.Context, state multistep.StateBag)
var
errs
*
multierror
.
Error
for
_
,
device
:=
range
s
.
LaunchDevices
{
wg
.
Add
(
1
)
go
func
(
device
o
api
.
BlockDeviceMappingVmCreation
)
{
go
func
(
device
o
sc
.
BlockDeviceMappingVmCreation
)
{
defer
wg
.
Done
()
if
err
:=
s
.
snapshotVolume
(
ctx
,
device
.
DeviceName
,
state
);
err
!=
nil
{
errs
=
multierror
.
Append
(
errs
,
err
)
...
...
@@ -94,11 +97,13 @@ func (s *StepSnapshotVolumes) Cleanup(state multistep.StateBag) {
_
,
halted
:=
state
.
GetOk
(
multistep
.
StateHalted
)
if
cancelled
||
halted
{
o
api
conn
:=
state
.
Get
(
"o
api
"
)
.
(
*
o
api
.
Client
)
o
sc
conn
:=
state
.
Get
(
"o
sc
"
)
.
(
*
o
sc
.
API
Client
)
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
ui
.
Say
(
"Removing snapshots since we cancelled or halted..."
)
for
_
,
snapshotId
:=
range
s
.
snapshotIds
{
_
,
err
:=
oapiconn
.
POST_DeleteSnapshot
(
oapi
.
DeleteSnapshotRequest
{
SnapshotId
:
snapshotId
})
for
_
,
snapshotID
:=
range
s
.
snapshotIds
{
_
,
_
,
err
:=
oscconn
.
SnapshotApi
.
DeleteSnapshot
(
context
.
Background
(),
&
osc
.
DeleteSnapshotOpts
{
DeleteSnapshotRequest
:
optional
.
NewInterface
(
osc
.
DeleteSnapshotRequest
{
SnapshotId
:
snapshotID
}),
})
if
err
!=
nil
{
ui
.
Error
(
fmt
.
Sprintf
(
"Error: %s"
,
err
))
}
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help