Beispiel #1
0
func loadDsHostFqdn(reader io.Reader, logger *log.Logger) (*mdb.Mdb, error) {
	type machineType struct {
		Fqdn string
	}

	type dataCentreType map[string]machineType

	type inMdbType map[string]dataCentreType

	var inMdb inMdbType
	var outMdb mdb.Mdb
	decoder := json.NewDecoder(reader)
	if err := decoder.Decode(&inMdb); err != nil {
		return nil, errors.New("Error decoding: " + err.Error())
	}
	for dsName, dataCentre := range inMdb {
		for machineName, inMachine := range dataCentre {
			var outMachine mdb.Machine
			if inMachine.Fqdn == "" {
				outMachine.Hostname = machineName + "." + dsName
			} else {
				outMachine.Hostname = inMachine.Fqdn
			}
			outMdb.Machines = append(outMdb.Machines, outMachine)
		}
	}
	return &outMdb, nil
}
Beispiel #2
0
func loadCis(reader io.Reader, logger *log.Logger) (*mdb.Mdb, error) {
	type sourceType struct {
		Name string
	}

	type hitType struct {
		Source sourceType `json:"_source"`
	}

	type hitListType struct {
		Hits []hitType
	}

	type inMdbType struct {
		Hits hitListType
	}

	var inMdb inMdbType
	var outMdb mdb.Mdb
	decoder := json.NewDecoder(reader)
	if err := decoder.Decode(&inMdb); err != nil {
		return nil, errors.New("Error decoding: " + err.Error())
	}
	for _, hit := range inMdb.Hits.Hits {
		var outMachine mdb.Machine
		outMachine.Hostname = hit.Source.Name
		outMdb.Machines = append(outMdb.Machines, outMachine)
	}
	return &outMdb, nil
}
Beispiel #3
0
func loadText(reader io.Reader, logger *log.Logger) (*mdb.Mdb, error) {
	scanner := bufio.NewScanner(reader)
	var newMdb mdb.Mdb
	for scanner.Scan() {
		fields := strings.Fields(scanner.Text())
		if len(fields) > 0 {
			if fields[0][0] == '#' {
				continue
			}
			var machine mdb.Machine
			machine.Hostname = fields[0]
			if len(fields) > 1 {
				machine.RequiredImage = fields[1]
				if len(fields) > 2 {
					machine.PlannedImage = fields[2]
				}
			}
			newMdb.Machines = append(newMdb.Machines, machine)
		}
	}
	if err := scanner.Err(); err != nil {
		return nil, err
	}
	return &newMdb, nil
}
Beispiel #4
0
func loadFromAll(generators []generator, datacentre string,
	logger *log.Logger) (*mdb.Mdb, error) {
	machineMap := make(map[string]mdb.Machine)
	startTime := time.Now()
	var rusageStart, rusageStop syscall.Rusage
	syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStart)
	for _, gen := range generators {
		mdb, err := gen.Generate(datacentre, logger)
		if err != nil {
			return nil, err
		}
		for _, machine := range mdb.Machines {
			if oldMachine, ok := machineMap[machine.Hostname]; ok {
				oldMachine.UpdateFrom(machine)
				machineMap[machine.Hostname] = oldMachine
			} else {
				machineMap[machine.Hostname] = machine
			}
		}
	}
	var newMdb mdb.Mdb
	for _, machine := range machineMap {
		newMdb.Machines = append(newMdb.Machines, machine)
	}
	syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStop)
	loadTimeDistribution.Add(time.Since(startTime))
	loadCpuTimeDistribution.Add(time.Duration(
		rusageStop.Utime.Sec)*time.Second +
		time.Duration(rusageStop.Utime.Usec)*time.Microsecond -
		time.Duration(rusageStart.Utime.Sec)*time.Second -
		time.Duration(rusageStart.Utime.Usec)*time.Microsecond)
	return &newMdb, nil
}
Beispiel #5
0
func selectHosts(inMdb *mdb.Mdb, hostnameRE *regexp.Regexp) *mdb.Mdb {
	if hostnameRE == nil {
		return inMdb
	}
	var outMdb mdb.Mdb
	for _, machine := range inMdb.Machines {
		if hostnameRE.MatchString(machine.Hostname) {
			outMdb.Machines = append(outMdb.Machines, machine)
		}
	}
	return &outMdb
}
Beispiel #6
0
func loadCis(reader io.Reader, datacentre string, logger *log.Logger) (
	*mdb.Mdb, error) {

	type instanceMetadataType struct {
		RequiredImage  string `json:"required_image"`
		PlannedImage   string `json:"planned_image"`
		DisableUpdates bool   `json:"disable_updates"`
		OwnerGroup     string `json:"owner_group"`
	}

	type sourceType struct {
		HostName         string               `json:"host_name"`
		InstanceMetadata instanceMetadataType `json:"instance_metadata"`
		Fqdn             string
	}

	type hitType struct {
		Source sourceType `json:"_source"`
	}

	type hitListType struct {
		Hits []hitType
	}

	type inMdbType struct {
		Hits hitListType
	}

	var inMdb inMdbType
	var outMdb mdb.Mdb
	decoder := json.NewDecoder(reader)
	if err := decoder.Decode(&inMdb); err != nil {
		return nil, errors.New("Error decoding: " + err.Error())
	}
	for _, hit := range inMdb.Hits.Hits {
		var outMachine mdb.Machine
		if hit.Source.Fqdn != "" {
			outMachine.Hostname = hit.Source.Fqdn
		} else {
			outMachine.Hostname = hit.Source.HostName
		}
		outMachine.RequiredImage = hit.Source.InstanceMetadata.RequiredImage
		outMachine.PlannedImage = hit.Source.InstanceMetadata.PlannedImage
		outMachine.DisableUpdates = hit.Source.InstanceMetadata.DisableUpdates
		outMachine.OwnerGroup = hit.Source.InstanceMetadata.OwnerGroup
		outMdb.Machines = append(outMdb.Machines, outMachine)
	}
	return &outMdb, nil
}
Beispiel #7
0
func loadFromAll(sources []source, logger *log.Logger) (*mdb.Mdb, error) {
	var newMdb mdb.Mdb
	hostMap := make(map[string]struct{})
	for _, source := range sources {
		mdb, err := loadMdb(source.driverFunc, source.url, logger)
		if err != nil {
			return nil, err
		}
		for _, machine := range mdb.Machines {
			if _, ok := hostMap[machine.Hostname]; !ok {
				newMdb.Machines = append(newMdb.Machines, machine)
				hostMap[machine.Hostname] = struct{}{}
			}
		}
	}
	return &newMdb, nil
}
Beispiel #8
0
func extractMdb(output *ec2.DescribeInstancesOutput) *mdb.Mdb {
	var result mdb.Mdb
	for _, reservation := range output.Reservations {
		for _, instance := range reservation.Instances {
			if instance.PrivateDnsName != nil {
				machine := mdb.Machine{
					Hostname: *instance.PrivateDnsName,
					AwsMetadata: &mdb.AwsMetadata{
						InstanceId: *instance.InstanceId,
					},
				}
				if instance.PrivateIpAddress != nil {
					machine.IpAddress = *instance.PrivateIpAddress
				}
				extractTags(instance.Tags, &machine)
				result.Machines = append(result.Machines, machine)
			}
		}
	}
	return &result
}
Beispiel #9
0
func showMdb(mdb *mdb.Mdb) {
	mdb.DebugWrite(os.Stdout)
	fmt.Println()
}