github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/stage1/init/common/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 common
    18  
    19  import (
    20  	"path/filepath"
    21  
    22  	"github.com/appc/spec/schema/types"
    23  	"github.com/coreos/go-systemd/unit"
    24  	"github.com/coreos/rkt/common"
    25  )
    26  
    27  const (
    28  	// UnitsDir is the default path to systemd systemd unit directory
    29  	UnitsDir        = "/usr/lib/systemd/system"
    30  	envDir          = "/rkt/env" // TODO(vc): perhaps this doesn't belong in /rkt?
    31  	defaultWantsDir = UnitsDir + "/default.target.wants"
    32  	socketsWantsDir = UnitsDir + "/sockets.target.wants"
    33  )
    34  
    35  // ServiceUnitName returns a systemd service unit name for the given app name.
    36  func ServiceUnitName(appName types.ACName) string {
    37  	return appName.String() + ".service"
    38  }
    39  
    40  // ServiceUnitPath returns the path to the systemd service file for the given
    41  // app name.
    42  func ServiceUnitPath(root string, appName types.ACName) string {
    43  	return filepath.Join(common.Stage1RootfsPath(root), UnitsDir, ServiceUnitName(appName))
    44  }
    45  
    46  // RelEnvFilePath returns the path to the environment file for the given app name
    47  // relative to the pod's root.
    48  func RelEnvFilePath(appName types.ACName) string {
    49  	return filepath.Join(envDir, appName.String())
    50  }
    51  
    52  // EnvFilePath returns the path to the environment file for the given app name.
    53  func EnvFilePath(root string, appName types.ACName) string {
    54  	return filepath.Join(common.Stage1RootfsPath(root), RelEnvFilePath(appName))
    55  }
    56  
    57  // ServiceWantPath returns the systemd default.target want symlink path for the
    58  // given app name.
    59  func ServiceWantPath(root string, appName types.ACName) string {
    60  	return filepath.Join(common.Stage1RootfsPath(root), defaultWantsDir, ServiceUnitName(appName))
    61  }
    62  
    63  // InstantiatedPrepareAppUnitName returns the systemd service unit name for prepare-app
    64  // instantiated for the given root.
    65  func InstantiatedPrepareAppUnitName(appName types.ACName) string {
    66  	// Naming respecting escaping rules, see systemd.unit(5) and systemd-escape(1)
    67  	escapedRoot := unit.UnitNamePathEscape(common.RelAppRootfsPath(appName))
    68  	return "prepare-app@" + escapedRoot + ".service"
    69  }
    70  
    71  // SocketUnitName returns a systemd socket unit name for the given app name.
    72  func SocketUnitName(appName types.ACName) string {
    73  	return appName.String() + ".socket"
    74  }
    75  
    76  // SocketUnitPath returns the path to the systemd socket file for the given app name.
    77  func SocketUnitPath(root string, appName types.ACName) string {
    78  	return filepath.Join(common.Stage1RootfsPath(root), UnitsDir, SocketUnitName(appName))
    79  }
    80  
    81  // SocketWantPath returns the systemd sockets.target.wants symlink path for the
    82  // given app name.
    83  func SocketWantPath(root string, appName types.ACName) string {
    84  	return filepath.Join(common.Stage1RootfsPath(root), socketsWantsDir, SocketUnitName(appName))
    85  }