-
Notifications
You must be signed in to change notification settings - Fork 1
/
mgr_chef_berkshelf.go
116 lines (98 loc) · 3.66 KB
/
mgr_chef_berkshelf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package mgr
import (
"fmt"
"github.com/analogj/go-util/utils"
"github.com/packagrio/go-common/errors"
"github.com/packagrio/go-common/metadata"
"github.com/packagrio/go-common/pipeline"
"github.com/packagrio/publishr/pkg/config"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
)
func DetectChefBerkshelf(pipelineData *pipeline.Data, myconfig config.Interface, client *http.Client) bool {
berksfilePath := path.Join(pipelineData.GitLocalPath, "Berksfile")
return utils.FileExists(berksfilePath)
}
type mgrChefBerkshelf struct {
Config config.Interface
PipelineData *pipeline.Data
Client *http.Client
}
func (m *mgrChefBerkshelf) Init(pipelineData *pipeline.Data, myconfig config.Interface, client *http.Client) error {
m.PipelineData = pipelineData
m.Config = myconfig
if client != nil {
//primarily used for testing.
m.Client = client
}
return nil
}
func (m *mgrChefBerkshelf) MgrValidateTools() error {
//a chef/berkshelf like environment needs to be available for this Engine
if _, kerr := exec.LookPath("knife"); kerr != nil {
return errors.EngineValidateToolError("knife binary is missing")
}
if _, berr := exec.LookPath("berks"); berr != nil {
return errors.EngineValidateToolError("berkshelf binary is missing")
}
//TODO: figure out how to validate that "bundle audit" command exists.
if _, berr := exec.LookPath("bundle"); berr != nil {
return errors.EngineValidateToolError("bundler binary is missing")
}
return nil
}
func (m *mgrChefBerkshelf) MgrDistStep(nextMetadata interface{}) error {
if !m.Config.IsSet("chef_supermarket_username") || !m.Config.IsSet("chef_supermarket_key") {
return errors.MgrDistCredentialsMissing("Cannot deploy cookbook to supermarket, credentials missing")
}
// knife is really sensitive to folder names. The cookbook name MUST match the folder name otherwise knife throws up
// when doing a knife cookbook share. So we're going to make a new tmp directory, create a subdirectory with the EXACT
// cookbook name, and then copy the cookbook contents into it. Yeah yeah, its pretty nasty, but blame Chef.
tmpParentPath, terr := ioutil.TempDir("", "")
if terr != nil {
return terr
}
defer os.RemoveAll(tmpParentPath)
tmpLocalPath := path.Join(tmpParentPath, nextMetadata.(*metadata.ChefMetadata).Name)
if cerr := utils.CopyDir(m.PipelineData.GitLocalPath, tmpLocalPath); cerr != nil {
return cerr
}
pemFile, _ := ioutil.TempFile("", "client.pem")
defer os.Remove(pemFile.Name())
knifeFile, _ := ioutil.TempFile("", "knife.rb")
defer os.Remove(knifeFile.Name())
// write the knife.rb config jfile.
knifeContent := fmt.Sprintf(utils.StripIndent(
`node_name "%s" # Replace with the login name you use to login to the Supermarket.
client_key "%s" # Define the path to wherever your client.pem file lives. This is the key you generated when you signed up for a Chef account.
cookbook_path [ '%s' ] # Directory where the cookbook you're uploading resides.
`),
m.Config.GetString(config.PACKAGR_CHEF_SUPERMARKET_USERNAME),
pemFile.Name(),
tmpParentPath,
)
_, kerr := knifeFile.Write([]byte(knifeContent))
if kerr != nil {
return kerr
}
chefKey, berr := m.Config.GetBase64Decoded("chef_supermarket_key")
if berr != nil {
return berr
}
_, perr := pemFile.Write([]byte(chefKey))
if perr != nil {
return perr
}
cookbookDistCmd := fmt.Sprintf("knife cookbook site share %s %s -c %s",
nextMetadata.(*metadata.ChefMetadata).Name,
m.Config.GetString(config.PACKAGR_CHEF_SUPERMARKET_TYPE),
knifeFile.Name(),
)
if derr := utils.BashCmdExec(cookbookDistCmd, "", nil, ""); derr != nil {
return errors.MgrDistPackageError("knife cookbook upload to supermarket failed")
}
return nil
}