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
7d4efdc2
Commit
7d4efdc2
authored
Jun 16, 2014
by
Jack Pearkes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
post-processor/vagrant-cloud: initial commit
parent
e2acb8e9
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
360 additions
and
2 deletions
+360
-2
config.go
config.go
+2
-1
plugin/post-processor-vagrant-cloud/main.go
plugin/post-processor-vagrant-cloud/main.go
+15
-0
plugin/post-processor-vagrant-cloud/main_test.go
plugin/post-processor-vagrant-cloud/main_test.go
+1
-0
post-processor/vagrant-cloud/api.go
post-processor/vagrant-cloud/api.go
+97
-0
post-processor/vagrant-cloud/artifact.go
post-processor/vagrant-cloud/artifact.go
+40
-0
post-processor/vagrant-cloud/artifact_test.go
post-processor/vagrant-cloud/artifact_test.go
+14
-0
post-processor/vagrant-cloud/post-processor.go
post-processor/vagrant-cloud/post-processor.go
+142
-0
post-processor/vagrant-cloud/post-processor_test.go
post-processor/vagrant-cloud/post-processor_test.go
+41
-0
post-processor/vagrant/artifact.go
post-processor/vagrant/artifact.go
+1
-1
post-processor/vagrant/artifact_test.go
post-processor/vagrant/artifact_test.go
+7
-0
No files found.
config.go
View file @
7d4efdc2
...
...
@@ -47,7 +47,8 @@ const defaultConfig = `
"vagrant": "packer-post-processor-vagrant",
"vsphere": "packer-post-processor-vsphere",
"docker-push": "packer-post-processor-docker-push",
"docker-import": "packer-post-processor-docker-import"
"docker-import": "packer-post-processor-docker-import",
"vagrant-cloud": "packer-post-processor-vagrant-cloud"
},
"provisioners": {
...
...
plugin/post-processor-vagrant-cloud/main.go
0 → 100644
View file @
7d4efdc2
package
main
import
(
"github.com/mitchellh/packer/packer/plugin"
"github.com/mitchellh/packer/post-processor/vagrant-cloud"
)
func
main
()
{
server
,
err
:=
plugin
.
Server
()
if
err
!=
nil
{
panic
(
err
)
}
server
.
RegisterPostProcessor
(
new
(
vagrantcloud
.
PostProcessor
))
server
.
Serve
()
}
plugin/post-processor-vagrant-cloud/main_test.go
0 → 100644
View file @
7d4efdc2
package
main
post-processor/vagrant-cloud/api.go
0 → 100644
View file @
7d4efdc2
package
vagrantcloud
import
(
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
)
type
Box
struct
{
Tag
string
`json:"tag"`
}
type
VagrantCloudClient
struct
{
// The http client for communicating
client
*
http
.
Client
// The base URL of the API
BaseURL
string
// Access token
AccessToken
string
}
func
(
v
VagrantCloudClient
)
New
(
baseUrl
string
,
token
string
)
*
VagrantCloudClient
{
c
:=
&
VagrantCloudClient
{
client
:
&
http
.
Client
{
Transport
:
&
http
.
Transport
{
Proxy
:
http
.
ProxyFromEnvironment
,
},
},
BaseURL
:
baseUrl
,
AccessToken
:
token
,
}
return
c
}
func
decodeBody
(
resp
*
http
.
Response
,
out
interface
{})
error
{
defer
resp
.
Body
.
Close
()
dec
:=
json
.
NewDecoder
(
resp
.
Body
)
return
dec
.
Decode
(
out
)
}
// encodeBody is used to encode a request body
func
encodeBody
(
obj
interface
{})
(
io
.
Reader
,
error
)
{
buf
:=
bytes
.
NewBuffer
(
nil
)
enc
:=
json
.
NewEncoder
(
buf
)
if
err
:=
enc
.
Encode
(
obj
);
err
!=
nil
{
return
nil
,
err
}
return
buf
,
nil
}
func
(
v
VagrantCloudClient
)
Box
(
tag
string
)
(
*
Box
,
error
)
{
resp
,
err
:=
v
.
Get
(
tag
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Error retrieving box: %s"
,
err
)
}
box
:=
&
Box
{}
if
err
=
decodeBody
(
resp
,
box
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Error parsing box response: %s"
,
err
)
}
return
box
,
nil
}
func
(
v
VagrantCloudClient
)
Get
(
path
string
)
(
*
http
.
Response
,
error
)
{
params
:=
url
.
Values
{}
params
.
Set
(
"access_token"
,
v
.
AccessToken
)
reqUrl
:=
fmt
.
Sprintf
(
"%s/%s%s"
,
v
.
BaseURL
,
path
,
params
.
Encode
())
// Scrub API key for logs
scrubbedUrl
:=
strings
.
Replace
(
reqUrl
,
v
.
AccessToken
,
"ACCESS_TOKEN"
,
-
1
)
log
.
Printf
(
"Post-Processor Vagrant Cloud API GET: %s"
,
scrubbedUrl
)
req
,
err
:=
http
.
NewRequest
(
"GET"
,
reqUrl
,
nil
)
resp
,
err
:=
v
.
client
.
Do
(
req
)
log
.
Printf
(
"Post-Processor Vagrant Cloud API Response:
\n\n
%s"
,
resp
)
return
resp
,
err
}
func
(
v
VagrantCloudClient
)
Post
(
path
string
,
body
map
[
string
]
interface
{})
(
map
[
string
]
interface
{},
error
)
{
// Scrub API key for logs
scrubbedUrl
:=
strings
.
Replace
(
path
,
v
.
AccessToken
,
"ACCESS_TOKEN"
,
-
1
)
log
.
Printf
(
"Post-Processor Vagrant Cloud API POST: %s.
\n\n
Body: %s"
,
scrubbedUrl
,
body
)
return
nil
,
nil
}
post-processor/vagrant-cloud/artifact.go
0 → 100644
View file @
7d4efdc2
package
vagrantcloud
import
(
"fmt"
"os"
)
const
BuilderId
=
"pearkes.post-processor.vagrant-cloud"
type
Artifact
struct
{
Path
string
Provider
string
}
func
NewArtifact
(
provider
,
path
string
)
*
Artifact
{
return
&
Artifact
{
Path
:
path
,
Provider
:
provider
,
}
}
func
(
*
Artifact
)
BuilderId
()
string
{
return
BuilderId
}
func
(
a
*
Artifact
)
Files
()
[]
string
{
return
[]
string
{
a
.
Path
}
}
func
(
a
*
Artifact
)
Id
()
string
{
return
""
}
func
(
a
*
Artifact
)
String
()
string
{
return
fmt
.
Sprintf
(
"'%s' provider box: %s"
,
a
.
Provider
,
a
.
Path
)
}
func
(
a
*
Artifact
)
Destroy
()
error
{
return
os
.
Remove
(
a
.
Path
)
}
post-processor/vagrant-cloud/artifact_test.go
0 → 100644
View file @
7d4efdc2
package
vagrantcloud
import
(
"github.com/mitchellh/packer/packer"
"testing"
)
func
TestArtifact_ImplementsArtifact
(
t
*
testing
.
T
)
{
var
raw
interface
{}
raw
=
&
Artifact
{}
if
_
,
ok
:=
raw
.
(
packer
.
Artifact
);
!
ok
{
t
.
Fatalf
(
"Artifact should be a Artifact"
)
}
}
post-processor/vagrant-cloud/post-processor.go
0 → 100644
View file @
7d4efdc2
// vagrant_cloud implements the packer.PostProcessor interface and adds a
// post-processor that uploads artifacts from the vagrant post-processor
// to Vagrant Cloud (vagrantcloud.com)
package
vagrantcloud
import
(
"fmt"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
)
const
VAGRANT_CLOUD_URL
=
"https://vagrantcloud.com"
type
Config
struct
{
common
.
PackerConfig
`mapstructure:",squash"`
Tag
string
`mapstructure:"box_tag"`
Version
string
`mapstructure:"version"`
AccessToken
string
`mapstructure:"access_token"`
VagrantCloudUrl
string
`mapstructure:"vagrant_cloud_url"`
tpl
*
packer
.
ConfigTemplate
}
type
PostProcessor
struct
{
config
Config
client
*
VagrantCloudClient
}
func
(
p
*
PostProcessor
)
Configure
(
raws
...
interface
{})
error
{
_
,
err
:=
common
.
DecodeConfig
(
&
p
.
config
,
raws
...
)
if
err
!=
nil
{
return
err
}
p
.
config
.
tpl
,
err
=
packer
.
NewConfigTemplate
()
if
err
!=
nil
{
return
err
}
p
.
config
.
tpl
.
UserVars
=
p
.
config
.
PackerUserVars
// Default configuration
if
p
.
config
.
VagrantCloudUrl
==
""
{
p
.
config
.
VagrantCloudUrl
=
VAGRANT_CLOUD_URL
}
// Accumulate any errors
errs
:=
new
(
packer
.
MultiError
)
// required configuration
templates
:=
map
[
string
]
*
string
{
"box_tag"
:
&
p
.
config
.
Tag
,
"version"
:
&
p
.
config
.
Version
,
"access_token"
:
&
p
.
config
.
AccessToken
,
}
for
key
,
ptr
:=
range
templates
{
if
*
ptr
==
""
{
errs
=
packer
.
MultiErrorAppend
(
errs
,
fmt
.
Errorf
(
"%s must be set"
,
key
))
}
}
// Template process
for
key
,
ptr
:=
range
templates
{
*
ptr
,
err
=
p
.
config
.
tpl
.
Process
(
*
ptr
,
nil
)
if
err
!=
nil
{
errs
=
packer
.
MultiErrorAppend
(
errs
,
fmt
.
Errorf
(
"Error processing %s: %s"
,
key
,
err
))
}
}
if
len
(
errs
.
Errors
)
>
0
{
return
errs
}
return
nil
}
func
(
p
*
PostProcessor
)
PostProcess
(
ui
packer
.
Ui
,
artifact
packer
.
Artifact
)
(
packer
.
Artifact
,
bool
,
error
)
{
config
:=
p
.
config
// Only accepts input from the vagrant post-processor
if
artifact
.
BuilderId
()
!=
"mitchellh.post-processor.vagrant"
{
return
nil
,
false
,
fmt
.
Errorf
(
"Unknown artifact type, requires box from vagrant post-processor: %s"
,
artifact
.
BuilderId
())
}
// The name of the provider for vagrant cloud, and vagrant
provider
:=
providerFromBuilderName
(
artifact
.
Id
())
version
:=
p
.
config
.
Version
tag
:=
p
.
config
.
Tag
// create the HTTP client
p
.
client
=
VagrantCloudClient
{}
.
New
(
p
.
config
.
VagrantCloudUrl
,
p
.
config
.
AccessToken
)
ui
.
Say
(
fmt
.
Sprintf
(
"Verifying box is accessible: %s"
,
tag
))
box
,
err
:=
p
.
client
.
Box
(
tag
)
if
err
!=
nil
{
return
nil
,
false
,
err
}
if
box
.
Tag
!=
tag
{
ui
.
Say
(
fmt
.
Sprintf
(
"Could not verify box is correct: %s"
,
tag
))
return
nil
,
false
,
err
}
ui
.
Say
(
fmt
.
Sprintf
(
"Creating Version %s"
,
version
))
ui
.
Say
(
fmt
.
Sprintf
(
"Creating Provider %s"
,
version
))
ui
.
Say
(
fmt
.
Sprintf
(
"Uploading Box %s"
,
version
))
ui
.
Say
(
fmt
.
Sprintf
(
"Verifying upload %s"
,
version
))
ui
.
Say
(
fmt
.
Sprintf
(
"Releasing version %s"
,
version
))
return
NewArtifact
(
provider
,
config
.
Tag
),
true
,
nil
}
// Runs a cleanup if the post processor fails to upload
func
(
p
*
PostProcessor
)
Cleanup
()
{
// Delete the version
}
// converts a packer builder name to the corresponding vagrant
// provider
func
providerFromBuilderName
(
name
string
)
string
{
switch
name
{
case
"aws"
:
return
"aws"
case
"digitalocean"
:
return
"digitalocean"
case
"virtualbox"
:
return
"virtualbox"
case
"vmware"
:
return
"vmware_desktop"
case
"parallels"
:
return
"parallels"
default
:
return
name
}
}
post-processor/vagrant-cloud/post-processor_test.go
0 → 100644
View file @
7d4efdc2
package
vagrantcloud
import
(
"bytes"
"github.com/mitchellh/packer/packer"
"testing"
)
func
testConfig
()
map
[
string
]
interface
{}
{
return
map
[
string
]
interface
{}{}
}
func
testPP
(
t
*
testing
.
T
)
*
PostProcessor
{
var
p
PostProcessor
if
err
:=
p
.
Configure
(
testConfig
());
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
return
&
p
}
func
testUi
()
*
packer
.
BasicUi
{
return
&
packer
.
BasicUi
{
Reader
:
new
(
bytes
.
Buffer
),
Writer
:
new
(
bytes
.
Buffer
),
}
}
func
TestPostProcessor_ImplementsPostProcessor
(
t
*
testing
.
T
)
{
var
_
packer
.
PostProcessor
=
new
(
PostProcessor
)
}
func
TestproviderFromBuilderName
(
t
*
testing
.
T
)
{
if
providerFromBuilderName
(
"foobar"
)
!=
"foobar"
{
t
.
Fatal
(
"should copy unknown provider"
)
}
if
providerFromBuilderName
(
"vmware"
)
!=
"vmware_desktop"
{
t
.
Fatal
(
"should convert provider"
)
}
}
post-processor/vagrant/artifact.go
View file @
7d4efdc2
...
...
@@ -28,7 +28,7 @@ func (a *Artifact) Files() []string {
}
func
(
a
*
Artifact
)
Id
()
string
{
return
""
return
a
.
Provider
}
func
(
a
*
Artifact
)
String
()
string
{
...
...
post-processor/vagrant/artifact_test.go
View file @
7d4efdc2
...
...
@@ -12,3 +12,10 @@ func TestArtifact_ImplementsArtifact(t *testing.T) {
t
.
Fatalf
(
"Artifact should be a Artifact"
)
}
}
func
TestArtifact_Id
(
t
*
testing
.
T
)
{
artifact
:=
NewArtifact
(
"vmware"
,
"./"
)
if
artifact
.
Id
()
!=
"vmware"
{
t
.
Fatalf
(
"should return name as Id"
)
}
}
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