Skip to content

Commit

Permalink
add appserve cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
robertchoi80 committed Nov 30, 2022
1 parent d037c71 commit cd11bac
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 2 deletions.
46 changes: 46 additions & 0 deletions cmd/appserve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright © 2021 SK Telecom <https://github.com/openinfradev>
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")
}
121 changes: 121 additions & 0 deletions cmd/appserve_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright © 2021 SK Telecom <https://github.com/openinfradev>
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 <APPNAME> [--config CONFIGFILE]`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("Usage: tks appserve create <APPNAME>")
}

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")
}
4 changes: 2 additions & 2 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ limitations under the License.
package cmd

var (
tksInfoUrl string
tksClusterLcmUrl string
tksInfoUrl string
tksClusterLcmUrl string
)
17 changes: 17 additions & 0 deletions files/appserve-config.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cd11bac

Please sign in to comment.