github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/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/rkt/rkt/common"
    25  )
    26  
    27  const (
    28  	// UnitsDir is the default path to systemd unit directory
    29  	UnitsDir        = "/usr/lib/systemd/system"
    30  	envDir          = "/rkt/env"
    31  	statusDir       = "/rkt/status"
    32  	ioMuxDir        = "/rkt/iottymux"
    33  	defaultWantsDir = UnitsDir + "/default.target.wants"
    34  	socketsWantsDir = UnitsDir + "/sockets.target.wants"
    35  )
    36  
    37  // ServiceUnitName returns a systemd service unit name for the given app name.
    38  func ServiceUnitName(appName types.ACName) string {
    39  	return appName.String() + ".service"
    40  }
    41  
    42  // ServiceUnitPath returns the path to the systemd service file for the given
    43  // app name.
    44  func ServiceUnitPath(root string, appName types.ACName) string {
    45  	return filepath.Join(common.Stage1RootfsPath(root), UnitsDir, ServiceUnitName(appName))
    46  }
    47  
    48  // ServiceUnitPath returns the path to the systemd service file for the given
    49  // app name.
    50  func TargetUnitPath(root string, name string) string {
    51  	return filepath.Join(common.Stage1RootfsPath(root), UnitsDir, name+".target")
    52  }
    53  
    54  // RelEnvFilePath returns the path to the environment file for the given
    55  // app name relative to the pod's root.
    56  func RelEnvFilePath(appName types.ACName) string {
    57  	return filepath.Join(envDir, appName.String())
    58  }
    59  
    60  // EnvFilePath returns the path to the environment file for the given app name.
    61  func EnvFilePath(root string, appName types.ACName) string {
    62  	return filepath.Join(common.Stage1RootfsPath(root), RelEnvFilePath(appName))
    63  }
    64  
    65  // IOMUxFilePath returns the path to the environment file for the given app name.
    66  func IOMuxDir(root string, appName types.ACName) string {
    67  	return filepath.Join(common.Stage1RootfsPath(root), ioMuxDir, appName.String())
    68  }
    69  
    70  // ServiceWantPath returns the systemd default.target want symlink path for the
    71  // given app name.
    72  func ServiceWantPath(root string, appName types.ACName) string {
    73  	return filepath.Join(common.Stage1RootfsPath(root), defaultWantsDir, ServiceUnitName(appName))
    74  }
    75  
    76  // InstantiatedPrepareAppUnitName returns the systemd service unit name for prepare-app
    77  // instantiated for the given root.
    78  func InstantiatedPrepareAppUnitName(appName types.ACName) string {
    79  	// Naming respecting escaping rules, see systemd.unit(5) and systemd-escape(1)
    80  	escapedRoot := unit.UnitNamePathEscape(common.RelAppRootfsPath(appName))
    81  	return "prepare-app@-" + escapedRoot + ".service"
    82  }
    83  
    84  // SocketUnitName returns a systemd socket unit name for the given app name.
    85  func SocketUnitName(appName types.ACName) string {
    86  	return appName.String() + ".socket"
    87  }
    88  
    89  // SocketUnitPath returns the path to the systemd socket file for the given app name.
    90  func SocketUnitPath(root string, appName types.ACName) string {
    91  	return filepath.Join(common.Stage1RootfsPath(root), UnitsDir, SocketUnitName(appName))
    92  }
    93  
    94  // SocketWantPath returns the systemd sockets.target.wants symlink path for the
    95  // given app name.
    96  func SocketWantPath(root string, appName types.ACName) string {
    97  	return filepath.Join(common.Stage1RootfsPath(root), socketsWantsDir, SocketUnitName(appName))
    98  }
    99  
   100  // TypedUnitPath returns the path to a custom-typed unit file
   101  func TypedUnitPath(root string, unitName string, unitType string) string {
   102  	return filepath.Join(common.Stage1RootfsPath(root), UnitsDir, unitName+"."+unitType)
   103  }