v0.0.0-...-4ea40fb Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2023 License: MIT Imports: 39 Imported by: 1




View Source
const (
	Day      = 24 * time.Hour
	Week     = 7 * Day
	Month    = 30 * Day
	Year     = 12 * Month
	LongTime = 37 * Year

	KB = 1000
	MB = 1000 * KB
	GB = 1000 * MB
	TB = 1000 * GB
	PB = 1000 * TB

	KiB = 1024
	MiB = 1024 * KiB
	GiB = 1024 * MiB
	TiB = 1024 * GiB
	PiB = 1024 * TiB

Seconds-based time units

View Source
const (
	// DefaultVolumeLabel is the default label of a volume created with mkfs
	DefaultVolumeLabel = "default"

	// VolumeDelimiter is the reserved character used as delimiter between
	// volume name and uuid/label
	VolumeDelimiter = ":"
View Source
const APIKeyHeader = "x-api-key"

APIKeyHeader is the header key where we set the api key for packagehub

View Source
const CredentialFileName = "credentials"

CredentialFileName is the name of the file which stores packagehub's credentials

View Source
const OpsReleaseURL = "https://storage.googleapis.com/cli/%v/ops"

OpsReleaseURL gives URL to download latest ops binary

View Source
const PackageBaseURL string = PkghubBaseURL + "/v2/packages"

PackageBaseURL gives URL for downloading of packages

View Source
const PackageManifestFileName string = "manifest.json"

PackageManifestFileName is manifest file path

View Source
const PackageManifestURL string = PkghubBaseURL + "/v2/manifest.json"

PackageManifestURL stores info about all packages

View Source
const PackageSysRootFolderName = "sysroot"

PackageSysRootFolderName is the name of package root folder

View Source
const PkghubBaseURL string = "https://repo.ops.city"

PkghubBaseURL is the base url of packagehub


View Source
var AltGOARCH = ""

AltGOARCH is an optional user-supplied cross-build arch for both build and run.

View Source
var ErrCredentialsNotExist = errors.New("credentials not exist")

ErrCredentialsNotExist is the error we return if the credential file doesn't exist

View Source
var (
	// ErrVolumeNotFound is error returned when a volume with a given id does not exist
	ErrVolumeNotFound = func(id string) error { return errors.Errorf("volume with UUID %s not found", id) }
View Source
var LatestReleaseVersion = getLatestRelVersion()

LatestReleaseVersion give latest stable release for nanos

View Source
var LocalImageDir = path.Join(GetOpsHome(), "images")

LocalImageDir is the directory where ops save images

View Source
var LocalReleaseVersion = getLocalRelVersion()

LocalReleaseVersion is version latest release downloaded in ops home

View Source
var (
	// LocalVolumeDir is the default local volume directory
	LocalVolumeDir = path.Join(GetOpsHome(), "volumes")
View Source
var NightlyLocalFolderm = NightlyLocalFolder()

NightlyLocalFolderm is directory path where nightly builds are stored

View Source
var NightlyReleaseURLm = NightlyReleaseURL()

NightlyReleaseURLm give URL for nightly build.

View Source
var PackagesCache = getPackageCache()

PackagesCache where all packages are stored

View Source
var RealGOARCH = getGOARCH()

RealGOARCH is the underlying host architecture.

View Source
var (
	// TTLDefault is the default ttl value used to create DNS records
	TTLDefault = 300
View Source
var Version string

Version for ops


func AddMounts

func AddMounts(mounts []string, config *types.Config) error

AddMounts adds Mounts and RunConfig.Mounts to image from flags

func Apply

func Apply(update io.Reader, opts Options) error

Apply performs an update of the current executable (or opts.TargetFile, if set) with the contents of the given io.Reader.

Apply performs the following actions to ensure a safe cross-platform update:

1. If configured, applies the contents of the update io.Reader as a binary patch.

2. If configured, computes the checksum of the new executable and verifies it matches.

3. If configured, verifies the signature with a public key.

4. Creates a new file, /path/to/.target.new with the TargetMode with the contents of the updated file

5. Renames /path/to/target to /path/to/.target.old

6. Renames /path/to/.target.new to /path/to/target

7. If the final rename is successful, deletes /path/to/.target.old, returns no error. On Windows, the removal of /path/to/target.old always fails, so instead Apply hides the old file instead.

8. If the final rename fails, attempts to roll back by renaming /path/to/.target.old back to /path/to/target.

func BaseHTTPRequest

func BaseHTTPRequest(method string, url string, body io.Reader) (*http.Request, error)

BaseHTTPRequest is a wrapper around http.NewRequest with a header that holds the ops version.

func BuildImage

func BuildImage(c types.Config) error

BuildImage builds a unikernel image for user supplied ELF binary.

func BuildImageFromPackage

func BuildImageFromPackage(packagepath string, c types.Config) error

BuildImageFromPackage builds nanos image using a package

func BuildManifest

func BuildManifest(c *types.Config) (*fs.Manifest, error)

BuildManifest builds manifest using config

func BuildPackageManifest

func BuildPackageManifest(packagepath string, c *types.Config) (*fs.Manifest, error)

BuildPackageManifest builds manifest using package

func BuildRequestForArchiveUpload

func BuildRequestForArchiveUpload(namespace, name string, pkg Package, archiveLocation string, private bool) (*http.Request, error)

BuildRequestForArchiveUpload builds the request to upload a package with the provided metadata

func Bytes2Human

func Bytes2Human(b int64) string

Bytes2Human parses bytes to other byte units as MegaByte and Gigabyte.

func CCidr

func CCidr(netmask string) int

CCidr converts network mask to CIDR size.

func CheckNanosVersionExists

func CheckNanosVersionExists(version string) (bool, error)

CheckNanosVersionExists verifies whether version exists in filesystem

func ClonePackage

func ClonePackage(old string, newPkg string, version string, oldconfig *types.Config, newconfig *types.Config)

ClonePackage will cloned a package from ~/.ops/packages to ~/.ops/local_packages.

func CreateArchive

func CreateArchive(archive string, files []string) error

CreateArchive compress files into an archive

func CreateDNSRecord

func CreateDNSRecord(config *types.Config, aRecordIP string, dnsService DNSService) error

CreateDNSRecord does the necessary operations to create a DNS record without issues in an cloud provider

func CreatePackageFromRun

func CreatePackageFromRun(newPkg string, version string, mergedCfg *types.Config)

CreatePackageFromRun builds a new package as if you were doing an 'ops run myprogram'

func CreateTarGz

func CreateTarGz(src string, destination string) error

CreateTarGz builds a .tar.gz archive with the directory of the source as the root of the archive

func CustomRelTime

func CustomRelTime(a, b time.Time, albl, blbl string, magnitudes []RelTimeMagnitude) string

CustomRelTime formats a time into a relative string.

It takes two times two labels and a table of relative time formats. In addition to the generic time delta string (e.g. 5 minutes), the labels are used applied so that the label corresponding to the smaller time is applied.

func DoUpdate

func DoUpdate(url string) error

DoUpdate updates file using provided URL

func DownloadCommonFiles

func DownloadCommonFiles() error

DownloadCommonFiles dowloads common tarball files and extract them to common directory

func DownloadFile

func DownloadFile(fpath string, url string, timeout int, showProgress bool) error

DownloadFile downloads file using URL

func DownloadFileWithProgress

func DownloadFileWithProgress(filepath string, url string, timeout int) error

DownloadFileWithProgress downloads file using URL displaying progress counter

func DownloadNightlyImages

func DownloadNightlyImages(c *types.Config) error

DownloadNightlyImages downloads nightly build for nanos

func DownloadPackage

func DownloadPackage(identifier string, config *types.Config) (string, error)

DownloadPackage downloads package by identifier

func DownloadReleaseImages

func DownloadReleaseImages(version string, arch string) error

DownloadReleaseImages downloads nanos for particular release version arch defaults to x86-64 if empty

func ErrInstanceNotFound

func ErrInstanceNotFound(name string) error

ErrInstanceNotFound creates new error stating instance with given name cannot be found

func ExtractNS

func ExtractNS(identifier string) (string, string)

ExtractNS extracts namespace from the package identifier of format <namespace>/<packageWithVersion> and returns the namespace and package with version

func ExtractPackage

func ExtractPackage(archive, dest string, config *types.Config)

ExtractPackage extracts package in ops home. This function is currently over-loaded.

func GenerateImageName

func GenerateImageName(program string) string

GenerateImageName generate image name

func GetElfFileInfo

func GetElfFileInfo(path string) (*elf.File, error)

GetElfFileInfo returns an object with elf information of the path program

func GetLocalUsername

func GetLocalUsername() string

GetLocalUsername gets the local username and return "" if not found

func GetNSPkgnameAndVersion

func GetNSPkgnameAndVersion(pkgIdentifier string) (string, string, string)

GetNSPkgnameAndVersion gets the namespace, name and version from the pkg identifier

func GetOpsHome

func GetOpsHome() string

GetOpsHome get ops directory path We store all ops related info, packages, images in this directory

func GetPackageManifestFile

func GetPackageManifestFile() string

GetPackageManifestFile give path for package manifest file

func GetSizeInGb

func GetSizeInGb(size string) (int, error)

GetSizeInGb converts a string representation of a volume size to an integer number of GB

func GetUefiBoot

func GetUefiBoot(version string) string

GetUefiBoot retrieves UEFI bootloader file path, if found

func HasDebuggingSymbols

func HasDebuggingSymbols(efd *elf.File) bool

HasDebuggingSymbols checks whether elf file has debugging symbols

func HomeDir

func HomeDir() (string, error)

HomeDir returns the home directory for the executing user. This uses an OS-specific method for discovering the home directory. An error is returned if a home directory cannot be detected.

func IsDynamicLinked

func IsDynamicLinked(efd *elf.File) bool

IsDynamicLinked checks whether elf file was linked dynamically

func IsInstanceNotFoundError

func IsInstanceNotFoundError(err error) bool

IsInstanceNotFoundError checks if given error is an ErrInstanceNotFound

func LocalTimeStamp

func LocalTimeStamp() (string, error)

LocalTimeStamp gives local timestamp from download nightly build

func NewConfig

func NewConfig() *types.Config

NewConfig construct instance of Config with default values

func NightlyLocalFolder

func NightlyLocalFolder() string

NightlyLocalFolder points to the latest nightly release url that is arch dependent upon flag set.

func NightlyReleaseURL

func NightlyReleaseURL() string

NightlyReleaseURL points to the latest nightly release url that is arch dependent upon flag set.

func PackageManifestChanged

func PackageManifestChanged(fino os.FileInfo, remoteURL string) bool

PackageManifestChanged verifies if package manifest changed

func PrintVolumesList

func PrintVolumesList(volumes *[]NanosVolume)

PrintVolumesList writes into console a table with volumes details

func RAMInBytes

func RAMInBytes(size string) (int64, error)

RAMInBytes parses a human-readable string representing an amount of RAM in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and returns the number of bytes, or -1 if the string is unparseable. Units are case-insensitive, and the 'b' suffix is optional.

func RelTime

func RelTime(a, b time.Time, albl, blbl string) string

RelTime formats a time into a relative string.

It takes two times and two labels. In addition to the generic time delta string (e.g. 5 minutes), the labels are used applied so that the label corresponding to the smaller time is applied.

RelTime(timeInPast, timeInFuture, "earlier", "later") -> "3 weeks earlier"

func RemoteTimeStamp

func RemoteTimeStamp() (string, error)

RemoteTimeStamp gives latest nightly build timestamp

func RollbackError

func RollbackError(err error) error

RollbackError takes an error value returned by Apply and returns the error, if any, that occurred when attempting to roll back from a failed update. Applications should always call this function on any non-nil errors returned by Apply.

If no rollback was needed or if the rollback was successful, RollbackError returns nil, otherwise it returns the error encountered when trying to roll back.

func SliceAtoi

func SliceAtoi(sa []string) ([]int, error)

SliceAtoi converts slice of strings to a slice of integers

func StoreCredentials

func StoreCredentials(creds Credentials) error

StoreCredentials stores the credentials in the credential file. Overrides it if there is an existing one.

func Time2Human

func Time2Human(then time.Time) string

Time2Human formats a time into a relative string. Time(someT) -> "3 weeks ago"

func UpdateLocalRelease

func UpdateLocalRelease(version string) error

UpdateLocalRelease updates nanos version used on ops operations

func ValidateELF

func ValidateELF(executablePath string)

ValidateELF validates ELF executable format given the file path


type APIMetadataRequest

type APIMetadataRequest struct {
	Namespace string `json:"namespace"`
	PkgName   string `json:"pkg_name"`
	Version   string `json:"version"`
	Arch      string `json:"arch"`

APIMetadataRequest is payload sent to get metadata for a package

type CloudImage

type CloudImage struct {
	ID      string
	Name    string
	Status  string
	Size    int64
	Path    string
	Created time.Time
	Tag     string // could merge w/below
	Labels  []string

CloudImage abstracts images for various cloud providers

type CloudInstance

type CloudInstance struct {
	ID         string
	Name       string
	Status     string
	Created    string // TODO: prob. should be datetime w/helpers for human formatting
	PrivateIps []string
	PublicIps  []string
	Ports      []string
	Image      string

CloudInstance represents the instance that widely use in different Cloud Providers. mainly used for formatting standard response from any cloud provider

func (CloudInstance) MarshalJSON

func (c CloudInstance) MarshalJSON() ([]byte, error)

MarshalJSON ensures correct json serialization of potential null vals.

type Context

type Context struct {
	// contains filtered or unexported fields

Context captures required info for provider operation

func NewContext

func NewContext(c *types.Config) *Context

NewContext Create a new context for the given provider valid providers are "gcp", "aws" and "onprem"

func (Context) Config

func (c Context) Config() *types.Config

Config returns context configuration

func (Context) Logger

func (c Context) Logger() *log.Logger

Logger returns logger

type Credentials

type Credentials struct {
	Username string
	APIKey   string

Credentials is the information that will be stored in the ~/.ops/credentials

func ReadCredsFromLocal

func ReadCredsFromLocal() (*Credentials, error)

ReadCredsFromLocal gets the credentials from the credential file in the ops home returns an ErrCredentialsNotExist if the file doesn't exist. This means the user hasn't authenticated yet.

type DNSRecord

type DNSRecord struct {
	Name string
	IP   string
	Type string
	TTL  int

DNSRecord is ops representation of a dns record

type DNSService

type DNSService interface {
	FindOrCreateZoneIDByName(config *types.Config, name string) (string, error)
	DeleteZoneRecordIfExists(config *types.Config, zoneID string, recordName string) error
	CreateZoneRecord(config *types.Config, zoneID string, record *DNSRecord) error

DNSService is an interface for DNS related operations

type JSONStore

type JSONStore struct {
	// contains filtered or unexported fields

JSONStore implements volumeStore TODO probably use a more established KV-store

func (*JSONStore) Delete

func (s *JSONStore) Delete(id string) (NanosVolume, error)

Delete deletes volume of a given UUID

func (*JSONStore) Get

func (s *JSONStore) Get(id string) (NanosVolume, error)

Get gets volume data of a given UUID

func (*JSONStore) GetAll

func (s *JSONStore) GetAll() ([]NanosVolume, error)

GetAll gets all volume data

func (*JSONStore) Insert

func (s *JSONStore) Insert(vol NanosVolume) error

Insert inserts volume data

func (*JSONStore) Update

func (s *JSONStore) Update(v NanosVolume) error

Update updates a given volume

type Mirrorer

type Mirrorer interface {
	MirrorImage(ctx *Context, imageName, srcRegion, dstRegion string) (string, error)

Mirrorer is an interface that all provider which provider feature to copy an image from one region to another

type NanosVolume

type NanosVolume struct {
	ID         string `json:"id"`
	Name       string `json:"name"`
	Label      string `json:"label"`
	Data       string `json:"data"`
	Size       string `json:"size"` // this has extra meta in it that should be converted to just bytes
	Path       string `json:"path"`
	AttachedTo string `json:"attached_to"`
	CreatedAt  string `json:"created_at"`
	Status     string `json:"status"`

NanosVolume information for nanos-managed volume

func CreateLocalVolume

func CreateLocalVolume(config *types.Config, name, data, provider string) (NanosVolume, error)

CreateLocalVolume creates volume on ops directory creates a volume named <name>:<uuid> where <uuid> is generated on creation also creates a symlink to volume label at <name> TODO investigate symlinked volume interaction with image

func (NanosVolume) MatchedByQueries

func (v NanosVolume) MatchedByQueries(query map[string]string) bool

MatchedByQueries returns true if this volume matched one or more given query.

type Options

type Options struct {
	// TargetPath defines the path to the file to update.
	// The empty string means 'the executable file of the running program'.
	TargetPath string

	// Create TargetPath replacement with this file mode. If zero, defaults to 0755.
	TargetMode os.FileMode

	// Checksum of the new binary to verify against. If nil, no checksum or signature verification is done.
	Checksum []byte

	// Public key to use for signature verification. If nil, no signature verification is done.
	PublicKey crypto.PublicKey

	// Signature to verify the updated file. If nil, no signature verification is done.
	Signature []byte

	// Use this hash function to generate the checksum. If not set, SHA256 is used.
	Hash crypto.Hash

	// Store the old executable file at this path after a successful update.
	// The empty string means the old executable file will be removed after the update.
	OldSavePath string

Options for binary update

func (*Options) CheckPermissions

func (o *Options) CheckPermissions() error

CheckPermissions determines whether the process has the correct permissions to perform the requested update. If the update can proceed, it returns nil, otherwise it returns the error that would occur if an update were attempted.

func (*Options) SetPublicKeyPEM

func (o *Options) SetPublicKeyPEM(pembytes []byte) error

SetPublicKeyPEM is a convenience method to set the PublicKey property used for checking a completed update's signature by parsing a Public Key formatted as PEM data.

type Package

type Package struct {
	Runtime     string `json:"runtime"`
	Version     string `json:"version"`
	Language    string `json:"language"`
	Description string `json:"description,omitempty"`
	SHA256      string `json:"sha256"`
	Name        string `json:"name"`
	Namespace   string `json:"namespace"`
	Arch        string `json:"arch"`

Package is the definition of an OPS package.

func GetLocalPackageList

func GetLocalPackageList() ([]Package, error)

GetLocalPackageList provides list of local packages

func GetPackageMetadata

func GetPackageMetadata(namespace, pkgName, version string) (*Package, error)

GetPackageMetadata get metadata for the package

type PackageIdentifier

type PackageIdentifier struct {
	Name      string `json:"name"`
	Namespace string `json:"namespace"`
	Version   string `json:"version"`

PackageIdentifier is used to identify a namespaced package

func ParseIdentifier

func ParseIdentifier(identifier string) PackageIdentifier

ParseIdentifier parses a package identifier which looks like <namespace>/<pkg>:<version>

func (*PackageIdentifier) Match

func (pkgidf *PackageIdentifier) Match(pkg Package) bool

Match matches a package with all the fields of this identifier

type PackageList

type PackageList struct {
	Version  int       `json:"Version"`
	Packages []Package `json:"Packages"`

PackageList contains a list of known packages.

func GetPackageList

func GetPackageList(config *types.Config) (*PackageList, error)

GetPackageList provides list of packages

func SearchPackages

func SearchPackages(q string) (*PackageList, error)

SearchPackages searches packages against pkghub

func (*PackageList) List

func (pkglist *PackageList) List() []Package

List returns the package list

type Provider

type Provider interface {
	Initialize(config *types.ProviderConfig) error

	BuildImage(ctx *Context) (string, error)
	BuildImageWithPackage(ctx *Context, pkgpath string) (string, error)
	CreateImage(ctx *Context, imagePath string) error
	ListImages(ctx *Context) error
	GetImages(ctx *Context) ([]CloudImage, error)
	DeleteImage(ctx *Context, imagename string) error
	ResizeImage(ctx *Context, imagename string, hbytes string) error
	SyncImage(config *types.Config, target Provider, imagename string) error
	CustomizeImage(ctx *Context) (string, error)

	CreateInstance(ctx *Context) error
	ListInstances(ctx *Context) error
	GetInstances(ctx *Context) ([]CloudInstance, error)
	GetInstanceByName(ctx *Context, name string) (*CloudInstance, error)
	DeleteInstance(ctx *Context, instancename string) error
	StopInstance(ctx *Context, instancename string) error
	StartInstance(ctx *Context, instancename string) error
	RebootInstance(ctx *Context, instancename string) error
	GetInstanceLogs(ctx *Context, instancename string) (string, error)
	PrintInstanceLogs(ctx *Context, instancename string, watch bool) error


Provider is an interface that provider must implement

type RelTimeMagnitude

type RelTimeMagnitude struct {
	D      time.Duration
	Format string
	DivBy  time.Duration

A RelTimeMagnitude struct contains a relative time point at which the relative format of time will switch to a new format string. A slice of these in ascending order by their "D" field is passed to CustomRelTime to format durations.

The Format field is a string that may contain a "%s" which will be replaced with the appropriate signed label (e.g. "ago" or "from now") and a "%d" that will be replaced by the quantity.

The DivBy field is the amount of time the time difference must be divided by in order to display correctly.

e.g. if D is 2*time.Minute and you want to display "%d minutes %s" DivBy should be time.Minute so whatever the duration is will be expressed in minutes.

type Storage

type Storage interface {
	CopyToBucket(config *types.Config, source string) error

Storage is an interface that provider's storage must implement

type ValidateSuccessResponse

type ValidateSuccessResponse struct {
	Username string

ValidateSuccessResponse is the structure of the success response from validate api key endpoint

func ValidateAPIKey

func ValidateAPIKey(apikey string) (*ValidateSuccessResponse, error)

ValidateAPIKey uses the api key provided and sends it to validate endpoint of packagehub if the response is a valid user then we return that or else error is returned.

type VolumeService

type VolumeService interface {
	CreateVolume(ctx *Context, volumeName, data, provider string) (NanosVolume, error)
	GetAllVolumes(ctx *Context) (*[]NanosVolume, error)
	DeleteVolume(ctx *Context, volumeName string) error
	AttachVolume(ctx *Context, instanceName, volumeName string, attachID int) error
	DetachVolume(ctx *Context, instanceName, volumeName string) error

VolumeService is an interface for volume related operations

type WriteCounter

type WriteCounter struct {
	// contains filtered or unexported fields

WriteCounter counts the number of bytes written to it. It implements to the io.Writer interface and we can pass this into io.TeeReader() which will report progress on each write cycle.

func NewWriteCounter

func NewWriteCounter(total int) *WriteCounter

NewWriteCounter creates new write counter

func (*WriteCounter) Finish

func (wc *WriteCounter) Finish()

Finish progress bar

func (*WriteCounter) Start

func (wc *WriteCounter) Start()

Start progress bar

func (*WriteCounter) Write

func (wc *WriteCounter) Write(p []byte) (int, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL