This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
app_start.go
95 lines (80 loc) · 2.62 KB
/
app_start.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2016 The rkt Authors
//
// 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.
//+build linux
package stage0
import (
"errors"
"fmt"
"os"
"github.com/appc/spec/schema/types"
"github.com/hashicorp/errwrap"
"github.com/rkt/rkt/common"
pkgPod "github.com/rkt/rkt/pkg/pod"
)
type StartConfig struct {
*CommonConfig
PodPath string
UsesOverlay bool
AppName *types.ACName
PodPID int
}
func StartApp(cfg StartConfig) error {
pod, err := pkgPod.PodFromUUIDString(cfg.DataDir, cfg.UUID.String())
if err != nil {
return errwrap.Wrap(errors.New("error loading pod"), err)
}
defer pod.Close()
pm, err := pod.SandboxManifest()
if err != nil {
return errwrap.Wrap(errors.New("cannot start application"), err)
}
app := pm.Apps.Get(*cfg.AppName)
if app == nil {
return fmt.Errorf("error: nonexistent app %q", *cfg.AppName)
}
args := []string{
fmt.Sprintf("--debug=%t", cfg.Debug),
fmt.Sprintf("--app=%s", cfg.AppName),
}
appStatusPath := common.AppStatusPath(cfg.PodPath, cfg.AppName.String())
appStartedPath := common.AppStartedPath(cfg.PodPath, cfg.AppName.String())
// The app may be restarted. In this case the /rkt/status/app and
// rkt/status/app-started files already exist.
// We could touch app-started file and compare its mtime with the app status file
// to determine the actual status but this won't work if the server time was modified.
//
// Instead we:
// 1. remove the app-started file
// In this window rkt app list may report that the application exited.
// 2. remove the app status file
// In this window rkt app list may report that the app was created.
// 3. re-create the the app-started file
_ = os.Remove(appStartedPath)
_ = os.Remove(appStatusPath)
if _, err := os.Create(appStartedPath); err != nil {
log.FatalE(fmt.Sprintf("error creating %s-started file", cfg.AppName.String()), err)
}
ce := CrossingEntrypoint{
PodPath: cfg.PodPath,
PodPID: cfg.PodPID,
AppName: cfg.AppName.String(),
EntrypointName: appStartEntrypoint,
EntrypointArgs: args,
Interactive: false,
}
if err := ce.Run(); err != nil {
return err
}
return nil
}