Skip to content

Commit

Permalink
logs added,code formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
logeshkrish committed Jan 12, 2021
1 parent ba8d6ee commit 4fbbc9a
Show file tree
Hide file tree
Showing 15 changed files with 885 additions and 524 deletions.
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file renamed sail → cmd/gpctl
Binary file not shown.
141 changes: 72 additions & 69 deletions cmd/sail.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,57 @@
package main


import (

"encoding/json"
"flag"
"fmt"
"gopaddle/sail/misc"
trace "gopaddle/sail/trace"
"net/http"
util "gopaddle/sail/util"
"gopaddle/sail/util/cmd"
"log"
"os"
"bytes"
"os/exec"

flag "github.com/spf13/pflag"
)

func main() {

listCommand := flag.NewFlagSet("list", flag.ExitOnError)
dockerizeCommand := flag.NewFlagSet("dockerize", flag.ExitOnError)
helpCommand := flag.NewFlagSet("help", flag.ExitOnError)
listTextPtr := listCommand.String("all", "", "What to list (Required), list --all process")
listHelpPtr := listCommand.Bool("help",false,"Help regarding commands")
dockerizeTextPtr := dockerizeCommand.String("pid", "", "pid of process (Required)")
dockerizeTimePtr := dockerizeCommand.Int("time",2,"Time for which trace commands runs")
dockerizeImagePtr := dockerizeCommand.String("imageName","final","Final image name")
dockerizeHelpPtr := dockerizeCommand.Bool("help",false,"Help regarding commands")
listTextPtr := listCommand.StringP("all", "a", "", "list --all process")
listHelpPtr := listCommand.BoolP("help", "h", false, "Help regarding commands")
dockerizeTextPtr := dockerizeCommand.StringP("pid", "p", "", "pid of the process to trace")
dockerizeTimePtr := dockerizeCommand.IntP("time", "t", 2, "Time in seconds to trace the process to build its docker profile")
dockerizeImagePtr := dockerizeCommand.StringP("imageName", "i", "final", "Name of the final docker image")
dockerizeHelpPtr := dockerizeCommand.BoolP("help", "h", false, "Help regarding commands")

if len(os.Args) < 2 {
fmt.Println("Command required.")
fmt.Println("To check the commands possible, run sail help")
fmt.Println("To check the commands possible, run sail --help or -h")
os.Exit(1)
}

switch os.Args[1] {
case "list":
listCommand.Parse(os.Args[2:])
case "dockerize":
dockerizeCommand.Parse(os.Args[2:])
case "help":
dockerizeCommand.Parse(os.Args[2:])
case "--help":
helpCommand.Parse(os.Args[2:])
case "-h":
helpCommand.Parse(os.Args[2:])
default:
fmt.Println("Invalid command. Please check sail help for available options")
flag.PrintDefaults()
os.Exit(1)
}

if listCommand.Parsed() {

if *listHelpPtr == true {

fmt.Print("\nArguments\n")
fmt.Printf("%-12s%s\n","all","consider all processes (syntax : saillist --all process)")
fmt.Print("List the Process running on the Machine for the current user. \n")
fmt.Print("\n")
fmt.Printf(" sail list (--all | -a) process.\n")
os.Exit(0)
}

Expand All @@ -57,7 +60,7 @@ func main() {
os.Exit(1)
}

if os.Args[3] == "process" {
if len(os.Args) >= 3 && os.Args[3] == "process" {
out, err := exec.Command("ps", "-eo", "pid,ppid,cmd").Output()
if err != nil {
fmt.Printf("%s", err)
Expand All @@ -66,71 +69,71 @@ func main() {
fmt.Println(output)
} else {
fmt.Println("Enter complete command (try : sail list --all process)")
}
}
}

if dockerizeCommand.Parsed() {


if *dockerizeHelpPtr == true {

fmt.Print("\nArguments\n")
fmt.Printf("%-12s%s\n","pid","Process pid (required field)")
fmt.Printf("%-12s%s\n","time","Time for which the process should be traced")
fmt.Printf("%-12s%s\n","imageName","Final image name the image created should be stored with (Default imageName is : final)")
fmt.Print("Migrate a running linux process in to a Docker Image. \n")
fmt.Print("\n")
fmt.Printf("sail dockerize --pid <process id> [--time <time in seconds>] [--imageName <docker image name>]\n")
fmt.Print("\n")
fmt.Printf(" %-20s%s\n", "-p, --pid", "pid of the process to trace.")
fmt.Printf(" %-20s%s\n", "-t, --time", "Time in seconds to trace the process to build its docker profile. Defaults to 2 seconds.")
fmt.Printf(" %-20s%s\n", "-i, --imageName", "Name of the final docker image. Defaults to 'final'.")
os.Exit(0)
}

if *dockerizeTextPtr == "" {
dockerizeCommand.PrintDefaults()
os.Exit(1)
}
values4 := map[string]string{"finalimagename": *dockerizeImagePtr,"home": "/tmp"}
dir := [2]string{"packages.log", "pkg_install.sh"}
values3 := map[string][2]string{"dirs": dir}
url2 := "http://localhost:9000/api/1/v1/dockercopy"
url3 := "http://localhost:9000/api/1/v1/finalimage"
client := &http.Client{}
json2, err3 := json.Marshal(values3)
if err3 != nil {
panic(err3)
}
json3, err4 := json.Marshal(values4)
if err4 != nil {
panic(err4)
}
trace.StartTracing_noreq(*dockerizeTextPtr,*dockerizeTimePtr)
osname := "ubuntu"
osver := "20.04"
trace.DockerCreate_noreq(osname, osver, "final")
req1, err1 := http.NewRequest(http.MethodPut, url2, bytes.NewBuffer(json2))
if err1 != nil {
fmt.Println(err1)
}
req1.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err2 := client.Do(req1)
if err2 != nil {
fmt.Println(err2)
}
fmt.Print("Copying log file of trace to container...")
req3, err6 := http.NewRequest(http.MethodPut, url3, bytes.NewBuffer(json3))
if err6 != nil {
fmt.Println(err6)
}
req1.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err5 := client.Do(req3)
if err5 != nil {
fmt.Println(err5)
//values4 := map[string]string{"finalimagename": *dockerizeImagePtr,"home": "/tmp"}
dir := []string{"/tmp/sail", "packages.fmt"}

requestID := util.NewRequestID()
defer func() {
if r := recover(); r != nil {
misc.PanicHandler(r, requestID)
}
fmt.Print(*dockerizeImagePtr + " created")
fmt.Println(" ",resp.StatusCode)
fmt.Println("To check the image, use command : docker image inspect " + *dockerizeImagePtr)
}()

// Start Tracing
log.Println("start tracing...")
if _, err := trace.StartTracing_noreq(*dockerizeTextPtr, *dockerizeTimePtr, requestID); err != nil {
log.Println("tracing failed :", err.Error())
}
log.Println("tracing completed")

// Docker Create
_, osname, osver, _ := cmd.GetOS()
log.Println("Docker creating...")
if _, err := trace.DockerCreate_noreq(osname, osver, "final", requestID); err != nil {
log.Println("Docker container creation failed :", err.Error())
}
log.Println("Docker creating completed")

//Docker Copy
log.Println("Docker file copying ...")
if _, err := trace.DockerCopy_noreq(dir, requestID); err != nil {
log.Println("Docker file copy failed :", err.Error())
}
log.Println("Docker file copying completed")
log.Println("Copying fmt file of trace to container...")

//Docker final image create
trace.FinalImageCreate_noreq("$HOME", *dockerizeImagePtr, requestID)
log.Println(*dockerizeImagePtr + " created")
log.Println("To check the image, use command : docker image inspect " + *dockerizeImagePtr)

}

if helpCommand.Parsed() {
fmt.Println("Enter sail [command] --help for more details on specific commands and their arguments.\nUse --[argument] for additional arguments")
fmt.Println("\nCommands")
fmt.Printf("%-12s%s\n","list","list processes")
fmt.Printf("%-12s%s\n","dockerize","given a process id (pid),it traces the process, creates a container for it and creates the image")
fmt.Print("Migrate a running linux process in to a Docker Image. \n")
fmt.Println("\nEnter sail <command> --help or -h for more details on specific commands and their arguments.")
fmt.Println("\nCommands:")
fmt.Printf(" %-12s%s\n", "dockerize", "Migrate a linux process to Docker Image")
fmt.Printf(" %-12s%s\n", "list", "List all processes owned by the current user")
}
}
7 changes: 6 additions & 1 deletion directory/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"gopaddle/sail/util/json"
"io/ioutil"
"log"
"os"
"strings"
)

Expand Down Expand Up @@ -75,7 +76,11 @@ func LoadConfig(env string) bool {

log.Println("Environment: ", env)

osFamily, osName, osVersion := cmd.GetOS()
osFamily, osName, osVersion, err := cmd.GetOS()
if err != nil {
log.Println("Error while loading Env: ", err)
os.Exit(0)
}
context.Instance().Set("osFamily", osFamily)
context.Instance().Set("osName", osName)
context.Instance().Set("osVersion", osVersion)
Expand Down
36 changes: 36 additions & 0 deletions misc/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package misc

import (
"encoding/json"
"gopaddle/sail/directory"
gson "gopaddle/sail/util/json"
"gopaddle/sail/util/log"
"runtime"
)

func BuildHTTPErrorJSON(message string, requestID string) string {
errJSON, e := json.Marshal(Error{Message: message, RequestID: requestID})
if e != nil {
log.Errorln(e)
}
return string(errJSON)
}

// PanicHandler To handle server break from syntax errors
func PanicHandler(r interface{}, requestID string) Response {
resp := Response{Code: 500}
bytes := make([]byte, 2000)
runtime.Stack(bytes, true)
log.Errorf("Panic Message: %v and Error stack: %s", r, string(bytes))
err := gson.New()
err.Put("requestID", requestID)
err.Put("reason", directory.ErrorFmt("All", "SOMETHING_WRONG"))
resp.Response = err.ToString()
switch x := r.(type) {
case Error:
err.Put("reason", x.Message)
resp.Response = err.ToString()
}

return resp
}
38 changes: 38 additions & 0 deletions misc/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package misc

import (
"gopaddle/clustermanager/cluster/dao"
"gopaddle/clustermanager/util/bhttp"

"github.com/sirupsen/logrus"
)

type InternalContext struct {
DS dao.ReadWritter
Log *logrus.Entry
HTTP bhttp.HTTPInterface
}

type MiscHandle struct {
InternalContext
AccountID string
RequestID string
}

type BaseContext struct {
AccountID string
RequestID string
InternalContext
MiscHandle MiscHandle
}

type Response struct {
Code int
Response string
}

type Error struct {
Message string `json:"message"`

RequestID string `json:"requestID"`
}
Loading

0 comments on commit 4fbbc9a

Please sign in to comment.