From cd11bac4148fc864d549dd8c1d7eb2ab1a8be9b7 Mon Sep 17 00:00:00 2001 From: Robert Choi Date: Wed, 9 Nov 2022 00:24:10 +0900 Subject: [PATCH] add appserve cmd --- cmd/appserve.go | 46 ++++++++++++++ cmd/appserve_create.go | 121 +++++++++++++++++++++++++++++++++++++ cmd/common.go | 4 +- files/appserve-config.yaml | 17 ++++++ 4 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 cmd/appserve.go create mode 100644 cmd/appserve_create.go create mode 100644 files/appserve-config.yaml diff --git a/cmd/appserve.go b/cmd/appserve.go new file mode 100644 index 0000000..f93b4c7 --- /dev/null +++ b/cmd/appserve.go @@ -0,0 +1,46 @@ +/* +Copyright © 2021 SK Telecom + +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. +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// appserveCmd represents the appserve command +var appserveCmd = &cobra.Command{ + Use: "appserve", + Short: "Operation for TKS Appserve", + Long: `Operation for TKS Appserve`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("appserve called") + }, +} + +func init() { + rootCmd.AddCommand(appserveCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // appserveCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // appserveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/appserve_create.go b/cmd/appserve_create.go new file mode 100644 index 0000000..6090684 --- /dev/null +++ b/cmd/appserve_create.go @@ -0,0 +1,121 @@ +/* +Copyright © 2021 SK Telecom + +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. +*/ +package cmd + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "io/ioutil" + "net/http" +) + +var appserveCreateCmd = &cobra.Command{ + Use: "create", + Short: "Create an app by AppServing service", + Long: `Create an app by AppServing service. + +Example: +tks appserve create [--config CONFIGFILE]`, + SilenceUsage: true, + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return errors.New("Usage: tks appserve create ") + } + + if cfgFile != "" { + // Use config file from the flag. + viper.SetConfigFile(cfgFile) + } else { + viper.SetConfigName("appserve-config") // name of config file (without extension) + viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name + viper.AddConfigPath(".") // optionally look for config in the working directory + } + + viper.AutomaticEnv() // read in environment variables that match + + // If a config file is found, read it in. + if err := viper.ReadInConfig(); err != nil { + if _, ok := err.(viper.ConfigFileNotFoundError); ok { + // Config file not found; ignore error if desired + + } else { + // Config file was found but another error was produced + + } + } else { + fmt.Printf("Using config file: %s\n", viper.ConfigFileUsed()) + } + + appserveApiUrl := viper.GetString("tks_appserve_api_url") + if appserveApiUrl == "" { + return errors.New("tks_appserve_api_url is mandatory in config file") + } + + c := viper.AllSettings() + //fmt.Printf("viper map: %v\n\n", c) + delete(c, "tks_appserve_api_url") + cBytes, err := json.Marshal(c) // 맵을 JSON 문서로 변환 + if err != nil { + // TODO: %s or %v?? + return fmt.Errorf("Unable to marshal config to JSON: %v", err) + } + + fmt.Println("========== ") + fmt.Println("Json data: ") + fmt.Println(string(cBytes)) + fmt.Println("========== ") + + buff := bytes.NewBuffer(cBytes) + resp, err := http.Post(appserveApiUrl+"/apps", "application/json", buff) + if err != nil { + //panic(err) + return fmt.Errorf("Error from POST req: %s", err) + } + + defer resp.Body.Close() + + // Check response + respBody, err := ioutil.ReadAll(resp.Body) + if err == nil { + str := string(respBody) + // TODO: after test run, fix this output msg. + fmt.Println("Response:\n", str) + fmt.Println("Success: The request to create appserve ", args[0], " was accepted.") + } + + return nil + }, +} + +func init() { + appserveCmd.AddCommand(appserveCreateCmd) + + appserveCreateCmd.Flags().StringVar(&cfgFile, "config", "", "config file (default is ./appserve-config.yaml)") + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // appserveCreateCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // appserveCreateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/common.go b/cmd/common.go index 38bf6d5..e95260e 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -16,6 +16,6 @@ limitations under the License. package cmd var ( - tksInfoUrl string - tksClusterLcmUrl string + tksInfoUrl string + tksClusterLcmUrl string ) diff --git a/files/appserve-config.yaml b/files/appserve-config.yaml new file mode 100644 index 0000000..bb521b9 --- /dev/null +++ b/files/appserve-config.yaml @@ -0,0 +1,17 @@ +tks_appserve_api_url: http://localhost:9112 +contract_id: psh8wxoj7 +name: my-petclinic +version: v1 +target_cluster_id: cdozqn511 +task_type: all +artifact_url: "http://ab846aadb5b974536a0463a29ed866f5-200924269.ap-northeast-2.elb.amazonaws.com:8081/repository/my-release-repo/default/petclinic/1.0/petclinic-1.0.jar" +port: "8080" +resource_spec: medium +profile: default +persistent_volume: + enabled: false + storageclass: taco-storage + accessMode: ReadWriteOnce + size: 5Gi + mountPath: /usr/share/temp11 +