예제 #1
0
func createCluster(cmd *cobra.Command, args []string) {
	createClusterFlags.VaultAddress = vaultCfg.VaultAddr
	createClusterFlags.VaultCertificatePath = vaultCfg.VaultCACert

	requireProfile := false
	loadArgumentsFromCluster(cmd.Flags(), requireProfile)
	clusterInfoFromArgs(&createClusterFlags.ClusterInfo, args)

	provider := newProvider()
	createClusterFlags = provider.CreateClusterDefaults(createClusterFlags)

	// Create cluster ID if needed
	if createClusterFlags.ID == "" {
		createClusterFlags.ID = strings.ToLower(uniuri.NewLen(40))
	} else {
		createClusterFlags.ID = strings.ToLower(createClusterFlags.ID)
	}

	// Create weave password if needed
	if createClusterFlags.WeavePassword == "" {
		createClusterFlags.WeavePassword = uniuri.NewLen(40)
	}

	// Validate
	if err := createClusterFlags.Validate(); err != nil {
		Exitf("Create failed: %s\n", err.Error())
	}

	// See if there are already instances for the given cluster
	instances, err := provider.GetInstances(createClusterFlags.ClusterInfo)
	if err != nil {
		Exitf("Failed to query existing instances: %v\n", err)
	}
	if len(instances) > 0 {
		Exitf("Cluster %s.%s already exists.\n", createClusterFlags.Name, createClusterFlags.Domain)
	}

	// Confirm
	if err := confirm(fmt.Sprintf("Are you sure you want to create a %d instance cluster of %s?", createClusterFlags.InstanceCount, createClusterFlags.InstanceConfig)); err != nil {
		Exitf("%v\n", err)
	}

	// Create
	err = provider.CreateCluster(log, createClusterFlags, newDnsProvider())
	if err != nil {
		Exitf("Failed to create new cluster: %v\n", err)
	}

	// Update all members
	reboot := true
	if err := providers.UpdateClusterMembers(log, createClusterFlags.ClusterInfo, reboot, nil, provider); err != nil {
		Exitf("Failed to update cluster members: %v\n", err)
	}

	Infof("Cluster created with ID: %s\n", createClusterFlags.ID)
}
예제 #2
0
func destroyInstance(cmd *cobra.Command, args []string) {
	requireProfile := false
	loadArgumentsFromCluster(cmd.Flags(), requireProfile)
	clusterInstanceInfoFromArgs(&destroyInstanceFlags, args)

	provider := newProvider()
	destroyInstanceFlags.ClusterInfo = provider.ClusterDefaults(destroyInstanceFlags.ClusterInfo)

	if destroyInstanceFlags.Domain == "" {
		Exitf("Please specify a domain\n")
	}
	if destroyInstanceFlags.Name == "" {
		Exitf("Please specify a name\n")
	}
	if destroyInstanceFlags.Prefix == "" {
		Exitf("Please specify a prefix\n")
	}
	if err := confirm(fmt.Sprintf("Are you sure you want to destroy %s?", destroyInstanceFlags.String())); err != nil {
		Exitf("%v\n", err)
	}

	// Remove instance from etcd
	instances, err := provider.GetInstances(destroyInstanceFlags.ClusterInfo)
	if err != nil {
		Exitf("Failed to list instances: %v\n", err)
	}
	toRemove, err := instances.InstanceByName(destroyInstanceFlags.String())
	if err != nil {
		Exitf("Failed to find instance '%s'\n", destroyInstanceFlags.String())
	}
	machineID, err := toRemove.GetMachineID(log)
	if err != nil {
		Exitf("Failed to query machine id for instance: %#v", err)
	}
	isEtcdProxy, err := toRemove.IsEtcdProxy(log)
	if err != nil {
		Exitf("Failed to query etcd mode for instance: %#v", err)
	}
	if !isEtcdProxy {
		remainingInstances := instances.Except(toRemove)
		if err := remainingInstances.RemoveEtcdMember(log, toRemove.Name, toRemove.ClusterIP); err != nil {
			Exitf("Failed to remove instance '%s' from ETCD\n", destroyInstanceFlags.String())
		}
	}

	if err := provider.DeleteInstance(destroyInstanceFlags, newDnsProvider()); err != nil {
		Exitf("Failed to destroy instance: %v\n", err)
	}

	// Update existing members
	if err := providers.UpdateClusterMembers(log, destroyInstanceFlags.ClusterInfo, false, nil, provider); err != nil {
		Exitf("Failed to update cluster members: %v\n", err)
	}

	// Remove machine from vault
	if err := newVaultProvider().RemoveMachine(machineID); err != nil {
		log.Warningf("Failed to remove machine from vault: %#v", err)
	}

	Infof("Destroyed instance %s\n", destroyInstanceFlags)
}
예제 #3
0
func createInstance(cmd *cobra.Command, args []string) {
	createInstanceFlags.VaultAddress = vaultCfg.VaultAddr
	createInstanceFlags.VaultCertificatePath = vaultCfg.VaultCACert

	requireProfile := true
	loadArgumentsFromCluster(cmd.Flags(), requireProfile)
	clusterInfoFromArgs(&createInstanceFlags.ClusterInfo, args)

	provider := newProvider()
	createInstanceFlags = provider.CreateInstanceDefaults(createInstanceFlags)
	createInstanceFlags.SetupNames("", createInstanceFlags.Name, createInstanceFlags.Domain)

	// Validate
	validateVault := false
	if err := createInstanceFlags.Validate(validateVault); err != nil {
		Exitf("Create failed: %s\n", err.Error())
	}

	// See if there are already instances for the given cluster
	instances, err := provider.GetInstances(createInstanceFlags.ClusterInfo)
	if err != nil {
		Exitf("Failed to query existing instances: %v\n", err)
	}
	if len(instances) == 0 {
		Exitf("Cluster %s.%s does not exist.\n", createInstanceFlags.Name, createInstanceFlags.Domain)
	}

	// Fetch cluster ID
	clusterID, err := instances.GetClusterID(log)
	if err != nil {
		Exitf("Failed to get cluster-id: %v\n", err)
	}
	createInstanceFlags.ID = clusterID

	// Fetch vault address
	vaultAddr, err := instances.GetVaultAddr(log)
	if err != nil {
		Exitf("Failed to get vault-addr: %v\n", err)
	}
	createInstanceFlags.VaultAddress = vaultAddr

	// Fetch vault CA certificate
	vaultCACert, err := instances.GetVaultCrt(log)
	if err != nil {
		Exitf("Failed to get vault-cacert: %v\n", err)
	}
	createInstanceFlags.SetVaultCertificate(vaultCACert)

	// Fetch weave.env
	weaveEnv, err := instances.GetWeaveEnv(log)
	if err != nil {
		Exitf("Failed to get weave.env: %v\n", err)
	}
	createInstanceFlags.WeaveEnv = weaveEnv

	// Fetch weave-seed
	weaveSeed, err := instances.GetWeaveSeed(log)
	if err != nil {
		Exitf("Failed to get weave-seed: %v\n", err)
	}
	createInstanceFlags.WeaveSeed = weaveSeed

	// Setup instance index
	if createInstanceFlags.InstanceIndex == 0 {
		createInstanceFlags.InstanceIndex = len(instances) + 1
	}

	// Check tinc IP (if any)
	if createInstanceFlags.TincIpv4 != "" {
		for _, i := range instances {
			if i.ClusterIP == createInstanceFlags.TincIpv4 {
				Exitf("Duplicate cluster IP: %s\n", createInstanceFlags.TincIpv4)
			}
		}
	}

	// Now validate everything
	validateVault = true
	if err := createInstanceFlags.Validate(validateVault); err != nil {
		Exitf("Create failed: %s\n", err.Error())
	}

	// Create
	log.Infof("Creating new instance on %s.%s", createInstanceFlags.Name, createInstanceFlags.Domain)
	instance, err := provider.CreateInstance(log, createInstanceFlags, newDnsProvider())
	if err != nil {
		Exitf("Failed to create new instance: %v\n", err)
	}

	// Get the id of the new machine
	machineID, err := instance.GetMachineID(log)
	if err != nil {
		Exitf("Failed to get machine ID: %v\n", err)
	}

	// Add new instance to ETCD (if not a proxy)
	if !createInstanceFlags.EtcdProxy {
		if err := instances.AddEtcdMember(log, machineID, instance.ClusterIP); err != nil {
			Exitf("Failed to add new instance to etcd: %v\n", err)
		}
	}

	// Add new instance to vault cluster
	if err := newVaultProvider().AddMachine(clusterID, machineID); err != nil {
		log.Warningf("Failed to add machine to vault: %#v", err)
	}

	// Add new instance to list
	instances = append(instances, instance)

	// Load cluster-members data
	isEtcdProxy := func(i providers.ClusterInstance) (bool, error) {
		if i.ClusterIP == instance.ClusterIP {
			return createInstanceFlags.EtcdProxy, nil
		}
		result, err := i.IsEtcdProxy(log)
		return result, maskAny(err)
	}
	clusterMembers, err := instances.AsClusterMemberList(log, isEtcdProxy)
	if err != nil {
		Exitf("Failed to convert instance list to member list: %v\n", err)
	}

	// Perform initial setup on new instance
	iso := providers.InitialSetupOptions{
		ClusterMembers:   clusterMembers,
		FleetMetadata:    createInstanceFlags.CreateFleetMetadata(createInstanceFlags.InstanceIndex),
		EtcdClusterState: "existing",
	}
	if err := instance.InitialSetup(log, createInstanceFlags, iso, provider); err != nil {
		Exitf("Failed to perform initial instance setup: %v\n", err)
	}

	// Update existing members
	if err := providers.UpdateClusterMembers(log, createInstanceFlags.ClusterInfo, false, isEtcdProxy, provider); err != nil {
		Exitf("Failed to update cluster members: %v\n", err)
	}

	// Reboot new instance
	if err := provider.RebootInstance(instance); err != nil {
		Exitf("Failed to reboot new instance: %v\n", err)
	}

	Infof("Instance created\n")
}