Documentation
¶
Overview ¶
Package client builds an authenticated Hostim API client from resolved config and maps API error responses (including 429 rate limiting) into Go errors.
Index ¶
- Constants
- Variables
- func Check(status int, body []byte) error
- func HasDockerfile(dir, dockerfile string) bool
- func IsStaticSite(dir string) bool
- func MergeVars(base, updates []api.EnvVar) []api.EnvVar
- func New(r config.Resolved) (*api.ClientWithResponses, error)
- func NewAnonymous(apiURL string) (*api.ClientWithResponses, error)
- func NormalizeApp(app *api.App)
- func Pack(dir string, static bool) (path, sha256Hex string, size int64, err error)
- func ResolveProjectID(ctx context.Context, cl *api.ClientWithResponses, ref string) (string, error)
- func StaticFallback(dir, dockerfile string) bool
- func Upload(ctx context.Context, c *api.ClientWithResponses, project, dir string, ...) (string, error)
- type APIError
- type Baseline
- type BuildResult
- type VolumeMount
Constants ¶
const MaxUploadSize int64 = 500 << 20
MaxUploadSize is the largest source archive the API accepts, after gzip.
const ProjectIDPrefix = "hpr-"
ProjectIDPrefix is the prefix of the system-generated project identifiers used as the {projectName} path parameter by the API. Friendly project names are resolved to an ID before nested calls.
Variables ¶
var ErrBuildFailed = errors.New("build failed")
ErrBuildFailed is returned by PollBuild when the build reaches "failed".
var ErrDeployFailed = errors.New("deploy failed")
ErrDeployFailed is returned by PollBuild when the new rollout cannot become healthy: the image can't be pulled, or the app keeps crashing.
var ErrEmptyResponse = errors.New("the API returned an empty response; retry, and report it if it keeps happening")
ErrEmptyResponse covers a 2xx answer with no payload: nothing the caller did causes it and no command can continue past it.
var UserAgent = "hostim-cli/dev"
UserAgent is sent on every request so CLI traffic can be told apart from the console in the backend access logs. cmd sets it to the build version.
Functions ¶
func Check ¶
Check returns an *APIError if the response status is >= 400, else nil. Generated response structs expose StatusCode() and a Body []byte field, so callers use client.Check(resp.StatusCode(), resp.Body).
func HasDockerfile ¶ added in v0.1.53
HasDockerfile reports whether dir contains dockerfile, defaulting to "Dockerfile" when the path is empty.
func IsStaticSite ¶ added in v0.1.54
IsStaticSite reports whether dir looks like built static-site output: an index.html at its root and no Dockerfile.
func MergeVars ¶ added in v0.1.22
MergeVars overlays updates onto base, replacing by name and appending new keys. The result is never nil, since the API rejects a null env array.
func New ¶
func New(r config.Resolved) (*api.ClientWithResponses, error)
New returns a ClientWithResponses that injects the Bearer token and transparently retries on 429 responses, honouring Retry-After.
func NewAnonymous ¶ added in v0.1.20
func NewAnonymous(apiURL string) (*api.ClientWithResponses, error)
NewAnonymous returns a client for the endpoints that take no token (the device-login flow), with the same 429 retry behaviour as New. Without it a single rate-limited poll would end a login that is otherwise fine.
func NormalizeApp ¶ added in v0.1.22
NormalizeApp replaces nil required-array fields with empty slices so they serialize as [] rather than null, which the API schema rejects.
func Pack ¶ added in v0.1.53
Pack writes a deterministic .tar.gz of dir to a temporary file and returns its path, hex SHA-256 and size. The same tree always gives the same hash, so a re-deploy is recognised as an existing upload. With static, dir is taken as built site output: every file is packed (a build folder is usually gitignored, and nothing in it reads .dockerignore), any root Dockerfile is replaced by an nginx one and the nginx config is added under .hostim/.
func ResolveProjectID ¶ added in v0.1.22
ResolveProjectID maps a project reference (friendly name or ID) to the ID the API expects in the {projectName} path parameter. An ID (hpr-…) is returned as-is; a name is looked up via GetProjects. Callers pass whatever the user typed, so both work everywhere a project is referenced.
func StaticFallback ¶ added in v0.1.54
StaticFallback reports whether a source that names dockerfile should be served as a static site instead: only for the default Dockerfile path, when dir has none but has an index.html.
func Upload ¶ added in v0.1.53
func Upload(ctx context.Context, c *api.ClientWithResponses, project, dir string, static bool, progress func(string)) (string, error)
Upload packs dir and stores it through the API, returning the archive's sha256. static packs dir as a static site (see Pack). progress, when set, is called with short status lines.
Types ¶
type APIError ¶
type APIError struct {
Status int
Code string // GenericMessage.error, when present
Message string // GenericMessage.message, when present
}
APIError is a structured error built from a non-2xx API response.
type Baseline ¶ added in v0.1.41
type Baseline struct {
BuildStatus api.AppStatusBuildStatus
LastBuiltAt *time.Time
LastDeployedAt *time.Time
// contains filtered or unexported fields
}
Baseline is an existing app's state captured before a deploy changes it. The operator acts on a rebuild request asynchronously, so for a few seconds after the request the status still describes the previous build and rollout. The baseline tells those stale values apart from the ones this deploy produces.
func NewBaseline ¶ added in v0.1.41
func NewBaseline(ctx context.Context, c *api.ClientWithResponses, project, app string) (*Baseline, error)
NewBaseline reads app's current status (and, after a failed build, its BuildFailed events) for PollBuild.
type BuildResult ¶
type BuildResult struct {
BuildStatus api.AppStatusBuildStatus
RuntimeStatus api.AppStatusRuntimeStatus
Status *api.AppStatus
}
BuildResult is the terminal outcome of a deploy poll.
func PollBuild ¶
func PollBuild( ctx context.Context, c *api.ClientWithResponses, project, app string, interval time.Duration, expectBuild bool, since *Baseline, onTick func(*api.AppStatus), ) (BuildResult, error)
PollBuild polls an app's status until the deploy reaches a terminal state or the context is cancelled. onTick, if non-nil, is called with each observed status so callers can render progress.
A git source (expectBuild) first waits for its build: succeeded moves on to the rollout, failed returns ErrBuildFailed. Then every deploy waits for the new rollout: running is success, imagePullBackoff is a definitive failure, and a crash seen on 3 consecutive polls is treated as one too; both return ErrDeployFailed. Other transient states (pending) keep polling.
since is the app's state before an update (nil for a create, which has no previous build or rollout to mistake for this one). With it set, a succeeded build counts only once lastBuiltAt moves past since, a failed one only once it is shown to be new, and the runtime status only once lastDeployedAt moves past since. lastDeployedAt is the creation time of the Deployment's newest ReplicaSet; the operator stamps hostim.dev/restarted-at into the pod template on every rebuild, so every deploy creates a new one.
type VolumeMount ¶ added in v0.1.22
type VolumeMount = struct {
MountPath *string `json:"mountPath,omitempty"`
Name *string `json:"name,omitempty"`
}
VolumeMount matches the anonymous struct type of App.VolumeMounts, so it can be assigned directly.
func ParseVolumeMounts ¶ added in v0.1.22
func ParseVolumeMounts(specs []string) ([]VolumeMount, error)
ParseVolumeMounts turns "name:/mount/path" values into app volume mounts.