github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/stage1/init/path.go (about)

     1  // Copyright 2014 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //+build linux
    16  
    17  package main
    18  
    19  import (
    20  	"path/filepath"
    21  
    22  	"github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/schema/types"
    23  	"github.com/coreos/rkt/Godeps/_workspace/src/github.com/coreos/go-systemd/unit"
    24  	"github.com/coreos/rkt/common"
    25  )
    26  
    27  const (
    28  	envDir          = "/rkt/env" // TODO(vc): perhaps this doesn't belong in /rkt?
    29  	unitsDir        = "/usr/lib/systemd/system"
    30  	defaultWantsDir = unitsDir + "/default.target.wants"
    31  	socketsWantsDir = unitsDir + "/sockets.target.wants"
    32  )
    33  
    34  // ServiceUnitName returns a systemd service unit name for the given app name.
    35  func ServiceUnitName(appName types.ACName) string {
    36  	return appName.String() + ".service"
    37  }
    38  
    39  // ServiceUnitPath returns the path to the systemd service file for the given
    40  // app name.
    41  func ServiceUnitPath(root string, appName types.ACName) string {
    42  	return filepath.Join(common.Stage1RootfsPath(root), unitsDir, ServiceUnitName(appName))
    43  }
    44  
    45  // RelEnvFilePath returns the path to the environment file for the given app name
    46  // relative to the pod's root.
    47  func RelEnvFilePath(appName types.ACName) string {
    48  	return filepath.Join(envDir, appName.String())
    49  }
    50  
    51  // EnvFilePath returns the path to the environment file for the given app name.
    52  func EnvFilePath(root string, appName types.ACName) string {
    53  	return filepath.Join(common.Stage1RootfsPath(root), RelEnvFilePath(appName))
    54  }
    55  
    56  // ServiceWantPath returns the systemd default.target want symlink path for the
    57  // given app name.
    58  func ServiceWantPath(root string, appName types.ACName) string {
    59  	return filepath.Join(common.Stage1RootfsPath(root), defaultWantsDir, ServiceUnitName(appName))
    60  }
    61  
    62  // InstantiatedPrepareAppUnitName returns the systemd service unit name for prepare-app
    63  // instantiated for the given root.
    64  func InstantiatedPrepareAppUnitName(appName types.ACName) string {
    65  	// Naming respecting escaping rules, see systemd.unit(5) and systemd-escape(1)
    66  	escaped_root := unit.UnitNamePathEscape(common.RelAppRootfsPath(appName))
    67  	return "prepare-app@" + escaped_root + ".service"
    68  }
    69  
    70  // SocketUnitName returns a systemd socket unit name for the given app name.
    71  func SocketUnitName(appName types.ACName) string {
    72  	return appName.String() + ".socket"
    73  }
    74  
    75  // SocketUnitPath returns the path to the systemd socket file for the given app name.
    76  func SocketUnitPath(root string, appName types.ACName) string {
    77  	return filepath.Join(common.Stage1RootfsPath(root), unitsDir, SocketUnitName(appName))
    78  }
    79  
    80  // SocketWantPath returns the systemd sockets.target.wants symlink path for the
    81  // given app name.
    82  func SocketWantPath(root string, appName types.ACName) string {
    83  	return filepath.Join(common.Stage1RootfsPath(root), socketsWantsDir, SocketUnitName(appName))
    84  }