Example #1
0
func OpenTerminalToServer(name string, appConfig *Config) error {
	// load connection data from the configuration file
	server, err := appConfig.GetServer(name)
	if err != nil {
		return err
	}
	uri := server["uri"]
	key, _ := homedir.Expand(server["key"])

	if uri == "" {
		return fmt.Errorf("Missing connection string. Define your server in the configuration file first.")
	}

	// create a new client
	sshConfig := ssh.NewConfig(uri, key, true, true, true)
	client := ssh.New(sshConfig)
	defer client.Disconnect()

	// test the connection
	err = client.TryConnection()
	if err != nil {
		return fmt.Errorf("Unable to connect to server using uri '%s' key '%s' got: %s", uri, key, err)
	}

	// start the terminal shell
	err = client.Shell()
	if err != nil {
		return err
	}
	return nil
}
Example #2
0
/**
Provision a single server with the list of tasks and variables
*/
func ProvisionServer(name string, tasks []map[string]string, checks []map[string]string, variables map[string]string, config *Config) error {
	// load connection data from the configuration file
	server, err := config.GetServer(name)
	if err != nil {
		return err
	}
	uri := server["uri"]
	key, _ := homedir.Expand(server["key"])

	if uri == "" {
		return fmt.Errorf("Missing connection string. Define your server in the configuration file first.")
	}

	// create a new client
	sshConfig := ssh.NewConfig(uri, key, true, true, true)
	client := ssh.New(sshConfig)
	defer client.Disconnect()

	// test the connection
	err = client.TryConnection()
	if err != nil {
		return fmt.Errorf("Unable to connect to server using uri '%s' key '%s' got: %s", uri, key, err)
	}

	// execute tasks on the current server
	err = ExecuteTasksOnServer(client, tasks, variables, config)
	if err != nil {
		return err
	}

	// @todo Execute checks on server
	return nil
}
Example #3
0
func Copy(fromWithHost, toWithHost string, appConfig *Config) error {
	fromHost, fromPath := SplitIdentifierFromPath(fromWithHost)
	toHost, toPath := SplitIdentifierFromPath(toWithHost)

	if fromHost != "" && toHost != "" {
		return fmt.Errorf("Unable to copy between servers yet... Sorry")
	}

	var name string
	if fromHost != "" {
		name = fromHost
	} else {
		name = toHost
	}
	// load connection data from the configuration file
	server, err := appConfig.GetServer(name)
	if err != nil {
		return err
	}
	uri := server["uri"]
	key, _ := homedir.Expand(server["key"])

	// setup new connection to server
	sshConfig := ssh.NewConfig(uri, key, true, true, true)
	client := ssh.New(sshConfig)
	defer client.Disconnect()

	err = client.TryConnection()
	if err != nil {
		return err
	}

	// start copy data transfer
	if toHost != "" {
		err = client.Copy(fromPath, toPath)
	} else {
		err = client.Download(fromPath, toPath)
	}

	if err != nil {
		return fmt.Errorf("Unable to copy from %s to %s: %s\n", fromWithHost, toWithHost, err)
	}
	return nil
}
Example #4
0
func OpenTerminalToServer(name string, appConfig *Config) error {
	var (
		sshConfig *ssh.Config
		uri       string
		key       string
	)

	server, _ := appConfig.GetServer(name)
	if server != nil {
		uri = server["uri"]
		key, _ = homedir.Expand(server["key"])
	}

	if uri == "" {
		sshConfig = ssh.NewConfig(name, "", false, true, true)
	} else {
		// load connection data from the configuration file
		sshConfig = ssh.NewConfig(uri, key, true, true, true)

	}

	client := ssh.New(sshConfig)
	defer client.Disconnect()

	// test the connection
	err := client.TryConnection()
	if err != nil {
		return fmt.Errorf("Unable to connect to server using uri '%s' key '%s' got: %s", uri, key, err)
	}

	// start the terminal shell
	err = client.Shell()
	if err != nil {
		return err
	}
	return nil
}