2019-02-21 14:54:50 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// 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 driver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-05-09 10:36:30 +00:00
|
|
|
"fmt"
|
2019-02-21 14:54:50 +00:00
|
|
|
"io"
|
2019-05-09 10:36:30 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-04-22 12:38:25 +00:00
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/services/executor/registry"
|
2019-07-31 13:39:07 +00:00
|
|
|
"agola.io/agola/services/types"
|
2019-02-21 14:54:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-05-09 10:36:30 +00:00
|
|
|
toolboxPrefix = "agola-toolbox"
|
|
|
|
|
2019-04-22 16:19:43 +00:00
|
|
|
labelPrefix = "agola.io/"
|
|
|
|
|
|
|
|
agolaLabelKey = labelPrefix + "agola"
|
2019-02-21 14:54:50 +00:00
|
|
|
agolaLabelValue = "true"
|
|
|
|
|
2019-04-22 16:19:43 +00:00
|
|
|
executorIDKey = labelPrefix + "executorid"
|
|
|
|
podIDKey = labelPrefix + "podid"
|
|
|
|
taskIDKey = labelPrefix + "taskid"
|
2019-02-21 14:54:50 +00:00
|
|
|
|
2019-04-22 16:19:43 +00:00
|
|
|
containerIndexKey = labelPrefix + "containerindex"
|
2019-02-21 14:54:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Driver is a generic interface around the pod concept (a group of "containers"
|
|
|
|
// sharing, at least, the same network namespace)
|
|
|
|
// It's just tailored aroun the need of an executor and should be quite generic
|
|
|
|
// to work with multiple implementations. For example:
|
|
|
|
// * Docker containers
|
|
|
|
// * Kubernetes pods
|
|
|
|
// * A Virtual Machine on which we execute multiple processes
|
|
|
|
type Driver interface {
|
2019-04-22 16:17:55 +00:00
|
|
|
Setup(ctx context.Context) error
|
2019-03-13 14:48:35 +00:00
|
|
|
NewPod(ctx context.Context, podConfig *PodConfig, out io.Writer) (Pod, error)
|
2019-04-24 10:37:55 +00:00
|
|
|
GetPods(ctx context.Context, all bool) ([]Pod, error)
|
|
|
|
ExecutorGroup(ctx context.Context) (string, error)
|
|
|
|
GetExecutors(ctx context.Context) ([]string, error)
|
2019-07-31 13:39:07 +00:00
|
|
|
Archs(ctx context.Context) ([]types.Arch, error)
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Pod interface {
|
|
|
|
// ID returns the pod id
|
|
|
|
ID() string
|
2019-04-24 10:37:55 +00:00
|
|
|
// ExecutorID return the pod owner executor id
|
|
|
|
ExecutorID() string
|
|
|
|
// TaskID return the pod task id
|
|
|
|
TaskID() string
|
2019-02-21 14:54:50 +00:00
|
|
|
// Stop stops the pod
|
|
|
|
Stop(ctx context.Context) error
|
|
|
|
// Stop stops the pod
|
|
|
|
Remove(ctx context.Context) error
|
|
|
|
// Exec executes a command inside the first container in the Pod
|
|
|
|
Exec(ctx context.Context, execConfig *ExecConfig) (ContainerExec, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ContainerExec interface {
|
|
|
|
Stdin() io.WriteCloser
|
|
|
|
Wait(ctx context.Context) (int, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type PodConfig struct {
|
2019-04-22 15:53:34 +00:00
|
|
|
ID string
|
2019-04-24 10:37:55 +00:00
|
|
|
TaskID string
|
2019-02-21 14:54:50 +00:00
|
|
|
Containers []*ContainerConfig
|
2019-07-31 13:39:07 +00:00
|
|
|
Arch types.Arch
|
2019-02-21 14:54:50 +00:00
|
|
|
// The container dir where the init volume will be mounted
|
|
|
|
InitVolumeDir string
|
2019-04-22 12:38:25 +00:00
|
|
|
DockerConfig *registry.DockerConfig
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ContainerConfig struct {
|
2019-04-22 12:38:25 +00:00
|
|
|
Cmd []string
|
|
|
|
Env map[string]string
|
|
|
|
WorkingDir string
|
|
|
|
Image string
|
|
|
|
User string
|
|
|
|
Privileged bool
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExecConfig struct {
|
2019-05-10 14:50:31 +00:00
|
|
|
Cmd []string
|
|
|
|
Env map[string]string
|
|
|
|
WorkingDir string
|
|
|
|
User string
|
|
|
|
AttachStdin bool
|
|
|
|
Stdout io.Writer
|
|
|
|
Stderr io.Writer
|
|
|
|
Tty bool
|
2019-02-21 14:54:50 +00:00
|
|
|
}
|
2019-05-09 10:36:30 +00:00
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func toolboxExecPath(toolboxDir string, arch types.Arch) (string, error) {
|
2019-05-09 10:36:30 +00:00
|
|
|
toolboxPath := filepath.Join(toolboxDir, fmt.Sprintf("%s-linux-%s", toolboxPrefix, arch))
|
|
|
|
_, err := os.Stat(toolboxPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return toolboxPath, nil
|
|
|
|
}
|