|
|
@ -2,10 +2,12 @@ package api |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"context" |
|
|
|
|
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"io/ioutil" |
|
|
|
"io/ioutil" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
"net/url" |
|
|
|
"net/url" |
|
|
|
|
|
|
|
"os" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
"sync" |
|
|
|
"sync" |
|
|
|
|
|
|
|
|
|
|
@ -40,6 +42,43 @@ func newGalleryApplier(modelPath string) *galleryApplier { |
|
|
|
statuses: make(map[string]*galleryOpStatus), |
|
|
|
statuses: make(map[string]*galleryOpStatus), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func applyGallery(modelPath string, req ApplyGalleryModelRequest, cm *ConfigMerger) error { |
|
|
|
|
|
|
|
url, err := req.DecodeURL() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Send a GET request to the URL
|
|
|
|
|
|
|
|
response, err := http.Get(url) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
defer response.Body.Close() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Read the response body
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(response.Body) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Unmarshal YAML data into a Config struct
|
|
|
|
|
|
|
|
var config gallery.Config |
|
|
|
|
|
|
|
err = yaml.Unmarshal(body, &config) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
config.Files = append(config.Files, req.AdditionalFiles...) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err := gallery.Apply(modelPath, req.Name, &config, req.Overrides); err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Reload models
|
|
|
|
|
|
|
|
return cm.LoadConfigs(modelPath) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (g *galleryApplier) updatestatus(s string, op *galleryOpStatus) { |
|
|
|
func (g *galleryApplier) updatestatus(s string, op *galleryOpStatus) { |
|
|
|
g.Lock() |
|
|
|
g.Lock() |
|
|
|
defer g.Unlock() |
|
|
|
defer g.Unlock() |
|
|
@ -66,52 +105,50 @@ func (g *galleryApplier) start(c context.Context, cm *ConfigMerger) { |
|
|
|
g.updatestatus(op.id, &galleryOpStatus{Error: e, Processed: true}) |
|
|
|
g.updatestatus(op.id, &galleryOpStatus{Error: e, Processed: true}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
url, err := op.req.DecodeURL() |
|
|
|
if err := applyGallery(g.modelPath, op.req, cm); err != nil { |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
updateError(err) |
|
|
|
updateError(err) |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Send a GET request to the URL
|
|
|
|
g.updatestatus(op.id, &galleryOpStatus{Processed: true, Message: "completed"}) |
|
|
|
response, err := http.Get(url) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
updateError(err) |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
defer response.Body.Close() |
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Read the response body
|
|
|
|
func ApplyGalleryFromFile(modelPath, s string, cm *ConfigMerger) error { |
|
|
|
body, err := ioutil.ReadAll(response.Body) |
|
|
|
dat, err := os.ReadFile(s) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
updateError(err) |
|
|
|
return err |
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var requests []ApplyGalleryModelRequest |
|
|
|
// Unmarshal YAML data into a Config struct
|
|
|
|
err = json.Unmarshal(dat, &requests) |
|
|
|
var config gallery.Config |
|
|
|
|
|
|
|
err = yaml.Unmarshal(body, &config) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
updateError(fmt.Errorf("failed to unmarshal YAML: %v", err)) |
|
|
|
return err |
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
config.Files = append(config.Files, op.req.AdditionalFiles...) |
|
|
|
for _, r := range requests { |
|
|
|
|
|
|
|
if err := applyGallery(modelPath, r, cm); err != nil { |
|
|
|
if err := gallery.Apply(g.modelPath, op.req.Name, &config, op.req.Overrides); err != nil { |
|
|
|
return err |
|
|
|
updateError(err) |
|
|
|
} |
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Reload models
|
|
|
|
return nil |
|
|
|
if err := cm.LoadConfigs(g.modelPath); err != nil { |
|
|
|
} |
|
|
|
updateError(err) |
|
|
|
func ApplyGalleryFromString(modelPath, s string, cm *ConfigMerger) error { |
|
|
|
continue |
|
|
|
var requests []ApplyGalleryModelRequest |
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(s), &requests) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
g.updatestatus(op.id, &galleryOpStatus{Processed: true, Message: "completed"}) |
|
|
|
for _, r := range requests { |
|
|
|
|
|
|
|
if err := applyGallery(modelPath, r, cm); err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// endpoints
|
|
|
|
// endpoints
|
|
|
|