mirror of
https://github.com/hanzoai/compute.git
synced 2026-10-30 14:50:41 +00:00
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.
248 lines
7.1 KiB
Go
248 lines
7.1 KiB
Go
// Copyright 2024 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 Machine struct {
|
|
Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
|
|
Name string `xorm:"varchar(100) notnull pk" json:"name"`
|
|
// Project is the second attribution dimension alongside Owner (the tenant
|
|
// boundary): the project WITHIN the org that owns this machine. Not part of the
|
|
// (Owner, Name) primary key — an additive, Sync2-safe column that defaults to
|
|
// "" (the org's default project), so existing rows and callers are unaffected.
|
|
Project string `xorm:"varchar(100)" json:"project"`
|
|
Id string `xorm:"varchar(100)" json:"id"`
|
|
Provider string `xorm:"varchar(100)" json:"provider"`
|
|
// Source is which account this machine is known through: "registry" for the
|
|
// organization's own provider credentials, "live" for the house account. It
|
|
// is answered, never stored — the two used to be two ADDRESSES, and this is
|
|
// the property that difference actually was.
|
|
Source string `xorm:"-" json:"source,omitempty"`
|
|
// Account is which credential within the provider this machine launched on,
|
|
// carrying the provider's ProviderKey.Name; "" is the provider's own account.
|
|
// A launch cycles across a provider's accounts, and on most clouds a resource
|
|
// created under one account's key is invisible to another's, so the machine
|
|
// records its account to be listed and managed on the same one. Additive and
|
|
// Sync2-safe: "" is a machine on the provider's own credential, as before.
|
|
Account string `xorm:"varchar(100)" json:"account"`
|
|
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
|
|
UpdatedTime string `xorm:"varchar(100)" json:"updatedTime"`
|
|
ExpireTime string `xorm:"varchar(100)" json:"expireTime"`
|
|
DisplayName string `xorm:"varchar(100)" json:"displayName"`
|
|
|
|
Region string `xorm:"varchar(100)" json:"region"`
|
|
Zone string `xorm:"varchar(100)" json:"zone"`
|
|
Category string `xorm:"varchar(100)" json:"category"`
|
|
Type string `xorm:"varchar(100)" json:"type"`
|
|
Size string `xorm:"varchar(100)" json:"size"`
|
|
Tag string `xorm:"varchar(500)" json:"tag"`
|
|
State string `xorm:"varchar(100)" json:"state"`
|
|
|
|
Image string `xorm:"varchar(100)" json:"image"`
|
|
Os string `xorm:"varchar(100)" json:"os"`
|
|
PublicIp string `xorm:"varchar(100)" json:"publicIp"`
|
|
PrivateIp string `xorm:"varchar(100)" json:"privateIp"`
|
|
CpuSize string `xorm:"varchar(100)" json:"cpuSize"`
|
|
MemSize string `xorm:"varchar(100)" json:"memSize"`
|
|
|
|
// DB info
|
|
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"`
|
|
}
|
|
|
|
func GetMachineCount(owner, field, value string) (int64, error) {
|
|
session := GetSession(owner, -1, -1, field, value, "", "")
|
|
return session.Count(&Machine{})
|
|
}
|
|
|
|
func GetMachines(owner string) ([]*Machine, error) {
|
|
machines := []*Machine{}
|
|
engine, err := EngineFor(owner)
|
|
if err != nil {
|
|
return machines, err
|
|
}
|
|
err = engine.Desc("created_time").Find(&machines, &Machine{Owner: owner})
|
|
if err != nil {
|
|
return machines, err
|
|
}
|
|
return machines, nil
|
|
}
|
|
|
|
func GetPaginationMachines(owner string, offset, limit int, field, value, sortField, sortOrder string) ([]*Machine, error) {
|
|
machines := []*Machine{}
|
|
session := GetSession(owner, offset, limit, field, value, sortField, sortOrder)
|
|
err := session.Find(&machines)
|
|
if err != nil {
|
|
return machines, err
|
|
}
|
|
|
|
return machines, nil
|
|
}
|
|
|
|
func getMachine(owner string, name string) (*Machine, error) {
|
|
if owner == "" || name == "" {
|
|
return nil, nil
|
|
}
|
|
|
|
engine, err := EngineFor(owner)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
machine := Machine{Owner: owner, Name: name}
|
|
existed, err := engine.Get(&machine)
|
|
if err != nil {
|
|
return &machine, err
|
|
}
|
|
|
|
if existed {
|
|
return &machine, nil
|
|
} else {
|
|
return nil, nil
|
|
}
|
|
}
|
|
|
|
func GetMachine(id string) (*Machine, error) {
|
|
owner, name := util.GetOwnerAndNameFromId(id)
|
|
return getMachine(owner, name)
|
|
}
|
|
|
|
func GetMaskedMachine(machine *Machine, errs ...error) (*Machine, error) {
|
|
if len(errs) > 0 && errs[0] != nil {
|
|
return nil, errs[0]
|
|
}
|
|
|
|
if machine == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
if machine.RemotePassword != "" {
|
|
machine.RemotePassword = "***"
|
|
}
|
|
|
|
return machine, nil
|
|
}
|
|
|
|
func GetMaskedMachines(machines []*Machine, errs ...error) ([]*Machine, error) {
|
|
if len(errs) > 0 && errs[0] != nil {
|
|
return nil, errs[0]
|
|
}
|
|
|
|
var err error
|
|
for _, machine := range machines {
|
|
machine, err = GetMaskedMachine(machine)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return machines, nil
|
|
}
|
|
|
|
func UpdateMachine(id string, machine *Machine) (bool, error) {
|
|
owner, name := util.GetOwnerAndNameFromId(id)
|
|
oldMachine, err := getMachine(owner, name)
|
|
if err != nil {
|
|
return false, err
|
|
} else if oldMachine == nil {
|
|
return false, nil
|
|
}
|
|
|
|
if machine.RemotePassword == "***" {
|
|
machine.RemotePassword = oldMachine.RemotePassword
|
|
}
|
|
|
|
_, err = updateMachineCloud(oldMachine, machine)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
engine, err := EngineFor(owner)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
affected, err := engine.ID(schemas.PK{owner, name}).AllCols().Update(machine)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return affected != 0, nil
|
|
}
|
|
|
|
func AddMachine(machine *Machine) (bool, error) {
|
|
engine, err := EngineFor(machine.Owner)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
affected, err := engine.Insert(machine)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return affected != 0, nil
|
|
}
|
|
|
|
// addMachines batch-inserts machines that all belong to owner (they come from a
|
|
// single owner's cloud sync), so one EngineFor(owner) routes the whole batch.
|
|
func addMachines(owner string, machines []*Machine) (bool, error) {
|
|
engine, err := EngineFor(owner)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
affected, err := engine.Insert(machines)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return affected != 0, nil
|
|
}
|
|
|
|
func DeleteMachine(machine *Machine) (bool, error) {
|
|
engine, err := EngineFor(machine.Owner)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
affected, err := engine.ID(schemas.PK{machine.Owner, machine.Name}).Delete(&Machine{})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return affected != 0, nil
|
|
}
|
|
|
|
func deleteMachines(owner string) (bool, error) {
|
|
engine, err := EngineFor(owner)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
affected, err := engine.Delete(&Machine{Owner: owner})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return affected != 0, nil
|
|
}
|
|
|
|
func (machine *Machine) GetId() string {
|
|
return fmt.Sprintf("%s/%s", machine.Owner, machine.Name)
|
|
}
|