github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/stage1/attach/attach.go (about) 1 // Copyright 2016 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 package main 16 17 import ( 18 "flag" 19 "fmt" 20 "io/ioutil" 21 "os" 22 "os/exec" 23 24 "github.com/appc/spec/schema/types" 25 rktlog "github.com/rkt/rkt/pkg/log" 26 stage1common "github.com/rkt/rkt/stage1/common" 27 stage1initcommon "github.com/rkt/rkt/stage1/init/common" 28 ) 29 30 var ( 31 log *rktlog.Logger 32 diag *rktlog.Logger 33 34 app string 35 action string 36 debug bool 37 38 attachTTYIn bool 39 attachTTYOut bool 40 attachStdin bool 41 attachStdout bool 42 attachStderr bool 43 ) 44 45 func init() { 46 flag.StringVar(&action, "action", "list", "Action") 47 flag.StringVar(&app, "app", "", "Application name") 48 flag.BoolVar(&debug, "debug", false, "Run in debug mode") 49 50 flag.BoolVar(&attachTTYIn, "tty-in", false, "attach tty input") 51 flag.BoolVar(&attachTTYOut, "tty-out", false, "attach tty output") 52 flag.BoolVar(&attachStdin, "stdin", false, "attach stdin") 53 flag.BoolVar(&attachStdout, "stdout", false, "attach stdin") 54 flag.BoolVar(&attachStderr, "stderr", false, "attach stderr") 55 } 56 57 func main() { 58 flag.Parse() 59 60 stage1initcommon.InitDebug(debug) 61 62 log, diag, _ = rktlog.NewLogSet("stage1-attach", debug) 63 if !debug { 64 diag.SetOutput(ioutil.Discard) 65 } 66 67 appName, err := types.NewACName(app) 68 if err != nil { 69 log.PrintE("invalid application name", err) 70 os.Exit(254) 71 } 72 73 if action != "list" && action != "auto-attach" && action != "custom-attach" { 74 log.Printf("invalid attach action %q", action) 75 os.Exit(254) 76 } 77 78 args := stage1common.PrepareEnterCmd(false) 79 args = append(args, 80 "/iottymux", 81 fmt.Sprintf("--action=%s", action), 82 fmt.Sprintf("--app=%s", appName), 83 ) 84 85 cmd := exec.Cmd{ 86 Path: args[0], 87 Args: args, 88 Stdin: os.Stdin, 89 Stdout: os.Stdout, 90 Stderr: os.Stderr, 91 Env: []string{ 92 fmt.Sprintf("PATH=%s", os.Getenv("PATH")), 93 fmt.Sprintf("STAGE2_DEBUG=%t", debug), 94 fmt.Sprintf("STAGE2_ATTACH_TTYIN=%t", attachTTYIn), 95 fmt.Sprintf("STAGE2_ATTACH_TTYOUT=%t", attachTTYOut), 96 fmt.Sprintf("STAGE2_ATTACH_STDIN=%t", attachStdin), 97 fmt.Sprintf("STAGE2_ATTACH_STDOUT=%t", attachStdout), 98 fmt.Sprintf("STAGE2_ATTACH_STDERR=%t", attachStderr), 99 }, 100 } 101 102 if err := cmd.Run(); err != nil { 103 log.PrintE(`error executing "iottymux"`, err) 104 os.Exit(254) 105 } 106 107 os.Exit(0) 108 }