// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }