gcp

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2017 License: GPL-3.0 Imports: 37 Imported by: 0

Documentation

Overview

Package gcp provides implementations of interfaces defined in cloud package.

Index

Constants

View Source
const (

	// AuthSucceedURL is a page showing authentication success.
	AuthSucceedURL = "https://jkawamoto.github.io/roadie/auth/succeed/"
	// AuthErrorURL is a page showing authentication error.
	AuthErrorURL = "https://jkawamoto.github.io/roadie/auth/error/"
)
View Source
const (

	// StoragePrefix is a prefix used to store related data into the cloud
	// storage.
	StoragePrefix = ".roadie"
	// DefaultZone defines the default zone.
	DefaultZone = "us-central1-b"
	// DefaultMachineType defines the default machine type.
	DefaultMachineType = "n1-standard-1"
	// DefaultDiskSize defines the default disk size.
	DefaultDiskSize = 10

	// DefaultBaseImage defines the default base image for sandbox containers.
	DefaultBaseImage = "jkawamoto/roadie-gcp"

	// LogTimeFormat defines time format of Google Logging.
	LogTimeFormat = "2006-01-02T15:04:05Z"
	// LogEventSubtypeInsert means this event is creating an instance.
	LogEventSubtypeInsert = "compute.instances.insert"
	// LogEventSubtypeDelete means this event is deleting an instance.
	LogEventSubtypeDelete = "compute.instances.delete"

	// StatusRunning is a status representing an instance is running.
	StatusRunning = "running"
	// StatusTerminated is a status representing an instance is terminated.
	StatusTerminated = "terminated"
)
View Source
const (
	// QueueKind defines kind of entries stored in cloud datastore.
	QueueKind = "roadie-queue"
	// QueueManagerVersion defines the version of queue manager to be used.
	QueueManagerVersion = "0.2.3"
)
View Source
const (
	// TaskStatusWaiting represents a task is waiting to be run.
	TaskStatusWaiting = "waiting"
	// TaskStatusRunning represents a task is running.
	TaskStatusRunning = "running"
	// TaskStatusPending represents a task is pending to be run.
	TaskStatusPending = "pending"
)
View Source
const (
	// DefaultIgnitionVersion defines the version of ignition configuration.
	DefaultIgnitionVersion = "2.0.0"
)
View Source
const (
	// SourceImage defines the ID of the source image to be used for instance.
	SourceImage = "projects/coreos-cloud/global/images/coreos-stable-1298-7-0-v20170401"
)

Variables

View Source
var (
	// CodeVerifierChars defines a set of characters used to generate a code verifier.
	CodeVerifierChars []byte
)
View Source
var (
	// RegexpCommentLine defines a regular expression for a comment line.
	RegexpCommentLine = regexp.MustCompile("#.*\n")
)
View Source
var (
	// RoadieSchemeURLOffset defines an offset value to remove scheme name from
	// URLs.
	RoadieSchemeURLOffset = len(script.RoadieSchemePrefix)
)

Functions

func CreateURL

func CreateURL(cfg *Config, name string) string

CreateURL creates a valid URL for uploaing object.

func GenerateCodeVerifier added in v0.3.6

func GenerateCodeVerifier() (codeVerifier []byte, err error)

GenerateCodeVerifier generates a plain code verifier.

func NewAuthorizationConfig added in v0.3.6

func NewAuthorizationConfig(port int) *oauth2.Config

NewAuthorizationConfig creates a new configuration for authorization.

func ReplaceURLScheme

func ReplaceURLScheme(cfg *Config, task *script.Script)

ReplaceURLScheme replaced URLs which start with "roadie://". Those URLs are modified to "gs://<bucketname>/.roadie/".

func RequestToken added in v0.3.6

func RequestToken(ctx context.Context, output io.Writer) (token *oauth2.Token, err error)

RequestToken requests a new authorization token.

Types

type ActivityPayload

type ActivityPayload struct {
	EventTimestampUs string `structpb:"event_timestamp_us"`
	EventType        string `structpb:"vent_type"`
	TraceID          string `structpb:"trace_id"`
	Actor            struct {
		User string
	}
	Resource struct {
		Zone string
		Type string
		ID   string
		Name string
	}
	Version      string
	EventSubtype string `structpb:"event_subtype"`
	Operation    struct {
		Zone string
		Type string
		ID   string
		Name string
	}
}

ActivityPayload defines the payload structure of activity log.

func NewActivityPayload

func NewActivityPayload(payload interface{}) (res *ActivityPayload, err error)

NewActivityPayload converts LogEntry's payload to a ActivityPayload.

type ActivityPayloadHandler

type ActivityPayloadHandler func(time.Time, *ActivityPayload) error

ActivityPayloadHandler is a function type to handle ActivityPayloads.

type ComputeService

type ComputeService struct {
	Config    *Config
	Logger    *log.Logger
	SleepTime time.Duration
}

ComputeService implements cloud.InstanceManager based on Google Cloud Platform.

func NewComputeService

func NewComputeService(cfg *Config, logger *log.Logger) *ComputeService

NewComputeService creates a new compute service client.

func (*ComputeService) AvailableMachineTypes

func (s *ComputeService) AvailableMachineTypes(ctx context.Context) (types []cloud.MachineType, err error)

AvailableMachineTypes returns a slice of machie type names.

func (*ComputeService) AvailableRegions

func (s *ComputeService) AvailableRegions(ctx context.Context) (regions []cloud.Region, err error)

AvailableRegions returns a slice of region information.

func (*ComputeService) CreateInstance

func (s *ComputeService) CreateInstance(ctx context.Context, task *script.Script) (err error)

CreateInstance creates a new instance based on the builder's configuration.

func (*ComputeService) DeleteInstance

func (s *ComputeService) DeleteInstance(ctx context.Context, name string) (err error)

DeleteInstance deletes a given named instance.

func (*ComputeService) Instances

func (s *ComputeService) Instances(ctx context.Context, handler cloud.InstanceHandler) (err error)

Instances is different from instances and sends names of instances which are not working for any queue into the given handler.

type Config

type Config struct {
	// Project name.
	Project string `yaml:"project"`
	// Bucket name
	Bucket string `yaml:"bucket"`
	// Zone where instances will run.
	Zone string `yaml:"zone"`
	// Default machine type of new instances.
	MachineType string `yaml:"machine_type"`
	// Instance disk size.
	DiskSize int64 `yaml:"disk_size,omitempty"`
	// If true, instances will not shutdown automatically.
	NoShutdown bool `yaml:"no_shutdown,omitempty"`
	// Authorization token.
	Token *oauth2.Token `yaml:"token,omitempty"`
}

Config defines information to access Google Cloud Platform.

func (*Config) UnmarshalYAML

func (cfg *Config) UnmarshalYAML(unmarshal func(interface{}) error) (err error)

UnmarshalYAML helps to unmarshal Config objects.

type EntryHandler

type EntryHandler func(*loggingpb.LogEntry) error

EntryHandler is a function type to handler Entries.

type IgnitionConfig

type IgnitionConfig struct {
	Ignition IgnitionVersion `json:"ignition,omitempty"`
	Systemd  SystemdConfig   `json:"systemd,omitempty"`
}

IgnitionConfig defines a simple structure for ignition config file which has only systemd's unit configurations.

{
  "ignition": { "version": "2.0.0" },
  "systemd": {
    "units": [{
      "name": "example.service",
      "enable": true,
      "contents": "[Service]\nType=oneshot\nExecStart=/usr/bin/echo Hello World\n\n[Install]\nWantedBy=multi-user.target"
    }]
  }
}

func NewIgnitionConfig

func NewIgnitionConfig() *IgnitionConfig

NewIgnitionConfig creates a new ignition configuration.

func (*IgnitionConfig) Append

func (c *IgnitionConfig) Append(unit SystemdUnit) *IgnitionConfig

Append a new unit and returns updated this configuration.

func (*IgnitionConfig) String

func (c *IgnitionConfig) String() string

String marshals this config to a JSON string.

type IgnitionVersion

type IgnitionVersion struct {
	Version string `json:"version,omitempty"`
}

IgnitionVersion is configuration of ignition version.

type LogManager

type LogManager struct {
	// Config is a reference for a configuration of GCP.
	Config    *Config
	Logger    *log.Logger
	SleepTime time.Duration
}

LogManager implements cloud.LogManager interface. It requests logs to google cloud logging service.

func NewLogManager

func NewLogManager(cfg *Config, logger *log.Logger) (m *LogManager)

NewLogManager creates a new log manager.

func (*LogManager) Delete

func (s *LogManager) Delete(ctx context.Context, instanceName string) error

Delete doesn't do anything.

func (*LogManager) Entries

func (s *LogManager) Entries(ctx context.Context, filter string, handler EntryHandler) (err error)

Entries get log entries matching with a given filter from given project logs. Found log entries will be passed a given handler one by one. If the handler returns non-nil value as an error, this function will end.

func (*LogManager) Get

func (s *LogManager) Get(ctx context.Context, instanceName string, from time.Time, handler cloud.LogHandler) (err error)

Get requests log entries of the given named instance.

func (*LogManager) GetQueueLog

func (s *LogManager) GetQueueLog(ctx context.Context, queue string, handler cloud.LogHandler) (err error)

GetQueueLog retrieves log entries from a queue.

func (*LogManager) GetTaskLog

func (s *LogManager) GetTaskLog(ctx context.Context, queue, task string, handler cloud.LogHandler) (err error)

GetTaskLog retrieves log entries for a task in a queue.

func (*LogManager) InstanceLogEntries

func (s *LogManager) InstanceLogEntries(ctx context.Context, instanceName string, from time.Time, handler RoadiePayloadHandler) error

InstanceLogEntries requests log entries of a given instance. Obtained log entries will be passed a given handler entry by entry. If the handler returns non nil value, obtaining log entries is canceled immediately.

func (*LogManager) OperationLogEntries

func (s *LogManager) OperationLogEntries(ctx context.Context, from time.Time, handler ActivityPayloadHandler) error

OperationLogEntries requests log entries about google cloud platform operations. Obtained log entries will be passed a given handler entry by entry. If the handler returns non nil value, obtaining log entries is canceled immediately.

type Provider

type Provider struct {
	Config *Config
	Logger *log.Logger
}

Provider defines information for the service provider for GCP.

func NewProvider

func NewProvider(ctx context.Context, cfg *Config, logger *log.Logger, forceAuth bool) (p *Provider, err error)

NewProvider creates a new provider for GCP service.

func (*Provider) InstanceManager

func (p *Provider) InstanceManager(ctx context.Context) (cloud.InstanceManager, error)

InstanceManager returns an instance manager interface.

func (*Provider) LogManager

func (p *Provider) LogManager(ctx context.Context) (cloud.LogManager, error)

LogManager returns a log manager interface.

func (*Provider) QueueManager

func (p *Provider) QueueManager(ctx context.Context) (cloud.QueueManager, error)

QueueManager returns a queue manager interface.

func (*Provider) ResourceManager

func (p *Provider) ResourceManager(ctx context.Context) (cloud.ResourceManager, error)

ResourceManager returns a resource manager interface.

func (*Provider) StorageManager

func (p *Provider) StorageManager(ctx context.Context) (cloud.StorageManager, error)

StorageManager returns a storage manager interface.

type QueueService

type QueueService struct {
	Config *Config
	Logger *log.Logger
}

QueueService implements cloud.QueueManager based on Google Cloud Datastore.

func NewQueueService

func NewQueueService(ctx context.Context, cfg *Config, logger *log.Logger) (*QueueService, error)

NewQueueService creates an interface for a queue service based on Google Cloud Datastore.

func (*QueueService) CreateWorkers

func (s *QueueService) CreateWorkers(ctx context.Context, queue string, n int, handler cloud.QueueManagerNameHandler) (err error)

CreateWorkers creates worker instances working for a given named queue.

func (*QueueService) DeleteQueue

func (s *QueueService) DeleteQueue(ctx context.Context, queue string) (err error)

DeleteQueue deletes a given named queue. This function deletes all tasks in a given queue and deletes all workers for that queue.

func (*QueueService) DeleteTask

func (s *QueueService) DeleteTask(ctx context.Context, queue, task string) (err error)

DeleteTask deletes a given named task in a given named queue.

func (*QueueService) Enqueue

func (s *QueueService) Enqueue(ctx context.Context, queue string, task *script.Script) (err error)

Enqueue add a given script to a given named queue.

func (*QueueService) Fetch

func (s *QueueService) Fetch(ctx context.Context, queue string) (task *Task, err error)

Fetch retrieves one task from a queue and returns it; status of the returned task is updated to running. If there is no task, return nil with nil error.

func (*QueueService) Queues

func (s *QueueService) Queues(ctx context.Context, handler cloud.QueueStatusHandler) (err error)

Queues retrieves existing queue names.

func (*QueueService) Restart

func (s *QueueService) Restart(ctx context.Context, queue string) (err error)

Restart executing tasks in a queue which has a given name.

func (*QueueService) Stop

func (s *QueueService) Stop(ctx context.Context, queue string) error

Stop executing tasks in a queue which has a given name.

func (*QueueService) Tasks

func (s *QueueService) Tasks(ctx context.Context, queue string, handler cloud.QueueManagerTaskHandler) (err error)

Tasks retrieves tasks in a given names queue.

func (*QueueService) UpdateTask

func (s *QueueService) UpdateTask(ctx context.Context, queue string, modifier func(*Task) *Task) (err error)

UpdateTask updates tasks in a given named queue with a given modifier.

func (*QueueService) Workers

func (s *QueueService) Workers(ctx context.Context, queue string, handler cloud.QueueManagerNameHandler) (err error)

Workers retrieves worker instance names for a given queue.

type ResourceService

type ResourceService struct {
	Config *Config
	Logger *log.Logger
}

ResourceService is a service to get and set cloud configuration.

func NewResourceService

func NewResourceService(cfg *Config, logger *log.Logger) *ResourceService

NewResourceService creates a new resource service.

func (*ResourceService) GetMachineType

func (m *ResourceService) GetMachineType() string

GetMachineType returns a machine type the current project uses by default.

func (*ResourceService) GetProjectID

func (m *ResourceService) GetProjectID() string

GetProjectID returns an ID of the current project.

func (*ResourceService) GetRegion

func (m *ResourceService) GetRegion() string

GetRegion returns a region name the current project working on.

func (*ResourceService) MachineTypes

func (m *ResourceService) MachineTypes(ctx context.Context) ([]cloud.MachineType, error)

MachineTypes returns a set of available machine types.

func (*ResourceService) Regions

func (m *ResourceService) Regions(ctx context.Context) ([]cloud.Region, error)

Regions returns a set of available regions.

func (*ResourceService) SetMachineType

func (m *ResourceService) SetMachineType(t string)

SetMachineType sets a machine type as the default one.

func (*ResourceService) SetProjectID

func (m *ResourceService) SetProjectID(id string)

SetProjectID sets an ID to the current project.

func (*ResourceService) SetRegion

func (m *ResourceService) SetRegion(region string)

SetRegion sets a region to the current project.

type RoadiePayloadHandler

type RoadiePayloadHandler func(time.Time, string) error

RoadiePayloadHandler is a function type to handle RoadiePayloads.

type StorageService

type StorageService struct {
	// Config is a reference for a configuration of GCP.
	Config *Config
	// Logger
	Logger *log.Logger
}

StorageService implements cloud.StorageServicer interface for accessing GCP's cloud storage.

func NewStorageService

func NewStorageService(ctx context.Context, cfg *Config, logger *log.Logger) (s *StorageService, err error)

NewStorageService creates a new storage accessor to a bucket specified in a given configuration.

func (*StorageService) Delete

func (s *StorageService) Delete(ctx context.Context, loc *url.URL) (err error)

Delete deletes a given file.

func (*StorageService) Download

func (s *StorageService) Download(ctx context.Context, loc *url.URL, out io.Writer) (err error)

Download downloads a file and write it to a given writer.

func (*StorageService) GetFileInfo

func (s *StorageService) GetFileInfo(ctx context.Context, loc *url.URL) (info *cloud.FileInfo, err error)

GetFileInfo returns a file status of an object.

func (*StorageService) List

func (s *StorageService) List(ctx context.Context, loc *url.URL, handler cloud.FileInfoHandler) (err error)

List searches items, i.e. files and folders, matching a given prefix. Found items will be passed to a given handler item by item. If the handler returns a non nil value, listing up will be canceled. In that case, this function will also return the given value.

func (*StorageService) Upload

func (s *StorageService) Upload(ctx context.Context, loc *url.URL, in io.Reader) (err error)

Upload a file to a location.

type SystemdConfig

type SystemdConfig struct {
	Units []SystemdUnit `json:"units,omitempty"`
}

SystemdConfig defines a set of units to be set up.

type SystemdUnit

type SystemdUnit struct {
	Name     string `json:"name,omitempty"`
	Enable   bool   `json:"enable,omitempty"`
	Contents string `json:"contents,omitempty"`
}

SystemdUnit is configuration for a systemd unit.

func FluentdUnit

func FluentdUnit(name string) (unit SystemdUnit, err error)

FluentdUnit creates a new SystemdUnit for fluentd.

func LogcastUnit

func LogcastUnit(service string) (unit SystemdUnit, err error)

LogcastUnit creates a new unit for forwarding log to Fluentd.

func QueueManagerUnit

func QueueManagerUnit(project, version, queueName string) (unit SystemdUnit, err error)

QueueManagerUnit creates a new unit for Roadie queue manager.

func RoadieUnit

func RoadieUnit(name, image, options string) (unit SystemdUnit, err error)

RoadieUnit creates a new SystemdUnit for roadie-gcp.

type Task

type Task struct {
	// Name of this task.
	Name string `yaml:"name,omitempty"`
	// The script body.
	Script *script.Script `yaml:"script,omitempty"`
	// Queue name.
	QueueName string `yaml:"queue-name"`
	// Status of this task
	Status string `yaml:"status"`
}

Task defines a data structure of enqueued script file.

Jump to

Keyboard shortcuts

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