--- /dev/null
+/*
+Copyright 2020 Huawei Technologies Co., Ltd.
+
+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 adapter
+
+import (
+ "ealt/cmd/common"
+ "encoding/json"
+ "fmt"
+ "log"
+ "strings"
+)
+
+func BuilderRequest(valueArgs []string, command string) error {
+
+ var URIString string
+
+ switch command {
+ case "NewAppCreateCommand":
+ //Onboard Command
+ //ealtedge/mepm/app_pkgm/v1/app_packages/
+ //read the file from the system.
+ URIString = common.AppmUri
+ var packageName string
+ var body []byte
+ body = jsonEmptyBodyFormat()
+ packageName = strings.TrimSpace(valueArgs[0])
+ HttpMultiPartPostRequestBuilder(URIString, body, packageName)
+ fmt.Println(packageName)
+
+ case "NewAppDeleteCommand":
+ //The Delete Application Package URI
+ //ealtedge/mepm/app_pkgm/v1/app_packages/{{ID}}
+ var body []byte
+ URIString = common.AppmUri + strings.TrimSpace(valueArgs[0])
+ body = jsonEmptyBodyFormat()
+ fmt.Println(URIString)
+ HttpDeleteRequestBuilder(URIString, body)
+
+ fmt.Println(URIString)
+
+ case "NewApplcmCreateCommand":
+ //appLCM application Creation URI
+ //ealtedge/mepm/app_lcm/v1/app_instances
+ var body []byte
+
+ URIString = common.ApplcmUri
+ body, err := json.Marshal(map[string]string{
+ "appDId": strings.TrimSpace(valueArgs[0]),
+ "appInstancename": strings.TrimSpace(valueArgs[1]),
+ "appInstanceDescriptor": strings.TrimSpace(valueArgs[2]),
+ })
+
+ if err != nil {
+ log.Fatalln(err)
+ }
+ fmt.Println(URIString)
+ HttpPostRequestBuilder(URIString, body)
+
+ case "NewApplcmDeleteCommand":
+ //appLCM Delete Application URI
+ ///ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}
+ var body []byte
+ URIString = common.ApplcmUri + strings.TrimSpace(valueArgs[0])
+ body = jsonEmptyBodyFormat()
+ fmt.Println(URIString)
+ HttpDeleteRequestBuilder(URIString, body)
+
+ case "NewApplcmStartCommand":
+ //applcm application instantiate uri
+ //ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}/instantiate
+ var body []byte
+
+ URIString = common.ApplcmUri + strings.TrimSpace(valueArgs[0]) + common.InstantiateUri
+ body = jsonEmptyBodyFormat()
+ fmt.Println(URIString)
+ HttpPostRequestBuilder(URIString, body)
+
+ case "NewApplcmTerminateCommand":
+ //applcm application terminate uri
+ //ealtedge/mepm/app_lcm/v1/app_instances/{appInstanceId}/terminate
+ var body []byte
+ URIString = common.ApplcmUri + strings.TrimSpace(valueArgs[0]) + common.TerminateUri
+ body = jsonEmptyBodyFormat()
+ fmt.Println(URIString)
+ HttpPostRequestBuilder(URIString, body)
+
+ }
+
+ return nil
+}
+
+func jsonEmptyBodyFormat() []byte {
+ var jsonstr []byte
+ jsonstr = []byte(`{"":""}`)
+ return jsonstr
+}
--- /dev/null
+/*
+Copyright 2020 Huawei Technologies Co., Ltd.
+
+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 adapter
+
+import (
+ "bytes"
+ "ealt/cmd/common"
+ "encoding/json"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "log"
+ "mime/multipart"
+ "net/http"
+ "os"
+ "strings"
+)
+
+var MECMClusterIP = os.Getenv("MECMClusterIP")
+var APPLCMPort = os.Getenv("MECMClusterPort")
+var ONBOARDPACKAGEPATH = os.Getenv("ONBOARDPACKAGEPATH")
+var client = http.Client{}
+
+func httpEndPointBuider(uri string) string {
+
+ return "http://" + strings.TrimSpace(MECMClusterIP) + strings.TrimSpace(APPLCMPort) + uri
+
+}
+
+func HttpDeleteRequestBuilder(uri string, body []byte) {
+
+ uri = httpEndPointBuider(uri)
+ request, err := http.NewRequest(http.MethodDelete, uri, bytes.NewBuffer(body))
+ request.Header.Set("Content-Type", "application/json")
+
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ response, err := client.Do(request)
+ if err != nil {
+ log.Fatalln(err)
+ }
+ defer response.Body.Close()
+
+ output, err := ioutil.ReadAll(response.Body)
+
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ fmt.Println(string(output))
+
+}
+
+func HttpPostRequestBuilder(uri string, body []byte) error {
+
+ uri = httpEndPointBuider(uri)
+ request, err := http.NewRequest(http.MethodPost, uri, bytes.NewBuffer(body))
+ request.Header.Set("Content-Type", "application/json")
+
+ fmt.Println(request)
+
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ response, err := client.Do(request)
+ if err != nil {
+ log.Fatalln(err)
+ }
+ defer response.Body.Close()
+
+ output, err := ioutil.ReadAll(response.Body)
+
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ fmt.Println(string(output))
+ return nil
+}
+
+func HttpMultiPartPostRequestBuilder(uri string, body []byte, file string) error {
+
+ filepath := getFilePathWithName(file)
+ uri = httpEndPointBuider(uri)
+
+ request, err := fileUploadRequest(uri, "file", filepath)
+
+ response, err := client.Do(request)
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ var result map[string]interface{}
+ json.NewDecoder(response.Body).Decode(&result)
+ fmt.Println(result)
+
+ return nil
+}
+
+func getFilePathWithName(file string) string {
+
+ return ONBOARDPACKAGEPATH + common.PATHSLASH + file
+}
+
+func fileUploadRequest(uri string, paramName, filepath string) (*http.Request, error) {
+
+ file, err := os.Open(filepath)
+ if err != nil {
+ return nil, err
+ }
+
+ //close the file later
+ defer file.Close()
+
+ //Buffer to store the request body as bytes
+ var requestBody bytes.Buffer
+ multiPartWriter := multipart.NewWriter(&requestBody)
+
+ fileWriter, err := multiPartWriter.CreateFormFile("file_field", filepath)
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ //Copy the actual file contents
+ _, err = io.Copy(fileWriter, file)
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ //Close multiwriter
+ multiPartWriter.Close()
+
+ request, err := http.NewRequest(http.MethodPost, uri, &requestBody)
+ if err != nil {
+ log.Fatalln(err)
+ }
+ request.Header.Set("Content-Type", "multipart/form-data")
+ return request, err
+}
// applcmCmd represents the applcm command
var applcmCmd = &cobra.Command{
Use: "applcm",
- Short: "A brief description of your command",
- Long: `A longer description that spans multiple lines and likely contains examples
-and usage of using your command. For example:
-
-Cobra is a CLI library for Go that empowers applications.
-This application is a tool to generate the needed files
-to quickly create a Cobra application.`,
+ Short: "Commands to send request to the APPLCM Application",
+ Long: `To manage the application running on the MEP Node, APPLCM exposes
+ some API which can be used to manage the Applicaton running on the MEP Node
+ The command have following options :
+ 1. Create
+ 2. Start
+ 3. Delete
+ 4. Stop.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("applcm called")
},
func init() {
applcmCmd.AddCommand(applcmCmds.NewApplcmCreateCommand())
+ applcmCmd.AddCommand(applcmCmds.NewApplcmStartCommand())
+ applcmCmd.AddCommand(applcmCmds.NewApplcmDeleteCommand())
+ applcmCmd.AddCommand(applcmCmds.NewApplcmTerminateCommand())
+
rootCmd.AddCommand(applcmCmd)
}
--- /dev/null
+/*
+Copyright 2020 Huawei Technologies Co., Ltd.
+
+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 applcmpkg
+
+import (
+ "ealt/cmd/adapter"
+
+ "github.com/spf13/cobra"
+)
+
+// deployCmd represents the deploy command
+func NewApplcmCreateCommand() *cobra.Command {
+ var cmd = &cobra.Command{
+ Use: "create",
+ Short: "To create the application instance on MEP Node",
+ Long: `To create the application instance on MEP Node`,
+ RunE: func(cmd *cobra.Command, args []string) error {
+ theFlags := []string{cmd.Flag("appdid").Value.String(),
+ cmd.Flag("name").Value.String(),
+ cmd.Flag("desc").Value.String()}
+ err := adapter.BuilderRequest(theFlags, "NewApplcmCreateCommand")
+ if err != nil {
+ return err
+ }
+ return nil
+ },
+ }
+ cmd.Flags().StringP("appdid", "i", "", "Application ID to be created MEP")
+ cmd.Flags().StringP("name", "n", "", "Application Instance Name")
+ cmd.Flags().StringP("desc", "d", "", "Application Instance Descriptor")
+ cmd.MarkFlagRequired("appdid")
+ cmd.MarkFlagRequired("name")
+ cmd.MarkFlagRequired("desc")
+ return cmd
+}
Short: "Install Complete EALT Deployment Environment",
Long: `Install Complete EALT Deployment Environment`,
RunE: func(cmd *cobra.Command, args []string) error {
- applicationIdArg := cmd.Flag("appid")
- applicationId := applicationIdArg.Value.String()
- err := adapter.BuilderRequest(applicationId, "NewApplcmDeleteCommand")
+ var theFlags []string
+ theFlags[0] = cmd.Flag("appid").Value.String()
+
+ err := adapter.BuilderRequest(theFlags, "NewApplcmDeleteCommand")
if err != nil {
return err
}
},
}
- cmd.Flags().StringP("appid", "i", "", "Application Package ID to be deleted from MEP")
-
+ cmd.Flags().StringP("appid", "i", "", "Application Instance ID to be started")
+ cmd.MarkFlagRequired("appid")
return cmd
}
import (
"ealt/cmd/adapter"
+ "fmt"
"github.com/spf13/cobra"
)
Short: "To start the application instance on MEP Node",
Long: `To start the application instance on MEP Node`,
RunE: func(cmd *cobra.Command, args []string) error {
- appIdArg := cmd.Flag("appid")
- applicationId := appIdArg.Value.String()
- err := adapter.BuilderRequest(applicationId, "NewApplcmStartCommand")
+ theFlags := []string{cmd.Flag("appid").Value.String()}
+ //theFlags[0] = cmd.Flag("appid").Value.String()
+ fmt.Println(theFlags[0])
+ err := adapter.BuilderRequest(theFlags, "NewApplcmStartCommand")
if err != nil {
return err
}
},
}
cmd.Flags().StringP("appid", "i", "", "Application Instance ID to be started")
+ cmd.MarkFlagRequired("appid")
return cmd
}
Short: "To kill the application ",
Long: `Install Complete EALT Deployment Environment`,
RunE: func(cmd *cobra.Command, args []string) error {
- appIdArg := cmd.Flag("appid")
- applicationId := appIdArg.Value.String()
- err := adapter.BuilderRequest(applicationId, "NewApplcmTerminateCommand")
+ var theFlags []string
+ theFlags[0] = cmd.Flag("appid").Value.String()
+ err := adapter.BuilderRequest(theFlags, "NewApplcmTerminateCommand")
if err != nil {
return err
}
}
cmd.Flags().StringP("appid", "i", "", "Application Instance ID to be terminated")
+ cmd.MarkFlagRequired("appid")
return cmd
}
--- /dev/null
+/*
+Copyright 2020 Huawei Technologies Co., Ltd.
+
+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 appm
+
+import (
+ "ealt/cmd/adapter"
+
+ "github.com/spf13/cobra"
+)
+
+// deployCmd represents the deploy command
+func NewAppCreateCommand() *cobra.Command {
+ var cmd = &cobra.Command{
+ Use: "create",
+ Short: "To create the application on MEP Node",
+ Long: `To create the application on MEP Node`,
+ RunE: func(cmd *cobra.Command, args []string) error {
+ var theFlags []string
+ theFlags[0] = cmd.Flag("packagefile").Value.String()
+ err := adapter.BuilderRequest(theFlags, "NewAppCreateCommand")
+ if err != nil {
+ return err
+ }
+ return nil
+ },
+ }
+ cmd.Flags().StringP("packagefile", "f", "", "Application Package File to be onboarded to MEP")
+ cmd.MarkFlagRequired("packagefile")
+ return cmd
+}
Short: "Install Complete EALT Deployment Environment",
Long: `Install Complete EALT Deployment Environment`,
RunE: func(cmd *cobra.Command, args []string) error {
- pkgidArg := cmd.Flag("pkgid")
- packageId := pkgidArg.Value.String()
- err := adapter.BuilderRequest(packageId, "NewAppDeleteCommand")
+ var theFlags []string
+ theFlags[0] = cmd.Flag("pkgid").Value.String()
+ err := adapter.BuilderRequest(theFlags, "NewAppDeleteCommand")
if err != nil {
return err
}
},
}
- cmd.Flags().StringP("pkgid", "i", "", "Application Package ID to be deleted from MEP")
+ cmd.Flags().StringP("packageid", "i", "", "Application Package ID to be deleted from MEP")
+ cmd.MarkFlagRequired("packageid")
return cmd
}
package cmd
import (
-
cleancmds "ealt/cmd/clean"
"github.com/spf13/cobra"
)
var cleanCmd = &cobra.Command{
Use: "clean",
Short: "To uninstall ealt environment or specific component or node.",
- Long: `To uninstall ealt environment or specific component or node.`,
+ Long: `To uninstall ealt environment or specific component or node.`,
}
func init() {
//Add init subcommand to root command.
rootCmd.AddCommand(cleanCmd)
-}
\ No newline at end of file
+}
package init
import (
-
setup "ealt/cmd/setup"
"github.com/spf13/cobra"
package init
import (
-
setup "ealt/cmd/setup"
"github.com/spf13/cobra"
var cmd = &cobra.Command{
Use: "infra",
Short: "Uninstall only infrastructure components on MECM and Edge Node",
- Long: `Command to Uninstall only infrastructure components on MECM and Edge Node For Example : ealt clean infra`,
+ Long: `Command to Uninstall only infrastructure components on MECM and Edge Node For Example : ealt clean infra`,
RunE: func(cmd *cobra.Command, args []string) error {
err := setup.EaltReset("infra")
if err != nil {
--- /dev/null
+/*
+Copyright 2020 Huawei Technologies Co., Ltd.
+
+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 common
+
+const (
+ AppmUri = "/ealtedge/mepm/app_pkgm/v1/app_packages/"
+ ApplcmUri = "/ealtedge/mepm/app_lcm/v1/app_instances/"
+
+ InstantiateUri = "/instantiate"
+ TerminateUri = "/terminate"
+
+ PATHSLASH = "/"
+)
package cmd
import (
-
initCmds "ealt/cmd/init"
"github.com/spf13/cobra"
func init() {
//Adding the various sub-commands of init command
- //Adding all subcommand to init
+ //Adding all subcommand to init
initCmd.AddCommand(initCmds.NewAllCommand())
//ealt init infra
initCmd.AddCommand(initCmds.NewInfraCommand())
package init
import (
-
setup "ealt/cmd/setup"
"github.com/spf13/cobra"
var cmd = &cobra.Command{
Use: "infra",
Short: "Install only infrastructure components on MECM and Edge Node",
- Long: `Command to install only infrastructure components on MECM and Edge Node For Example : ealt init infra`,
+ Long: `Command to install only infrastructure components on MECM and Edge Node For Example : ealt init infra`,
RunE: func(cmd *cobra.Command, args []string) error {
err := setup.EaltInstall("infra")
if err != nil {
*/
package cmd
-
- import (
+import (
"fmt"
"os"
}
func init() {
-}
\ No newline at end of file
+}
// It returns an error if the command outputs anything on the stderr.
package setup
-
import (
"bytes"
"fmt"
}
func runCommandAtShell(command string) (string, error) {
- cmd := &Command{Cmd: exec.Command("sh", "-c", command)}
- err := cmd.ExecuteCmdShowOutput()
- if err != nil {
- return "", err
- }
- errout := cmd.GetStdErr()
- if errout != "" {
- return "", fmt.Errorf("%s", errout)
- }
- return cmd.GetStdOutput(), nil
+ cmd := &Command{Cmd: exec.Command("sh", "-c", command)}
+ err := cmd.ExecuteCmdShowOutput()
+ if err != nil {
+ return "", err
+ }
+ errout := cmd.GetStdErr()
+ if errout != "" {
+ return "", fmt.Errorf("%s", errout)
+ }
+ return cmd.GetStdOutput(), nil
}
*/
package setup
-
import (
"fmt"
)
+
func EaltInstall(component string) error {
var strEaltSetup string
switch component {
case "k8s":
strEaltSetup = fmt.Sprintf("ansible-playbook ealt-all.yml -i ealt-inventory.ini --tags \"k8s\" --extra-vars \"operation=install\"")
case "k3s":
- strEaltSetup = fmt.Sprintf("ansible-playbook ealt-all.yml -i ealt-inventory.ini --tags \"k3s\" --extra-vars \"operation=install\"")
+ strEaltSetup = fmt.Sprintf("ansible-playbook ealt-all.yml -i ealt-inventory.ini --tags \"k3s\" --extra-vars \"operation=install\"")
default:
fmt.Println("Provide subcommand for ealt init [all|infra|manager|edge|k8s|k3s]")
}
case "k8s":
strEaltReset = fmt.Sprintf("ansible-playbook ealt-all-uninstall.yml -i ealt-inventory.ini --tags \"k8s\" --extra-vars \"operation=uninstall\"")
case "k3s":
- strEaltReset = fmt.Sprintf("ansible-playbook ealt-all-uninstall.yml -i ealt-inventory.ini --tags \"k3s\" --extra-vars \"operation=uninstall\"")
+ strEaltReset = fmt.Sprintf("ansible-playbook ealt-all-uninstall.yml -i ealt-inventory.ini --tags \"k3s\" --extra-vars \"operation=uninstall\"")
default:
fmt.Println("Provide subcommand for ealt clean [all|infra|manager|edge|k8s|k3s]")
}
}
fmt.Println(stdout)
return nil
-}
\ No newline at end of file
+}
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
+
package main
import "ealt/cmd"