Files
zeekay 2b14fd1659 Compute: the module, the binary and the image say so
Cloud's side of this rename landed some time ago — the app is apps/compute,
the env is COMPUTE_URL — and left a note saying the other half moves with
the manifest that names it. This is the half that does not touch the
cluster: the Go module is github.com/hanzoai/compute, the binary it builds
is compute, and the image is ghcr.io/hanzoai/compute. GitHub already
carries the new name.

What is deliberately NOT here is the cutover. The Service universe declares
is still visor, and so are visor-config, visor-kms-sync and visor-db-bucket.
Pointing anything at compute.hanzo.svc before those move resolves to
nothing, so they move together, in universe, not here.
2026-09-07 17:37:50 -07:00

234 lines
6.0 KiB
Go

// Copyright 2023 Hanzo Industries Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package object
import (
"fmt"
"github.com/hanzoai/orm/relational/schemas"
"github.com/hanzoai/compute/util"
)
type Service struct {
No int `json:"no"`
Name string `json:"name"`
Path string `json:"path"`
Port int `json:"port"`
ProcessId int `json:"processId"`
ExpectedStatus string `json:"expectedStatus"`
Status string `json:"status"`
SubStatus string `json:"subStatus"`
Message string `json:"message"`
}
type Patch struct {
Name string `json:"name"`
Category string `json:"category"`
Title string `json:"title"`
Url string `json:"url"`
Size string `json:"size"`
ExpectedStatus string `json:"expectedStatus"`
Status string `json:"status"`
InstallTime string `json:"installTime"`
Message string `json:"message"`
}
type RemoteApp struct {
No int `json:"no"`
RemoteAppName string `xorm:"varchar(100)" json:"remoteAppName"`
RemoteAppDir string `xorm:"varchar(100)" json:"remoteAppDir"`
RemoteAppArgs string `xorm:"varchar(100)" json:"remoteAppArgs"`
}
type Asset struct {
Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
Name string `xorm:"varchar(100) notnull pk" json:"name"`
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
UpdatedTime string `xorm:"varchar(100)" json:"updatedTime"`
DisplayName string `xorm:"varchar(100)" json:"displayName"`
Category string `xorm:"varchar(100)" json:"category"`
Type string `xorm:"varchar(100)" json:"type"`
Tag string `xorm:"varchar(100)" json:"tag"`
MachineName string `xorm:"varchar(100)" json:"machineName"`
Os string `xorm:"varchar(100)" json:"os"`
PublicIp string `xorm:"varchar(100)" json:"publicIp"`
PrivateIp string `xorm:"varchar(100)" json:"privateIp"`
Size string `xorm:"varchar(100)" json:"size"`
CpuSize string `xorm:"varchar(100)" json:"cpuSize"`
MemSize string `xorm:"varchar(100)" json:"memSize"`
RemoteProtocol string `xorm:"varchar(100)" json:"remoteProtocol"`
RemotePort int `json:"remotePort"`
RemoteUsername string `xorm:"varchar(100)" json:"remoteUsername"`
RemotePassword string `xorm:"varchar(100)" json:"remotePassword"`
AutoQuery bool `json:"autoQuery"`
IsPermanent bool `json:"isPermanent"`
Language string `xorm:"varchar(100)" json:"language"`
EnableRemoteApp bool `json:"enableRemoteApp"`
RemoteApps []*RemoteApp `json:"remoteApps"`
Services []*Service `json:"services"`
Patches []*Patch `json:"patches"`
}
func GetAssetCount(owner, field, value string) (int64, error) {
session := GetSession(owner, -1, -1, field, value, "", "")
return session.Count(&Asset{})
}
func GetAssets(owner string) ([]*Asset, error) {
assets := []*Asset{}
engine, err := EngineFor(owner)
if err != nil {
return assets, err
}
err = engine.Desc("created_time").Find(&assets, &Asset{Owner: owner})
if err != nil {
return assets, err
}
return assets, nil
}
func GetPaginationAssets(owner string, offset, limit int, field, value, sortField, sortOrder string) ([]*Asset, error) {
assets := []*Asset{}
session := GetSession(owner, offset, limit, field, value, sortField, sortOrder)
err := session.Find(&assets)
if err != nil {
return assets, err
}
return assets, nil
}
func getAsset(owner string, name string) (*Asset, error) {
if owner == "" || name == "" {
return nil, nil
}
engine, err := EngineFor(owner)
if err != nil {
return nil, err
}
asset := Asset{Owner: owner, Name: name}
existed, err := engine.Get(&asset)
if err != nil {
return &asset, err
}
if existed {
return &asset, nil
} else {
return nil, nil
}
}
func GetAsset(id string) (*Asset, error) {
owner, name := util.GetOwnerAndNameFromId(id)
return getAsset(owner, name)
}
func GetMaskedAsset(asset *Asset, errs ...error) (*Asset, error) {
if len(errs) > 0 && errs[0] != nil {
return nil, errs[0]
}
if asset == nil {
return nil, nil
}
if asset.RemotePassword != "" {
asset.RemotePassword = "***"
}
return asset, nil
}
func GetMaskedAssets(assets []*Asset, errs ...error) ([]*Asset, error) {
if len(errs) > 0 && errs[0] != nil {
return nil, errs[0]
}
var err error
for _, asset := range assets {
asset, err = GetMaskedAsset(asset)
if err != nil {
return nil, err
}
}
return assets, nil
}
func UpdateAsset(id string, asset *Asset) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
p, err := getAsset(owner, name)
if err != nil {
return false, err
} else if p == nil {
return false, nil
}
if asset.RemotePassword == "***" {
asset.RemotePassword = p.RemotePassword
}
engine, err := EngineFor(owner)
if err != nil {
return false, err
}
affected, err := engine.ID(schemas.PK{owner, name}).AllCols().Update(asset)
if err != nil {
return false, err
}
return affected != 0, nil
}
func AddAsset(asset *Asset) (bool, error) {
engine, err := EngineFor(asset.Owner)
if err != nil {
return false, err
}
affected, err := engine.Insert(asset)
if err != nil {
return false, err
}
return affected != 0, nil
}
func DeleteAsset(asset *Asset) (bool, error) {
engine, err := EngineFor(asset.Owner)
if err != nil {
return false, err
}
affected, err := engine.ID(schemas.PK{asset.Owner, asset.Name}).Delete(&Asset{})
if err != nil {
return false, err
}
return affected != 0, nil
}
func (asset *Asset) getId() string {
return fmt.Sprintf("%s/%s", asset.Owner, asset.Name)
}