Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions v23/services/application/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Make the Envelope type JSON-codeable.

package application

import (
"encoding/base64"
"encoding/json"
"time"

"v.io/v23/security"
"v.io/v23/verror"
"v.io/v23/vom"
)

const pkgPath = "v.io/v23/services/application"

var (
errCantVOMEncodePublisher = verror.Register(pkgPath+".errCantVOMEncodePublisher", verror.NoRetry, "{1:}{2:} failed to vom-encode Publisher{:_}")
errCantBase64DecodePublisher = verror.Register(pkgPath+".errCantBase64DecodePublisher", verror.NoRetry, "{1:}{2:} failed to base64-decode Publisher{:_}")
errCantVOMDecodePublisher = verror.Register(pkgPath+".errCantVOMDecodePublisher", verror.NoRetry, "{1:}{2:} failed to vom-decode Publisher{:_}")
)

type jsonType struct {
Title string
Args []string
Binary SignedFile
Publisher string // base64-vom-encoded security.Blessings
Env []string
Packages Packages
Restarts int32
RestartTimeWindow time.Duration
}

func (env Envelope) MarshalJSON() ([]byte, error) {
var bytes []byte
if !env.Publisher.IsZero() {
var err error
if bytes, err = vom.Encode(env.Publisher); err != nil {
return nil, verror.New(errCantVOMEncodePublisher, nil, err)
}
}
return json.Marshal(jsonType{
Title: env.Title,
Args: env.Args,
Binary: env.Binary,
Publisher: base64.URLEncoding.EncodeToString(bytes),
Env: env.Env,
Packages: env.Packages,
Restarts: env.Restarts,
RestartTimeWindow: env.RestartTimeWindow,
})
}

func (env *Envelope) UnmarshalJSON(input []byte) error {
var jt jsonType
if err := json.Unmarshal(input, &jt); err != nil {
return err
}
var publisher security.Blessings
if len(jt.Publisher) > 0 {
bytes, err := base64.URLEncoding.DecodeString(jt.Publisher)
if err != nil {
return verror.New(errCantBase64DecodePublisher, nil, err)
}
if err := vom.Decode(bytes, &publisher); err != nil {
return verror.New(errCantVOMDecodePublisher, nil, err)
}
}
env.Title = jt.Title
env.Args = jt.Args
env.Binary = jt.Binary
env.Publisher = publisher
env.Env = jt.Env
env.Packages = jt.Packages
env.Restarts = jt.Restarts
env.RestartTimeWindow = jt.RestartTimeWindow
return nil
}
81 changes: 81 additions & 0 deletions v23/services/application/application.vdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package application defines types for describing applications.
package application

import (
"time"
"v.io/v23/security"
)

// Device manager application envelopes must present this title.
const DeviceManagerTitle = "device manager"

// Envelope is a collection of metadata that describes an application.
type Envelope struct {
// Title is the publisher-assigned application title. Application
// installations with the same title are considered as belonging to the
// same application by the application management system.
//
// A change in the title signals a new application.
Title string
// Args is an array of command-line arguments to be used when executing
// the binary.
Args []string
// Binary identifies the application binary.
Binary SignedFile
// Publisher represents the set of blessings that have been bound to
// the principal who published this binary.
Publisher security.WireBlessings
// Env is an array that stores the environment variable values to be
// used when executing the binary.
Env []string
// Packages is the set of packages to install on the local filesystem
// before executing the binary
Packages Packages
// Restarts specifies how many times the device manager will attempt
// to automatically restart an application that has crashed before
// giving up and marking the application as NotRunning.
Restarts int32
// RestartTimeWindow is the time window within which an
// application exit is considered a crash that counts against the
// Restarts budget. If the application crashes after less than
// RestartTimeWindow time for Restarts consecutive times, the
// application is marked NotRunning and no more restart attempts
// are made. If the application has run continuously for more
// than RestartTimeWindow, subsequent crashes will again benefit
// from up to Restarts restarts (that is, the Restarts budget is
// reset by a successful run of at least RestartTimeWindow
// duration).
RestartTimeWindow time.Duration
}

// Packages represents a set of packages. The map key is the local
// file/directory name, relative to the instance's packages directory, where the
// package should be installed. For archives, this name represents a directory
// into which the archive is to be extracted, and for regular files it
// represents the name for the file. The map value is the package
// specification.
//
// Each object's media type determines how to install it.
//
// For example, with key=pkg1,value=SignedFile{File:binaryrepo/configfiles} (an
// archive), the "configfiles" package will be installed under the "pkg1"
// directory. With key=pkg2,value=SignedFile{File:binaryrepo/binfile} (a
// binary), the "binfile" file will be installed as the "pkg2" file.
//
// The keys must be valid file/directory names, without path separators.
//
// Any number of packages may be specified.
type Packages map[string]SignedFile

// SignedFile represents a file accompanied by a signature of its contents.
type SignedFile struct {
// File is the object name of the file.
File string
// Signature represents a signature on the sha256 hash of the file
// contents by the publisher principal.
Signature security.Signature
}
Loading