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
2c8b1980
Commit
2c8b1980
authored
Jun 09, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer/rpc: Support Cache interface
parent
7968068a
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
173 additions
and
8 deletions
+173
-8
packer/cache.go
packer/cache.go
+3
-3
packer/cache_test.go
packer/cache_test.go
+1
-5
packer/rpc/cache.go
packer/rpc/cache.go
+77
-0
packer/rpc/cache_test.go
packer/rpc/cache_test.go
+86
-0
packer/rpc/server.go
packer/rpc/server.go
+6
-0
No files found.
packer/cache.go
View file @
2c8b1980
...
...
@@ -14,7 +14,7 @@ type Cache interface {
// the lock is held.
//
// The cache will block and wait for the lock.
Lock
(
string
)
(
string
,
error
)
Lock
(
string
)
string
// Unlock will unlock a certain cache key. Be very careful that this
// is only called once per lock obtained.
...
...
@@ -38,12 +38,12 @@ type FileCache struct {
rw
map
[
string
]
*
sync
.
RWMutex
}
func
(
f
*
FileCache
)
Lock
(
key
string
)
(
string
,
error
)
{
func
(
f
*
FileCache
)
Lock
(
key
string
)
string
{
hashKey
:=
f
.
hashKey
(
key
)
rw
:=
f
.
rwLock
(
hashKey
)
rw
.
Lock
()
return
filepath
.
Join
(
f
.
CacheDir
,
hashKey
)
,
nil
return
filepath
.
Join
(
f
.
CacheDir
,
hashKey
)
}
func
(
f
*
FileCache
)
Unlock
(
key
string
)
{
...
...
packer/cache_test.go
View file @
2c8b1980
...
...
@@ -22,11 +22,7 @@ func TestFileCache(t *testing.T) {
defer
os
.
RemoveAll
(
cacheDir
)
cache
:=
&
FileCache
{
CacheDir
:
cacheDir
}
path
,
err
:=
cache
.
Lock
(
"foo"
)
if
err
!=
nil
{
t
.
Fatalf
(
"error locking: %s"
,
err
)
}
path
:=
cache
.
Lock
(
"foo"
)
err
=
ioutil
.
WriteFile
(
path
,
[]
byte
(
"data"
),
0666
)
if
err
!=
nil
{
t
.
Fatalf
(
"error writing: %s"
,
err
)
...
...
packer/rpc/cache.go
0 → 100644
View file @
2c8b1980
package
rpc
import
(
"github.com/mitchellh/packer/packer"
"net/rpc"
)
// An implementation of packer.Cache where the cache is actually executed
// over an RPC connection.
type
cache
struct
{
client
*
rpc
.
Client
}
// CacheServer wraps a packer.Cache implementation and makes it exportable
// as part of a Golang RPC server.
type
CacheServer
struct
{
cache
packer
.
Cache
}
func
Cache
(
client
*
rpc
.
Client
)
*
cache
{
return
&
cache
{
client
}
}
type
CacheRLockResponse
struct
{
Path
string
Exists
bool
}
func
(
c
*
cache
)
Lock
(
key
string
)
(
result
string
)
{
if
err
:=
c
.
client
.
Call
(
"Cache.Lock"
,
key
,
&
result
);
err
!=
nil
{
panic
(
err
)
}
return
}
func
(
c
*
cache
)
RLock
(
key
string
)
(
string
,
bool
)
{
var
result
CacheRLockResponse
if
err
:=
c
.
client
.
Call
(
"Cache.RLock"
,
key
,
&
result
);
err
!=
nil
{
panic
(
err
)
}
return
result
.
Path
,
result
.
Exists
}
func
(
c
*
cache
)
Unlock
(
key
string
)
{
if
err
:=
c
.
client
.
Call
(
"Cache.Unlock"
,
key
,
new
(
interface
{}));
err
!=
nil
{
panic
(
err
)
}
}
func
(
c
*
cache
)
RUnlock
(
key
string
)
{
if
err
:=
c
.
client
.
Call
(
"Cache.RUnlock"
,
key
,
new
(
interface
{}));
err
!=
nil
{
panic
(
err
)
}
}
func
(
c
*
CacheServer
)
Lock
(
key
string
,
result
*
string
)
error
{
*
result
=
c
.
cache
.
Lock
(
key
)
return
nil
}
func
(
c
*
CacheServer
)
Unlock
(
key
string
,
result
*
interface
{})
error
{
c
.
cache
.
Unlock
(
key
)
return
nil
}
func
(
c
*
CacheServer
)
RLock
(
key
string
,
result
*
CacheRLockResponse
)
error
{
path
,
exists
:=
c
.
cache
.
RLock
(
key
)
*
result
=
CacheRLockResponse
{
path
,
exists
}
return
nil
}
func
(
c
*
CacheServer
)
RUnlock
(
key
string
,
result
*
interface
{})
error
{
c
.
cache
.
RUnlock
(
key
)
return
nil
}
packer/rpc/cache_test.go
0 → 100644
View file @
2c8b1980
package
rpc
import
(
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"net/rpc"
"testing"
)
type
testCache
struct
{
lockCalled
bool
lockKey
string
unlockCalled
bool
unlockKey
string
rlockCalled
bool
rlockKey
string
runlockCalled
bool
runlockKey
string
}
func
(
t
*
testCache
)
Lock
(
key
string
)
string
{
t
.
lockCalled
=
true
t
.
lockKey
=
key
return
"foo"
}
func
(
t
*
testCache
)
RLock
(
key
string
)
(
string
,
bool
)
{
t
.
rlockCalled
=
true
t
.
rlockKey
=
key
return
"foo"
,
true
}
func
(
t
*
testCache
)
Unlock
(
key
string
)
{
t
.
unlockCalled
=
true
t
.
unlockKey
=
key
}
func
(
t
*
testCache
)
RUnlock
(
key
string
)
{
t
.
runlockCalled
=
true
t
.
runlockKey
=
key
}
func
TestCache_Implements
(
t
*
testing
.
T
)
{
var
raw
interface
{}
raw
=
Cache
(
nil
)
if
_
,
ok
:=
raw
.
(
packer
.
Cache
);
!
ok
{
t
.
Fatal
(
"Cache must be a cache."
)
}
}
func
TestCacheRPC
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
// Create the interface to test
c
:=
new
(
testCache
)
// Start the server
server
:=
rpc
.
NewServer
()
RegisterCache
(
server
,
c
)
address
:=
serveSingleConn
(
server
)
// Create the client over RPC and run some methods to verify it works
rpcClient
,
err
:=
rpc
.
Dial
(
"tcp"
,
address
)
assert
.
Nil
(
err
,
"should be able to connect"
)
client
:=
Cache
(
rpcClient
)
// Test Lock
client
.
Lock
(
"foo"
)
assert
.
True
(
c
.
lockCalled
,
"should be called"
)
assert
.
Equal
(
c
.
lockKey
,
"foo"
,
"should have proper key"
)
// Test Unlock
client
.
Unlock
(
"foo"
)
assert
.
True
(
c
.
unlockCalled
,
"should be called"
)
assert
.
Equal
(
c
.
unlockKey
,
"foo"
,
"should have proper key"
)
// Test RLock
client
.
RLock
(
"foo"
)
assert
.
True
(
c
.
rlockCalled
,
"should be called"
)
assert
.
Equal
(
c
.
rlockKey
,
"foo"
,
"should have proper key"
)
// Test RUnlock
client
.
RUnlock
(
"foo"
)
assert
.
True
(
c
.
runlockCalled
,
"should be called"
)
assert
.
Equal
(
c
.
runlockKey
,
"foo"
,
"should have proper key"
)
}
packer/rpc/server.go
View file @
2c8b1980
...
...
@@ -23,6 +23,12 @@ func RegisterBuilder(s *rpc.Server, b packer.Builder) {
s
.
RegisterName
(
"Builder"
,
&
BuilderServer
{
b
})
}
// Registers the appropriate endpoint on an RPC server to serve a
// Packer Cache.
func
RegisterCache
(
s
*
rpc
.
Server
,
c
packer
.
Cache
)
{
s
.
RegisterName
(
"Cache"
,
&
CacheServer
{
c
})
}
// Registers the appropriate endpoint on an RPC server to serve a
// Packer Command.
func
RegisterCommand
(
s
*
rpc
.
Server
,
c
packer
.
Command
)
{
...
...
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