Skip to content

Commit

Permalink
Remove the common machine name function, and replace with the instanc…
Browse files Browse the repository at this point in the history
…e namespace hostname.
howbazaar committed May 30, 2016
1 parent 9f94831 commit 38351e4
Showing 22 changed files with 129 additions and 84 deletions.
6 changes: 2 additions & 4 deletions container/kvm/kvm.go
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ import (

"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names"
"github.com/juju/utils/arch"

"github.com/juju/juju/agent"
@@ -79,7 +78,7 @@ func NewContainerManager(conf container.ManagerConfig) (container.Manager, error
if modelUUID == "" {
return nil, errors.Errorf("model UUID is required")
}
namespace, err := instance.NewNamespace(names.NewModelTag(modelUUID))
namespace, err := instance.NewNamespace(modelUUID)
if err != nil {
return nil, errors.Trace(err)
}
@@ -117,8 +116,7 @@ func (manager *containerManager) CreateContainer(
callback container.StatusCallback,
) (_ instance.Instance, _ *instance.HardwareCharacteristics, err error) {

machineTag := names.NewMachineTag(instanceConfig.MachineId)
name, err := manager.namespace.Hostname(machineTag)
name, err := manager.namespace.Hostname(instanceConfig.MachineId)
if err != nil {
return nil, nil, errors.Trace(err)
}
6 changes: 2 additions & 4 deletions container/lxc/lxc.go
Original file line number Diff line number Diff line change
@@ -18,7 +18,6 @@ import (

"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names"
"github.com/juju/utils"
"github.com/juju/utils/arch"
"github.com/juju/utils/keyvalues"
@@ -105,7 +104,7 @@ func newContainerManager(
if modelUUID == "" {
return nil, errors.Errorf("model UUID is required")
}
namespace, err := instance.NewNamespace(names.NewModelTag(modelUUID))
namespace, err := instance.NewNamespace(modelUUID)
if err != nil {
return nil, errors.Trace(err)
}
@@ -200,8 +199,7 @@ func (manager *containerManager) CreateContainer(
}
}(time.Now())

machineTag := names.NewMachineTag(instanceConfig.MachineId)
name, err := manager.namespace.Hostname(machineTag)
name, err := manager.namespace.Hostname(instanceConfig.MachineId)
if err != nil {
return nil, nil, errors.Trace(err)
}
6 changes: 2 additions & 4 deletions container/lxd/lxd.go
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@ import (

"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names"

"github.com/juju/juju/cloudconfig/containerinit"
"github.com/juju/juju/cloudconfig/instancecfg"
@@ -68,7 +67,7 @@ func NewContainerManager(conf container.ManagerConfig) (container.Manager, error
if modelUUID == "" {
return nil, errors.Errorf("model UUID is required")
}
namespace, err := instance.NewNamespace(names.NewModelTag(modelUUID))
namespace, err := instance.NewNamespace(modelUUID)
if err != nil {
return nil, errors.Trace(err)
}
@@ -117,8 +116,7 @@ func (manager *containerManager) CreateContainer(
return
}

machineTag := names.NewMachineTag(instanceConfig.MachineId)
name, err := manager.namespace.Hostname(machineTag)
name, err := manager.namespace.Hostname(instanceConfig.MachineId)
if err != nil {
return nil, nil, errors.Trace(err)
}
4 changes: 1 addition & 3 deletions container/testing/common.go
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@ import (

"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names"
jc "github.com/juju/testing/checkers"
"github.com/juju/version"
gc "gopkg.in/check.v1"
@@ -80,8 +79,7 @@ func CreateContainerWithMachineAndNetworkAndStorageConfig(
) instance.Instance {

if networkConfig != nil && len(networkConfig.Interfaces) > 0 {
machineTag := names.NewMachineTag(instanceConfig.MachineId)
name, err := manager.Namespace().Hostname(machineTag)
name, err := manager.Namespace().Hostname(instanceConfig.MachineId)
c.Assert(err, jc.ErrorIsNil)
EnsureLXCRootFSEtcNetwork(c, name)
}
16 changes: 6 additions & 10 deletions instance/hostname.go
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ type Namespace interface {
Prefix() string
// Hostname returns a name suitable to be used for a machine hostname.
// This function returns an error if the machine tags is invalid.
Hostname(machine names.MachineTag) (string, error)
Hostname(machineID string) (string, error)

// MachineTag does the reverse of the Hostname method, and extracts the
// Tag from the hostname.
@@ -49,26 +49,22 @@ type namespace struct {

// NewNamespace returns a Namespace identified by the last six hex digits of the
// model UUID. NewNamespace returns an error if the model tag is invalid.
func NewNamespace(model names.ModelTag) (Namespace, error) {
uuid := model.Id()
// TODO: would be nice if the tags exported a method Valid().
if !names.IsValidModel(uuid) {
return nil, errors.Errorf("model ID %q is not a valid model", uuid)
func NewNamespace(modelUUID string) (Namespace, error) {
if !names.IsValidModel(modelUUID) {
return nil, errors.Errorf("model ID %q is not a valid model", modelUUID)
}
// The suffix is the last six hex digits of the model uuid.
suffix := uuid[len(uuid)-6:]
suffix := modelUUID[len(modelUUID)-6:]

return &namespace{name: suffix}, nil
}

// Hostname implements Namespace.
func (n *namespace) Hostname(machine names.MachineTag) (string, error) {
machineID := machine.Id()
func (n *namespace) Hostname(machineID string) (string, error) {
if !names.IsValidMachine(machineID) {
return "", errors.Errorf("machine ID %q is not a valid machine", machineID)
}
machineID = strings.Replace(machineID, "/", "-", -1)

return n.Prefix() + machineID, nil
}

9 changes: 0 additions & 9 deletions provider/common/util.go
Original file line number Diff line number Diff line change
@@ -15,12 +15,3 @@ import "fmt"
func EnvFullName(modelUUID string) string {
return fmt.Sprintf("juju-%s", modelUUID)
}

// MachineFullName returns a string based on the provided model
// UUID and machine ID that is suitable for identifying instances
// on a provider. See EnvFullName for an explanation on how this
// function helps juju users.
func MachineFullName(modelUUID, machineID string) string {
modelstr := EnvFullName(modelUUID)
return fmt.Sprintf("%s-machine-%s", modelstr, machineID)
}
18 changes: 14 additions & 4 deletions provider/gce/environ.go
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import (
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/simplestreams"
"github.com/juju/juju/instance"
"github.com/juju/juju/network"
"github.com/juju/juju/provider/common"
"github.com/juju/juju/provider/gce/google"
@@ -64,6 +65,9 @@ type environ struct {
lock sync.Mutex // lock protects access to ecfg
ecfg *environConfig

// namespace is used to create the machine and device hostnames.
namespace instance.Namespace

archLock sync.Mutex // protects supportedArchitectures
supportedArchitectures []string
}
@@ -88,11 +92,17 @@ func newEnviron(cfg *config.Config) (*environ, error) {
if err != nil {
return nil, errors.Trace(err)
}
namespace, err := instance.NewNamespace(cfg.UUID())
if err != nil {
return nil, errors.Trace(err)
}

return &environ{
name: ecfg.config.Name(),
uuid: ecfg.config.UUID(),
ecfg: ecfg,
gce: conn,
name: ecfg.config.Name(),
uuid: ecfg.config.UUID(),
ecfg: ecfg,
gce: conn,
namespace: namespace,
}, nil
}

12 changes: 8 additions & 4 deletions provider/gce/environ_broker.go
Original file line number Diff line number Diff line change
@@ -145,7 +145,10 @@ func (env *environ) findInstanceSpec(
// provisioned, relative to the provided args and spec. Info for that
// low-level instance is returned.
func (env *environ) newRawInstance(args environs.StartInstanceParams, spec *instances.InstanceSpec) (*google.Instance, error) {
machineID := common.MachineFullName(env.Config().UUID(), args.InstanceConfig.MachineId)
hostname, err := env.namespace.Hostname(args.InstanceConfig.MachineId)
if err != nil {
return nil, errors.Trace(err)
}

os, err := series.GetOSFromSeries(args.InstanceConfig.Series)
if err != nil {
@@ -158,7 +161,8 @@ func (env *environ) newRawInstance(args environs.StartInstanceParams, spec *inst
}
tags := []string{
env.globalFirewallName(),
machineID,
env.Config().UUID(),
hostname,
}

disks, err := getDisks(
@@ -173,7 +177,7 @@ func (env *environ) newRawInstance(args environs.StartInstanceParams, spec *inst
// TODO(ericsnow) Support multiple networks?
// TODO(ericsnow) Use a different net interface name? Configurable?
instSpec := google.InstanceSpec{
ID: machineID,
ID: hostname,
Type: spec.InstanceType.Name,
Disks: disks,
NetworkInterfaces: []string{"ExternalNAT"},
@@ -308,7 +312,7 @@ func (env *environ) StopInstances(instances ...instance.Id) error {
ids = append(ids, string(id))
}

prefix := common.MachineFullName(env.Config().UUID(), "")
prefix := env.namespace.Prefix()
err := env.gce.RemoveInstances(prefix, ids...)
return errors.Trace(err)
}
5 changes: 2 additions & 3 deletions provider/gce/environ_instance.go
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ import (
"github.com/juju/juju/constraints"
"github.com/juju/juju/environs"
"github.com/juju/juju/instance"
"github.com/juju/juju/provider/common"
"github.com/juju/juju/provider/gce/google"
)

@@ -74,7 +73,7 @@ var getInstances = func(env *environ) ([]instance.Instance, error) {
// will see they are not tracked in state, assume they're stale/rogue,
// and shut them down.
func (env *environ) instances() ([]instance.Instance, error) {
prefix := common.MachineFullName(env.Config().UUID(), "")
prefix := env.namespace.Prefix()
instances, err := env.gce.Instances(prefix, instStatuses...)
err = errors.Trace(err)

@@ -95,7 +94,7 @@ func (env *environ) instances() ([]instance.Instance, error) {
// ControllerInstances returns the IDs of the instances corresponding
// to juju controllers.
func (env *environ) ControllerInstances() ([]instance.Id, error) {
prefix := common.MachineFullName(env.Config().ControllerUUID(), "")
prefix := env.namespace.Prefix()
instances, err := env.gce.Instances(prefix, instStatuses...)
if err != nil {
return nil, errors.Trace(err)
20 changes: 14 additions & 6 deletions provider/gce/instance.go
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@ import (

"github.com/juju/juju/instance"
"github.com/juju/juju/network"
"github.com/juju/juju/provider/common"
"github.com/juju/juju/provider/gce/google"
"github.com/juju/juju/status"
)
@@ -72,24 +71,33 @@ func findInst(id instance.Id, instances []instance.Instance) instance.Instance {
// should have been started with the given machine id.
func (inst *environInstance) OpenPorts(machineID string, ports []network.PortRange) error {
// TODO(ericsnow) Make sure machineId matches inst.Id()?
name := common.MachineFullName(inst.env.Config().UUID(), machineID)
err := inst.env.gce.OpenPorts(name, ports...)
name, err := inst.env.namespace.Hostname(machineID)
if err != nil {
return errors.Trace(err)
}
err = inst.env.gce.OpenPorts(name, ports...)
return errors.Trace(err)
}

// ClosePorts closes the given ports on the instance, which
// should have been started with the given machine id.
func (inst *environInstance) ClosePorts(machineID string, ports []network.PortRange) error {
name := common.MachineFullName(inst.env.Config().UUID(), machineID)
err := inst.env.gce.ClosePorts(name, ports...)
name, err := inst.env.namespace.Hostname(machineID)
if err != nil {
return errors.Trace(err)
}
err = inst.env.gce.ClosePorts(name, ports...)
return errors.Trace(err)
}

// Ports returns the set of ports open on the instance, which
// should have been started with the given machine id.
// The ports are returned as sorted by SortPorts.
func (inst *environInstance) Ports(machineID string) ([]network.PortRange, error) {
name := common.MachineFullName(inst.env.Config().UUID(), machineID)
name, err := inst.env.namespace.Hostname(machineID)
if err != nil {
return nil, errors.Trace(err)
}
ports, err := inst.env.gce.Ports(name)
return ports, errors.Trace(err)
}
9 changes: 9 additions & 0 deletions provider/lxd/environ.go
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ import (
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/tags"
"github.com/juju/juju/instance"
"github.com/juju/juju/provider/common"
)

@@ -32,6 +33,9 @@ type environ struct {
raw *rawProvider
base baseProvider

// namespace is used to create the machine and device hostnames.
namespace instance.Namespace

lock sync.Mutex
ecfg *environConfig
}
@@ -55,6 +59,11 @@ func newEnviron(cfg *config.Config, newRawProvider newRawProviderFunc) (*environ
return nil, errors.Trace(err)
}

env.namespace, err = instance.NewNamespace(cfg.UUID())
if err != nil {
return nil, errors.Trace(err)
}

//TODO(wwitzel3) make sure we are also cleaning up profiles during destroy
if err := env.initProfile(); err != nil {
return nil, errors.Trace(err)
10 changes: 6 additions & 4 deletions provider/lxd/environ_broker.go
Original file line number Diff line number Diff line change
@@ -17,7 +17,6 @@ import (
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/tags"
"github.com/juju/juju/instance"
"github.com/juju/juju/provider/common"
"github.com/juju/juju/state/multiwatcher"
"github.com/juju/juju/status"
"github.com/juju/juju/tools"
@@ -136,7 +135,10 @@ func (env *environ) getImageSources() ([]lxdclient.Remote, error) {
// provisioned, relative to the provided args and spec. Info for that
// low-level instance is returned.
func (env *environ) newRawInstance(args environs.StartInstanceParams) (*lxdclient.Instance, error) {
machineID := common.MachineFullName(env.Config().UUID(), args.InstanceConfig.MachineId)
hostname, err := env.namespace.Hostname(args.InstanceConfig.MachineId)
if err != nil {
return nil, errors.Trace(err)
}

// Note: other providers have the ImageMetadata already read for them
// and passed in as args.ImageMetadata. However, lxd provider doesn't
@@ -176,7 +178,7 @@ func (env *environ) newRawInstance(args environs.StartInstanceParams) (*lxdclien
// TODO(ericsnow) Support multiple networks?
// TODO(ericsnow) Use a different net interface name? Configurable?
instSpec := lxdclient.InstanceSpec{
Name: machineID,
Name: hostname,
Image: image,
//Type: spec.InstanceType.Name,
//Disks: getDisks(spec, args.Constraints),
@@ -292,7 +294,7 @@ func (env *environ) StopInstances(instances ...instance.Id) error {
ids = append(ids, string(id))
}

prefix := common.MachineFullName(env.Config().UUID(), "")
prefix := env.namespace.Prefix()
err := env.raw.RemoveInstances(prefix, ids...)
return errors.Trace(err)
}
5 changes: 2 additions & 3 deletions provider/lxd/environ_instance.go
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ import (
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/tags"
"github.com/juju/juju/instance"
"github.com/juju/juju/provider/common"
"github.com/juju/juju/tools/lxdclient"
)

@@ -70,7 +69,7 @@ func findInst(id instance.Id, instances []*environInstance) instance.Instance {
// to ensure that only machines for the environment are returned. This
// is necessary to isolate multiple models within the same LXD.
func (env *environ) allInstances() ([]*environInstance, error) {
prefix := common.MachineFullName(env.Config().UUID(), "")
prefix := env.namespace.Prefix()
return env.prefixedInstances(prefix)
}

@@ -95,7 +94,7 @@ func (env *environ) prefixedInstances(prefix string) ([]*environInstance, error)
// ControllerInstances returns the IDs of the instances corresponding
// to juju controllers.
func (env *environ) ControllerInstances() ([]instance.Id, error) {
prefix := common.MachineFullName(env.Config().ControllerUUID(), "")
prefix := env.namespace.Prefix()
instances, err := env.raw.Instances(prefix, lxdclient.AliveStatuses...)
if err != nil {
return nil, errors.Trace(err)
Loading

0 comments on commit 38351e4

Please sign in to comment.