Skip to content

Commit

Permalink
feat(hatchery): add vsphere support
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Coenen <[email protected]>
  • Loading branch information
bnjjj authored and yesnault committed Sep 5, 2017
1 parent b3c57c0 commit c389564
Show file tree
Hide file tree
Showing 137 changed files with 100,832 additions and 12 deletions.
55 changes: 47 additions & 8 deletions cli/cds/worker/model/add.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package model

import (
"bytes"
"encoding/base64"
"encoding/json"
"io/ioutil"
"regexp"
"strings"

"github.com/spf13/cobra"

"github.com/ovh/cds/sdk"
)

var (
imageP string
openstackFlavorP string
openstackUserDataFileP string
groupName string
imageP string
openstackFlavorP string
userDataFileP string
groupName string
)

func cmdWorkerModelAdd() *cobra.Command {
Expand All @@ -25,13 +28,14 @@ func cmdWorkerModelAdd() *cobra.Command {
Available model type :
- Docker images ("docker")
- Openstack image ("openstack")
- VSphere image ("vsphere")
`,
Run: addWorkerModel,
}

cmd.Flags().StringVar(&imageP, "image", "", "Image value (docker or openstack)")
cmd.Flags().StringVar(&imageP, "image", "", "Image value (docker, openstack, vsphere)")
cmd.Flags().StringVar(&openstackFlavorP, "flavor", "", "Flavor value (openstack)")
cmd.Flags().StringVar(&openstackUserDataFileP, "userdata", "", "Path to UserData file (openstack)")
cmd.Flags().StringVar(&userDataFileP, "userdata", "", "Path to UserData file (vsphere or openstack)")
cmd.Flags().StringVar(&groupName, "group", "", "Group name")

return cmd
Expand Down Expand Up @@ -66,10 +70,10 @@ func addWorkerModel(cmd *cobra.Command, args []string) {
if d.Flavor == "" {
sdk.Exit("Error: Openstack flavor not provided (--flavor)\n")
}
if openstackUserDataFileP == "" {
if userDataFileP == "" {
sdk.Exit("Error: Openstack UserData file not provided (--userdata)\n")
}
file, err := ioutil.ReadFile(openstackUserDataFileP)
file, err := ioutil.ReadFile(userDataFileP)
if err != nil {
sdk.Exit("Error: Cannot read Openstack UserData file (%s)\n", err)
}
Expand All @@ -80,6 +84,33 @@ func addWorkerModel(cmd *cobra.Command, args []string) {
}
image = string(data)
break
case string(sdk.VSphere):
t = sdk.VSphere
d := sdk.OpenstackModelData{
Image: imageP,
}
if d.Image == "" {
sdk.Exit("Error: VSphere image not provided (--image)\n")
}

if userDataFileP == "" {
sdk.Exit("Error: VSphere UserData file not provided (--userdata)\n")
}
file, err := ioutil.ReadFile(userDataFileP)
if err != nil {
sdk.Exit("Error: Cannot read Openstack UserData file (%s)\n", err)
}

rx := regexp.MustCompile(`(?m)(#.*)$`)
file = rx.ReplaceAll(file, []byte(""))
d.UserData = strings.Replace(string(file), "\n", " ; ", -1)

data, err := jsonWithoutHTMLEncode(d)
if err != nil {
sdk.Exit("Error: Cannot marshal model info (%s)\n", err)
}
image = string(data)
break
default:
sdk.Exit("Unknown worker type: %s\n", modelType)
}
Expand All @@ -97,3 +128,11 @@ func addWorkerModel(cmd *cobra.Command, args []string) {
sdk.Exit("Error: cannot add worker model (%s)\n", err)
}
}

func jsonWithoutHTMLEncode(t interface{}) ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(t)
return buffer.Bytes(), err
}
34 changes: 31 additions & 3 deletions cli/cds/worker/model/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/base64"
"encoding/json"
"io/ioutil"
"regexp"
"strings"

"github.com/spf13/cobra"

Expand All @@ -20,7 +22,7 @@ func cmdWorkerModelUpdate() *cobra.Command {

cmd.Flags().StringVar(&imageP, "image", "", "Image value (docker or openstack)")
cmd.Flags().StringVar(&openstackFlavorP, "flavor", "", "Flavor value (openstack)")
cmd.Flags().StringVar(&openstackUserDataFileP, "userdata", "", "Path to UserData file (openstack)")
cmd.Flags().StringVar(&userDataFileP, "userdata", "", "Path to UserData file (openstack)")
return cmd
}

Expand Down Expand Up @@ -59,10 +61,10 @@ func updateWorkerModel(cmd *cobra.Command, args []string) {
if d.Flavor == "" {
sdk.Exit("Error: Openstack flavor not provided (--flavor)\n")
}
if openstackUserDataFileP == "" {
if userDataFileP == "" {
sdk.Exit("Error: Openstack UserData file not provided (--userdata)\n")
}
file, err := ioutil.ReadFile(openstackUserDataFileP)
file, err := ioutil.ReadFile(userDataFileP)
if err != nil {
sdk.Exit("Error: Cannot read Openstack UserData file (%s)\n", err)
}
Expand All @@ -73,6 +75,32 @@ func updateWorkerModel(cmd *cobra.Command, args []string) {
}
value = string(data)
break
case string(sdk.VSphere):
t = sdk.VSphere
d := sdk.OpenstackModelData{
Image: imageP,
}
if d.Image == "" {
sdk.Exit("Error: VSphere image not provided (--image)\n")
}
if userDataFileP == "" {
sdk.Exit("Error: VSphere UserData file not provided (--userdata)\n")
}
file, err := ioutil.ReadFile(userDataFileP)
if err != nil {
sdk.Exit("Error: Cannot read VSphere UserData file (%s)\n", err)
}

rx := regexp.MustCompile(`(?m)(#.*)$`)
file = rx.ReplaceAll(file, []byte(""))
d.UserData = strings.Replace(string(file), "\n", " ; ", -1)

data, err := jsonWithoutHTMLEncode(d)
if err != nil {
sdk.Exit("Error: Cannot marshal model info (%s)\n", err)
}
value = string(data)
break
default:
sdk.Exit("Unknown worker type: %s\n", typeS)
}
Expand Down
1 change: 1 addition & 0 deletions engine/api/worker_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func spawnErrorWorkerModelHandler(w http.ResponseWriter, r *http.Request, db *go

func updateWorkerModel(w http.ResponseWriter, r *http.Request, db *gorp.DbMap, c *businesscontext.Ctx) error {
workerModelID, errr := requestVarInt(r, "permModelID")

if errr != nil {
return sdk.WrapError(errr, "updateWorkerModel> Invalid permModelID")
}
Expand Down
2 changes: 2 additions & 0 deletions engine/hatchery/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ovh/cds/engine/hatchery/marathon"
"github.com/ovh/cds/engine/hatchery/openstack"
"github.com/ovh/cds/engine/hatchery/swarm"
"github.com/ovh/cds/engine/hatchery/vsphere"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/log"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -75,6 +76,7 @@ func addCommands() {
rootCmd.AddCommand(marathon.Cmd)
rootCmd.AddCommand(swarm.Cmd)
rootCmd.AddCommand(openstack.Cmd)
rootCmd.AddCommand(vsphere.Cmd)
rootCmd.AddCommand(cmdVersion)
}

Expand Down
Loading

0 comments on commit c389564

Please sign in to comment.