From 9584d6f68c3bb225a767fcafb2cc3443cee18495 Mon Sep 17 00:00:00 2001 From: sharadregoti Date: Wed, 22 Jan 2020 01:17:28 +0530 Subject: [PATCH 1/2] tested login commands --- actions.go | 21 ++-- cmd/code.go | 16 ++- cmd/creds.go | 12 ++- cmd/helper.go | 12 +-- cmd/login.go | 37 +++---- cmd/setup.go | 125 ++++++++++++++++++++++++ go.mod | 7 +- go.sum | 22 +++-- main.go | 10 +- model/cli.go | 9 +- model/service.go | 5 +- runner/config.go | 16 --- runner/driver/driver.go | 1 + runner/handle.go | 1 - runner/services/do/digitalocean.go | 2 +- runner/services/do/digitalocean_test.go | 4 +- runner/services/services.go | 4 +- 17 files changed, 227 insertions(+), 77 deletions(-) create mode 100644 cmd/setup.go diff --git a/actions.go b/actions.go index e4b4ec5..2eff572 100644 --- a/actions.go +++ b/actions.go @@ -6,15 +6,16 @@ import ( "strings" "github.com/sirupsen/logrus" + "github.com/urfave/cli" + "github.com/spaceuptech/galaxy/cmd" "github.com/spaceuptech/galaxy/model" "github.com/spaceuptech/galaxy/proxy" "github.com/spaceuptech/galaxy/runner" "github.com/spaceuptech/galaxy/runner/driver" - "github.com/spaceuptech/galaxy/runner/services" + "github.com/spaceuptech/galaxy/runner/services" "github.com/spaceuptech/galaxy/server" "github.com/spaceuptech/galaxy/utils/auth" - "github.com/urfave/cli" ) func actionRunner(c *cli.Context) error { @@ -146,16 +147,20 @@ func actionBuildCode(c *cli.Context) error { func actionLogin(c *cli.Context) error { userName := c.String("username") key := c.String("key") + serverUrl := c.String("url") local := c.Bool("local") - tempurl := c.String("url") - url := "url1" + url := "url1" // todo set default url if local { - url = "ur2" + url = "localhost:4122" } - if tempurl != "default url" { - url = tempurl + if serverUrl != "default url" { // todo get default url + url = serverUrl } - if err := cmd.LoginStart(userName, key, url, local); err != nil { + return cmd.LoginStart(userName, key, url, local) +} + +func actionSetup(c *cli.Context) error { + if err := cmd.CodeSetup(); err != nil { return err } return nil diff --git a/cmd/code.go b/cmd/code.go index 726bab7..cd79517 100644 --- a/cmd/code.go +++ b/cmd/code.go @@ -12,13 +12,16 @@ import ( "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/mount" "github.com/docker/docker/client" + "github.com/sirupsen/logrus" + "github.com/spaceuptech/galaxy/model" ) -//CodeStart starts the code commands +// CodeStart starts the code commands func CodeStart(envID string) (*model.Service, *model.LoginResponse, error) { credential, err := getCreds() if err != nil { + logrus.Errorf("error code start unable to get credentials got error message - %v", err) return nil, nil, err } @@ -26,6 +29,7 @@ func CodeStart(envID string) (*model.Service, *model.LoginResponse, error) { loginRes, err := login(selectedAccount) if err != nil { + logrus.Errorf("error code start unable to login got error message - %v", err) return nil, nil, err } @@ -33,13 +37,14 @@ func CodeStart(envID string) (*model.Service, *model.LoginResponse, error) { if err != nil { c, err = generateServiceConfig(loginRes.Projects, selectedAccount, envID) if err != nil { + logrus.Errorf("error code start unable to generate service config got error message - %v", err) return nil, nil, err } } return c, loginRes, nil } -func generateServiceConfig(projects []model.Projects, selectedaccount *model.Account, envID string) (*model.Service, error) { +func generateServiceConfig(projects []*model.Projects, selectedaccount *model.Account, envID string) (*model.Service, error) { progLang, err := getProgLang() if err != nil { return nil, err @@ -121,8 +126,9 @@ func generateServiceConfig(projects []model.Projects, selectedaccount *model.Acc return c, nil } -//RunDockerFile starts a container using go docker client +// RunDockerFile starts a container using go docker client func RunDockerFile(s *model.ActionCode, loginResp *model.LoginResponse) error { + s.Condition = "create" dir, err := os.Getwd() if err != nil { return err @@ -136,13 +142,15 @@ func RunDockerFile(s *model.ActionCode, loginResp *model.LoginResponse) error { if err != nil { return err } + // todo potential bug we are running runtime image here + // todo potential bug in builder image worddir is /builder but in sh script we use cd /build resp, err := cli.ContainerCreate(ctx, &container.Config{ Image: s.Service.Tasks[0].Docker.Image, Env: []string{ "FILE_PATH=/", fmt.Sprintf("URL=%s", s.Service.Tasks[0].Env["URL"]), - fmt.Sprintf("TOKEN=%s", loginResp.FileToken), + fmt.Sprintf("TOKEN=%s", loginResp.Token), fmt.Sprintf("meta=%s", string(sa))}, }, &container.HostConfig{Mounts: []mount.Mount{ diff --git a/cmd/creds.go b/cmd/creds.go index e35df69..55761f1 100644 --- a/cmd/creds.go +++ b/cmd/creds.go @@ -4,8 +4,10 @@ import ( "fmt" "io/ioutil" - "github.com/spaceuptech/galaxy/model" + "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" + + "github.com/spaceuptech/galaxy/model" ) func getSelectedAccount(credential *model.Credential) *model.Account { @@ -36,22 +38,29 @@ func checkCred(selectedAccount *model.Account) error { fileName := fmt.Sprintf("/%s/galaxy/config.yaml", getHomeDirectory()) yamlFile, err := ioutil.ReadFile(fileName) if err != nil { + // create config.yaml file credential := model.Credential{ Accounts: []model.Account{*selectedAccount}, SelectedAccount: selectedAccount.ID, } if err := generateYamlFile(&credential); err != nil { + logrus.Errorf("error checking credentials unable to generate login yaml file got error message - %v", err) return err } + return nil } + credential := new(model.Credential) if err := yaml.Unmarshal(yamlFile, credential); err != nil { + logrus.Errorf("error checking credentials unable to unmarshal login yaml file got error message - %v", err) return err } for _, val := range credential.Accounts { + // update existing login details if val.ID == selectedAccount.ID { val.ID, val.UserName, val.Key, val.ServerUrl = selectedAccount.ID, selectedAccount.UserName, selectedAccount.Key, selectedAccount.ServerUrl if err := generateYamlFile(credential); err != nil { + logrus.Errorf("error checking credentials unable to update config yaml file got error message - %v", err) return err } return nil @@ -60,6 +69,7 @@ func checkCred(selectedAccount *model.Account) error { credential.Accounts = append(credential.Accounts, *selectedAccount) credential.SelectedAccount = selectedAccount.ID if err := generateYamlFile(credential); err != nil { + logrus.Errorf("error checking credentials unable to update login yaml file got error message - %v", err) return err } return nil diff --git a/cmd/helper.go b/cmd/helper.go index 28b2e34..65a3acb 100644 --- a/cmd/helper.go +++ b/cmd/helper.go @@ -75,7 +75,7 @@ func getImage(progLang string) (string, error) { case "nodejs": return "spaceuptech/runtime-node", nil case "go": - return "spaceuptech/runtime-alpine", nil + return "spaceuptech/runtime-golang", nil default: return "", fmt.Errorf("%s is not supported", progLang) } @@ -94,25 +94,25 @@ func getCmd(progLang string) []string { } } -func getProject(projectID string, projects []model.Projects) (*model.Projects, error) { +func getProject(projectID string, projects []*model.Projects) (*model.Projects, error) { for _, project := range projects { if projectID == project.ID { - return &project, nil + return project, nil } } return nil, fmt.Errorf("Invalid Project Name") } -func getEnvironment(envID string, environments []model.Environment) (*model.Environment, error) { +func getEnvironment(envID string, environments []*model.Environment) (*model.Environment, error) { for _, env := range environments { if envID == env.ID { - return &env, nil + return env, nil } } return nil, errors.New("Invalid Project Name") } -func getProjects(projects []model.Projects) []string { +func getProjects(projects []*model.Projects) []string { var projnames []string for _, val := range projects { projnames = append(projnames, fmt.Sprintf("%s (%s)", val.ID, val.Name)) diff --git a/cmd/login.go b/cmd/login.go index 9a10be6..a77a5a5 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -4,53 +4,53 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" "net/http" "github.com/AlecAivazis/survey/v2" + "github.com/sirupsen/logrus" + "github.com/spaceuptech/galaxy/model" "github.com/spaceuptech/galaxy/utils" ) func login(selectedAccount *model.Account) (*model.LoginResponse, error) { requestBody, err := json.Marshal(map[string]string{ - "username": selectedAccount.UserName, - "key": selectedAccount.Key, + "user": selectedAccount.UserName, + "key": selectedAccount.Key, }) if err != nil { + logrus.Errorf("error in login unable to marshal data for login got error message - %v", err) return nil, err } - resp, err := http.Post(fmt.Sprintf("%s/v1/galaxy/login", selectedAccount.ServerUrl), "application/json", bytes.NewBuffer(requestBody)) + resp, err := http.Post(fmt.Sprintf("http://%s/v1/config/login", selectedAccount.ServerUrl), "application/json", bytes.NewBuffer(requestBody)) if err != nil { + logrus.Errorf("error in login unable to send http request for login got error message - %v", err) return nil, err } defer utils.CloseReaderCloser(resp.Body) - if resp.StatusCode != 200 { - return nil, err - } - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } loginResp := new(model.LoginResponse) - if err := json.Unmarshal(body, loginResp); err != nil { - return nil, err + _ = json.NewDecoder(resp.Body).Decode(loginResp) + + if resp.StatusCode != 200 { + logrus.Errorf("error in login unable to login got status code %v with error message - %v", resp.StatusCode, loginResp.Error) + return nil, fmt.Errorf("%v", loginResp.Error) } return loginResp, nil - } -//LoginStart logs the user in galaxy +// LoginStart logs the user in galaxy func LoginStart(userName, key, url string, local bool) error { if userName == "None" { - if err := survey.AskOne(&survey.Input{Message: "Enter username"}, &userName); err != nil { + if err := survey.AskOne(&survey.Input{Message: "Enter username:"}, &userName); err != nil { + logrus.Error("error starting login unable to get username from user got error message -%v", err) return err } } if key == "None" { - if err := survey.AskOne(&survey.Input{Message: "Enter key"}, &key); err != nil { + if err := survey.AskOne(&survey.Password{Message: "Enter key:"}, &key); err != nil { + logrus.Error("error starting login unable to get key from user got error message -%v", err) return err } } @@ -63,8 +63,9 @@ func LoginStart(userName, key, url string, local bool) error { if err != nil { return err } + fmt.Printf("Login Successful\n") selectedAccount = model.Account{ - ID: loginRes.AccountID, + ID: loginRes.Token, UserName: userName, Key: key, ServerUrl: url, diff --git a/cmd/setup.go b/cmd/setup.go new file mode 100644 index 0000000..1381698 --- /dev/null +++ b/cmd/setup.go @@ -0,0 +1,125 @@ +package cmd + +import ( + "context" + "fmt" + "io" + "log" + "os" + + "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/container" + "github.com/docker/docker/api/types/mount" + "github.com/docker/docker/client" + "github.com/docker/go-connections/nat" + "github.com/sirupsen/logrus" + "github.com/txn2/txeh" +) + +// CodeSetup initializes development environment +func CodeSetup() error { + const dockerImageSpaceCloud string = "spaceuptech/space-cloud" + containersToCreate := []struct { + containerName string + commands []string + exposedPorts nat.PortSet + portMapping nat.PortMap + }{ + { + containerName: "space-cloud-gateway", + commands: []string{"./space-cloud", "run", "-dev"}, + exposedPorts: nat.PortSet{ + "4122": struct{}{}, + }, + portMapping: nat.PortMap{ + "4122": []nat.PortBinding{{HostIP: "localhost", HostPort: "4122"}}, + }, + }, + { + containerName: "space-cloud-runner", + commands: []string{"./space-cloud", "run", "-dev"}, + }, + + { + containerName: "space-cloud-artifact-store", + commands: []string{"./space-cloud", "run", "-dev"}, + }, + } + + ctx := context.Background() + cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) + if err != nil { + logrus.Errorf("error cli setup unable to initialize docker client got error message - %v", err) + return err + } + + hosts, err := txeh.NewHostsDefault() + if err != nil { + logrus.Errorf("error cli setup unable to load host file with suitable default got error message - %v", err) + return err + } + + // pull image from docker hub + out, err := cli.ImagePull(ctx, dockerImageSpaceCloud, types.ImagePullOptions{}) + if err != nil { + logrus.Errorf("error cli setup unable to pull image from docker hub got error message - %v", err) + return err + } + io.Copy(os.Stdout, out) + + if err := os.MkdirAll(fmt.Sprintf("%s/space-cloud-ip-table", getHomeDirectory()), 0755); err != nil { + logrus.Errorf("error cli setup unable to create directory for storing host file got error message - %v", err) + return err + } + + hostFilePath := fmt.Sprintf("%s/space-cloud-ip-table/.space-cloud-ip_table.hosts", getHomeDirectory()) + hostFileDir := fmt.Sprintf("%s/space-cloud-ip-table", getHomeDirectory()) + + if err := hosts.SaveAs(hostFilePath); err != nil { + logrus.Errorf("error cli setup unable to save host file to specified path (%s) got error message - %v", hostFilePath, err) + return err + } + + // change the default host file location for crud operation to our specified path + // default value /etc/hosts + hosts.WriteFilePath = hostFilePath + + for _, c := range containersToCreate { + resp, err := cli.ContainerCreate(ctx, &container.Config{ + Image: dockerImageSpaceCloud, + Cmd: c.commands, + ExposedPorts: c.exposedPorts, + }, &container.HostConfig{ + Mounts: []mount.Mount{ + { + Type: mount.TypeBind, + Source: hostFileDir, + Target: "/space-cloud-ip-table", + }, + }, + PortBindings: c.portMapping, + }, nil, c.containerName) + if err != nil { + logrus.Errorf("error cli setup unable to create container %s got error message - %v", c.containerName, err) + return err + } + + if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { + logrus.Errorf("error cli setup unable to start container %s got error message - %v", c.containerName, err) + return err + } + + data, err := cli.ContainerInspect(ctx, c.containerName) + if err != nil { + logrus.Errorf("error cli setup unable to inspect container %s got error message - %v", c.containerName, err) + } + log.Println("container name:", data.Name) + hosts.AddHost(data.NetworkSettings.IPAddress, c.containerName) + } + + if err := hosts.Save(); err != nil { + logrus.Errorf("error cli setup unable to save host file got error message - %v", err) + return err + } + return nil +} diff --git a/go.mod b/go.mod index 6895023..76c1341 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.13 require ( github.com/AlecAivazis/survey/v2 v2.0.5 + github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect github.com/Microsoft/go-winio v0.4.14 // indirect github.com/containerd/containerd v1.3.2 // indirect github.com/dgraph-io/badger v1.6.0 @@ -11,20 +12,22 @@ require ( github.com/digitalocean/godo v1.29.0 github.com/docker/distribution v2.7.1+incompatible // indirect github.com/docker/docker v1.4.2-0.20190927142053-ada3c14355ce - github.com/docker/go-connections v0.4.0 // indirect + github.com/docker/go-connections v0.4.0 github.com/docker/go-units v0.4.0 // indirect github.com/gogo/protobuf v1.3.0 github.com/gorilla/mux v1.7.3 github.com/gorilla/websocket v1.4.1 + github.com/morikuni/aec v1.0.0 // indirect github.com/opencontainers/go-digest v1.0.0-rc1 // indirect github.com/opencontainers/image-spec v1.0.1 // indirect github.com/rs/cors v1.7.0 github.com/segmentio/ksuid v1.0.2 github.com/sirupsen/logrus v1.4.2 - github.com/stretchr/testify v1.3.0 + github.com/txn2/txeh v1.3.0 github.com/urfave/cli v1.22.2 golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 gopkg.in/yaml.v2 v2.2.7 + gotest.tools v2.2.0+incompatible // indirect istio.io/api v0.0.0-20191109011911-e51134872853 istio.io/client-go v0.0.0-20191206191348-5c576a7ecef0 k8s.io/api v0.0.0-20191114100352-16d7abae0d2a diff --git a/go.sum b/go.sum index fd19f4a..c162ea1 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/AlecAivazis/survey/v2 v2.0.5 h1:xpZp+Q55wi5C7Iaze+40onHnEkex1jSc34Clt github.com/AlecAivazis/survey/v2 v2.0.5/go.mod h1:WYBhg6f0y/fNYUuesWQc0PKbJcEliGcYHB9sNT3Bg74= github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9 h1:HD8gA2tkByhMAwYaFAX9w2l7vxvBQ5NMoxDrkhqhtn4= github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8= +github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8= +github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8= github.com/Azure/go-autorest/autorest v0.9.0/go.mod h1:xyHB1BMZT0cuDHU7I0+g046+BFDTQ8rEZB0s4Yfa6bI= github.com/Azure/go-autorest/autorest/adal v0.5.0/go.mod h1:8Z9fGy2MpX0PvDjB1pEgQTmVqjGhiHBW7RJJEciWzS0= github.com/Azure/go-autorest/autorest/date v0.1.0/go.mod h1:plvfp3oPSKwf2DNjlBjWF/7vwR+cUD/ELuzDCXwHUVA= @@ -48,7 +50,6 @@ github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4Kfc github.com/docker/docker v1.4.2-0.20190927142053-ada3c14355ce h1:H3csZuxZESJeeEiOxq4YXPNmLFbjl7u2qVBrAAGX/sA= github.com/docker/docker v1.4.2-0.20190927142053-ada3c14355ce/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/docker v1.13.1 h1:IkZjBSIc8hBjLpqeAbeE5mca5mNgeatLHBy3GO78BWo= -github.com/docker/docker v1.13.1/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw= @@ -71,6 +72,7 @@ github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dp github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= github.com/gogo/protobuf v1.3.0 h1:G8O7TerXerS4F6sx9OV7/nRfJdnXgHZu/S/7F2SN+UE= github.com/gogo/protobuf v1.3.0/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= @@ -83,6 +85,7 @@ github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= @@ -122,9 +125,12 @@ github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQL github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/pty v1.1.4 h1:5Myjjh3JY/NaAi4IsUbHADytDyl1VE1Y9PXDlL+P/VQ= github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= @@ -143,6 +149,8 @@ github.com/modern-go/reflect2 v0.0.0-20180320133207-05fbef0ca5da/go.mod h1:bx2lN github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= +github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -174,8 +182,6 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= -github.com/spaceuptech/galaxy v0.0.0-20200104102554-37dd7d3161e4 h1:hj+pufYodCpodXwQaSlcNN0R3v3JvozYjJ2F7wGvJVI= -github.com/spaceuptech/galaxy v0.0.0-20200104102554-37dd7d3161e4/go.mod h1:bn9hngEXqg1qMG8UqQdfXewi+G/jyHsCVseCESLdm7s= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= @@ -184,8 +190,6 @@ github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb6 github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= -github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= -github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -194,6 +198,8 @@ github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/txn2/txeh v1.3.0 h1:vnbv63htVMZCaQgLqVBxKvj2+HHHFUzNW7I183zjg3E= +github.com/txn2/txeh v1.3.0/go.mod h1:O7M6gUTPeMF+vsa4c4Ipx3JDkOYrruB1Wry8QRsMcw8= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo= github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= @@ -251,8 +257,6 @@ golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190922100055-0a153f010e69/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe h1:6fAMxZRR6sl1Uq8U61gxU+kPTs2tR8uOySCbBP7BN/M= golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191224085550-c709ea063b76 h1:Dho5nD6R3PcW2SH1or8vS0dszDaXRxIw55lBX7XiE5g= -golang.org/x/sys v0.0.0-20191224085550-c709ea063b76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -273,6 +277,7 @@ google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEt google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM= google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= @@ -285,6 +290,7 @@ google.golang.org/grpc v1.23.1 h1:q4XQuHFC6I28BKZpo6IYyb3mNO+l7lSOxRuYTCiDfXk= google.golang.org/grpc v1.23.1/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/inf.v0 v0.9.0/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= @@ -296,6 +302,8 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo= gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= +gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/main.go b/main.go index b7c2288..e5a8caa 100644 --- a/main.go +++ b/main.go @@ -185,7 +185,7 @@ func main() { cli.StringFlag{ Name: "username", Usage: "Accepts the username for login", - EnvVar: "USERNAME", + EnvVar: "USER_NAME", // don't set environment variable as USERNAME -> defaults to username of host machine in linux Value: "None", }, cli.StringFlag{ @@ -198,7 +198,7 @@ func main() { Name: "url", Usage: "Accepts the URL of server", EnvVar: "URL", - Value: "noorain.bolega.com", + Value: "localhost:4122", }, cli.BoolFlag{ Name: "local", @@ -208,6 +208,12 @@ func main() { }, Action: actionLogin, }, + { + Name: "setup", + Usage: "setup development environment", + Flags: []cli.Flag{}, + Action: actionSetup, + }, } // Start the app diff --git a/model/cli.go b/model/cli.go index c43b235..e8ab4bb 100644 --- a/model/cli.go +++ b/model/cli.go @@ -1,10 +1,9 @@ package model type LoginResponse struct { - AccountID string `json:"id" yaml:"id"` - Token string `json:"token" yaml:"token"` - FileToken string `json:"fileToken" yaml:"fileToken"` - Projects []Projects `json:"projects" yaml:"projects"` + Token string `json:"token" yaml:"token"` + Projects []*Projects `json:"projects" yaml:"projects"` + Error string `json:"error"` } type Credential struct { @@ -22,7 +21,7 @@ type Account struct { type Projects struct { Name string `json:"name" yaml:"name"` ID string `json:"id" yaml:"id"` - Environments []Environment `json:"environment" yaml:"environment"` + Environments []*Environment `json:"environment,omitempty" yaml:"environment"` } type Environment struct { diff --git a/model/service.go b/model/service.go index b0474ba..c0f1ad9 100644 --- a/model/service.go +++ b/model/service.go @@ -128,6 +128,7 @@ type ExposeRuleURI struct { } type ActionCode struct { - Service *Service `json:"service" yaml:"service"` - IsDeploy bool `json:"isdeploy" yaml:"isdeploy"` + Service *Service `json:"service" yaml:"service"` + IsDeploy bool `json:"isdeploy" yaml:"isdeploy"` + Condition string `json:"condition" yaml:"condition"` } diff --git a/runner/config.go b/runner/config.go index 2a42192..a737134 100644 --- a/runner/config.go +++ b/runner/config.go @@ -1,18 +1,2 @@ package runner -import ( - "github.com/spaceuptech/galaxy/runner/driver" - "github.com/spaceuptech/galaxy/utils/auth" -) - -// Config is the object required to configure the runner -type Config struct { - Port string - ProxyPort string - - // Configuration for the driver - Driver *driver.Config - - // Configuration for the auth module - Auth *auth.Config -} diff --git a/runner/driver/driver.go b/runner/driver/driver.go index 6696f49..35d246f 100644 --- a/runner/driver/driver.go +++ b/runner/driver/driver.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/spaceuptech/galaxy/model" + "github.com/spaceuptech/galaxy/runner/driver/istio" "github.com/spaceuptech/galaxy/utils/auth" ) diff --git a/runner/handle.go b/runner/handle.go index 7714d05..79cafc1 100644 --- a/runner/handle.go +++ b/runner/handle.go @@ -3,7 +3,6 @@ package runner import ( "encoding/json" "fmt" - "github.com/sirupsen/logrus" "io" "io/ioutil" "net/http" diff --git a/runner/services/do/digitalocean.go b/runner/services/do/digitalocean.go index 45bf797..d539a38 100644 --- a/runner/services/do/digitalocean.go +++ b/runner/services/do/digitalocean.go @@ -9,7 +9,7 @@ import ( "github.com/digitalocean/godo" "golang.org/x/oauth2" - "github.com/spaceuptech/launchpad/model" + "github.com/spaceuptech/galaxy/model" ) // DigitalOcean is used to manage DO clients diff --git a/runner/services/do/digitalocean_test.go b/runner/services/do/digitalocean_test.go index 85babfe..7acaaf5 100644 --- a/runner/services/do/digitalocean_test.go +++ b/runner/services/do/digitalocean_test.go @@ -11,8 +11,8 @@ import ( "testing" "github.com/digitalocean/godo" - "github.com/spaceuptech/launchpad/model" - "github.com/spaceuptech/launchpad/utils" + "github.com/spaceuptech/galaxy/model" + "github.com/spaceuptech/galaxy/utils" ) type httpMock struct { diff --git a/runner/services/services.go b/runner/services/services.go index 88b5340..a3150f8 100644 --- a/runner/services/services.go +++ b/runner/services/services.go @@ -6,8 +6,8 @@ import ( "strings" "sync" - "github.com/spaceuptech/launchpad/model" - "github.com/spaceuptech/launchpad/runner/services/do" + "github.com/spaceuptech/galaxy/model" + "github.com/spaceuptech/galaxy/runner/services/do" ) // ManagedServices contains the map of Provider interface From 5da9dbf207afeb2dcd71f03f53e6e1a32cc35876 Mon Sep 17 00:00:00 2001 From: sharadregoti Date: Wed, 22 Jan 2020 08:08:47 +0530 Subject: [PATCH 2/2] test space-api login --- actions.go | 5 +- cmd/code.go | 170 -------------------------------------------------- cmd/creds.go | 76 ---------------------- cmd/helper.go | 170 -------------------------------------------------- cmd/login.go | 77 ----------------------- cmd/setup.go | 125 ------------------------------------- 6 files changed, 1 insertion(+), 622 deletions(-) delete mode 100644 cmd/code.go delete mode 100644 cmd/creds.go delete mode 100644 cmd/helper.go delete mode 100644 cmd/login.go delete mode 100644 cmd/setup.go diff --git a/actions.go b/actions.go index 2eff572..22b14b6 100644 --- a/actions.go +++ b/actions.go @@ -160,8 +160,5 @@ func actionLogin(c *cli.Context) error { } func actionSetup(c *cli.Context) error { - if err := cmd.CodeSetup(); err != nil { - return err - } - return nil + return cmd.CodeSetup() } diff --git a/cmd/code.go b/cmd/code.go deleted file mode 100644 index cd79517..0000000 --- a/cmd/code.go +++ /dev/null @@ -1,170 +0,0 @@ -package cmd - -import ( - "context" - "encoding/json" - "fmt" - "os" - "strings" - - "github.com/AlecAivazis/survey/v2" - "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/container" - "github.com/docker/docker/api/types/mount" - "github.com/docker/docker/client" - "github.com/sirupsen/logrus" - - "github.com/spaceuptech/galaxy/model" -) - -// CodeStart starts the code commands -func CodeStart(envID string) (*model.Service, *model.LoginResponse, error) { - credential, err := getCreds() - if err != nil { - logrus.Errorf("error code start unable to get credentials got error message - %v", err) - return nil, nil, err - } - - selectedAccount := getSelectedAccount(credential) - - loginRes, err := login(selectedAccount) - if err != nil { - logrus.Errorf("error code start unable to login got error message - %v", err) - return nil, nil, err - } - - c, err := getServiceConfig(".galaxy.yaml") - if err != nil { - c, err = generateServiceConfig(loginRes.Projects, selectedAccount, envID) - if err != nil { - logrus.Errorf("error code start unable to generate service config got error message - %v", err) - return nil, nil, err - } - } - return c, loginRes, nil -} - -func generateServiceConfig(projects []*model.Projects, selectedaccount *model.Account, envID string) (*model.Service, error) { - progLang, err := getProgLang() - if err != nil { - return nil, err - } - var envNameID string - var clusters []string - serviceName := "" - if err := survey.AskOne(&survey.Input{Message: "Enter Service Name"}, &serviceName); err != nil { - return nil, err - } - defaultServiceID := strings.ReplaceAll(serviceName, " ", "-") - serviceID := "" - if err := survey.AskOne(&survey.Input{Message: "Enter Service ID", Default: strings.ToLower(defaultServiceID)}, &serviceID); err != nil { - return nil, err - } - var port int32 - if err := survey.AskOne(&survey.Input{Message: "Enter Service Port"}, &port); err != nil { - return nil, err - } - projectNameID := "" - if err := survey.AskOne(&survey.Select{Message: "Select Project Name", Options: getProjects(projects)}, &projectNameID); err != nil { - return nil, err - } - - temp := strings.Split(projectNameID, " ") - projectID := temp[0] - - var project model.Projects - if envID == "none" { - project, err := getProject(projectID, projects) - if err != nil { - return nil, err - } - if err := survey.AskOne(&survey.Select{Message: "Select Environment", Options: getEnvironments(project)}, &envNameID); err != nil { - return nil, err - } - temp := strings.Split(envNameID, " ") - envID = temp[0] - } - - selectedEnv, err := getEnvironment(envID, project.Environments) - if err != nil { - return nil, err - } - if err := survey.AskOne(&survey.MultiSelect{Message: "Select Clusters", Options: getClusters(selectedEnv)}, &clusters); err != nil { - return nil, err - } - var progCmd string - if err := survey.AskOne(&survey.Input{Message: "Enter Run Cmd", - Default: strings.Join(getCmd(progLang), " ")}, &progCmd); err != nil { - return nil, err - } - img, err := getImage(progLang) - if err != nil { - return nil, err - } - - c := &model.Service{ - ID: serviceID, - Name: serviceName, - ProjectID: projectID, - Environment: envID, - Version: "v1", - Scale: model.ScaleConfig{Replicas: 0, MinReplicas: 0, MaxReplicas: 100, Concurrency: 50}, - Tasks: []model.Task{ - { - ID: serviceID, - Name: serviceName, - Ports: []model.Port{model.Port{Protocol: "http", Port: port}}, - Resources: model.Resources{CPU: 250, Memory: 512}, - Docker: model.Docker{Image: img}, - Env: map[string]string{"URL": selectedaccount.ServerUrl, "CMD": progCmd}, - }, - }, - Whitelist: []string{"project:*"}, - Upstreams: []model.Upstream{model.Upstream{ProjectID: projectID, Service: "*"}}, - Runtime: "code", - } - return c, nil -} - -// RunDockerFile starts a container using go docker client -func RunDockerFile(s *model.ActionCode, loginResp *model.LoginResponse) error { - s.Condition = "create" - dir, err := os.Getwd() - if err != nil { - return err - } - sa, err := json.Marshal(s) - if err != nil { - return err - } - ctx := context.Background() - cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) - if err != nil { - return err - } - // todo potential bug we are running runtime image here - // todo potential bug in builder image worddir is /builder but in sh script we use cd /build - resp, err := cli.ContainerCreate(ctx, - &container.Config{ - Image: s.Service.Tasks[0].Docker.Image, - Env: []string{ - "FILE_PATH=/", - fmt.Sprintf("URL=%s", s.Service.Tasks[0].Env["URL"]), - fmt.Sprintf("TOKEN=%s", loginResp.Token), - fmt.Sprintf("meta=%s", string(sa))}, - }, - &container.HostConfig{Mounts: []mount.Mount{ - { - Type: mount.TypeBind, - Source: dir, - Target: "/build", - }, - }}, nil, "") - if err != nil { - return err - } - if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { - return err - } - return nil -} diff --git a/cmd/creds.go b/cmd/creds.go deleted file mode 100644 index 55761f1..0000000 --- a/cmd/creds.go +++ /dev/null @@ -1,76 +0,0 @@ -package cmd - -import ( - "fmt" - "io/ioutil" - - "github.com/sirupsen/logrus" - "gopkg.in/yaml.v2" - - "github.com/spaceuptech/galaxy/model" -) - -func getSelectedAccount(credential *model.Credential) *model.Account { - var selectedaccount model.Account - for _, v := range credential.Accounts { - if credential.SelectedAccount == v.ID { - selectedaccount = v - } - } - return &selectedaccount -} - -func getCreds() (*model.Credential, error) { - fileName := fmt.Sprintf("/%s/galaxy/config.yaml", getHomeDirectory()) - yamlFile, err := ioutil.ReadFile(fileName) - if err != nil { - return nil, fmt.Errorf("error reading yaml file: %s", err) - } - - credential := new(model.Credential) - if err := yaml.Unmarshal(yamlFile, credential); err != nil { - return nil, err - } - return credential, nil -} - -func checkCred(selectedAccount *model.Account) error { - fileName := fmt.Sprintf("/%s/galaxy/config.yaml", getHomeDirectory()) - yamlFile, err := ioutil.ReadFile(fileName) - if err != nil { - // create config.yaml file - credential := model.Credential{ - Accounts: []model.Account{*selectedAccount}, - SelectedAccount: selectedAccount.ID, - } - if err := generateYamlFile(&credential); err != nil { - logrus.Errorf("error checking credentials unable to generate login yaml file got error message - %v", err) - return err - } - return nil - } - - credential := new(model.Credential) - if err := yaml.Unmarshal(yamlFile, credential); err != nil { - logrus.Errorf("error checking credentials unable to unmarshal login yaml file got error message - %v", err) - return err - } - for _, val := range credential.Accounts { - // update existing login details - if val.ID == selectedAccount.ID { - val.ID, val.UserName, val.Key, val.ServerUrl = selectedAccount.ID, selectedAccount.UserName, selectedAccount.Key, selectedAccount.ServerUrl - if err := generateYamlFile(credential); err != nil { - logrus.Errorf("error checking credentials unable to update config yaml file got error message - %v", err) - return err - } - return nil - } - } - credential.Accounts = append(credential.Accounts, *selectedAccount) - credential.SelectedAccount = selectedAccount.ID - if err := generateYamlFile(credential); err != nil { - logrus.Errorf("error checking credentials unable to update login yaml file got error message - %v", err) - return err - } - return nil -} diff --git a/cmd/helper.go b/cmd/helper.go deleted file mode 100644 index 65a3acb..0000000 --- a/cmd/helper.go +++ /dev/null @@ -1,170 +0,0 @@ -package cmd - -import ( - "errors" - "fmt" - "io/ioutil" - "os" - "path/filepath" - "regexp" - "runtime" - - "github.com/spaceuptech/galaxy/model" - "gopkg.in/yaml.v2" -) - -func getHomeDirectory() string { - if runtime.GOOS == "windows" { - home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") - if home == "" { - home = os.Getenv("USERPROFILE") - } - return home - } - return os.Getenv("HOME") -} - -func getServiceConfig(filename string) (*model.Service, error) { - dir, err := os.Getwd() - if err != nil { - return nil, err - } - fileName := fmt.Sprintf("/%s/%s", dir, filename) - file, err := ioutil.ReadFile(fileName) - if err != nil { - return nil, err - } - c := new(model.Service) - err = yaml.Unmarshal(file, c) - if err != nil { - return nil, err - } - return c, nil -} - -func checkFile(filename string) bool { - dir, err := os.Getwd() - if err != nil { - return false - } - fileName := fmt.Sprintf("/%s/%s", dir, filename) - _, err = ioutil.ReadFile(fileName) - if err != nil { - return false - } - return true -} - -func getProgLang() (string, error) { - if checkFile("requirement.txt") { - return "python", nil - } - if checkFile("package.json") { - return "node", nil - } - if checkExt(".go") { - return "go", nil - } - return "", fmt.Errorf("No Programming language detected") -} - -func getImage(progLang string) (string, error) { - switch progLang { - case "python": - return "spaceuptech/runtime-python", nil - case "nodejs": - return "spaceuptech/runtime-node", nil - case "go": - return "spaceuptech/runtime-golang", nil - default: - return "", fmt.Errorf("%s is not supported", progLang) - } -} - -func getCmd(progLang string) []string { - switch progLang { - case "python": - return []string{"python3", "app.py"} - case "nodejs": - return []string{"npm", "start"} - case "go": - return []string{"./app"} - default: - return []string{} - } -} - -func getProject(projectID string, projects []*model.Projects) (*model.Projects, error) { - for _, project := range projects { - if projectID == project.ID { - return project, nil - } - } - return nil, fmt.Errorf("Invalid Project Name") -} - -func getEnvironment(envID string, environments []*model.Environment) (*model.Environment, error) { - for _, env := range environments { - if envID == env.ID { - return env, nil - } - } - return nil, errors.New("Invalid Project Name") -} - -func getProjects(projects []*model.Projects) []string { - var projnames []string - for _, val := range projects { - projnames = append(projnames, fmt.Sprintf("%s (%s)", val.ID, val.Name)) - } - return projnames -} - -func getEnvironments(project *model.Projects) []string { - var envs []string - for _, val := range project.Environments { - envs = append(envs, fmt.Sprintf("%s %s", val.ID, val.Name)) - } - return envs -} - -func getClusters(environment *model.Environment) []string { - var clusternames []string - for _, val := range environment.Clusters { - clusternames = append(clusternames, val.ID) - } - return clusternames -} - -func checkExt(ext string) bool { - pathS, err := os.Getwd() - if err != nil { - panic(err) - } - present := false - filepath.Walk(pathS, func(path string, f os.FileInfo, _ error) error { - if !f.IsDir() { - r, err := regexp.MatchString(ext, f.Name()) - if err == nil && r { - present = true - } - } - return nil - }) - return present -} - -func generateYamlFile(credential *model.Credential) error { - d, err := yaml.Marshal(&credential) - if err != nil { - return err - } - - fileName := fmt.Sprintf("/%s/galaxy/config.yaml", getHomeDirectory()) - err = ioutil.WriteFile(fileName, d, 0644) - if err != nil { - return err - } - - return nil -} diff --git a/cmd/login.go b/cmd/login.go deleted file mode 100644 index a77a5a5..0000000 --- a/cmd/login.go +++ /dev/null @@ -1,77 +0,0 @@ -package cmd - -import ( - "bytes" - "encoding/json" - "fmt" - "net/http" - - "github.com/AlecAivazis/survey/v2" - "github.com/sirupsen/logrus" - - "github.com/spaceuptech/galaxy/model" - "github.com/spaceuptech/galaxy/utils" -) - -func login(selectedAccount *model.Account) (*model.LoginResponse, error) { - requestBody, err := json.Marshal(map[string]string{ - "user": selectedAccount.UserName, - "key": selectedAccount.Key, - }) - if err != nil { - logrus.Errorf("error in login unable to marshal data for login got error message - %v", err) - return nil, err - } - - resp, err := http.Post(fmt.Sprintf("http://%s/v1/config/login", selectedAccount.ServerUrl), "application/json", bytes.NewBuffer(requestBody)) - if err != nil { - logrus.Errorf("error in login unable to send http request for login got error message - %v", err) - return nil, err - } - defer utils.CloseReaderCloser(resp.Body) - - loginResp := new(model.LoginResponse) - _ = json.NewDecoder(resp.Body).Decode(loginResp) - - if resp.StatusCode != 200 { - logrus.Errorf("error in login unable to login got status code %v with error message - %v", resp.StatusCode, loginResp.Error) - return nil, fmt.Errorf("%v", loginResp.Error) - } - return loginResp, nil -} - -// LoginStart logs the user in galaxy -func LoginStart(userName, key, url string, local bool) error { - if userName == "None" { - if err := survey.AskOne(&survey.Input{Message: "Enter username:"}, &userName); err != nil { - logrus.Error("error starting login unable to get username from user got error message -%v", err) - return err - } - } - if key == "None" { - if err := survey.AskOne(&survey.Password{Message: "Enter key:"}, &key); err != nil { - logrus.Error("error starting login unable to get key from user got error message -%v", err) - return err - } - } - selectedAccount := model.Account{ - UserName: userName, - Key: key, - ServerUrl: url, - } - loginRes, err := login(&selectedAccount) - if err != nil { - return err - } - fmt.Printf("Login Successful\n") - selectedAccount = model.Account{ - ID: loginRes.Token, - UserName: userName, - Key: key, - ServerUrl: url, - } - if err := checkCred(&selectedAccount); err != nil { - return err - } - return nil -} diff --git a/cmd/setup.go b/cmd/setup.go deleted file mode 100644 index 1381698..0000000 --- a/cmd/setup.go +++ /dev/null @@ -1,125 +0,0 @@ -package cmd - -import ( - "context" - "fmt" - "io" - "log" - "os" - - "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/container" - "github.com/docker/docker/api/types/mount" - "github.com/docker/docker/client" - "github.com/docker/go-connections/nat" - "github.com/sirupsen/logrus" - "github.com/txn2/txeh" -) - -// CodeSetup initializes development environment -func CodeSetup() error { - const dockerImageSpaceCloud string = "spaceuptech/space-cloud" - containersToCreate := []struct { - containerName string - commands []string - exposedPorts nat.PortSet - portMapping nat.PortMap - }{ - { - containerName: "space-cloud-gateway", - commands: []string{"./space-cloud", "run", "-dev"}, - exposedPorts: nat.PortSet{ - "4122": struct{}{}, - }, - portMapping: nat.PortMap{ - "4122": []nat.PortBinding{{HostIP: "localhost", HostPort: "4122"}}, - }, - }, - { - containerName: "space-cloud-runner", - commands: []string{"./space-cloud", "run", "-dev"}, - }, - - { - containerName: "space-cloud-artifact-store", - commands: []string{"./space-cloud", "run", "-dev"}, - }, - } - - ctx := context.Background() - cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) - if err != nil { - logrus.Errorf("error cli setup unable to initialize docker client got error message - %v", err) - return err - } - - hosts, err := txeh.NewHostsDefault() - if err != nil { - logrus.Errorf("error cli setup unable to load host file with suitable default got error message - %v", err) - return err - } - - // pull image from docker hub - out, err := cli.ImagePull(ctx, dockerImageSpaceCloud, types.ImagePullOptions{}) - if err != nil { - logrus.Errorf("error cli setup unable to pull image from docker hub got error message - %v", err) - return err - } - io.Copy(os.Stdout, out) - - if err := os.MkdirAll(fmt.Sprintf("%s/space-cloud-ip-table", getHomeDirectory()), 0755); err != nil { - logrus.Errorf("error cli setup unable to create directory for storing host file got error message - %v", err) - return err - } - - hostFilePath := fmt.Sprintf("%s/space-cloud-ip-table/.space-cloud-ip_table.hosts", getHomeDirectory()) - hostFileDir := fmt.Sprintf("%s/space-cloud-ip-table", getHomeDirectory()) - - if err := hosts.SaveAs(hostFilePath); err != nil { - logrus.Errorf("error cli setup unable to save host file to specified path (%s) got error message - %v", hostFilePath, err) - return err - } - - // change the default host file location for crud operation to our specified path - // default value /etc/hosts - hosts.WriteFilePath = hostFilePath - - for _, c := range containersToCreate { - resp, err := cli.ContainerCreate(ctx, &container.Config{ - Image: dockerImageSpaceCloud, - Cmd: c.commands, - ExposedPorts: c.exposedPorts, - }, &container.HostConfig{ - Mounts: []mount.Mount{ - { - Type: mount.TypeBind, - Source: hostFileDir, - Target: "/space-cloud-ip-table", - }, - }, - PortBindings: c.portMapping, - }, nil, c.containerName) - if err != nil { - logrus.Errorf("error cli setup unable to create container %s got error message - %v", c.containerName, err) - return err - } - - if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { - logrus.Errorf("error cli setup unable to start container %s got error message - %v", c.containerName, err) - return err - } - - data, err := cli.ContainerInspect(ctx, c.containerName) - if err != nil { - logrus.Errorf("error cli setup unable to inspect container %s got error message - %v", c.containerName, err) - } - log.Println("container name:", data.Name) - hosts.AddHost(data.NetworkSettings.IPAddress, c.containerName) - } - - if err := hosts.Save(); err != nil { - logrus.Errorf("error cli setup unable to save host file got error message - %v", err) - return err - } - return nil -}