Esempio n. 1
0
// CollectData collects the data and returns an error if any.
func CollectData() (Data, error) {
	d := make(Data)
	if fileutil.Exists("/etc/lsb-release") {
		f, err := os.Open("/etc/lsb-release")
		if err != nil {
			return d, err
		}
		defer f.Close()
		s := bufio.NewScanner(f)
		for s.Scan() {
			l := strings.Split(s.Text(), "=")
			if len(l) == 2 {
				k, v := l[0], l[1]
				switch k {
				case "DISTRIB_ID":
					d["id"] = v
				case "DISTRIB_RELEASE":
					d["release"] = v
				case "DISTRIB_CODENAME":
					d["codename"] = v
				case "DISTRIB_DESCRIPTION":
					d["description"] = strings.Trim(v, `"`)
				}
			}
		}
		if err := s.Err(); err != nil {
			return d, err
		}
		return d, nil
	}

	if fileutil.Exists("/usr/bin/lsb_release") {
		out, err := exec.Command("/usr/bin/lsb_release", "-a").Output()
		if err != nil {
			return d, err
		}
		lines := strings.Split(string(out), "\n")
		for _, line := range lines {
			l := strings.Split(line, ":")
			if len(l) == 2 {
				k, v := l[0], strings.TrimSpace(l[1])
				switch k {
				case "Distributor ID":
					d["id"] = v
				case "Release":
					d["release"] = v
				case "Codename":
					d["codename"] = v
				case "Description":
					d["description"] = v
				}
			}
		}
		return d, nil
	}

	return nil, errors.New("cannot find /etc/lsb-release or /usr/bin/lsb_release")
}
Esempio n. 2
0
// CollectData collects the data and returns an error if any.
func CollectData() (Data, error) {
	d := make(Data)
	basePath := "/sys/block/"
	if !fileutil.Exists(basePath) {
		return nil, errors.New("blockdevice: cannot find /sys/block/ directory")
	}
	f, err := os.Open(basePath)
	if err != nil {
		return nil, err
	}
	defer f.Close()
	devices, err := f.Readdirnames(-1)
	if err != nil {
		return nil, err
	}

	for _, device := range devices {
		d[device] = make(map[string]string)
		m := d[device].(map[string]string) // map for a device
		devicePath := filepath.Join(basePath, device)
		for _, key := range [...]string{"size", "removable"} {
			p := filepath.Join(devicePath, key)
			if fileutil.Exists(p) {
				b, err := ioutil.ReadFile(p)
				if err != nil {
					return nil, err
				}
				m[key] = strings.TrimSpace(string(b))
			}
		}

		for _, key := range [...]string{"model", "rev", "state", "timeout", "vendor"} {
			p := filepath.Join(devicePath, "device", key)
			if fileutil.Exists(p) {
				b, err := ioutil.ReadFile(p)
				if err != nil {
					return nil, err
				}
				m[key] = strings.TrimSpace(string(b))
			}
		}

		p := filepath.Join(devicePath, "queue", "rotational")
		if fileutil.Exists(p) {
			b, err := ioutil.ReadFile(p)
			if err != nil {
				return nil, err
			}
			m["rotational"] = strings.TrimSpace(string(b))
		}
	}
	return d, nil
}
Esempio n. 3
0
// Save saves the config in a configuration file.
// If it already exists, it removes it and writes it freshly.
// Make sure you lock and unlock the config while calling Save.
func (c *Config) Save() error {
	if fileutil.Exists(configPath) {
		if err := os.Remove(configPath); err != nil {
			return err
		}
	}

	f, err := os.Create(configPath)
	if err != nil {
		return err
	}
	// Brad Fitzpatrick:
	// Never defer a file Close when the file was opened for writing.
	// Many filesystems do their real work (and thus their real failures) on close.
	// You can defer a file.Close for Read, but not for write.

	var out bytes.Buffer
	b, err := json.MarshalIndent(c, "", "  ")
	if err != nil {
		return err
	}
	out.Write(b)
	out.WriteTo(f)

	if err := f.Close(); err != nil {
		return err
	}

	return nil
}
Esempio n. 4
0
// Save saves the config in a configuration file.
// If it already exists, it removes it and writes it freshly.
func (c *Config) Save() error {
	if fileutil.Exists(configPath) {
		if err := os.Remove(configPath); err != nil {
			return err
		}
	}

	f, err := os.Create(configPath)
	if err != nil {
		return err
	}
	// Brad Fitzpatrick:
	// Never defer a file Close when the file was opened for writing.
	// Many filesystems do their real work (and thus their real failures) on close.
	// You can defer a file.Close for Read, but not for write.

	enc := json.NewEncoder(f)
	if err := enc.Encode(c); err != nil {
		return err
	}

	if err := f.Close(); err != nil {
		return err
	}

	return nil
}
Esempio n. 5
0
func rubyData(d map[string]interface{}) {
	rvars := map[string]string{
		"platform":      "RUBY_PLATFORM",
		"version":       "RUBY_VERSION",
		"release_date":  "RUBY_RELEASE_DATE",
		"target":        "RbConfig::CONFIG['target']",
		"target_cpu":    "RbConfig::CONFIG['target_cpu']",
		"target_vendor": "RbConfig::CONFIG['target_vendor']",
		"target_os":     "RbConfig::CONFIG['target_os']",
		"host":          "RbConfig::CONFIG['host']",
		"host_cpu":      "RbConfig::CONFIG['host_cpu']",
		"host_os":       "RbConfig::CONFIG['host_os']",
		"host_vendor":   "RbConfig::CONFIG['host_vendor']",
		"bin_dir":       "RbConfig::CONFIG['bindir']",
		"ruby_bin":      "::File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])",
	}
	d["ruby"] = make(map[string]string)
	m := d["ruby"].(map[string]string)
	for k, v := range rvars {
		t := fmt.Sprintf(`require "rbconfig"; puts %s`, v)
		if out, err := exec.Command("ruby", "-e", t).Output(); err == nil {
			m[k] = strings.TrimSpace(string(out))
		}
	}
	if out, err := exec.Command("ruby", "-e", `require "rubygems"; puts Gem::default_exec_format % "gem"`).Output(); err == nil {
		g := strings.TrimSpace(string(out))
		var gemBin string
		if p := filepath.Join(m["bin_dir"], g); fileutil.Exists(p) {
			gemBin = p
		} else if p := filepath.Join(m["bin_dir"], "gem"); fileutil.Exists(p) {
			gemBin = p
		}
		if gemBin != "" {
			m["gem_bin"] = gemBin

			// TODO: A bit of a doubt. Check gems_dir once.
			if out, err := exec.Command(m["ruby_bin"], gemBin, "env", "gemdir").Output(); err == nil {
				m["gems_dir"] = strings.TrimSpace(string(out))
			}
		}
	}
}
Esempio n. 6
0
func mountData(d Data) error {
	out, err := exec.Command("mount").Output()
	if err != nil {
		return err
	}

	lines := strings.Split(string(out), "\n")
	for _, line := range lines {
		// proc on /proc type proc (rw,noexec,nosuid,nodev)

		a := strings.Fields(line)
		if len(a) >= 6 {
			if _, ok := d[a[0]]; !ok {
				d[a[0]] = make(map[string]interface{})
			}
			m := d[a[0]].(map[string]interface{})
			m["mount"] = a[2]
			m["fs_type"] = a[4]
			m["mount_options"] = strings.Split(strings.Trim(a[5], "()"), ",")
		}
	}

	// Get missing mount info from /proc/mounts

	if fileutil.Exists("/proc/mounts") {
		f, err := os.Open("/proc/mounts")
		if err != nil {
			return err
		}
		defer f.Close()
		s := bufio.NewScanner(f)
		for s.Scan() {
			a := strings.Fields(s.Text())
			if len(a) >= 4 {
				if _, ok := d[a[0]]; !ok {
					d[a[0]] = map[string]interface{}{
						"mount":         a[1],
						"fs_type":       a[2],
						"mount_options": strings.Split(a[3], ","),
					}
				}
			}
		}
		if err := s.Err(); err != nil {
			return err
		}
	}
	return nil
}
Esempio n. 7
0
func TestInitNew(t *testing.T) {
	configPath = filepath.Join(os.TempDir(), "recond_fake_new_config") // doesn't exist
	c, err := Init()
	if err != nil {
		t.Error(err)
	}
	if !fileutil.Exists(configPath) {
		t.Errorf("Init didn't create the config file")
	}
	if c.UID == "" {
		t.Errorf("got config UID as an empty string; want a non empty string")
	}
	if err := os.Remove(configPath); err != nil {
		t.Error(err)
	}
}
Esempio n. 8
0
// Init initializes and returns a Config. i.e. if the config file doesn't exist,
// it generates an new UID, creates the config file and returns the corresponding Config.
func Init() (*Config, error) {
	if fileutil.Exists(configPath) {
		f, err := os.Open(configPath)
		if err != nil {
			return nil, err
		}
		defer f.Close()
		return parseConfig(f)
	}
	uid, err := generateUID()
	if err != nil {
		return nil, err
	}
	c := &Config{
		UID: uid,
	}
	err = c.Save()
	return c, err
}
Esempio n. 9
0
// ipv6Enabled returns true if IPv6 is enabled
// on the machine.
func ipv6Enabled() bool {
	return fileutil.Exists("/proc/net/if_inet6")
}