systemd

package
v0.0.0-...-2bb29a1 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2014 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Systemd concerns for gears: unit files, socket activation, event subscriptions, etc.

Index

Constants

This section is empty.

Variables

View Source
var ContainerSocketTemplate = template.Must(template.New("unit.socket").Parse(`
[Unit]
Description=Container socket {{.Id}}

[Socket]
{{range .PortPairs}}ListenStream={{.External}}
{{end}}

[Install]
WantedBy=container-sockets.target
`))
View Source
var ContainerUnitTemplate = template.Must(template.New("unit.service").Parse(`
{{define "COMMON_UNIT"}}
[Unit]
Description=Container {{.Id}}
{{end}}

{{define "COMMON_SERVICE"}}
[Service]
Type=simple
TimeoutStartSec=5m
{{ if .Slice }}Slice={{.Slice}}{{ end }}
{{ if .EnvironmentPath }}EnvironmentFile={{.EnvironmentPath}}{{ end }}
{{end}}

{{define "COMMON_CONTAINER"}}
[Install]
WantedBy=container.target

# Container information
X-ContainerId={{.Id}}
X-ContainerImage={{.Image}}
X-ContainerUserId={{.User}}
X-ContainerRequestId={{.ReqId}}
X-ContainerType={{ if .Isolate }}isolated{{ else }}simple{{ end }}
{{range .PortPairs}}X-PortMapping={{.Internal}}:{{.External}}
{{end}}
{{end}}

{{/* A unit that lets docker own the container processes and only integrates via API */}}
{{define "SIMPLE"}}
{{template "COMMON_UNIT" .}}
{{template "COMMON_SERVICE" .}}
# Create data container
ExecStartPre=/bin/sh -c '/usr/bin/docker inspect --format="Reusing {{"{{.Id}}"}}" "{{.Id}}-data" || \
             exec docker run --name "{{.Id}}-data" {{.VolumeSpec}} --entrypoint /bin/true "{{.Image}}"'
ExecStartPre=-/usr/bin/docker rm "{{.Id}}"
{{ if .Isolate }}# Initialize user and volumes
ExecStartPre={{.ExecutablePath}} init --pre "{{.Id}}" "{{.Image}}"{{ end }}
ExecStart=/usr/bin/docker run --rm --name "{{.Id}}" \
          --volumes-from "{{.Id}}-data" \
          {{ if and .EnvironmentPath .DockerFeatures.EnvironmentFile }}--env-file "{{ .EnvironmentPath }}"{{ end }} \
          -a stdout -a stderr {{.PortSpec}} {{.RunSpec}} {{.BindMountSpec}} \
          {{ if .Isolate }} -v {{.RunDir}}:/.container.init:ro -u root {{end}} \
          "{{.Image}}" {{ if .Isolate }} /.container.init/container-init.sh {{ end }}
# Set links (requires container have a name)
ExecStartPost=-{{.ExecutablePath}} init --post "{{.Id}}" "{{.Image}}"
ExecReload=-/usr/bin/docker stop "{{.Id}}"
ExecReload=-/usr/bin/docker rm "{{.Id}}"
ExecStop=-/usr/bin/docker stop "{{.Id}}"
{{template "COMMON_CONTAINER" .}}
{{end}}

{{/* A unit that uses Docker with the 'foreground' flag to run an image under the current context */}}
{{define "FOREGROUND"}}
{{template "COMMON_UNIT" .}}
{{template "COMMON_SERVICE" .}}
# Create data container
ExecStartPre=/bin/sh -c '/usr/bin/docker inspect --format="Reusing {{"{{.Id}}"}}" "{{.Id}}-data" || \
             exec docker run --name "{{.Id}}-data" {{.VolumeSpec}} --entrypoint /bin/true "{{.Image}}"'
ExecStartPre=-/usr/bin/docker rm "{{.Id}}"
{{ if .Isolate }}# Initialize user and volumes
ExecStartPre={{.ExecutablePath}} init --pre "{{.Id}}" "{{.Image}}"{{ end }}
ExecStart=/usr/bin/docker run --rm --foreground \
          {{ if and .EnvironmentPath .DockerFeatures.EnvironmentFile }}--env-file "{{ .EnvironmentPath }}"{{ end }} \
          {{.PortSpec}} {{.RunSpec}} {{.BindMountSpec}} \
          --name "{{.Id}}" --volumes-from "{{.Id}}-data" \
          {{ if .Isolate }} -v {{.RunDir}}:/.container.init:ro -u root {{end}} \
          "{{.Image}}" {{ if .Isolate }} /.container.init/container-init.sh {{ end }}
# Set links (requires container have a name)
ExecStartPost=-{{.ExecutablePath}} init --post "{{.Id}}" "{{.Image}}"
{{template "COMMON_CONTAINER" .}}
{{end}}

{{/* A unit that exposes socket activation and process isolation */}}
{{define "SOCKETACTIVATED"}}
{{template "COMMON_UNIT" .}}
BindsTo={{.SocketUnitName}}

{{template "COMMON_SERVICE" .}}
ExecStartPre={{.ExecutablePath}} init --pre "{{.Id}}" "{{.Image}}"
ExecStart=/usr/bin/docker run \
            --name "{{.Id}}" \
            --volumes-from "{{.Id}}" \
            {{ if and .EnvironmentPath .DockerFeatures.EnvironmentFile }}--env-file "{{ .EnvironmentPath }}"{{ end }} \
            -a stdout -a stderr {{.RunSpec}} \
            --env LISTEN_FDS \
            -v {{.RunDir}}:/.container.init:ro \
            -v /usr/sbin/systemd-socket-proxyd:/usr/sbin/systemd-socket-proxyd:ro \
            -u root -f --rm \
            "{{.Image}}" /.container.init/container-init.sh
ExecStartPost=-{{.ExecutablePath}} init --post "{{.Id}}" "{{.Image}}"
{{template "COMMON_CONTAINER" .}}
X-SocketActivated={{.SocketActivationType}}
{{end}}

{{/* Run DEFAULT */}}
{{template "SIMPLE" .}}
`))
View Source
var SliceUnitTemplate = template.Must(template.New("unit.slice").Parse(`
[Unit]
Description=Container slice {{.Name}}

[Slice]
CPUAccounting=yes
MemoryAccounting=yes
MemoryLimit={{.MemoryLimit}}
{{ if .Parent }}Slice={{.Parent}}{{ end }}

[Install]
WantedBy=container.target container-active.target
`))
View Source
var TargetUnitTemplate = template.Must(template.New("unit.target").Parse(`
[Unit]
Description=Container target {{.Name}}

[Install]
WantedBy={{.WantedBy}}
`))

Functions

func GetSocketActivation

func GetSocketActivation(id containers.Identifier) (bool, string, error)

func SetUnitStartOnBoot

func SetUnitStartOnBoot(i containers.Identifier, active bool) error

func UnitStartOnBoot

func UnitStartOnBoot(i containers.Identifier) (bool, error)

Types

type ContainerEvent

type ContainerEvent struct {
	Id   containers.Identifier
	Type EventType
}

func (ContainerEvent) String

func (e ContainerEvent) String() string

type ContainerUnit

type ContainerUnit struct {
	Id            containers.Identifier
	Image         string
	PortSpec      string
	RunSpec       string
	VolumeSpec    string
	BindMountSpec string
	Slice         string
	Isolate       bool
	User          string
	ReqId         string

	HomeDir         string
	RunDir          string
	EnvironmentPath string
	ExecutablePath  string
	IncludePath     string

	PortPairs            port.PortPairs
	SocketUnitName       string
	SocketActivationType string

	DockerFeatures config.DockerFeatures
}

type EventListener

type EventListener struct {
	// contains filtered or unexported fields
}

func NewEventListener

func NewEventListener() (*EventListener, error)

func (*EventListener) Close

func (e *EventListener) Close()

func (*EventListener) Run

func (e *EventListener) Run() (<-chan *ContainerEvent, <-chan error)

func (*EventListener) Stop

func (e *EventListener) Stop()

type EventType

type EventType int
const (
	Unknown EventType = iota
	Started
	Idled
	Stopped
	Deleted
	Errored
)

type SliceUnit

type SliceUnit struct {
	Name        string
	Parent      string
	MemoryLimit string
}

type TargetUnit

type TargetUnit struct {
	Name     string
	WantedBy string
}

Directories

Path Synopsis
The gear 'init' extension - setup environment for containers.
The gear 'init' extension - setup environment for containers.

Jump to

Keyboard shortcuts

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