Ejemplo n.º 1
0
// handleErrorMsg send the error message of an app if present.
func handleErrorMsg(request *api.Request) (interface{}, error) {
	// Map the data to the custom type.
	var data api.RequestErrorMsg
	err := request.MapTo(&data)
	if err != nil {
		return nil, err
	}

	// Validate.
	if len(data.Name) == 0 {
		return nil, fmt.Errorf("missing or invalid data: %+v", data)
	}

	// Obtain the app with the given name.
	a, err := apps.Get(data.Name)
	if err != nil {
		return nil, fmt.Errorf("failed to get error message: %v", err)
	}

	// Create the response value.
	res := api.ResponseErrorMsg{
		Name: data.Name,
	}

	// Get the app error if present.
	appErr := a.Error()
	if appErr != nil {
		res.ErrorMessage = strings.TrimSpace(appErr.Error())
	}

	return res, nil
}
Ejemplo n.º 2
0
// handleRestoreBackup handles the restore backup request.
func handleRestoreBackup(request *api.Request) (interface{}, error) {
	// Map the data to the custom type.
	var data api.RequestRestoreBackup
	err := request.MapTo(&data)
	if err != nil {
		return nil, err
	}

	// Validate.
	if len(data.Name) == 0 || len(data.Unix) == 0 {
		return nil, fmt.Errorf("missing or invalid data: %+v", data)
	}

	// Obtain the app with the given name.
	a, err := apps.Get(data.Name)
	if err != nil {
		return nil, fmt.Errorf("failed to restore backup: %v", err)
	}

	// Restore the backup.
	err = a.RestoreBackup(data.Unix)
	if err != nil {
		return nil, fmt.Errorf("failed to restore backup: %v", err)
	}

	return nil, nil
}
Ejemplo n.º 3
0
// handleSetupSet obtains the setup values and configures the app.
func handleSetupSet(request *api.Request) (interface{}, error) {
	// Map the data to the custom type.
	var data api.RequestSetupSet
	err := request.MapTo(&data)
	if err != nil {
		return nil, err
	}

	// Validate.
	if len(data.Name) == 0 {
		return nil, fmt.Errorf("missing or invalid data: %+v", data)
	}

	// Obtain the app with the given name.
	a, err := apps.Get(data.Name)
	if err != nil {
		return nil, fmt.Errorf("failed to setup app: %v", err)
	}

	// Get the response setup data from the app.
	err = a.Setup(&data.Setup)
	if err != nil {
		return nil, fmt.Errorf("failed to setup app: %v", err)
	}

	return nil, nil
}
Ejemplo n.º 4
0
// handleLogs send the log message of an app.
func handleLogs(request *api.Request) (interface{}, error) {
	// Map the data to the custom type.
	var data api.RequestLogs
	err := request.MapTo(&data)
	if err != nil {
		return nil, err
	}

	// Validate.
	if len(data.Name) == 0 {
		return nil, fmt.Errorf("missing or invalid data: %+v", data)
	}

	// Obtain the app with the given name.
	a, err := apps.Get(data.Name)
	if err != nil {
		return nil, fmt.Errorf("failed to get log message: %v", err)
	}

	// Create the response value.
	res := api.ResponseLogs{}

	// If no specific container is passed,
	// then send a list of all available containers.
	if len(data.Container) == 0 {
		res.Containers, err = a.Containers()
		if err != nil {
			return nil, fmt.Errorf("failed to get app containers: %v", err)
		}
	} else {
		var streamType docker.StdStream = docker.StdStreamCombined

		// Get the stream option.
		if len(data.Stream) > 0 {
			if data.Stream == "stderr" {
				streamType = docker.StdStreamError
			} else if data.Stream == "stdout" {
				streamType = docker.StdStreamOutput
			} else {
				return nil, fmt.Errorf("failed to get log messages: invalid stream option.")
			}
		}

		// Get the log messages.
		logs, err := docker.Logs(a.ContainerNamePrefix()+data.Container, streamType)
		if err != nil {
			return nil, fmt.Errorf("failed to get log messages: %v", err)
		}

		// Set the response log messages.
		res.LogMessages = logs
	}

	return res, nil
}
Ejemplo n.º 5
0
// handleHostFingerprintInfo obtains the host fingerprint and some information about it.
func handleHostFingerprintInfo(request *api.Request) (interface{}, error) {
	// Map the data to the custom type.
	var data api.RequestHostFingerprintInfo
	err := request.MapTo(&data)
	if err != nil {
		return nil, err
	}

	// Be sure it is a valid stripped host.
	data.Host = utils.GetHostFromUrl(data.Host)

	// Validate.
	if len(data.Host) == 0 {
		return nil, fmt.Errorf("invalid host: '%s'", data.Host)
	}

	// Skip host fingerprint checking if this is a local repository.
	if strings.HasPrefix(data.Host, "file://") {
		// Create the response value.
		response := &api.ResponseHostFingerprintInfo{
			Host:        data.Host,
			Trusted:     true,
			Fingerprint: "",
		}

		return response, nil
	}

	// Check if the fingerprint exists for the host.
	exists, err := hostFingerprintExists(data.Host)
	if err != nil {
		return nil, fmt.Errorf("failed to get information about host fingerprint: %v", err)
	}

	// Obtain the fingerprint of the host.
	fingerprint, err := getSshHostFingerprint(data.Host)
	if err != nil {
		return nil, fmt.Errorf("failed to obtain host fingerprint: %v", err)
	}

	// Create the response value.
	response := &api.ResponseHostFingerprintInfo{
		Host:        data.Host,
		Trusted:     exists,
		Fingerprint: fingerprint,
	}

	return response, nil
}
Ejemplo n.º 6
0
// handleInfo returns information about an app.
func handleInfo(request *api.Request) (interface{}, error) {
	// Map the data to the custom type.
	var data api.RequestInfo
	err := request.MapTo(&data)
	if err != nil {
		return nil, err
	}

	// Validate.
	if len(data.Name) == 0 {
		return nil, fmt.Errorf("missing or invalid data: %+v", data)
	}

	// Obtain the app with the given name.
	a, err := apps.Get(data.Name)
	if err != nil {
		return nil, fmt.Errorf("failed to get information of app: %v", err)
	}

	// Get the setup data from the app.
	setup, err := a.GetSetup()
	if err != nil {
		return nil, fmt.Errorf("failed to get information of app: %v", err)
	}

	// Get the turtlefile.
	// Continue also on error.
	t, err := a.Turtlefile()
	if err != nil {
		return nil, err
	}

	// Create the response value.
	res := api.ResponseInfo{
		Name:       a.Name(),
		Maintainer: t.Maintainer,
		Turtlefile: t.Name,
		State:      a.State(),
		SourceURL:  a.SourceURL(),
		Branch:     a.Branch(),

		Setup: setup,
	}

	return res, nil
}
Ejemplo n.º 7
0
// handleListBackups returns a list of all app backups.
func handleListBackups(request *api.Request) (interface{}, error) {
	// Map the data to the custom type.
	var data api.RequestListBackups
	err := request.MapTo(&data)
	if err != nil {
		return nil, err
	}

	// Validate.
	if len(data.Name) == 0 {
		return nil, fmt.Errorf("missing or invalid data: %+v", data)
	}

	// Obtain the app with the given name.
	a, err := apps.Get(data.Name)
	if err != nil {
		return nil, fmt.Errorf("failed to list backups: %v", err)
	}

	// Get all backup timestamps of the app.
	list, err := a.Backups()
	if err != nil {
		return nil, fmt.Errorf("failed to list backups: %v", err)
	}

	// Create the response value.
	res := api.ResponseListBackups{
		Backups: make([]api.ResponseListBackup, len(list)),
	}

	// Add all the backup timestamps to the response value.
	for i, u := range list {
		unix, err := strconv.ParseInt(u, 10, 64)
		if err != nil {
			return nil, fmt.Errorf("failed to list backups: failed to parse unix timestamp: %v", err)
		}

		res.Backups[i] = api.ResponseListBackup{
			Unix: u,
			Date: time.Unix(unix, 0).String(),
		}
	}

	return res, nil
}
Ejemplo n.º 8
0
// handleAddHostFingerprint adds a new host fingerprint.
func handleAddHostFingerprint(request *api.Request) (interface{}, error) {
	// Map the data to the custom type.
	var data api.RequestAddHostFingerprint
	err := request.MapTo(&data)
	if err != nil {
		return nil, err
	}

	// Validate.
	if len(data.Fingerprint) == 0 {
		return nil, fmt.Errorf("missing or invalid data: %+v", data)
	}

	// Add the fingerprint.
	err = addHostFingerprint(data.Fingerprint)
	if err != nil {
		return nil, fmt.Errorf("failed to add host fingerprint: %v", err)
	}

	return nil, nil
}
Ejemplo n.º 9
0
// handleAdd handles the add App request.
func handleAdd(request *api.Request) (interface{}, error) {
	// Map the data to the custom type.
	var data api.RequestAdd
	err := request.MapTo(&data)
	if err != nil {
		return nil, err
	}

	// Validate.
	if len(data.Name) == 0 || len(data.SourceURL) == 0 || len(data.Branch) == 0 {
		return nil, fmt.Errorf("missing or invalid data: %+v", data)
	}

	// Add a new app.
	err = apps.Add(data.Name, data.SourceURL, data.Branch)
	if err != nil {
		return nil, fmt.Errorf("failed to add app: %v", err)
	}

	return nil, nil
}