Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
cf99f85f
Commit
cf99f85f
authored
Jul 30, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/amazon/chroot: flock so that device searching is safe
parent
b75bd29b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
105 additions
and
0 deletions
+105
-0
builder/amazon/chroot/builder.go
builder/amazon/chroot/builder.go
+1
-0
builder/amazon/chroot/lockfile.go
builder/amazon/chroot/lockfile.go
+13
-0
builder/amazon/chroot/lockfile_unix.go
builder/amazon/chroot/lockfile_unix.go
+32
-0
builder/amazon/chroot/step_flock.go
builder/amazon/chroot/step_flock.go
+59
-0
No files found.
builder/amazon/chroot/builder.go
View file @
cf99f85f
...
...
@@ -123,6 +123,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
steps
:=
[]
multistep
.
Step
{
&
StepInstanceInfo
{},
&
StepSourceAMIInfo
{},
&
StepFlock
{},
&
StepPrepareDevice
{},
&
StepCreateVolume
{},
&
StepAttachVolume
{},
...
...
builder/amazon/chroot/lockfile.go
0 → 100644
View file @
cf99f85f
// +build windows
package
chroot
import
"errors"
func
lockFile
(
*
os
.
File
)
error
{
return
errors
.
New
(
"not supported on Windows"
)
}
func
unlockFile
(
f
*
os
.
File
)
error
{
return
nil
}
builder/amazon/chroot/lockfile_unix.go
0 → 100644
View file @
cf99f85f
// +build !windows
package
chroot
import
(
"errors"
"os"
"syscall"
)
// See: http://linux.die.net/include/sys/file.h
const
LOCK_EX
=
2
const
LOCK_NB
=
4
const
LOCK_UN
=
8
func
lockFile
(
f
*
os
.
File
)
error
{
err
:=
syscall
.
Flock
(
int
(
f
.
Fd
()),
LOCK_EX
|
LOCK_NB
)
if
err
!=
nil
{
errno
,
ok
:=
err
.
(
syscall
.
Errno
)
if
ok
&&
errno
==
syscall
.
EWOULDBLOCK
{
return
errors
.
New
(
"file already locked"
)
}
return
err
}
return
nil
}
func
unlockFile
(
f
*
os
.
File
)
error
{
return
syscall
.
Flock
(
int
(
f
.
Fd
()),
LOCK_UN
)
}
builder/amazon/chroot/step_flock.go
0 → 100644
View file @
cf99f85f
package
chroot
import
(
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
"os"
"path/filepath"
)
// StepFlock provisions the instance within a chroot.
type
StepFlock
struct
{
fh
*
os
.
File
}
func
(
s
*
StepFlock
)
Run
(
state
map
[
string
]
interface
{})
multistep
.
StepAction
{
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
lockfile
:=
"/var/lock/packer-chroot/lock"
if
err
:=
os
.
MkdirAll
(
filepath
.
Dir
(
lockfile
),
0755
);
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error creating lock: %s"
,
err
)
state
[
"error"
]
=
err
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
log
.
Printf
(
"Obtaining lock: %s"
,
lockfile
)
f
,
err
:=
os
.
Create
(
lockfile
)
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error creating lock: %s"
,
err
)
state
[
"error"
]
=
err
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
// LOCK!
if
err
:=
lockFile
(
f
);
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error creating lock: %s"
,
err
)
state
[
"error"
]
=
err
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
// Set the file handle, we can't close it because we need to hold
// the lock.
s
.
fh
=
f
return
multistep
.
ActionContinue
}
func
(
s
*
StepFlock
)
Cleanup
(
state
map
[
string
]
interface
{})
{
if
s
.
fh
==
nil
{
return
}
log
.
Printf
(
"Unlocking: %s"
,
s
.
fh
.
Name
())
unlockFile
(
s
.
fh
)
}
Write
Preview
Markdown
is supported
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