Skip to content

Commit

Permalink
Working version of cloudsigma provider
Browse files Browse the repository at this point in the history
  • Loading branch information
s-matyukevich committed Jan 6, 2015
1 parent 944652c commit edc2eb5
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 109 deletions.
39 changes: 31 additions & 8 deletions provider/cloudsigma/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
package cloudsigma

import (
"encoding/base64"
"fmt"
"strings"

"github.com/Altoros/gosigma"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/imagemetadata"
"github.com/juju/juju/instance"
ar "github.com/juju/juju/juju/arch"
"github.com/juju/loggo"
"github.com/juju/utils"
)
Expand All @@ -20,6 +22,7 @@ type environClient struct {
conn *gosigma.Client
uuid string
storage *environStorage
config *environConfig
}

type tracer struct{}
Expand Down Expand Up @@ -50,8 +53,9 @@ var newClient = func(cfg *environConfig) (client *environClient, err error) {
}

client = &environClient{
conn: conn,
uuid: uuid,
conn: conn,
uuid: uuid,
config: cfg,
}

return
Expand All @@ -63,6 +67,8 @@ const (
jujuMetaInstanceServer = "server"

jujuMetaEnvironment = "juju-environment"
jujuMetaCoudInit = "cloudinit-user-data"
jujuMetaBase64 = "base64_fields"
)

func (c *environClient) isMyEnvironment(s gosigma.Server) bool {
Expand Down Expand Up @@ -164,7 +170,7 @@ func (c *environClient) stopInstance(id instance.Id) error {
}

// start new instance
func (c *environClient) newInstance(args environs.StartInstanceParams, img *imagemetadata.ImageMetadata) (srv gosigma.Server, drv gosigma.Drive, err error) {
func (c *environClient) newInstance(args environs.StartInstanceParams, img *imagemetadata.ImageMetadata, userData []byte) (srv gosigma.Server, drv gosigma.Drive, arch string, err error) {

cleanup := func() {
if err == nil {
Expand Down Expand Up @@ -217,9 +223,9 @@ func (c *environClient) newInstance(args environs.StartInstanceParams, img *imag
}
}

cc, err := c.generateSigmaComponents(baseName, constraints, args, drv)
cc, err := c.generateSigmaComponents(baseName, constraints, args, drv, userData)
if err != nil {
return nil, drv, err
return
}

if srv, err = c.conn.CreateServer(cc); err != nil {
Expand All @@ -229,12 +235,23 @@ func (c *environClient) newInstance(args environs.StartInstanceParams, img *imag

if err = srv.Start(); err != nil {
err = fmt.Errorf("error booting new instance: %v", err)
return
}

// populate root drive hardware characteristics
switch originalDrive.Arch() {
case "64":
arch = ar.AMD64
case "32":
arch = ar.I386
default:
err = fmt.Errorf("unknown arch: %v", arch)
}

return
}

func (c *environClient) generateSigmaComponents(baseName string, constraints *sigmaConstraints, args environs.StartInstanceParams, drv gosigma.Drive) (cc gosigma.Components, err error) {
func (c *environClient) generateSigmaComponents(baseName string, constraints *sigmaConstraints, args environs.StartInstanceParams, drv gosigma.Drive, userData []byte) (cc gosigma.Components, err error) {
cc.SetName(baseName)
cc.SetDescription(baseName)
cc.SetSMP(constraints.cores)
Expand All @@ -247,8 +264,8 @@ func (c *environClient) generateSigmaComponents(baseName string, constraints *si
return
}
cc.SetVNCPassword(vncpass)

cc.SetSSHPublicKey(args.MachineConfig.AuthorizedKeys)
logger.Debugf("Setting ssh key: %s end", c.config.AuthorizedKeys())
cc.SetSSHPublicKey(c.config.AuthorizedKeys())
cc.AttachDrive(1, "0:0", "virtio", drv.UUID())
cc.NetworkDHCP4(gosigma.ModelVirtio)

Expand All @@ -259,6 +276,12 @@ func (c *environClient) generateSigmaComponents(baseName string, constraints *si
}

cc.SetMeta(jujuMetaEnvironment, c.uuid)
data, err := utils.Gunzip(userData)
if err != nil {
return
}
cc.SetMeta(jujuMetaCoudInit, base64.StdEncoding.EncodeToString(data))
cc.SetMeta(jujuMetaBase64, jujuMetaCoudInit)

return
}
13 changes: 9 additions & 4 deletions provider/cloudsigma/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/juju/juju/tools"
"github.com/juju/juju/version"
"github.com/juju/loggo"
"github.com/juju/utils"
gc "gopkg.in/check.v1"
)

Expand Down Expand Up @@ -191,8 +192,9 @@ func (s *clientSuite) TestClientNewInstanceInvalidParams(c *gc.C) {
img := &imagemetadata.ImageMetadata{
Id: validImageId,
}
server, drive, err := cli.newInstance(params, img)
server, drive, arch, err := cli.newInstance(params, img, nil)
c.Check(server, gc.IsNil)
c.Check(arch, gc.Equals, "")
c.Check(drive, gc.IsNil)
c.Check(err, gc.ErrorMatches, "invalid configuration for new instance")
}
Expand All @@ -215,8 +217,9 @@ func (s *clientSuite) TestClientNewInstanceInvalidTemplate(c *gc.C) {
img := &imagemetadata.ImageMetadata{
Id: "invalid-id",
}
server, drive, err := cli.newInstance(params, img)
server, drive, arch, err := cli.newInstance(params, img, nil)
c.Check(server, gc.IsNil)
c.Check(arch, gc.Equals, "")
c.Check(drive, gc.IsNil)
c.Check(err, gc.ErrorMatches, "query drive template: 404 Not Found.*")
}
Expand Down Expand Up @@ -249,7 +252,7 @@ func (s *clientSuite) TestClientNewInstance(c *gc.C) {
templateDrive := &data.Drive{
Resource: data.Resource{URI: "uri", UUID: cs.driveTemplate},
LibraryDrive: data.LibraryDrive{
Arch: "arch",
Arch: "64",
ImageType: "image-type",
OS: "os",
Paid: true,
Expand All @@ -260,8 +263,10 @@ func (s *clientSuite) TestClientNewInstance(c *gc.C) {
mock.ResetDrives()
mock.LibDrives.Add(templateDrive)

server, drive, err := cli.newInstance(params, img)
server, drive, arch, err := cli.newInstance(params, img, utils.Gzip([]byte{}))
c.Check(server, gc.NotNil)
c.Check(drive, gc.NotNil)
c.Check(arch, gc.NotNil)
fmt.Printf("%v", err)
c.Check(err, gc.IsNil)
}
17 changes: 10 additions & 7 deletions provider/cloudsigma/environ.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ const (

// This file contains the core of the Environ implementation.
type environ struct {
common.SupportsUnitPlacementPolicy
name string

lock sync.Mutex
lock sync.Mutex
archMutex sync.Mutex

ecfg *environConfig
client *environClient
storage *environStorage
ecfg *environConfig
client *environClient
storage *environStorage
supportedArchitectures []string
}

var _ environs.Environ = (*environ)(nil)
Expand Down Expand Up @@ -107,11 +110,11 @@ func (env environ) Storage() storage.Storage {
// Bootstrap is responsible for selecting the appropriate tools,
// and setting the agent-version configuration attribute prior to
// bootstrapping the environment.
func (env *environ) Bootstrap(ctx environs.BootstrapContext, params environs.BootstrapParams) (arch, series string, finalizer environs.BootstrapFinalizer, err error) {
arch, series, finalizer, err = common.Bootstrap(ctx, env, params)
func (env *environ) Bootstrap(ctx environs.BootstrapContext, params environs.BootstrapParams) (string, string, environs.BootstrapFinalizer, error) {
arch, series, finalizer, err := common.Bootstrap(ctx, env, params)

if err != nil {
return
return "", "", nil, err
}

newFinalizer := func(ctx environs.BootstrapContext, mcfg *cloudinit.MachineConfig) (err error) {
Expand Down
25 changes: 13 additions & 12 deletions provider/cloudsigma/environ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,20 @@ func (s *environSuite) TestBase(c *gc.C) {
})

baseConfig := newConfig(c, validAttrs().Merge(testing.Attrs{"name": "testname"}))
environ, err := environs.New(baseConfig)
env, err := environs.New(baseConfig)
c.Assert(err, gc.IsNil)
env.(*environ).supportedArchitectures = []string{arch.AMD64}

cfg := environ.Config()
cfg := env.Config()
c.Assert(cfg, gc.NotNil)
c.Check(cfg.Name(), gc.Equals, "testname")

environstrage, ok := environ.(environs.EnvironStorage)
environstrage, ok := env.(environs.EnvironStorage)
c.Check(environstrage.Storage(), gc.Equals, &emptyStorage)

c.Check(environ.PrecheckInstance("", constraints.Value{}, ""), gc.IsNil)
c.Check(env.PrecheckInstance("", constraints.Value{}, ""), gc.IsNil)

hasRegion, ok := environ.(simplestreams.HasRegion)
hasRegion, ok := env.(simplestreams.HasRegion)
c.Check(ok, gc.Equals, true)
c.Assert(hasRegion, gc.NotNil)

Expand All @@ -66,23 +67,23 @@ func (s *environSuite) TestBase(c *gc.C) {
c.Check(cloudSpec.Region, gc.Not(gc.Equals), "")
c.Check(cloudSpec.Endpoint, gc.Not(gc.Equals), "")

archs, err := environ.SupportedArchitectures()
archs, err := env.SupportedArchitectures()
c.Check(err, gc.IsNil)
c.Assert(archs, gc.NotNil)
c.Assert(archs, gc.HasLen, 1)
c.Check(archs[0], gc.Equals, arch.AMD64)

validator, err := environ.ConstraintsValidator()
validator, err := env.ConstraintsValidator()
c.Check(validator, gc.NotNil)
c.Check(err, gc.IsNil)

c.Check(environ.SupportNetworks(), gc.Equals, false)
c.Check(environ.SupportsUnitPlacement(), gc.IsNil)
c.Check(env.SupportNetworks(), gc.Equals, false)
c.Check(env.SupportsUnitPlacement(), gc.IsNil)

c.Check(environ.OpenPorts(nil), gc.IsNil)
c.Check(environ.ClosePorts(nil), gc.IsNil)
c.Check(env.OpenPorts(nil), gc.IsNil)
c.Check(env.ClosePorts(nil), gc.IsNil)

ports, err := environ.Ports()
ports, err := env.Ports()
c.Check(ports, gc.IsNil)
c.Check(err, gc.IsNil)
}
29 changes: 25 additions & 4 deletions provider/cloudsigma/environcaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,35 @@
package cloudsigma

import (
"github.com/juju/juju/juju/arch"
"github.com/juju/juju/constraints"
"github.com/juju/juju/environs/imagemetadata"
"github.com/juju/juju/environs/simplestreams"
"github.com/juju/juju/network"
"github.com/juju/juju/provider/common"
)

// SupportedArchitectures returns the image architectures which can
// be hosted by this environment.
func (env *environ) SupportedArchitectures() ([]string, error) {
return []string{arch.AMD64}, nil
env.archMutex.Lock()
defer env.archMutex.Unlock()
if env.supportedArchitectures != nil {
return env.supportedArchitectures, nil
}
logger.Debugf("Getting supported architectures from simplestream.")
cloudSpec, err := env.Region()
if err != nil {
return nil, err
}
imageConstraint := imagemetadata.NewImageConstraint(simplestreams.LookupParams{
CloudSpec: cloudSpec,
Stream: env.Config().ImageStream(),
})
env.supportedArchitectures, err = common.SupportedArchitectures(env, imageConstraint)
logger.Debugf("Supported architectures: %v", env.supportedArchitectures)
return env.supportedArchitectures, err
}

func (env *environ) SupportAddressAllocation(netId network.Id) (bool, error) {
return false, nil
}

var unsupportedConstraints = []string{
Expand Down
Loading

0 comments on commit edc2eb5

Please sign in to comment.