diff --git a/apps/types.go b/apps/types.go index a9a6513..2dcab54 100644 --- a/apps/types.go +++ b/apps/types.go @@ -83,6 +83,9 @@ type App struct { DiskUsageBytes int `json:"disk_usage_bytes"` // Disk usage in inodes DiskUsageInodes int `json:"disk_usage_inodes"` + + // This is not store in the database but used in create request when the app suppose to be created from an existing snapshot + Snapshot string `json:"snapshot" gorm:"-"` } // Validate do basic checks of the struct values diff --git a/handlers_nats.go b/handlers_nats.go index a27e1c7..bf1187a 100644 --- a/handlers_nats.go +++ b/handlers_nats.go @@ -195,6 +195,31 @@ func createEventHandler(m *nats.Msg, message *RequestMessage) error { return err } + // Restore from snapshot if it's noted in the request + if len(appTemplate.Snapshot) > 0 { + // Setup processors + snapshotProcessor := apps.SnapshotProcessor{ + AppsPath: config.AppsPath, + TmpSnapshotPath: config.SnapshotsPath, + + Driver: drivers.S3Driver{ + S3AccessKey: config.SnapshotsS3AccessKey, + S3SecretKey: config.SnapshotsS3SecretKey, + S3Endpoint: config.SnapshotsS3Endpoint, + S3SSL: config.SnapshotsS3SSL, + Bucket: config.SnapshotsS3Bucket, + }, + } + + // Restore the data + err = snapshotProcessor.RestoreSnapshot(appTemplate.Snapshot, message.AppName) + if err != nil { + log.Println("ERROR restore snapshot error: " + err.Error()) + publish(message.AppName, "backend problem", true) + return err + } + } + err = container.Start() if err != nil { log.Println("ERROR create application problem: " + err.Error())