Esempio n. 1
0
// New gets a path to configuration directory
// path to temporary directory where the vHDD image supposed to be mounted
// and path to vHDD image.
// Returns a pointer to the structure and error/nil
func New(config *Disk, rootfsMp string, bins *Utils, remoteConfig *sshfs.Config) (i *image, err error) {
	i = new(image)
	i.needToFormat = false
	var qemuImgError string

	if remoteConfig == nil {
		i.run = utils.RunFunc(nil)
		i.slashpath = rootfsMp
		i.utils = bins
		qemuImgError = "please install qemu-img"
	} else {
		i.run = utils.RunFunc(remoteConfig.Common)
		i.client, err = sshfs.NewClient(remoteConfig)
		if err != nil {
			err = utils.FormatError(err)
			return
		}
		i.localmount = rootfsMp
		if i.slashpath, err = i.run("mktemp -d --suffix _deployer_rootfs"); err != nil {
			err = utils.FormatError(err)
			return
		}
		if err = i.client.Attach(i.slashpath, i.localmount); err != nil {
			err = utils.FormatError(err)
			return
		}
		if err = setUtilNewPaths(i, bins); err != nil {
			err = utils.FormatError(err)
			return
		}
		qemuImgError = "please install qemu-img on remote host"
	}

	if config.Type != StorageTypeRAW {
		if _, err = i.run("which qemu-img"); err != nil {
			err = utils.FormatError(errors.New(qemuImgError))
			return
		}
		// set temporary name
		config.Path = strings.Replace(config.Path, "."+string(config.Type), "", -1)
	}

	i.config = config
	i.config.Path = config.Path + ".raw"
	if _, err = i.run("ls" + config.Path); err != nil {
		if err = i.create(); err != nil {
			err = utils.FormatError(err)
			return
		}
		if config.Partitions != nil {
			i.needToFormat = true
		}
	}

	i.loopDevice = new(loopDevice)
	i.loopDevice.amountOfMappers = 0
	return
}
Esempio n. 2
0
// NewCollector constructs new lshw Collector
// The output will be represented in JSON format
func NewCollector(sshconf *ssh.Config, lshwpath, hwinfoFile string) (*Collector, error) {
	c := new(Collector)
	c.Run = utils.RunFunc(sshconf)
	c.hwinfoFile = hwinfoFile
	c.prepare = prepareFunc(c, sshconf, lshwpath)
	return c, nil
}
Esempio n. 3
0
func (b *MetadataBuilder) Run() (deployer.Artifact, error) {
	// in case no source template exists apparently we should use the default metadata
	_, err := os.Stat(b.Source)
	if err != nil {
		b.Source = b.Dest
	}

	f, err := ioutil.ReadFile(b.Source)
	if err != nil {
		return nil, utils.FormatError(err)
	}

	data, err := utils.ProcessTemplate(string(f), b.UserData)
	if err != nil {
		return nil, utils.FormatError(err)
	}

	run := utils.RunFunc(b.SshConfig)
	if _, err := run(fmt.Sprintf("echo \"%s\" > %s", data, b.Dest)); err != nil {
		return nil, utils.FormatError(err)
	}
	return &deployer.CommonArtifact{
		Name: filepath.Base(b.Dest),
		Path: b.Dest,
		Type: deployer.MetadataArtifact,
	}, nil
}
Esempio n. 4
0
// Destroy is responsible for removing appropriate artifact.
func (a *CommonArtifact) Destroy() error {
	run := utils.RunFunc(a.SshConfig)
	if _, err := run("rm " + a.Path); err != nil {
		return utils.FormatError(err)
	}
	return nil
}
Esempio n. 5
0
func NewCollector(config *ssh.Config) *Collector {
	c := new(Collector)
	c.run = utils.RunFunc(config)
	return c
}
Esempio n. 6
0
func NewDriver(config *ssh.Config) *Driver {
	d := new(Driver)
	d.Run = utils.RunFunc(config)
	return d
}
Esempio n. 7
0
func UiSshConfig(ui *gui.DialogUi) (*sshconf.Config, error) {
	cfg := new(sshconf.Config)
	cfg.Port = "22"
	cfg.User = "******"
	origlist := []string{"IP      : ", "1", "1", "", "1", "10", "22", "0", "0",
		"SSH Port: ", "2", "1", cfg.Port, "2", "10", "22", "0", "0",
		"Username: "******"3", "1", cfg.User, "3", "10", "22", "0", "0"}

MainLoop:
	for {
		ui.HelpButton(true)
		ui.SetHelpLabel("Back")
		reslist, err := ui.Mixedform("Remote session configuration", false, origlist[0:]...)
		if err != nil {
			return nil, err
		}
		if len(reslist) < 3 {
			continue
		}
		if net.ParseIP(reslist[0]) == nil {
			continue
		}
		cfg.Host = reslist[0]

		portDig, err := strconv.Atoi(reslist[1])
		if err != nil {
			return nil, utils.FormatError(err)
		}
		if portDig > 65535 {
			continue
		}

	AuthLoop:
		for {
			ui.SetTitle("Authentication method")
			ui.SetSize(9, 18)
			ui.HelpButton(true)
			ui.SetHelpLabel("Back")
			val, err := ui.Menu(2, "1", "Password", "2", "Private key")
			if err != nil {
				switch err.Error() {
				case gui.DialogMoveBack:
					continue MainLoop
				case gui.DialogExit:
					os.Exit(1)
				}
			}

			switch val {
			case "1":
				cfg.Password, err = ui.GetPasswordFromInput(cfg.Host, cfg.User, "Back", "", false)
			case "2":
				cfg.PrvtKeyFile, err = ui.GetPathToFileFromInput("Path to ssh private key file", "Back", "")
			}
			if err != nil {
				switch err.Error() {
				case gui.DialogMoveBack:
					continue AuthLoop
				case gui.DialogExit:
					os.Exit(1)
				}
			}
			break MainLoop
		}
	}

	run := utils.RunFunc(cfg)
	errCh := make(chan error)
	defer close(errCh)
	go func() {
		// verifying that user is able execute a command by using sudo
		_, err := run("uname")
		errCh <- err
	}()

	if err := ui.Wait("Trying to establish SSH connection to remote host.\nPlease wait...", time.Second*1, time.Second*5, errCh); err != nil {
		ui.Output(gui.Warning, "Unable to establish SSH connection.", "Press <OK> to return to menu.")
		goto MainLoop
	}
	return cfg, nil
}